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