diff --git a/examples/client/client.c b/examples/client/client.c index c577562235..7c2aa674d4 100644 --- a/examples/client/client.c +++ b/examples/client/client.c @@ -3042,10 +3042,6 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args) != 0) { err_sys("unable to load static memory"); } - /* for test case (does not handle all memory used on default build) - wolfSSL_SetGlobalHeapHint(heap); - */ - ctx = wolfSSL_CTX_new_ex(method(heap), heap); if (ctx == NULL) diff --git a/examples/server/server.c b/examples/server/server.c index 2572a185e4..6f0faf3385 100644 --- a/examples/server/server.c +++ b/examples/server/server.c @@ -2518,10 +2518,6 @@ THREAD_RETURN WOLFSSL_THREAD server_test(void* args) != WOLFSSL_SUCCESS) err_sys_ex(catastrophic, "unable to load static memory and create ctx"); - /* for test case (does not handle all memory used on default build) - wolfSSL_SetGlobalHeapHint(wolfSSL_CTX_GetHeap(ctx, NULL)); - */ - /* load in a buffer for IO */ if (wolfSSL_CTX_load_static_memory(&ctx, NULL, memoryIO, sizeof(memoryIO), WOLFMEM_IO_POOL_FIXED | WOLFMEM_TRACK_STATS, 1) diff --git a/wolfcrypt/src/memory.c b/wolfcrypt/src/memory.c index 884bfafc06..2e30c4c818 100644 --- a/wolfcrypt/src/memory.c +++ b/wolfcrypt/src/memory.c @@ -904,9 +904,23 @@ int wolfSSL_GetMemStats(WOLFSSL_HEAP* heap, WOLFSSL_MEM_STATS* stats) * NOT thread safe, should be set once before any expected XMALLOC XFREE calls */ static void* globalHeapHint = NULL; -void wolfSSL_SetGlobalHeapHint(void* heap) + + +/* Used to set a new global heap hint. Returns a pointer to the current global + * heap hint before being set. */ +void* wolfSSL_SetGlobalHeapHint(void* heap) { + void *oldHint = globalHeapHint; + globalHeapHint = heap; + return oldHint; +} + + +/* returns a pointer to the current global heap hint */ +void* wolfSSL_GetGlobalHeapHint() +{ + return globalHeapHint; } @@ -967,6 +981,9 @@ void* wolfSSL_Malloc(size_t size, void* heap, int type) if (hint == NULL) { hint = (WOLFSSL_HEAP_HINT*)globalHeapHint; + #ifdef WOLFSSL_DEBUG_MEMORY + fprintf(stderr, "(Using global heap hint %p) ", hint); + #endif } mem = hint->memory; @@ -1119,6 +1136,9 @@ void wolfSSL_Free(void *ptr, void* heap, int type) if (hint == NULL) { hint = (WOLFSSL_HEAP_HINT*)globalHeapHint; + #ifdef WOLFSSL_DEBUG_MEMORY + fprintf(stderr, "(Using global heap hint %p) ", hint); + #endif } mem = hint->memory; @@ -1219,6 +1239,9 @@ void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type) if (hint == NULL) { hint = (WOLFSSL_HEAP_HINT*)globalHeapHint; + #ifdef WOLFSSL_DEBUG_MEMORY + fprintf(stderr, "(Using global heap hint %p) ", hint); + #endif } mem = hint->memory; diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index e1d6387f10..3a34454a77 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -1059,7 +1059,9 @@ options: [-s max_relative_stack_bytes] [-m max_relative_heap_memory_bytes]\n\ printf("unable to load static memory.\n"); return(EXIT_FAILURE); } + #ifndef OPENSSL_EXTRA wolfSSL_SetGlobalHeapHint(HEAP_HINT); + #endif #endif #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND) @@ -2014,6 +2016,9 @@ options: [-s max_relative_stack_bytes] [-m max_relative_heap_memory_bytes]\n\ #endif #endif +#ifndef OPENSSL_EXTRA + wolfSSL_SetGlobalHeapHint(NULL); +#endif TEST_PASS("Test complete\n"); EXIT_TEST(ret); diff --git a/wolfssl/wolfcrypt/memory.h b/wolfssl/wolfcrypt/memory.h index 9af3c728f2..87f277c70c 100644 --- a/wolfssl/wolfcrypt/memory.h +++ b/wolfssl/wolfcrypt/memory.h @@ -241,6 +241,8 @@ WOLFSSL_API int wolfSSL_GetAllocators(wolfSSL_Malloc_cb* mf, } WOLFSSL_HEAP_HINT; WOLFSSL_API void wolfSSL_SetGlobalHeapHint(void* heap); + WOLFSSL_API void* wolfSSL_SetGlobalHeapHint(void* heap); + WOLFSSL_API void* wolfSSL_GetGlobalHeapHint(void); WOLFSSL_API int wc_LoadStaticMemory_ex(WOLFSSL_HEAP_HINT** pHint, unsigned int listSz, const unsigned int *sizeList, const unsigned int *distList, unsigned char* buf, unsigned int sz,