SSL/TLS Library
CyaSSL is SSL/TLS library for embedded systems.
ctaocrypt/src/memory.c@0:9d17e4342598, 2014-04-20 (annotated)
- Committer:
- wolfSSL
- Date:
- Sun Apr 20 12:40:57 2014 +0000
- Revision:
- 0:9d17e4342598
CyaSSL SSL/TLS Library 2.9.4;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wolfSSL | 0:9d17e4342598 | 1 | /* memory.c |
wolfSSL | 0:9d17e4342598 | 2 | * |
wolfSSL | 0:9d17e4342598 | 3 | * Copyright (C) 2006-2013 wolfSSL Inc. |
wolfSSL | 0:9d17e4342598 | 4 | * |
wolfSSL | 0:9d17e4342598 | 5 | * This file is part of CyaSSL. |
wolfSSL | 0:9d17e4342598 | 6 | * |
wolfSSL | 0:9d17e4342598 | 7 | * CyaSSL is free software; you can redistribute it and/or modify |
wolfSSL | 0:9d17e4342598 | 8 | * it under the terms of the GNU General Public License as published by |
wolfSSL | 0:9d17e4342598 | 9 | * the Free Software Foundation; either version 2 of the License, or |
wolfSSL | 0:9d17e4342598 | 10 | * (at your option) any later version. |
wolfSSL | 0:9d17e4342598 | 11 | * |
wolfSSL | 0:9d17e4342598 | 12 | * CyaSSL is distributed in the hope that it will be useful, |
wolfSSL | 0:9d17e4342598 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
wolfSSL | 0:9d17e4342598 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
wolfSSL | 0:9d17e4342598 | 15 | * GNU General Public License for more details. |
wolfSSL | 0:9d17e4342598 | 16 | * |
wolfSSL | 0:9d17e4342598 | 17 | * You should have received a copy of the GNU General Public License |
wolfSSL | 0:9d17e4342598 | 18 | * along with this program; if not, write to the Free Software |
wolfSSL | 0:9d17e4342598 | 19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
wolfSSL | 0:9d17e4342598 | 20 | */ |
wolfSSL | 0:9d17e4342598 | 21 | |
wolfSSL | 0:9d17e4342598 | 22 | #ifdef HAVE_CONFIG_H |
wolfSSL | 0:9d17e4342598 | 23 | #include <config.h> |
wolfSSL | 0:9d17e4342598 | 24 | #endif |
wolfSSL | 0:9d17e4342598 | 25 | |
wolfSSL | 0:9d17e4342598 | 26 | #include <cyassl/ctaocrypt/settings.h> |
wolfSSL | 0:9d17e4342598 | 27 | |
wolfSSL | 0:9d17e4342598 | 28 | #ifdef USE_CYASSL_MEMORY |
wolfSSL | 0:9d17e4342598 | 29 | |
wolfSSL | 0:9d17e4342598 | 30 | #include <cyassl/ctaocrypt/memory.h> |
wolfSSL | 0:9d17e4342598 | 31 | #include <cyassl/ctaocrypt/error-crypt.h> |
wolfSSL | 0:9d17e4342598 | 32 | |
wolfSSL | 0:9d17e4342598 | 33 | #ifdef CYASSL_MALLOC_CHECK |
wolfSSL | 0:9d17e4342598 | 34 | #include <stdio.h> |
wolfSSL | 0:9d17e4342598 | 35 | #endif |
wolfSSL | 0:9d17e4342598 | 36 | |
wolfSSL | 0:9d17e4342598 | 37 | /* Set these to default values initially. */ |
wolfSSL | 0:9d17e4342598 | 38 | static CyaSSL_Malloc_cb malloc_function = 0; |
wolfSSL | 0:9d17e4342598 | 39 | static CyaSSL_Free_cb free_function = 0; |
wolfSSL | 0:9d17e4342598 | 40 | static CyaSSL_Realloc_cb realloc_function = 0; |
wolfSSL | 0:9d17e4342598 | 41 | |
wolfSSL | 0:9d17e4342598 | 42 | int CyaSSL_SetAllocators(CyaSSL_Malloc_cb mf, |
wolfSSL | 0:9d17e4342598 | 43 | CyaSSL_Free_cb ff, |
wolfSSL | 0:9d17e4342598 | 44 | CyaSSL_Realloc_cb rf) |
wolfSSL | 0:9d17e4342598 | 45 | { |
wolfSSL | 0:9d17e4342598 | 46 | int res = 0; |
wolfSSL | 0:9d17e4342598 | 47 | |
wolfSSL | 0:9d17e4342598 | 48 | if (mf) |
wolfSSL | 0:9d17e4342598 | 49 | malloc_function = mf; |
wolfSSL | 0:9d17e4342598 | 50 | else |
wolfSSL | 0:9d17e4342598 | 51 | res = BAD_FUNC_ARG; |
wolfSSL | 0:9d17e4342598 | 52 | |
wolfSSL | 0:9d17e4342598 | 53 | if (ff) |
wolfSSL | 0:9d17e4342598 | 54 | free_function = ff; |
wolfSSL | 0:9d17e4342598 | 55 | else |
wolfSSL | 0:9d17e4342598 | 56 | res = BAD_FUNC_ARG; |
wolfSSL | 0:9d17e4342598 | 57 | |
wolfSSL | 0:9d17e4342598 | 58 | if (rf) |
wolfSSL | 0:9d17e4342598 | 59 | realloc_function = rf; |
wolfSSL | 0:9d17e4342598 | 60 | else |
wolfSSL | 0:9d17e4342598 | 61 | res = BAD_FUNC_ARG; |
wolfSSL | 0:9d17e4342598 | 62 | |
wolfSSL | 0:9d17e4342598 | 63 | return res; |
wolfSSL | 0:9d17e4342598 | 64 | } |
wolfSSL | 0:9d17e4342598 | 65 | |
wolfSSL | 0:9d17e4342598 | 66 | |
wolfSSL | 0:9d17e4342598 | 67 | void* CyaSSL_Malloc(size_t size) |
wolfSSL | 0:9d17e4342598 | 68 | { |
wolfSSL | 0:9d17e4342598 | 69 | void* res = 0; |
wolfSSL | 0:9d17e4342598 | 70 | |
wolfSSL | 0:9d17e4342598 | 71 | if (malloc_function) |
wolfSSL | 0:9d17e4342598 | 72 | res = malloc_function(size); |
wolfSSL | 0:9d17e4342598 | 73 | else |
wolfSSL | 0:9d17e4342598 | 74 | res = malloc(size); |
wolfSSL | 0:9d17e4342598 | 75 | |
wolfSSL | 0:9d17e4342598 | 76 | #ifdef CYASSL_MALLOC_CHECK |
wolfSSL | 0:9d17e4342598 | 77 | if (res == NULL) |
wolfSSL | 0:9d17e4342598 | 78 | puts("CyaSSL_malloc failed"); |
wolfSSL | 0:9d17e4342598 | 79 | #endif |
wolfSSL | 0:9d17e4342598 | 80 | |
wolfSSL | 0:9d17e4342598 | 81 | return res; |
wolfSSL | 0:9d17e4342598 | 82 | } |
wolfSSL | 0:9d17e4342598 | 83 | |
wolfSSL | 0:9d17e4342598 | 84 | void CyaSSL_Free(void *ptr) |
wolfSSL | 0:9d17e4342598 | 85 | { |
wolfSSL | 0:9d17e4342598 | 86 | if (free_function) |
wolfSSL | 0:9d17e4342598 | 87 | free_function(ptr); |
wolfSSL | 0:9d17e4342598 | 88 | else |
wolfSSL | 0:9d17e4342598 | 89 | free(ptr); |
wolfSSL | 0:9d17e4342598 | 90 | } |
wolfSSL | 0:9d17e4342598 | 91 | |
wolfSSL | 0:9d17e4342598 | 92 | void* CyaSSL_Realloc(void *ptr, size_t size) |
wolfSSL | 0:9d17e4342598 | 93 | { |
wolfSSL | 0:9d17e4342598 | 94 | void* res = 0; |
wolfSSL | 0:9d17e4342598 | 95 | |
wolfSSL | 0:9d17e4342598 | 96 | if (realloc_function) |
wolfSSL | 0:9d17e4342598 | 97 | res = realloc_function(ptr, size); |
wolfSSL | 0:9d17e4342598 | 98 | else |
wolfSSL | 0:9d17e4342598 | 99 | res = realloc(ptr, size); |
wolfSSL | 0:9d17e4342598 | 100 | |
wolfSSL | 0:9d17e4342598 | 101 | return res; |
wolfSSL | 0:9d17e4342598 | 102 | } |
wolfSSL | 0:9d17e4342598 | 103 | |
wolfSSL | 0:9d17e4342598 | 104 | #endif /* USE_CYASSL_MEMORY */ |
wolfSSL | 0:9d17e4342598 | 105 | |
wolfSSL | 0:9d17e4342598 | 106 | |
wolfSSL | 0:9d17e4342598 | 107 | #ifdef HAVE_IO_POOL |
wolfSSL | 0:9d17e4342598 | 108 | |
wolfSSL | 0:9d17e4342598 | 109 | /* Example for user io pool, shared build may need definitions in lib proper */ |
wolfSSL | 0:9d17e4342598 | 110 | |
wolfSSL | 0:9d17e4342598 | 111 | #include <cyassl/ctaocrypt/types.h> |
wolfSSL | 0:9d17e4342598 | 112 | #include <stdlib.h> |
wolfSSL | 0:9d17e4342598 | 113 | |
wolfSSL | 0:9d17e4342598 | 114 | #ifndef HAVE_THREAD_LS |
wolfSSL | 0:9d17e4342598 | 115 | #error "Oops, simple I/O pool example needs thread local storage" |
wolfSSL | 0:9d17e4342598 | 116 | #endif |
wolfSSL | 0:9d17e4342598 | 117 | |
wolfSSL | 0:9d17e4342598 | 118 | |
wolfSSL | 0:9d17e4342598 | 119 | /* allow simple per thread in and out pools */ |
wolfSSL | 0:9d17e4342598 | 120 | /* use 17k size sense max record size is 16k plus overhead */ |
wolfSSL | 0:9d17e4342598 | 121 | static THREAD_LS_T byte pool_in[17*1024]; |
wolfSSL | 0:9d17e4342598 | 122 | static THREAD_LS_T byte pool_out[17*1024]; |
wolfSSL | 0:9d17e4342598 | 123 | |
wolfSSL | 0:9d17e4342598 | 124 | |
wolfSSL | 0:9d17e4342598 | 125 | void* XMALLOC(size_t n, void* heap, int type) |
wolfSSL | 0:9d17e4342598 | 126 | { |
wolfSSL | 0:9d17e4342598 | 127 | (void)heap; |
wolfSSL | 0:9d17e4342598 | 128 | |
wolfSSL | 0:9d17e4342598 | 129 | if (type == DYNAMIC_TYPE_IN_BUFFER) { |
wolfSSL | 0:9d17e4342598 | 130 | if (n < sizeof(pool_in)) |
wolfSSL | 0:9d17e4342598 | 131 | return pool_in; |
wolfSSL | 0:9d17e4342598 | 132 | else |
wolfSSL | 0:9d17e4342598 | 133 | return NULL; |
wolfSSL | 0:9d17e4342598 | 134 | } |
wolfSSL | 0:9d17e4342598 | 135 | |
wolfSSL | 0:9d17e4342598 | 136 | if (type == DYNAMIC_TYPE_OUT_BUFFER) { |
wolfSSL | 0:9d17e4342598 | 137 | if (n < sizeof(pool_out)) |
wolfSSL | 0:9d17e4342598 | 138 | return pool_out; |
wolfSSL | 0:9d17e4342598 | 139 | else |
wolfSSL | 0:9d17e4342598 | 140 | return NULL; |
wolfSSL | 0:9d17e4342598 | 141 | } |
wolfSSL | 0:9d17e4342598 | 142 | |
wolfSSL | 0:9d17e4342598 | 143 | return malloc(n); |
wolfSSL | 0:9d17e4342598 | 144 | } |
wolfSSL | 0:9d17e4342598 | 145 | |
wolfSSL | 0:9d17e4342598 | 146 | void* XREALLOC(void *p, size_t n, void* heap, int type) |
wolfSSL | 0:9d17e4342598 | 147 | { |
wolfSSL | 0:9d17e4342598 | 148 | (void)heap; |
wolfSSL | 0:9d17e4342598 | 149 | |
wolfSSL | 0:9d17e4342598 | 150 | if (type == DYNAMIC_TYPE_IN_BUFFER) { |
wolfSSL | 0:9d17e4342598 | 151 | if (n < sizeof(pool_in)) |
wolfSSL | 0:9d17e4342598 | 152 | return pool_in; |
wolfSSL | 0:9d17e4342598 | 153 | else |
wolfSSL | 0:9d17e4342598 | 154 | return NULL; |
wolfSSL | 0:9d17e4342598 | 155 | } |
wolfSSL | 0:9d17e4342598 | 156 | |
wolfSSL | 0:9d17e4342598 | 157 | if (type == DYNAMIC_TYPE_OUT_BUFFER) { |
wolfSSL | 0:9d17e4342598 | 158 | if (n < sizeof(pool_out)) |
wolfSSL | 0:9d17e4342598 | 159 | return pool_out; |
wolfSSL | 0:9d17e4342598 | 160 | else |
wolfSSL | 0:9d17e4342598 | 161 | return NULL; |
wolfSSL | 0:9d17e4342598 | 162 | } |
wolfSSL | 0:9d17e4342598 | 163 | |
wolfSSL | 0:9d17e4342598 | 164 | return realloc(p, n); |
wolfSSL | 0:9d17e4342598 | 165 | } |
wolfSSL | 0:9d17e4342598 | 166 | |
wolfSSL | 0:9d17e4342598 | 167 | |
wolfSSL | 0:9d17e4342598 | 168 | /* unit api calls, let's make sure visisble with CYASSL_API */ |
wolfSSL | 0:9d17e4342598 | 169 | CYASSL_API void XFREE(void *p, void* heap, int type) |
wolfSSL | 0:9d17e4342598 | 170 | { |
wolfSSL | 0:9d17e4342598 | 171 | (void)heap; |
wolfSSL | 0:9d17e4342598 | 172 | |
wolfSSL | 0:9d17e4342598 | 173 | if (type == DYNAMIC_TYPE_IN_BUFFER) |
wolfSSL | 0:9d17e4342598 | 174 | return; /* do nothing, static pool */ |
wolfSSL | 0:9d17e4342598 | 175 | |
wolfSSL | 0:9d17e4342598 | 176 | if (type == DYNAMIC_TYPE_OUT_BUFFER) |
wolfSSL | 0:9d17e4342598 | 177 | return; /* do nothing, static pool */ |
wolfSSL | 0:9d17e4342598 | 178 | |
wolfSSL | 0:9d17e4342598 | 179 | free(p); |
wolfSSL | 0:9d17e4342598 | 180 | } |
wolfSSL | 0:9d17e4342598 | 181 | |
wolfSSL | 0:9d17e4342598 | 182 | #endif /* HAVE_IO_POOL */ |
wolfSSL | 0:9d17e4342598 | 183 |