From bcf53cf8d02fbefb59abdc89ee360f024a1aa3d3 Mon Sep 17 00:00:00 2001 From: Brian Wellington Date: Wed, 7 Nov 2001 23:03:54 +0000 Subject: [PATCH] Move the memory allocation wrappers into openssl_link.c, since they're openssl specific. --- lib/dns/sec/dst/dst_api.c | 46 ++++++------------------------ lib/dns/sec/dst/dst_internal.h | 5 +++- lib/dns/sec/dst/openssl_link.c | 52 ++++++++++++++++++++++++++++------ 3 files changed, 56 insertions(+), 47 deletions(-) diff --git a/lib/dns/sec/dst/dst_api.c b/lib/dns/sec/dst/dst_api.c index e7c82f7b92..35b85da33f 100644 --- a/lib/dns/sec/dst/dst_api.c +++ b/lib/dns/sec/dst/dst_api.c @@ -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 @@ -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; diff --git a/lib/dns/sec/dst/dst_internal.h b/lib/dns/sec/dst/dst_internal.h index 532d669146..fb29b3a354 100644 --- a/lib/dns/sec/dst/dst_internal.h +++ b/lib/dns/sec/dst/dst_internal.h @@ -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 #include #include +#include #include @@ -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 ***/ diff --git a/lib/dns/sec/dst/openssl_link.c b/lib/dns/sec/dst/openssl_link.c index 5df661e902..78f5742f1e 100644 --- a/lib/dns/sec/dst/openssl_link.c +++ b/lib/dns/sec/dst/openssl_link.c @@ -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 #include #include @@ -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 */