wolfSSL SSL/TLS library, support up to TLS1.3
Dependents: CyaSSL-Twitter-OAuth4Tw Example-client-tls-cert TwitterReader TweetTest ... more
wolfssl/wolfcrypt/memory.h@7:481bce714567, 2017-05-02 (annotated)
- Committer:
- wolfSSL
- Date:
- Tue May 02 08:44:47 2017 +0000
- Revision:
- 7:481bce714567
wolfSSL3.10.2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wolfSSL | 7:481bce714567 | 1 | /* memory.h |
wolfSSL | 7:481bce714567 | 2 | * |
wolfSSL | 7:481bce714567 | 3 | * Copyright (C) 2006-2016 wolfSSL Inc. |
wolfSSL | 7:481bce714567 | 4 | * |
wolfSSL | 7:481bce714567 | 5 | * This file is part of wolfSSL. |
wolfSSL | 7:481bce714567 | 6 | * |
wolfSSL | 7:481bce714567 | 7 | * wolfSSL is free software; you can redistribute it and/or modify |
wolfSSL | 7:481bce714567 | 8 | * it under the terms of the GNU General Public License as published by |
wolfSSL | 7:481bce714567 | 9 | * the Free Software Foundation; either version 2 of the License, or |
wolfSSL | 7:481bce714567 | 10 | * (at your option) any later version. |
wolfSSL | 7:481bce714567 | 11 | * |
wolfSSL | 7:481bce714567 | 12 | * wolfSSL is distributed in the hope that it will be useful, |
wolfSSL | 7:481bce714567 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
wolfSSL | 7:481bce714567 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
wolfSSL | 7:481bce714567 | 15 | * GNU General Public License for more details. |
wolfSSL | 7:481bce714567 | 16 | * |
wolfSSL | 7:481bce714567 | 17 | * You should have received a copy of the GNU General Public License |
wolfSSL | 7:481bce714567 | 18 | * along with this program; if not, write to the Free Software |
wolfSSL | 7:481bce714567 | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
wolfSSL | 7:481bce714567 | 20 | */ |
wolfSSL | 7:481bce714567 | 21 | |
wolfSSL | 7:481bce714567 | 22 | |
wolfSSL | 7:481bce714567 | 23 | /* submitted by eof */ |
wolfSSL | 7:481bce714567 | 24 | |
wolfSSL | 7:481bce714567 | 25 | |
wolfSSL | 7:481bce714567 | 26 | #ifndef WOLFSSL_MEMORY_H |
wolfSSL | 7:481bce714567 | 27 | #define WOLFSSL_MEMORY_H |
wolfSSL | 7:481bce714567 | 28 | |
wolfSSL | 7:481bce714567 | 29 | #include <stdlib.h> |
wolfSSL | 7:481bce714567 | 30 | #include <wolfssl/wolfcrypt/types.h> |
wolfSSL | 7:481bce714567 | 31 | |
wolfSSL | 7:481bce714567 | 32 | #ifdef __cplusplus |
wolfSSL | 7:481bce714567 | 33 | extern "C" { |
wolfSSL | 7:481bce714567 | 34 | #endif |
wolfSSL | 7:481bce714567 | 35 | |
wolfSSL | 7:481bce714567 | 36 | #ifdef WOLFSSL_STATIC_MEMORY |
wolfSSL | 7:481bce714567 | 37 | #ifdef WOLFSSL_DEBUG_MEMORY |
wolfSSL | 7:481bce714567 | 38 | typedef void *(*wolfSSL_Malloc_cb)(size_t size, void* heap, int type, const char* func, unsigned int line); |
wolfSSL | 7:481bce714567 | 39 | typedef void (*wolfSSL_Free_cb)(void *ptr, void* heap, int type, const char* func, unsigned int line); |
wolfSSL | 7:481bce714567 | 40 | typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size, void* heap, int type, const char* func, unsigned int line); |
wolfSSL | 7:481bce714567 | 41 | WOLFSSL_API void* wolfSSL_Malloc(size_t size, void* heap, int type, const char* func, unsigned int line); |
wolfSSL | 7:481bce714567 | 42 | WOLFSSL_API void wolfSSL_Free(void *ptr, void* heap, int type, const char* func, unsigned int line); |
wolfSSL | 7:481bce714567 | 43 | WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type, const char* func, unsigned int line); |
wolfSSL | 7:481bce714567 | 44 | #else |
wolfSSL | 7:481bce714567 | 45 | typedef void *(*wolfSSL_Malloc_cb)(size_t size, void* heap, int type); |
wolfSSL | 7:481bce714567 | 46 | typedef void (*wolfSSL_Free_cb)(void *ptr, void* heap, int type); |
wolfSSL | 7:481bce714567 | 47 | typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size, void* heap, int type); |
wolfSSL | 7:481bce714567 | 48 | WOLFSSL_API void* wolfSSL_Malloc(size_t size, void* heap, int type); |
wolfSSL | 7:481bce714567 | 49 | WOLFSSL_API void wolfSSL_Free(void *ptr, void* heap, int type); |
wolfSSL | 7:481bce714567 | 50 | WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type); |
wolfSSL | 7:481bce714567 | 51 | #endif /* WOLFSSL_DEBUG_MEMORY */ |
wolfSSL | 7:481bce714567 | 52 | #else |
wolfSSL | 7:481bce714567 | 53 | #ifdef WOLFSSL_DEBUG_MEMORY |
wolfSSL | 7:481bce714567 | 54 | typedef void *(*wolfSSL_Malloc_cb)(size_t size, const char* func, unsigned int line); |
wolfSSL | 7:481bce714567 | 55 | typedef void (*wolfSSL_Free_cb)(void *ptr, const char* func, unsigned int line); |
wolfSSL | 7:481bce714567 | 56 | typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size, const char* func, unsigned int line); |
wolfSSL | 7:481bce714567 | 57 | |
wolfSSL | 7:481bce714567 | 58 | /* Public in case user app wants to use XMALLOC/XFREE */ |
wolfSSL | 7:481bce714567 | 59 | WOLFSSL_API void* wolfSSL_Malloc(size_t size, const char* func, unsigned int line); |
wolfSSL | 7:481bce714567 | 60 | WOLFSSL_API void wolfSSL_Free(void *ptr, const char* func, unsigned int line); |
wolfSSL | 7:481bce714567 | 61 | WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size, const char* func, unsigned int line); |
wolfSSL | 7:481bce714567 | 62 | #else |
wolfSSL | 7:481bce714567 | 63 | typedef void *(*wolfSSL_Malloc_cb)(size_t size); |
wolfSSL | 7:481bce714567 | 64 | typedef void (*wolfSSL_Free_cb)(void *ptr); |
wolfSSL | 7:481bce714567 | 65 | typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size); |
wolfSSL | 7:481bce714567 | 66 | /* Public in case user app wants to use XMALLOC/XFREE */ |
wolfSSL | 7:481bce714567 | 67 | WOLFSSL_API void* wolfSSL_Malloc(size_t size); |
wolfSSL | 7:481bce714567 | 68 | WOLFSSL_API void wolfSSL_Free(void *ptr); |
wolfSSL | 7:481bce714567 | 69 | WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size); |
wolfSSL | 7:481bce714567 | 70 | #endif /* WOLFSSL_DEBUG_MEMORY */ |
wolfSSL | 7:481bce714567 | 71 | #endif /* WOLFSSL_STATIC_MEMORY */ |
wolfSSL | 7:481bce714567 | 72 | |
wolfSSL | 7:481bce714567 | 73 | /* Public set function */ |
wolfSSL | 7:481bce714567 | 74 | WOLFSSL_API int wolfSSL_SetAllocators(wolfSSL_Malloc_cb malloc_function, |
wolfSSL | 7:481bce714567 | 75 | wolfSSL_Free_cb free_function, |
wolfSSL | 7:481bce714567 | 76 | wolfSSL_Realloc_cb realloc_function); |
wolfSSL | 7:481bce714567 | 77 | |
wolfSSL | 7:481bce714567 | 78 | #ifdef WOLFSSL_STATIC_MEMORY |
wolfSSL | 7:481bce714567 | 79 | #define WOLFSSL_STATIC_TIMEOUT 1 |
wolfSSL | 7:481bce714567 | 80 | #ifndef WOLFSSL_STATIC_ALIGN |
wolfSSL | 7:481bce714567 | 81 | #define WOLFSSL_STATIC_ALIGN 16 |
wolfSSL | 7:481bce714567 | 82 | #endif |
wolfSSL | 7:481bce714567 | 83 | #ifndef WOLFMEM_MAX_BUCKETS |
wolfSSL | 7:481bce714567 | 84 | #define WOLFMEM_MAX_BUCKETS 9 |
wolfSSL | 7:481bce714567 | 85 | #endif |
wolfSSL | 7:481bce714567 | 86 | #define WOLFMEM_DEF_BUCKETS 9 /* number of default memory blocks */ |
wolfSSL | 7:481bce714567 | 87 | #define WOLFMEM_IO_SZ 16992 /* 16 byte aligned */ |
wolfSSL | 7:481bce714567 | 88 | #ifndef WOLFMEM_BUCKETS |
wolfSSL | 7:481bce714567 | 89 | /* default size of chunks of memory to seperate into |
wolfSSL | 7:481bce714567 | 90 | * having session certs enabled makes a 21k SSL struct */ |
wolfSSL | 7:481bce714567 | 91 | #ifndef SESSION_CERTS |
wolfSSL | 7:481bce714567 | 92 | #define WOLFMEM_BUCKETS 64,128,256,512,1024,2432,3456,4544,16128 |
wolfSSL | 7:481bce714567 | 93 | #else |
wolfSSL | 7:481bce714567 | 94 | #define WOLFMEM_BUCKETS 64,128,256,512,1024,2432,3456,4544,21056 |
wolfSSL | 7:481bce714567 | 95 | #endif |
wolfSSL | 7:481bce714567 | 96 | #endif |
wolfSSL | 7:481bce714567 | 97 | #ifndef WOLFMEM_DIST |
wolfSSL | 7:481bce714567 | 98 | #define WOLFMEM_DIST 8,4,4,12,4,5,2,1,1 |
wolfSSL | 7:481bce714567 | 99 | #endif |
wolfSSL | 7:481bce714567 | 100 | |
wolfSSL | 7:481bce714567 | 101 | /* flags for loading static memory (one hot bit) */ |
wolfSSL | 7:481bce714567 | 102 | #define WOLFMEM_GENERAL 0x01 |
wolfSSL | 7:481bce714567 | 103 | #define WOLFMEM_IO_POOL 0x02 |
wolfSSL | 7:481bce714567 | 104 | #define WOLFMEM_IO_POOL_FIXED 0x04 |
wolfSSL | 7:481bce714567 | 105 | #define WOLFMEM_TRACK_STATS 0x08 |
wolfSSL | 7:481bce714567 | 106 | |
wolfSSL | 7:481bce714567 | 107 | #ifndef WOLFSSL_MEM_GUARD |
wolfSSL | 7:481bce714567 | 108 | #define WOLFSSL_MEM_GUARD |
wolfSSL | 7:481bce714567 | 109 | typedef struct WOLFSSL_MEM_STATS WOLFSSL_MEM_STATS; |
wolfSSL | 7:481bce714567 | 110 | typedef struct WOLFSSL_MEM_CONN_STATS WOLFSSL_MEM_CONN_STATS; |
wolfSSL | 7:481bce714567 | 111 | #endif |
wolfSSL | 7:481bce714567 | 112 | |
wolfSSL | 7:481bce714567 | 113 | struct WOLFSSL_MEM_CONN_STATS { |
wolfSSL | 7:481bce714567 | 114 | word32 peakMem; /* peak memory usage */ |
wolfSSL | 7:481bce714567 | 115 | word32 curMem; /* current memory usage */ |
wolfSSL | 7:481bce714567 | 116 | word32 peakAlloc; /* peak memory allocations */ |
wolfSSL | 7:481bce714567 | 117 | word32 curAlloc; /* current memory allocations */ |
wolfSSL | 7:481bce714567 | 118 | word32 totalAlloc;/* total memory allocations for lifetime */ |
wolfSSL | 7:481bce714567 | 119 | word32 totalFr; /* total frees for lifetime */ |
wolfSSL | 7:481bce714567 | 120 | }; |
wolfSSL | 7:481bce714567 | 121 | |
wolfSSL | 7:481bce714567 | 122 | struct WOLFSSL_MEM_STATS { |
wolfSSL | 7:481bce714567 | 123 | word32 curAlloc; /* current memory allocations */ |
wolfSSL | 7:481bce714567 | 124 | word32 totalAlloc;/* total memory allocations for lifetime */ |
wolfSSL | 7:481bce714567 | 125 | word32 totalFr; /* total frees for lifetime */ |
wolfSSL | 7:481bce714567 | 126 | word32 totalUse; /* total amount of memory used in blocks */ |
wolfSSL | 7:481bce714567 | 127 | word32 avaIO; /* available IO specific pools */ |
wolfSSL | 7:481bce714567 | 128 | word32 maxHa; /* max number of concurent handshakes allowed */ |
wolfSSL | 7:481bce714567 | 129 | word32 maxIO; /* max number of concurent IO connections allowed */ |
wolfSSL | 7:481bce714567 | 130 | word32 blockSz[WOLFMEM_MAX_BUCKETS]; /* block sizes in stacks */ |
wolfSSL | 7:481bce714567 | 131 | word32 avaBlock[WOLFMEM_MAX_BUCKETS];/* ava block sizes */ |
wolfSSL | 7:481bce714567 | 132 | word32 usedBlock[WOLFMEM_MAX_BUCKETS]; |
wolfSSL | 7:481bce714567 | 133 | int flag; /* flag used */ |
wolfSSL | 7:481bce714567 | 134 | }; |
wolfSSL | 7:481bce714567 | 135 | |
wolfSSL | 7:481bce714567 | 136 | typedef struct wc_Memory wc_Memory; /* internal structure for mem bucket */ |
wolfSSL | 7:481bce714567 | 137 | typedef struct WOLFSSL_HEAP { |
wolfSSL | 7:481bce714567 | 138 | wc_Memory* ava[WOLFMEM_MAX_BUCKETS]; |
wolfSSL | 7:481bce714567 | 139 | wc_Memory* io; /* list of buffers to use for IO */ |
wolfSSL | 7:481bce714567 | 140 | word32 maxHa; /* max concurent handshakes */ |
wolfSSL | 7:481bce714567 | 141 | word32 curHa; |
wolfSSL | 7:481bce714567 | 142 | word32 maxIO; /* max concurrent IO connections */ |
wolfSSL | 7:481bce714567 | 143 | word32 curIO; |
wolfSSL | 7:481bce714567 | 144 | word32 sizeList[WOLFMEM_MAX_BUCKETS];/* memory sizes in ava list */ |
wolfSSL | 7:481bce714567 | 145 | word32 distList[WOLFMEM_MAX_BUCKETS];/* general distribution */ |
wolfSSL | 7:481bce714567 | 146 | word32 inUse; /* amount of memory currently in use */ |
wolfSSL | 7:481bce714567 | 147 | word32 ioUse; |
wolfSSL | 7:481bce714567 | 148 | word32 alloc; /* total number of allocs */ |
wolfSSL | 7:481bce714567 | 149 | word32 frAlc; /* total number of frees */ |
wolfSSL | 7:481bce714567 | 150 | int flag; |
wolfSSL | 7:481bce714567 | 151 | wolfSSL_Mutex memory_mutex; |
wolfSSL | 7:481bce714567 | 152 | } WOLFSSL_HEAP; |
wolfSSL | 7:481bce714567 | 153 | |
wolfSSL | 7:481bce714567 | 154 | /* structure passed into XMALLOC as heap hint |
wolfSSL | 7:481bce714567 | 155 | * having this abstraction allows tracking statistics of individual ssl's |
wolfSSL | 7:481bce714567 | 156 | */ |
wolfSSL | 7:481bce714567 | 157 | typedef struct WOLFSSL_HEAP_HINT { |
wolfSSL | 7:481bce714567 | 158 | WOLFSSL_HEAP* memory; |
wolfSSL | 7:481bce714567 | 159 | WOLFSSL_MEM_CONN_STATS* stats; /* hold individual connection stats */ |
wolfSSL | 7:481bce714567 | 160 | wc_Memory* outBuf; /* set if using fixed io buffers */ |
wolfSSL | 7:481bce714567 | 161 | wc_Memory* inBuf; |
wolfSSL | 7:481bce714567 | 162 | byte haFlag; /* flag used for checking handshake count */ |
wolfSSL | 7:481bce714567 | 163 | } WOLFSSL_HEAP_HINT; |
wolfSSL | 7:481bce714567 | 164 | |
wolfSSL | 7:481bce714567 | 165 | WOLFSSL_API int wc_LoadStaticMemory(WOLFSSL_HEAP_HINT** pHint, |
wolfSSL | 7:481bce714567 | 166 | unsigned char* buf, unsigned int sz, int flag, int max); |
wolfSSL | 7:481bce714567 | 167 | |
wolfSSL | 7:481bce714567 | 168 | WOLFSSL_LOCAL int wolfSSL_init_memory_heap(WOLFSSL_HEAP* heap); |
wolfSSL | 7:481bce714567 | 169 | WOLFSSL_LOCAL int wolfSSL_load_static_memory(byte* buffer, word32 sz, |
wolfSSL | 7:481bce714567 | 170 | int flag, WOLFSSL_HEAP* heap); |
wolfSSL | 7:481bce714567 | 171 | WOLFSSL_LOCAL int wolfSSL_GetMemStats(WOLFSSL_HEAP* heap, |
wolfSSL | 7:481bce714567 | 172 | WOLFSSL_MEM_STATS* stats); |
wolfSSL | 7:481bce714567 | 173 | WOLFSSL_LOCAL int SetFixedIO(WOLFSSL_HEAP* heap, wc_Memory** io); |
wolfSSL | 7:481bce714567 | 174 | WOLFSSL_LOCAL int FreeFixedIO(WOLFSSL_HEAP* heap, wc_Memory** io); |
wolfSSL | 7:481bce714567 | 175 | |
wolfSSL | 7:481bce714567 | 176 | WOLFSSL_API int wolfSSL_StaticBufferSz(byte* buffer, word32 sz, int flag); |
wolfSSL | 7:481bce714567 | 177 | WOLFSSL_API int wolfSSL_MemoryPaddingSz(void); |
wolfSSL | 7:481bce714567 | 178 | #endif /* WOLFSSL_STATIC_MEMORY */ |
wolfSSL | 7:481bce714567 | 179 | |
wolfSSL | 7:481bce714567 | 180 | #ifdef __cplusplus |
wolfSSL | 7:481bce714567 | 181 | } /* extern "C" */ |
wolfSSL | 7:481bce714567 | 182 | #endif |
wolfSSL | 7:481bce714567 | 183 | |
wolfSSL | 7:481bce714567 | 184 | #endif /* WOLFSSL_MEMORY_H */ |
wolfSSL | 7:481bce714567 | 185 | |
wolfSSL | 7:481bce714567 | 186 |