Memory Callback

1. Allow SetAllocators to use NULL as a callback. Memory wrappers will use default with NULL.
2. Remove the ResetAllocators function. Use SetAllocators with NULL.
3. Modify memory tracker to save the old allocators on init and restore them on cleanup.
This commit is contained in:
John Safranek
2018-07-31 09:24:44 -07:00
parent 2b3f94944d
commit 50372b7033
4 changed files with 15 additions and 44 deletions

View File

@ -66,33 +66,9 @@ int wolfSSL_SetAllocators(wolfSSL_Malloc_cb mf,
wolfSSL_Free_cb ff,
wolfSSL_Realloc_cb rf)
{
int res = 0;
if (mf)
malloc_function = mf;
else
res = BAD_FUNC_ARG;
if (ff)
free_function = ff;
else
res = BAD_FUNC_ARG;
if (rf)
realloc_function = rf;
else
res = BAD_FUNC_ARG;
return res;
}
int wolfSSL_ResetAllocators(void)
{
/* allow nulls to be set for callbacks to restore defaults */
malloc_function = NULL;
free_function = NULL;
realloc_function = NULL;
malloc_function = mf;
free_function = ff;
realloc_function = rf;
return 0;
}

View File

@ -19585,20 +19585,6 @@ int memcb_test(void)
XFREE(b, NULL, DYNAMIC_TYPE_TMP_BUFFER);
b = NULL;
/* Parameter Validation testing. */
if (wolfSSL_SetAllocators(NULL, (wolfSSL_Free_cb)&my_Free_cb,
(wolfSSL_Realloc_cb)&my_Realloc_cb) != BAD_FUNC_ARG) {
ERROR_OUT(-10002, exit_memcb);
}
if (wolfSSL_SetAllocators((wolfSSL_Malloc_cb)&my_Malloc_cb, NULL,
(wolfSSL_Realloc_cb)&my_Realloc_cb) != BAD_FUNC_ARG) {
ERROR_OUT(-10003, exit_memcb);
}
if (wolfSSL_SetAllocators((wolfSSL_Malloc_cb)&my_Malloc_cb,
(wolfSSL_Free_cb)&my_Free_cb, NULL) != BAD_FUNC_ARG) {
ERROR_OUT(-10004, exit_memcb);
}
/* Use API. */
if (wolfSSL_SetAllocators((wolfSSL_Malloc_cb)&my_Malloc_cb,
(wolfSSL_Free_cb)&my_Free_cb, (wolfSSL_Realloc_cb)my_Realloc_cb) != 0) {

View File

@ -308,9 +308,19 @@
}
#ifdef WOLFSSL_TRACK_MEMORY
static wolfSSL_Malloc_cb mfDefault = NULL;
static wolfSSL_Free_cb ffDefault = NULL;
static wolfSSL_Realloc_cb rfDefault = NULL;
STATIC WC_INLINE int InitMemoryTracker(void)
{
int ret = wolfSSL_SetAllocators(TrackMalloc, TrackFree, TrackRealloc);
int ret;
ret = wolfSSL_GetAllocators(&mfDefault, &ffDefault, &rfDefault);
if (ret < 0) {
printf("wolfSSL GetAllocators failed to get the defaults\n");
}
ret = wolfSSL_SetAllocators(TrackMalloc, TrackFree, TrackRealloc);
if (ret < 0) {
printf("wolfSSL SetAllocators failed for track memory\n");
return ret;
@ -380,7 +390,7 @@
STATIC WC_INLINE int CleanupMemoryTracker(void)
{
/* restore default allocators */
return wolfSSL_ResetAllocators();
return wolfSSL_SetAllocators(mfDefault, ffDefault, rfDefault);
}
#endif

View File

@ -81,7 +81,6 @@
WOLFSSL_API int wolfSSL_SetAllocators(wolfSSL_Malloc_cb,
wolfSSL_Free_cb,
wolfSSL_Realloc_cb);
WOLFSSL_API int wolfSSL_ResetAllocators(void);
WOLFSSL_API int wolfSSL_GetAllocators(wolfSSL_Malloc_cb*,
wolfSSL_Free_cb*,
wolfSSL_Realloc_cb*);