wolf SSL / wolfSSL-TLS13-Beta

Fork of wolfSSL by wolf SSL

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers memory.h Source File

memory.h

00001 /* memory.h
00002  *
00003  * Copyright (C) 2006-2016 wolfSSL Inc.
00004  *
00005  * This file is part of wolfSSL.
00006  *
00007  * wolfSSL is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * wolfSSL is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
00020  */
00021 
00022 
00023 /* submitted by eof */
00024 
00025 
00026 #ifndef WOLFSSL_MEMORY_H
00027 #define WOLFSSL_MEMORY_H
00028 
00029 #include <stdlib.h>
00030 #include <wolfssl/wolfcrypt/types.h>
00031 
00032 #ifdef __cplusplus
00033     extern "C" {
00034 #endif
00035 
00036 #ifdef WOLFSSL_STATIC_MEMORY
00037     #ifdef WOLFSSL_DEBUG_MEMORY
00038         typedef void *(*wolfSSL_Malloc_cb)(size_t size, void* heap, int type, const char* func, unsigned int line);
00039         typedef void (*wolfSSL_Free_cb)(void *ptr, void* heap, int type, const char* func, unsigned int line);
00040         typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size, void* heap, int type, const char* func, unsigned int line);
00041         WOLFSSL_API void* wolfSSL_Malloc(size_t size, void* heap, int type, const char* func, unsigned int line);
00042         WOLFSSL_API void  wolfSSL_Free(void *ptr, void* heap, int type, const char* func, unsigned int line);
00043         WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type, const char* func, unsigned int line);
00044     #else
00045         typedef void *(*wolfSSL_Malloc_cb)(size_t size, void* heap, int type);
00046         typedef void (*wolfSSL_Free_cb)(void *ptr, void* heap, int type);
00047         typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size, void* heap, int type);
00048         WOLFSSL_API void* wolfSSL_Malloc(size_t size, void* heap, int type);
00049         WOLFSSL_API void  wolfSSL_Free(void *ptr, void* heap, int type);
00050         WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type);
00051     #endif /* WOLFSSL_DEBUG_MEMORY */
00052 #else
00053     #ifdef WOLFSSL_DEBUG_MEMORY
00054         typedef void *(*wolfSSL_Malloc_cb)(size_t size, const char* func, unsigned int line);
00055         typedef void (*wolfSSL_Free_cb)(void *ptr, const char* func, unsigned int line);
00056         typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size, const char* func, unsigned int line);
00057 
00058         /* Public in case user app wants to use XMALLOC/XFREE */
00059         WOLFSSL_API void* wolfSSL_Malloc(size_t size, const char* func, unsigned int line);
00060         WOLFSSL_API void  wolfSSL_Free(void *ptr, const char* func, unsigned int line);
00061         WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size, const char* func, unsigned int line);
00062     #else
00063         typedef void *(*wolfSSL_Malloc_cb)(size_t size);
00064         typedef void (*wolfSSL_Free_cb)(void *ptr);
00065         typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size);
00066         /* Public in case user app wants to use XMALLOC/XFREE */
00067         WOLFSSL_API void* wolfSSL_Malloc(size_t size);
00068         WOLFSSL_API void  wolfSSL_Free(void *ptr);
00069         WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size);
00070     #endif /* WOLFSSL_DEBUG_MEMORY */
00071 #endif /* WOLFSSL_STATIC_MEMORY */
00072 
00073 /* Public get/set functions */
00074 WOLFSSL_API int wolfSSL_SetAllocators(wolfSSL_Malloc_cb,
00075                                       wolfSSL_Free_cb,
00076                                       wolfSSL_Realloc_cb);
00077 
00078 WOLFSSL_API int wolfSSL_GetAllocators(wolfSSL_Malloc_cb*,
00079                                       wolfSSL_Free_cb*,
00080                                       wolfSSL_Realloc_cb*);
00081 
00082 #ifdef WOLFSSL_STATIC_MEMORY
00083     #define WOLFSSL_STATIC_TIMEOUT 1
00084     #ifndef WOLFSSL_STATIC_ALIGN
00085         #define WOLFSSL_STATIC_ALIGN 16
00086     #endif
00087     #ifndef WOLFMEM_MAX_BUCKETS
00088         #define WOLFMEM_MAX_BUCKETS  9
00089     #endif
00090     #define WOLFMEM_DEF_BUCKETS  9     /* number of default memory blocks */
00091     #define WOLFMEM_IO_SZ        16992 /* 16 byte aligned */
00092     #ifndef WOLFMEM_BUCKETS
00093         /* default size of chunks of memory to seperate into
00094          * having session certs enabled makes a 21k SSL struct */
00095         #ifndef SESSION_CERTS
00096             #define WOLFMEM_BUCKETS 64,128,256,512,1024,2432,3456,4544,16128
00097         #else
00098             #define WOLFMEM_BUCKETS 64,128,256,512,1024,2432,3456,4544,21056
00099         #endif
00100     #endif
00101     #ifndef WOLFMEM_DIST
00102         #define WOLFMEM_DIST    8,4,4,12,4,5,8,1,1
00103     #endif
00104 
00105     /* flags for loading static memory (one hot bit) */
00106     #define WOLFMEM_GENERAL       0x01
00107     #define WOLFMEM_IO_POOL       0x02
00108     #define WOLFMEM_IO_POOL_FIXED 0x04
00109     #define WOLFMEM_TRACK_STATS   0x08
00110 
00111     #ifndef WOLFSSL_MEM_GUARD
00112     #define WOLFSSL_MEM_GUARD
00113         typedef struct WOLFSSL_MEM_STATS      WOLFSSL_MEM_STATS;
00114         typedef struct WOLFSSL_MEM_CONN_STATS WOLFSSL_MEM_CONN_STATS;
00115     #endif
00116 
00117     struct WOLFSSL_MEM_CONN_STATS {
00118         word32 peakMem;   /* peak memory usage    */
00119         word32 curMem;    /* current memory usage */
00120         word32 peakAlloc; /* peak memory allocations */
00121         word32 curAlloc;  /* current memory allocations */
00122         word32 totalAlloc;/* total memory allocations for lifetime */
00123         word32 totalFr;   /* total frees for lifetime */
00124     };
00125 
00126     struct WOLFSSL_MEM_STATS {
00127         word32 curAlloc;  /* current memory allocations */
00128         word32 totalAlloc;/* total memory allocations for lifetime */
00129         word32 totalFr;   /* total frees for lifetime */
00130         word32 totalUse;  /* total amount of memory used in blocks */
00131         word32 avaIO;     /* available IO specific pools */
00132         word32 maxHa;     /* max number of concurent handshakes allowed */
00133         word32 maxIO;     /* max number of concurent IO connections allowed */
00134         word32 blockSz[WOLFMEM_MAX_BUCKETS]; /* block sizes in stacks */
00135         word32 avaBlock[WOLFMEM_MAX_BUCKETS];/* ava block sizes */
00136         word32 usedBlock[WOLFMEM_MAX_BUCKETS];
00137         int    flag; /* flag used */
00138     };
00139 
00140     typedef struct wc_Memory wc_Memory; /* internal structure for mem bucket */
00141     typedef struct WOLFSSL_HEAP {
00142         wc_Memory* ava[WOLFMEM_MAX_BUCKETS];
00143         wc_Memory* io;                  /* list of buffers to use for IO */
00144         word32     maxHa;               /* max concurent handshakes */
00145         word32     curHa;
00146         word32     maxIO;               /* max concurrent IO connections */
00147         word32     curIO;
00148         word32     sizeList[WOLFMEM_MAX_BUCKETS];/* memory sizes in ava list */
00149         word32     distList[WOLFMEM_MAX_BUCKETS];/* general distribution */
00150         word32     inUse; /* amount of memory currently in use */
00151         word32     ioUse;
00152         word32     alloc; /* total number of allocs */
00153         word32     frAlc; /* total number of frees  */
00154         int        flag;
00155         wolfSSL_Mutex memory_mutex;
00156     } WOLFSSL_HEAP;
00157 
00158     /* structure passed into XMALLOC as heap hint
00159      * having this abstraction allows tracking statistics of individual ssl's
00160      */
00161     typedef struct WOLFSSL_HEAP_HINT {
00162         WOLFSSL_HEAP*           memory;
00163         WOLFSSL_MEM_CONN_STATS* stats;  /* hold individual connection stats */
00164         wc_Memory*  outBuf; /* set if using fixed io buffers */
00165         wc_Memory*  inBuf;
00166         byte        haFlag; /* flag used for checking handshake count */
00167     } WOLFSSL_HEAP_HINT;
00168 
00169     WOLFSSL_API int wc_LoadStaticMemory(WOLFSSL_HEAP_HINT** pHint,
00170             unsigned char* buf, unsigned int sz, int flag, int max);
00171 
00172     WOLFSSL_LOCAL int wolfSSL_init_memory_heap(WOLFSSL_HEAP* heap);
00173     WOLFSSL_LOCAL int wolfSSL_load_static_memory(byte* buffer, word32 sz,
00174                                                   int flag, WOLFSSL_HEAP* heap);
00175     WOLFSSL_LOCAL int wolfSSL_GetMemStats(WOLFSSL_HEAP* heap,
00176                                                       WOLFSSL_MEM_STATS* stats);
00177     WOLFSSL_LOCAL int SetFixedIO(WOLFSSL_HEAP* heap, wc_Memory** io);
00178     WOLFSSL_LOCAL int FreeFixedIO(WOLFSSL_HEAP* heap, wc_Memory** io);
00179 
00180     WOLFSSL_API int wolfSSL_StaticBufferSz(byte* buffer, word32 sz, int flag);
00181     WOLFSSL_API int wolfSSL_MemoryPaddingSz(void);
00182 #endif /* WOLFSSL_STATIC_MEMORY */
00183 
00184 #ifdef __cplusplus
00185     }  /* extern "C" */
00186 #endif
00187 
00188 #endif /* WOLFSSL_MEMORY_H */
00189 
00190