Xuyi Wang / wolfSSL

Dependents:   OS

Committer:
wolfSSL
Date:
Sat Aug 18 22:20:43 2018 +0000
Revision:
15:117db924cf7c
wolfSSL 3.15.3

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 15:117db924cf7c 1 /* memory.c
wolfSSL 15:117db924cf7c 2 *
wolfSSL 15:117db924cf7c 3 * Copyright (C) 2006-2017 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 15:117db924cf7c 37 #ifdef USE_WOLFSSL_MEMORY
wolfSSL 15:117db924cf7c 38
wolfSSL 15:117db924cf7c 39 #include <wolfssl/wolfcrypt/memory.h>
wolfSSL 15:117db924cf7c 40 #include <wolfssl/wolfcrypt/error-crypt.h>
wolfSSL 15:117db924cf7c 41 #include <wolfssl/wolfcrypt/logging.h>
wolfSSL 15:117db924cf7c 42
wolfSSL 15:117db924cf7c 43 #if defined(WOLFSSL_MALLOC_CHECK) || defined(WOLFSSL_TRACK_MEMORY_FULL)
wolfSSL 15:117db924cf7c 44 #include <stdio.h>
wolfSSL 15:117db924cf7c 45 #endif
wolfSSL 15:117db924cf7c 46
wolfSSL 15:117db924cf7c 47
wolfSSL 15:117db924cf7c 48 /* Set these to default values initially. */
wolfSSL 15:117db924cf7c 49 static wolfSSL_Malloc_cb malloc_function = NULL;
wolfSSL 15:117db924cf7c 50 static wolfSSL_Free_cb free_function = NULL;
wolfSSL 15:117db924cf7c 51 static wolfSSL_Realloc_cb realloc_function = NULL;
wolfSSL 15:117db924cf7c 52
wolfSSL 15:117db924cf7c 53 int wolfSSL_SetAllocators(wolfSSL_Malloc_cb mf,
wolfSSL 15:117db924cf7c 54 wolfSSL_Free_cb ff,
wolfSSL 15:117db924cf7c 55 wolfSSL_Realloc_cb rf)
wolfSSL 15:117db924cf7c 56 {
wolfSSL 15:117db924cf7c 57 int res = 0;
wolfSSL 15:117db924cf7c 58
wolfSSL 15:117db924cf7c 59 if (mf)
wolfSSL 15:117db924cf7c 60 malloc_function = mf;
wolfSSL 15:117db924cf7c 61 else
wolfSSL 15:117db924cf7c 62 res = BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 63
wolfSSL 15:117db924cf7c 64 if (ff)
wolfSSL 15:117db924cf7c 65 free_function = ff;
wolfSSL 15:117db924cf7c 66 else
wolfSSL 15:117db924cf7c 67 res = BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 68
wolfSSL 15:117db924cf7c 69 if (rf)
wolfSSL 15:117db924cf7c 70 realloc_function = rf;
wolfSSL 15:117db924cf7c 71 else
wolfSSL 15:117db924cf7c 72 res = BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 73
wolfSSL 15:117db924cf7c 74 return res;
wolfSSL 15:117db924cf7c 75 }
wolfSSL 15:117db924cf7c 76
wolfSSL 15:117db924cf7c 77 int wolfSSL_ResetAllocators(void)
wolfSSL 15:117db924cf7c 78 {
wolfSSL 15:117db924cf7c 79 /* allow nulls to be set for callbacks to restore defaults */
wolfSSL 15:117db924cf7c 80 malloc_function = NULL;
wolfSSL 15:117db924cf7c 81 free_function = NULL;
wolfSSL 15:117db924cf7c 82 realloc_function = NULL;
wolfSSL 15:117db924cf7c 83
wolfSSL 15:117db924cf7c 84 return 0;
wolfSSL 15:117db924cf7c 85 }
wolfSSL 15:117db924cf7c 86
wolfSSL 15:117db924cf7c 87 int wolfSSL_GetAllocators(wolfSSL_Malloc_cb* mf,
wolfSSL 15:117db924cf7c 88 wolfSSL_Free_cb* ff,
wolfSSL 15:117db924cf7c 89 wolfSSL_Realloc_cb* rf)
wolfSSL 15:117db924cf7c 90 {
wolfSSL 15:117db924cf7c 91 if (mf) *mf = malloc_function;
wolfSSL 15:117db924cf7c 92 if (ff) *ff = free_function;
wolfSSL 15:117db924cf7c 93 if (rf) *rf = realloc_function;
wolfSSL 15:117db924cf7c 94 return 0;
wolfSSL 15:117db924cf7c 95 }
wolfSSL 15:117db924cf7c 96
wolfSSL 15:117db924cf7c 97 #ifndef WOLFSSL_STATIC_MEMORY
wolfSSL 15:117db924cf7c 98 #ifdef WOLFSSL_DEBUG_MEMORY
wolfSSL 15:117db924cf7c 99 void* wolfSSL_Malloc(size_t size, const char* func, unsigned int line)
wolfSSL 15:117db924cf7c 100 #else
wolfSSL 15:117db924cf7c 101 void* wolfSSL_Malloc(size_t size)
wolfSSL 15:117db924cf7c 102 #endif
wolfSSL 15:117db924cf7c 103 {
wolfSSL 15:117db924cf7c 104 void* res = 0;
wolfSSL 15:117db924cf7c 105
wolfSSL 15:117db924cf7c 106 if (malloc_function) {
wolfSSL 15:117db924cf7c 107 #ifdef WOLFSSL_DEBUG_MEMORY
wolfSSL 15:117db924cf7c 108 res = malloc_function(size, func, line);
wolfSSL 15:117db924cf7c 109 #else
wolfSSL 15:117db924cf7c 110 res = malloc_function(size);
wolfSSL 15:117db924cf7c 111 #endif
wolfSSL 15:117db924cf7c 112 }
wolfSSL 15:117db924cf7c 113 else {
wolfSSL 15:117db924cf7c 114 #ifndef WOLFSSL_NO_MALLOC
wolfSSL 15:117db924cf7c 115 res = malloc(size);
wolfSSL 15:117db924cf7c 116 #else
wolfSSL 15:117db924cf7c 117 WOLFSSL_MSG("No malloc available");
wolfSSL 15:117db924cf7c 118 #endif
wolfSSL 15:117db924cf7c 119 }
wolfSSL 15:117db924cf7c 120
wolfSSL 15:117db924cf7c 121 #ifdef WOLFSSL_MALLOC_CHECK
wolfSSL 15:117db924cf7c 122 if (res == NULL)
wolfSSL 15:117db924cf7c 123 puts("wolfSSL_malloc failed");
wolfSSL 15:117db924cf7c 124 #endif
wolfSSL 15:117db924cf7c 125
wolfSSL 15:117db924cf7c 126 return res;
wolfSSL 15:117db924cf7c 127 }
wolfSSL 15:117db924cf7c 128
wolfSSL 15:117db924cf7c 129 #ifdef WOLFSSL_DEBUG_MEMORY
wolfSSL 15:117db924cf7c 130 void wolfSSL_Free(void *ptr, const char* func, unsigned int line)
wolfSSL 15:117db924cf7c 131 #else
wolfSSL 15:117db924cf7c 132 void wolfSSL_Free(void *ptr)
wolfSSL 15:117db924cf7c 133 #endif
wolfSSL 15:117db924cf7c 134 {
wolfSSL 15:117db924cf7c 135 if (free_function) {
wolfSSL 15:117db924cf7c 136 #ifdef WOLFSSL_DEBUG_MEMORY
wolfSSL 15:117db924cf7c 137 free_function(ptr, func, line);
wolfSSL 15:117db924cf7c 138 #else
wolfSSL 15:117db924cf7c 139 free_function(ptr);
wolfSSL 15:117db924cf7c 140 #endif
wolfSSL 15:117db924cf7c 141 }
wolfSSL 15:117db924cf7c 142 else {
wolfSSL 15:117db924cf7c 143 #ifndef WOLFSSL_NO_MALLOC
wolfSSL 15:117db924cf7c 144 free(ptr);
wolfSSL 15:117db924cf7c 145 #else
wolfSSL 15:117db924cf7c 146 WOLFSSL_MSG("No free available");
wolfSSL 15:117db924cf7c 147 #endif
wolfSSL 15:117db924cf7c 148 }
wolfSSL 15:117db924cf7c 149 }
wolfSSL 15:117db924cf7c 150
wolfSSL 15:117db924cf7c 151 #ifdef WOLFSSL_DEBUG_MEMORY
wolfSSL 15:117db924cf7c 152 void* wolfSSL_Realloc(void *ptr, size_t size, const char* func, unsigned int line)
wolfSSL 15:117db924cf7c 153 #else
wolfSSL 15:117db924cf7c 154 void* wolfSSL_Realloc(void *ptr, size_t size)
wolfSSL 15:117db924cf7c 155 #endif
wolfSSL 15:117db924cf7c 156 {
wolfSSL 15:117db924cf7c 157 void* res = 0;
wolfSSL 15:117db924cf7c 158
wolfSSL 15:117db924cf7c 159 if (realloc_function) {
wolfSSL 15:117db924cf7c 160 #ifdef WOLFSSL_DEBUG_MEMORY
wolfSSL 15:117db924cf7c 161 res = realloc_function(ptr, size, func, line);
wolfSSL 15:117db924cf7c 162 #else
wolfSSL 15:117db924cf7c 163 res = realloc_function(ptr, size);
wolfSSL 15:117db924cf7c 164 #endif
wolfSSL 15:117db924cf7c 165 }
wolfSSL 15:117db924cf7c 166 else {
wolfSSL 15:117db924cf7c 167 #ifndef WOLFSSL_NO_MALLOC
wolfSSL 15:117db924cf7c 168 res = realloc(ptr, size);
wolfSSL 15:117db924cf7c 169 #else
wolfSSL 15:117db924cf7c 170 WOLFSSL_MSG("No realloc available");
wolfSSL 15:117db924cf7c 171 #endif
wolfSSL 15:117db924cf7c 172 }
wolfSSL 15:117db924cf7c 173
wolfSSL 15:117db924cf7c 174 return res;
wolfSSL 15:117db924cf7c 175 }
wolfSSL 15:117db924cf7c 176 #endif /* WOLFSSL_STATIC_MEMORY */
wolfSSL 15:117db924cf7c 177
wolfSSL 15:117db924cf7c 178 #ifdef WOLFSSL_STATIC_MEMORY
wolfSSL 15:117db924cf7c 179
wolfSSL 15:117db924cf7c 180 struct wc_Memory {
wolfSSL 15:117db924cf7c 181 byte* buffer;
wolfSSL 15:117db924cf7c 182 struct wc_Memory* next;
wolfSSL 15:117db924cf7c 183 word32 sz;
wolfSSL 15:117db924cf7c 184 };
wolfSSL 15:117db924cf7c 185
wolfSSL 15:117db924cf7c 186
wolfSSL 15:117db924cf7c 187 /* returns amount of memory used on success. On error returns negative value
wolfSSL 15:117db924cf7c 188 wc_Memory** list is the list that new buckets are prepended to
wolfSSL 15:117db924cf7c 189 */
wolfSSL 15:117db924cf7c 190 static int create_memory_buckets(byte* buffer, word32 bufSz,
wolfSSL 15:117db924cf7c 191 word32 buckSz, word32 buckNum, wc_Memory** list) {
wolfSSL 15:117db924cf7c 192 word32 i;
wolfSSL 15:117db924cf7c 193 byte* pt = buffer;
wolfSSL 15:117db924cf7c 194 int ret = 0;
wolfSSL 15:117db924cf7c 195 word32 memSz = (word32)sizeof(wc_Memory);
wolfSSL 15:117db924cf7c 196 word32 padSz = -(int)memSz & (WOLFSSL_STATIC_ALIGN - 1);
wolfSSL 15:117db924cf7c 197
wolfSSL 15:117db924cf7c 198 /* if not enough space available for bucket size then do not try */
wolfSSL 15:117db924cf7c 199 if (buckSz + memSz + padSz > bufSz) {
wolfSSL 15:117db924cf7c 200 return ret;
wolfSSL 15:117db924cf7c 201 }
wolfSSL 15:117db924cf7c 202
wolfSSL 15:117db924cf7c 203 for (i = 0; i < buckNum; i++) {
wolfSSL 15:117db924cf7c 204 if ((buckSz + memSz + padSz) <= (bufSz - ret)) {
wolfSSL 15:117db924cf7c 205 /* create a new struct and set its values */
wolfSSL 15:117db924cf7c 206 wc_Memory* mem = (struct wc_Memory*)(pt);
wolfSSL 15:117db924cf7c 207 mem->sz = buckSz;
wolfSSL 15:117db924cf7c 208 mem->buffer = (byte*)pt + padSz + memSz;
wolfSSL 15:117db924cf7c 209 mem->next = NULL;
wolfSSL 15:117db924cf7c 210
wolfSSL 15:117db924cf7c 211 /* add the newly created struct to front of list */
wolfSSL 15:117db924cf7c 212 if (*list == NULL) {
wolfSSL 15:117db924cf7c 213 *list = mem;
wolfSSL 15:117db924cf7c 214 } else {
wolfSSL 15:117db924cf7c 215 mem->next = *list;
wolfSSL 15:117db924cf7c 216 *list = mem;
wolfSSL 15:117db924cf7c 217 }
wolfSSL 15:117db924cf7c 218
wolfSSL 15:117db924cf7c 219 /* advance pointer and keep track of memory used */
wolfSSL 15:117db924cf7c 220 ret += buckSz + padSz + memSz;
wolfSSL 15:117db924cf7c 221 pt += buckSz + padSz + memSz;
wolfSSL 15:117db924cf7c 222 }
wolfSSL 15:117db924cf7c 223 else {
wolfSSL 15:117db924cf7c 224 break; /* not enough space left for more buckets of this size */
wolfSSL 15:117db924cf7c 225 }
wolfSSL 15:117db924cf7c 226 }
wolfSSL 15:117db924cf7c 227
wolfSSL 15:117db924cf7c 228 return ret;
wolfSSL 15:117db924cf7c 229 }
wolfSSL 15:117db924cf7c 230
wolfSSL 15:117db924cf7c 231 int wolfSSL_init_memory_heap(WOLFSSL_HEAP* heap)
wolfSSL 15:117db924cf7c 232 {
wolfSSL 15:117db924cf7c 233 word32 wc_MemSz[WOLFMEM_DEF_BUCKETS] = { WOLFMEM_BUCKETS };
wolfSSL 15:117db924cf7c 234 word32 wc_Dist[WOLFMEM_DEF_BUCKETS] = { WOLFMEM_DIST };
wolfSSL 15:117db924cf7c 235
wolfSSL 15:117db924cf7c 236 if (heap == NULL) {
wolfSSL 15:117db924cf7c 237 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 238 }
wolfSSL 15:117db924cf7c 239
wolfSSL 15:117db924cf7c 240 XMEMSET(heap, 0, sizeof(WOLFSSL_HEAP));
wolfSSL 15:117db924cf7c 241
wolfSSL 15:117db924cf7c 242 XMEMCPY(heap->sizeList, wc_MemSz, sizeof(wc_MemSz));
wolfSSL 15:117db924cf7c 243 XMEMCPY(heap->distList, wc_Dist, sizeof(wc_Dist));
wolfSSL 15:117db924cf7c 244
wolfSSL 15:117db924cf7c 245 if (wc_InitMutex(&(heap->memory_mutex)) != 0) {
wolfSSL 15:117db924cf7c 246 WOLFSSL_MSG("Error creating heap memory mutex");
wolfSSL 15:117db924cf7c 247 return BAD_MUTEX_E;
wolfSSL 15:117db924cf7c 248 }
wolfSSL 15:117db924cf7c 249
wolfSSL 15:117db924cf7c 250 return 0;
wolfSSL 15:117db924cf7c 251 }
wolfSSL 15:117db924cf7c 252
wolfSSL 15:117db924cf7c 253 int wc_LoadStaticMemory(WOLFSSL_HEAP_HINT** pHint,
wolfSSL 15:117db924cf7c 254 unsigned char* buf, unsigned int sz, int flag, int max)
wolfSSL 15:117db924cf7c 255 {
wolfSSL 15:117db924cf7c 256 int ret;
wolfSSL 15:117db924cf7c 257 WOLFSSL_HEAP* heap;
wolfSSL 15:117db924cf7c 258 WOLFSSL_HEAP_HINT* hint;
wolfSSL 15:117db924cf7c 259 word32 idx = 0;
wolfSSL 15:117db924cf7c 260
wolfSSL 15:117db924cf7c 261 if (pHint == NULL || buf == NULL) {
wolfSSL 15:117db924cf7c 262 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 263 }
wolfSSL 15:117db924cf7c 264
wolfSSL 15:117db924cf7c 265 if ((sizeof(WOLFSSL_HEAP) + sizeof(WOLFSSL_HEAP_HINT)) > sz - idx) {
wolfSSL 15:117db924cf7c 266 return BUFFER_E; /* not enough memory for structures */
wolfSSL 15:117db924cf7c 267 }
wolfSSL 15:117db924cf7c 268
wolfSSL 15:117db924cf7c 269 /* check if hint has already been assigned */
wolfSSL 15:117db924cf7c 270 if (*pHint == NULL) {
wolfSSL 15:117db924cf7c 271 heap = (WOLFSSL_HEAP*)buf;
wolfSSL 15:117db924cf7c 272 idx += sizeof(WOLFSSL_HEAP);
wolfSSL 15:117db924cf7c 273 hint = (WOLFSSL_HEAP_HINT*)(buf + idx);
wolfSSL 15:117db924cf7c 274 idx += sizeof(WOLFSSL_HEAP_HINT);
wolfSSL 15:117db924cf7c 275
wolfSSL 15:117db924cf7c 276 ret = wolfSSL_init_memory_heap(heap);
wolfSSL 15:117db924cf7c 277 if (ret != 0) {
wolfSSL 15:117db924cf7c 278 return ret;
wolfSSL 15:117db924cf7c 279 }
wolfSSL 15:117db924cf7c 280
wolfSSL 15:117db924cf7c 281 XMEMSET(hint, 0, sizeof(WOLFSSL_HEAP_HINT));
wolfSSL 15:117db924cf7c 282 hint->memory = heap;
wolfSSL 15:117db924cf7c 283 }
wolfSSL 15:117db924cf7c 284 else {
wolfSSL 15:117db924cf7c 285 #ifdef WOLFSSL_HEAP_TEST
wolfSSL 15:117db924cf7c 286 /* do not load in memory if test has been set */
wolfSSL 15:117db924cf7c 287 if (heap == (void*)WOLFSSL_HEAP_TEST) {
wolfSSL 15:117db924cf7c 288 return 0;
wolfSSL 15:117db924cf7c 289 }
wolfSSL 15:117db924cf7c 290 #endif
wolfSSL 15:117db924cf7c 291
wolfSSL 15:117db924cf7c 292 hint = (WOLFSSL_HEAP_HINT*)(*pHint);
wolfSSL 15:117db924cf7c 293 heap = hint->memory;
wolfSSL 15:117db924cf7c 294 }
wolfSSL 15:117db924cf7c 295
wolfSSL 15:117db924cf7c 296 ret = wolfSSL_load_static_memory(buf + idx, sz - idx, flag, heap);
wolfSSL 15:117db924cf7c 297 if (ret != 1) {
wolfSSL 15:117db924cf7c 298 WOLFSSL_MSG("Error partitioning memory");
wolfSSL 15:117db924cf7c 299 return -1;
wolfSSL 15:117db924cf7c 300 }
wolfSSL 15:117db924cf7c 301
wolfSSL 15:117db924cf7c 302 /* determine what max applies too */
wolfSSL 15:117db924cf7c 303 if ((flag & WOLFMEM_IO_POOL) || (flag & WOLFMEM_IO_POOL_FIXED)) {
wolfSSL 15:117db924cf7c 304 heap->maxIO = max;
wolfSSL 15:117db924cf7c 305 }
wolfSSL 15:117db924cf7c 306 else { /* general memory used in handshakes */
wolfSSL 15:117db924cf7c 307 heap->maxHa = max;
wolfSSL 15:117db924cf7c 308 }
wolfSSL 15:117db924cf7c 309
wolfSSL 15:117db924cf7c 310 heap->flag |= flag;
wolfSSL 15:117db924cf7c 311 *pHint = hint;
wolfSSL 15:117db924cf7c 312
wolfSSL 15:117db924cf7c 313 (void)max;
wolfSSL 15:117db924cf7c 314
wolfSSL 15:117db924cf7c 315 return 0;
wolfSSL 15:117db924cf7c 316 }
wolfSSL 15:117db924cf7c 317
wolfSSL 15:117db924cf7c 318 int wolfSSL_load_static_memory(byte* buffer, word32 sz, int flag,
wolfSSL 15:117db924cf7c 319 WOLFSSL_HEAP* heap)
wolfSSL 15:117db924cf7c 320 {
wolfSSL 15:117db924cf7c 321 word32 ava = sz;
wolfSSL 15:117db924cf7c 322 byte* pt = buffer;
wolfSSL 15:117db924cf7c 323 int ret = 0;
wolfSSL 15:117db924cf7c 324 word32 memSz = (word32)sizeof(wc_Memory);
wolfSSL 15:117db924cf7c 325 word32 padSz = -(int)memSz & (WOLFSSL_STATIC_ALIGN - 1);
wolfSSL 15:117db924cf7c 326
wolfSSL 15:117db924cf7c 327 WOLFSSL_ENTER("wolfSSL_load_static_memory");
wolfSSL 15:117db924cf7c 328
wolfSSL 15:117db924cf7c 329 if (buffer == NULL) {
wolfSSL 15:117db924cf7c 330 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 331 }
wolfSSL 15:117db924cf7c 332
wolfSSL 15:117db924cf7c 333 /* align pt */
wolfSSL 15:117db924cf7c 334 while ((wolfssl_word)pt % WOLFSSL_STATIC_ALIGN && pt < (buffer + sz)) {
wolfSSL 15:117db924cf7c 335 *pt = 0x00;
wolfSSL 15:117db924cf7c 336 pt++;
wolfSSL 15:117db924cf7c 337 ava--;
wolfSSL 15:117db924cf7c 338 }
wolfSSL 15:117db924cf7c 339
wolfSSL 15:117db924cf7c 340 #ifdef WOLFSSL_DEBUG_MEMORY
wolfSSL 15:117db924cf7c 341 printf("Allocated %d bytes for static memory @ %p\n", ava, pt);
wolfSSL 15:117db924cf7c 342 #endif
wolfSSL 15:117db924cf7c 343
wolfSSL 15:117db924cf7c 344 /* devide into chunks of memory and add them to available list */
wolfSSL 15:117db924cf7c 345 while (ava >= (heap->sizeList[0] + padSz + memSz)) {
wolfSSL 15:117db924cf7c 346 int i;
wolfSSL 15:117db924cf7c 347 /* creating only IO buffers from memory passed in, max TLS is 16k */
wolfSSL 15:117db924cf7c 348 if (flag & WOLFMEM_IO_POOL || flag & WOLFMEM_IO_POOL_FIXED) {
wolfSSL 15:117db924cf7c 349 if ((ret = create_memory_buckets(pt, ava,
wolfSSL 15:117db924cf7c 350 WOLFMEM_IO_SZ, 1, &(heap->io))) < 0) {
wolfSSL 15:117db924cf7c 351 WOLFSSL_LEAVE("wolfSSL_load_static_memory", ret);
wolfSSL 15:117db924cf7c 352 return ret;
wolfSSL 15:117db924cf7c 353 }
wolfSSL 15:117db924cf7c 354
wolfSSL 15:117db924cf7c 355 /* check if no more room left for creating IO buffers */
wolfSSL 15:117db924cf7c 356 if (ret == 0) {
wolfSSL 15:117db924cf7c 357 break;
wolfSSL 15:117db924cf7c 358 }
wolfSSL 15:117db924cf7c 359
wolfSSL 15:117db924cf7c 360 /* advance pointer in buffer for next buckets and keep track
wolfSSL 15:117db924cf7c 361 of how much memory is left available */
wolfSSL 15:117db924cf7c 362 pt += ret;
wolfSSL 15:117db924cf7c 363 ava -= ret;
wolfSSL 15:117db924cf7c 364 }
wolfSSL 15:117db924cf7c 365 else {
wolfSSL 15:117db924cf7c 366 /* start at largest and move to smaller buckets */
wolfSSL 15:117db924cf7c 367 for (i = (WOLFMEM_MAX_BUCKETS - 1); i >= 0; i--) {
wolfSSL 15:117db924cf7c 368 if ((heap->sizeList[i] + padSz + memSz) <= ava) {
wolfSSL 15:117db924cf7c 369 if ((ret = create_memory_buckets(pt, ava, heap->sizeList[i],
wolfSSL 15:117db924cf7c 370 heap->distList[i], &(heap->ava[i]))) < 0) {
wolfSSL 15:117db924cf7c 371 WOLFSSL_LEAVE("wolfSSL_load_static_memory", ret);
wolfSSL 15:117db924cf7c 372 return ret;
wolfSSL 15:117db924cf7c 373 }
wolfSSL 15:117db924cf7c 374
wolfSSL 15:117db924cf7c 375 /* advance pointer in buffer for next buckets and keep track
wolfSSL 15:117db924cf7c 376 of how much memory is left available */
wolfSSL 15:117db924cf7c 377 pt += ret;
wolfSSL 15:117db924cf7c 378 ava -= ret;
wolfSSL 15:117db924cf7c 379 }
wolfSSL 15:117db924cf7c 380 }
wolfSSL 15:117db924cf7c 381 }
wolfSSL 15:117db924cf7c 382 }
wolfSSL 15:117db924cf7c 383
wolfSSL 15:117db924cf7c 384 return 1;
wolfSSL 15:117db924cf7c 385 }
wolfSSL 15:117db924cf7c 386
wolfSSL 15:117db924cf7c 387
wolfSSL 15:117db924cf7c 388 /* returns the size of management memory needed for each bucket.
wolfSSL 15:117db924cf7c 389 * This is memory that is used to keep track of and align memory buckets. */
wolfSSL 15:117db924cf7c 390 int wolfSSL_MemoryPaddingSz(void)
wolfSSL 15:117db924cf7c 391 {
wolfSSL 15:117db924cf7c 392 word32 memSz = (word32)sizeof(wc_Memory);
wolfSSL 15:117db924cf7c 393 word32 padSz = -(int)memSz & (WOLFSSL_STATIC_ALIGN - 1);
wolfSSL 15:117db924cf7c 394 return memSz + padSz;
wolfSSL 15:117db924cf7c 395 }
wolfSSL 15:117db924cf7c 396
wolfSSL 15:117db924cf7c 397
wolfSSL 15:117db924cf7c 398 /* Used to calculate memory size for optimum use with buckets.
wolfSSL 15:117db924cf7c 399 returns the suggested size rounded down to the nearest bucket. */
wolfSSL 15:117db924cf7c 400 int wolfSSL_StaticBufferSz(byte* buffer, word32 sz, int flag)
wolfSSL 15:117db924cf7c 401 {
wolfSSL 15:117db924cf7c 402 word32 bucketSz[WOLFMEM_MAX_BUCKETS] = {WOLFMEM_BUCKETS};
wolfSSL 15:117db924cf7c 403 word32 distList[WOLFMEM_MAX_BUCKETS] = {WOLFMEM_DIST};
wolfSSL 15:117db924cf7c 404
wolfSSL 15:117db924cf7c 405 word32 ava = sz;
wolfSSL 15:117db924cf7c 406 byte* pt = buffer;
wolfSSL 15:117db924cf7c 407 word32 memSz = (word32)sizeof(wc_Memory);
wolfSSL 15:117db924cf7c 408 word32 padSz = -(int)memSz & (WOLFSSL_STATIC_ALIGN - 1);
wolfSSL 15:117db924cf7c 409
wolfSSL 15:117db924cf7c 410 WOLFSSL_ENTER("wolfSSL_static_size");
wolfSSL 15:117db924cf7c 411
wolfSSL 15:117db924cf7c 412 if (buffer == NULL) {
wolfSSL 15:117db924cf7c 413 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 414 }
wolfSSL 15:117db924cf7c 415
wolfSSL 15:117db924cf7c 416 /* align pt */
wolfSSL 15:117db924cf7c 417 while ((wolfssl_word)pt % WOLFSSL_STATIC_ALIGN && pt < (buffer + sz)) {
wolfSSL 15:117db924cf7c 418 pt++;
wolfSSL 15:117db924cf7c 419 ava--;
wolfSSL 15:117db924cf7c 420 }
wolfSSL 15:117db924cf7c 421
wolfSSL 15:117db924cf7c 422 /* creating only IO buffers from memory passed in, max TLS is 16k */
wolfSSL 15:117db924cf7c 423 if (flag & WOLFMEM_IO_POOL || flag & WOLFMEM_IO_POOL_FIXED) {
wolfSSL 15:117db924cf7c 424 if (ava < (memSz + padSz + WOLFMEM_IO_SZ)) {
wolfSSL 15:117db924cf7c 425 return 0; /* not enough room for even one bucket */
wolfSSL 15:117db924cf7c 426 }
wolfSSL 15:117db924cf7c 427
wolfSSL 15:117db924cf7c 428 ava = ava % (memSz + padSz + WOLFMEM_IO_SZ);
wolfSSL 15:117db924cf7c 429 }
wolfSSL 15:117db924cf7c 430 else {
wolfSSL 15:117db924cf7c 431 int i, k;
wolfSSL 15:117db924cf7c 432
wolfSSL 15:117db924cf7c 433 if (ava < (bucketSz[0] + padSz + memSz)) {
wolfSSL 15:117db924cf7c 434 return 0; /* not enough room for even one bucket */
wolfSSL 15:117db924cf7c 435 }
wolfSSL 15:117db924cf7c 436
wolfSSL 15:117db924cf7c 437 while ((ava >= (bucketSz[0] + padSz + memSz)) && (ava > 0)) {
wolfSSL 15:117db924cf7c 438 /* start at largest and move to smaller buckets */
wolfSSL 15:117db924cf7c 439 for (i = (WOLFMEM_MAX_BUCKETS - 1); i >= 0; i--) {
wolfSSL 15:117db924cf7c 440 for (k = distList[i]; k > 0; k--) {
wolfSSL 15:117db924cf7c 441 if ((bucketSz[i] + padSz + memSz) <= ava) {
wolfSSL 15:117db924cf7c 442 ava -= bucketSz[i] + padSz + memSz;
wolfSSL 15:117db924cf7c 443 }
wolfSSL 15:117db924cf7c 444 }
wolfSSL 15:117db924cf7c 445 }
wolfSSL 15:117db924cf7c 446 }
wolfSSL 15:117db924cf7c 447 }
wolfSSL 15:117db924cf7c 448
wolfSSL 15:117db924cf7c 449 return sz - ava; /* round down */
wolfSSL 15:117db924cf7c 450 }
wolfSSL 15:117db924cf7c 451
wolfSSL 15:117db924cf7c 452
wolfSSL 15:117db924cf7c 453 int FreeFixedIO(WOLFSSL_HEAP* heap, wc_Memory** io)
wolfSSL 15:117db924cf7c 454 {
wolfSSL 15:117db924cf7c 455 WOLFSSL_MSG("Freeing fixed IO buffer");
wolfSSL 15:117db924cf7c 456
wolfSSL 15:117db924cf7c 457 /* check if fixed buffer was set */
wolfSSL 15:117db924cf7c 458 if (*io == NULL) {
wolfSSL 15:117db924cf7c 459 return 1;
wolfSSL 15:117db924cf7c 460 }
wolfSSL 15:117db924cf7c 461
wolfSSL 15:117db924cf7c 462 if (heap == NULL) {
wolfSSL 15:117db924cf7c 463 WOLFSSL_MSG("No heap to return fixed IO too");
wolfSSL 15:117db924cf7c 464 }
wolfSSL 15:117db924cf7c 465 else {
wolfSSL 15:117db924cf7c 466 /* put IO buffer back into IO pool */
wolfSSL 15:117db924cf7c 467 (*io)->next = heap->io;
wolfSSL 15:117db924cf7c 468 heap->io = *io;
wolfSSL 15:117db924cf7c 469 *io = NULL;
wolfSSL 15:117db924cf7c 470 }
wolfSSL 15:117db924cf7c 471
wolfSSL 15:117db924cf7c 472 return 1;
wolfSSL 15:117db924cf7c 473 }
wolfSSL 15:117db924cf7c 474
wolfSSL 15:117db924cf7c 475
wolfSSL 15:117db924cf7c 476 int SetFixedIO(WOLFSSL_HEAP* heap, wc_Memory** io)
wolfSSL 15:117db924cf7c 477 {
wolfSSL 15:117db924cf7c 478 WOLFSSL_MSG("Setting fixed IO for SSL");
wolfSSL 15:117db924cf7c 479 if (heap == NULL) {
wolfSSL 15:117db924cf7c 480 return MEMORY_E;
wolfSSL 15:117db924cf7c 481 }
wolfSSL 15:117db924cf7c 482
wolfSSL 15:117db924cf7c 483 *io = heap->io;
wolfSSL 15:117db924cf7c 484
wolfSSL 15:117db924cf7c 485 if (*io != NULL) {
wolfSSL 15:117db924cf7c 486 heap->io = (*io)->next;
wolfSSL 15:117db924cf7c 487 (*io)->next = NULL;
wolfSSL 15:117db924cf7c 488 }
wolfSSL 15:117db924cf7c 489 else { /* failed to grab an IO buffer */
wolfSSL 15:117db924cf7c 490 return 0;
wolfSSL 15:117db924cf7c 491 }
wolfSSL 15:117db924cf7c 492
wolfSSL 15:117db924cf7c 493 return 1;
wolfSSL 15:117db924cf7c 494 }
wolfSSL 15:117db924cf7c 495
wolfSSL 15:117db924cf7c 496
wolfSSL 15:117db924cf7c 497 int wolfSSL_GetMemStats(WOLFSSL_HEAP* heap, WOLFSSL_MEM_STATS* stats)
wolfSSL 15:117db924cf7c 498 {
wolfSSL 15:117db924cf7c 499 word32 i;
wolfSSL 15:117db924cf7c 500 wc_Memory* pt;
wolfSSL 15:117db924cf7c 501
wolfSSL 15:117db924cf7c 502 XMEMSET(stats, 0, sizeof(WOLFSSL_MEM_STATS));
wolfSSL 15:117db924cf7c 503
wolfSSL 15:117db924cf7c 504 stats->totalAlloc = heap->alloc;
wolfSSL 15:117db924cf7c 505 stats->totalFr = heap->frAlc;
wolfSSL 15:117db924cf7c 506 stats->curAlloc = stats->totalAlloc - stats->totalFr;
wolfSSL 15:117db924cf7c 507 stats->maxHa = heap->maxHa;
wolfSSL 15:117db924cf7c 508 stats->maxIO = heap->maxIO;
wolfSSL 15:117db924cf7c 509 for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
wolfSSL 15:117db924cf7c 510 stats->blockSz[i] = heap->sizeList[i];
wolfSSL 15:117db924cf7c 511 for (pt = heap->ava[i]; pt != NULL; pt = pt->next) {
wolfSSL 15:117db924cf7c 512 stats->avaBlock[i] += 1;
wolfSSL 15:117db924cf7c 513 }
wolfSSL 15:117db924cf7c 514 }
wolfSSL 15:117db924cf7c 515
wolfSSL 15:117db924cf7c 516 for (pt = heap->io; pt != NULL; pt = pt->next) {
wolfSSL 15:117db924cf7c 517 stats->avaIO++;
wolfSSL 15:117db924cf7c 518 }
wolfSSL 15:117db924cf7c 519
wolfSSL 15:117db924cf7c 520 stats->flag = heap->flag; /* flag used */
wolfSSL 15:117db924cf7c 521
wolfSSL 15:117db924cf7c 522 return 1;
wolfSSL 15:117db924cf7c 523 }
wolfSSL 15:117db924cf7c 524
wolfSSL 15:117db924cf7c 525
wolfSSL 15:117db924cf7c 526 #ifdef WOLFSSL_DEBUG_MEMORY
wolfSSL 15:117db924cf7c 527 void* wolfSSL_Malloc(size_t size, void* heap, int type, const char* func, unsigned int line)
wolfSSL 15:117db924cf7c 528 #else
wolfSSL 15:117db924cf7c 529 void* wolfSSL_Malloc(size_t size, void* heap, int type)
wolfSSL 15:117db924cf7c 530 #endif
wolfSSL 15:117db924cf7c 531 {
wolfSSL 15:117db924cf7c 532 void* res = 0;
wolfSSL 15:117db924cf7c 533 wc_Memory* pt = NULL;
wolfSSL 15:117db924cf7c 534 int i;
wolfSSL 15:117db924cf7c 535
wolfSSL 15:117db924cf7c 536 /* check for testing heap hint was set */
wolfSSL 15:117db924cf7c 537 #ifdef WOLFSSL_HEAP_TEST
wolfSSL 15:117db924cf7c 538 if (heap == (void*)WOLFSSL_HEAP_TEST) {
wolfSSL 15:117db924cf7c 539 return malloc(size);
wolfSSL 15:117db924cf7c 540 }
wolfSSL 15:117db924cf7c 541 #endif
wolfSSL 15:117db924cf7c 542
wolfSSL 15:117db924cf7c 543 /* if no heap hint then use dynamic memory*/
wolfSSL 15:117db924cf7c 544 if (heap == NULL) {
wolfSSL 15:117db924cf7c 545 #ifdef WOLFSSL_HEAP_TEST
wolfSSL 15:117db924cf7c 546 /* allow using malloc for creating ctx and method */
wolfSSL 15:117db924cf7c 547 if (type == DYNAMIC_TYPE_CTX || type == DYNAMIC_TYPE_METHOD ||
wolfSSL 15:117db924cf7c 548 type == DYNAMIC_TYPE_CERT_MANAGER) {
wolfSSL 15:117db924cf7c 549 WOLFSSL_MSG("ERROR allowing null heap hint for ctx/method\n");
wolfSSL 15:117db924cf7c 550 res = malloc(size);
wolfSSL 15:117db924cf7c 551 }
wolfSSL 15:117db924cf7c 552 else {
wolfSSL 15:117db924cf7c 553 WOLFSSL_MSG("ERROR null heap hint passed into XMALLOC\n");
wolfSSL 15:117db924cf7c 554 res = NULL;
wolfSSL 15:117db924cf7c 555 }
wolfSSL 15:117db924cf7c 556 #else
wolfSSL 15:117db924cf7c 557 #ifndef WOLFSSL_NO_MALLOC
wolfSSL 15:117db924cf7c 558 #ifdef FREERTOS
wolfSSL 15:117db924cf7c 559 res = pvPortMalloc(size);
wolfSSL 15:117db924cf7c 560 #else
wolfSSL 15:117db924cf7c 561 res = malloc(size);
wolfSSL 15:117db924cf7c 562 #endif
wolfSSL 15:117db924cf7c 563 #else
wolfSSL 15:117db924cf7c 564 WOLFSSL_MSG("No heap hint found to use and no malloc");
wolfSSL 15:117db924cf7c 565 #ifdef WOLFSSL_DEBUG_MEMORY
wolfSSL 15:117db924cf7c 566 printf("ERROR: at %s:%d\n", func, line);
wolfSSL 15:117db924cf7c 567 #endif
wolfSSL 15:117db924cf7c 568 #endif /* WOLFSSL_NO_MALLOC */
wolfSSL 15:117db924cf7c 569 #endif /* WOLFSSL_HEAP_TEST */
wolfSSL 15:117db924cf7c 570 }
wolfSSL 15:117db924cf7c 571 else {
wolfSSL 15:117db924cf7c 572 WOLFSSL_HEAP_HINT* hint = (WOLFSSL_HEAP_HINT*)heap;
wolfSSL 15:117db924cf7c 573 WOLFSSL_HEAP* mem = hint->memory;
wolfSSL 15:117db924cf7c 574
wolfSSL 15:117db924cf7c 575 if (wc_LockMutex(&(mem->memory_mutex)) != 0) {
wolfSSL 15:117db924cf7c 576 WOLFSSL_MSG("Bad memory_mutex lock");
wolfSSL 15:117db924cf7c 577 return NULL;
wolfSSL 15:117db924cf7c 578 }
wolfSSL 15:117db924cf7c 579
wolfSSL 15:117db924cf7c 580 /* case of using fixed IO buffers */
wolfSSL 15:117db924cf7c 581 if (mem->flag & WOLFMEM_IO_POOL_FIXED &&
wolfSSL 15:117db924cf7c 582 (type == DYNAMIC_TYPE_OUT_BUFFER ||
wolfSSL 15:117db924cf7c 583 type == DYNAMIC_TYPE_IN_BUFFER)) {
wolfSSL 15:117db924cf7c 584 if (type == DYNAMIC_TYPE_OUT_BUFFER) {
wolfSSL 15:117db924cf7c 585 pt = hint->outBuf;
wolfSSL 15:117db924cf7c 586 }
wolfSSL 15:117db924cf7c 587 if (type == DYNAMIC_TYPE_IN_BUFFER) {
wolfSSL 15:117db924cf7c 588 pt = hint->inBuf;
wolfSSL 15:117db924cf7c 589 }
wolfSSL 15:117db924cf7c 590 }
wolfSSL 15:117db924cf7c 591 else {
wolfSSL 15:117db924cf7c 592 /* check if using IO pool flag */
wolfSSL 15:117db924cf7c 593 if (mem->flag & WOLFMEM_IO_POOL &&
wolfSSL 15:117db924cf7c 594 (type == DYNAMIC_TYPE_OUT_BUFFER ||
wolfSSL 15:117db924cf7c 595 type == DYNAMIC_TYPE_IN_BUFFER)) {
wolfSSL 15:117db924cf7c 596 if (mem->io != NULL) {
wolfSSL 15:117db924cf7c 597 pt = mem->io;
wolfSSL 15:117db924cf7c 598 mem->io = pt->next;
wolfSSL 15:117db924cf7c 599 }
wolfSSL 15:117db924cf7c 600 }
wolfSSL 15:117db924cf7c 601
wolfSSL 15:117db924cf7c 602 /* general static memory */
wolfSSL 15:117db924cf7c 603 if (pt == NULL) {
wolfSSL 15:117db924cf7c 604 for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
wolfSSL 15:117db924cf7c 605 if ((word32)size < mem->sizeList[i]) {
wolfSSL 15:117db924cf7c 606 if (mem->ava[i] != NULL) {
wolfSSL 15:117db924cf7c 607 pt = mem->ava[i];
wolfSSL 15:117db924cf7c 608 mem->ava[i] = pt->next;
wolfSSL 15:117db924cf7c 609 break;
wolfSSL 15:117db924cf7c 610 }
wolfSSL 15:117db924cf7c 611 }
wolfSSL 15:117db924cf7c 612 }
wolfSSL 15:117db924cf7c 613 }
wolfSSL 15:117db924cf7c 614 }
wolfSSL 15:117db924cf7c 615
wolfSSL 15:117db924cf7c 616 if (pt != NULL) {
wolfSSL 15:117db924cf7c 617 mem->inUse += pt->sz;
wolfSSL 15:117db924cf7c 618 mem->alloc += 1;
wolfSSL 15:117db924cf7c 619 res = pt->buffer;
wolfSSL 15:117db924cf7c 620
wolfSSL 15:117db924cf7c 621 #ifdef WOLFSSL_DEBUG_MEMORY
wolfSSL 15:117db924cf7c 622 printf("Alloc: %p -> %u at %s:%d\n", pt->buffer, pt->sz, func, line);
wolfSSL 15:117db924cf7c 623 #endif
wolfSSL 15:117db924cf7c 624
wolfSSL 15:117db924cf7c 625 /* keep track of connection statistics if flag is set */
wolfSSL 15:117db924cf7c 626 if (mem->flag & WOLFMEM_TRACK_STATS) {
wolfSSL 15:117db924cf7c 627 WOLFSSL_MEM_CONN_STATS* stats = hint->stats;
wolfSSL 15:117db924cf7c 628 if (stats != NULL) {
wolfSSL 15:117db924cf7c 629 stats->curMem += pt->sz;
wolfSSL 15:117db924cf7c 630 if (stats->peakMem < stats->curMem) {
wolfSSL 15:117db924cf7c 631 stats->peakMem = stats->curMem;
wolfSSL 15:117db924cf7c 632 }
wolfSSL 15:117db924cf7c 633 stats->curAlloc++;
wolfSSL 15:117db924cf7c 634 if (stats->peakAlloc < stats->curAlloc) {
wolfSSL 15:117db924cf7c 635 stats->peakAlloc = stats->curAlloc;
wolfSSL 15:117db924cf7c 636 }
wolfSSL 15:117db924cf7c 637 stats->totalAlloc++;
wolfSSL 15:117db924cf7c 638 }
wolfSSL 15:117db924cf7c 639 }
wolfSSL 15:117db924cf7c 640 }
wolfSSL 15:117db924cf7c 641 else {
wolfSSL 15:117db924cf7c 642 WOLFSSL_MSG("ERROR ran out of static memory");
wolfSSL 15:117db924cf7c 643 #ifdef WOLFSSL_DEBUG_MEMORY
wolfSSL 15:117db924cf7c 644 printf("Looking for %lu bytes at %s:%d\n", size, func, line);
wolfSSL 15:117db924cf7c 645 #endif
wolfSSL 15:117db924cf7c 646 }
wolfSSL 15:117db924cf7c 647
wolfSSL 15:117db924cf7c 648 wc_UnLockMutex(&(mem->memory_mutex));
wolfSSL 15:117db924cf7c 649 }
wolfSSL 15:117db924cf7c 650
wolfSSL 15:117db924cf7c 651 #ifdef WOLFSSL_MALLOC_CHECK
wolfSSL 15:117db924cf7c 652 if ((wolfssl_word)res % WOLFSSL_STATIC_ALIGN) {
wolfSSL 15:117db924cf7c 653 WOLFSSL_MSG("ERROR memory is not alligned");
wolfSSL 15:117db924cf7c 654 res = NULL;
wolfSSL 15:117db924cf7c 655 }
wolfSSL 15:117db924cf7c 656 #endif
wolfSSL 15:117db924cf7c 657
wolfSSL 15:117db924cf7c 658
wolfSSL 15:117db924cf7c 659 (void)i;
wolfSSL 15:117db924cf7c 660 (void)pt;
wolfSSL 15:117db924cf7c 661 (void)type;
wolfSSL 15:117db924cf7c 662
wolfSSL 15:117db924cf7c 663 return res;
wolfSSL 15:117db924cf7c 664 }
wolfSSL 15:117db924cf7c 665
wolfSSL 15:117db924cf7c 666
wolfSSL 15:117db924cf7c 667 #ifdef WOLFSSL_DEBUG_MEMORY
wolfSSL 15:117db924cf7c 668 void wolfSSL_Free(void *ptr, void* heap, int type, const char* func, unsigned int line)
wolfSSL 15:117db924cf7c 669 #else
wolfSSL 15:117db924cf7c 670 void wolfSSL_Free(void *ptr, void* heap, int type)
wolfSSL 15:117db924cf7c 671 #endif
wolfSSL 15:117db924cf7c 672 {
wolfSSL 15:117db924cf7c 673 int i;
wolfSSL 15:117db924cf7c 674 wc_Memory* pt;
wolfSSL 15:117db924cf7c 675
wolfSSL 15:117db924cf7c 676 if (ptr) {
wolfSSL 15:117db924cf7c 677 /* check for testing heap hint was set */
wolfSSL 15:117db924cf7c 678 #ifdef WOLFSSL_HEAP_TEST
wolfSSL 15:117db924cf7c 679 if (heap == (void*)WOLFSSL_HEAP_TEST) {
wolfSSL 15:117db924cf7c 680 return free(ptr);
wolfSSL 15:117db924cf7c 681 }
wolfSSL 15:117db924cf7c 682 #endif
wolfSSL 15:117db924cf7c 683
wolfSSL 15:117db924cf7c 684 if (heap == NULL) {
wolfSSL 15:117db924cf7c 685 #ifdef WOLFSSL_HEAP_TEST
wolfSSL 15:117db924cf7c 686 /* allow using malloc for creating ctx and method */
wolfSSL 15:117db924cf7c 687 if (type == DYNAMIC_TYPE_CTX || type == DYNAMIC_TYPE_METHOD ||
wolfSSL 15:117db924cf7c 688 type == DYNAMIC_TYPE_CERT_MANAGER) {
wolfSSL 15:117db924cf7c 689 WOLFSSL_MSG("ERROR allowing null heap hint for ctx/method\n");
wolfSSL 15:117db924cf7c 690 }
wolfSSL 15:117db924cf7c 691 else {
wolfSSL 15:117db924cf7c 692 WOLFSSL_MSG("ERROR null heap hint passed into XFREE\n");
wolfSSL 15:117db924cf7c 693 }
wolfSSL 15:117db924cf7c 694 #endif
wolfSSL 15:117db924cf7c 695 #ifndef WOLFSSL_NO_MALLOC
wolfSSL 15:117db924cf7c 696 #ifdef FREERTOS
wolfSSL 15:117db924cf7c 697 vPortFree(ptr);
wolfSSL 15:117db924cf7c 698 #else
wolfSSL 15:117db924cf7c 699 free(ptr);
wolfSSL 15:117db924cf7c 700 #endif
wolfSSL 15:117db924cf7c 701 #else
wolfSSL 15:117db924cf7c 702 WOLFSSL_MSG("Error trying to call free when turned off");
wolfSSL 15:117db924cf7c 703 #endif /* WOLFSSL_NO_MALLOC */
wolfSSL 15:117db924cf7c 704 }
wolfSSL 15:117db924cf7c 705 else {
wolfSSL 15:117db924cf7c 706 WOLFSSL_HEAP_HINT* hint = (WOLFSSL_HEAP_HINT*)heap;
wolfSSL 15:117db924cf7c 707 WOLFSSL_HEAP* mem = hint->memory;
wolfSSL 15:117db924cf7c 708 word32 padSz = -(int)sizeof(wc_Memory) & (WOLFSSL_STATIC_ALIGN - 1);
wolfSSL 15:117db924cf7c 709
wolfSSL 15:117db924cf7c 710 /* get memory struct and add it to available list */
wolfSSL 15:117db924cf7c 711 pt = (wc_Memory*)((byte*)ptr - sizeof(wc_Memory) - padSz);
wolfSSL 15:117db924cf7c 712 if (wc_LockMutex(&(mem->memory_mutex)) != 0) {
wolfSSL 15:117db924cf7c 713 WOLFSSL_MSG("Bad memory_mutex lock");
wolfSSL 15:117db924cf7c 714 return;
wolfSSL 15:117db924cf7c 715 }
wolfSSL 15:117db924cf7c 716
wolfSSL 15:117db924cf7c 717 /* case of using fixed IO buffers */
wolfSSL 15:117db924cf7c 718 if (mem->flag & WOLFMEM_IO_POOL_FIXED &&
wolfSSL 15:117db924cf7c 719 (type == DYNAMIC_TYPE_OUT_BUFFER ||
wolfSSL 15:117db924cf7c 720 type == DYNAMIC_TYPE_IN_BUFFER)) {
wolfSSL 15:117db924cf7c 721 /* fixed IO pools are free'd at the end of SSL lifetime
wolfSSL 15:117db924cf7c 722 using FreeFixedIO(WOLFSSL_HEAP* heap, wc_Memory** io) */
wolfSSL 15:117db924cf7c 723 }
wolfSSL 15:117db924cf7c 724 else if (mem->flag & WOLFMEM_IO_POOL && pt->sz == WOLFMEM_IO_SZ &&
wolfSSL 15:117db924cf7c 725 (type == DYNAMIC_TYPE_OUT_BUFFER ||
wolfSSL 15:117db924cf7c 726 type == DYNAMIC_TYPE_IN_BUFFER)) {
wolfSSL 15:117db924cf7c 727 pt->next = mem->io;
wolfSSL 15:117db924cf7c 728 mem->io = pt;
wolfSSL 15:117db924cf7c 729 }
wolfSSL 15:117db924cf7c 730 else { /* general memory free */
wolfSSL 15:117db924cf7c 731 for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
wolfSSL 15:117db924cf7c 732 if (pt->sz == mem->sizeList[i]) {
wolfSSL 15:117db924cf7c 733 pt->next = mem->ava[i];
wolfSSL 15:117db924cf7c 734 mem->ava[i] = pt;
wolfSSL 15:117db924cf7c 735 break;
wolfSSL 15:117db924cf7c 736 }
wolfSSL 15:117db924cf7c 737 }
wolfSSL 15:117db924cf7c 738 }
wolfSSL 15:117db924cf7c 739 mem->inUse -= pt->sz;
wolfSSL 15:117db924cf7c 740 mem->frAlc += 1;
wolfSSL 15:117db924cf7c 741
wolfSSL 15:117db924cf7c 742 #ifdef WOLFSSL_DEBUG_MEMORY
wolfSSL 15:117db924cf7c 743 printf("Free: %p -> %u at %s:%d\n", pt->buffer, pt->sz, func, line);
wolfSSL 15:117db924cf7c 744 #endif
wolfSSL 15:117db924cf7c 745
wolfSSL 15:117db924cf7c 746 /* keep track of connection statistics if flag is set */
wolfSSL 15:117db924cf7c 747 if (mem->flag & WOLFMEM_TRACK_STATS) {
wolfSSL 15:117db924cf7c 748 WOLFSSL_MEM_CONN_STATS* stats = hint->stats;
wolfSSL 15:117db924cf7c 749 if (stats != NULL) {
wolfSSL 15:117db924cf7c 750 /* avoid under flow */
wolfSSL 15:117db924cf7c 751 if (stats->curMem > pt->sz) {
wolfSSL 15:117db924cf7c 752 stats->curMem -= pt->sz;
wolfSSL 15:117db924cf7c 753 }
wolfSSL 15:117db924cf7c 754 else {
wolfSSL 15:117db924cf7c 755 stats->curMem = 0;
wolfSSL 15:117db924cf7c 756 }
wolfSSL 15:117db924cf7c 757
wolfSSL 15:117db924cf7c 758 if (stats->curAlloc > 0) {
wolfSSL 15:117db924cf7c 759 stats->curAlloc--;
wolfSSL 15:117db924cf7c 760 }
wolfSSL 15:117db924cf7c 761 stats->totalFr++;
wolfSSL 15:117db924cf7c 762 }
wolfSSL 15:117db924cf7c 763 }
wolfSSL 15:117db924cf7c 764 wc_UnLockMutex(&(mem->memory_mutex));
wolfSSL 15:117db924cf7c 765 }
wolfSSL 15:117db924cf7c 766 }
wolfSSL 15:117db924cf7c 767
wolfSSL 15:117db924cf7c 768 (void)i;
wolfSSL 15:117db924cf7c 769 (void)pt;
wolfSSL 15:117db924cf7c 770 (void)type;
wolfSSL 15:117db924cf7c 771 }
wolfSSL 15:117db924cf7c 772
wolfSSL 15:117db924cf7c 773 #ifdef WOLFSSL_DEBUG_MEMORY
wolfSSL 15:117db924cf7c 774 void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type, const char* func, unsigned int line)
wolfSSL 15:117db924cf7c 775 #else
wolfSSL 15:117db924cf7c 776 void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type)
wolfSSL 15:117db924cf7c 777 #endif
wolfSSL 15:117db924cf7c 778 {
wolfSSL 15:117db924cf7c 779 void* res = 0;
wolfSSL 15:117db924cf7c 780 wc_Memory* pt = NULL;
wolfSSL 15:117db924cf7c 781 word32 prvSz;
wolfSSL 15:117db924cf7c 782 int i;
wolfSSL 15:117db924cf7c 783
wolfSSL 15:117db924cf7c 784 /* check for testing heap hint was set */
wolfSSL 15:117db924cf7c 785 #ifdef WOLFSSL_HEAP_TEST
wolfSSL 15:117db924cf7c 786 if (heap == (void*)WOLFSSL_HEAP_TEST) {
wolfSSL 15:117db924cf7c 787 return realloc(ptr, size);
wolfSSL 15:117db924cf7c 788 }
wolfSSL 15:117db924cf7c 789 #endif
wolfSSL 15:117db924cf7c 790
wolfSSL 15:117db924cf7c 791 if (heap == NULL) {
wolfSSL 15:117db924cf7c 792 #ifdef WOLFSSL_HEAP_TEST
wolfSSL 15:117db924cf7c 793 WOLFSSL_MSG("ERROR null heap hint passed in to XREALLOC\n");
wolfSSL 15:117db924cf7c 794 #endif
wolfSSL 15:117db924cf7c 795 #ifndef WOLFSSL_NO_MALLOC
wolfSSL 15:117db924cf7c 796 res = realloc(ptr, size);
wolfSSL 15:117db924cf7c 797 #else
wolfSSL 15:117db924cf7c 798 WOLFSSL_MSG("NO heap found to use for realloc");
wolfSSL 15:117db924cf7c 799 #endif /* WOLFSSL_NO_MALLOC */
wolfSSL 15:117db924cf7c 800 }
wolfSSL 15:117db924cf7c 801 else {
wolfSSL 15:117db924cf7c 802 WOLFSSL_HEAP_HINT* hint = (WOLFSSL_HEAP_HINT*)heap;
wolfSSL 15:117db924cf7c 803 WOLFSSL_HEAP* mem = hint->memory;
wolfSSL 15:117db924cf7c 804 word32 padSz = -(int)sizeof(wc_Memory) & (WOLFSSL_STATIC_ALIGN - 1);
wolfSSL 15:117db924cf7c 805
wolfSSL 15:117db924cf7c 806 if (wc_LockMutex(&(mem->memory_mutex)) != 0) {
wolfSSL 15:117db924cf7c 807 WOLFSSL_MSG("Bad memory_mutex lock");
wolfSSL 15:117db924cf7c 808 return NULL;
wolfSSL 15:117db924cf7c 809 }
wolfSSL 15:117db924cf7c 810
wolfSSL 15:117db924cf7c 811 /* case of using fixed IO buffers or IO pool */
wolfSSL 15:117db924cf7c 812 if (((mem->flag & WOLFMEM_IO_POOL)||(mem->flag & WOLFMEM_IO_POOL_FIXED))
wolfSSL 15:117db924cf7c 813 && (type == DYNAMIC_TYPE_OUT_BUFFER ||
wolfSSL 15:117db924cf7c 814 type == DYNAMIC_TYPE_IN_BUFFER)) {
wolfSSL 15:117db924cf7c 815 /* no realloc, is fixed size */
wolfSSL 15:117db924cf7c 816 pt = (wc_Memory*)((byte*)ptr - padSz - sizeof(wc_Memory));
wolfSSL 15:117db924cf7c 817 if (pt->sz < size) {
wolfSSL 15:117db924cf7c 818 WOLFSSL_MSG("Error IO memory was not large enough");
wolfSSL 15:117db924cf7c 819 res = NULL; /* return NULL in error case */
wolfSSL 15:117db924cf7c 820 }
wolfSSL 15:117db924cf7c 821 res = pt->buffer;
wolfSSL 15:117db924cf7c 822 }
wolfSSL 15:117db924cf7c 823 else {
wolfSSL 15:117db924cf7c 824 /* general memory */
wolfSSL 15:117db924cf7c 825 for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
wolfSSL 15:117db924cf7c 826 if ((word32)size < mem->sizeList[i]) {
wolfSSL 15:117db924cf7c 827 if (mem->ava[i] != NULL) {
wolfSSL 15:117db924cf7c 828 pt = mem->ava[i];
wolfSSL 15:117db924cf7c 829 mem->ava[i] = pt->next;
wolfSSL 15:117db924cf7c 830 break;
wolfSSL 15:117db924cf7c 831 }
wolfSSL 15:117db924cf7c 832 }
wolfSSL 15:117db924cf7c 833 }
wolfSSL 15:117db924cf7c 834
wolfSSL 15:117db924cf7c 835 if (pt != NULL && res == NULL) {
wolfSSL 15:117db924cf7c 836 res = pt->buffer;
wolfSSL 15:117db924cf7c 837
wolfSSL 15:117db924cf7c 838 /* copy over original information and free ptr */
wolfSSL 15:117db924cf7c 839 prvSz = ((wc_Memory*)((byte*)ptr - padSz -
wolfSSL 15:117db924cf7c 840 sizeof(wc_Memory)))->sz;
wolfSSL 15:117db924cf7c 841 prvSz = (prvSz > pt->sz)? pt->sz: prvSz;
wolfSSL 15:117db924cf7c 842 XMEMCPY(pt->buffer, ptr, prvSz);
wolfSSL 15:117db924cf7c 843 mem->inUse += pt->sz;
wolfSSL 15:117db924cf7c 844 mem->alloc += 1;
wolfSSL 15:117db924cf7c 845
wolfSSL 15:117db924cf7c 846 /* free memory that was previously being used */
wolfSSL 15:117db924cf7c 847 wc_UnLockMutex(&(mem->memory_mutex));
wolfSSL 15:117db924cf7c 848 wolfSSL_Free(ptr, heap, type
wolfSSL 15:117db924cf7c 849 #ifdef WOLFSSL_DEBUG_MEMORY
wolfSSL 15:117db924cf7c 850 , func, line
wolfSSL 15:117db924cf7c 851 #endif
wolfSSL 15:117db924cf7c 852 );
wolfSSL 15:117db924cf7c 853 if (wc_LockMutex(&(mem->memory_mutex)) != 0) {
wolfSSL 15:117db924cf7c 854 WOLFSSL_MSG("Bad memory_mutex lock");
wolfSSL 15:117db924cf7c 855 return NULL;
wolfSSL 15:117db924cf7c 856 }
wolfSSL 15:117db924cf7c 857 }
wolfSSL 15:117db924cf7c 858 }
wolfSSL 15:117db924cf7c 859 wc_UnLockMutex(&(mem->memory_mutex));
wolfSSL 15:117db924cf7c 860 }
wolfSSL 15:117db924cf7c 861
wolfSSL 15:117db924cf7c 862 #ifdef WOLFSSL_MALLOC_CHECK
wolfSSL 15:117db924cf7c 863 if ((wolfssl_word)res % WOLFSSL_STATIC_ALIGN) {
wolfSSL 15:117db924cf7c 864 WOLFSSL_MSG("ERROR memory is not alligned");
wolfSSL 15:117db924cf7c 865 res = NULL;
wolfSSL 15:117db924cf7c 866 }
wolfSSL 15:117db924cf7c 867 #endif
wolfSSL 15:117db924cf7c 868
wolfSSL 15:117db924cf7c 869 (void)i;
wolfSSL 15:117db924cf7c 870 (void)pt;
wolfSSL 15:117db924cf7c 871 (void)type;
wolfSSL 15:117db924cf7c 872
wolfSSL 15:117db924cf7c 873 return res;
wolfSSL 15:117db924cf7c 874 }
wolfSSL 15:117db924cf7c 875 #endif /* WOLFSSL_STATIC_MEMORY */
wolfSSL 15:117db924cf7c 876
wolfSSL 15:117db924cf7c 877 #endif /* USE_WOLFSSL_MEMORY */
wolfSSL 15:117db924cf7c 878
wolfSSL 15:117db924cf7c 879
wolfSSL 15:117db924cf7c 880 #ifdef HAVE_IO_POOL
wolfSSL 15:117db924cf7c 881
wolfSSL 15:117db924cf7c 882 /* Example for user io pool, shared build may need definitions in lib proper */
wolfSSL 15:117db924cf7c 883
wolfSSL 15:117db924cf7c 884 #include <wolfssl/wolfcrypt/types.h>
wolfSSL 15:117db924cf7c 885 #include <stdlib.h>
wolfSSL 15:117db924cf7c 886
wolfSSL 15:117db924cf7c 887 #ifndef HAVE_THREAD_LS
wolfSSL 15:117db924cf7c 888 #error "Oops, simple I/O pool example needs thread local storage"
wolfSSL 15:117db924cf7c 889 #endif
wolfSSL 15:117db924cf7c 890
wolfSSL 15:117db924cf7c 891
wolfSSL 15:117db924cf7c 892 /* allow simple per thread in and out pools */
wolfSSL 15:117db924cf7c 893 /* use 17k size since max record size is 16k plus overhead */
wolfSSL 15:117db924cf7c 894 static THREAD_LS_T byte pool_in[17*1024];
wolfSSL 15:117db924cf7c 895 static THREAD_LS_T byte pool_out[17*1024];
wolfSSL 15:117db924cf7c 896
wolfSSL 15:117db924cf7c 897
wolfSSL 15:117db924cf7c 898 void* XMALLOC(size_t n, void* heap, int type)
wolfSSL 15:117db924cf7c 899 {
wolfSSL 15:117db924cf7c 900 (void)heap;
wolfSSL 15:117db924cf7c 901
wolfSSL 15:117db924cf7c 902 if (type == DYNAMIC_TYPE_IN_BUFFER) {
wolfSSL 15:117db924cf7c 903 if (n < sizeof(pool_in))
wolfSSL 15:117db924cf7c 904 return pool_in;
wolfSSL 15:117db924cf7c 905 else
wolfSSL 15:117db924cf7c 906 return NULL;
wolfSSL 15:117db924cf7c 907 }
wolfSSL 15:117db924cf7c 908
wolfSSL 15:117db924cf7c 909 if (type == DYNAMIC_TYPE_OUT_BUFFER) {
wolfSSL 15:117db924cf7c 910 if (n < sizeof(pool_out))
wolfSSL 15:117db924cf7c 911 return pool_out;
wolfSSL 15:117db924cf7c 912 else
wolfSSL 15:117db924cf7c 913 return NULL;
wolfSSL 15:117db924cf7c 914 }
wolfSSL 15:117db924cf7c 915
wolfSSL 15:117db924cf7c 916 return malloc(n);
wolfSSL 15:117db924cf7c 917 }
wolfSSL 15:117db924cf7c 918
wolfSSL 15:117db924cf7c 919 void* XREALLOC(void *p, size_t n, void* heap, int type)
wolfSSL 15:117db924cf7c 920 {
wolfSSL 15:117db924cf7c 921 (void)heap;
wolfSSL 15:117db924cf7c 922
wolfSSL 15:117db924cf7c 923 if (type == DYNAMIC_TYPE_IN_BUFFER) {
wolfSSL 15:117db924cf7c 924 if (n < sizeof(pool_in))
wolfSSL 15:117db924cf7c 925 return pool_in;
wolfSSL 15:117db924cf7c 926 else
wolfSSL 15:117db924cf7c 927 return NULL;
wolfSSL 15:117db924cf7c 928 }
wolfSSL 15:117db924cf7c 929
wolfSSL 15:117db924cf7c 930 if (type == DYNAMIC_TYPE_OUT_BUFFER) {
wolfSSL 15:117db924cf7c 931 if (n < sizeof(pool_out))
wolfSSL 15:117db924cf7c 932 return pool_out;
wolfSSL 15:117db924cf7c 933 else
wolfSSL 15:117db924cf7c 934 return NULL;
wolfSSL 15:117db924cf7c 935 }
wolfSSL 15:117db924cf7c 936
wolfSSL 15:117db924cf7c 937 return realloc(p, n);
wolfSSL 15:117db924cf7c 938 }
wolfSSL 15:117db924cf7c 939
wolfSSL 15:117db924cf7c 940 void XFREE(void *p, void* heap, int type)
wolfSSL 15:117db924cf7c 941 {
wolfSSL 15:117db924cf7c 942 (void)heap;
wolfSSL 15:117db924cf7c 943
wolfSSL 15:117db924cf7c 944 if (type == DYNAMIC_TYPE_IN_BUFFER)
wolfSSL 15:117db924cf7c 945 return; /* do nothing, static pool */
wolfSSL 15:117db924cf7c 946
wolfSSL 15:117db924cf7c 947 if (type == DYNAMIC_TYPE_OUT_BUFFER)
wolfSSL 15:117db924cf7c 948 return; /* do nothing, static pool */
wolfSSL 15:117db924cf7c 949
wolfSSL 15:117db924cf7c 950 free(p);
wolfSSL 15:117db924cf7c 951 }
wolfSSL 15:117db924cf7c 952
wolfSSL 15:117db924cf7c 953 #endif /* HAVE_IO_POOL */
wolfSSL 15:117db924cf7c 954
wolfSSL 15:117db924cf7c 955