forked from wolfSSL/wolfssl
Generic Memory Pools
1. Modify wolfSSL_CTX_load_static_memory() to use wc_LoadStaticMemory() instead of reimplementing it. 2. Initialize the pointers in wc_LoadStaticMemory() to null. 3. Whitespace changes.
This commit is contained in:
52
src/ssl.c
52
src/ssl.c
@@ -2572,9 +2572,7 @@ int wolfSSL_CTX_load_static_memory(WOLFSSL_CTX** ctx,
|
|||||||
wolfSSL_method_func method, unsigned char* buf, unsigned int sz, int flag,
|
wolfSSL_method_func method, unsigned char* buf, unsigned int sz, int flag,
|
||||||
int maxSz)
|
int maxSz)
|
||||||
{
|
{
|
||||||
WOLFSSL_HEAP* heap;
|
WOLFSSL_HEAP_HINT* hint = NULL;
|
||||||
WOLFSSL_HEAP_HINT* hint;
|
|
||||||
word32 idx = 0;
|
|
||||||
|
|
||||||
if (ctx == NULL || buf == NULL) {
|
if (ctx == NULL || buf == NULL) {
|
||||||
return BAD_FUNC_ARG;
|
return BAD_FUNC_ARG;
|
||||||
@@ -2584,42 +2582,23 @@ int wolfSSL_CTX_load_static_memory(WOLFSSL_CTX** ctx,
|
|||||||
return BAD_FUNC_ARG;
|
return BAD_FUNC_ARG;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (*ctx == NULL || (*ctx)->heap == NULL) {
|
/* If there is a heap already, capture it in hint. */
|
||||||
if (sizeof(WOLFSSL_HEAP) + sizeof(WOLFSSL_HEAP_HINT) > sz - idx) {
|
if (*ctx && (*ctx)->heap != NULL) {
|
||||||
return BUFFER_E; /* not enough memory for structures */
|
hint = (*ctx)->heap;
|
||||||
}
|
}
|
||||||
heap = (WOLFSSL_HEAP*)buf;
|
|
||||||
idx += sizeof(WOLFSSL_HEAP);
|
if (wc_LoadStaticMemory(&hint, buf, sz, flag, maxSz)) {
|
||||||
if (wolfSSL_init_memory_heap(heap) != 0) {
|
WOLFSSL_MSG("Error loading static memory");
|
||||||
return WOLFSSL_FAILURE;
|
return WOLFSSL_FAILURE;
|
||||||
}
|
}
|
||||||
hint = (WOLFSSL_HEAP_HINT*)(buf + idx);
|
|
||||||
idx += sizeof(WOLFSSL_HEAP_HINT);
|
|
||||||
XMEMSET(hint, 0, sizeof(WOLFSSL_HEAP_HINT));
|
|
||||||
hint->memory = heap;
|
|
||||||
|
|
||||||
if (*ctx && (*ctx)->heap == NULL) {
|
if (*ctx) {
|
||||||
|
if ((*ctx)->heap == NULL) {
|
||||||
(*ctx)->heap = (void*)hint;
|
(*ctx)->heap = (void*)hint;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
#ifdef WOLFSSL_HEAP_TEST
|
|
||||||
/* do not load in memory if test has been set */
|
|
||||||
if ((*ctx)->heap == (void*)WOLFSSL_HEAP_TEST) {
|
|
||||||
return WOLFSSL_SUCCESS;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
hint = (WOLFSSL_HEAP_HINT*)((*ctx)->heap);
|
|
||||||
heap = hint->memory;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (wolfSSL_load_static_memory(buf + idx, sz - idx, flag, heap) != 1) {
|
|
||||||
WOLFSSL_MSG("Error partitioning memory");
|
|
||||||
return WOLFSSL_FAILURE;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* create ctx if needed */
|
/* create ctx if needed */
|
||||||
if (*ctx == NULL) {
|
|
||||||
*ctx = wolfSSL_CTX_new_ex(method(hint), hint);
|
*ctx = wolfSSL_CTX_new_ex(method(hint), hint);
|
||||||
if (*ctx == NULL) {
|
if (*ctx == NULL) {
|
||||||
WOLFSSL_MSG("Error creating ctx");
|
WOLFSSL_MSG("Error creating ctx");
|
||||||
@@ -2627,19 +2606,6 @@ int wolfSSL_CTX_load_static_memory(WOLFSSL_CTX** ctx,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* determine what max applies too */
|
|
||||||
if (flag & WOLFMEM_IO_POOL || flag & WOLFMEM_IO_POOL_FIXED) {
|
|
||||||
heap->maxIO = maxSz;
|
|
||||||
}
|
|
||||||
else { /* general memory used in handshakes */
|
|
||||||
heap->maxHa = maxSz;
|
|
||||||
}
|
|
||||||
|
|
||||||
heap->flag |= flag;
|
|
||||||
|
|
||||||
(void)maxSz;
|
|
||||||
(void)method;
|
|
||||||
|
|
||||||
return WOLFSSL_SUCCESS;
|
return WOLFSSL_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -587,10 +587,10 @@ int wolfSSL_init_memory_heap(WOLFSSL_HEAP* heap)
|
|||||||
int wc_LoadStaticMemory(WOLFSSL_HEAP_HINT** pHint,
|
int wc_LoadStaticMemory(WOLFSSL_HEAP_HINT** pHint,
|
||||||
unsigned char* buf, unsigned int sz, int flag, int maxSz)
|
unsigned char* buf, unsigned int sz, int flag, int maxSz)
|
||||||
{
|
{
|
||||||
int ret;
|
WOLFSSL_HEAP* heap = NULL;
|
||||||
WOLFSSL_HEAP* heap;
|
WOLFSSL_HEAP_HINT* hint = NULL;
|
||||||
WOLFSSL_HEAP_HINT* hint;
|
|
||||||
word32 idx = 0;
|
word32 idx = 0;
|
||||||
|
int ret;
|
||||||
|
|
||||||
if (pHint == NULL || buf == NULL) {
|
if (pHint == NULL || buf == NULL) {
|
||||||
return BAD_FUNC_ARG;
|
return BAD_FUNC_ARG;
|
||||||
|
Reference in New Issue
Block a user