Xuyi Wang / wolfcrypt

Dependents:   OS

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-2017 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     \file wolfssl/wolfcrypt/memory.h
00027 */
00028 
00029 #ifndef WOLFSSL_MEMORY_H
00030 #define WOLFSSL_MEMORY_H
00031 
00032 #include <stdlib.h>
00033 #include <wolfcrypt/types.h>
00034 
00035 #ifdef __cplusplus
00036     extern "C" {
00037 #endif
00038 
00039 #ifdef WOLFSSL_STATIC_MEMORY
00040     #ifdef WOLFSSL_DEBUG_MEMORY
00041         typedef void *(*wolfSSL_Malloc_cb)(size_t size, void* heap, int type, const char* func, unsigned int line);
00042         typedef void (*wolfSSL_Free_cb)(void *ptr, void* heap, int type, const char* func, unsigned int line);
00043         typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size, void* heap, int type, const char* func, unsigned int line);
00044         WOLFSSL_API void* wolfSSL_Malloc(size_t size, void* heap, int type, const char* func, unsigned int line);
00045         WOLFSSL_API void  wolfSSL_Free(void *ptr, void* heap, int type, const char* func, unsigned int line);
00046         WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type, const char* func, unsigned int line);
00047     #else
00048         typedef void *(*wolfSSL_Malloc_cb)(size_t size, void* heap, int type);
00049         typedef void (*wolfSSL_Free_cb)(void *ptr, void* heap, int type);
00050         typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size, void* heap, int type);
00051         WOLFSSL_API void* wolfSSL_Malloc(size_t size, void* heap, int type);
00052         WOLFSSL_API void  wolfSSL_Free(void *ptr, void* heap, int type);
00053         WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type);
00054     #endif /* WOLFSSL_DEBUG_MEMORY */
00055 #else
00056     #ifdef WOLFSSL_DEBUG_MEMORY
00057         typedef void *(*wolfSSL_Malloc_cb)(size_t size, const char* func, unsigned int line);
00058         typedef void (*wolfSSL_Free_cb)(void *ptr, const char* func, unsigned int line);
00059         typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size, const char* func, unsigned int line);
00060 
00061         /* Public in case user app wants to use XMALLOC/XFREE */
00062         WOLFSSL_API void* wolfSSL_Malloc(size_t size, const char* func, unsigned int line);
00063         WOLFSSL_API void  wolfSSL_Free(void *ptr, const char* func, unsigned int line);
00064         WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size, const char* func, unsigned int line);
00065     #else
00066         typedef void *(*wolfSSL_Malloc_cb)(size_t size);
00067         typedef void (*wolfSSL_Free_cb)(void *ptr);
00068         typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size);
00069         /* Public in case user app wants to use XMALLOC/XFREE */
00070         WOLFSSL_API void* wolfSSL_Malloc(size_t size);
00071         WOLFSSL_API void  wolfSSL_Free(void *ptr);
00072         WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size);
00073     #endif /* WOLFSSL_DEBUG_MEMORY */
00074 #endif /* WOLFSSL_STATIC_MEMORY */
00075 
00076 /* Public get/set functions */
00077 WOLFSSL_API int wolfSSL_SetAllocators(wolfSSL_Malloc_cb,
00078                                       wolfSSL_Free_cb,
00079                                       wolfSSL_Realloc_cb);
00080 WOLFSSL_API int wolfSSL_ResetAllocators(void);
00081 WOLFSSL_API int wolfSSL_GetAllocators(wolfSSL_Malloc_cb*,
00082                                       wolfSSL_Free_cb*,
00083                                       wolfSSL_Realloc_cb*);
00084 
00085 #ifdef WOLFSSL_STATIC_MEMORY
00086     #define WOLFSSL_STATIC_TIMEOUT 1
00087     #ifndef WOLFSSL_STATIC_ALIGN
00088         #define WOLFSSL_STATIC_ALIGN 16
00089     #endif
00090     #ifndef WOLFMEM_MAX_BUCKETS
00091         #define WOLFMEM_MAX_BUCKETS  9
00092     #endif
00093     #define WOLFMEM_DEF_BUCKETS  9     /* number of default memory blocks */
00094     #ifndef WOLFMEM_IO_SZ
00095         #define WOLFMEM_IO_SZ        16992 /* 16 byte aligned */
00096     #endif
00097     #ifndef WOLFMEM_BUCKETS
00098         /* default size of chunks of memory to seperate into
00099          * having session certs enabled makes a 21k SSL struct */
00100         #ifndef SESSION_CERTS
00101             #define WOLFMEM_BUCKETS 64,128,256,512,1024,2432,3456,4544,16128
00102         #else
00103             #define WOLFMEM_BUCKETS 64,128,256,512,1024,2432,3456,4544,21920
00104         #endif
00105     #endif
00106     #ifndef WOLFMEM_DIST
00107         #define WOLFMEM_DIST    8,4,4,12,4,5,8,1,1
00108     #endif
00109 
00110     /* flags for loading static memory (one hot bit) */
00111     #define WOLFMEM_GENERAL       0x01
00112     #define WOLFMEM_IO_POOL       0x02
00113     #define WOLFMEM_IO_POOL_FIXED 0x04
00114     #define WOLFMEM_TRACK_STATS   0x08
00115 
00116     #ifndef WOLFSSL_MEM_GUARD
00117     #define WOLFSSL_MEM_GUARD
00118         typedef struct WOLFSSL_MEM_STATS      WOLFSSL_MEM_STATS;
00119         typedef struct WOLFSSL_MEM_CONN_STATS WOLFSSL_MEM_CONN_STATS;
00120     #endif
00121 
00122     struct WOLFSSL_MEM_CONN_STATS {
00123         word32 peakMem;   /* peak memory usage    */
00124         word32 curMem;    /* current memory usage */
00125         word32 peakAlloc; /* peak memory allocations */
00126         word32 curAlloc;  /* current memory allocations */
00127         word32 totalAlloc;/* total memory allocations for lifetime */
00128         word32 totalFr;   /* total frees for lifetime */
00129     };
00130 
00131     struct WOLFSSL_MEM_STATS {
00132         word32 curAlloc;  /* current memory allocations */
00133         word32 totalAlloc;/* total memory allocations for lifetime */
00134         word32 totalFr;   /* total frees for lifetime */
00135         word32 totalUse;  /* total amount of memory used in blocks */
00136         word32 avaIO;     /* available IO specific pools */
00137         word32 maxHa;     /* max number of concurent handshakes allowed */
00138         word32 maxIO;     /* max number of concurent IO connections allowed */
00139         word32 blockSz[WOLFMEM_MAX_BUCKETS]; /* block sizes in stacks */
00140         word32 avaBlock[WOLFMEM_MAX_BUCKETS];/* ava block sizes */
00141         word32 usedBlock[WOLFMEM_MAX_BUCKETS];
00142         int    flag; /* flag used */
00143     };
00144 
00145     typedef struct wc_Memory wc_Memory; /* internal structure for mem bucket */
00146     typedef struct WOLFSSL_HEAP {
00147         wc_Memory* ava[WOLFMEM_MAX_BUCKETS];
00148         wc_Memory* io;                  /* list of buffers to use for IO */
00149         word32     maxHa;               /* max concurent handshakes */
00150         word32     curHa;
00151         word32     maxIO;               /* max concurrent IO connections */
00152         word32     curIO;
00153         word32     sizeList[WOLFMEM_MAX_BUCKETS];/* memory sizes in ava list */
00154         word32     distList[WOLFMEM_MAX_BUCKETS];/* general distribution */
00155         word32     inUse; /* amount of memory currently in use */
00156         word32     ioUse;
00157         word32     alloc; /* total number of allocs */
00158         word32     frAlc; /* total number of frees  */
00159         int        flag;
00160         wolfSSL_Mutex memory_mutex;
00161     } WOLFSSL_HEAP;
00162 
00163     /* structure passed into XMALLOC as heap hint
00164      * having this abstraction allows tracking statistics of individual ssl's
00165      */
00166     typedef struct WOLFSSL_HEAP_HINT {
00167         WOLFSSL_HEAP*           memory;
00168         WOLFSSL_MEM_CONN_STATS* stats;  /* hold individual connection stats */
00169         wc_Memory*  outBuf; /* set if using fixed io buffers */
00170         wc_Memory*  inBuf;
00171         byte        haFlag; /* flag used for checking handshake count */
00172     } WOLFSSL_HEAP_HINT;
00173 
00174     WOLFSSL_API int wc_LoadStaticMemory(WOLFSSL_HEAP_HINT** pHint,
00175             unsigned char* buf, unsigned int sz, int flag, int max);
00176 
00177     WOLFSSL_LOCAL int wolfSSL_init_memory_heap(WOLFSSL_HEAP* heap);
00178     WOLFSSL_LOCAL int wolfSSL_load_static_memory(byte* buffer, word32 sz,
00179                                                   int flag, WOLFSSL_HEAP* heap);
00180     WOLFSSL_LOCAL int wolfSSL_GetMemStats(WOLFSSL_HEAP* heap,
00181                                                       WOLFSSL_MEM_STATS* stats);
00182     WOLFSSL_LOCAL int SetFixedIO(WOLFSSL_HEAP* heap, wc_Memory** io);
00183     WOLFSSL_LOCAL int FreeFixedIO(WOLFSSL_HEAP* heap, wc_Memory** io);
00184 
00185     WOLFSSL_API int wolfSSL_StaticBufferSz(byte* buffer, word32 sz, int flag);
00186     WOLFSSL_API int wolfSSL_MemoryPaddingSz(void);
00187 #endif /* WOLFSSL_STATIC_MEMORY */
00188 
00189 #ifdef __cplusplus
00190     }  /* extern "C" */
00191 #endif
00192 
00193 #endif /* WOLFSSL_MEMORY_H */
00194 
00195