3 * Copyright (C) 2006-2014 wolfSSL Inc.
5 * This file is part of CyaSSL.
7 * CyaSSL 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 * CyaSSL 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 <cyassl/ctaocrypt/settings.h>
28 #ifdef USE_CYASSL_MEMORY
30 #include <cyassl/ctaocrypt/memory.h>
31 #include <cyassl/ctaocrypt/error-crypt.h>
33 #ifdef CYASSL_MALLOC_CHECK
37 /* Set these to default values initially. */
38 static CyaSSL_Malloc_cb malloc_function = 0;
39 static CyaSSL_Free_cb free_function = 0;
40 static CyaSSL_Realloc_cb realloc_function = 0;
42 int CyaSSL_SetAllocators(CyaSSL_Malloc_cb mf,
59 realloc_function = rf;
67 void* CyaSSL_Malloc(size_t size)
72 res = malloc_function(size);
76 #ifdef CYASSL_MALLOC_CHECK
78 puts("CyaSSL_malloc failed");
84 void CyaSSL_Free(void *ptr)
92 void* CyaSSL_Realloc(void *ptr, size_t size)
97 res = realloc_function(ptr, size);
99 res = realloc(ptr, size);
104 #endif /* USE_CYASSL_MEMORY */
109 /* Example for user io pool, shared build may need definitions in lib proper */
111 #include <cyassl/ctaocrypt/types.h>
114 #ifndef HAVE_THREAD_LS
115 #error "Oops, simple I/O pool example needs thread local storage"
119 /* allow simple per thread in and out pools */
120 /* use 17k size sense max record size is 16k plus overhead */
121 static THREAD_LS_T byte pool_in[17*1024];
122 static THREAD_LS_T byte pool_out[17*1024];
125 void* XMALLOC(size_t n, void* heap, int type)
129 if (type == DYNAMIC_TYPE_IN_BUFFER) {
130 if (n < sizeof(pool_in))
136 if (type == DYNAMIC_TYPE_OUT_BUFFER) {
137 if (n < sizeof(pool_out))
146 void* XREALLOC(void *p, size_t n, void* heap, int type)
150 if (type == DYNAMIC_TYPE_IN_BUFFER) {
151 if (n < sizeof(pool_in))
157 if (type == DYNAMIC_TYPE_OUT_BUFFER) {
158 if (n < sizeof(pool_out))
164 return realloc(p, n);
168 /* unit api calls, let's make sure visisble with CYASSL_API */
169 CYASSL_API void XFREE(void *p, void* heap, int type)
173 if (type == DYNAMIC_TYPE_IN_BUFFER)
174 return; /* do nothing, static pool */
176 if (type == DYNAMIC_TYPE_OUT_BUFFER)
177 return; /* do nothing, static pool */
182 #endif /* HAVE_IO_POOL */