forked from wolfSSL/wolfssl
Added support for static memory with wolfCrypt. Adds new "wc_LoadStaticMemory" function and moves "wolfSSL_init_memory_heap" into wolfCrypt layer. Enhanced wolfCrypt test and benchmark to use the static memory tool if enabled. Added support for static memory with "WOLFSSL_DEBUG_MEMORY" defined. Fixed issue with have-iopool and XMALLOC/XFREE. Added check to prevent using WOLFSSL_STATIC_MEMORY with HAVE_IO_POOL, XMALLOC_USER or NO_WOLFSSL_MEMORY defined.
This commit is contained in:
@ -659,7 +659,7 @@ THREAD_RETURN CYASSL_THREAD server_test(void* args)
|
||||
err_sys("unable to load static memory and create ctx");
|
||||
#else
|
||||
ctx = SSL_CTX_new(method(NULL));
|
||||
#endif
|
||||
#endif /* WOLFSSL_STATIC_MEMORY */
|
||||
if (ctx == NULL)
|
||||
err_sys("unable to get ctx");
|
||||
|
||||
|
51
src/ssl.c
51
src/ssl.c
@ -638,32 +638,10 @@ int wolfSSL_GetObjectSize(void)
|
||||
return sizeof(WOLFSSL);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef WOLFSSL_STATIC_MEMORY
|
||||
|
||||
|
||||
int wolfSSL_init_memory_heap(WOLFSSL_HEAP* heap)
|
||||
{
|
||||
word32 wc_MemSz[WOLFMEM_DEF_BUCKETS] = { WOLFMEM_BUCKETS };
|
||||
word32 wc_Dist[WOLFMEM_DEF_BUCKETS] = { WOLFMEM_DIST };
|
||||
|
||||
if (heap == NULL) {
|
||||
return BAD_FUNC_ARG;
|
||||
}
|
||||
|
||||
XMEMSET(heap, 0, sizeof(WOLFSSL_HEAP));
|
||||
|
||||
XMEMCPY(heap->sizeList, wc_MemSz, sizeof(wc_MemSz));
|
||||
XMEMCPY(heap->distList, wc_Dist, sizeof(wc_Dist));
|
||||
|
||||
if (InitMutex(&(heap->memory_mutex)) != 0) {
|
||||
WOLFSSL_MSG("Error creating heap memory mutex");
|
||||
return BAD_MUTEX_E;
|
||||
}
|
||||
|
||||
return SSL_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
int wolfSSL_CTX_load_static_memory(WOLFSSL_CTX** ctx, wolfSSL_method_func method,
|
||||
unsigned char* buf, unsigned int sz,
|
||||
int flag, int max)
|
||||
@ -680,34 +658,23 @@ int wolfSSL_CTX_load_static_memory(WOLFSSL_CTX** ctx, wolfSSL_method_func method
|
||||
return BAD_FUNC_ARG;
|
||||
}
|
||||
|
||||
if (*ctx == NULL) {
|
||||
if (*ctx == NULL || (*ctx)->heap == NULL) {
|
||||
if (sizeof(WOLFSSL_HEAP) + sizeof(WOLFSSL_HEAP_HINT) > sz - idx) {
|
||||
return BUFFER_E; /* not enough memory for structures */
|
||||
}
|
||||
heap = (WOLFSSL_HEAP*)buf;
|
||||
idx += sizeof(WOLFSSL_HEAP);
|
||||
if (wolfSSL_init_memory_heap(heap) != SSL_SUCCESS) {
|
||||
if (wolfSSL_init_memory_heap(heap) != 0) {
|
||||
return SSL_FAILURE;
|
||||
}
|
||||
hint = (WOLFSSL_HEAP_HINT*)(buf + idx);
|
||||
idx += sizeof(WOLFSSL_HEAP_HINT);
|
||||
XMEMSET(hint, 0, sizeof(WOLFSSL_HEAP_HINT));
|
||||
hint->memory = heap;
|
||||
}
|
||||
else if ((*ctx)->heap == NULL) {
|
||||
if (sizeof(WOLFSSL_HEAP) + sizeof(WOLFSSL_HEAP_HINT) > sz - idx) {
|
||||
return BUFFER_E; /* not enough memory for structures */
|
||||
|
||||
if (*ctx && (*ctx)->heap == NULL) {
|
||||
(*ctx)->heap = (void*)hint;
|
||||
}
|
||||
heap = (WOLFSSL_HEAP*)buf;
|
||||
idx += sizeof(WOLFSSL_HEAP);
|
||||
if (wolfSSL_init_memory_heap(heap) != SSL_SUCCESS) {
|
||||
return SSL_FAILURE;
|
||||
}
|
||||
hint = (WOLFSSL_HEAP_HINT*)(buf + idx);
|
||||
idx += sizeof(WOLFSSL_HEAP_HINT);
|
||||
XMEMSET(hint, 0, sizeof(WOLFSSL_HEAP_HINT));
|
||||
hint->memory = heap;
|
||||
(*ctx)->heap = (void*)hint;
|
||||
}
|
||||
else {
|
||||
#ifdef WOLFSSL_HEAP_TEST
|
||||
@ -767,7 +734,7 @@ int wolfSSL_is_static_memory(WOLFSSL* ssl, WOLFSSL_MEM_CONN_STATS* mem_stats)
|
||||
}
|
||||
}
|
||||
|
||||
return (ssl->heap)? 1 : 0;
|
||||
return (ssl->heap) ? 1 : 0;
|
||||
}
|
||||
|
||||
|
||||
@ -786,7 +753,7 @@ int wolfSSL_CTX_is_static_memory(WOLFSSL_CTX* ctx, WOLFSSL_MEM_STATS* mem_stats)
|
||||
}
|
||||
}
|
||||
|
||||
return (ctx->heap)? 1 : 0;
|
||||
return (ctx->heap) ? 1 : 0;
|
||||
}
|
||||
|
||||
#endif /* WOLFSSL_STATIC_MEMORY */
|
||||
|
@ -32,6 +32,13 @@
|
||||
/* Macro to disable benchmark */
|
||||
#ifndef NO_CRYPT_BENCHMARK
|
||||
|
||||
#ifdef WOLFSSL_STATIC_MEMORY
|
||||
#include <wolfssl/wolfcrypt/memory.h>
|
||||
static WOLFSSL_HEAP_HINT* HEAP_HINT;
|
||||
#else
|
||||
#define HEAP_HINT NULL
|
||||
#endif /* WOLFSSL_STATIC_MEMORY */
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#ifdef FREESCALE_MQX
|
||||
@ -85,8 +92,8 @@
|
||||
|
||||
#ifdef WOLFSSL_ASYNC_CRYPT
|
||||
#include <wolfssl/wolfcrypt/async.h>
|
||||
static int devId = INVALID_DEVID;
|
||||
#endif
|
||||
static int devId = INVALID_DEVID;
|
||||
|
||||
#ifdef HAVE_WNR
|
||||
const char* wnrConfigFile = "wnr-example.conf";
|
||||
@ -249,6 +256,20 @@ int benchmark_test(void *args)
|
||||
(void)args;
|
||||
#endif
|
||||
|
||||
#ifdef WOLFSSL_STATIC_MEMORY
|
||||
#ifdef BENCH_EMBEDDED
|
||||
byte memory[50000];
|
||||
#else
|
||||
byte memory[400000];
|
||||
#endif
|
||||
|
||||
if (wc_LoadStaticMemory(&HEAP_HINT, memory, sizeof(memory),
|
||||
WOLFMEM_GENERAL, 1) != 0) {
|
||||
printf("unable to load static memory");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(USE_WOLFSSL_MEMORY) && defined(WOLFSSL_TRACK_MEMORY)
|
||||
InitMemoryTracker();
|
||||
#endif
|
||||
@ -1406,7 +1427,7 @@ void bench_rsa(void)
|
||||
#error "need a cert buffer size"
|
||||
#endif /* USE_CERT_BUFFERS */
|
||||
|
||||
if ((ret = wc_InitRsaKey(&rsaKey, 0)) < 0) {
|
||||
if ((ret = wc_InitRsaKey(&rsaKey, HEAP_HINT)) < 0) {
|
||||
printf("InitRsaKey failed! %d\n", ret);
|
||||
return;
|
||||
}
|
||||
@ -1440,7 +1461,7 @@ void bench_rsa(void)
|
||||
wc_RsaSetRNG(&rsaKey, &rng);
|
||||
#endif
|
||||
start = current_time(1);
|
||||
|
||||
|
||||
/* capture resulting encrypt length */
|
||||
idx = ret;
|
||||
|
||||
@ -1509,7 +1530,7 @@ void bench_rsa_async(void)
|
||||
/* init events and keys */
|
||||
for (i = 0; i < WOLF_ASYNC_MAX_PENDING; i++) {
|
||||
/* setup an async context for each key */
|
||||
if ((ret = wc_InitRsaKey_ex(&rsaKey[i], 0, devId)) < 0) {
|
||||
if ((ret = wc_InitRsaKey_ex(&rsaKey[i], HEAP_HINT, devId)) < 0) {
|
||||
goto done;
|
||||
}
|
||||
#ifdef WC_RSA_BLINDING
|
||||
@ -1538,7 +1559,7 @@ void bench_rsa_async(void)
|
||||
|
||||
/* while free pending slots in queue, submit RSA operations */
|
||||
for (evtNum = 0; evtNum < WOLF_ASYNC_MAX_PENDING; evtNum++) {
|
||||
if (events[evtNum].done || (events[evtNum].pending == 0 &&
|
||||
if (events[evtNum].done || (events[evtNum].pending == 0 &&
|
||||
(i + asyncPend) < ntimes))
|
||||
{
|
||||
/* check for event error */
|
||||
@ -1592,7 +1613,7 @@ void bench_rsa_async(void)
|
||||
|
||||
/* begin private async RSA */
|
||||
start = current_time(1);
|
||||
|
||||
|
||||
/* capture resulting encrypt length */
|
||||
idx = sizeof(enc); /* fixed at 2048 bit */
|
||||
|
||||
@ -1602,7 +1623,7 @@ void bench_rsa_async(void)
|
||||
|
||||
/* while free pending slots in queue, submit RSA operations */
|
||||
for (evtNum = 0; evtNum < WOLF_ASYNC_MAX_PENDING; evtNum++) {
|
||||
if (events[evtNum].done || (events[evtNum].pending == 0 &&
|
||||
if (events[evtNum].done || (events[evtNum].pending == 0 &&
|
||||
(i + asyncPend) < ntimes))
|
||||
{
|
||||
/* check for event error */
|
||||
@ -1772,7 +1793,7 @@ void bench_rsaKeyGen(void)
|
||||
start = current_time(1);
|
||||
|
||||
for(i = 0; i < genTimes; i++) {
|
||||
wc_InitRsaKey(&genKey, 0);
|
||||
wc_InitRsaKey(&genKey, HEAP_HINT);
|
||||
wc_MakeRsaKey(&genKey, 1024, 65537, &rng);
|
||||
wc_FreeRsaKey(&genKey);
|
||||
}
|
||||
@ -1788,7 +1809,7 @@ void bench_rsaKeyGen(void)
|
||||
start = current_time(1);
|
||||
|
||||
for(i = 0; i < genTimes; i++) {
|
||||
wc_InitRsaKey(&genKey, 0);
|
||||
wc_InitRsaKey(&genKey, HEAP_HINT);
|
||||
wc_MakeRsaKey(&genKey, 2048, 65537, &rng);
|
||||
wc_FreeRsaKey(&genKey);
|
||||
}
|
||||
@ -2045,7 +2066,7 @@ void bench_eccKeyGen(void)
|
||||
start = current_time(1);
|
||||
|
||||
for(i = 0; i < genTimes; i++) {
|
||||
wc_ecc_init(&genKey);
|
||||
wc_ecc_init_ex(&genKey, HEAP_HINT, devId);
|
||||
wc_ecc_make_key(&rng, 32, &genKey);
|
||||
wc_ecc_free(&genKey);
|
||||
}
|
||||
@ -2071,8 +2092,8 @@ void bench_eccKeyAgree(void)
|
||||
byte digest[32];
|
||||
word32 x = 0;
|
||||
|
||||
wc_ecc_init(&genKey);
|
||||
wc_ecc_init(&genKey2);
|
||||
wc_ecc_init_ex(&genKey, HEAP_HINT, devId);
|
||||
wc_ecc_init_ex(&genKey2, HEAP_HINT, devId);
|
||||
|
||||
ret = wc_ecc_make_key(&rng, 32, &genKey);
|
||||
if (ret != 0) {
|
||||
@ -2158,8 +2179,8 @@ void bench_eccEncrypt(void)
|
||||
int ret, i;
|
||||
double start, total, each, milliEach;
|
||||
|
||||
wc_ecc_init(&userA);
|
||||
wc_ecc_init(&userB);
|
||||
wc_ecc_init_ex(&userA, HEAP_HINT, devId);
|
||||
wc_ecc_init_ex(&userB, HEAP_HINT, devId);
|
||||
|
||||
wc_ecc_make_key(&rng, 32, &userA);
|
||||
wc_ecc_make_key(&rng, 32, &userB);
|
||||
@ -2449,7 +2470,7 @@ void bench_ed25519KeySign(void)
|
||||
}
|
||||
|
||||
#elif defined(WOLFSSL_EMBOS)
|
||||
|
||||
|
||||
#include "RTOS.h"
|
||||
|
||||
double current_time(int reset)
|
||||
|
@ -196,6 +196,92 @@ static int create_memory_buckets(byte* buffer, word32 bufSz,
|
||||
return ret;
|
||||
}
|
||||
|
||||
int wolfSSL_init_memory_heap(WOLFSSL_HEAP* heap)
|
||||
{
|
||||
word32 wc_MemSz[WOLFMEM_DEF_BUCKETS] = { WOLFMEM_BUCKETS };
|
||||
word32 wc_Dist[WOLFMEM_DEF_BUCKETS] = { WOLFMEM_DIST };
|
||||
|
||||
if (heap == NULL) {
|
||||
return BAD_FUNC_ARG;
|
||||
}
|
||||
|
||||
XMEMSET(heap, 0, sizeof(WOLFSSL_HEAP));
|
||||
|
||||
XMEMCPY(heap->sizeList, wc_MemSz, sizeof(wc_MemSz));
|
||||
XMEMCPY(heap->distList, wc_Dist, sizeof(wc_Dist));
|
||||
|
||||
if (InitMutex(&(heap->memory_mutex)) != 0) {
|
||||
WOLFSSL_MSG("Error creating heap memory mutex");
|
||||
return BAD_MUTEX_E;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int wc_LoadStaticMemory(WOLFSSL_HEAP_HINT** pHint,
|
||||
unsigned char* buf, unsigned int sz, int flag, int max)
|
||||
{
|
||||
int ret;
|
||||
WOLFSSL_HEAP* heap;
|
||||
WOLFSSL_HEAP_HINT* hint;
|
||||
word32 idx = 0;
|
||||
|
||||
if (pHint == NULL || buf == NULL) {
|
||||
return BAD_FUNC_ARG;
|
||||
}
|
||||
|
||||
if ((sizeof(WOLFSSL_HEAP) + sizeof(WOLFSSL_HEAP_HINT)) > sz - idx) {
|
||||
return BUFFER_E; /* not enough memory for structures */
|
||||
}
|
||||
|
||||
/* check if hint has already been assigned */
|
||||
if (*pHint == NULL) {
|
||||
heap = (WOLFSSL_HEAP*)buf;
|
||||
idx += sizeof(WOLFSSL_HEAP);
|
||||
hint = (WOLFSSL_HEAP_HINT*)(buf + idx);
|
||||
idx += sizeof(WOLFSSL_HEAP_HINT);
|
||||
|
||||
ret = wolfSSL_init_memory_heap(heap);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
XMEMSET(hint, 0, sizeof(WOLFSSL_HEAP_HINT));
|
||||
hint->memory = heap;
|
||||
}
|
||||
else {
|
||||
#ifdef WOLFSSL_HEAP_TEST
|
||||
/* do not load in memory if test has been set */
|
||||
if (heap == (void*)WOLFSSL_HEAP_TEST) {
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
hint = (WOLFSSL_HEAP_HINT*)(*pHint);
|
||||
heap = hint->memory;
|
||||
}
|
||||
|
||||
ret = wolfSSL_load_static_memory(buf + idx, sz - idx, flag, heap);
|
||||
if (ret != 1) {
|
||||
WOLFSSL_MSG("Error partitioning memory");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* determine what max applies too */
|
||||
if ((flag & WOLFMEM_IO_POOL) || (flag & WOLFMEM_IO_POOL_FIXED)) {
|
||||
heap->maxIO = max;
|
||||
}
|
||||
else { /* general memory used in handshakes */
|
||||
heap->maxHa = max;
|
||||
}
|
||||
|
||||
heap->flag |= flag;
|
||||
*pHint = hint;
|
||||
|
||||
(void)max;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int wolfSSL_load_static_memory(byte* buffer, word32 sz, int flag,
|
||||
WOLFSSL_HEAP* heap)
|
||||
@ -219,6 +305,10 @@ int wolfSSL_load_static_memory(byte* buffer, word32 sz, int flag,
|
||||
ava--;
|
||||
}
|
||||
|
||||
#ifdef WOLFSSL_DEBUG_MEMORY
|
||||
printf("Allocated %d bytes for static memory @ %p\n", ava, pt);
|
||||
#endif
|
||||
|
||||
/* devide into chunks of memory and add them to available list */
|
||||
while (ava >= (heap->sizeList[0] + padSz + memSz)) {
|
||||
int i;
|
||||
@ -392,7 +482,11 @@ int wolfSSL_GetMemStats(WOLFSSL_HEAP* heap, WOLFSSL_MEM_STATS* stats)
|
||||
}
|
||||
|
||||
|
||||
#ifdef WOLFSSL_DEBUG_MEMORY
|
||||
void* wolfSSL_Malloc(size_t size, void* heap, int type, const char* func, unsigned int line)
|
||||
#else
|
||||
void* wolfSSL_Malloc(size_t size, void* heap, int type)
|
||||
#endif
|
||||
{
|
||||
void* res = 0;
|
||||
wc_Memory* pt = NULL;
|
||||
@ -473,6 +567,10 @@ void* wolfSSL_Malloc(size_t size, void* heap, int type)
|
||||
mem->alloc += 1;
|
||||
res = pt->buffer;
|
||||
|
||||
#ifdef WOLFSSL_DEBUG_MEMORY
|
||||
printf("Alloc: %p -> %u at %s:%d\n", pt->buffer, pt->sz, func, line);
|
||||
#endif
|
||||
|
||||
/* keep track of connection statistics if flag is set */
|
||||
if (mem->flag & WOLFMEM_TRACK_STATS) {
|
||||
WOLFSSL_MEM_CONN_STATS* stats = hint->stats;
|
||||
@ -512,7 +610,11 @@ void* wolfSSL_Malloc(size_t size, void* heap, int type)
|
||||
}
|
||||
|
||||
|
||||
#ifdef WOLFSSL_DEBUG_MEMORY
|
||||
void wolfSSL_Free(void *ptr, void* heap, int type, const char* func, unsigned int line)
|
||||
#else
|
||||
void wolfSSL_Free(void *ptr, void* heap, int type)
|
||||
#endif
|
||||
{
|
||||
int i;
|
||||
wc_Memory* pt;
|
||||
@ -579,6 +681,10 @@ void wolfSSL_Free(void *ptr, void* heap, int type)
|
||||
mem->inUse -= pt->sz;
|
||||
mem->frAlc += 1;
|
||||
|
||||
#ifdef WOLFSSL_DEBUG_MEMORY
|
||||
printf("Free: %p -> %u at %s:%d\n", pt->buffer, pt->sz, func, line);
|
||||
#endif
|
||||
|
||||
/* keep track of connection statistics if flag is set */
|
||||
if (mem->flag & WOLFMEM_TRACK_STATS) {
|
||||
WOLFSSL_MEM_CONN_STATS* stats = hint->stats;
|
||||
@ -606,8 +712,11 @@ void wolfSSL_Free(void *ptr, void* heap, int type)
|
||||
(void)type;
|
||||
}
|
||||
|
||||
|
||||
#ifdef WOLFSSL_DEBUG_MEMORY
|
||||
void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type, const char* func, unsigned int line)
|
||||
#else
|
||||
void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type)
|
||||
#endif
|
||||
{
|
||||
void* res = 0;
|
||||
wc_Memory* pt = NULL;
|
||||
@ -678,7 +787,11 @@ void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type)
|
||||
|
||||
/* free memory that was previously being used */
|
||||
UnLockMutex(&(mem->memory_mutex));
|
||||
wolfSSL_Free(ptr, heap, type);
|
||||
wolfSSL_Free(ptr, heap, type
|
||||
#ifdef WOLFSSL_DEBUG_MEMORY
|
||||
, func, line
|
||||
#endif
|
||||
);
|
||||
if (LockMutex(&(mem->memory_mutex)) != 0) {
|
||||
WOLFSSL_MSG("Bad memory_mutex lock");
|
||||
return NULL;
|
||||
@ -719,7 +832,7 @@ void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type)
|
||||
|
||||
|
||||
/* allow simple per thread in and out pools */
|
||||
/* use 17k size sense max record size is 16k plus overhead */
|
||||
/* use 17k size since max record size is 16k plus overhead */
|
||||
static THREAD_LS_T byte pool_in[17*1024];
|
||||
static THREAD_LS_T byte pool_out[17*1024];
|
||||
|
||||
@ -766,9 +879,7 @@ void* XREALLOC(void *p, size_t n, void* heap, int type)
|
||||
return realloc(p, n);
|
||||
}
|
||||
|
||||
|
||||
/* unit api calls, let's make sure visible with WOLFSSL_API */
|
||||
WOLFSSL_API void XFREE(void *p, void* heap, int type)
|
||||
void XFREE(void *p, void* heap, int type)
|
||||
{
|
||||
(void)heap;
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -33,36 +33,42 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef WOLFSSL_DEBUG_MEMORY
|
||||
typedef void *(*wolfSSL_Malloc_cb)(size_t size, const char* func, unsigned int line);
|
||||
typedef void (*wolfSSL_Free_cb)(void *ptr, const char* func, unsigned int line);
|
||||
typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size, const char* func, unsigned int line);
|
||||
|
||||
/* Public in case user app wants to use XMALLOC/XFREE */
|
||||
WOLFSSL_API void* wolfSSL_Malloc(size_t size, const char* func, unsigned int line);
|
||||
WOLFSSL_API void wolfSSL_Free(void *ptr, const char* func, unsigned int line);
|
||||
WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size, const char* func, unsigned int line);
|
||||
|
||||
#else
|
||||
#if defined(WOLFSSL_STATIC_MEMORY)
|
||||
typedef void *(*wolfSSL_Malloc_cb)(size_t size, void* heap, int type);
|
||||
typedef void (*wolfSSL_Free_cb)(void *ptr, void* heap, int type);
|
||||
typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size,
|
||||
void* heap, int type);
|
||||
WOLFSSL_API void* wolfSSL_Malloc(size_t size, void* heap, int type);
|
||||
WOLFSSL_API void wolfSSL_Free(void *ptr, void* heap, int type);
|
||||
WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size,
|
||||
void* heap, int type);
|
||||
#ifdef WOLFSSL_STATIC_MEMORY
|
||||
#ifdef WOLFSSL_DEBUG_MEMORY
|
||||
typedef void *(*wolfSSL_Malloc_cb)(size_t size, void* heap, int type, const char* func, unsigned int line);
|
||||
typedef void (*wolfSSL_Free_cb)(void *ptr, void* heap, int type, const char* func, unsigned int line);
|
||||
typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size, void* heap, int type, const char* func, unsigned int line);
|
||||
WOLFSSL_API void* wolfSSL_Malloc(size_t size, void* heap, int type, const char* func, unsigned int line);
|
||||
WOLFSSL_API void wolfSSL_Free(void *ptr, void* heap, int type, const char* func, unsigned int line);
|
||||
WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type, const char* func, unsigned int line);
|
||||
#else
|
||||
typedef void *(*wolfSSL_Malloc_cb)(size_t size);
|
||||
typedef void (*wolfSSL_Free_cb)(void *ptr);
|
||||
typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size);
|
||||
/* Public in case user app wants to use XMALLOC/XFREE */
|
||||
WOLFSSL_API void* wolfSSL_Malloc(size_t size);
|
||||
WOLFSSL_API void wolfSSL_Free(void *ptr);
|
||||
WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size);
|
||||
#endif
|
||||
#endif
|
||||
typedef void *(*wolfSSL_Malloc_cb)(size_t size, void* heap, int type);
|
||||
typedef void (*wolfSSL_Free_cb)(void *ptr, void* heap, int type);
|
||||
typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size, void* heap, int type);
|
||||
WOLFSSL_API void* wolfSSL_Malloc(size_t size, void* heap, int type);
|
||||
WOLFSSL_API void wolfSSL_Free(void *ptr, void* heap, int type);
|
||||
WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type);
|
||||
#endif /* WOLFSSL_DEBUG_MEMORY */
|
||||
#else
|
||||
#ifdef WOLFSSL_DEBUG_MEMORY
|
||||
typedef void *(*wolfSSL_Malloc_cb)(size_t size, const char* func, unsigned int line);
|
||||
typedef void (*wolfSSL_Free_cb)(void *ptr, const char* func, unsigned int line);
|
||||
typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size, const char* func, unsigned int line);
|
||||
|
||||
/* Public in case user app wants to use XMALLOC/XFREE */
|
||||
WOLFSSL_API void* wolfSSL_Malloc(size_t size, const char* func, unsigned int line);
|
||||
WOLFSSL_API void wolfSSL_Free(void *ptr, const char* func, unsigned int line);
|
||||
WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size, const char* func, unsigned int line);
|
||||
#else
|
||||
typedef void *(*wolfSSL_Malloc_cb)(size_t size);
|
||||
typedef void (*wolfSSL_Free_cb)(void *ptr);
|
||||
typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size);
|
||||
/* Public in case user app wants to use XMALLOC/XFREE */
|
||||
WOLFSSL_API void* wolfSSL_Malloc(size_t size);
|
||||
WOLFSSL_API void wolfSSL_Free(void *ptr);
|
||||
WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size);
|
||||
#endif /* WOLFSSL_DEBUG_MEMORY */
|
||||
#endif /* WOLFSSL_STATIC_MEMORY */
|
||||
|
||||
/* Public set function */
|
||||
WOLFSSL_API int wolfSSL_SetAllocators(wolfSSL_Malloc_cb malloc_function,
|
||||
@ -156,7 +162,10 @@ WOLFSSL_API int wolfSSL_SetAllocators(wolfSSL_Malloc_cb malloc_function,
|
||||
byte haFlag; /* flag used for checking handshake count */
|
||||
} WOLFSSL_HEAP_HINT;
|
||||
|
||||
WOLFSSL_API int wc_LoadStaticMemory(WOLFSSL_HEAP_HINT** pHint,
|
||||
unsigned char* buf, unsigned int sz, int flag, int max);
|
||||
|
||||
WOLFSSL_LOCAL int wolfSSL_init_memory_heap(WOLFSSL_HEAP* heap);
|
||||
WOLFSSL_LOCAL int wolfSSL_load_static_memory(byte* buffer, word32 sz,
|
||||
int flag, WOLFSSL_HEAP* heap);
|
||||
WOLFSSL_LOCAL int wolfSSL_GetMemStats(WOLFSSL_HEAP* heap,
|
||||
|
@ -1251,6 +1251,9 @@ static char *fgets(char *buff, int sz, FILE *fp)
|
||||
|
||||
/* restriction with static memory */
|
||||
#ifdef WOLFSSL_STATIC_MEMORY
|
||||
#if defined(HAVE_IO_POOL) || defined(XMALLOC_USER) || defined(NO_WOLFSSL_MEMORY)
|
||||
#error static memory cannot be used with HAVE_IO_POOL, XMALLOC_USER or NO_WOLFSSL_MEMORY
|
||||
#endif
|
||||
#ifndef USE_FAST_MATH
|
||||
#error static memory requires fast math please define USE_FAST_MATH
|
||||
#endif
|
||||
@ -1258,6 +1261,8 @@ static char *fgets(char *buff, int sz, FILE *fp)
|
||||
#error static memory does not support small stack please undefine
|
||||
#endif
|
||||
#endif /* WOLFSSL_STATIC_MEMORY */
|
||||
|
||||
|
||||
/* Place any other flags or defines here */
|
||||
|
||||
#if defined(WOLFSSL_MYSQL_COMPATIBLE) && defined(_WIN32) \
|
||||
|
@ -171,24 +171,22 @@
|
||||
/* default to libc stuff */
|
||||
/* XREALLOC is used once in normal math lib, not in fast math lib */
|
||||
/* XFREE on some embeded systems doesn't like free(0) so test */
|
||||
#if defined(XMALLOC_USER)
|
||||
#if defined(HAVE_IO_POOL)
|
||||
WOLFSSL_API void* XMALLOC(size_t n, void* heap, int type);
|
||||
WOLFSSL_API void* XREALLOC(void *p, size_t n, void* heap, int type);
|
||||
WOLFSSL_API void XFREE(void *p, void* heap, int type);
|
||||
#elif defined(XMALLOC_USER)
|
||||
/* prototypes for user heap override functions */
|
||||
#include <stddef.h> /* for size_t */
|
||||
extern void *XMALLOC(size_t n, void* heap, int type);
|
||||
extern void *XREALLOC(void *p, size_t n, void* heap, int type);
|
||||
extern void XFREE(void *p, void* heap, int type);
|
||||
#ifdef WOLFSSL_STATIC_MEMORY /* don't use wolfSSL static memory either*/
|
||||
#undef WOLFSSL_STATIC_MEMORY
|
||||
#endif
|
||||
#elif defined(NO_WOLFSSL_MEMORY)
|
||||
/* just use plain C stdlib stuff if desired */
|
||||
#include <stdlib.h>
|
||||
#define XMALLOC(s, h, t) ((void)h, (void)t, malloc((s)))
|
||||
#define XFREE(p, h, t) {void* xp = (p); if((xp)) free((xp));}
|
||||
#define XREALLOC(p, n, h, t) realloc((p), (n))
|
||||
#ifdef WOLFSSL_STATIC_MEMORY /* don't use wolfSSL static memory either*/
|
||||
#undef WOLFSSL_STATIC_MEMORY
|
||||
#endif
|
||||
#elif !defined(MICRIUM_MALLOC) && !defined(EBSNET) \
|
||||
&& !defined(WOLFSSL_SAFERTOS) && !defined(FREESCALE_MQX) \
|
||||
&& !defined(FREESCALE_KSDK_MQX) && !defined(FREESCALE_FREE_RTOS) \
|
||||
@ -196,19 +194,27 @@
|
||||
&& !defined(WOLFSSL_uITRON4) && !defined(WOLFSSL_uTKERNEL2)
|
||||
/* default C runtime, can install different routines at runtime via cbs */
|
||||
#include <wolfssl/wolfcrypt/memory.h>
|
||||
#if defined(WOLFSSL_DEBUG_MEMORY) && !defined(WOLFSSL_STATIC_MEMORY)
|
||||
#define XMALLOC(s, h, t) ((void)h, (void)t, wolfSSL_Malloc((s), __func__, __LINE__))
|
||||
#define XFREE(p, h, t) {void* xp = (p); if((xp)) wolfSSL_Free((xp), __func__, __LINE__);}
|
||||
#define XREALLOC(p, n, h, t) wolfSSL_Realloc((p), (n), __func__, __LINE__)
|
||||
#elif defined(WOLFSSL_STATIC_MEMORY)
|
||||
#define XMALLOC(s, h, t) wolfSSL_Malloc((s), (h), (t))
|
||||
#define XFREE(p, h, t) {void* xp = (p); if((xp)) wolfSSL_Free((xp), (h), (t));}
|
||||
#define XREALLOC(p, n, h, t) wolfSSL_Realloc((p), (n), (h), (t))
|
||||
#else
|
||||
#define XMALLOC(s, h, t) ((void)h, (void)t, wolfSSL_Malloc((s)))
|
||||
#define XFREE(p, h, t) {void* xp = (p); if((xp)) wolfSSL_Free((xp));}
|
||||
#define XREALLOC(p, n, h, t) wolfSSL_Realloc((p), (n))
|
||||
#endif
|
||||
#ifdef WOLFSSL_STATIC_MEMORY
|
||||
#ifdef WOLFSSL_DEBUG_MEMORY
|
||||
#define XMALLOC(s, h, t) wolfSSL_Malloc((s), (h), (t), __func__, __LINE__)
|
||||
#define XFREE(p, h, t) {void* xp = (p); if((xp)) wolfSSL_Free((xp), (h), (t), __func__, __LINE__);}
|
||||
#define XREALLOC(p, n, h, t) wolfSSL_Realloc((p), (n), (h), (t), __func__, __LINE__)
|
||||
#else
|
||||
#define XMALLOC(s, h, t) wolfSSL_Malloc((s), (h), (t))
|
||||
#define XFREE(p, h, t) {void* xp = (p); if((xp)) wolfSSL_Free((xp), (h), (t));}
|
||||
#define XREALLOC(p, n, h, t) wolfSSL_Realloc((p), (n), (h), (t))
|
||||
#endif /* WOLFSSL_DEBUG_MEMORY */
|
||||
#else
|
||||
#ifdef WOLFSSL_DEBUG_MEMORY
|
||||
#define XMALLOC(s, h, t) ((void)h, (void)t, wolfSSL_Malloc((s), __func__, __LINE__))
|
||||
#define XFREE(p, h, t) {void* xp = (p); if((xp)) wolfSSL_Free((xp), __func__, __LINE__);}
|
||||
#define XREALLOC(p, n, h, t) wolfSSL_Realloc((p), (n), __func__, __LINE__)
|
||||
#else
|
||||
#define XMALLOC(s, h, t) ((void)h, (void)t, wolfSSL_Malloc((s)))
|
||||
#define XFREE(p, h, t) {void* xp = (p); if((xp)) wolfSSL_Free((xp));}
|
||||
#define XREALLOC(p, n, h, t) wolfSSL_Realloc((p), (n))
|
||||
#endif /* WOLFSSL_DEBUG_MEMORY */
|
||||
#endif /* WOLFSSL_STATIC_MEMORY */
|
||||
#endif
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user