wolfSSL 3.11.1 for TLS1.3 beta

Fork of wolfSSL by wolf SSL

Committer:
wolfSSL
Date:
Tue May 30 01:44:10 2017 +0000
Revision:
11:cee25a834751
wolfSSL 3.11.0

Who changed what in which revision?

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