Skip to content

Commit 98a19f9

Browse files
add debug log and adjust set global heap hint function
1 parent de3d1a4 commit 98a19f9

5 files changed

Lines changed: 31 additions & 9 deletions

File tree

examples/client/client.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3042,10 +3042,6 @@ 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-
30493045

30503046
ctx = wolfSSL_CTX_new_ex(method(heap), heap);
30513047
if (ctx == NULL)

examples/server/server.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2518,10 +2518,6 @@ 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-
25252521
/* load in a buffer for IO */
25262522
if (wolfSSL_CTX_load_static_memory(&ctx, NULL, memoryIO, sizeof(memoryIO),
25272523
WOLFMEM_IO_POOL_FIXED | WOLFMEM_TRACK_STATS, 1)

wolfcrypt/src/memory.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -904,9 +904,23 @@ int wolfSSL_GetMemStats(WOLFSSL_HEAP* heap, WOLFSSL_MEM_STATS* stats)
904904
* NOT thread safe, should be set once before any expected XMALLOC XFREE calls
905905
*/
906906
static void* globalHeapHint = NULL;
907-
void wolfSSL_SetGlobalHeapHint(void* heap)
907+
908+
909+
/* Used to set a new global heap hint. Returns a pointer to the current global
910+
* heap hint before being set. */
911+
void* wolfSSL_SetGlobalHeapHint(void* heap)
908912
{
913+
void *oldHint = globalHeapHint;
914+
909915
globalHeapHint = heap;
916+
return oldHint;
917+
}
918+
919+
920+
/* returns a pointer to the current global heap hint */
921+
void* wolfSSL_GetGlobalHeapHint()
922+
{
923+
return globalHeapHint;
910924
}
911925

912926

@@ -967,6 +981,9 @@ void* wolfSSL_Malloc(size_t size, void* heap, int type)
967981

968982
if (hint == NULL) {
969983
hint = (WOLFSSL_HEAP_HINT*)globalHeapHint;
984+
#ifdef WOLFSSL_DEBUG_MEMORY
985+
fprintf(stderr, "(Using global heap hint %p) ", hint);
986+
#endif
970987
}
971988
mem = hint->memory;
972989

@@ -1119,6 +1136,9 @@ void wolfSSL_Free(void *ptr, void* heap, int type)
11191136

11201137
if (hint == NULL) {
11211138
hint = (WOLFSSL_HEAP_HINT*)globalHeapHint;
1139+
#ifdef WOLFSSL_DEBUG_MEMORY
1140+
fprintf(stderr, "(Using global heap hint %p) ", hint);
1141+
#endif
11221142
}
11231143
mem = hint->memory;
11241144

@@ -1219,6 +1239,9 @@ void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type)
12191239

12201240
if (hint == NULL) {
12211241
hint = (WOLFSSL_HEAP_HINT*)globalHeapHint;
1242+
#ifdef WOLFSSL_DEBUG_MEMORY
1243+
fprintf(stderr, "(Using global heap hint %p) ", hint);
1244+
#endif
12221245
}
12231246
mem = hint->memory;
12241247

wolfcrypt/test/test.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,7 +1059,9 @@ 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+
#ifndef OPENSSL_EXTRA
10621063
wolfSSL_SetGlobalHeapHint(HEAP_HINT);
1064+
#endif
10631065
#endif
10641066

10651067
#if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND)
@@ -2014,6 +2016,9 @@ options: [-s max_relative_stack_bytes] [-m max_relative_heap_memory_bytes]\n\
20142016
#endif
20152017
#endif
20162018

2019+
#ifndef OPENSSL_EXTRA
2020+
wolfSSL_SetGlobalHeapHint(NULL);
2021+
#endif
20172022
TEST_PASS("Test complete\n");
20182023

20192024
EXIT_TEST(ret);

wolfssl/wolfcrypt/memory.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ WOLFSSL_API int wolfSSL_GetAllocators(wolfSSL_Malloc_cb* mf,
241241
} WOLFSSL_HEAP_HINT;
242242

243243
WOLFSSL_API void wolfSSL_SetGlobalHeapHint(void* heap);
244+
WOLFSSL_API void* wolfSSL_SetGlobalHeapHint(void* heap);
245+
WOLFSSL_API void* wolfSSL_GetGlobalHeapHint(void);
244246
WOLFSSL_API int wc_LoadStaticMemory_ex(WOLFSSL_HEAP_HINT** pHint,
245247
unsigned int listSz, const unsigned int *sizeList,
246248
const unsigned int *distList, unsigned char* buf, unsigned int sz,

0 commit comments

Comments
 (0)