ssh lib

Dependents:   OS

Committer:
sPymbed
Date:
Mon Nov 25 14:23:49 2019 +0000
Revision:
1:e4ea39eba2fb
Parent:
0:1387ff3eed4a
improved

Who changed what in which revision?

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