This is a port of cyaSSL 2.7.0.

Dependents:   CyaSSL_DTLS_Cellular CyaSSL_DTLS_Ethernet

Committer:
ashleymills
Date:
Thu Sep 05 10:33:04 2013 +0000
Revision:
0:714293de3836
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ashleymills 0:714293de3836 1 /* memory.c
ashleymills 0:714293de3836 2 *
ashleymills 0:714293de3836 3 * Copyright (C) 2006-2013 wolfSSL Inc.
ashleymills 0:714293de3836 4 *
ashleymills 0:714293de3836 5 * This file is part of CyaSSL.
ashleymills 0:714293de3836 6 *
ashleymills 0:714293de3836 7 * CyaSSL is free software; you can redistribute it and/or modify
ashleymills 0:714293de3836 8 * it under the terms of the GNU General Public License as published by
ashleymills 0:714293de3836 9 * the Free Software Foundation; either version 2 of the License, or
ashleymills 0:714293de3836 10 * (at your option) any later version.
ashleymills 0:714293de3836 11 *
ashleymills 0:714293de3836 12 * CyaSSL is distributed in the hope that it will be useful,
ashleymills 0:714293de3836 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ashleymills 0:714293de3836 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ashleymills 0:714293de3836 15 * GNU General Public License for more details.
ashleymills 0:714293de3836 16 *
ashleymills 0:714293de3836 17 * You should have received a copy of the GNU General Public License
ashleymills 0:714293de3836 18 * along with this program; if not, write to the Free Software
ashleymills 0:714293de3836 19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
ashleymills 0:714293de3836 20 */
ashleymills 0:714293de3836 21
ashleymills 0:714293de3836 22 #ifdef HAVE_CONFIG_H
ashleymills 0:714293de3836 23 #include <config.h>
ashleymills 0:714293de3836 24 #endif
ashleymills 0:714293de3836 25
ashleymills 0:714293de3836 26 #include <cyassl/ctaocrypt/settings.h>
ashleymills 0:714293de3836 27
ashleymills 0:714293de3836 28 #ifdef USE_CYASSL_MEMORY
ashleymills 0:714293de3836 29
ashleymills 0:714293de3836 30 #include <cyassl/ctaocrypt/memory.h>
ashleymills 0:714293de3836 31 #include <cyassl/ctaocrypt/ctaoerror2.h>
ashleymills 0:714293de3836 32
ashleymills 0:714293de3836 33 #ifdef CYASSL_MALLOC_CHECK
ashleymills 0:714293de3836 34 #include <stdio.h>
ashleymills 0:714293de3836 35 #endif
ashleymills 0:714293de3836 36
ashleymills 0:714293de3836 37 /* Set these to default values initially. */
ashleymills 0:714293de3836 38 static CyaSSL_Malloc_cb malloc_function = 0;
ashleymills 0:714293de3836 39 static CyaSSL_Free_cb free_function = 0;
ashleymills 0:714293de3836 40 static CyaSSL_Realloc_cb realloc_function = 0;
ashleymills 0:714293de3836 41
ashleymills 0:714293de3836 42 int CyaSSL_SetAllocators(CyaSSL_Malloc_cb mf,
ashleymills 0:714293de3836 43 CyaSSL_Free_cb ff,
ashleymills 0:714293de3836 44 CyaSSL_Realloc_cb rf)
ashleymills 0:714293de3836 45 {
ashleymills 0:714293de3836 46 int res = 0;
ashleymills 0:714293de3836 47
ashleymills 0:714293de3836 48 if (mf)
ashleymills 0:714293de3836 49 malloc_function = mf;
ashleymills 0:714293de3836 50 else
ashleymills 0:714293de3836 51 res = BAD_FUNC_ARG;
ashleymills 0:714293de3836 52
ashleymills 0:714293de3836 53 if (ff)
ashleymills 0:714293de3836 54 free_function = ff;
ashleymills 0:714293de3836 55 else
ashleymills 0:714293de3836 56 res = BAD_FUNC_ARG;
ashleymills 0:714293de3836 57
ashleymills 0:714293de3836 58 if (rf)
ashleymills 0:714293de3836 59 realloc_function = rf;
ashleymills 0:714293de3836 60 else
ashleymills 0:714293de3836 61 res = BAD_FUNC_ARG;
ashleymills 0:714293de3836 62
ashleymills 0:714293de3836 63 return res;
ashleymills 0:714293de3836 64 }
ashleymills 0:714293de3836 65
ashleymills 0:714293de3836 66
ashleymills 0:714293de3836 67 void* CyaSSL_Malloc(size_t size)
ashleymills 0:714293de3836 68 {
ashleymills 0:714293de3836 69 void* res = 0;
ashleymills 0:714293de3836 70
ashleymills 0:714293de3836 71 if (malloc_function)
ashleymills 0:714293de3836 72 res = malloc_function(size);
ashleymills 0:714293de3836 73 else
ashleymills 0:714293de3836 74 res = malloc(size);
ashleymills 0:714293de3836 75
ashleymills 0:714293de3836 76 #ifdef CYASSL_MALLOC_CHECK
ashleymills 0:714293de3836 77 if (res == NULL)
ashleymills 0:714293de3836 78 printf("CyaSSL_malloc failed\n");
ashleymills 0:714293de3836 79 #endif
ashleymills 0:714293de3836 80
ashleymills 0:714293de3836 81 return res;
ashleymills 0:714293de3836 82 }
ashleymills 0:714293de3836 83
ashleymills 0:714293de3836 84 void CyaSSL_Free(void *ptr)
ashleymills 0:714293de3836 85 {
ashleymills 0:714293de3836 86 if (free_function)
ashleymills 0:714293de3836 87 free_function(ptr);
ashleymills 0:714293de3836 88 else
ashleymills 0:714293de3836 89 free(ptr);
ashleymills 0:714293de3836 90 }
ashleymills 0:714293de3836 91
ashleymills 0:714293de3836 92 void* CyaSSL_Realloc(void *ptr, size_t size)
ashleymills 0:714293de3836 93 {
ashleymills 0:714293de3836 94 void* res = 0;
ashleymills 0:714293de3836 95
ashleymills 0:714293de3836 96 if (realloc_function)
ashleymills 0:714293de3836 97 res = realloc_function(ptr, size);
ashleymills 0:714293de3836 98 else
ashleymills 0:714293de3836 99 res = realloc(ptr, size);
ashleymills 0:714293de3836 100
ashleymills 0:714293de3836 101 return res;
ashleymills 0:714293de3836 102 }
ashleymills 0:714293de3836 103
ashleymills 0:714293de3836 104 #endif /* USE_CYASSL_MEMORY */