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