Francois Berder / cyassl-lib

Dependents:   TLS_cyassl TLS_cyassl

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-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/ctaoerror2.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             printf("CyaSSL_malloc failed\n");
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 */