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