Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
memory.c
00001 /* memory.c 00002 * 00003 * Copyright (C) 2006-2013 wolfSSL Inc. 00004 * 00005 * This file is part of CyaSSL. 00006 * 00007 * CyaSSL 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 * CyaSSL 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA 00020 */ 00021 00022 #ifdef HAVE_CONFIG_H 00023 #include <config.h> 00024 #endif 00025 00026 #include <cyassl/ctaocrypt/settings.h> 00027 00028 #ifdef USE_CYASSL_MEMORY 00029 00030 #include <cyassl/ctaocrypt/memory.h> 00031 #include <cyassl/ctaocrypt/error-crypt.h> 00032 00033 #ifdef CYASSL_MALLOC_CHECK 00034 #include <stdio.h> 00035 #endif 00036 00037 /* Set these to default values initially. */ 00038 static CyaSSL_Malloc_cb malloc_function = 0; 00039 static CyaSSL_Free_cb free_function = 0; 00040 static CyaSSL_Realloc_cb realloc_function = 0; 00041 00042 int CyaSSL_SetAllocators(CyaSSL_Malloc_cb mf, 00043 CyaSSL_Free_cb ff, 00044 CyaSSL_Realloc_cb rf) 00045 { 00046 int res = 0; 00047 00048 if (mf) 00049 malloc_function = mf; 00050 else 00051 res = BAD_FUNC_ARG; 00052 00053 if (ff) 00054 free_function = ff; 00055 else 00056 res = BAD_FUNC_ARG; 00057 00058 if (rf) 00059 realloc_function = rf; 00060 else 00061 res = BAD_FUNC_ARG; 00062 00063 return res; 00064 } 00065 00066 00067 void* CyaSSL_Malloc(size_t size) 00068 { 00069 void* res = 0; 00070 00071 if (malloc_function) 00072 res = malloc_function(size); 00073 else 00074 res = malloc(size); 00075 00076 #ifdef CYASSL_MALLOC_CHECK 00077 if (res == NULL) 00078 puts("CyaSSL_malloc failed"); 00079 #endif 00080 00081 return res; 00082 } 00083 00084 void CyaSSL_Free(void *ptr) 00085 { 00086 if (free_function) 00087 free_function(ptr); 00088 else 00089 free(ptr); 00090 } 00091 00092 void* CyaSSL_Realloc(void *ptr, size_t size) 00093 { 00094 void* res = 0; 00095 00096 if (realloc_function) 00097 res = realloc_function(ptr, size); 00098 else 00099 res = realloc(ptr, size); 00100 00101 return res; 00102 } 00103 00104 #endif /* USE_CYASSL_MEMORY */ 00105 00106 00107 #ifdef HAVE_IO_POOL 00108 00109 /* Example for user io pool, shared build may need definitions in lib proper */ 00110 00111 #include <cyassl/ctaocrypt/types.h> 00112 #include <stdlib.h> 00113 00114 #ifndef HAVE_THREAD_LS 00115 #error "Oops, simple I/O pool example needs thread local storage" 00116 #endif 00117 00118 00119 /* allow simple per thread in and out pools */ 00120 /* use 17k size sense max record size is 16k plus overhead */ 00121 static THREAD_LS_T byte pool_in[17*1024]; 00122 static THREAD_LS_T byte pool_out[17*1024]; 00123 00124 00125 void* XMALLOC(size_t n, void* heap, int type) 00126 { 00127 (void)heap; 00128 00129 if (type == DYNAMIC_TYPE_IN_BUFFER) { 00130 if (n < sizeof(pool_in)) 00131 return pool_in; 00132 else 00133 return NULL; 00134 } 00135 00136 if (type == DYNAMIC_TYPE_OUT_BUFFER) { 00137 if (n < sizeof(pool_out)) 00138 return pool_out; 00139 else 00140 return NULL; 00141 } 00142 00143 return malloc(n); 00144 } 00145 00146 void* XREALLOC(void *p, size_t n, void* heap, int type) 00147 { 00148 (void)heap; 00149 00150 if (type == DYNAMIC_TYPE_IN_BUFFER) { 00151 if (n < sizeof(pool_in)) 00152 return pool_in; 00153 else 00154 return NULL; 00155 } 00156 00157 if (type == DYNAMIC_TYPE_OUT_BUFFER) { 00158 if (n < sizeof(pool_out)) 00159 return pool_out; 00160 else 00161 return NULL; 00162 } 00163 00164 return realloc(p, n); 00165 } 00166 00167 00168 /* unit api calls, let's make sure visisble with CYASSL_API */ 00169 CYASSL_API void XFREE(void *p, void* heap, int type) 00170 { 00171 (void)heap; 00172 00173 if (type == DYNAMIC_TYPE_IN_BUFFER) 00174 return; /* do nothing, static pool */ 00175 00176 if (type == DYNAMIC_TYPE_OUT_BUFFER) 00177 return; /* do nothing, static pool */ 00178 00179 free(p); 00180 } 00181 00182 #endif /* HAVE_IO_POOL */ 00183
Generated on Tue Jul 12 2022 20:12:51 by
