Hannes Tschofenig
/
aes-gcm-test-program
Example program to test AES-GCM functionality. Used for a workshop
SSL/library/memory_buffer_alloc.c@0:796d0f61a05b, 2018-09-27 (annotated)
- Committer:
- HannesTschofenig
- Date:
- Thu Sep 27 06:34:22 2018 +0000
- Revision:
- 0:796d0f61a05b
Example AES-GCM test program
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
HannesTschofenig | 0:796d0f61a05b | 1 | /* |
HannesTschofenig | 0:796d0f61a05b | 2 | * Buffer-based memory allocator |
HannesTschofenig | 0:796d0f61a05b | 3 | * |
HannesTschofenig | 0:796d0f61a05b | 4 | * Copyright (C) 2006-2014, Brainspark B.V. |
HannesTschofenig | 0:796d0f61a05b | 5 | * |
HannesTschofenig | 0:796d0f61a05b | 6 | * This file is part of PolarSSL (http://www.polarssl.org) |
HannesTschofenig | 0:796d0f61a05b | 7 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
HannesTschofenig | 0:796d0f61a05b | 8 | * |
HannesTschofenig | 0:796d0f61a05b | 9 | * All rights reserved. |
HannesTschofenig | 0:796d0f61a05b | 10 | * |
HannesTschofenig | 0:796d0f61a05b | 11 | * This program is free software; you can redistribute it and/or modify |
HannesTschofenig | 0:796d0f61a05b | 12 | * it under the terms of the GNU General Public License as published by |
HannesTschofenig | 0:796d0f61a05b | 13 | * the Free Software Foundation; either version 2 of the License, or |
HannesTschofenig | 0:796d0f61a05b | 14 | * (at your option) any later version. |
HannesTschofenig | 0:796d0f61a05b | 15 | * |
HannesTschofenig | 0:796d0f61a05b | 16 | * This program is distributed in the hope that it will be useful, |
HannesTschofenig | 0:796d0f61a05b | 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
HannesTschofenig | 0:796d0f61a05b | 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
HannesTschofenig | 0:796d0f61a05b | 19 | * GNU General Public License for more details. |
HannesTschofenig | 0:796d0f61a05b | 20 | * |
HannesTschofenig | 0:796d0f61a05b | 21 | * You should have received a copy of the GNU General Public License along |
HannesTschofenig | 0:796d0f61a05b | 22 | * with this program; if not, write to the Free Software Foundation, Inc., |
HannesTschofenig | 0:796d0f61a05b | 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
HannesTschofenig | 0:796d0f61a05b | 24 | */ |
HannesTschofenig | 0:796d0f61a05b | 25 | |
HannesTschofenig | 0:796d0f61a05b | 26 | #if !defined(POLARSSL_CONFIG_FILE) |
HannesTschofenig | 0:796d0f61a05b | 27 | #include "polarssl/config.h" |
HannesTschofenig | 0:796d0f61a05b | 28 | #else |
HannesTschofenig | 0:796d0f61a05b | 29 | #include POLARSSL_CONFIG_FILE |
HannesTschofenig | 0:796d0f61a05b | 30 | #endif |
HannesTschofenig | 0:796d0f61a05b | 31 | |
HannesTschofenig | 0:796d0f61a05b | 32 | #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) |
HannesTschofenig | 0:796d0f61a05b | 33 | |
HannesTschofenig | 0:796d0f61a05b | 34 | #include "polarssl/memory_buffer_alloc.h" |
HannesTschofenig | 0:796d0f61a05b | 35 | |
HannesTschofenig | 0:796d0f61a05b | 36 | #include <string.h> |
HannesTschofenig | 0:796d0f61a05b | 37 | |
HannesTschofenig | 0:796d0f61a05b | 38 | #if defined(POLARSSL_MEMORY_DEBUG) |
HannesTschofenig | 0:796d0f61a05b | 39 | #include <stdio.h> |
HannesTschofenig | 0:796d0f61a05b | 40 | #if defined(POLARSSL_MEMORY_BACKTRACE) |
HannesTschofenig | 0:796d0f61a05b | 41 | #include <execinfo.h> |
HannesTschofenig | 0:796d0f61a05b | 42 | #endif |
HannesTschofenig | 0:796d0f61a05b | 43 | #endif |
HannesTschofenig | 0:796d0f61a05b | 44 | |
HannesTschofenig | 0:796d0f61a05b | 45 | #if defined(POLARSSL_THREADING_C) |
HannesTschofenig | 0:796d0f61a05b | 46 | #include "polarssl/threading.h" |
HannesTschofenig | 0:796d0f61a05b | 47 | #endif |
HannesTschofenig | 0:796d0f61a05b | 48 | |
HannesTschofenig | 0:796d0f61a05b | 49 | #if defined(POLARSSL_PLATFORM_C) |
HannesTschofenig | 0:796d0f61a05b | 50 | #include "polarssl/platform.h" |
HannesTschofenig | 0:796d0f61a05b | 51 | #else |
HannesTschofenig | 0:796d0f61a05b | 52 | #define polarssl_fprintf fprintf |
HannesTschofenig | 0:796d0f61a05b | 53 | #endif |
HannesTschofenig | 0:796d0f61a05b | 54 | |
HannesTschofenig | 0:796d0f61a05b | 55 | #define MAGIC1 0xFF00AA55 |
HannesTschofenig | 0:796d0f61a05b | 56 | #define MAGIC2 0xEE119966 |
HannesTschofenig | 0:796d0f61a05b | 57 | #define MAX_BT 20 |
HannesTschofenig | 0:796d0f61a05b | 58 | |
HannesTschofenig | 0:796d0f61a05b | 59 | typedef struct _memory_header memory_header; |
HannesTschofenig | 0:796d0f61a05b | 60 | struct _memory_header |
HannesTschofenig | 0:796d0f61a05b | 61 | { |
HannesTschofenig | 0:796d0f61a05b | 62 | size_t magic1; |
HannesTschofenig | 0:796d0f61a05b | 63 | size_t size; |
HannesTschofenig | 0:796d0f61a05b | 64 | size_t alloc; |
HannesTschofenig | 0:796d0f61a05b | 65 | memory_header *prev; |
HannesTschofenig | 0:796d0f61a05b | 66 | memory_header *next; |
HannesTschofenig | 0:796d0f61a05b | 67 | memory_header *prev_free; |
HannesTschofenig | 0:796d0f61a05b | 68 | memory_header *next_free; |
HannesTschofenig | 0:796d0f61a05b | 69 | #if defined(POLARSSL_MEMORY_BACKTRACE) |
HannesTschofenig | 0:796d0f61a05b | 70 | char **trace; |
HannesTschofenig | 0:796d0f61a05b | 71 | size_t trace_count; |
HannesTschofenig | 0:796d0f61a05b | 72 | #endif |
HannesTschofenig | 0:796d0f61a05b | 73 | size_t magic2; |
HannesTschofenig | 0:796d0f61a05b | 74 | }; |
HannesTschofenig | 0:796d0f61a05b | 75 | |
HannesTschofenig | 0:796d0f61a05b | 76 | typedef struct |
HannesTschofenig | 0:796d0f61a05b | 77 | { |
HannesTschofenig | 0:796d0f61a05b | 78 | unsigned char *buf; |
HannesTschofenig | 0:796d0f61a05b | 79 | size_t len; |
HannesTschofenig | 0:796d0f61a05b | 80 | memory_header *first; |
HannesTschofenig | 0:796d0f61a05b | 81 | memory_header *first_free; |
HannesTschofenig | 0:796d0f61a05b | 82 | size_t current_alloc_size; |
HannesTschofenig | 0:796d0f61a05b | 83 | int verify; |
HannesTschofenig | 0:796d0f61a05b | 84 | #if defined(POLARSSL_MEMORY_DEBUG) |
HannesTschofenig | 0:796d0f61a05b | 85 | size_t malloc_count; |
HannesTschofenig | 0:796d0f61a05b | 86 | size_t free_count; |
HannesTschofenig | 0:796d0f61a05b | 87 | size_t total_used; |
HannesTschofenig | 0:796d0f61a05b | 88 | size_t maximum_used; |
HannesTschofenig | 0:796d0f61a05b | 89 | size_t header_count; |
HannesTschofenig | 0:796d0f61a05b | 90 | size_t maximum_header_count; |
HannesTschofenig | 0:796d0f61a05b | 91 | #endif |
HannesTschofenig | 0:796d0f61a05b | 92 | #if defined(POLARSSL_THREADING_C) |
HannesTschofenig | 0:796d0f61a05b | 93 | threading_mutex_t mutex; |
HannesTschofenig | 0:796d0f61a05b | 94 | #endif |
HannesTschofenig | 0:796d0f61a05b | 95 | } |
HannesTschofenig | 0:796d0f61a05b | 96 | buffer_alloc_ctx; |
HannesTschofenig | 0:796d0f61a05b | 97 | |
HannesTschofenig | 0:796d0f61a05b | 98 | static buffer_alloc_ctx heap; |
HannesTschofenig | 0:796d0f61a05b | 99 | |
HannesTschofenig | 0:796d0f61a05b | 100 | #if defined(POLARSSL_MEMORY_DEBUG) |
HannesTschofenig | 0:796d0f61a05b | 101 | static void debug_header( memory_header *hdr ) |
HannesTschofenig | 0:796d0f61a05b | 102 | { |
HannesTschofenig | 0:796d0f61a05b | 103 | #if defined(POLARSSL_MEMORY_BACKTRACE) |
HannesTschofenig | 0:796d0f61a05b | 104 | size_t i; |
HannesTschofenig | 0:796d0f61a05b | 105 | #endif |
HannesTschofenig | 0:796d0f61a05b | 106 | |
HannesTschofenig | 0:796d0f61a05b | 107 | polarssl_fprintf( stderr, "HDR: PTR(%10u), PREV(%10u), NEXT(%10u), " |
HannesTschofenig | 0:796d0f61a05b | 108 | "ALLOC(%u), SIZE(%10u)\n", |
HannesTschofenig | 0:796d0f61a05b | 109 | (size_t) hdr, (size_t) hdr->prev, (size_t) hdr->next, |
HannesTschofenig | 0:796d0f61a05b | 110 | hdr->alloc, hdr->size ); |
HannesTschofenig | 0:796d0f61a05b | 111 | polarssl_fprintf( stderr, " FPREV(%10u), FNEXT(%10u)\n", |
HannesTschofenig | 0:796d0f61a05b | 112 | (size_t) hdr->prev_free, (size_t) hdr->next_free ); |
HannesTschofenig | 0:796d0f61a05b | 113 | |
HannesTschofenig | 0:796d0f61a05b | 114 | #if defined(POLARSSL_MEMORY_BACKTRACE) |
HannesTschofenig | 0:796d0f61a05b | 115 | polarssl_fprintf( stderr, "TRACE: \n" ); |
HannesTschofenig | 0:796d0f61a05b | 116 | for( i = 0; i < hdr->trace_count; i++ ) |
HannesTschofenig | 0:796d0f61a05b | 117 | polarssl_fprintf( stderr, "%s\n", hdr->trace[i] ); |
HannesTschofenig | 0:796d0f61a05b | 118 | polarssl_fprintf( stderr, "\n" ); |
HannesTschofenig | 0:796d0f61a05b | 119 | #endif |
HannesTschofenig | 0:796d0f61a05b | 120 | } |
HannesTschofenig | 0:796d0f61a05b | 121 | |
HannesTschofenig | 0:796d0f61a05b | 122 | static void debug_chain() |
HannesTschofenig | 0:796d0f61a05b | 123 | { |
HannesTschofenig | 0:796d0f61a05b | 124 | memory_header *cur = heap.first; |
HannesTschofenig | 0:796d0f61a05b | 125 | |
HannesTschofenig | 0:796d0f61a05b | 126 | polarssl_fprintf( stderr, "\nBlock list\n" ); |
HannesTschofenig | 0:796d0f61a05b | 127 | while( cur != NULL ) |
HannesTschofenig | 0:796d0f61a05b | 128 | { |
HannesTschofenig | 0:796d0f61a05b | 129 | debug_header( cur ); |
HannesTschofenig | 0:796d0f61a05b | 130 | cur = cur->next; |
HannesTschofenig | 0:796d0f61a05b | 131 | } |
HannesTschofenig | 0:796d0f61a05b | 132 | |
HannesTschofenig | 0:796d0f61a05b | 133 | polarssl_fprintf( stderr, "Free list\n" ); |
HannesTschofenig | 0:796d0f61a05b | 134 | cur = heap.first_free; |
HannesTschofenig | 0:796d0f61a05b | 135 | |
HannesTschofenig | 0:796d0f61a05b | 136 | while( cur != NULL ) |
HannesTschofenig | 0:796d0f61a05b | 137 | { |
HannesTschofenig | 0:796d0f61a05b | 138 | debug_header( cur ); |
HannesTschofenig | 0:796d0f61a05b | 139 | cur = cur->next_free; |
HannesTschofenig | 0:796d0f61a05b | 140 | } |
HannesTschofenig | 0:796d0f61a05b | 141 | } |
HannesTschofenig | 0:796d0f61a05b | 142 | #endif /* POLARSSL_MEMORY_DEBUG */ |
HannesTschofenig | 0:796d0f61a05b | 143 | |
HannesTschofenig | 0:796d0f61a05b | 144 | static int verify_header( memory_header *hdr ) |
HannesTschofenig | 0:796d0f61a05b | 145 | { |
HannesTschofenig | 0:796d0f61a05b | 146 | if( hdr->magic1 != MAGIC1 ) |
HannesTschofenig | 0:796d0f61a05b | 147 | { |
HannesTschofenig | 0:796d0f61a05b | 148 | #if defined(POLARSSL_MEMORY_DEBUG) |
HannesTschofenig | 0:796d0f61a05b | 149 | polarssl_fprintf( stderr, "FATAL: MAGIC1 mismatch\n" ); |
HannesTschofenig | 0:796d0f61a05b | 150 | #endif |
HannesTschofenig | 0:796d0f61a05b | 151 | return( 1 ); |
HannesTschofenig | 0:796d0f61a05b | 152 | } |
HannesTschofenig | 0:796d0f61a05b | 153 | |
HannesTschofenig | 0:796d0f61a05b | 154 | if( hdr->magic2 != MAGIC2 ) |
HannesTschofenig | 0:796d0f61a05b | 155 | { |
HannesTschofenig | 0:796d0f61a05b | 156 | #if defined(POLARSSL_MEMORY_DEBUG) |
HannesTschofenig | 0:796d0f61a05b | 157 | polarssl_fprintf( stderr, "FATAL: MAGIC2 mismatch\n" ); |
HannesTschofenig | 0:796d0f61a05b | 158 | #endif |
HannesTschofenig | 0:796d0f61a05b | 159 | return( 1 ); |
HannesTschofenig | 0:796d0f61a05b | 160 | } |
HannesTschofenig | 0:796d0f61a05b | 161 | |
HannesTschofenig | 0:796d0f61a05b | 162 | if( hdr->alloc > 1 ) |
HannesTschofenig | 0:796d0f61a05b | 163 | { |
HannesTschofenig | 0:796d0f61a05b | 164 | #if defined(POLARSSL_MEMORY_DEBUG) |
HannesTschofenig | 0:796d0f61a05b | 165 | polarssl_fprintf( stderr, "FATAL: alloc has illegal value\n" ); |
HannesTschofenig | 0:796d0f61a05b | 166 | #endif |
HannesTschofenig | 0:796d0f61a05b | 167 | return( 1 ); |
HannesTschofenig | 0:796d0f61a05b | 168 | } |
HannesTschofenig | 0:796d0f61a05b | 169 | |
HannesTschofenig | 0:796d0f61a05b | 170 | if( hdr->prev != NULL && hdr->prev == hdr->next ) |
HannesTschofenig | 0:796d0f61a05b | 171 | { |
HannesTschofenig | 0:796d0f61a05b | 172 | #if defined(POLARSSL_MEMORY_DEBUG) |
HannesTschofenig | 0:796d0f61a05b | 173 | polarssl_fprintf( stderr, "FATAL: prev == next\n" ); |
HannesTschofenig | 0:796d0f61a05b | 174 | #endif |
HannesTschofenig | 0:796d0f61a05b | 175 | return( 1 ); |
HannesTschofenig | 0:796d0f61a05b | 176 | } |
HannesTschofenig | 0:796d0f61a05b | 177 | |
HannesTschofenig | 0:796d0f61a05b | 178 | if( hdr->prev_free != NULL && hdr->prev_free == hdr->next_free ) |
HannesTschofenig | 0:796d0f61a05b | 179 | { |
HannesTschofenig | 0:796d0f61a05b | 180 | #if defined(POLARSSL_MEMORY_DEBUG) |
HannesTschofenig | 0:796d0f61a05b | 181 | polarssl_fprintf( stderr, "FATAL: prev_free == next_free\n" ); |
HannesTschofenig | 0:796d0f61a05b | 182 | #endif |
HannesTschofenig | 0:796d0f61a05b | 183 | return( 1 ); |
HannesTschofenig | 0:796d0f61a05b | 184 | } |
HannesTschofenig | 0:796d0f61a05b | 185 | |
HannesTschofenig | 0:796d0f61a05b | 186 | return( 0 ); |
HannesTschofenig | 0:796d0f61a05b | 187 | } |
HannesTschofenig | 0:796d0f61a05b | 188 | |
HannesTschofenig | 0:796d0f61a05b | 189 | static int verify_chain() |
HannesTschofenig | 0:796d0f61a05b | 190 | { |
HannesTschofenig | 0:796d0f61a05b | 191 | memory_header *prv = heap.first, *cur = heap.first->next; |
HannesTschofenig | 0:796d0f61a05b | 192 | |
HannesTschofenig | 0:796d0f61a05b | 193 | if( verify_header( heap.first ) != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 194 | { |
HannesTschofenig | 0:796d0f61a05b | 195 | #if defined(POLARSSL_MEMORY_DEBUG) |
HannesTschofenig | 0:796d0f61a05b | 196 | polarssl_fprintf( stderr, "FATAL: verification of first header " |
HannesTschofenig | 0:796d0f61a05b | 197 | "failed\n" ); |
HannesTschofenig | 0:796d0f61a05b | 198 | #endif |
HannesTschofenig | 0:796d0f61a05b | 199 | return( 1 ); |
HannesTschofenig | 0:796d0f61a05b | 200 | } |
HannesTschofenig | 0:796d0f61a05b | 201 | |
HannesTschofenig | 0:796d0f61a05b | 202 | if( heap.first->prev != NULL ) |
HannesTschofenig | 0:796d0f61a05b | 203 | { |
HannesTschofenig | 0:796d0f61a05b | 204 | #if defined(POLARSSL_MEMORY_DEBUG) |
HannesTschofenig | 0:796d0f61a05b | 205 | polarssl_fprintf( stderr, "FATAL: verification failed: " |
HannesTschofenig | 0:796d0f61a05b | 206 | "first->prev != NULL\n" ); |
HannesTschofenig | 0:796d0f61a05b | 207 | #endif |
HannesTschofenig | 0:796d0f61a05b | 208 | return( 1 ); |
HannesTschofenig | 0:796d0f61a05b | 209 | } |
HannesTschofenig | 0:796d0f61a05b | 210 | |
HannesTschofenig | 0:796d0f61a05b | 211 | while( cur != NULL ) |
HannesTschofenig | 0:796d0f61a05b | 212 | { |
HannesTschofenig | 0:796d0f61a05b | 213 | if( verify_header( cur ) != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 214 | { |
HannesTschofenig | 0:796d0f61a05b | 215 | #if defined(POLARSSL_MEMORY_DEBUG) |
HannesTschofenig | 0:796d0f61a05b | 216 | polarssl_fprintf( stderr, "FATAL: verification of header " |
HannesTschofenig | 0:796d0f61a05b | 217 | "failed\n" ); |
HannesTschofenig | 0:796d0f61a05b | 218 | #endif |
HannesTschofenig | 0:796d0f61a05b | 219 | return( 1 ); |
HannesTschofenig | 0:796d0f61a05b | 220 | } |
HannesTschofenig | 0:796d0f61a05b | 221 | |
HannesTschofenig | 0:796d0f61a05b | 222 | if( cur->prev != prv ) |
HannesTschofenig | 0:796d0f61a05b | 223 | { |
HannesTschofenig | 0:796d0f61a05b | 224 | #if defined(POLARSSL_MEMORY_DEBUG) |
HannesTschofenig | 0:796d0f61a05b | 225 | polarssl_fprintf( stderr, "FATAL: verification failed: " |
HannesTschofenig | 0:796d0f61a05b | 226 | "cur->prev != prv\n" ); |
HannesTschofenig | 0:796d0f61a05b | 227 | #endif |
HannesTschofenig | 0:796d0f61a05b | 228 | return( 1 ); |
HannesTschofenig | 0:796d0f61a05b | 229 | } |
HannesTschofenig | 0:796d0f61a05b | 230 | |
HannesTschofenig | 0:796d0f61a05b | 231 | prv = cur; |
HannesTschofenig | 0:796d0f61a05b | 232 | cur = cur->next; |
HannesTschofenig | 0:796d0f61a05b | 233 | } |
HannesTschofenig | 0:796d0f61a05b | 234 | |
HannesTschofenig | 0:796d0f61a05b | 235 | return( 0 ); |
HannesTschofenig | 0:796d0f61a05b | 236 | } |
HannesTschofenig | 0:796d0f61a05b | 237 | |
HannesTschofenig | 0:796d0f61a05b | 238 | static void *buffer_alloc_malloc( size_t len ) |
HannesTschofenig | 0:796d0f61a05b | 239 | { |
HannesTschofenig | 0:796d0f61a05b | 240 | memory_header *new, *cur = heap.first_free; |
HannesTschofenig | 0:796d0f61a05b | 241 | unsigned char *p; |
HannesTschofenig | 0:796d0f61a05b | 242 | #if defined(POLARSSL_MEMORY_BACKTRACE) |
HannesTschofenig | 0:796d0f61a05b | 243 | void *trace_buffer[MAX_BT]; |
HannesTschofenig | 0:796d0f61a05b | 244 | size_t trace_cnt; |
HannesTschofenig | 0:796d0f61a05b | 245 | #endif |
HannesTschofenig | 0:796d0f61a05b | 246 | |
HannesTschofenig | 0:796d0f61a05b | 247 | if( heap.buf == NULL || heap.first == NULL ) |
HannesTschofenig | 0:796d0f61a05b | 248 | return( NULL ); |
HannesTschofenig | 0:796d0f61a05b | 249 | |
HannesTschofenig | 0:796d0f61a05b | 250 | if( len % POLARSSL_MEMORY_ALIGN_MULTIPLE ) |
HannesTschofenig | 0:796d0f61a05b | 251 | { |
HannesTschofenig | 0:796d0f61a05b | 252 | len -= len % POLARSSL_MEMORY_ALIGN_MULTIPLE; |
HannesTschofenig | 0:796d0f61a05b | 253 | len += POLARSSL_MEMORY_ALIGN_MULTIPLE; |
HannesTschofenig | 0:796d0f61a05b | 254 | } |
HannesTschofenig | 0:796d0f61a05b | 255 | |
HannesTschofenig | 0:796d0f61a05b | 256 | // Find block that fits |
HannesTschofenig | 0:796d0f61a05b | 257 | // |
HannesTschofenig | 0:796d0f61a05b | 258 | while( cur != NULL ) |
HannesTschofenig | 0:796d0f61a05b | 259 | { |
HannesTschofenig | 0:796d0f61a05b | 260 | if( cur->size >= len ) |
HannesTschofenig | 0:796d0f61a05b | 261 | break; |
HannesTschofenig | 0:796d0f61a05b | 262 | |
HannesTschofenig | 0:796d0f61a05b | 263 | cur = cur->next_free; |
HannesTschofenig | 0:796d0f61a05b | 264 | } |
HannesTschofenig | 0:796d0f61a05b | 265 | |
HannesTschofenig | 0:796d0f61a05b | 266 | if( cur == NULL ) |
HannesTschofenig | 0:796d0f61a05b | 267 | return( NULL ); |
HannesTschofenig | 0:796d0f61a05b | 268 | |
HannesTschofenig | 0:796d0f61a05b | 269 | if( cur->alloc != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 270 | { |
HannesTschofenig | 0:796d0f61a05b | 271 | #if defined(POLARSSL_MEMORY_DEBUG) |
HannesTschofenig | 0:796d0f61a05b | 272 | polarssl_fprintf( stderr, "FATAL: block in free_list but allocated " |
HannesTschofenig | 0:796d0f61a05b | 273 | "data\n" ); |
HannesTschofenig | 0:796d0f61a05b | 274 | #endif |
HannesTschofenig | 0:796d0f61a05b | 275 | exit( 1 ); |
HannesTschofenig | 0:796d0f61a05b | 276 | } |
HannesTschofenig | 0:796d0f61a05b | 277 | |
HannesTschofenig | 0:796d0f61a05b | 278 | #if defined(POLARSSL_MEMORY_DEBUG) |
HannesTschofenig | 0:796d0f61a05b | 279 | heap.malloc_count++; |
HannesTschofenig | 0:796d0f61a05b | 280 | #endif |
HannesTschofenig | 0:796d0f61a05b | 281 | |
HannesTschofenig | 0:796d0f61a05b | 282 | // Found location, split block if > memory_header + 4 room left |
HannesTschofenig | 0:796d0f61a05b | 283 | // |
HannesTschofenig | 0:796d0f61a05b | 284 | if( cur->size - len < sizeof(memory_header) + |
HannesTschofenig | 0:796d0f61a05b | 285 | POLARSSL_MEMORY_ALIGN_MULTIPLE ) |
HannesTschofenig | 0:796d0f61a05b | 286 | { |
HannesTschofenig | 0:796d0f61a05b | 287 | cur->alloc = 1; |
HannesTschofenig | 0:796d0f61a05b | 288 | |
HannesTschofenig | 0:796d0f61a05b | 289 | // Remove from free_list |
HannesTschofenig | 0:796d0f61a05b | 290 | // |
HannesTschofenig | 0:796d0f61a05b | 291 | if( cur->prev_free != NULL ) |
HannesTschofenig | 0:796d0f61a05b | 292 | cur->prev_free->next_free = cur->next_free; |
HannesTschofenig | 0:796d0f61a05b | 293 | else |
HannesTschofenig | 0:796d0f61a05b | 294 | heap.first_free = cur->next_free; |
HannesTschofenig | 0:796d0f61a05b | 295 | |
HannesTschofenig | 0:796d0f61a05b | 296 | if( cur->next_free != NULL ) |
HannesTschofenig | 0:796d0f61a05b | 297 | cur->next_free->prev_free = cur->prev_free; |
HannesTschofenig | 0:796d0f61a05b | 298 | |
HannesTschofenig | 0:796d0f61a05b | 299 | cur->prev_free = NULL; |
HannesTschofenig | 0:796d0f61a05b | 300 | cur->next_free = NULL; |
HannesTschofenig | 0:796d0f61a05b | 301 | |
HannesTschofenig | 0:796d0f61a05b | 302 | #if defined(POLARSSL_MEMORY_DEBUG) |
HannesTschofenig | 0:796d0f61a05b | 303 | heap.total_used += cur->size; |
HannesTschofenig | 0:796d0f61a05b | 304 | if( heap.total_used > heap.maximum_used) |
HannesTschofenig | 0:796d0f61a05b | 305 | heap.maximum_used = heap.total_used; |
HannesTschofenig | 0:796d0f61a05b | 306 | #endif |
HannesTschofenig | 0:796d0f61a05b | 307 | #if defined(POLARSSL_MEMORY_BACKTRACE) |
HannesTschofenig | 0:796d0f61a05b | 308 | trace_cnt = backtrace( trace_buffer, MAX_BT ); |
HannesTschofenig | 0:796d0f61a05b | 309 | cur->trace = backtrace_symbols( trace_buffer, trace_cnt ); |
HannesTschofenig | 0:796d0f61a05b | 310 | cur->trace_count = trace_cnt; |
HannesTschofenig | 0:796d0f61a05b | 311 | #endif |
HannesTschofenig | 0:796d0f61a05b | 312 | |
HannesTschofenig | 0:796d0f61a05b | 313 | if( ( heap.verify & MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 314 | exit( 1 ); |
HannesTschofenig | 0:796d0f61a05b | 315 | |
HannesTschofenig | 0:796d0f61a05b | 316 | return ( (unsigned char *) cur ) + sizeof(memory_header); |
HannesTschofenig | 0:796d0f61a05b | 317 | } |
HannesTschofenig | 0:796d0f61a05b | 318 | |
HannesTschofenig | 0:796d0f61a05b | 319 | p = ( (unsigned char *) cur ) + sizeof(memory_header) + len; |
HannesTschofenig | 0:796d0f61a05b | 320 | new = (memory_header *) p; |
HannesTschofenig | 0:796d0f61a05b | 321 | |
HannesTschofenig | 0:796d0f61a05b | 322 | new->size = cur->size - len - sizeof(memory_header); |
HannesTschofenig | 0:796d0f61a05b | 323 | new->alloc = 0; |
HannesTschofenig | 0:796d0f61a05b | 324 | new->prev = cur; |
HannesTschofenig | 0:796d0f61a05b | 325 | new->next = cur->next; |
HannesTschofenig | 0:796d0f61a05b | 326 | #if defined(POLARSSL_MEMORY_BACKTRACE) |
HannesTschofenig | 0:796d0f61a05b | 327 | new->trace = NULL; |
HannesTschofenig | 0:796d0f61a05b | 328 | new->trace_count = 0; |
HannesTschofenig | 0:796d0f61a05b | 329 | #endif |
HannesTschofenig | 0:796d0f61a05b | 330 | new->magic1 = MAGIC1; |
HannesTschofenig | 0:796d0f61a05b | 331 | new->magic2 = MAGIC2; |
HannesTschofenig | 0:796d0f61a05b | 332 | |
HannesTschofenig | 0:796d0f61a05b | 333 | if( new->next != NULL ) |
HannesTschofenig | 0:796d0f61a05b | 334 | new->next->prev = new; |
HannesTschofenig | 0:796d0f61a05b | 335 | |
HannesTschofenig | 0:796d0f61a05b | 336 | // Replace cur with new in free_list |
HannesTschofenig | 0:796d0f61a05b | 337 | // |
HannesTschofenig | 0:796d0f61a05b | 338 | new->prev_free = cur->prev_free; |
HannesTschofenig | 0:796d0f61a05b | 339 | new->next_free = cur->next_free; |
HannesTschofenig | 0:796d0f61a05b | 340 | if( new->prev_free != NULL ) |
HannesTschofenig | 0:796d0f61a05b | 341 | new->prev_free->next_free = new; |
HannesTschofenig | 0:796d0f61a05b | 342 | else |
HannesTschofenig | 0:796d0f61a05b | 343 | heap.first_free = new; |
HannesTschofenig | 0:796d0f61a05b | 344 | |
HannesTschofenig | 0:796d0f61a05b | 345 | if( new->next_free != NULL ) |
HannesTschofenig | 0:796d0f61a05b | 346 | new->next_free->prev_free = new; |
HannesTschofenig | 0:796d0f61a05b | 347 | |
HannesTschofenig | 0:796d0f61a05b | 348 | cur->alloc = 1; |
HannesTschofenig | 0:796d0f61a05b | 349 | cur->size = len; |
HannesTschofenig | 0:796d0f61a05b | 350 | cur->next = new; |
HannesTschofenig | 0:796d0f61a05b | 351 | cur->prev_free = NULL; |
HannesTschofenig | 0:796d0f61a05b | 352 | cur->next_free = NULL; |
HannesTschofenig | 0:796d0f61a05b | 353 | |
HannesTschofenig | 0:796d0f61a05b | 354 | #if defined(POLARSSL_MEMORY_DEBUG) |
HannesTschofenig | 0:796d0f61a05b | 355 | heap.header_count++; |
HannesTschofenig | 0:796d0f61a05b | 356 | if( heap.header_count > heap.maximum_header_count ) |
HannesTschofenig | 0:796d0f61a05b | 357 | heap.maximum_header_count = heap.header_count; |
HannesTschofenig | 0:796d0f61a05b | 358 | heap.total_used += cur->size; |
HannesTschofenig | 0:796d0f61a05b | 359 | if( heap.total_used > heap.maximum_used) |
HannesTschofenig | 0:796d0f61a05b | 360 | heap.maximum_used = heap.total_used; |
HannesTschofenig | 0:796d0f61a05b | 361 | #endif |
HannesTschofenig | 0:796d0f61a05b | 362 | #if defined(POLARSSL_MEMORY_BACKTRACE) |
HannesTschofenig | 0:796d0f61a05b | 363 | trace_cnt = backtrace( trace_buffer, MAX_BT ); |
HannesTschofenig | 0:796d0f61a05b | 364 | cur->trace = backtrace_symbols( trace_buffer, trace_cnt ); |
HannesTschofenig | 0:796d0f61a05b | 365 | cur->trace_count = trace_cnt; |
HannesTschofenig | 0:796d0f61a05b | 366 | #endif |
HannesTschofenig | 0:796d0f61a05b | 367 | |
HannesTschofenig | 0:796d0f61a05b | 368 | if( ( heap.verify & MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 369 | exit( 1 ); |
HannesTschofenig | 0:796d0f61a05b | 370 | |
HannesTschofenig | 0:796d0f61a05b | 371 | return ( (unsigned char *) cur ) + sizeof(memory_header); |
HannesTschofenig | 0:796d0f61a05b | 372 | } |
HannesTschofenig | 0:796d0f61a05b | 373 | |
HannesTschofenig | 0:796d0f61a05b | 374 | static void buffer_alloc_free( void *ptr ) |
HannesTschofenig | 0:796d0f61a05b | 375 | { |
HannesTschofenig | 0:796d0f61a05b | 376 | memory_header *hdr, *old = NULL; |
HannesTschofenig | 0:796d0f61a05b | 377 | unsigned char *p = (unsigned char *) ptr; |
HannesTschofenig | 0:796d0f61a05b | 378 | |
HannesTschofenig | 0:796d0f61a05b | 379 | if( ptr == NULL || heap.buf == NULL || heap.first == NULL ) |
HannesTschofenig | 0:796d0f61a05b | 380 | return; |
HannesTschofenig | 0:796d0f61a05b | 381 | |
HannesTschofenig | 0:796d0f61a05b | 382 | if( p < heap.buf || p > heap.buf + heap.len ) |
HannesTschofenig | 0:796d0f61a05b | 383 | { |
HannesTschofenig | 0:796d0f61a05b | 384 | #if defined(POLARSSL_MEMORY_DEBUG) |
HannesTschofenig | 0:796d0f61a05b | 385 | polarssl_fprintf( stderr, "FATAL: polarssl_free() outside of managed " |
HannesTschofenig | 0:796d0f61a05b | 386 | "space\n" ); |
HannesTschofenig | 0:796d0f61a05b | 387 | #endif |
HannesTschofenig | 0:796d0f61a05b | 388 | exit( 1 ); |
HannesTschofenig | 0:796d0f61a05b | 389 | } |
HannesTschofenig | 0:796d0f61a05b | 390 | |
HannesTschofenig | 0:796d0f61a05b | 391 | p -= sizeof(memory_header); |
HannesTschofenig | 0:796d0f61a05b | 392 | hdr = (memory_header *) p; |
HannesTschofenig | 0:796d0f61a05b | 393 | |
HannesTschofenig | 0:796d0f61a05b | 394 | if( verify_header( hdr ) != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 395 | exit( 1 ); |
HannesTschofenig | 0:796d0f61a05b | 396 | |
HannesTschofenig | 0:796d0f61a05b | 397 | if( hdr->alloc != 1 ) |
HannesTschofenig | 0:796d0f61a05b | 398 | { |
HannesTschofenig | 0:796d0f61a05b | 399 | #if defined(POLARSSL_MEMORY_DEBUG) |
HannesTschofenig | 0:796d0f61a05b | 400 | polarssl_fprintf( stderr, "FATAL: polarssl_free() on unallocated " |
HannesTschofenig | 0:796d0f61a05b | 401 | "data\n" ); |
HannesTschofenig | 0:796d0f61a05b | 402 | #endif |
HannesTschofenig | 0:796d0f61a05b | 403 | exit( 1 ); |
HannesTschofenig | 0:796d0f61a05b | 404 | } |
HannesTschofenig | 0:796d0f61a05b | 405 | |
HannesTschofenig | 0:796d0f61a05b | 406 | hdr->alloc = 0; |
HannesTschofenig | 0:796d0f61a05b | 407 | |
HannesTschofenig | 0:796d0f61a05b | 408 | #if defined(POLARSSL_MEMORY_DEBUG) |
HannesTschofenig | 0:796d0f61a05b | 409 | heap.free_count++; |
HannesTschofenig | 0:796d0f61a05b | 410 | heap.total_used -= hdr->size; |
HannesTschofenig | 0:796d0f61a05b | 411 | #endif |
HannesTschofenig | 0:796d0f61a05b | 412 | |
HannesTschofenig | 0:796d0f61a05b | 413 | // Regroup with block before |
HannesTschofenig | 0:796d0f61a05b | 414 | // |
HannesTschofenig | 0:796d0f61a05b | 415 | if( hdr->prev != NULL && hdr->prev->alloc == 0 ) |
HannesTschofenig | 0:796d0f61a05b | 416 | { |
HannesTschofenig | 0:796d0f61a05b | 417 | #if defined(POLARSSL_MEMORY_DEBUG) |
HannesTschofenig | 0:796d0f61a05b | 418 | heap.header_count--; |
HannesTschofenig | 0:796d0f61a05b | 419 | #endif |
HannesTschofenig | 0:796d0f61a05b | 420 | hdr->prev->size += sizeof(memory_header) + hdr->size; |
HannesTschofenig | 0:796d0f61a05b | 421 | hdr->prev->next = hdr->next; |
HannesTschofenig | 0:796d0f61a05b | 422 | old = hdr; |
HannesTschofenig | 0:796d0f61a05b | 423 | hdr = hdr->prev; |
HannesTschofenig | 0:796d0f61a05b | 424 | |
HannesTschofenig | 0:796d0f61a05b | 425 | if( hdr->next != NULL ) |
HannesTschofenig | 0:796d0f61a05b | 426 | hdr->next->prev = hdr; |
HannesTschofenig | 0:796d0f61a05b | 427 | |
HannesTschofenig | 0:796d0f61a05b | 428 | #if defined(POLARSSL_MEMORY_BACKTRACE) |
HannesTschofenig | 0:796d0f61a05b | 429 | free( old->trace ); |
HannesTschofenig | 0:796d0f61a05b | 430 | #endif |
HannesTschofenig | 0:796d0f61a05b | 431 | memset( old, 0, sizeof(memory_header) ); |
HannesTschofenig | 0:796d0f61a05b | 432 | } |
HannesTschofenig | 0:796d0f61a05b | 433 | |
HannesTschofenig | 0:796d0f61a05b | 434 | // Regroup with block after |
HannesTschofenig | 0:796d0f61a05b | 435 | // |
HannesTschofenig | 0:796d0f61a05b | 436 | if( hdr->next != NULL && hdr->next->alloc == 0 ) |
HannesTschofenig | 0:796d0f61a05b | 437 | { |
HannesTschofenig | 0:796d0f61a05b | 438 | #if defined(POLARSSL_MEMORY_DEBUG) |
HannesTschofenig | 0:796d0f61a05b | 439 | heap.header_count--; |
HannesTschofenig | 0:796d0f61a05b | 440 | #endif |
HannesTschofenig | 0:796d0f61a05b | 441 | hdr->size += sizeof(memory_header) + hdr->next->size; |
HannesTschofenig | 0:796d0f61a05b | 442 | old = hdr->next; |
HannesTschofenig | 0:796d0f61a05b | 443 | hdr->next = hdr->next->next; |
HannesTschofenig | 0:796d0f61a05b | 444 | |
HannesTschofenig | 0:796d0f61a05b | 445 | if( hdr->prev_free != NULL || hdr->next_free != NULL ) |
HannesTschofenig | 0:796d0f61a05b | 446 | { |
HannesTschofenig | 0:796d0f61a05b | 447 | if( hdr->prev_free != NULL ) |
HannesTschofenig | 0:796d0f61a05b | 448 | hdr->prev_free->next_free = hdr->next_free; |
HannesTschofenig | 0:796d0f61a05b | 449 | else |
HannesTschofenig | 0:796d0f61a05b | 450 | heap.first_free = hdr->next_free; |
HannesTschofenig | 0:796d0f61a05b | 451 | |
HannesTschofenig | 0:796d0f61a05b | 452 | if( hdr->next_free != NULL ) |
HannesTschofenig | 0:796d0f61a05b | 453 | hdr->next_free->prev_free = hdr->prev_free; |
HannesTschofenig | 0:796d0f61a05b | 454 | } |
HannesTschofenig | 0:796d0f61a05b | 455 | |
HannesTschofenig | 0:796d0f61a05b | 456 | hdr->prev_free = old->prev_free; |
HannesTschofenig | 0:796d0f61a05b | 457 | hdr->next_free = old->next_free; |
HannesTschofenig | 0:796d0f61a05b | 458 | |
HannesTschofenig | 0:796d0f61a05b | 459 | if( hdr->prev_free != NULL ) |
HannesTschofenig | 0:796d0f61a05b | 460 | hdr->prev_free->next_free = hdr; |
HannesTschofenig | 0:796d0f61a05b | 461 | else |
HannesTschofenig | 0:796d0f61a05b | 462 | heap.first_free = hdr; |
HannesTschofenig | 0:796d0f61a05b | 463 | |
HannesTschofenig | 0:796d0f61a05b | 464 | if( hdr->next_free != NULL ) |
HannesTschofenig | 0:796d0f61a05b | 465 | hdr->next_free->prev_free = hdr; |
HannesTschofenig | 0:796d0f61a05b | 466 | |
HannesTschofenig | 0:796d0f61a05b | 467 | if( hdr->next != NULL ) |
HannesTschofenig | 0:796d0f61a05b | 468 | hdr->next->prev = hdr; |
HannesTschofenig | 0:796d0f61a05b | 469 | |
HannesTschofenig | 0:796d0f61a05b | 470 | #if defined(POLARSSL_MEMORY_BACKTRACE) |
HannesTschofenig | 0:796d0f61a05b | 471 | free( old->trace ); |
HannesTschofenig | 0:796d0f61a05b | 472 | #endif |
HannesTschofenig | 0:796d0f61a05b | 473 | memset( old, 0, sizeof(memory_header) ); |
HannesTschofenig | 0:796d0f61a05b | 474 | } |
HannesTschofenig | 0:796d0f61a05b | 475 | |
HannesTschofenig | 0:796d0f61a05b | 476 | // Prepend to free_list if we have not merged |
HannesTschofenig | 0:796d0f61a05b | 477 | // (Does not have to stay in same order as prev / next list) |
HannesTschofenig | 0:796d0f61a05b | 478 | // |
HannesTschofenig | 0:796d0f61a05b | 479 | if( old == NULL ) |
HannesTschofenig | 0:796d0f61a05b | 480 | { |
HannesTschofenig | 0:796d0f61a05b | 481 | hdr->next_free = heap.first_free; |
HannesTschofenig | 0:796d0f61a05b | 482 | heap.first_free->prev_free = hdr; |
HannesTschofenig | 0:796d0f61a05b | 483 | heap.first_free = hdr; |
HannesTschofenig | 0:796d0f61a05b | 484 | } |
HannesTschofenig | 0:796d0f61a05b | 485 | |
HannesTschofenig | 0:796d0f61a05b | 486 | #if defined(POLARSSL_MEMORY_BACKTRACE) |
HannesTschofenig | 0:796d0f61a05b | 487 | hdr->trace = NULL; |
HannesTschofenig | 0:796d0f61a05b | 488 | hdr->trace_count = 0; |
HannesTschofenig | 0:796d0f61a05b | 489 | #endif |
HannesTschofenig | 0:796d0f61a05b | 490 | |
HannesTschofenig | 0:796d0f61a05b | 491 | if( ( heap.verify & MEMORY_VERIFY_FREE ) && verify_chain() != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 492 | exit( 1 ); |
HannesTschofenig | 0:796d0f61a05b | 493 | } |
HannesTschofenig | 0:796d0f61a05b | 494 | |
HannesTschofenig | 0:796d0f61a05b | 495 | void memory_buffer_set_verify( int verify ) |
HannesTschofenig | 0:796d0f61a05b | 496 | { |
HannesTschofenig | 0:796d0f61a05b | 497 | heap.verify = verify; |
HannesTschofenig | 0:796d0f61a05b | 498 | } |
HannesTschofenig | 0:796d0f61a05b | 499 | |
HannesTschofenig | 0:796d0f61a05b | 500 | int memory_buffer_alloc_verify() |
HannesTschofenig | 0:796d0f61a05b | 501 | { |
HannesTschofenig | 0:796d0f61a05b | 502 | return verify_chain(); |
HannesTschofenig | 0:796d0f61a05b | 503 | } |
HannesTschofenig | 0:796d0f61a05b | 504 | |
HannesTschofenig | 0:796d0f61a05b | 505 | #if defined(POLARSSL_MEMORY_DEBUG) |
HannesTschofenig | 0:796d0f61a05b | 506 | void memory_buffer_alloc_status() |
HannesTschofenig | 0:796d0f61a05b | 507 | { |
HannesTschofenig | 0:796d0f61a05b | 508 | polarssl_fprintf( stderr, |
HannesTschofenig | 0:796d0f61a05b | 509 | "Current use: %u blocks / %u bytes, max: %u blocks / " |
HannesTschofenig | 0:796d0f61a05b | 510 | "%u bytes (total %u bytes), malloc / free: %u / %u\n", |
HannesTschofenig | 0:796d0f61a05b | 511 | heap.header_count, heap.total_used, |
HannesTschofenig | 0:796d0f61a05b | 512 | heap.maximum_header_count, heap.maximum_used, |
HannesTschofenig | 0:796d0f61a05b | 513 | heap.maximum_header_count * sizeof( memory_header ) |
HannesTschofenig | 0:796d0f61a05b | 514 | + heap.maximum_used, |
HannesTschofenig | 0:796d0f61a05b | 515 | heap.malloc_count, heap.free_count ); |
HannesTschofenig | 0:796d0f61a05b | 516 | |
HannesTschofenig | 0:796d0f61a05b | 517 | if( heap.first->next == NULL ) |
HannesTschofenig | 0:796d0f61a05b | 518 | polarssl_fprintf( stderr, "All memory de-allocated in stack buffer\n" ); |
HannesTschofenig | 0:796d0f61a05b | 519 | else |
HannesTschofenig | 0:796d0f61a05b | 520 | { |
HannesTschofenig | 0:796d0f61a05b | 521 | polarssl_fprintf( stderr, "Memory currently allocated:\n" ); |
HannesTschofenig | 0:796d0f61a05b | 522 | debug_chain(); |
HannesTschofenig | 0:796d0f61a05b | 523 | } |
HannesTschofenig | 0:796d0f61a05b | 524 | } |
HannesTschofenig | 0:796d0f61a05b | 525 | #endif /* POLARSSL_MEMORY_DEBUG */ |
HannesTschofenig | 0:796d0f61a05b | 526 | |
HannesTschofenig | 0:796d0f61a05b | 527 | #if defined(POLARSSL_THREADING_C) |
HannesTschofenig | 0:796d0f61a05b | 528 | static void *buffer_alloc_malloc_mutexed( size_t len ) |
HannesTschofenig | 0:796d0f61a05b | 529 | { |
HannesTschofenig | 0:796d0f61a05b | 530 | void *buf; |
HannesTschofenig | 0:796d0f61a05b | 531 | polarssl_mutex_lock( &heap.mutex ); |
HannesTschofenig | 0:796d0f61a05b | 532 | buf = buffer_alloc_malloc( len ); |
HannesTschofenig | 0:796d0f61a05b | 533 | polarssl_mutex_unlock( &heap.mutex ); |
HannesTschofenig | 0:796d0f61a05b | 534 | return( buf ); |
HannesTschofenig | 0:796d0f61a05b | 535 | } |
HannesTschofenig | 0:796d0f61a05b | 536 | |
HannesTschofenig | 0:796d0f61a05b | 537 | static void buffer_alloc_free_mutexed( void *ptr ) |
HannesTschofenig | 0:796d0f61a05b | 538 | { |
HannesTschofenig | 0:796d0f61a05b | 539 | polarssl_mutex_lock( &heap.mutex ); |
HannesTschofenig | 0:796d0f61a05b | 540 | buffer_alloc_free( ptr ); |
HannesTschofenig | 0:796d0f61a05b | 541 | polarssl_mutex_unlock( &heap.mutex ); |
HannesTschofenig | 0:796d0f61a05b | 542 | } |
HannesTschofenig | 0:796d0f61a05b | 543 | #endif /* POLARSSL_THREADING_C */ |
HannesTschofenig | 0:796d0f61a05b | 544 | |
HannesTschofenig | 0:796d0f61a05b | 545 | int memory_buffer_alloc_init( unsigned char *buf, size_t len ) |
HannesTschofenig | 0:796d0f61a05b | 546 | { |
HannesTschofenig | 0:796d0f61a05b | 547 | memset( &heap, 0, sizeof(buffer_alloc_ctx) ); |
HannesTschofenig | 0:796d0f61a05b | 548 | memset( buf, 0, len ); |
HannesTschofenig | 0:796d0f61a05b | 549 | |
HannesTschofenig | 0:796d0f61a05b | 550 | #if defined(POLARSSL_THREADING_C) |
HannesTschofenig | 0:796d0f61a05b | 551 | polarssl_mutex_init( &heap.mutex ); |
HannesTschofenig | 0:796d0f61a05b | 552 | platform_set_malloc_free( buffer_alloc_malloc_mutexed, |
HannesTschofenig | 0:796d0f61a05b | 553 | buffer_alloc_free_mutexed ); |
HannesTschofenig | 0:796d0f61a05b | 554 | #else |
HannesTschofenig | 0:796d0f61a05b | 555 | platform_set_malloc_free( buffer_alloc_malloc, buffer_alloc_free ); |
HannesTschofenig | 0:796d0f61a05b | 556 | #endif |
HannesTschofenig | 0:796d0f61a05b | 557 | |
HannesTschofenig | 0:796d0f61a05b | 558 | heap.buf = buf; |
HannesTschofenig | 0:796d0f61a05b | 559 | heap.len = len; |
HannesTschofenig | 0:796d0f61a05b | 560 | |
HannesTschofenig | 0:796d0f61a05b | 561 | heap.first = (memory_header *) buf; |
HannesTschofenig | 0:796d0f61a05b | 562 | heap.first->size = len - sizeof(memory_header); |
HannesTschofenig | 0:796d0f61a05b | 563 | heap.first->magic1 = MAGIC1; |
HannesTschofenig | 0:796d0f61a05b | 564 | heap.first->magic2 = MAGIC2; |
HannesTschofenig | 0:796d0f61a05b | 565 | heap.first_free = heap.first; |
HannesTschofenig | 0:796d0f61a05b | 566 | return( 0 ); |
HannesTschofenig | 0:796d0f61a05b | 567 | } |
HannesTschofenig | 0:796d0f61a05b | 568 | |
HannesTschofenig | 0:796d0f61a05b | 569 | void memory_buffer_alloc_free() |
HannesTschofenig | 0:796d0f61a05b | 570 | { |
HannesTschofenig | 0:796d0f61a05b | 571 | #if defined(POLARSSL_THREADING_C) |
HannesTschofenig | 0:796d0f61a05b | 572 | polarssl_mutex_free( &heap.mutex ); |
HannesTschofenig | 0:796d0f61a05b | 573 | #endif |
HannesTschofenig | 0:796d0f61a05b | 574 | memset( &heap, 0, sizeof(buffer_alloc_ctx) ); |
HannesTschofenig | 0:796d0f61a05b | 575 | } |
HannesTschofenig | 0:796d0f61a05b | 576 | |
HannesTschofenig | 0:796d0f61a05b | 577 | #endif /* POLARSSL_MEMORY_BUFFER_ALLOC_C */ |
HannesTschofenig | 0:796d0f61a05b | 578 | |
HannesTschofenig | 0:796d0f61a05b | 579 |