wolfSSL SSL/TLS library, support up to TLS1.3

Dependents:   CyaSSL-Twitter-OAuth4Tw Example-client-tls-cert TwitterReader TweetTest ... more

Committer:
wolfSSL
Date:
Fri Jun 05 00:11:07 2020 +0000
Revision:
17:a5f916481144
Parent:
16:8e0d178b1d1e
wolfSSL 4.4.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 15:117db924cf7c 1 /* memory.c
wolfSSL 15:117db924cf7c 2 *
wolfSSL 16:8e0d178b1d1e 3 * Copyright (C) 2006-2020 wolfSSL Inc.
wolfSSL 15:117db924cf7c 4 *
wolfSSL 15:117db924cf7c 5 * This file is part of wolfSSL.
wolfSSL 15:117db924cf7c 6 *
wolfSSL 15:117db924cf7c 7 * wolfSSL is free software; you can redistribute it and/or modify
wolfSSL 15:117db924cf7c 8 * it under the terms of the GNU General Public License as published by
wolfSSL 15:117db924cf7c 9 * the Free Software Foundation; either version 2 of the License, or
wolfSSL 15:117db924cf7c 10 * (at your option) any later version.
wolfSSL 15:117db924cf7c 11 *
wolfSSL 15:117db924cf7c 12 * wolfSSL is distributed in the hope that it will be useful,
wolfSSL 15:117db924cf7c 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wolfSSL 15:117db924cf7c 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolfSSL 15:117db924cf7c 15 * GNU General Public License for more details.
wolfSSL 15:117db924cf7c 16 *
wolfSSL 15:117db924cf7c 17 * You should have received a copy of the GNU General Public License
wolfSSL 15:117db924cf7c 18 * along with this program; if not, write to the Free Software
wolfSSL 15:117db924cf7c 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
wolfSSL 15:117db924cf7c 20 */
wolfSSL 15:117db924cf7c 21
wolfSSL 15:117db924cf7c 22
wolfSSL 15:117db924cf7c 23 #ifdef HAVE_CONFIG_H
wolfSSL 15:117db924cf7c 24 #include <config.h>
wolfSSL 15:117db924cf7c 25 #endif
wolfSSL 15:117db924cf7c 26
wolfSSL 15:117db924cf7c 27 #include <wolfssl/wolfcrypt/settings.h>
wolfSSL 15:117db924cf7c 28
wolfSSL 15:117db924cf7c 29 /* check old macros @wc_fips */
wolfSSL 15:117db924cf7c 30 #if defined(USE_CYASSL_MEMORY) && !defined(USE_WOLFSSL_MEMORY)
wolfSSL 15:117db924cf7c 31 #define USE_WOLFSSL_MEMORY
wolfSSL 15:117db924cf7c 32 #endif
wolfSSL 15:117db924cf7c 33 #if defined(CYASSL_MALLOC_CHECK) && !defined(WOLFSSL_MALLOC_CHECK)
wolfSSL 15:117db924cf7c 34 #define WOLFSSL_MALLOC_CHECK
wolfSSL 15:117db924cf7c 35 #endif
wolfSSL 15:117db924cf7c 36
wolfSSL 16:8e0d178b1d1e 37
wolfSSL 16:8e0d178b1d1e 38 /*
wolfSSL 16:8e0d178b1d1e 39 Possible memory options:
wolfSSL 16:8e0d178b1d1e 40 * NO_WOLFSSL_MEMORY: Disables wolf memory callback support. When not defined settings.h defines USE_WOLFSSL_MEMORY.
wolfSSL 16:8e0d178b1d1e 41 * WOLFSSL_STATIC_MEMORY: Turns on the use of static memory buffers and functions.
wolfSSL 16:8e0d178b1d1e 42 This allows for using static memory instead of dynamic.
wolfSSL 16:8e0d178b1d1e 43 * WOLFSSL_STATIC_ALIGN: Define defaults to 16 to indicate static memory alignment.
wolfSSL 16:8e0d178b1d1e 44 * HAVE_IO_POOL: Enables use of static thread safe memory pool for input/output buffers.
wolfSSL 16:8e0d178b1d1e 45 * XMALLOC_OVERRIDE: Allows override of the XMALLOC, XFREE and XREALLOC macros.
wolfSSL 16:8e0d178b1d1e 46 * XMALLOC_USER: Allows custom XMALLOC, XFREE and XREALLOC functions to be defined.
wolfSSL 16:8e0d178b1d1e 47 * WOLFSSL_NO_MALLOC: Disables the fall-back case to use STDIO malloc/free when no callbacks are set.
wolfSSL 16:8e0d178b1d1e 48 * WOLFSSL_TRACK_MEMORY: Enables memory tracking for total stats and list of allocated memory.
wolfSSL 16:8e0d178b1d1e 49 * WOLFSSL_DEBUG_MEMORY: Enables extra function and line number args for memory callbacks.
wolfSSL 16:8e0d178b1d1e 50 * WOLFSSL_DEBUG_MEMORY_PRINT: Enables printing of each malloc/free.
wolfSSL 16:8e0d178b1d1e 51 * WOLFSSL_MALLOC_CHECK: Reports malloc or alignment failure using WOLFSSL_STATIC_ALIGN
wolfSSL 16:8e0d178b1d1e 52 * WOLFSSL_FORCE_MALLOC_FAIL_TEST: Used for internal testing to induce random malloc failures.
wolfSSL 16:8e0d178b1d1e 53 * WOLFSSL_HEAP_TEST: Used for internal testing of heap hint
wolfSSL 16:8e0d178b1d1e 54 */
wolfSSL 16:8e0d178b1d1e 55
wolfSSL 16:8e0d178b1d1e 56 #ifdef WOLFSSL_ZEPHYR
wolfSSL 16:8e0d178b1d1e 57 #undef realloc
wolfSSL 16:8e0d178b1d1e 58 void *z_realloc(void *ptr, size_t size)
wolfSSL 16:8e0d178b1d1e 59 {
wolfSSL 16:8e0d178b1d1e 60 if (ptr == NULL)
wolfSSL 16:8e0d178b1d1e 61 ptr = malloc(size);
wolfSSL 16:8e0d178b1d1e 62 else
wolfSSL 16:8e0d178b1d1e 63 ptr = realloc(ptr, size);
wolfSSL 16:8e0d178b1d1e 64
wolfSSL 16:8e0d178b1d1e 65 return ptr;
wolfSSL 16:8e0d178b1d1e 66 }
wolfSSL 16:8e0d178b1d1e 67 #define realloc z_realloc
wolfSSL 16:8e0d178b1d1e 68 #endif
wolfSSL 16:8e0d178b1d1e 69
wolfSSL 15:117db924cf7c 70 #ifdef USE_WOLFSSL_MEMORY
wolfSSL 15:117db924cf7c 71
wolfSSL 15:117db924cf7c 72 #include <wolfssl/wolfcrypt/memory.h>
wolfSSL 15:117db924cf7c 73 #include <wolfssl/wolfcrypt/error-crypt.h>
wolfSSL 15:117db924cf7c 74 #include <wolfssl/wolfcrypt/logging.h>
wolfSSL 15:117db924cf7c 75
wolfSSL 16:8e0d178b1d1e 76 #if defined(WOLFSSL_DEBUG_MEMORY) && defined(WOLFSSL_DEBUG_MEMORY_PRINT)
wolfSSL 16:8e0d178b1d1e 77 #include <stdio.h>
wolfSSL 16:8e0d178b1d1e 78 #endif
wolfSSL 16:8e0d178b1d1e 79
wolfSSL 16:8e0d178b1d1e 80 #ifdef WOLFSSL_FORCE_MALLOC_FAIL_TEST
wolfSSL 16:8e0d178b1d1e 81 static int gMemFailCountSeed;
wolfSSL 16:8e0d178b1d1e 82 static int gMemFailCount;
wolfSSL 16:8e0d178b1d1e 83 void wolfSSL_SetMemFailCount(int memFailCount)
wolfSSL 16:8e0d178b1d1e 84 {
wolfSSL 16:8e0d178b1d1e 85 if (gMemFailCountSeed == 0) {
wolfSSL 16:8e0d178b1d1e 86 gMemFailCountSeed = memFailCount;
wolfSSL 16:8e0d178b1d1e 87 gMemFailCount = memFailCount;
wolfSSL 16:8e0d178b1d1e 88 }
wolfSSL 16:8e0d178b1d1e 89 }
wolfSSL 16:8e0d178b1d1e 90 #endif
wolfSSL 16:8e0d178b1d1e 91 #if defined(WOLFSSL_MALLOC_CHECK) || defined(WOLFSSL_TRACK_MEMORY_FULL) || \
wolfSSL 16:8e0d178b1d1e 92 defined(WOLFSSL_MEMORY_LOG)
wolfSSL 15:117db924cf7c 93 #include <stdio.h>
wolfSSL 15:117db924cf7c 94 #endif
wolfSSL 15:117db924cf7c 95
wolfSSL 15:117db924cf7c 96
wolfSSL 15:117db924cf7c 97 /* Set these to default values initially. */
wolfSSL 15:117db924cf7c 98 static wolfSSL_Malloc_cb malloc_function = NULL;
wolfSSL 15:117db924cf7c 99 static wolfSSL_Free_cb free_function = NULL;
wolfSSL 15:117db924cf7c 100 static wolfSSL_Realloc_cb realloc_function = NULL;
wolfSSL 15:117db924cf7c 101
wolfSSL 15:117db924cf7c 102 int wolfSSL_SetAllocators(wolfSSL_Malloc_cb mf,
wolfSSL 15:117db924cf7c 103 wolfSSL_Free_cb ff,
wolfSSL 15:117db924cf7c 104 wolfSSL_Realloc_cb rf)
wolfSSL 15:117db924cf7c 105 {
wolfSSL 16:8e0d178b1d1e 106 malloc_function = mf;
wolfSSL 16:8e0d178b1d1e 107 free_function = ff;
wolfSSL 16:8e0d178b1d1e 108 realloc_function = rf;
wolfSSL 15:117db924cf7c 109 return 0;
wolfSSL 15:117db924cf7c 110 }
wolfSSL 15:117db924cf7c 111
wolfSSL 15:117db924cf7c 112 int wolfSSL_GetAllocators(wolfSSL_Malloc_cb* mf,
wolfSSL 15:117db924cf7c 113 wolfSSL_Free_cb* ff,
wolfSSL 15:117db924cf7c 114 wolfSSL_Realloc_cb* rf)
wolfSSL 15:117db924cf7c 115 {
wolfSSL 15:117db924cf7c 116 if (mf) *mf = malloc_function;
wolfSSL 15:117db924cf7c 117 if (ff) *ff = free_function;
wolfSSL 15:117db924cf7c 118 if (rf) *rf = realloc_function;
wolfSSL 15:117db924cf7c 119 return 0;
wolfSSL 15:117db924cf7c 120 }
wolfSSL 15:117db924cf7c 121
wolfSSL 15:117db924cf7c 122 #ifndef WOLFSSL_STATIC_MEMORY
wolfSSL 15:117db924cf7c 123 #ifdef WOLFSSL_DEBUG_MEMORY
wolfSSL 15:117db924cf7c 124 void* wolfSSL_Malloc(size_t size, const char* func, unsigned int line)
wolfSSL 15:117db924cf7c 125 #else
wolfSSL 15:117db924cf7c 126 void* wolfSSL_Malloc(size_t size)
wolfSSL 15:117db924cf7c 127 #endif
wolfSSL 15:117db924cf7c 128 {
wolfSSL 15:117db924cf7c 129 void* res = 0;
wolfSSL 15:117db924cf7c 130
wolfSSL 15:117db924cf7c 131 if (malloc_function) {
wolfSSL 15:117db924cf7c 132 #ifdef WOLFSSL_DEBUG_MEMORY
wolfSSL 15:117db924cf7c 133 res = malloc_function(size, func, line);
wolfSSL 15:117db924cf7c 134 #else
wolfSSL 15:117db924cf7c 135 res = malloc_function(size);
wolfSSL 15:117db924cf7c 136 #endif
wolfSSL 15:117db924cf7c 137 }
wolfSSL 15:117db924cf7c 138 else {
wolfSSL 15:117db924cf7c 139 #ifndef WOLFSSL_NO_MALLOC
wolfSSL 15:117db924cf7c 140 res = malloc(size);
wolfSSL 15:117db924cf7c 141 #else
wolfSSL 15:117db924cf7c 142 WOLFSSL_MSG("No malloc available");
wolfSSL 15:117db924cf7c 143 #endif
wolfSSL 15:117db924cf7c 144 }
wolfSSL 15:117db924cf7c 145
wolfSSL 16:8e0d178b1d1e 146 #ifdef WOLFSSL_DEBUG_MEMORY
wolfSSL 16:8e0d178b1d1e 147 #if defined(WOLFSSL_DEBUG_MEMORY_PRINT) && !defined(WOLFSSL_TRACK_MEMORY)
wolfSSL 16:8e0d178b1d1e 148 printf("Alloc: %p -> %u at %s:%d\n", res, (word32)size, func, line);
wolfSSL 16:8e0d178b1d1e 149 #else
wolfSSL 16:8e0d178b1d1e 150 (void)func;
wolfSSL 16:8e0d178b1d1e 151 (void)line;
wolfSSL 16:8e0d178b1d1e 152 #endif
wolfSSL 16:8e0d178b1d1e 153 #endif
wolfSSL 16:8e0d178b1d1e 154
wolfSSL 16:8e0d178b1d1e 155 #ifdef WOLFSSL_MALLOC_CHECK
wolfSSL 16:8e0d178b1d1e 156 if (res == NULL)
wolfSSL 16:8e0d178b1d1e 157 WOLFSSL_MSG("wolfSSL_malloc failed");
wolfSSL 16:8e0d178b1d1e 158 #endif
wolfSSL 16:8e0d178b1d1e 159
wolfSSL 16:8e0d178b1d1e 160 #ifdef WOLFSSL_FORCE_MALLOC_FAIL_TEST
wolfSSL 16:8e0d178b1d1e 161 if (res && --gMemFailCount == 0) {
wolfSSL 16:8e0d178b1d1e 162 printf("\n---FORCED MEM FAIL TEST---\n");
wolfSSL 16:8e0d178b1d1e 163 if (free_function) {
wolfSSL 16:8e0d178b1d1e 164 #ifdef WOLFSSL_DEBUG_MEMORY
wolfSSL 16:8e0d178b1d1e 165 free_function(res, func, line);
wolfSSL 16:8e0d178b1d1e 166 #else
wolfSSL 16:8e0d178b1d1e 167 free_function(res);
wolfSSL 16:8e0d178b1d1e 168 #endif
wolfSSL 16:8e0d178b1d1e 169 }
wolfSSL 16:8e0d178b1d1e 170 else {
wolfSSL 16:8e0d178b1d1e 171 free(res); /* clear */
wolfSSL 16:8e0d178b1d1e 172 }
wolfSSL 16:8e0d178b1d1e 173 gMemFailCount = gMemFailCountSeed; /* reset */
wolfSSL 16:8e0d178b1d1e 174 return NULL;
wolfSSL 16:8e0d178b1d1e 175 }
wolfSSL 16:8e0d178b1d1e 176 #endif
wolfSSL 15:117db924cf7c 177
wolfSSL 15:117db924cf7c 178 return res;
wolfSSL 15:117db924cf7c 179 }
wolfSSL 15:117db924cf7c 180
wolfSSL 15:117db924cf7c 181 #ifdef WOLFSSL_DEBUG_MEMORY
wolfSSL 15:117db924cf7c 182 void wolfSSL_Free(void *ptr, const char* func, unsigned int line)
wolfSSL 15:117db924cf7c 183 #else
wolfSSL 15:117db924cf7c 184 void wolfSSL_Free(void *ptr)
wolfSSL 15:117db924cf7c 185 #endif
wolfSSL 15:117db924cf7c 186 {
wolfSSL 16:8e0d178b1d1e 187 #ifdef WOLFSSL_DEBUG_MEMORY
wolfSSL 16:8e0d178b1d1e 188 #if defined(WOLFSSL_DEBUG_MEMORY_PRINT) && !defined(WOLFSSL_TRACK_MEMORY)
wolfSSL 16:8e0d178b1d1e 189 printf("Free: %p at %s:%d\n", ptr, func, line);
wolfSSL 16:8e0d178b1d1e 190 #else
wolfSSL 16:8e0d178b1d1e 191 (void)func;
wolfSSL 16:8e0d178b1d1e 192 (void)line;
wolfSSL 16:8e0d178b1d1e 193 #endif
wolfSSL 16:8e0d178b1d1e 194 #endif
wolfSSL 16:8e0d178b1d1e 195
wolfSSL 15:117db924cf7c 196 if (free_function) {
wolfSSL 15:117db924cf7c 197 #ifdef WOLFSSL_DEBUG_MEMORY
wolfSSL 15:117db924cf7c 198 free_function(ptr, func, line);
wolfSSL 15:117db924cf7c 199 #else
wolfSSL 15:117db924cf7c 200 free_function(ptr);
wolfSSL 15:117db924cf7c 201 #endif
wolfSSL 15:117db924cf7c 202 }
wolfSSL 15:117db924cf7c 203 else {
wolfSSL 15:117db924cf7c 204 #ifndef WOLFSSL_NO_MALLOC
wolfSSL 15:117db924cf7c 205 free(ptr);
wolfSSL 15:117db924cf7c 206 #else
wolfSSL 15:117db924cf7c 207 WOLFSSL_MSG("No free available");
wolfSSL 15:117db924cf7c 208 #endif
wolfSSL 15:117db924cf7c 209 }
wolfSSL 15:117db924cf7c 210 }
wolfSSL 15:117db924cf7c 211
wolfSSL 15:117db924cf7c 212 #ifdef WOLFSSL_DEBUG_MEMORY
wolfSSL 15:117db924cf7c 213 void* wolfSSL_Realloc(void *ptr, size_t size, const char* func, unsigned int line)
wolfSSL 15:117db924cf7c 214 #else
wolfSSL 15:117db924cf7c 215 void* wolfSSL_Realloc(void *ptr, size_t size)
wolfSSL 15:117db924cf7c 216 #endif
wolfSSL 15:117db924cf7c 217 {
wolfSSL 15:117db924cf7c 218 void* res = 0;
wolfSSL 15:117db924cf7c 219
wolfSSL 15:117db924cf7c 220 if (realloc_function) {
wolfSSL 15:117db924cf7c 221 #ifdef WOLFSSL_DEBUG_MEMORY
wolfSSL 15:117db924cf7c 222 res = realloc_function(ptr, size, func, line);
wolfSSL 15:117db924cf7c 223 #else
wolfSSL 15:117db924cf7c 224 res = realloc_function(ptr, size);
wolfSSL 15:117db924cf7c 225 #endif
wolfSSL 15:117db924cf7c 226 }
wolfSSL 15:117db924cf7c 227 else {
wolfSSL 15:117db924cf7c 228 #ifndef WOLFSSL_NO_MALLOC
wolfSSL 15:117db924cf7c 229 res = realloc(ptr, size);
wolfSSL 15:117db924cf7c 230 #else
wolfSSL 15:117db924cf7c 231 WOLFSSL_MSG("No realloc available");
wolfSSL 15:117db924cf7c 232 #endif
wolfSSL 15:117db924cf7c 233 }
wolfSSL 15:117db924cf7c 234
wolfSSL 15:117db924cf7c 235 return res;
wolfSSL 15:117db924cf7c 236 }
wolfSSL 15:117db924cf7c 237 #endif /* WOLFSSL_STATIC_MEMORY */
wolfSSL 15:117db924cf7c 238
wolfSSL 15:117db924cf7c 239 #ifdef WOLFSSL_STATIC_MEMORY
wolfSSL 15:117db924cf7c 240
wolfSSL 15:117db924cf7c 241 struct wc_Memory {
wolfSSL 15:117db924cf7c 242 byte* buffer;
wolfSSL 15:117db924cf7c 243 struct wc_Memory* next;
wolfSSL 15:117db924cf7c 244 word32 sz;
wolfSSL 15:117db924cf7c 245 };
wolfSSL 15:117db924cf7c 246
wolfSSL 15:117db924cf7c 247
wolfSSL 15:117db924cf7c 248 /* returns amount of memory used on success. On error returns negative value
wolfSSL 15:117db924cf7c 249 wc_Memory** list is the list that new buckets are prepended to
wolfSSL 15:117db924cf7c 250 */
wolfSSL 15:117db924cf7c 251 static int create_memory_buckets(byte* buffer, word32 bufSz,
wolfSSL 15:117db924cf7c 252 word32 buckSz, word32 buckNum, wc_Memory** list) {
wolfSSL 15:117db924cf7c 253 word32 i;
wolfSSL 15:117db924cf7c 254 byte* pt = buffer;
wolfSSL 15:117db924cf7c 255 int ret = 0;
wolfSSL 15:117db924cf7c 256 word32 memSz = (word32)sizeof(wc_Memory);
wolfSSL 15:117db924cf7c 257 word32 padSz = -(int)memSz & (WOLFSSL_STATIC_ALIGN - 1);
wolfSSL 15:117db924cf7c 258
wolfSSL 15:117db924cf7c 259 /* if not enough space available for bucket size then do not try */
wolfSSL 15:117db924cf7c 260 if (buckSz + memSz + padSz > bufSz) {
wolfSSL 15:117db924cf7c 261 return ret;
wolfSSL 15:117db924cf7c 262 }
wolfSSL 15:117db924cf7c 263
wolfSSL 15:117db924cf7c 264 for (i = 0; i < buckNum; i++) {
wolfSSL 15:117db924cf7c 265 if ((buckSz + memSz + padSz) <= (bufSz - ret)) {
wolfSSL 15:117db924cf7c 266 /* create a new struct and set its values */
wolfSSL 15:117db924cf7c 267 wc_Memory* mem = (struct wc_Memory*)(pt);
wolfSSL 15:117db924cf7c 268 mem->sz = buckSz;
wolfSSL 15:117db924cf7c 269 mem->buffer = (byte*)pt + padSz + memSz;
wolfSSL 15:117db924cf7c 270 mem->next = NULL;
wolfSSL 15:117db924cf7c 271
wolfSSL 15:117db924cf7c 272 /* add the newly created struct to front of list */
wolfSSL 15:117db924cf7c 273 if (*list == NULL) {
wolfSSL 15:117db924cf7c 274 *list = mem;
wolfSSL 15:117db924cf7c 275 } else {
wolfSSL 15:117db924cf7c 276 mem->next = *list;
wolfSSL 15:117db924cf7c 277 *list = mem;
wolfSSL 15:117db924cf7c 278 }
wolfSSL 15:117db924cf7c 279
wolfSSL 15:117db924cf7c 280 /* advance pointer and keep track of memory used */
wolfSSL 15:117db924cf7c 281 ret += buckSz + padSz + memSz;
wolfSSL 15:117db924cf7c 282 pt += buckSz + padSz + memSz;
wolfSSL 15:117db924cf7c 283 }
wolfSSL 15:117db924cf7c 284 else {
wolfSSL 15:117db924cf7c 285 break; /* not enough space left for more buckets of this size */
wolfSSL 15:117db924cf7c 286 }
wolfSSL 15:117db924cf7c 287 }
wolfSSL 15:117db924cf7c 288
wolfSSL 15:117db924cf7c 289 return ret;
wolfSSL 15:117db924cf7c 290 }
wolfSSL 15:117db924cf7c 291
wolfSSL 15:117db924cf7c 292 int wolfSSL_init_memory_heap(WOLFSSL_HEAP* heap)
wolfSSL 15:117db924cf7c 293 {
wolfSSL 15:117db924cf7c 294 word32 wc_MemSz[WOLFMEM_DEF_BUCKETS] = { WOLFMEM_BUCKETS };
wolfSSL 15:117db924cf7c 295 word32 wc_Dist[WOLFMEM_DEF_BUCKETS] = { WOLFMEM_DIST };
wolfSSL 15:117db924cf7c 296
wolfSSL 15:117db924cf7c 297 if (heap == NULL) {
wolfSSL 15:117db924cf7c 298 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 299 }
wolfSSL 15:117db924cf7c 300
wolfSSL 15:117db924cf7c 301 XMEMSET(heap, 0, sizeof(WOLFSSL_HEAP));
wolfSSL 15:117db924cf7c 302
wolfSSL 15:117db924cf7c 303 XMEMCPY(heap->sizeList, wc_MemSz, sizeof(wc_MemSz));
wolfSSL 15:117db924cf7c 304 XMEMCPY(heap->distList, wc_Dist, sizeof(wc_Dist));
wolfSSL 15:117db924cf7c 305
wolfSSL 15:117db924cf7c 306 if (wc_InitMutex(&(heap->memory_mutex)) != 0) {
wolfSSL 15:117db924cf7c 307 WOLFSSL_MSG("Error creating heap memory mutex");
wolfSSL 15:117db924cf7c 308 return BAD_MUTEX_E;
wolfSSL 15:117db924cf7c 309 }
wolfSSL 15:117db924cf7c 310
wolfSSL 15:117db924cf7c 311 return 0;
wolfSSL 15:117db924cf7c 312 }
wolfSSL 15:117db924cf7c 313
wolfSSL 15:117db924cf7c 314 int wc_LoadStaticMemory(WOLFSSL_HEAP_HINT** pHint,
wolfSSL 15:117db924cf7c 315 unsigned char* buf, unsigned int sz, int flag, int max)
wolfSSL 15:117db924cf7c 316 {
wolfSSL 15:117db924cf7c 317 int ret;
wolfSSL 15:117db924cf7c 318 WOLFSSL_HEAP* heap;
wolfSSL 15:117db924cf7c 319 WOLFSSL_HEAP_HINT* hint;
wolfSSL 15:117db924cf7c 320 word32 idx = 0;
wolfSSL 15:117db924cf7c 321
wolfSSL 15:117db924cf7c 322 if (pHint == NULL || buf == NULL) {
wolfSSL 15:117db924cf7c 323 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 324 }
wolfSSL 15:117db924cf7c 325
wolfSSL 15:117db924cf7c 326 if ((sizeof(WOLFSSL_HEAP) + sizeof(WOLFSSL_HEAP_HINT)) > sz - idx) {
wolfSSL 15:117db924cf7c 327 return BUFFER_E; /* not enough memory for structures */
wolfSSL 15:117db924cf7c 328 }
wolfSSL 15:117db924cf7c 329
wolfSSL 15:117db924cf7c 330 /* check if hint has already been assigned */
wolfSSL 15:117db924cf7c 331 if (*pHint == NULL) {
wolfSSL 15:117db924cf7c 332 heap = (WOLFSSL_HEAP*)buf;
wolfSSL 15:117db924cf7c 333 idx += sizeof(WOLFSSL_HEAP);
wolfSSL 15:117db924cf7c 334 hint = (WOLFSSL_HEAP_HINT*)(buf + idx);
wolfSSL 15:117db924cf7c 335 idx += sizeof(WOLFSSL_HEAP_HINT);
wolfSSL 15:117db924cf7c 336
wolfSSL 15:117db924cf7c 337 ret = wolfSSL_init_memory_heap(heap);
wolfSSL 15:117db924cf7c 338 if (ret != 0) {
wolfSSL 15:117db924cf7c 339 return ret;
wolfSSL 15:117db924cf7c 340 }
wolfSSL 15:117db924cf7c 341
wolfSSL 15:117db924cf7c 342 XMEMSET(hint, 0, sizeof(WOLFSSL_HEAP_HINT));
wolfSSL 15:117db924cf7c 343 hint->memory = heap;
wolfSSL 15:117db924cf7c 344 }
wolfSSL 15:117db924cf7c 345 else {
wolfSSL 15:117db924cf7c 346 #ifdef WOLFSSL_HEAP_TEST
wolfSSL 15:117db924cf7c 347 /* do not load in memory if test has been set */
wolfSSL 15:117db924cf7c 348 if (heap == (void*)WOLFSSL_HEAP_TEST) {
wolfSSL 15:117db924cf7c 349 return 0;
wolfSSL 15:117db924cf7c 350 }
wolfSSL 15:117db924cf7c 351 #endif
wolfSSL 15:117db924cf7c 352
wolfSSL 15:117db924cf7c 353 hint = (WOLFSSL_HEAP_HINT*)(*pHint);
wolfSSL 15:117db924cf7c 354 heap = hint->memory;
wolfSSL 15:117db924cf7c 355 }
wolfSSL 15:117db924cf7c 356
wolfSSL 15:117db924cf7c 357 ret = wolfSSL_load_static_memory(buf + idx, sz - idx, flag, heap);
wolfSSL 15:117db924cf7c 358 if (ret != 1) {
wolfSSL 15:117db924cf7c 359 WOLFSSL_MSG("Error partitioning memory");
wolfSSL 15:117db924cf7c 360 return -1;
wolfSSL 15:117db924cf7c 361 }
wolfSSL 15:117db924cf7c 362
wolfSSL 15:117db924cf7c 363 /* determine what max applies too */
wolfSSL 15:117db924cf7c 364 if ((flag & WOLFMEM_IO_POOL) || (flag & WOLFMEM_IO_POOL_FIXED)) {
wolfSSL 15:117db924cf7c 365 heap->maxIO = max;
wolfSSL 15:117db924cf7c 366 }
wolfSSL 15:117db924cf7c 367 else { /* general memory used in handshakes */
wolfSSL 15:117db924cf7c 368 heap->maxHa = max;
wolfSSL 15:117db924cf7c 369 }
wolfSSL 15:117db924cf7c 370
wolfSSL 15:117db924cf7c 371 heap->flag |= flag;
wolfSSL 15:117db924cf7c 372 *pHint = hint;
wolfSSL 15:117db924cf7c 373
wolfSSL 15:117db924cf7c 374 (void)max;
wolfSSL 15:117db924cf7c 375
wolfSSL 15:117db924cf7c 376 return 0;
wolfSSL 15:117db924cf7c 377 }
wolfSSL 15:117db924cf7c 378
wolfSSL 15:117db924cf7c 379 int wolfSSL_load_static_memory(byte* buffer, word32 sz, int flag,
wolfSSL 15:117db924cf7c 380 WOLFSSL_HEAP* heap)
wolfSSL 15:117db924cf7c 381 {
wolfSSL 15:117db924cf7c 382 word32 ava = sz;
wolfSSL 15:117db924cf7c 383 byte* pt = buffer;
wolfSSL 15:117db924cf7c 384 int ret = 0;
wolfSSL 15:117db924cf7c 385 word32 memSz = (word32)sizeof(wc_Memory);
wolfSSL 15:117db924cf7c 386 word32 padSz = -(int)memSz & (WOLFSSL_STATIC_ALIGN - 1);
wolfSSL 15:117db924cf7c 387
wolfSSL 15:117db924cf7c 388 WOLFSSL_ENTER("wolfSSL_load_static_memory");
wolfSSL 15:117db924cf7c 389
wolfSSL 15:117db924cf7c 390 if (buffer == NULL) {
wolfSSL 15:117db924cf7c 391 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 392 }
wolfSSL 15:117db924cf7c 393
wolfSSL 15:117db924cf7c 394 /* align pt */
wolfSSL 15:117db924cf7c 395 while ((wolfssl_word)pt % WOLFSSL_STATIC_ALIGN && pt < (buffer + sz)) {
wolfSSL 15:117db924cf7c 396 *pt = 0x00;
wolfSSL 15:117db924cf7c 397 pt++;
wolfSSL 15:117db924cf7c 398 ava--;
wolfSSL 15:117db924cf7c 399 }
wolfSSL 15:117db924cf7c 400
wolfSSL 15:117db924cf7c 401 #ifdef WOLFSSL_DEBUG_MEMORY
wolfSSL 15:117db924cf7c 402 printf("Allocated %d bytes for static memory @ %p\n", ava, pt);
wolfSSL 15:117db924cf7c 403 #endif
wolfSSL 15:117db924cf7c 404
wolfSSL 16:8e0d178b1d1e 405 /* divide into chunks of memory and add them to available list */
wolfSSL 15:117db924cf7c 406 while (ava >= (heap->sizeList[0] + padSz + memSz)) {
wolfSSL 15:117db924cf7c 407 int i;
wolfSSL 15:117db924cf7c 408 /* creating only IO buffers from memory passed in, max TLS is 16k */
wolfSSL 15:117db924cf7c 409 if (flag & WOLFMEM_IO_POOL || flag & WOLFMEM_IO_POOL_FIXED) {
wolfSSL 15:117db924cf7c 410 if ((ret = create_memory_buckets(pt, ava,
wolfSSL 15:117db924cf7c 411 WOLFMEM_IO_SZ, 1, &(heap->io))) < 0) {
wolfSSL 15:117db924cf7c 412 WOLFSSL_LEAVE("wolfSSL_load_static_memory", ret);
wolfSSL 15:117db924cf7c 413 return ret;
wolfSSL 15:117db924cf7c 414 }
wolfSSL 15:117db924cf7c 415
wolfSSL 15:117db924cf7c 416 /* check if no more room left for creating IO buffers */
wolfSSL 15:117db924cf7c 417 if (ret == 0) {
wolfSSL 15:117db924cf7c 418 break;
wolfSSL 15:117db924cf7c 419 }
wolfSSL 15:117db924cf7c 420
wolfSSL 15:117db924cf7c 421 /* advance pointer in buffer for next buckets and keep track
wolfSSL 15:117db924cf7c 422 of how much memory is left available */
wolfSSL 15:117db924cf7c 423 pt += ret;
wolfSSL 15:117db924cf7c 424 ava -= ret;
wolfSSL 15:117db924cf7c 425 }
wolfSSL 15:117db924cf7c 426 else {
wolfSSL 15:117db924cf7c 427 /* start at largest and move to smaller buckets */
wolfSSL 15:117db924cf7c 428 for (i = (WOLFMEM_MAX_BUCKETS - 1); i >= 0; i--) {
wolfSSL 15:117db924cf7c 429 if ((heap->sizeList[i] + padSz + memSz) <= ava) {
wolfSSL 15:117db924cf7c 430 if ((ret = create_memory_buckets(pt, ava, heap->sizeList[i],
wolfSSL 15:117db924cf7c 431 heap->distList[i], &(heap->ava[i]))) < 0) {
wolfSSL 15:117db924cf7c 432 WOLFSSL_LEAVE("wolfSSL_load_static_memory", ret);
wolfSSL 15:117db924cf7c 433 return ret;
wolfSSL 15:117db924cf7c 434 }
wolfSSL 15:117db924cf7c 435
wolfSSL 15:117db924cf7c 436 /* advance pointer in buffer for next buckets and keep track
wolfSSL 15:117db924cf7c 437 of how much memory is left available */
wolfSSL 15:117db924cf7c 438 pt += ret;
wolfSSL 15:117db924cf7c 439 ava -= ret;
wolfSSL 15:117db924cf7c 440 }
wolfSSL 15:117db924cf7c 441 }
wolfSSL 15:117db924cf7c 442 }
wolfSSL 15:117db924cf7c 443 }
wolfSSL 15:117db924cf7c 444
wolfSSL 15:117db924cf7c 445 return 1;
wolfSSL 15:117db924cf7c 446 }
wolfSSL 15:117db924cf7c 447
wolfSSL 15:117db924cf7c 448
wolfSSL 15:117db924cf7c 449 /* returns the size of management memory needed for each bucket.
wolfSSL 15:117db924cf7c 450 * This is memory that is used to keep track of and align memory buckets. */
wolfSSL 15:117db924cf7c 451 int wolfSSL_MemoryPaddingSz(void)
wolfSSL 15:117db924cf7c 452 {
wolfSSL 15:117db924cf7c 453 word32 memSz = (word32)sizeof(wc_Memory);
wolfSSL 15:117db924cf7c 454 word32 padSz = -(int)memSz & (WOLFSSL_STATIC_ALIGN - 1);
wolfSSL 15:117db924cf7c 455 return memSz + padSz;
wolfSSL 15:117db924cf7c 456 }
wolfSSL 15:117db924cf7c 457
wolfSSL 15:117db924cf7c 458
wolfSSL 15:117db924cf7c 459 /* Used to calculate memory size for optimum use with buckets.
wolfSSL 15:117db924cf7c 460 returns the suggested size rounded down to the nearest bucket. */
wolfSSL 15:117db924cf7c 461 int wolfSSL_StaticBufferSz(byte* buffer, word32 sz, int flag)
wolfSSL 15:117db924cf7c 462 {
wolfSSL 15:117db924cf7c 463 word32 bucketSz[WOLFMEM_MAX_BUCKETS] = {WOLFMEM_BUCKETS};
wolfSSL 15:117db924cf7c 464 word32 distList[WOLFMEM_MAX_BUCKETS] = {WOLFMEM_DIST};
wolfSSL 15:117db924cf7c 465
wolfSSL 15:117db924cf7c 466 word32 ava = sz;
wolfSSL 15:117db924cf7c 467 byte* pt = buffer;
wolfSSL 15:117db924cf7c 468 word32 memSz = (word32)sizeof(wc_Memory);
wolfSSL 15:117db924cf7c 469 word32 padSz = -(int)memSz & (WOLFSSL_STATIC_ALIGN - 1);
wolfSSL 15:117db924cf7c 470
wolfSSL 15:117db924cf7c 471 WOLFSSL_ENTER("wolfSSL_static_size");
wolfSSL 15:117db924cf7c 472
wolfSSL 15:117db924cf7c 473 if (buffer == NULL) {
wolfSSL 15:117db924cf7c 474 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 475 }
wolfSSL 15:117db924cf7c 476
wolfSSL 15:117db924cf7c 477 /* align pt */
wolfSSL 15:117db924cf7c 478 while ((wolfssl_word)pt % WOLFSSL_STATIC_ALIGN && pt < (buffer + sz)) {
wolfSSL 15:117db924cf7c 479 pt++;
wolfSSL 15:117db924cf7c 480 ava--;
wolfSSL 15:117db924cf7c 481 }
wolfSSL 15:117db924cf7c 482
wolfSSL 15:117db924cf7c 483 /* creating only IO buffers from memory passed in, max TLS is 16k */
wolfSSL 15:117db924cf7c 484 if (flag & WOLFMEM_IO_POOL || flag & WOLFMEM_IO_POOL_FIXED) {
wolfSSL 15:117db924cf7c 485 if (ava < (memSz + padSz + WOLFMEM_IO_SZ)) {
wolfSSL 15:117db924cf7c 486 return 0; /* not enough room for even one bucket */
wolfSSL 15:117db924cf7c 487 }
wolfSSL 15:117db924cf7c 488
wolfSSL 15:117db924cf7c 489 ava = ava % (memSz + padSz + WOLFMEM_IO_SZ);
wolfSSL 15:117db924cf7c 490 }
wolfSSL 15:117db924cf7c 491 else {
wolfSSL 15:117db924cf7c 492 int i, k;
wolfSSL 15:117db924cf7c 493
wolfSSL 15:117db924cf7c 494 if (ava < (bucketSz[0] + padSz + memSz)) {
wolfSSL 15:117db924cf7c 495 return 0; /* not enough room for even one bucket */
wolfSSL 15:117db924cf7c 496 }
wolfSSL 15:117db924cf7c 497
wolfSSL 15:117db924cf7c 498 while ((ava >= (bucketSz[0] + padSz + memSz)) && (ava > 0)) {
wolfSSL 15:117db924cf7c 499 /* start at largest and move to smaller buckets */
wolfSSL 15:117db924cf7c 500 for (i = (WOLFMEM_MAX_BUCKETS - 1); i >= 0; i--) {
wolfSSL 15:117db924cf7c 501 for (k = distList[i]; k > 0; k--) {
wolfSSL 15:117db924cf7c 502 if ((bucketSz[i] + padSz + memSz) <= ava) {
wolfSSL 15:117db924cf7c 503 ava -= bucketSz[i] + padSz + memSz;
wolfSSL 15:117db924cf7c 504 }
wolfSSL 15:117db924cf7c 505 }
wolfSSL 15:117db924cf7c 506 }
wolfSSL 15:117db924cf7c 507 }
wolfSSL 15:117db924cf7c 508 }
wolfSSL 15:117db924cf7c 509
wolfSSL 15:117db924cf7c 510 return sz - ava; /* round down */
wolfSSL 15:117db924cf7c 511 }
wolfSSL 15:117db924cf7c 512
wolfSSL 15:117db924cf7c 513
wolfSSL 15:117db924cf7c 514 int FreeFixedIO(WOLFSSL_HEAP* heap, wc_Memory** io)
wolfSSL 15:117db924cf7c 515 {
wolfSSL 15:117db924cf7c 516 WOLFSSL_MSG("Freeing fixed IO buffer");
wolfSSL 15:117db924cf7c 517
wolfSSL 15:117db924cf7c 518 /* check if fixed buffer was set */
wolfSSL 15:117db924cf7c 519 if (*io == NULL) {
wolfSSL 15:117db924cf7c 520 return 1;
wolfSSL 15:117db924cf7c 521 }
wolfSSL 15:117db924cf7c 522
wolfSSL 15:117db924cf7c 523 if (heap == NULL) {
wolfSSL 15:117db924cf7c 524 WOLFSSL_MSG("No heap to return fixed IO too");
wolfSSL 15:117db924cf7c 525 }
wolfSSL 15:117db924cf7c 526 else {
wolfSSL 15:117db924cf7c 527 /* put IO buffer back into IO pool */
wolfSSL 15:117db924cf7c 528 (*io)->next = heap->io;
wolfSSL 15:117db924cf7c 529 heap->io = *io;
wolfSSL 15:117db924cf7c 530 *io = NULL;
wolfSSL 15:117db924cf7c 531 }
wolfSSL 15:117db924cf7c 532
wolfSSL 15:117db924cf7c 533 return 1;
wolfSSL 15:117db924cf7c 534 }
wolfSSL 15:117db924cf7c 535
wolfSSL 15:117db924cf7c 536
wolfSSL 15:117db924cf7c 537 int SetFixedIO(WOLFSSL_HEAP* heap, wc_Memory** io)
wolfSSL 15:117db924cf7c 538 {
wolfSSL 15:117db924cf7c 539 WOLFSSL_MSG("Setting fixed IO for SSL");
wolfSSL 15:117db924cf7c 540 if (heap == NULL) {
wolfSSL 15:117db924cf7c 541 return MEMORY_E;
wolfSSL 15:117db924cf7c 542 }
wolfSSL 15:117db924cf7c 543
wolfSSL 15:117db924cf7c 544 *io = heap->io;
wolfSSL 15:117db924cf7c 545
wolfSSL 15:117db924cf7c 546 if (*io != NULL) {
wolfSSL 15:117db924cf7c 547 heap->io = (*io)->next;
wolfSSL 15:117db924cf7c 548 (*io)->next = NULL;
wolfSSL 15:117db924cf7c 549 }
wolfSSL 15:117db924cf7c 550 else { /* failed to grab an IO buffer */
wolfSSL 15:117db924cf7c 551 return 0;
wolfSSL 15:117db924cf7c 552 }
wolfSSL 15:117db924cf7c 553
wolfSSL 15:117db924cf7c 554 return 1;
wolfSSL 15:117db924cf7c 555 }
wolfSSL 15:117db924cf7c 556
wolfSSL 15:117db924cf7c 557
wolfSSL 15:117db924cf7c 558 int wolfSSL_GetMemStats(WOLFSSL_HEAP* heap, WOLFSSL_MEM_STATS* stats)
wolfSSL 15:117db924cf7c 559 {
wolfSSL 15:117db924cf7c 560 word32 i;
wolfSSL 15:117db924cf7c 561 wc_Memory* pt;
wolfSSL 15:117db924cf7c 562
wolfSSL 15:117db924cf7c 563 XMEMSET(stats, 0, sizeof(WOLFSSL_MEM_STATS));
wolfSSL 15:117db924cf7c 564
wolfSSL 15:117db924cf7c 565 stats->totalAlloc = heap->alloc;
wolfSSL 15:117db924cf7c 566 stats->totalFr = heap->frAlc;
wolfSSL 15:117db924cf7c 567 stats->curAlloc = stats->totalAlloc - stats->totalFr;
wolfSSL 15:117db924cf7c 568 stats->maxHa = heap->maxHa;
wolfSSL 15:117db924cf7c 569 stats->maxIO = heap->maxIO;
wolfSSL 15:117db924cf7c 570 for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
wolfSSL 15:117db924cf7c 571 stats->blockSz[i] = heap->sizeList[i];
wolfSSL 15:117db924cf7c 572 for (pt = heap->ava[i]; pt != NULL; pt = pt->next) {
wolfSSL 15:117db924cf7c 573 stats->avaBlock[i] += 1;
wolfSSL 15:117db924cf7c 574 }
wolfSSL 15:117db924cf7c 575 }
wolfSSL 15:117db924cf7c 576
wolfSSL 15:117db924cf7c 577 for (pt = heap->io; pt != NULL; pt = pt->next) {
wolfSSL 15:117db924cf7c 578 stats->avaIO++;
wolfSSL 15:117db924cf7c 579 }
wolfSSL 15:117db924cf7c 580
wolfSSL 15:117db924cf7c 581 stats->flag = heap->flag; /* flag used */
wolfSSL 15:117db924cf7c 582
wolfSSL 15:117db924cf7c 583 return 1;
wolfSSL 15:117db924cf7c 584 }
wolfSSL 15:117db924cf7c 585
wolfSSL 15:117db924cf7c 586
wolfSSL 15:117db924cf7c 587 #ifdef WOLFSSL_DEBUG_MEMORY
wolfSSL 15:117db924cf7c 588 void* wolfSSL_Malloc(size_t size, void* heap, int type, const char* func, unsigned int line)
wolfSSL 15:117db924cf7c 589 #else
wolfSSL 15:117db924cf7c 590 void* wolfSSL_Malloc(size_t size, void* heap, int type)
wolfSSL 15:117db924cf7c 591 #endif
wolfSSL 15:117db924cf7c 592 {
wolfSSL 15:117db924cf7c 593 void* res = 0;
wolfSSL 15:117db924cf7c 594 wc_Memory* pt = NULL;
wolfSSL 15:117db924cf7c 595 int i;
wolfSSL 15:117db924cf7c 596
wolfSSL 15:117db924cf7c 597 /* check for testing heap hint was set */
wolfSSL 15:117db924cf7c 598 #ifdef WOLFSSL_HEAP_TEST
wolfSSL 15:117db924cf7c 599 if (heap == (void*)WOLFSSL_HEAP_TEST) {
wolfSSL 15:117db924cf7c 600 return malloc(size);
wolfSSL 15:117db924cf7c 601 }
wolfSSL 15:117db924cf7c 602 #endif
wolfSSL 15:117db924cf7c 603
wolfSSL 15:117db924cf7c 604 /* if no heap hint then use dynamic memory*/
wolfSSL 15:117db924cf7c 605 if (heap == NULL) {
wolfSSL 15:117db924cf7c 606 #ifdef WOLFSSL_HEAP_TEST
wolfSSL 15:117db924cf7c 607 /* allow using malloc for creating ctx and method */
wolfSSL 15:117db924cf7c 608 if (type == DYNAMIC_TYPE_CTX || type == DYNAMIC_TYPE_METHOD ||
wolfSSL 15:117db924cf7c 609 type == DYNAMIC_TYPE_CERT_MANAGER) {
wolfSSL 15:117db924cf7c 610 WOLFSSL_MSG("ERROR allowing null heap hint for ctx/method\n");
wolfSSL 15:117db924cf7c 611 res = malloc(size);
wolfSSL 15:117db924cf7c 612 }
wolfSSL 15:117db924cf7c 613 else {
wolfSSL 15:117db924cf7c 614 WOLFSSL_MSG("ERROR null heap hint passed into XMALLOC\n");
wolfSSL 15:117db924cf7c 615 res = NULL;
wolfSSL 15:117db924cf7c 616 }
wolfSSL 15:117db924cf7c 617 #else
wolfSSL 15:117db924cf7c 618 #ifndef WOLFSSL_NO_MALLOC
wolfSSL 15:117db924cf7c 619 #ifdef FREERTOS
wolfSSL 15:117db924cf7c 620 res = pvPortMalloc(size);
wolfSSL 15:117db924cf7c 621 #else
wolfSSL 15:117db924cf7c 622 res = malloc(size);
wolfSSL 15:117db924cf7c 623 #endif
wolfSSL 15:117db924cf7c 624 #else
wolfSSL 15:117db924cf7c 625 WOLFSSL_MSG("No heap hint found to use and no malloc");
wolfSSL 15:117db924cf7c 626 #ifdef WOLFSSL_DEBUG_MEMORY
wolfSSL 15:117db924cf7c 627 printf("ERROR: at %s:%d\n", func, line);
wolfSSL 15:117db924cf7c 628 #endif
wolfSSL 15:117db924cf7c 629 #endif /* WOLFSSL_NO_MALLOC */
wolfSSL 15:117db924cf7c 630 #endif /* WOLFSSL_HEAP_TEST */
wolfSSL 15:117db924cf7c 631 }
wolfSSL 15:117db924cf7c 632 else {
wolfSSL 15:117db924cf7c 633 WOLFSSL_HEAP_HINT* hint = (WOLFSSL_HEAP_HINT*)heap;
wolfSSL 15:117db924cf7c 634 WOLFSSL_HEAP* mem = hint->memory;
wolfSSL 15:117db924cf7c 635
wolfSSL 15:117db924cf7c 636 if (wc_LockMutex(&(mem->memory_mutex)) != 0) {
wolfSSL 15:117db924cf7c 637 WOLFSSL_MSG("Bad memory_mutex lock");
wolfSSL 15:117db924cf7c 638 return NULL;
wolfSSL 15:117db924cf7c 639 }
wolfSSL 15:117db924cf7c 640
wolfSSL 15:117db924cf7c 641 /* case of using fixed IO buffers */
wolfSSL 15:117db924cf7c 642 if (mem->flag & WOLFMEM_IO_POOL_FIXED &&
wolfSSL 15:117db924cf7c 643 (type == DYNAMIC_TYPE_OUT_BUFFER ||
wolfSSL 15:117db924cf7c 644 type == DYNAMIC_TYPE_IN_BUFFER)) {
wolfSSL 15:117db924cf7c 645 if (type == DYNAMIC_TYPE_OUT_BUFFER) {
wolfSSL 15:117db924cf7c 646 pt = hint->outBuf;
wolfSSL 15:117db924cf7c 647 }
wolfSSL 15:117db924cf7c 648 if (type == DYNAMIC_TYPE_IN_BUFFER) {
wolfSSL 15:117db924cf7c 649 pt = hint->inBuf;
wolfSSL 15:117db924cf7c 650 }
wolfSSL 15:117db924cf7c 651 }
wolfSSL 15:117db924cf7c 652 else {
wolfSSL 15:117db924cf7c 653 /* check if using IO pool flag */
wolfSSL 15:117db924cf7c 654 if (mem->flag & WOLFMEM_IO_POOL &&
wolfSSL 15:117db924cf7c 655 (type == DYNAMIC_TYPE_OUT_BUFFER ||
wolfSSL 15:117db924cf7c 656 type == DYNAMIC_TYPE_IN_BUFFER)) {
wolfSSL 15:117db924cf7c 657 if (mem->io != NULL) {
wolfSSL 15:117db924cf7c 658 pt = mem->io;
wolfSSL 15:117db924cf7c 659 mem->io = pt->next;
wolfSSL 15:117db924cf7c 660 }
wolfSSL 15:117db924cf7c 661 }
wolfSSL 15:117db924cf7c 662
wolfSSL 15:117db924cf7c 663 /* general static memory */
wolfSSL 15:117db924cf7c 664 if (pt == NULL) {
wolfSSL 15:117db924cf7c 665 for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
wolfSSL 15:117db924cf7c 666 if ((word32)size < mem->sizeList[i]) {
wolfSSL 15:117db924cf7c 667 if (mem->ava[i] != NULL) {
wolfSSL 15:117db924cf7c 668 pt = mem->ava[i];
wolfSSL 15:117db924cf7c 669 mem->ava[i] = pt->next;
wolfSSL 15:117db924cf7c 670 break;
wolfSSL 15:117db924cf7c 671 }
wolfSSL 16:8e0d178b1d1e 672 #ifdef WOLFSSL_DEBUG_STATIC_MEMORY
wolfSSL 16:8e0d178b1d1e 673 else {
wolfSSL 16:8e0d178b1d1e 674 printf("Size: %ld, Empty: %d\n", size,
wolfSSL 16:8e0d178b1d1e 675 mem->sizeList[i]);
wolfSSL 16:8e0d178b1d1e 676 }
wolfSSL 16:8e0d178b1d1e 677 #endif
wolfSSL 15:117db924cf7c 678 }
wolfSSL 15:117db924cf7c 679 }
wolfSSL 15:117db924cf7c 680 }
wolfSSL 15:117db924cf7c 681 }
wolfSSL 15:117db924cf7c 682
wolfSSL 15:117db924cf7c 683 if (pt != NULL) {
wolfSSL 15:117db924cf7c 684 mem->inUse += pt->sz;
wolfSSL 15:117db924cf7c 685 mem->alloc += 1;
wolfSSL 15:117db924cf7c 686 res = pt->buffer;
wolfSSL 15:117db924cf7c 687
wolfSSL 15:117db924cf7c 688 #ifdef WOLFSSL_DEBUG_MEMORY
wolfSSL 15:117db924cf7c 689 printf("Alloc: %p -> %u at %s:%d\n", pt->buffer, pt->sz, func, line);
wolfSSL 15:117db924cf7c 690 #endif
wolfSSL 15:117db924cf7c 691
wolfSSL 15:117db924cf7c 692 /* keep track of connection statistics if flag is set */
wolfSSL 15:117db924cf7c 693 if (mem->flag & WOLFMEM_TRACK_STATS) {
wolfSSL 15:117db924cf7c 694 WOLFSSL_MEM_CONN_STATS* stats = hint->stats;
wolfSSL 15:117db924cf7c 695 if (stats != NULL) {
wolfSSL 15:117db924cf7c 696 stats->curMem += pt->sz;
wolfSSL 15:117db924cf7c 697 if (stats->peakMem < stats->curMem) {
wolfSSL 15:117db924cf7c 698 stats->peakMem = stats->curMem;
wolfSSL 15:117db924cf7c 699 }
wolfSSL 15:117db924cf7c 700 stats->curAlloc++;
wolfSSL 15:117db924cf7c 701 if (stats->peakAlloc < stats->curAlloc) {
wolfSSL 15:117db924cf7c 702 stats->peakAlloc = stats->curAlloc;
wolfSSL 15:117db924cf7c 703 }
wolfSSL 15:117db924cf7c 704 stats->totalAlloc++;
wolfSSL 15:117db924cf7c 705 }
wolfSSL 15:117db924cf7c 706 }
wolfSSL 15:117db924cf7c 707 }
wolfSSL 15:117db924cf7c 708 else {
wolfSSL 15:117db924cf7c 709 WOLFSSL_MSG("ERROR ran out of static memory");
wolfSSL 15:117db924cf7c 710 #ifdef WOLFSSL_DEBUG_MEMORY
wolfSSL 15:117db924cf7c 711 printf("Looking for %lu bytes at %s:%d\n", size, func, line);
wolfSSL 15:117db924cf7c 712 #endif
wolfSSL 15:117db924cf7c 713 }
wolfSSL 15:117db924cf7c 714
wolfSSL 15:117db924cf7c 715 wc_UnLockMutex(&(mem->memory_mutex));
wolfSSL 15:117db924cf7c 716 }
wolfSSL 15:117db924cf7c 717
wolfSSL 15:117db924cf7c 718 #ifdef WOLFSSL_MALLOC_CHECK
wolfSSL 15:117db924cf7c 719 if ((wolfssl_word)res % WOLFSSL_STATIC_ALIGN) {
wolfSSL 16:8e0d178b1d1e 720 WOLFSSL_MSG("ERROR memory is not aligned");
wolfSSL 15:117db924cf7c 721 res = NULL;
wolfSSL 15:117db924cf7c 722 }
wolfSSL 15:117db924cf7c 723 #endif
wolfSSL 15:117db924cf7c 724
wolfSSL 15:117db924cf7c 725
wolfSSL 15:117db924cf7c 726 (void)i;
wolfSSL 15:117db924cf7c 727 (void)pt;
wolfSSL 15:117db924cf7c 728 (void)type;
wolfSSL 15:117db924cf7c 729
wolfSSL 15:117db924cf7c 730 return res;
wolfSSL 15:117db924cf7c 731 }
wolfSSL 15:117db924cf7c 732
wolfSSL 15:117db924cf7c 733
wolfSSL 15:117db924cf7c 734 #ifdef WOLFSSL_DEBUG_MEMORY
wolfSSL 15:117db924cf7c 735 void wolfSSL_Free(void *ptr, void* heap, int type, const char* func, unsigned int line)
wolfSSL 15:117db924cf7c 736 #else
wolfSSL 15:117db924cf7c 737 void wolfSSL_Free(void *ptr, void* heap, int type)
wolfSSL 15:117db924cf7c 738 #endif
wolfSSL 15:117db924cf7c 739 {
wolfSSL 15:117db924cf7c 740 int i;
wolfSSL 15:117db924cf7c 741 wc_Memory* pt;
wolfSSL 15:117db924cf7c 742
wolfSSL 15:117db924cf7c 743 if (ptr) {
wolfSSL 15:117db924cf7c 744 /* check for testing heap hint was set */
wolfSSL 15:117db924cf7c 745 #ifdef WOLFSSL_HEAP_TEST
wolfSSL 15:117db924cf7c 746 if (heap == (void*)WOLFSSL_HEAP_TEST) {
wolfSSL 15:117db924cf7c 747 return free(ptr);
wolfSSL 15:117db924cf7c 748 }
wolfSSL 15:117db924cf7c 749 #endif
wolfSSL 15:117db924cf7c 750
wolfSSL 15:117db924cf7c 751 if (heap == NULL) {
wolfSSL 15:117db924cf7c 752 #ifdef WOLFSSL_HEAP_TEST
wolfSSL 15:117db924cf7c 753 /* allow using malloc for creating ctx and method */
wolfSSL 15:117db924cf7c 754 if (type == DYNAMIC_TYPE_CTX || type == DYNAMIC_TYPE_METHOD ||
wolfSSL 15:117db924cf7c 755 type == DYNAMIC_TYPE_CERT_MANAGER) {
wolfSSL 15:117db924cf7c 756 WOLFSSL_MSG("ERROR allowing null heap hint for ctx/method\n");
wolfSSL 15:117db924cf7c 757 }
wolfSSL 15:117db924cf7c 758 else {
wolfSSL 15:117db924cf7c 759 WOLFSSL_MSG("ERROR null heap hint passed into XFREE\n");
wolfSSL 15:117db924cf7c 760 }
wolfSSL 15:117db924cf7c 761 #endif
wolfSSL 15:117db924cf7c 762 #ifndef WOLFSSL_NO_MALLOC
wolfSSL 15:117db924cf7c 763 #ifdef FREERTOS
wolfSSL 15:117db924cf7c 764 vPortFree(ptr);
wolfSSL 15:117db924cf7c 765 #else
wolfSSL 15:117db924cf7c 766 free(ptr);
wolfSSL 15:117db924cf7c 767 #endif
wolfSSL 15:117db924cf7c 768 #else
wolfSSL 15:117db924cf7c 769 WOLFSSL_MSG("Error trying to call free when turned off");
wolfSSL 15:117db924cf7c 770 #endif /* WOLFSSL_NO_MALLOC */
wolfSSL 15:117db924cf7c 771 }
wolfSSL 15:117db924cf7c 772 else {
wolfSSL 15:117db924cf7c 773 WOLFSSL_HEAP_HINT* hint = (WOLFSSL_HEAP_HINT*)heap;
wolfSSL 15:117db924cf7c 774 WOLFSSL_HEAP* mem = hint->memory;
wolfSSL 15:117db924cf7c 775 word32 padSz = -(int)sizeof(wc_Memory) & (WOLFSSL_STATIC_ALIGN - 1);
wolfSSL 15:117db924cf7c 776
wolfSSL 15:117db924cf7c 777 /* get memory struct and add it to available list */
wolfSSL 15:117db924cf7c 778 pt = (wc_Memory*)((byte*)ptr - sizeof(wc_Memory) - padSz);
wolfSSL 15:117db924cf7c 779 if (wc_LockMutex(&(mem->memory_mutex)) != 0) {
wolfSSL 15:117db924cf7c 780 WOLFSSL_MSG("Bad memory_mutex lock");
wolfSSL 15:117db924cf7c 781 return;
wolfSSL 15:117db924cf7c 782 }
wolfSSL 15:117db924cf7c 783
wolfSSL 15:117db924cf7c 784 /* case of using fixed IO buffers */
wolfSSL 15:117db924cf7c 785 if (mem->flag & WOLFMEM_IO_POOL_FIXED &&
wolfSSL 15:117db924cf7c 786 (type == DYNAMIC_TYPE_OUT_BUFFER ||
wolfSSL 15:117db924cf7c 787 type == DYNAMIC_TYPE_IN_BUFFER)) {
wolfSSL 15:117db924cf7c 788 /* fixed IO pools are free'd at the end of SSL lifetime
wolfSSL 15:117db924cf7c 789 using FreeFixedIO(WOLFSSL_HEAP* heap, wc_Memory** io) */
wolfSSL 15:117db924cf7c 790 }
wolfSSL 15:117db924cf7c 791 else if (mem->flag & WOLFMEM_IO_POOL && pt->sz == WOLFMEM_IO_SZ &&
wolfSSL 15:117db924cf7c 792 (type == DYNAMIC_TYPE_OUT_BUFFER ||
wolfSSL 15:117db924cf7c 793 type == DYNAMIC_TYPE_IN_BUFFER)) {
wolfSSL 15:117db924cf7c 794 pt->next = mem->io;
wolfSSL 15:117db924cf7c 795 mem->io = pt;
wolfSSL 15:117db924cf7c 796 }
wolfSSL 15:117db924cf7c 797 else { /* general memory free */
wolfSSL 15:117db924cf7c 798 for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
wolfSSL 15:117db924cf7c 799 if (pt->sz == mem->sizeList[i]) {
wolfSSL 15:117db924cf7c 800 pt->next = mem->ava[i];
wolfSSL 15:117db924cf7c 801 mem->ava[i] = pt;
wolfSSL 15:117db924cf7c 802 break;
wolfSSL 15:117db924cf7c 803 }
wolfSSL 15:117db924cf7c 804 }
wolfSSL 15:117db924cf7c 805 }
wolfSSL 15:117db924cf7c 806 mem->inUse -= pt->sz;
wolfSSL 15:117db924cf7c 807 mem->frAlc += 1;
wolfSSL 15:117db924cf7c 808
wolfSSL 15:117db924cf7c 809 #ifdef WOLFSSL_DEBUG_MEMORY
wolfSSL 15:117db924cf7c 810 printf("Free: %p -> %u at %s:%d\n", pt->buffer, pt->sz, func, line);
wolfSSL 15:117db924cf7c 811 #endif
wolfSSL 15:117db924cf7c 812
wolfSSL 15:117db924cf7c 813 /* keep track of connection statistics if flag is set */
wolfSSL 15:117db924cf7c 814 if (mem->flag & WOLFMEM_TRACK_STATS) {
wolfSSL 15:117db924cf7c 815 WOLFSSL_MEM_CONN_STATS* stats = hint->stats;
wolfSSL 15:117db924cf7c 816 if (stats != NULL) {
wolfSSL 15:117db924cf7c 817 /* avoid under flow */
wolfSSL 15:117db924cf7c 818 if (stats->curMem > pt->sz) {
wolfSSL 15:117db924cf7c 819 stats->curMem -= pt->sz;
wolfSSL 15:117db924cf7c 820 }
wolfSSL 15:117db924cf7c 821 else {
wolfSSL 15:117db924cf7c 822 stats->curMem = 0;
wolfSSL 15:117db924cf7c 823 }
wolfSSL 15:117db924cf7c 824
wolfSSL 15:117db924cf7c 825 if (stats->curAlloc > 0) {
wolfSSL 15:117db924cf7c 826 stats->curAlloc--;
wolfSSL 15:117db924cf7c 827 }
wolfSSL 15:117db924cf7c 828 stats->totalFr++;
wolfSSL 15:117db924cf7c 829 }
wolfSSL 15:117db924cf7c 830 }
wolfSSL 15:117db924cf7c 831 wc_UnLockMutex(&(mem->memory_mutex));
wolfSSL 15:117db924cf7c 832 }
wolfSSL 15:117db924cf7c 833 }
wolfSSL 15:117db924cf7c 834
wolfSSL 15:117db924cf7c 835 (void)i;
wolfSSL 15:117db924cf7c 836 (void)pt;
wolfSSL 15:117db924cf7c 837 (void)type;
wolfSSL 15:117db924cf7c 838 }
wolfSSL 15:117db924cf7c 839
wolfSSL 15:117db924cf7c 840 #ifdef WOLFSSL_DEBUG_MEMORY
wolfSSL 15:117db924cf7c 841 void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type, const char* func, unsigned int line)
wolfSSL 15:117db924cf7c 842 #else
wolfSSL 15:117db924cf7c 843 void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type)
wolfSSL 15:117db924cf7c 844 #endif
wolfSSL 15:117db924cf7c 845 {
wolfSSL 15:117db924cf7c 846 void* res = 0;
wolfSSL 15:117db924cf7c 847 wc_Memory* pt = NULL;
wolfSSL 15:117db924cf7c 848 word32 prvSz;
wolfSSL 15:117db924cf7c 849 int i;
wolfSSL 15:117db924cf7c 850
wolfSSL 15:117db924cf7c 851 /* check for testing heap hint was set */
wolfSSL 15:117db924cf7c 852 #ifdef WOLFSSL_HEAP_TEST
wolfSSL 15:117db924cf7c 853 if (heap == (void*)WOLFSSL_HEAP_TEST) {
wolfSSL 15:117db924cf7c 854 return realloc(ptr, size);
wolfSSL 15:117db924cf7c 855 }
wolfSSL 15:117db924cf7c 856 #endif
wolfSSL 15:117db924cf7c 857
wolfSSL 15:117db924cf7c 858 if (heap == NULL) {
wolfSSL 15:117db924cf7c 859 #ifdef WOLFSSL_HEAP_TEST
wolfSSL 15:117db924cf7c 860 WOLFSSL_MSG("ERROR null heap hint passed in to XREALLOC\n");
wolfSSL 15:117db924cf7c 861 #endif
wolfSSL 15:117db924cf7c 862 #ifndef WOLFSSL_NO_MALLOC
wolfSSL 15:117db924cf7c 863 res = realloc(ptr, size);
wolfSSL 15:117db924cf7c 864 #else
wolfSSL 15:117db924cf7c 865 WOLFSSL_MSG("NO heap found to use for realloc");
wolfSSL 15:117db924cf7c 866 #endif /* WOLFSSL_NO_MALLOC */
wolfSSL 15:117db924cf7c 867 }
wolfSSL 15:117db924cf7c 868 else {
wolfSSL 15:117db924cf7c 869 WOLFSSL_HEAP_HINT* hint = (WOLFSSL_HEAP_HINT*)heap;
wolfSSL 15:117db924cf7c 870 WOLFSSL_HEAP* mem = hint->memory;
wolfSSL 15:117db924cf7c 871 word32 padSz = -(int)sizeof(wc_Memory) & (WOLFSSL_STATIC_ALIGN - 1);
wolfSSL 15:117db924cf7c 872
wolfSSL 16:8e0d178b1d1e 873 if (ptr == NULL) {
wolfSSL 16:8e0d178b1d1e 874 #ifdef WOLFSSL_DEBUG_MEMORY
wolfSSL 16:8e0d178b1d1e 875 return wolfSSL_Malloc(size, heap, type, func, line);
wolfSSL 16:8e0d178b1d1e 876 #else
wolfSSL 16:8e0d178b1d1e 877 return wolfSSL_Malloc(size, heap, type);
wolfSSL 16:8e0d178b1d1e 878 #endif
wolfSSL 16:8e0d178b1d1e 879 }
wolfSSL 16:8e0d178b1d1e 880
wolfSSL 15:117db924cf7c 881 if (wc_LockMutex(&(mem->memory_mutex)) != 0) {
wolfSSL 15:117db924cf7c 882 WOLFSSL_MSG("Bad memory_mutex lock");
wolfSSL 15:117db924cf7c 883 return NULL;
wolfSSL 15:117db924cf7c 884 }
wolfSSL 15:117db924cf7c 885
wolfSSL 15:117db924cf7c 886 /* case of using fixed IO buffers or IO pool */
wolfSSL 15:117db924cf7c 887 if (((mem->flag & WOLFMEM_IO_POOL)||(mem->flag & WOLFMEM_IO_POOL_FIXED))
wolfSSL 15:117db924cf7c 888 && (type == DYNAMIC_TYPE_OUT_BUFFER ||
wolfSSL 15:117db924cf7c 889 type == DYNAMIC_TYPE_IN_BUFFER)) {
wolfSSL 15:117db924cf7c 890 /* no realloc, is fixed size */
wolfSSL 15:117db924cf7c 891 pt = (wc_Memory*)((byte*)ptr - padSz - sizeof(wc_Memory));
wolfSSL 15:117db924cf7c 892 if (pt->sz < size) {
wolfSSL 15:117db924cf7c 893 WOLFSSL_MSG("Error IO memory was not large enough");
wolfSSL 15:117db924cf7c 894 res = NULL; /* return NULL in error case */
wolfSSL 15:117db924cf7c 895 }
wolfSSL 15:117db924cf7c 896 res = pt->buffer;
wolfSSL 15:117db924cf7c 897 }
wolfSSL 15:117db924cf7c 898 else {
wolfSSL 15:117db924cf7c 899 /* general memory */
wolfSSL 15:117db924cf7c 900 for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
wolfSSL 15:117db924cf7c 901 if ((word32)size < mem->sizeList[i]) {
wolfSSL 15:117db924cf7c 902 if (mem->ava[i] != NULL) {
wolfSSL 15:117db924cf7c 903 pt = mem->ava[i];
wolfSSL 15:117db924cf7c 904 mem->ava[i] = pt->next;
wolfSSL 15:117db924cf7c 905 break;
wolfSSL 15:117db924cf7c 906 }
wolfSSL 15:117db924cf7c 907 }
wolfSSL 15:117db924cf7c 908 }
wolfSSL 15:117db924cf7c 909
wolfSSL 15:117db924cf7c 910 if (pt != NULL && res == NULL) {
wolfSSL 15:117db924cf7c 911 res = pt->buffer;
wolfSSL 15:117db924cf7c 912
wolfSSL 15:117db924cf7c 913 /* copy over original information and free ptr */
wolfSSL 15:117db924cf7c 914 prvSz = ((wc_Memory*)((byte*)ptr - padSz -
wolfSSL 15:117db924cf7c 915 sizeof(wc_Memory)))->sz;
wolfSSL 15:117db924cf7c 916 prvSz = (prvSz > pt->sz)? pt->sz: prvSz;
wolfSSL 15:117db924cf7c 917 XMEMCPY(pt->buffer, ptr, prvSz);
wolfSSL 15:117db924cf7c 918 mem->inUse += pt->sz;
wolfSSL 15:117db924cf7c 919 mem->alloc += 1;
wolfSSL 15:117db924cf7c 920
wolfSSL 15:117db924cf7c 921 /* free memory that was previously being used */
wolfSSL 15:117db924cf7c 922 wc_UnLockMutex(&(mem->memory_mutex));
wolfSSL 15:117db924cf7c 923 wolfSSL_Free(ptr, heap, type
wolfSSL 15:117db924cf7c 924 #ifdef WOLFSSL_DEBUG_MEMORY
wolfSSL 15:117db924cf7c 925 , func, line
wolfSSL 15:117db924cf7c 926 #endif
wolfSSL 15:117db924cf7c 927 );
wolfSSL 15:117db924cf7c 928 if (wc_LockMutex(&(mem->memory_mutex)) != 0) {
wolfSSL 15:117db924cf7c 929 WOLFSSL_MSG("Bad memory_mutex lock");
wolfSSL 15:117db924cf7c 930 return NULL;
wolfSSL 15:117db924cf7c 931 }
wolfSSL 15:117db924cf7c 932 }
wolfSSL 15:117db924cf7c 933 }
wolfSSL 15:117db924cf7c 934 wc_UnLockMutex(&(mem->memory_mutex));
wolfSSL 15:117db924cf7c 935 }
wolfSSL 15:117db924cf7c 936
wolfSSL 15:117db924cf7c 937 #ifdef WOLFSSL_MALLOC_CHECK
wolfSSL 15:117db924cf7c 938 if ((wolfssl_word)res % WOLFSSL_STATIC_ALIGN) {
wolfSSL 16:8e0d178b1d1e 939 WOLFSSL_MSG("ERROR memory is not aligned");
wolfSSL 15:117db924cf7c 940 res = NULL;
wolfSSL 15:117db924cf7c 941 }
wolfSSL 15:117db924cf7c 942 #endif
wolfSSL 15:117db924cf7c 943
wolfSSL 15:117db924cf7c 944 (void)i;
wolfSSL 15:117db924cf7c 945 (void)pt;
wolfSSL 15:117db924cf7c 946 (void)type;
wolfSSL 15:117db924cf7c 947
wolfSSL 15:117db924cf7c 948 return res;
wolfSSL 15:117db924cf7c 949 }
wolfSSL 15:117db924cf7c 950 #endif /* WOLFSSL_STATIC_MEMORY */
wolfSSL 15:117db924cf7c 951
wolfSSL 15:117db924cf7c 952 #endif /* USE_WOLFSSL_MEMORY */
wolfSSL 15:117db924cf7c 953
wolfSSL 15:117db924cf7c 954
wolfSSL 15:117db924cf7c 955 #ifdef HAVE_IO_POOL
wolfSSL 15:117db924cf7c 956
wolfSSL 15:117db924cf7c 957 /* Example for user io pool, shared build may need definitions in lib proper */
wolfSSL 15:117db924cf7c 958
wolfSSL 15:117db924cf7c 959 #include <wolfssl/wolfcrypt/types.h>
wolfSSL 15:117db924cf7c 960 #include <stdlib.h>
wolfSSL 15:117db924cf7c 961
wolfSSL 15:117db924cf7c 962 #ifndef HAVE_THREAD_LS
wolfSSL 15:117db924cf7c 963 #error "Oops, simple I/O pool example needs thread local storage"
wolfSSL 15:117db924cf7c 964 #endif
wolfSSL 15:117db924cf7c 965
wolfSSL 15:117db924cf7c 966
wolfSSL 15:117db924cf7c 967 /* allow simple per thread in and out pools */
wolfSSL 15:117db924cf7c 968 /* use 17k size since max record size is 16k plus overhead */
wolfSSL 15:117db924cf7c 969 static THREAD_LS_T byte pool_in[17*1024];
wolfSSL 15:117db924cf7c 970 static THREAD_LS_T byte pool_out[17*1024];
wolfSSL 15:117db924cf7c 971
wolfSSL 15:117db924cf7c 972
wolfSSL 15:117db924cf7c 973 void* XMALLOC(size_t n, void* heap, int type)
wolfSSL 15:117db924cf7c 974 {
wolfSSL 15:117db924cf7c 975 (void)heap;
wolfSSL 15:117db924cf7c 976
wolfSSL 15:117db924cf7c 977 if (type == DYNAMIC_TYPE_IN_BUFFER) {
wolfSSL 15:117db924cf7c 978 if (n < sizeof(pool_in))
wolfSSL 15:117db924cf7c 979 return pool_in;
wolfSSL 15:117db924cf7c 980 else
wolfSSL 15:117db924cf7c 981 return NULL;
wolfSSL 15:117db924cf7c 982 }
wolfSSL 15:117db924cf7c 983
wolfSSL 15:117db924cf7c 984 if (type == DYNAMIC_TYPE_OUT_BUFFER) {
wolfSSL 15:117db924cf7c 985 if (n < sizeof(pool_out))
wolfSSL 15:117db924cf7c 986 return pool_out;
wolfSSL 15:117db924cf7c 987 else
wolfSSL 15:117db924cf7c 988 return NULL;
wolfSSL 15:117db924cf7c 989 }
wolfSSL 15:117db924cf7c 990
wolfSSL 15:117db924cf7c 991 return malloc(n);
wolfSSL 15:117db924cf7c 992 }
wolfSSL 15:117db924cf7c 993
wolfSSL 15:117db924cf7c 994 void* XREALLOC(void *p, size_t n, void* heap, int type)
wolfSSL 15:117db924cf7c 995 {
wolfSSL 15:117db924cf7c 996 (void)heap;
wolfSSL 15:117db924cf7c 997
wolfSSL 15:117db924cf7c 998 if (type == DYNAMIC_TYPE_IN_BUFFER) {
wolfSSL 15:117db924cf7c 999 if (n < sizeof(pool_in))
wolfSSL 15:117db924cf7c 1000 return pool_in;
wolfSSL 15:117db924cf7c 1001 else
wolfSSL 15:117db924cf7c 1002 return NULL;
wolfSSL 15:117db924cf7c 1003 }
wolfSSL 15:117db924cf7c 1004
wolfSSL 15:117db924cf7c 1005 if (type == DYNAMIC_TYPE_OUT_BUFFER) {
wolfSSL 15:117db924cf7c 1006 if (n < sizeof(pool_out))
wolfSSL 15:117db924cf7c 1007 return pool_out;
wolfSSL 15:117db924cf7c 1008 else
wolfSSL 15:117db924cf7c 1009 return NULL;
wolfSSL 15:117db924cf7c 1010 }
wolfSSL 15:117db924cf7c 1011
wolfSSL 15:117db924cf7c 1012 return realloc(p, n);
wolfSSL 15:117db924cf7c 1013 }
wolfSSL 15:117db924cf7c 1014
wolfSSL 15:117db924cf7c 1015 void XFREE(void *p, void* heap, int type)
wolfSSL 15:117db924cf7c 1016 {
wolfSSL 15:117db924cf7c 1017 (void)heap;
wolfSSL 15:117db924cf7c 1018
wolfSSL 15:117db924cf7c 1019 if (type == DYNAMIC_TYPE_IN_BUFFER)
wolfSSL 15:117db924cf7c 1020 return; /* do nothing, static pool */
wolfSSL 15:117db924cf7c 1021
wolfSSL 15:117db924cf7c 1022 if (type == DYNAMIC_TYPE_OUT_BUFFER)
wolfSSL 15:117db924cf7c 1023 return; /* do nothing, static pool */
wolfSSL 15:117db924cf7c 1024
wolfSSL 15:117db924cf7c 1025 free(p);
wolfSSL 15:117db924cf7c 1026 }
wolfSSL 15:117db924cf7c 1027
wolfSSL 15:117db924cf7c 1028 #endif /* HAVE_IO_POOL */
wolfSSL 15:117db924cf7c 1029
wolfSSL 16:8e0d178b1d1e 1030 #ifdef WOLFSSL_MEMORY_LOG
wolfSSL 16:8e0d178b1d1e 1031 void *xmalloc(size_t n, void* heap, int type, const char* func,
wolfSSL 16:8e0d178b1d1e 1032 const char* file, unsigned int line)
wolfSSL 16:8e0d178b1d1e 1033 {
wolfSSL 16:8e0d178b1d1e 1034 void* p;
wolfSSL 16:8e0d178b1d1e 1035 word32* p32;
wolfSSL 16:8e0d178b1d1e 1036
wolfSSL 16:8e0d178b1d1e 1037 if (malloc_function)
wolfSSL 16:8e0d178b1d1e 1038 p32 = malloc_function(n + sizeof(word32) * 4);
wolfSSL 16:8e0d178b1d1e 1039 else
wolfSSL 16:8e0d178b1d1e 1040 p32 = malloc(n + sizeof(word32) * 4);
wolfSSL 16:8e0d178b1d1e 1041
wolfSSL 16:8e0d178b1d1e 1042 p32[0] = (word32)n;
wolfSSL 16:8e0d178b1d1e 1043 p = (void*)(p32 + 4);
wolfSSL 16:8e0d178b1d1e 1044
wolfSSL 16:8e0d178b1d1e 1045 fprintf(stderr, "Alloc: %p -> %u (%d) at %s:%s:%u\n", p, (word32)n, type,
wolfSSL 16:8e0d178b1d1e 1046 func, file, line);
wolfSSL 16:8e0d178b1d1e 1047
wolfSSL 16:8e0d178b1d1e 1048 (void)heap;
wolfSSL 16:8e0d178b1d1e 1049
wolfSSL 16:8e0d178b1d1e 1050 return p;
wolfSSL 16:8e0d178b1d1e 1051 }
wolfSSL 16:8e0d178b1d1e 1052 void *xrealloc(void *p, size_t n, void* heap, int type, const char* func,
wolfSSL 16:8e0d178b1d1e 1053 const char* file, unsigned int line)
wolfSSL 16:8e0d178b1d1e 1054 {
wolfSSL 16:8e0d178b1d1e 1055 void* newp = NULL;
wolfSSL 16:8e0d178b1d1e 1056 word32* p32;
wolfSSL 16:8e0d178b1d1e 1057 word32* oldp32 = NULL;
wolfSSL 16:8e0d178b1d1e 1058 word32 oldLen;
wolfSSL 16:8e0d178b1d1e 1059
wolfSSL 16:8e0d178b1d1e 1060 if (p != NULL) {
wolfSSL 16:8e0d178b1d1e 1061 oldp32 = (word32*)p;
wolfSSL 16:8e0d178b1d1e 1062 oldp32 -= 4;
wolfSSL 16:8e0d178b1d1e 1063 oldLen = oldp32[0];
wolfSSL 16:8e0d178b1d1e 1064 }
wolfSSL 16:8e0d178b1d1e 1065
wolfSSL 16:8e0d178b1d1e 1066 if (realloc_function)
wolfSSL 16:8e0d178b1d1e 1067 p32 = realloc_function(oldp32, n + sizeof(word32) * 4);
wolfSSL 16:8e0d178b1d1e 1068 else
wolfSSL 16:8e0d178b1d1e 1069 p32 = realloc(oldp32, n + sizeof(word32) * 4);
wolfSSL 16:8e0d178b1d1e 1070
wolfSSL 16:8e0d178b1d1e 1071 if (p32 != NULL) {
wolfSSL 16:8e0d178b1d1e 1072 p32[0] = (word32)n;
wolfSSL 16:8e0d178b1d1e 1073 newp = (void*)(p32 + 4);
wolfSSL 16:8e0d178b1d1e 1074
wolfSSL 16:8e0d178b1d1e 1075 fprintf(stderr, "Alloc: %p -> %u (%d) at %s:%s:%u\n", newp, (word32)n,
wolfSSL 16:8e0d178b1d1e 1076 type, func, file, line);
wolfSSL 16:8e0d178b1d1e 1077 if (p != NULL) {
wolfSSL 16:8e0d178b1d1e 1078 fprintf(stderr, "Free: %p -> %u (%d) at %s:%s:%u\n", p, oldLen,
wolfSSL 16:8e0d178b1d1e 1079 type, func, file, line);
wolfSSL 16:8e0d178b1d1e 1080 }
wolfSSL 16:8e0d178b1d1e 1081 }
wolfSSL 16:8e0d178b1d1e 1082
wolfSSL 16:8e0d178b1d1e 1083 (void)heap;
wolfSSL 16:8e0d178b1d1e 1084
wolfSSL 16:8e0d178b1d1e 1085 return newp;
wolfSSL 16:8e0d178b1d1e 1086 }
wolfSSL 16:8e0d178b1d1e 1087 void xfree(void *p, void* heap, int type, const char* func, const char* file,
wolfSSL 16:8e0d178b1d1e 1088 unsigned int line)
wolfSSL 16:8e0d178b1d1e 1089 {
wolfSSL 16:8e0d178b1d1e 1090 word32* p32 = (word32*)p;
wolfSSL 16:8e0d178b1d1e 1091
wolfSSL 16:8e0d178b1d1e 1092 if (p != NULL) {
wolfSSL 16:8e0d178b1d1e 1093 p32 -= 4;
wolfSSL 16:8e0d178b1d1e 1094
wolfSSL 16:8e0d178b1d1e 1095 fprintf(stderr, "Free: %p -> %u (%d) at %s:%s:%u\n", p, p32[0], type,
wolfSSL 16:8e0d178b1d1e 1096 func, file, line);
wolfSSL 16:8e0d178b1d1e 1097
wolfSSL 16:8e0d178b1d1e 1098 if (free_function)
wolfSSL 16:8e0d178b1d1e 1099 free_function(p32);
wolfSSL 16:8e0d178b1d1e 1100 else
wolfSSL 16:8e0d178b1d1e 1101 free(p32);
wolfSSL 16:8e0d178b1d1e 1102 }
wolfSSL 16:8e0d178b1d1e 1103
wolfSSL 16:8e0d178b1d1e 1104 (void)heap;
wolfSSL 16:8e0d178b1d1e 1105 }
wolfSSL 16:8e0d178b1d1e 1106 #endif /* WOLFSSL_MEMORY_LOG */
wolfSSL 16:8e0d178b1d1e 1107
wolfSSL 16:8e0d178b1d1e 1108 #ifdef WOLFSSL_STACK_LOG
wolfSSL 16:8e0d178b1d1e 1109 /* Note: this code only works with GCC using -finstrument-functions. */
wolfSSL 16:8e0d178b1d1e 1110 void __attribute__((no_instrument_function))
wolfSSL 16:8e0d178b1d1e 1111 __cyg_profile_func_enter(void *func, void *caller)
wolfSSL 16:8e0d178b1d1e 1112 {
wolfSSL 16:8e0d178b1d1e 1113 register void* sp asm("sp");
wolfSSL 16:8e0d178b1d1e 1114 fprintf(stderr, "ENTER: %016lx %p\n", (unsigned long)(size_t)func, sp);
wolfSSL 16:8e0d178b1d1e 1115 (void)caller;
wolfSSL 16:8e0d178b1d1e 1116 }
wolfSSL 16:8e0d178b1d1e 1117
wolfSSL 16:8e0d178b1d1e 1118 void __attribute__((no_instrument_function))
wolfSSL 16:8e0d178b1d1e 1119 __cyg_profile_func_exit(void *func, void *caller)
wolfSSL 16:8e0d178b1d1e 1120 {
wolfSSL 16:8e0d178b1d1e 1121 register void* sp asm("sp");
wolfSSL 16:8e0d178b1d1e 1122 fprintf(stderr, "EXIT: %016lx %p\n", (unsigned long)(size_t)func, sp);
wolfSSL 16:8e0d178b1d1e 1123 (void)caller;
wolfSSL 16:8e0d178b1d1e 1124 }
wolfSSL 16:8e0d178b1d1e 1125 #endif
wolfSSL 16:8e0d178b1d1e 1126
wolfSSL 15:117db924cf7c 1127