3 * Copyright (C) 2006-2015 wolfSSL Inc.
5 * This file is part of wolfSSL. (formerly known as CyaSSL)
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.
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.
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-1301, USA
26 #include <wolfssl/wolfcrypt/settings.h>
28 /* check old macros @wc_fips */
29 #if defined(USE_CYASSL_MEMORY) && !defined(USE_WOLFSSL_MEMORY)
30 #define USE_WOLFSSL_MEMORY
32 #if defined(CYASSL_MALLOC_CHECK) && !defined(WOLFSSL_MALLOC_CHECK)
33 #define WOLFSSL_MALLOC_CHECK
36 #ifdef USE_WOLFSSL_MEMORY
38 #include <wolfssl/wolfcrypt/memory.h>
39 #include <wolfssl/wolfcrypt/error-crypt.h>
41 #ifdef WOLFSSL_MALLOC_CHECK
45 /* Set these to default values initially. */
46 static wolfSSL_Malloc_cb malloc_function = 0;
47 static wolfSSL_Free_cb free_function = 0;
48 static wolfSSL_Realloc_cb realloc_function = 0;
50 int wolfSSL_SetAllocators(wolfSSL_Malloc_cb mf,
52 wolfSSL_Realloc_cb rf)
67 realloc_function = rf;
75 void* wolfSSL_Malloc(size_t size)
80 res = malloc_function(size);
84 #ifdef WOLFSSL_MALLOC_CHECK
86 puts("wolfSSL_malloc failed");
92 void wolfSSL_Free(void *ptr)
100 void* wolfSSL_Realloc(void *ptr, size_t size)
104 if (realloc_function)
105 res = realloc_function(ptr, size);
107 res = realloc(ptr, size);
112 #endif /* USE_WOLFSSL_MEMORY */
117 /* Example for user io pool, shared build may need definitions in lib proper */
119 #include <wolfssl/wolfcrypt/types.h>
122 #ifndef HAVE_THREAD_LS
123 #error "Oops, simple I/O pool example needs thread local storage"
127 /* allow simple per thread in and out pools */
128 /* use 17k size sense max record size is 16k plus overhead */
129 static THREAD_LS_T byte pool_in[17*1024];
130 static THREAD_LS_T byte pool_out[17*1024];
133 void* XMALLOC(size_t n, void* heap, int type)
137 if (type == DYNAMIC_TYPE_IN_BUFFER) {
138 if (n < sizeof(pool_in))
144 if (type == DYNAMIC_TYPE_OUT_BUFFER) {
145 if (n < sizeof(pool_out))
154 void* XREALLOC(void *p, size_t n, void* heap, int type)
158 if (type == DYNAMIC_TYPE_IN_BUFFER) {
159 if (n < sizeof(pool_in))
165 if (type == DYNAMIC_TYPE_OUT_BUFFER) {
166 if (n < sizeof(pool_out))
172 return realloc(p, n);
176 /* unit api calls, let's make sure visible with WOLFSSL_API */
177 WOLFSSL_API void XFREE(void *p, void* heap, int type)
181 if (type == DYNAMIC_TYPE_IN_BUFFER)
182 return; /* do nothing, static pool */
184 if (type == DYNAMIC_TYPE_OUT_BUFFER)
185 return; /* do nothing, static pool */
190 #endif /* HAVE_IO_POOL */