@@ -899,6 +899,31 @@ 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+
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 )
912+ {
913+ void * oldHint = globalHeapHint ;
914+
915+ globalHeapHint = heap ;
916+ return oldHint ;
917+ }
918+
919+
920+ /* returns a pointer to the current global heap hint */
921+ void * wolfSSL_GetGlobalHeapHint (void )
922+ {
923+ return globalHeapHint ;
924+ }
925+
926+
902927#ifdef WOLFSSL_DEBUG_MEMORY
903928void * wolfSSL_Malloc (size_t size , void * heap , int type , const char * func , unsigned int line )
904929#else
@@ -917,7 +942,7 @@ void* wolfSSL_Malloc(size_t size, void* heap, int type)
917942#endif
918943
919944 /* if no heap hint then use dynamic memory*/
920- if (heap == NULL ) {
945+ if (heap == NULL && globalHeapHint == NULL ) {
921946 #ifdef WOLFSSL_HEAP_TEST
922947 /* allow using malloc for creating ctx and method */
923948 if (type == DYNAMIC_TYPE_CTX || type == DYNAMIC_TYPE_METHOD ||
@@ -952,7 +977,15 @@ void* wolfSSL_Malloc(size_t size, void* heap, int type)
952977 }
953978 else {
954979 WOLFSSL_HEAP_HINT * hint = (WOLFSSL_HEAP_HINT * )heap ;
955- WOLFSSL_HEAP * mem = hint -> memory ;
980+ WOLFSSL_HEAP * mem ;
981+
982+ if (hint == NULL ) {
983+ hint = (WOLFSSL_HEAP_HINT * )globalHeapHint ;
984+ #ifdef WOLFSSL_DEBUG_MEMORY
985+ fprintf (stderr , "(Using global heap hint %p) " , hint );
986+ #endif
987+ }
988+ mem = hint -> memory ;
956989
957990 if (wc_LockMutex (& (mem -> memory_mutex )) != 0 ) {
958991 WOLFSSL_MSG ("Bad memory_mutex lock" );
@@ -1073,7 +1106,7 @@ void wolfSSL_Free(void *ptr, void* heap, int type)
10731106 }
10741107 #endif
10751108
1076- if (heap == NULL ) {
1109+ if (heap == NULL && globalHeapHint == NULL ) {
10771110 #ifdef WOLFSSL_HEAP_TEST
10781111 /* allow using malloc for creating ctx and method */
10791112 if (type == DYNAMIC_TYPE_CTX || type == DYNAMIC_TYPE_METHOD ||
@@ -1098,9 +1131,17 @@ void wolfSSL_Free(void *ptr, void* heap, int type)
10981131 }
10991132 else {
11001133 WOLFSSL_HEAP_HINT * hint = (WOLFSSL_HEAP_HINT * )heap ;
1101- WOLFSSL_HEAP * mem = hint -> memory ;
1134+ WOLFSSL_HEAP * mem ;
11021135 word32 padSz = - (int )sizeof (wc_Memory ) & (WOLFSSL_STATIC_ALIGN - 1 );
11031136
1137+ if (hint == NULL ) {
1138+ hint = (WOLFSSL_HEAP_HINT * )globalHeapHint ;
1139+ #ifdef WOLFSSL_DEBUG_MEMORY
1140+ fprintf (stderr , "(Using global heap hint %p) " , hint );
1141+ #endif
1142+ }
1143+ mem = hint -> memory ;
1144+
11041145 /* get memory struct and add it to available list */
11051146 pt = (wc_Memory * )((byte * )ptr - sizeof (wc_Memory ) - padSz );
11061147 if (wc_LockMutex (& (mem -> memory_mutex )) != 0 ) {
@@ -1181,7 +1222,7 @@ void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type)
11811222 }
11821223#endif
11831224
1184- if (heap == NULL ) {
1225+ if (heap == NULL && globalHeapHint == NULL ) {
11851226 #ifdef WOLFSSL_HEAP_TEST
11861227 WOLFSSL_MSG ("ERROR null heap hint passed in to XREALLOC" );
11871228 #endif
@@ -1193,9 +1234,17 @@ void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type)
11931234 }
11941235 else {
11951236 WOLFSSL_HEAP_HINT * hint = (WOLFSSL_HEAP_HINT * )heap ;
1196- WOLFSSL_HEAP * mem = hint -> memory ;
1237+ WOLFSSL_HEAP * mem ;
11971238 word32 padSz = - (int )sizeof (wc_Memory ) & (WOLFSSL_STATIC_ALIGN - 1 );
11981239
1240+ if (hint == NULL ) {
1241+ hint = (WOLFSSL_HEAP_HINT * )globalHeapHint ;
1242+ #ifdef WOLFSSL_DEBUG_MEMORY
1243+ fprintf (stderr , "(Using global heap hint %p) " , hint );
1244+ #endif
1245+ }
1246+ mem = hint -> memory ;
1247+
11991248 if (ptr == NULL ) {
12001249 #ifdef WOLFSSL_DEBUG_MEMORY
12011250 return wolfSSL_Malloc (size , heap , type , func , line );
0 commit comments