diff options
author | Davide Galassi <davxy@datawok.net> | 2020-02-10 16:49:10 +1000 |
---|---|---|
committer | Pauli <paul.dale@oracle.com> | 2020-02-10 16:49:10 +1000 |
commit | f4dcc09ba71c0a51771311428c20634f574ccf12 (patch) | |
tree | 3226ec1675e2f410bc9d3477778f40260d108629 /crypto/mem.c | |
parent | 96f0b8adddc4ffc8599c60b5755b0d9c19a9c84b (diff) |
Memory allocator code cleanup
Typedefs of CRYPTO malloc, realloc and free.
MEM_CHECK "modes" are used only as a CRYPTO_mem_ctrl() parameter
The CRYPTO_mem_ctrl is defined only if OPENSSL_NO_CRYPTO_MDEBUG is
defined, thus define the MEM_CHECK modes under the same condition.
Maybe the macros can be removed at all since:
1. CRYPTO_mem_ctrl() just returns -1 and ignores the parameter
2. CRYPTO_mem_ctr() is declared as DEPRECATED by 3.0
Reviewed-by: Richard Levitte <levitte@openssl.org>
Reviewed-by: Paul Dale <paul.dale@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/11042)
Diffstat (limited to 'crypto/mem.c')
-rw-r--r-- | crypto/mem.c | 66 |
1 files changed, 28 insertions, 38 deletions
diff --git a/crypto/mem.c b/crypto/mem.c index 640107be40..ad64fda5e2 100644 --- a/crypto/mem.c +++ b/crypto/mem.c @@ -19,13 +19,9 @@ * the following pointers may be changed as long as 'allow_customize' is set */ static int allow_customize = 1; - -static void *(*malloc_impl)(size_t, const char *, int) - = CRYPTO_malloc; -static void *(*realloc_impl)(void *, size_t, const char *, int) - = CRYPTO_realloc; -static void (*free_impl)(void *, const char *, int) - = CRYPTO_free; +static CRYPTO_malloc_fn malloc_impl = CRYPTO_malloc; +static CRYPTO_realloc_fn realloc_impl = CRYPTO_realloc; +static CRYPTO_free_fn free_impl = CRYPTO_free; #if !defined(OPENSSL_NO_CRYPTO_MDEBUG) && !defined(FIPS_MODE) # include "internal/tsan_assist.h" @@ -52,33 +48,31 @@ static int shouldfail(void); # define FAILTEST() /* empty */ #endif -int CRYPTO_set_mem_functions( - void *(*m)(size_t, const char *, int), - void *(*r)(void *, size_t, const char *, int), - void (*f)(void *, const char *, int)) +int CRYPTO_set_mem_functions(CRYPTO_malloc_fn malloc_fn, + CRYPTO_realloc_fn realloc_fn, + CRYPTO_free_fn free_fn) { if (!allow_customize) return 0; - if (m) - malloc_impl = m; - if (r) - realloc_impl = r; - if (f) - free_impl = f; + if (malloc_fn != NULL) + malloc_impl = malloc_fn; + if (realloc_fn != NULL) + realloc_impl = realloc_fn; + if (free_fn != NULL) + free_impl = free_fn; return 1; } -void CRYPTO_get_mem_functions( - void *(**m)(size_t, const char *, int), - void *(**r)(void *, size_t, const char *, int), - void (**f)(void *, const char *, int)) +void CRYPTO_get_mem_functions(CRYPTO_malloc_fn *malloc_fn, + CRYPTO_realloc_fn *realloc_fn, + CRYPTO_free_fn *free_fn) { - if (m != NULL) - *m = malloc_impl; - if (r != NULL) - *r = realloc_impl; - if (f != NULL) - *f = free_impl; + if (malloc_fn != NULL) + *malloc_fn = malloc_impl; + if (realloc_fn != NULL) + *realloc_fn = realloc_impl; + if (free_fn != NULL) + *free_fn = free_impl; } #if !defined(OPENSSL_NO_CRYPTO_MDEBUG) && !defined(FIPS_MODE) @@ -170,10 +164,8 @@ void ossl_malloc_setup_failures(void) void *CRYPTO_malloc(size_t num, const char *file, int line) { - void *ret = NULL; - INCREMENT(malloc_count); - if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc) + if (malloc_impl != CRYPTO_malloc) return malloc_impl(num, file, line); if (num == 0) @@ -188,26 +180,26 @@ void *CRYPTO_malloc(size_t num, const char *file, int line) */ allow_customize = 0; } - (void)(file); (void)(line); - ret = malloc(num); - return ret; + return malloc(num); } void *CRYPTO_zalloc(size_t num, const char *file, int line) { - void *ret = CRYPTO_malloc(num, file, line); + void *ret; + ret = CRYPTO_malloc(num, file, line); FAILTEST(); if (ret != NULL) memset(ret, 0, num); + return ret; } void *CRYPTO_realloc(void *str, size_t num, const char *file, int line) { INCREMENT(realloc_count); - if (realloc_impl != NULL && realloc_impl != &CRYPTO_realloc) + if (realloc_impl != CRYPTO_realloc) return realloc_impl(str, num, file, line); FAILTEST(); @@ -219,9 +211,7 @@ void *CRYPTO_realloc(void *str, size_t num, const char *file, int line) return NULL; } - (void)(file); (void)(line); return realloc(str, num); - } void *CRYPTO_clear_realloc(void *str, size_t old_len, size_t num, @@ -254,7 +244,7 @@ void *CRYPTO_clear_realloc(void *str, size_t old_len, size_t num, void CRYPTO_free(void *str, const char *file, int line) { INCREMENT(free_count); - if (free_impl != NULL && free_impl != &CRYPTO_free) { + if (free_impl != CRYPTO_free) { free_impl(str, file, line); return; } |