wolfSSL 3.11.1 for TLS1.3 beta
Fork of wolfSSL by
wolfssl/wolfcrypt/memory.h@13:80fb167dafdf, 2017-05-30 (annotated)
- Committer:
- wolfSSL
- Date:
- Tue May 30 06:16:19 2017 +0000
- Revision:
- 13:80fb167dafdf
- Parent:
- 11:cee25a834751
wolfSSL 3.11.1: TLS1.3 Beta
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wolfSSL | 11:cee25a834751 | 1 | /* memory.h |
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 | /* submitted by eof */ |
wolfSSL | 11:cee25a834751 | 24 | |
wolfSSL | 11:cee25a834751 | 25 | |
wolfSSL | 11:cee25a834751 | 26 | #ifndef WOLFSSL_MEMORY_H |
wolfSSL | 11:cee25a834751 | 27 | #define WOLFSSL_MEMORY_H |
wolfSSL | 11:cee25a834751 | 28 | |
wolfSSL | 11:cee25a834751 | 29 | #include <stdlib.h> |
wolfSSL | 11:cee25a834751 | 30 | #include <wolfssl/wolfcrypt/types.h> |
wolfSSL | 11:cee25a834751 | 31 | |
wolfSSL | 11:cee25a834751 | 32 | #ifdef __cplusplus |
wolfSSL | 11:cee25a834751 | 33 | extern "C" { |
wolfSSL | 11:cee25a834751 | 34 | #endif |
wolfSSL | 11:cee25a834751 | 35 | |
wolfSSL | 11:cee25a834751 | 36 | #ifdef WOLFSSL_STATIC_MEMORY |
wolfSSL | 11:cee25a834751 | 37 | #ifdef WOLFSSL_DEBUG_MEMORY |
wolfSSL | 11:cee25a834751 | 38 | typedef void *(*wolfSSL_Malloc_cb)(size_t size, void* heap, int type, const char* func, unsigned int line); |
wolfSSL | 11:cee25a834751 | 39 | typedef void (*wolfSSL_Free_cb)(void *ptr, void* heap, int type, const char* func, unsigned int line); |
wolfSSL | 11:cee25a834751 | 40 | typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size, void* heap, int type, const char* func, unsigned int line); |
wolfSSL | 11:cee25a834751 | 41 | WOLFSSL_API void* wolfSSL_Malloc(size_t size, void* heap, int type, const char* func, unsigned int line); |
wolfSSL | 11:cee25a834751 | 42 | WOLFSSL_API void wolfSSL_Free(void *ptr, void* heap, int type, const char* func, unsigned int line); |
wolfSSL | 11:cee25a834751 | 43 | WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type, const char* func, unsigned int line); |
wolfSSL | 11:cee25a834751 | 44 | #else |
wolfSSL | 11:cee25a834751 | 45 | typedef void *(*wolfSSL_Malloc_cb)(size_t size, void* heap, int type); |
wolfSSL | 11:cee25a834751 | 46 | typedef void (*wolfSSL_Free_cb)(void *ptr, void* heap, int type); |
wolfSSL | 11:cee25a834751 | 47 | typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size, void* heap, int type); |
wolfSSL | 11:cee25a834751 | 48 | WOLFSSL_API void* wolfSSL_Malloc(size_t size, void* heap, int type); |
wolfSSL | 11:cee25a834751 | 49 | WOLFSSL_API void wolfSSL_Free(void *ptr, void* heap, int type); |
wolfSSL | 11:cee25a834751 | 50 | WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type); |
wolfSSL | 11:cee25a834751 | 51 | #endif /* WOLFSSL_DEBUG_MEMORY */ |
wolfSSL | 11:cee25a834751 | 52 | #else |
wolfSSL | 11:cee25a834751 | 53 | #ifdef WOLFSSL_DEBUG_MEMORY |
wolfSSL | 11:cee25a834751 | 54 | typedef void *(*wolfSSL_Malloc_cb)(size_t size, const char* func, unsigned int line); |
wolfSSL | 11:cee25a834751 | 55 | typedef void (*wolfSSL_Free_cb)(void *ptr, const char* func, unsigned int line); |
wolfSSL | 11:cee25a834751 | 56 | typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size, const char* func, unsigned int line); |
wolfSSL | 11:cee25a834751 | 57 | |
wolfSSL | 11:cee25a834751 | 58 | /* Public in case user app wants to use XMALLOC/XFREE */ |
wolfSSL | 11:cee25a834751 | 59 | WOLFSSL_API void* wolfSSL_Malloc(size_t size, const char* func, unsigned int line); |
wolfSSL | 11:cee25a834751 | 60 | WOLFSSL_API void wolfSSL_Free(void *ptr, const char* func, unsigned int line); |
wolfSSL | 11:cee25a834751 | 61 | WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size, const char* func, unsigned int line); |
wolfSSL | 11:cee25a834751 | 62 | #else |
wolfSSL | 11:cee25a834751 | 63 | typedef void *(*wolfSSL_Malloc_cb)(size_t size); |
wolfSSL | 11:cee25a834751 | 64 | typedef void (*wolfSSL_Free_cb)(void *ptr); |
wolfSSL | 11:cee25a834751 | 65 | typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size); |
wolfSSL | 11:cee25a834751 | 66 | /* Public in case user app wants to use XMALLOC/XFREE */ |
wolfSSL | 11:cee25a834751 | 67 | WOLFSSL_API void* wolfSSL_Malloc(size_t size); |
wolfSSL | 11:cee25a834751 | 68 | WOLFSSL_API void wolfSSL_Free(void *ptr); |
wolfSSL | 11:cee25a834751 | 69 | WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size); |
wolfSSL | 11:cee25a834751 | 70 | #endif /* WOLFSSL_DEBUG_MEMORY */ |
wolfSSL | 11:cee25a834751 | 71 | #endif /* WOLFSSL_STATIC_MEMORY */ |
wolfSSL | 11:cee25a834751 | 72 | |
wolfSSL | 11:cee25a834751 | 73 | /* Public get/set functions */ |
wolfSSL | 11:cee25a834751 | 74 | WOLFSSL_API int wolfSSL_SetAllocators(wolfSSL_Malloc_cb, |
wolfSSL | 11:cee25a834751 | 75 | wolfSSL_Free_cb, |
wolfSSL | 11:cee25a834751 | 76 | wolfSSL_Realloc_cb); |
wolfSSL | 11:cee25a834751 | 77 | |
wolfSSL | 11:cee25a834751 | 78 | WOLFSSL_API int wolfSSL_GetAllocators(wolfSSL_Malloc_cb*, |
wolfSSL | 11:cee25a834751 | 79 | wolfSSL_Free_cb*, |
wolfSSL | 11:cee25a834751 | 80 | wolfSSL_Realloc_cb*); |
wolfSSL | 11:cee25a834751 | 81 | |
wolfSSL | 11:cee25a834751 | 82 | #ifdef WOLFSSL_STATIC_MEMORY |
wolfSSL | 11:cee25a834751 | 83 | #define WOLFSSL_STATIC_TIMEOUT 1 |
wolfSSL | 11:cee25a834751 | 84 | #ifndef WOLFSSL_STATIC_ALIGN |
wolfSSL | 11:cee25a834751 | 85 | #define WOLFSSL_STATIC_ALIGN 16 |
wolfSSL | 11:cee25a834751 | 86 | #endif |
wolfSSL | 11:cee25a834751 | 87 | #ifndef WOLFMEM_MAX_BUCKETS |
wolfSSL | 11:cee25a834751 | 88 | #define WOLFMEM_MAX_BUCKETS 9 |
wolfSSL | 11:cee25a834751 | 89 | #endif |
wolfSSL | 11:cee25a834751 | 90 | #define WOLFMEM_DEF_BUCKETS 9 /* number of default memory blocks */ |
wolfSSL | 11:cee25a834751 | 91 | #define WOLFMEM_IO_SZ 16992 /* 16 byte aligned */ |
wolfSSL | 11:cee25a834751 | 92 | #ifndef WOLFMEM_BUCKETS |
wolfSSL | 11:cee25a834751 | 93 | /* default size of chunks of memory to seperate into |
wolfSSL | 11:cee25a834751 | 94 | * having session certs enabled makes a 21k SSL struct */ |
wolfSSL | 11:cee25a834751 | 95 | #ifndef SESSION_CERTS |
wolfSSL | 11:cee25a834751 | 96 | #define WOLFMEM_BUCKETS 64,128,256,512,1024,2432,3456,4544,16128 |
wolfSSL | 11:cee25a834751 | 97 | #else |
wolfSSL | 11:cee25a834751 | 98 | #define WOLFMEM_BUCKETS 64,128,256,512,1024,2432,3456,4544,21056 |
wolfSSL | 11:cee25a834751 | 99 | #endif |
wolfSSL | 11:cee25a834751 | 100 | #endif |
wolfSSL | 11:cee25a834751 | 101 | #ifndef WOLFMEM_DIST |
wolfSSL | 11:cee25a834751 | 102 | #define WOLFMEM_DIST 8,4,4,12,4,5,8,1,1 |
wolfSSL | 11:cee25a834751 | 103 | #endif |
wolfSSL | 11:cee25a834751 | 104 | |
wolfSSL | 11:cee25a834751 | 105 | /* flags for loading static memory (one hot bit) */ |
wolfSSL | 11:cee25a834751 | 106 | #define WOLFMEM_GENERAL 0x01 |
wolfSSL | 11:cee25a834751 | 107 | #define WOLFMEM_IO_POOL 0x02 |
wolfSSL | 11:cee25a834751 | 108 | #define WOLFMEM_IO_POOL_FIXED 0x04 |
wolfSSL | 11:cee25a834751 | 109 | #define WOLFMEM_TRACK_STATS 0x08 |
wolfSSL | 11:cee25a834751 | 110 | |
wolfSSL | 11:cee25a834751 | 111 | #ifndef WOLFSSL_MEM_GUARD |
wolfSSL | 11:cee25a834751 | 112 | #define WOLFSSL_MEM_GUARD |
wolfSSL | 11:cee25a834751 | 113 | typedef struct WOLFSSL_MEM_STATS WOLFSSL_MEM_STATS; |
wolfSSL | 11:cee25a834751 | 114 | typedef struct WOLFSSL_MEM_CONN_STATS WOLFSSL_MEM_CONN_STATS; |
wolfSSL | 11:cee25a834751 | 115 | #endif |
wolfSSL | 11:cee25a834751 | 116 | |
wolfSSL | 11:cee25a834751 | 117 | struct WOLFSSL_MEM_CONN_STATS { |
wolfSSL | 11:cee25a834751 | 118 | word32 peakMem; /* peak memory usage */ |
wolfSSL | 11:cee25a834751 | 119 | word32 curMem; /* current memory usage */ |
wolfSSL | 11:cee25a834751 | 120 | word32 peakAlloc; /* peak memory allocations */ |
wolfSSL | 11:cee25a834751 | 121 | word32 curAlloc; /* current memory allocations */ |
wolfSSL | 11:cee25a834751 | 122 | word32 totalAlloc;/* total memory allocations for lifetime */ |
wolfSSL | 11:cee25a834751 | 123 | word32 totalFr; /* total frees for lifetime */ |
wolfSSL | 11:cee25a834751 | 124 | }; |
wolfSSL | 11:cee25a834751 | 125 | |
wolfSSL | 11:cee25a834751 | 126 | struct WOLFSSL_MEM_STATS { |
wolfSSL | 11:cee25a834751 | 127 | word32 curAlloc; /* current memory allocations */ |
wolfSSL | 11:cee25a834751 | 128 | word32 totalAlloc;/* total memory allocations for lifetime */ |
wolfSSL | 11:cee25a834751 | 129 | word32 totalFr; /* total frees for lifetime */ |
wolfSSL | 11:cee25a834751 | 130 | word32 totalUse; /* total amount of memory used in blocks */ |
wolfSSL | 11:cee25a834751 | 131 | word32 avaIO; /* available IO specific pools */ |
wolfSSL | 11:cee25a834751 | 132 | word32 maxHa; /* max number of concurent handshakes allowed */ |
wolfSSL | 11:cee25a834751 | 133 | word32 maxIO; /* max number of concurent IO connections allowed */ |
wolfSSL | 11:cee25a834751 | 134 | word32 blockSz[WOLFMEM_MAX_BUCKETS]; /* block sizes in stacks */ |
wolfSSL | 11:cee25a834751 | 135 | word32 avaBlock[WOLFMEM_MAX_BUCKETS];/* ava block sizes */ |
wolfSSL | 11:cee25a834751 | 136 | word32 usedBlock[WOLFMEM_MAX_BUCKETS]; |
wolfSSL | 11:cee25a834751 | 137 | int flag; /* flag used */ |
wolfSSL | 11:cee25a834751 | 138 | }; |
wolfSSL | 11:cee25a834751 | 139 | |
wolfSSL | 11:cee25a834751 | 140 | typedef struct wc_Memory wc_Memory; /* internal structure for mem bucket */ |
wolfSSL | 11:cee25a834751 | 141 | typedef struct WOLFSSL_HEAP { |
wolfSSL | 11:cee25a834751 | 142 | wc_Memory* ava[WOLFMEM_MAX_BUCKETS]; |
wolfSSL | 11:cee25a834751 | 143 | wc_Memory* io; /* list of buffers to use for IO */ |
wolfSSL | 11:cee25a834751 | 144 | word32 maxHa; /* max concurent handshakes */ |
wolfSSL | 11:cee25a834751 | 145 | word32 curHa; |
wolfSSL | 11:cee25a834751 | 146 | word32 maxIO; /* max concurrent IO connections */ |
wolfSSL | 11:cee25a834751 | 147 | word32 curIO; |
wolfSSL | 11:cee25a834751 | 148 | word32 sizeList[WOLFMEM_MAX_BUCKETS];/* memory sizes in ava list */ |
wolfSSL | 11:cee25a834751 | 149 | word32 distList[WOLFMEM_MAX_BUCKETS];/* general distribution */ |
wolfSSL | 11:cee25a834751 | 150 | word32 inUse; /* amount of memory currently in use */ |
wolfSSL | 11:cee25a834751 | 151 | word32 ioUse; |
wolfSSL | 11:cee25a834751 | 152 | word32 alloc; /* total number of allocs */ |
wolfSSL | 11:cee25a834751 | 153 | word32 frAlc; /* total number of frees */ |
wolfSSL | 11:cee25a834751 | 154 | int flag; |
wolfSSL | 11:cee25a834751 | 155 | wolfSSL_Mutex memory_mutex; |
wolfSSL | 11:cee25a834751 | 156 | } WOLFSSL_HEAP; |
wolfSSL | 11:cee25a834751 | 157 | |
wolfSSL | 11:cee25a834751 | 158 | /* structure passed into XMALLOC as heap hint |
wolfSSL | 11:cee25a834751 | 159 | * having this abstraction allows tracking statistics of individual ssl's |
wolfSSL | 11:cee25a834751 | 160 | */ |
wolfSSL | 11:cee25a834751 | 161 | typedef struct WOLFSSL_HEAP_HINT { |
wolfSSL | 11:cee25a834751 | 162 | WOLFSSL_HEAP* memory; |
wolfSSL | 11:cee25a834751 | 163 | WOLFSSL_MEM_CONN_STATS* stats; /* hold individual connection stats */ |
wolfSSL | 11:cee25a834751 | 164 | wc_Memory* outBuf; /* set if using fixed io buffers */ |
wolfSSL | 11:cee25a834751 | 165 | wc_Memory* inBuf; |
wolfSSL | 11:cee25a834751 | 166 | byte haFlag; /* flag used for checking handshake count */ |
wolfSSL | 11:cee25a834751 | 167 | } WOLFSSL_HEAP_HINT; |
wolfSSL | 11:cee25a834751 | 168 | |
wolfSSL | 11:cee25a834751 | 169 | WOLFSSL_API int wc_LoadStaticMemory(WOLFSSL_HEAP_HINT** pHint, |
wolfSSL | 11:cee25a834751 | 170 | unsigned char* buf, unsigned int sz, int flag, int max); |
wolfSSL | 11:cee25a834751 | 171 | |
wolfSSL | 11:cee25a834751 | 172 | WOLFSSL_LOCAL int wolfSSL_init_memory_heap(WOLFSSL_HEAP* heap); |
wolfSSL | 11:cee25a834751 | 173 | WOLFSSL_LOCAL int wolfSSL_load_static_memory(byte* buffer, word32 sz, |
wolfSSL | 11:cee25a834751 | 174 | int flag, WOLFSSL_HEAP* heap); |
wolfSSL | 11:cee25a834751 | 175 | WOLFSSL_LOCAL int wolfSSL_GetMemStats(WOLFSSL_HEAP* heap, |
wolfSSL | 11:cee25a834751 | 176 | WOLFSSL_MEM_STATS* stats); |
wolfSSL | 11:cee25a834751 | 177 | WOLFSSL_LOCAL int SetFixedIO(WOLFSSL_HEAP* heap, wc_Memory** io); |
wolfSSL | 11:cee25a834751 | 178 | WOLFSSL_LOCAL int FreeFixedIO(WOLFSSL_HEAP* heap, wc_Memory** io); |
wolfSSL | 11:cee25a834751 | 179 | |
wolfSSL | 11:cee25a834751 | 180 | WOLFSSL_API int wolfSSL_StaticBufferSz(byte* buffer, word32 sz, int flag); |
wolfSSL | 11:cee25a834751 | 181 | WOLFSSL_API int wolfSSL_MemoryPaddingSz(void); |
wolfSSL | 11:cee25a834751 | 182 | #endif /* WOLFSSL_STATIC_MEMORY */ |
wolfSSL | 11:cee25a834751 | 183 | |
wolfSSL | 11:cee25a834751 | 184 | #ifdef __cplusplus |
wolfSSL | 11:cee25a834751 | 185 | } /* extern "C" */ |
wolfSSL | 11:cee25a834751 | 186 | #endif |
wolfSSL | 11:cee25a834751 | 187 | |
wolfSSL | 11:cee25a834751 | 188 | #endif /* WOLFSSL_MEMORY_H */ |
wolfSSL | 11:cee25a834751 | 189 | |
wolfSSL | 11:cee25a834751 | 190 |