Skip to content

Commit de3d1a4

Browse files
add global heap hint setter function
1 parent cb68910 commit de3d1a4

5 files changed

Lines changed: 42 additions & 6 deletions

File tree

examples/client/client.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3042,6 +3042,10 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
30423042
!= 0) {
30433043
err_sys("unable to load static memory");
30443044
}
3045+
/* for test case (does not handle all memory used on default build)
3046+
wolfSSL_SetGlobalHeapHint(heap);
3047+
*/
3048+
30453049

30463050
ctx = wolfSSL_CTX_new_ex(method(heap), heap);
30473051
if (ctx == NULL)

examples/server/server.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2518,6 +2518,10 @@ THREAD_RETURN WOLFSSL_THREAD server_test(void* args)
25182518
!= WOLFSSL_SUCCESS)
25192519
err_sys_ex(catastrophic, "unable to load static memory and create ctx");
25202520

2521+
/* for test case (does not handle all memory used on default build)
2522+
wolfSSL_SetGlobalHeapHint(wolfSSL_CTX_GetHeap(ctx, NULL));
2523+
*/
2524+
25212525
/* load in a buffer for IO */
25222526
if (wolfSSL_CTX_load_static_memory(&ctx, NULL, memoryIO, sizeof(memoryIO),
25232527
WOLFMEM_IO_POOL_FIXED | WOLFMEM_TRACK_STATS, 1)

wolfcrypt/src/memory.c

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,17 @@ int wolfSSL_GetMemStats(WOLFSSL_HEAP* heap, WOLFSSL_MEM_STATS* stats)
899899
}
900900

901901

902+
/* global heap hint to fall back on when no heap hint is passed to
903+
* XMALLOC/XFREE
904+
* NOT thread safe, should be set once before any expected XMALLOC XFREE calls
905+
*/
906+
static void* globalHeapHint = NULL;
907+
void wolfSSL_SetGlobalHeapHint(void* heap)
908+
{
909+
globalHeapHint = heap;
910+
}
911+
912+
902913
#ifdef WOLFSSL_DEBUG_MEMORY
903914
void* wolfSSL_Malloc(size_t size, void* heap, int type, const char* func, unsigned int line)
904915
#else
@@ -917,7 +928,7 @@ void* wolfSSL_Malloc(size_t size, void* heap, int type)
917928
#endif
918929

919930
/* if no heap hint then use dynamic memory*/
920-
if (heap == NULL) {
931+
if (heap == NULL && globalHeapHint == NULL) {
921932
#ifdef WOLFSSL_HEAP_TEST
922933
/* allow using malloc for creating ctx and method */
923934
if (type == DYNAMIC_TYPE_CTX || type == DYNAMIC_TYPE_METHOD ||
@@ -952,7 +963,12 @@ void* wolfSSL_Malloc(size_t size, void* heap, int type)
952963
}
953964
else {
954965
WOLFSSL_HEAP_HINT* hint = (WOLFSSL_HEAP_HINT*)heap;
955-
WOLFSSL_HEAP* mem = hint->memory;
966+
WOLFSSL_HEAP* mem;
967+
968+
if (hint == NULL) {
969+
hint = (WOLFSSL_HEAP_HINT*)globalHeapHint;
970+
}
971+
mem = hint->memory;
956972

957973
if (wc_LockMutex(&(mem->memory_mutex)) != 0) {
958974
WOLFSSL_MSG("Bad memory_mutex lock");
@@ -1073,7 +1089,7 @@ void wolfSSL_Free(void *ptr, void* heap, int type)
10731089
}
10741090
#endif
10751091

1076-
if (heap == NULL) {
1092+
if (heap == NULL && globalHeapHint == NULL) {
10771093
#ifdef WOLFSSL_HEAP_TEST
10781094
/* allow using malloc for creating ctx and method */
10791095
if (type == DYNAMIC_TYPE_CTX || type == DYNAMIC_TYPE_METHOD ||
@@ -1098,9 +1114,14 @@ void wolfSSL_Free(void *ptr, void* heap, int type)
10981114
}
10991115
else {
11001116
WOLFSSL_HEAP_HINT* hint = (WOLFSSL_HEAP_HINT*)heap;
1101-
WOLFSSL_HEAP* mem = hint->memory;
1117+
WOLFSSL_HEAP* mem;
11021118
word32 padSz = -(int)sizeof(wc_Memory) & (WOLFSSL_STATIC_ALIGN - 1);
11031119

1120+
if (hint == NULL) {
1121+
hint = (WOLFSSL_HEAP_HINT*)globalHeapHint;
1122+
}
1123+
mem = hint->memory;
1124+
11041125
/* get memory struct and add it to available list */
11051126
pt = (wc_Memory*)((byte*)ptr - sizeof(wc_Memory) - padSz);
11061127
if (wc_LockMutex(&(mem->memory_mutex)) != 0) {
@@ -1181,7 +1202,7 @@ void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type)
11811202
}
11821203
#endif
11831204

1184-
if (heap == NULL) {
1205+
if (heap == NULL && globalHeapHint == NULL) {
11851206
#ifdef WOLFSSL_HEAP_TEST
11861207
WOLFSSL_MSG("ERROR null heap hint passed in to XREALLOC");
11871208
#endif
@@ -1193,9 +1214,14 @@ void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type)
11931214
}
11941215
else {
11951216
WOLFSSL_HEAP_HINT* hint = (WOLFSSL_HEAP_HINT*)heap;
1196-
WOLFSSL_HEAP* mem = hint->memory;
1217+
WOLFSSL_HEAP* mem;
11971218
word32 padSz = -(int)sizeof(wc_Memory) & (WOLFSSL_STATIC_ALIGN - 1);
11981219

1220+
if (hint == NULL) {
1221+
hint = (WOLFSSL_HEAP_HINT*)globalHeapHint;
1222+
}
1223+
mem = hint->memory;
1224+
11991225
if (ptr == NULL) {
12001226
#ifdef WOLFSSL_DEBUG_MEMORY
12011227
return wolfSSL_Malloc(size, heap, type, func, line);

wolfcrypt/test/test.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,7 @@ options: [-s max_relative_stack_bytes] [-m max_relative_heap_memory_bytes]\n\
10591059
printf("unable to load static memory.\n");
10601060
return(EXIT_FAILURE);
10611061
}
1062+
wolfSSL_SetGlobalHeapHint(HEAP_HINT);
10621063
#endif
10631064

10641065
#if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND)

wolfssl/wolfcrypt/memory.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ WOLFSSL_API int wolfSSL_GetAllocators(wolfSSL_Malloc_cb* mf,
240240
byte haFlag; /* flag used for checking handshake count */
241241
} WOLFSSL_HEAP_HINT;
242242

243+
WOLFSSL_API void wolfSSL_SetGlobalHeapHint(void* heap);
243244
WOLFSSL_API int wc_LoadStaticMemory_ex(WOLFSSL_HEAP_HINT** pHint,
244245
unsigned int listSz, const unsigned int *sizeList,
245246
const unsigned int *distList, unsigned char* buf, unsigned int sz,

0 commit comments

Comments
 (0)