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