Renesas / SecureDweet
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers memory.c Source File

memory.c

00001 /* memory.c
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 #ifdef HAVE_CONFIG_H
00024     #include <config.h>
00025 #endif
00026 
00027 #include <wolfssl/wolfcrypt/settings.h>
00028 
00029 /* check old macros @wc_fips */
00030 #if defined(USE_CYASSL_MEMORY) && !defined(USE_WOLFSSL_MEMORY)
00031     #define USE_WOLFSSL_MEMORY
00032 #endif
00033 #if defined(CYASSL_MALLOC_CHECK) && !defined(WOLFSSL_MALLOC_CHECK)
00034     #define WOLFSSL_MALLOC_CHECK
00035 #endif
00036 
00037 #ifdef USE_WOLFSSL_MEMORY
00038 
00039 #include <wolfssl/wolfcrypt/memory.h>
00040 #include <wolfssl/wolfcrypt/error-crypt.h>
00041 
00042 #ifdef WOLFSSL_MALLOC_CHECK
00043     #include <stdio.h>
00044 #endif
00045 
00046 /* Set these to default values initially. */
00047 static wolfSSL_Malloc_cb  malloc_function = 0;
00048 static wolfSSL_Free_cb    free_function = 0;
00049 static wolfSSL_Realloc_cb realloc_function = 0;
00050 
00051 int wolfSSL_SetAllocators(wolfSSL_Malloc_cb  mf,
00052                           wolfSSL_Free_cb    ff,
00053                           wolfSSL_Realloc_cb rf)
00054 {
00055     int res = 0;
00056 
00057     if (mf)
00058         malloc_function = mf;
00059     else
00060         res = BAD_FUNC_ARG;
00061 
00062     if (ff)
00063         free_function = ff;
00064     else
00065         res = BAD_FUNC_ARG;
00066 
00067     if (rf)
00068         realloc_function = rf;
00069     else
00070         res = BAD_FUNC_ARG;
00071 
00072     return res;
00073 }
00074 
00075 
00076 void* wolfSSL_Malloc(size_t size)
00077 {
00078     void* res = 0;
00079 
00080     if (malloc_function)
00081         res = malloc_function(size);
00082     else
00083         res = malloc(size);
00084 
00085     #ifdef WOLFSSL_MALLOC_CHECK
00086         if (res == NULL)
00087             puts("wolfSSL_malloc failed");
00088     #endif
00089                 
00090     return res;
00091 }
00092 
00093 void wolfSSL_Free(void *ptr)
00094 {
00095     if (free_function)
00096         free_function(ptr);
00097     else
00098         free(ptr);
00099 }
00100 
00101 void* wolfSSL_Realloc(void *ptr, size_t size)
00102 {
00103     void* res = 0;
00104 
00105     if (realloc_function)
00106         res = realloc_function(ptr, size);
00107     else
00108         res = realloc(ptr, size);
00109 
00110     return res;
00111 }
00112 
00113 #endif /* USE_WOLFSSL_MEMORY */
00114 
00115 
00116 #ifdef HAVE_IO_POOL
00117 
00118 /* Example for user io pool, shared build may need definitions in lib proper */
00119 
00120 #include <wolfssl/wolfcrypt/types.h>
00121 #include <stdlib.h>
00122 
00123 #ifndef HAVE_THREAD_LS
00124     #error "Oops, simple I/O pool example needs thread local storage"
00125 #endif
00126 
00127 
00128 /* allow simple per thread in and out pools */
00129 /* use 17k size sense max record size is 16k plus overhead */
00130 static THREAD_LS_T byte pool_in[17*1024];
00131 static THREAD_LS_T byte pool_out[17*1024];
00132 
00133 
00134 void* XMALLOC(size_t n, void* heap, int type)
00135 {
00136     (void)heap;
00137 
00138     if (type == DYNAMIC_TYPE_IN_BUFFER) {
00139         if (n < sizeof(pool_in))
00140             return pool_in;
00141         else
00142             return NULL;
00143     }
00144 
00145     if (type == DYNAMIC_TYPE_OUT_BUFFER) {
00146         if (n < sizeof(pool_out))
00147             return pool_out;
00148         else
00149             return NULL;
00150     }
00151 
00152     return malloc(n);
00153 }
00154 
00155 void* XREALLOC(void *p, size_t n, void* heap, int type)
00156 {
00157     (void)heap;
00158 
00159     if (type == DYNAMIC_TYPE_IN_BUFFER) {
00160         if (n < sizeof(pool_in))
00161             return pool_in;
00162         else
00163             return NULL;
00164     }
00165 
00166     if (type == DYNAMIC_TYPE_OUT_BUFFER) {
00167         if (n < sizeof(pool_out))
00168             return pool_out;
00169         else
00170             return NULL;
00171     }
00172 
00173     return realloc(p, n);
00174 }
00175 
00176 
00177 /* unit api calls, let's make sure visible with WOLFSSL_API */
00178 WOLFSSL_API void XFREE(void *p, void* heap, int type)
00179 {
00180     (void)heap;
00181 
00182     if (type == DYNAMIC_TYPE_IN_BUFFER)
00183         return;  /* do nothing, static pool */
00184 
00185     if (type == DYNAMIC_TYPE_OUT_BUFFER)
00186         return;  /* do nothing, static pool */
00187 
00188     free(p);
00189 }
00190 
00191 #endif /* HAVE_IO_POOL */
00192 
00193