2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-31 14:35:26 +00:00

Move the memory allocation wrappers into openssl_link.c, since they're

openssl specific.
This commit is contained in:
Brian Wellington
2001-11-07 23:03:54 +00:00
parent 66923d616a
commit bcf53cf8d0
3 changed files with 56 additions and 47 deletions

View File

@@ -19,7 +19,7 @@
/*
* Principal Author: Brian Wellington
* $Id: dst_api.c,v 1.93 2001/11/06 22:27:52 bwelling Exp $
* $Id: dst_api.c,v 1.94 2001/11/07 23:03:52 bwelling Exp $
*/
#include <config.h>
@@ -51,11 +51,12 @@
#include "dst_internal.h"
static dst_func_t *dst_t_func[DST_MAX_ALGS];
static isc_mem_t *dst_memory_pool = NULL;
static isc_entropy_t *dst_entropy_pool = NULL;
static unsigned int dst_entropy_flags = 0;
static isc_boolean_t dst_initialized = ISC_FALSE;
isc_mem_t *dst__memory_pool = NULL;
/*
* Static functions.
*/
@@ -111,7 +112,7 @@ dst_lib_init(isc_mem_t *mctx, isc_entropy_t *ectx, unsigned int eflags) {
REQUIRE(mctx != NULL && ectx != NULL);
REQUIRE(dst_initialized == ISC_FALSE);
dst_memory_pool = NULL;
dst__memory_pool = NULL;
#ifdef OPENSSL
UNUSED(mctx);
@@ -121,12 +122,12 @@ dst_lib_init(isc_mem_t *mctx, isc_entropy_t *ectx, unsigned int eflags) {
* Avoid assertions by using a local memory context and not checking
* for leaks on exit.
*/
result = isc_mem_create(0, 0, &dst_memory_pool);
result = isc_mem_create(0, 0, &dst__memory_pool);
if (result != ISC_R_SUCCESS)
return (result);
isc_mem_setdestroycheck(dst_memory_pool, ISC_FALSE);
isc_mem_setdestroycheck(dst__memory_pool, ISC_FALSE);
#else
isc_mem_attach(mctx, &dst_memory_pool);
isc_mem_attach(mctx, &dst__memory_pool);
#endif
isc_entropy_attach(ectx, &dst_entropy_pool);
dst_entropy_flags = eflags;
@@ -165,8 +166,8 @@ dst_lib_destroy(void) {
#ifdef OPENSSL
dst__openssl_destroy();
#endif
if (dst_memory_pool != NULL)
isc_mem_detach(&dst_memory_pool);
if (dst__memory_pool != NULL)
isc_mem_detach(&dst__memory_pool);
if (dst_entropy_pool != NULL)
isc_entropy_detach(&dst_entropy_pool);
@@ -1108,35 +1109,6 @@ dst__file_addsuffix(char *filename, unsigned int len,
return (ISC_R_SUCCESS);
}
void *
dst__mem_alloc(size_t size) {
INSIST(dst_memory_pool != NULL);
return (isc_mem_allocate(dst_memory_pool, size));
}
void
dst__mem_free(void *ptr) {
INSIST(dst_memory_pool != NULL);
if (ptr != NULL)
isc_mem_free(dst_memory_pool, ptr);
}
void *
dst__mem_realloc(void *ptr, size_t size) {
void *p;
INSIST(dst_memory_pool != NULL);
p = NULL;
if (size > 0) {
p = dst__mem_alloc(size);
if (p != NULL && ptr != NULL)
memcpy(p, ptr, size);
}
if (ptr != NULL)
dst__mem_free(ptr);
return (p);
}
isc_result_t
dst__entropy_getdata(void *buf, unsigned int len, isc_boolean_t pseudo) {
unsigned int flags = dst_entropy_flags;

View File

@@ -17,7 +17,7 @@
* PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: dst_internal.h,v 1.39 2001/11/06 20:47:53 bwelling Exp $ */
/* $Id: dst_internal.h,v 1.40 2001/11/07 23:03:53 bwelling Exp $ */
#ifndef DST_DST_INTERNAL_H
#define DST_DST_INTERNAL_H 1
@@ -27,6 +27,7 @@
#include <isc/int.h>
#include <isc/magic.h>
#include <isc/region.h>
#include <isc/types.h>
#include <dst/dst.h>
@@ -38,6 +39,8 @@ ISC_LANG_BEGINDECLS
#define VALID_KEY(x) ISC_MAGIC_VALID(x, KEY_MAGIC)
#define VALID_CTX(x) ISC_MAGIC_VALID(x, CTX_MAGIC)
extern isc_mem_t *dst__memory_pool;
/***
*** Types
***/

View File

@@ -19,7 +19,7 @@
/*
* Principal Author: Brian Wellington
* $Id: openssl_link.c,v 1.46 2001/07/31 03:45:04 marka Exp $
* $Id: openssl_link.c,v 1.47 2001/11/07 23:03:54 bwelling Exp $
*/
#ifdef OPENSSL
@@ -35,6 +35,7 @@
#include "dst_internal.h"
#include <openssl/err.h>
#include <openssl/rand.h>
#include <openssl/crypto.h>
@@ -94,14 +95,42 @@ id_callback(void) {
return ((unsigned long)isc_thread_self());
}
static void *
mem_alloc(size_t size) {
INSIST(dst__memory_pool != NULL);
return (isc_mem_allocate(dst__memory_pool, size));
}
static void
mem_free(void *ptr) {
INSIST(dst__memory_pool != NULL);
if (ptr != NULL)
isc_mem_free(dst__memory_pool, ptr);
}
static void *
mem_realloc(void *ptr, size_t size) {
void *p;
INSIST(dst__memory_pool != NULL);
p = NULL;
if (size > 0) {
p = mem_alloc(size);
if (p != NULL && ptr != NULL)
memcpy(p, ptr, size);
}
if (ptr != NULL)
mem_free(ptr);
return (p);
}
isc_result_t
dst__openssl_init() {
isc_result_t result;
CRYPTO_set_mem_functions(dst__mem_alloc, dst__mem_realloc,
dst__mem_free);
CRYPTO_set_mem_functions(mem_alloc, mem_realloc, mem_free);
nlocks = CRYPTO_num_locks();
locks = dst__mem_alloc(sizeof(isc_mutex_t) * nlocks);
locks = mem_alloc(sizeof(isc_mutex_t) * nlocks);
if (locks == NULL)
return (ISC_R_NOMEMORY);
result = isc_mutexblock_init(locks, nlocks);
@@ -109,7 +138,7 @@ dst__openssl_init() {
goto cleanup_mutexalloc;
CRYPTO_set_locking_callback(lock_callback);
CRYPTO_set_id_callback(id_callback);
rm = dst__mem_alloc(sizeof(RAND_METHOD));
rm = mem_alloc(sizeof(RAND_METHOD));
if (rm == NULL) {
result = ISC_R_NOMEMORY;
goto cleanup_mutexinit;
@@ -135,24 +164,29 @@ dst__openssl_init() {
#ifdef CRYPTO_LOCK_ENGINE
cleanup_rm:
dst__mem_free(rm);
mem_free(rm);
#endif
cleanup_mutexinit:
RUNTIME_CHECK(isc_mutexblock_destroy(locks, nlocks) == ISC_R_SUCCESS);
cleanup_mutexalloc:
dst__mem_free(locks);
mem_free(locks);
return (result);
}
void
dst__openssl_destroy() {
ERR_clear_error();
#ifdef CRYPTO_LOCK_ENGINE
if (e != NULL)
ENGINE_free(e);
#endif
if (locks != NULL) {
RUNTIME_CHECK(isc_mutexblock_destroy(locks, nlocks) ==
ISC_R_SUCCESS);
dst__mem_free(locks);
mem_free(locks);
}
if (rm != NULL)
dst__mem_free(rm);
mem_free(rm);
}
#endif /* OPENSSL */