I added functionality to get the RSSI, BER, and Cell Neighbor for reporting connection issues to M2X

Dependencies:   WncControllerK64F

Committer:
JMF
Date:
Thu Nov 17 16:13:29 2016 +0000
Revision:
18:198e9b0acf11
Parent:
12:0071cb144c7a
Updates to mbed os resulted in mutex.h going away and rtos.h needed to be used; This fixes the Mutex typedef failure.  Also cast data buffers from 'char *' to (const std::uint8_t*) to conform with Fred's changes in WncController

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JMF 12:0071cb144c7a 1 /*
JMF 12:0071cb144c7a 2 * Buffer-based memory allocator
JMF 12:0071cb144c7a 3 *
JMF 12:0071cb144c7a 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
JMF 12:0071cb144c7a 5 * SPDX-License-Identifier: Apache-2.0
JMF 12:0071cb144c7a 6 *
JMF 12:0071cb144c7a 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
JMF 12:0071cb144c7a 8 * not use this file except in compliance with the License.
JMF 12:0071cb144c7a 9 * You may obtain a copy of the License at
JMF 12:0071cb144c7a 10 *
JMF 12:0071cb144c7a 11 * http://www.apache.org/licenses/LICENSE-2.0
JMF 12:0071cb144c7a 12 *
JMF 12:0071cb144c7a 13 * Unless required by applicable law or agreed to in writing, software
JMF 12:0071cb144c7a 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
JMF 12:0071cb144c7a 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
JMF 12:0071cb144c7a 16 * See the License for the specific language governing permissions and
JMF 12:0071cb144c7a 17 * limitations under the License.
JMF 12:0071cb144c7a 18 *
JMF 12:0071cb144c7a 19 * This file is part of mbed TLS (https://tls.mbed.org)
JMF 12:0071cb144c7a 20 */
JMF 12:0071cb144c7a 21
JMF 12:0071cb144c7a 22 #if !defined(MBEDTLS_CONFIG_FILE)
JMF 12:0071cb144c7a 23 #include "mbedtls/config.h"
JMF 12:0071cb144c7a 24 #else
JMF 12:0071cb144c7a 25 #include MBEDTLS_CONFIG_FILE
JMF 12:0071cb144c7a 26 #endif
JMF 12:0071cb144c7a 27
JMF 12:0071cb144c7a 28 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
JMF 12:0071cb144c7a 29 #include "mbedtls/memory_buffer_alloc.h"
JMF 12:0071cb144c7a 30
JMF 12:0071cb144c7a 31 /* No need for the header guard as MBEDTLS_MEMORY_BUFFER_ALLOC_C
JMF 12:0071cb144c7a 32 is dependent upon MBEDTLS_PLATFORM_C */
JMF 12:0071cb144c7a 33 #include "mbedtls/platform.h"
JMF 12:0071cb144c7a 34
JMF 12:0071cb144c7a 35 #include <string.h>
JMF 12:0071cb144c7a 36
JMF 12:0071cb144c7a 37 #if defined(MBEDTLS_MEMORY_BACKTRACE)
JMF 12:0071cb144c7a 38 #include <execinfo.h>
JMF 12:0071cb144c7a 39 #endif
JMF 12:0071cb144c7a 40
JMF 12:0071cb144c7a 41 #if defined(MBEDTLS_THREADING_C)
JMF 12:0071cb144c7a 42 #include "mbedtls/threading.h"
JMF 12:0071cb144c7a 43 #endif
JMF 12:0071cb144c7a 44
JMF 12:0071cb144c7a 45 /* Implementation that should never be optimized out by the compiler */
JMF 12:0071cb144c7a 46 static void mbedtls_zeroize( void *v, size_t n ) {
JMF 12:0071cb144c7a 47 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
JMF 12:0071cb144c7a 48 }
JMF 12:0071cb144c7a 49
JMF 12:0071cb144c7a 50 #define MAGIC1 0xFF00AA55
JMF 12:0071cb144c7a 51 #define MAGIC2 0xEE119966
JMF 12:0071cb144c7a 52 #define MAX_BT 20
JMF 12:0071cb144c7a 53
JMF 12:0071cb144c7a 54 typedef struct _memory_header memory_header;
JMF 12:0071cb144c7a 55 struct _memory_header
JMF 12:0071cb144c7a 56 {
JMF 12:0071cb144c7a 57 size_t magic1;
JMF 12:0071cb144c7a 58 size_t size;
JMF 12:0071cb144c7a 59 size_t alloc;
JMF 12:0071cb144c7a 60 memory_header *prev;
JMF 12:0071cb144c7a 61 memory_header *next;
JMF 12:0071cb144c7a 62 memory_header *prev_free;
JMF 12:0071cb144c7a 63 memory_header *next_free;
JMF 12:0071cb144c7a 64 #if defined(MBEDTLS_MEMORY_BACKTRACE)
JMF 12:0071cb144c7a 65 char **trace;
JMF 12:0071cb144c7a 66 size_t trace_count;
JMF 12:0071cb144c7a 67 #endif
JMF 12:0071cb144c7a 68 size_t magic2;
JMF 12:0071cb144c7a 69 };
JMF 12:0071cb144c7a 70
JMF 12:0071cb144c7a 71 typedef struct
JMF 12:0071cb144c7a 72 {
JMF 12:0071cb144c7a 73 unsigned char *buf;
JMF 12:0071cb144c7a 74 size_t len;
JMF 12:0071cb144c7a 75 memory_header *first;
JMF 12:0071cb144c7a 76 memory_header *first_free;
JMF 12:0071cb144c7a 77 int verify;
JMF 12:0071cb144c7a 78 #if defined(MBEDTLS_MEMORY_DEBUG)
JMF 12:0071cb144c7a 79 size_t alloc_count;
JMF 12:0071cb144c7a 80 size_t free_count;
JMF 12:0071cb144c7a 81 size_t total_used;
JMF 12:0071cb144c7a 82 size_t maximum_used;
JMF 12:0071cb144c7a 83 size_t header_count;
JMF 12:0071cb144c7a 84 size_t maximum_header_count;
JMF 12:0071cb144c7a 85 #endif
JMF 12:0071cb144c7a 86 #if defined(MBEDTLS_THREADING_C)
JMF 12:0071cb144c7a 87 mbedtls_threading_mutex_t mutex;
JMF 12:0071cb144c7a 88 #endif
JMF 12:0071cb144c7a 89 }
JMF 12:0071cb144c7a 90 buffer_alloc_ctx;
JMF 12:0071cb144c7a 91
JMF 12:0071cb144c7a 92 static buffer_alloc_ctx heap;
JMF 12:0071cb144c7a 93
JMF 12:0071cb144c7a 94 #if defined(MBEDTLS_MEMORY_DEBUG)
JMF 12:0071cb144c7a 95 static void debug_header( memory_header *hdr )
JMF 12:0071cb144c7a 96 {
JMF 12:0071cb144c7a 97 #if defined(MBEDTLS_MEMORY_BACKTRACE)
JMF 12:0071cb144c7a 98 size_t i;
JMF 12:0071cb144c7a 99 #endif
JMF 12:0071cb144c7a 100
JMF 12:0071cb144c7a 101 mbedtls_fprintf( stderr, "HDR: PTR(%10zu), PREV(%10zu), NEXT(%10zu), "
JMF 12:0071cb144c7a 102 "ALLOC(%zu), SIZE(%10zu)\n",
JMF 12:0071cb144c7a 103 (size_t) hdr, (size_t) hdr->prev, (size_t) hdr->next,
JMF 12:0071cb144c7a 104 hdr->alloc, hdr->size );
JMF 12:0071cb144c7a 105 mbedtls_fprintf( stderr, " FPREV(%10zu), FNEXT(%10zu)\n",
JMF 12:0071cb144c7a 106 (size_t) hdr->prev_free, (size_t) hdr->next_free );
JMF 12:0071cb144c7a 107
JMF 12:0071cb144c7a 108 #if defined(MBEDTLS_MEMORY_BACKTRACE)
JMF 12:0071cb144c7a 109 mbedtls_fprintf( stderr, "TRACE: \n" );
JMF 12:0071cb144c7a 110 for( i = 0; i < hdr->trace_count; i++ )
JMF 12:0071cb144c7a 111 mbedtls_fprintf( stderr, "%s\n", hdr->trace[i] );
JMF 12:0071cb144c7a 112 mbedtls_fprintf( stderr, "\n" );
JMF 12:0071cb144c7a 113 #endif
JMF 12:0071cb144c7a 114 }
JMF 12:0071cb144c7a 115
JMF 12:0071cb144c7a 116 static void debug_chain()
JMF 12:0071cb144c7a 117 {
JMF 12:0071cb144c7a 118 memory_header *cur = heap.first;
JMF 12:0071cb144c7a 119
JMF 12:0071cb144c7a 120 mbedtls_fprintf( stderr, "\nBlock list\n" );
JMF 12:0071cb144c7a 121 while( cur != NULL )
JMF 12:0071cb144c7a 122 {
JMF 12:0071cb144c7a 123 debug_header( cur );
JMF 12:0071cb144c7a 124 cur = cur->next;
JMF 12:0071cb144c7a 125 }
JMF 12:0071cb144c7a 126
JMF 12:0071cb144c7a 127 mbedtls_fprintf( stderr, "Free list\n" );
JMF 12:0071cb144c7a 128 cur = heap.first_free;
JMF 12:0071cb144c7a 129
JMF 12:0071cb144c7a 130 while( cur != NULL )
JMF 12:0071cb144c7a 131 {
JMF 12:0071cb144c7a 132 debug_header( cur );
JMF 12:0071cb144c7a 133 cur = cur->next_free;
JMF 12:0071cb144c7a 134 }
JMF 12:0071cb144c7a 135 }
JMF 12:0071cb144c7a 136 #endif /* MBEDTLS_MEMORY_DEBUG */
JMF 12:0071cb144c7a 137
JMF 12:0071cb144c7a 138 static int verify_header( memory_header *hdr )
JMF 12:0071cb144c7a 139 {
JMF 12:0071cb144c7a 140 if( hdr->magic1 != MAGIC1 )
JMF 12:0071cb144c7a 141 {
JMF 12:0071cb144c7a 142 #if defined(MBEDTLS_MEMORY_DEBUG)
JMF 12:0071cb144c7a 143 mbedtls_fprintf( stderr, "FATAL: MAGIC1 mismatch\n" );
JMF 12:0071cb144c7a 144 #endif
JMF 12:0071cb144c7a 145 return( 1 );
JMF 12:0071cb144c7a 146 }
JMF 12:0071cb144c7a 147
JMF 12:0071cb144c7a 148 if( hdr->magic2 != MAGIC2 )
JMF 12:0071cb144c7a 149 {
JMF 12:0071cb144c7a 150 #if defined(MBEDTLS_MEMORY_DEBUG)
JMF 12:0071cb144c7a 151 mbedtls_fprintf( stderr, "FATAL: MAGIC2 mismatch\n" );
JMF 12:0071cb144c7a 152 #endif
JMF 12:0071cb144c7a 153 return( 1 );
JMF 12:0071cb144c7a 154 }
JMF 12:0071cb144c7a 155
JMF 12:0071cb144c7a 156 if( hdr->alloc > 1 )
JMF 12:0071cb144c7a 157 {
JMF 12:0071cb144c7a 158 #if defined(MBEDTLS_MEMORY_DEBUG)
JMF 12:0071cb144c7a 159 mbedtls_fprintf( stderr, "FATAL: alloc has illegal value\n" );
JMF 12:0071cb144c7a 160 #endif
JMF 12:0071cb144c7a 161 return( 1 );
JMF 12:0071cb144c7a 162 }
JMF 12:0071cb144c7a 163
JMF 12:0071cb144c7a 164 if( hdr->prev != NULL && hdr->prev == hdr->next )
JMF 12:0071cb144c7a 165 {
JMF 12:0071cb144c7a 166 #if defined(MBEDTLS_MEMORY_DEBUG)
JMF 12:0071cb144c7a 167 mbedtls_fprintf( stderr, "FATAL: prev == next\n" );
JMF 12:0071cb144c7a 168 #endif
JMF 12:0071cb144c7a 169 return( 1 );
JMF 12:0071cb144c7a 170 }
JMF 12:0071cb144c7a 171
JMF 12:0071cb144c7a 172 if( hdr->prev_free != NULL && hdr->prev_free == hdr->next_free )
JMF 12:0071cb144c7a 173 {
JMF 12:0071cb144c7a 174 #if defined(MBEDTLS_MEMORY_DEBUG)
JMF 12:0071cb144c7a 175 mbedtls_fprintf( stderr, "FATAL: prev_free == next_free\n" );
JMF 12:0071cb144c7a 176 #endif
JMF 12:0071cb144c7a 177 return( 1 );
JMF 12:0071cb144c7a 178 }
JMF 12:0071cb144c7a 179
JMF 12:0071cb144c7a 180 return( 0 );
JMF 12:0071cb144c7a 181 }
JMF 12:0071cb144c7a 182
JMF 12:0071cb144c7a 183 static int verify_chain()
JMF 12:0071cb144c7a 184 {
JMF 12:0071cb144c7a 185 memory_header *prv = heap.first, *cur = heap.first->next;
JMF 12:0071cb144c7a 186
JMF 12:0071cb144c7a 187 if( verify_header( heap.first ) != 0 )
JMF 12:0071cb144c7a 188 {
JMF 12:0071cb144c7a 189 #if defined(MBEDTLS_MEMORY_DEBUG)
JMF 12:0071cb144c7a 190 mbedtls_fprintf( stderr, "FATAL: verification of first header "
JMF 12:0071cb144c7a 191 "failed\n" );
JMF 12:0071cb144c7a 192 #endif
JMF 12:0071cb144c7a 193 return( 1 );
JMF 12:0071cb144c7a 194 }
JMF 12:0071cb144c7a 195
JMF 12:0071cb144c7a 196 if( heap.first->prev != NULL )
JMF 12:0071cb144c7a 197 {
JMF 12:0071cb144c7a 198 #if defined(MBEDTLS_MEMORY_DEBUG)
JMF 12:0071cb144c7a 199 mbedtls_fprintf( stderr, "FATAL: verification failed: "
JMF 12:0071cb144c7a 200 "first->prev != NULL\n" );
JMF 12:0071cb144c7a 201 #endif
JMF 12:0071cb144c7a 202 return( 1 );
JMF 12:0071cb144c7a 203 }
JMF 12:0071cb144c7a 204
JMF 12:0071cb144c7a 205 while( cur != NULL )
JMF 12:0071cb144c7a 206 {
JMF 12:0071cb144c7a 207 if( verify_header( cur ) != 0 )
JMF 12:0071cb144c7a 208 {
JMF 12:0071cb144c7a 209 #if defined(MBEDTLS_MEMORY_DEBUG)
JMF 12:0071cb144c7a 210 mbedtls_fprintf( stderr, "FATAL: verification of header "
JMF 12:0071cb144c7a 211 "failed\n" );
JMF 12:0071cb144c7a 212 #endif
JMF 12:0071cb144c7a 213 return( 1 );
JMF 12:0071cb144c7a 214 }
JMF 12:0071cb144c7a 215
JMF 12:0071cb144c7a 216 if( cur->prev != prv )
JMF 12:0071cb144c7a 217 {
JMF 12:0071cb144c7a 218 #if defined(MBEDTLS_MEMORY_DEBUG)
JMF 12:0071cb144c7a 219 mbedtls_fprintf( stderr, "FATAL: verification failed: "
JMF 12:0071cb144c7a 220 "cur->prev != prv\n" );
JMF 12:0071cb144c7a 221 #endif
JMF 12:0071cb144c7a 222 return( 1 );
JMF 12:0071cb144c7a 223 }
JMF 12:0071cb144c7a 224
JMF 12:0071cb144c7a 225 prv = cur;
JMF 12:0071cb144c7a 226 cur = cur->next;
JMF 12:0071cb144c7a 227 }
JMF 12:0071cb144c7a 228
JMF 12:0071cb144c7a 229 return( 0 );
JMF 12:0071cb144c7a 230 }
JMF 12:0071cb144c7a 231
JMF 12:0071cb144c7a 232 static void *buffer_alloc_calloc( size_t n, size_t size )
JMF 12:0071cb144c7a 233 {
JMF 12:0071cb144c7a 234 memory_header *new, *cur = heap.first_free;
JMF 12:0071cb144c7a 235 unsigned char *p;
JMF 12:0071cb144c7a 236 void *ret;
JMF 12:0071cb144c7a 237 size_t original_len, len;
JMF 12:0071cb144c7a 238 #if defined(MBEDTLS_MEMORY_BACKTRACE)
JMF 12:0071cb144c7a 239 void *trace_buffer[MAX_BT];
JMF 12:0071cb144c7a 240 size_t trace_cnt;
JMF 12:0071cb144c7a 241 #endif
JMF 12:0071cb144c7a 242
JMF 12:0071cb144c7a 243 if( heap.buf == NULL || heap.first == NULL )
JMF 12:0071cb144c7a 244 return( NULL );
JMF 12:0071cb144c7a 245
JMF 12:0071cb144c7a 246 original_len = len = n * size;
JMF 12:0071cb144c7a 247
JMF 12:0071cb144c7a 248 if( n != 0 && len / n != size )
JMF 12:0071cb144c7a 249 return( NULL );
JMF 12:0071cb144c7a 250
JMF 12:0071cb144c7a 251 if( len % MBEDTLS_MEMORY_ALIGN_MULTIPLE )
JMF 12:0071cb144c7a 252 {
JMF 12:0071cb144c7a 253 len -= len % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
JMF 12:0071cb144c7a 254 len += MBEDTLS_MEMORY_ALIGN_MULTIPLE;
JMF 12:0071cb144c7a 255 }
JMF 12:0071cb144c7a 256
JMF 12:0071cb144c7a 257 // Find block that fits
JMF 12:0071cb144c7a 258 //
JMF 12:0071cb144c7a 259 while( cur != NULL )
JMF 12:0071cb144c7a 260 {
JMF 12:0071cb144c7a 261 if( cur->size >= len )
JMF 12:0071cb144c7a 262 break;
JMF 12:0071cb144c7a 263
JMF 12:0071cb144c7a 264 cur = cur->next_free;
JMF 12:0071cb144c7a 265 }
JMF 12:0071cb144c7a 266
JMF 12:0071cb144c7a 267 if( cur == NULL )
JMF 12:0071cb144c7a 268 return( NULL );
JMF 12:0071cb144c7a 269
JMF 12:0071cb144c7a 270 if( cur->alloc != 0 )
JMF 12:0071cb144c7a 271 {
JMF 12:0071cb144c7a 272 #if defined(MBEDTLS_MEMORY_DEBUG)
JMF 12:0071cb144c7a 273 mbedtls_fprintf( stderr, "FATAL: block in free_list but allocated "
JMF 12:0071cb144c7a 274 "data\n" );
JMF 12:0071cb144c7a 275 #endif
JMF 12:0071cb144c7a 276 mbedtls_exit( 1 );
JMF 12:0071cb144c7a 277 }
JMF 12:0071cb144c7a 278
JMF 12:0071cb144c7a 279 #if defined(MBEDTLS_MEMORY_DEBUG)
JMF 12:0071cb144c7a 280 heap.alloc_count++;
JMF 12:0071cb144c7a 281 #endif
JMF 12:0071cb144c7a 282
JMF 12:0071cb144c7a 283 // Found location, split block if > memory_header + 4 room left
JMF 12:0071cb144c7a 284 //
JMF 12:0071cb144c7a 285 if( cur->size - len < sizeof(memory_header) +
JMF 12:0071cb144c7a 286 MBEDTLS_MEMORY_ALIGN_MULTIPLE )
JMF 12:0071cb144c7a 287 {
JMF 12:0071cb144c7a 288 cur->alloc = 1;
JMF 12:0071cb144c7a 289
JMF 12:0071cb144c7a 290 // Remove from free_list
JMF 12:0071cb144c7a 291 //
JMF 12:0071cb144c7a 292 if( cur->prev_free != NULL )
JMF 12:0071cb144c7a 293 cur->prev_free->next_free = cur->next_free;
JMF 12:0071cb144c7a 294 else
JMF 12:0071cb144c7a 295 heap.first_free = cur->next_free;
JMF 12:0071cb144c7a 296
JMF 12:0071cb144c7a 297 if( cur->next_free != NULL )
JMF 12:0071cb144c7a 298 cur->next_free->prev_free = cur->prev_free;
JMF 12:0071cb144c7a 299
JMF 12:0071cb144c7a 300 cur->prev_free = NULL;
JMF 12:0071cb144c7a 301 cur->next_free = NULL;
JMF 12:0071cb144c7a 302
JMF 12:0071cb144c7a 303 #if defined(MBEDTLS_MEMORY_DEBUG)
JMF 12:0071cb144c7a 304 heap.total_used += cur->size;
JMF 12:0071cb144c7a 305 if( heap.total_used > heap.maximum_used )
JMF 12:0071cb144c7a 306 heap.maximum_used = heap.total_used;
JMF 12:0071cb144c7a 307 #endif
JMF 12:0071cb144c7a 308 #if defined(MBEDTLS_MEMORY_BACKTRACE)
JMF 12:0071cb144c7a 309 trace_cnt = backtrace( trace_buffer, MAX_BT );
JMF 12:0071cb144c7a 310 cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
JMF 12:0071cb144c7a 311 cur->trace_count = trace_cnt;
JMF 12:0071cb144c7a 312 #endif
JMF 12:0071cb144c7a 313
JMF 12:0071cb144c7a 314 if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
JMF 12:0071cb144c7a 315 mbedtls_exit( 1 );
JMF 12:0071cb144c7a 316
JMF 12:0071cb144c7a 317 ret = (unsigned char *) cur + sizeof( memory_header );
JMF 12:0071cb144c7a 318 memset( ret, 0, original_len );
JMF 12:0071cb144c7a 319
JMF 12:0071cb144c7a 320 return( ret );
JMF 12:0071cb144c7a 321 }
JMF 12:0071cb144c7a 322
JMF 12:0071cb144c7a 323 p = ( (unsigned char *) cur ) + sizeof(memory_header) + len;
JMF 12:0071cb144c7a 324 new = (memory_header *) p;
JMF 12:0071cb144c7a 325
JMF 12:0071cb144c7a 326 new->size = cur->size - len - sizeof(memory_header);
JMF 12:0071cb144c7a 327 new->alloc = 0;
JMF 12:0071cb144c7a 328 new->prev = cur;
JMF 12:0071cb144c7a 329 new->next = cur->next;
JMF 12:0071cb144c7a 330 #if defined(MBEDTLS_MEMORY_BACKTRACE)
JMF 12:0071cb144c7a 331 new->trace = NULL;
JMF 12:0071cb144c7a 332 new->trace_count = 0;
JMF 12:0071cb144c7a 333 #endif
JMF 12:0071cb144c7a 334 new->magic1 = MAGIC1;
JMF 12:0071cb144c7a 335 new->magic2 = MAGIC2;
JMF 12:0071cb144c7a 336
JMF 12:0071cb144c7a 337 if( new->next != NULL )
JMF 12:0071cb144c7a 338 new->next->prev = new;
JMF 12:0071cb144c7a 339
JMF 12:0071cb144c7a 340 // Replace cur with new in free_list
JMF 12:0071cb144c7a 341 //
JMF 12:0071cb144c7a 342 new->prev_free = cur->prev_free;
JMF 12:0071cb144c7a 343 new->next_free = cur->next_free;
JMF 12:0071cb144c7a 344 if( new->prev_free != NULL )
JMF 12:0071cb144c7a 345 new->prev_free->next_free = new;
JMF 12:0071cb144c7a 346 else
JMF 12:0071cb144c7a 347 heap.first_free = new;
JMF 12:0071cb144c7a 348
JMF 12:0071cb144c7a 349 if( new->next_free != NULL )
JMF 12:0071cb144c7a 350 new->next_free->prev_free = new;
JMF 12:0071cb144c7a 351
JMF 12:0071cb144c7a 352 cur->alloc = 1;
JMF 12:0071cb144c7a 353 cur->size = len;
JMF 12:0071cb144c7a 354 cur->next = new;
JMF 12:0071cb144c7a 355 cur->prev_free = NULL;
JMF 12:0071cb144c7a 356 cur->next_free = NULL;
JMF 12:0071cb144c7a 357
JMF 12:0071cb144c7a 358 #if defined(MBEDTLS_MEMORY_DEBUG)
JMF 12:0071cb144c7a 359 heap.header_count++;
JMF 12:0071cb144c7a 360 if( heap.header_count > heap.maximum_header_count )
JMF 12:0071cb144c7a 361 heap.maximum_header_count = heap.header_count;
JMF 12:0071cb144c7a 362 heap.total_used += cur->size;
JMF 12:0071cb144c7a 363 if( heap.total_used > heap.maximum_used )
JMF 12:0071cb144c7a 364 heap.maximum_used = heap.total_used;
JMF 12:0071cb144c7a 365 #endif
JMF 12:0071cb144c7a 366 #if defined(MBEDTLS_MEMORY_BACKTRACE)
JMF 12:0071cb144c7a 367 trace_cnt = backtrace( trace_buffer, MAX_BT );
JMF 12:0071cb144c7a 368 cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
JMF 12:0071cb144c7a 369 cur->trace_count = trace_cnt;
JMF 12:0071cb144c7a 370 #endif
JMF 12:0071cb144c7a 371
JMF 12:0071cb144c7a 372 if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
JMF 12:0071cb144c7a 373 mbedtls_exit( 1 );
JMF 12:0071cb144c7a 374
JMF 12:0071cb144c7a 375 ret = (unsigned char *) cur + sizeof( memory_header );
JMF 12:0071cb144c7a 376 memset( ret, 0, original_len );
JMF 12:0071cb144c7a 377
JMF 12:0071cb144c7a 378 return( ret );
JMF 12:0071cb144c7a 379 }
JMF 12:0071cb144c7a 380
JMF 12:0071cb144c7a 381 static void buffer_alloc_free( void *ptr )
JMF 12:0071cb144c7a 382 {
JMF 12:0071cb144c7a 383 memory_header *hdr, *old = NULL;
JMF 12:0071cb144c7a 384 unsigned char *p = (unsigned char *) ptr;
JMF 12:0071cb144c7a 385
JMF 12:0071cb144c7a 386 if( ptr == NULL || heap.buf == NULL || heap.first == NULL )
JMF 12:0071cb144c7a 387 return;
JMF 12:0071cb144c7a 388
JMF 12:0071cb144c7a 389 if( p < heap.buf || p > heap.buf + heap.len )
JMF 12:0071cb144c7a 390 {
JMF 12:0071cb144c7a 391 #if defined(MBEDTLS_MEMORY_DEBUG)
JMF 12:0071cb144c7a 392 mbedtls_fprintf( stderr, "FATAL: mbedtls_free() outside of managed "
JMF 12:0071cb144c7a 393 "space\n" );
JMF 12:0071cb144c7a 394 #endif
JMF 12:0071cb144c7a 395 mbedtls_exit( 1 );
JMF 12:0071cb144c7a 396 }
JMF 12:0071cb144c7a 397
JMF 12:0071cb144c7a 398 p -= sizeof(memory_header);
JMF 12:0071cb144c7a 399 hdr = (memory_header *) p;
JMF 12:0071cb144c7a 400
JMF 12:0071cb144c7a 401 if( verify_header( hdr ) != 0 )
JMF 12:0071cb144c7a 402 mbedtls_exit( 1 );
JMF 12:0071cb144c7a 403
JMF 12:0071cb144c7a 404 if( hdr->alloc != 1 )
JMF 12:0071cb144c7a 405 {
JMF 12:0071cb144c7a 406 #if defined(MBEDTLS_MEMORY_DEBUG)
JMF 12:0071cb144c7a 407 mbedtls_fprintf( stderr, "FATAL: mbedtls_free() on unallocated "
JMF 12:0071cb144c7a 408 "data\n" );
JMF 12:0071cb144c7a 409 #endif
JMF 12:0071cb144c7a 410 mbedtls_exit( 1 );
JMF 12:0071cb144c7a 411 }
JMF 12:0071cb144c7a 412
JMF 12:0071cb144c7a 413 hdr->alloc = 0;
JMF 12:0071cb144c7a 414
JMF 12:0071cb144c7a 415 #if defined(MBEDTLS_MEMORY_DEBUG)
JMF 12:0071cb144c7a 416 heap.free_count++;
JMF 12:0071cb144c7a 417 heap.total_used -= hdr->size;
JMF 12:0071cb144c7a 418 #endif
JMF 12:0071cb144c7a 419
JMF 12:0071cb144c7a 420 #if defined(MBEDTLS_MEMORY_BACKTRACE)
JMF 12:0071cb144c7a 421 free( hdr->trace );
JMF 12:0071cb144c7a 422 hdr->trace = NULL;
JMF 12:0071cb144c7a 423 hdr->trace_count = 0;
JMF 12:0071cb144c7a 424 #endif
JMF 12:0071cb144c7a 425
JMF 12:0071cb144c7a 426 // Regroup with block before
JMF 12:0071cb144c7a 427 //
JMF 12:0071cb144c7a 428 if( hdr->prev != NULL && hdr->prev->alloc == 0 )
JMF 12:0071cb144c7a 429 {
JMF 12:0071cb144c7a 430 #if defined(MBEDTLS_MEMORY_DEBUG)
JMF 12:0071cb144c7a 431 heap.header_count--;
JMF 12:0071cb144c7a 432 #endif
JMF 12:0071cb144c7a 433 hdr->prev->size += sizeof(memory_header) + hdr->size;
JMF 12:0071cb144c7a 434 hdr->prev->next = hdr->next;
JMF 12:0071cb144c7a 435 old = hdr;
JMF 12:0071cb144c7a 436 hdr = hdr->prev;
JMF 12:0071cb144c7a 437
JMF 12:0071cb144c7a 438 if( hdr->next != NULL )
JMF 12:0071cb144c7a 439 hdr->next->prev = hdr;
JMF 12:0071cb144c7a 440
JMF 12:0071cb144c7a 441 memset( old, 0, sizeof(memory_header) );
JMF 12:0071cb144c7a 442 }
JMF 12:0071cb144c7a 443
JMF 12:0071cb144c7a 444 // Regroup with block after
JMF 12:0071cb144c7a 445 //
JMF 12:0071cb144c7a 446 if( hdr->next != NULL && hdr->next->alloc == 0 )
JMF 12:0071cb144c7a 447 {
JMF 12:0071cb144c7a 448 #if defined(MBEDTLS_MEMORY_DEBUG)
JMF 12:0071cb144c7a 449 heap.header_count--;
JMF 12:0071cb144c7a 450 #endif
JMF 12:0071cb144c7a 451 hdr->size += sizeof(memory_header) + hdr->next->size;
JMF 12:0071cb144c7a 452 old = hdr->next;
JMF 12:0071cb144c7a 453 hdr->next = hdr->next->next;
JMF 12:0071cb144c7a 454
JMF 12:0071cb144c7a 455 if( hdr->prev_free != NULL || hdr->next_free != NULL )
JMF 12:0071cb144c7a 456 {
JMF 12:0071cb144c7a 457 if( hdr->prev_free != NULL )
JMF 12:0071cb144c7a 458 hdr->prev_free->next_free = hdr->next_free;
JMF 12:0071cb144c7a 459 else
JMF 12:0071cb144c7a 460 heap.first_free = hdr->next_free;
JMF 12:0071cb144c7a 461
JMF 12:0071cb144c7a 462 if( hdr->next_free != NULL )
JMF 12:0071cb144c7a 463 hdr->next_free->prev_free = hdr->prev_free;
JMF 12:0071cb144c7a 464 }
JMF 12:0071cb144c7a 465
JMF 12:0071cb144c7a 466 hdr->prev_free = old->prev_free;
JMF 12:0071cb144c7a 467 hdr->next_free = old->next_free;
JMF 12:0071cb144c7a 468
JMF 12:0071cb144c7a 469 if( hdr->prev_free != NULL )
JMF 12:0071cb144c7a 470 hdr->prev_free->next_free = hdr;
JMF 12:0071cb144c7a 471 else
JMF 12:0071cb144c7a 472 heap.first_free = hdr;
JMF 12:0071cb144c7a 473
JMF 12:0071cb144c7a 474 if( hdr->next_free != NULL )
JMF 12:0071cb144c7a 475 hdr->next_free->prev_free = hdr;
JMF 12:0071cb144c7a 476
JMF 12:0071cb144c7a 477 if( hdr->next != NULL )
JMF 12:0071cb144c7a 478 hdr->next->prev = hdr;
JMF 12:0071cb144c7a 479
JMF 12:0071cb144c7a 480 memset( old, 0, sizeof(memory_header) );
JMF 12:0071cb144c7a 481 }
JMF 12:0071cb144c7a 482
JMF 12:0071cb144c7a 483 // Prepend to free_list if we have not merged
JMF 12:0071cb144c7a 484 // (Does not have to stay in same order as prev / next list)
JMF 12:0071cb144c7a 485 //
JMF 12:0071cb144c7a 486 if( old == NULL )
JMF 12:0071cb144c7a 487 {
JMF 12:0071cb144c7a 488 hdr->next_free = heap.first_free;
JMF 12:0071cb144c7a 489 if( heap.first_free != NULL )
JMF 12:0071cb144c7a 490 heap.first_free->prev_free = hdr;
JMF 12:0071cb144c7a 491 heap.first_free = hdr;
JMF 12:0071cb144c7a 492 }
JMF 12:0071cb144c7a 493
JMF 12:0071cb144c7a 494 if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_FREE ) && verify_chain() != 0 )
JMF 12:0071cb144c7a 495 mbedtls_exit( 1 );
JMF 12:0071cb144c7a 496 }
JMF 12:0071cb144c7a 497
JMF 12:0071cb144c7a 498 void mbedtls_memory_buffer_set_verify( int verify )
JMF 12:0071cb144c7a 499 {
JMF 12:0071cb144c7a 500 heap.verify = verify;
JMF 12:0071cb144c7a 501 }
JMF 12:0071cb144c7a 502
JMF 12:0071cb144c7a 503 int mbedtls_memory_buffer_alloc_verify()
JMF 12:0071cb144c7a 504 {
JMF 12:0071cb144c7a 505 return verify_chain();
JMF 12:0071cb144c7a 506 }
JMF 12:0071cb144c7a 507
JMF 12:0071cb144c7a 508 #if defined(MBEDTLS_MEMORY_DEBUG)
JMF 12:0071cb144c7a 509 void mbedtls_memory_buffer_alloc_status()
JMF 12:0071cb144c7a 510 {
JMF 12:0071cb144c7a 511 mbedtls_fprintf( stderr,
JMF 12:0071cb144c7a 512 "Current use: %zu blocks / %zu bytes, max: %zu blocks / "
JMF 12:0071cb144c7a 513 "%zu bytes (total %zu bytes), alloc / free: %zu / %zu\n",
JMF 12:0071cb144c7a 514 heap.header_count, heap.total_used,
JMF 12:0071cb144c7a 515 heap.maximum_header_count, heap.maximum_used,
JMF 12:0071cb144c7a 516 heap.maximum_header_count * sizeof( memory_header )
JMF 12:0071cb144c7a 517 + heap.maximum_used,
JMF 12:0071cb144c7a 518 heap.alloc_count, heap.free_count );
JMF 12:0071cb144c7a 519
JMF 12:0071cb144c7a 520 if( heap.first->next == NULL )
JMF 12:0071cb144c7a 521 mbedtls_fprintf( stderr, "All memory de-allocated in stack buffer\n" );
JMF 12:0071cb144c7a 522 else
JMF 12:0071cb144c7a 523 {
JMF 12:0071cb144c7a 524 mbedtls_fprintf( stderr, "Memory currently allocated:\n" );
JMF 12:0071cb144c7a 525 debug_chain();
JMF 12:0071cb144c7a 526 }
JMF 12:0071cb144c7a 527 }
JMF 12:0071cb144c7a 528
JMF 12:0071cb144c7a 529 void mbedtls_memory_buffer_alloc_max_get( size_t *max_used, size_t *max_blocks )
JMF 12:0071cb144c7a 530 {
JMF 12:0071cb144c7a 531 *max_used = heap.maximum_used;
JMF 12:0071cb144c7a 532 *max_blocks = heap.maximum_header_count;
JMF 12:0071cb144c7a 533 }
JMF 12:0071cb144c7a 534
JMF 12:0071cb144c7a 535 void mbedtls_memory_buffer_alloc_max_reset( void )
JMF 12:0071cb144c7a 536 {
JMF 12:0071cb144c7a 537 heap.maximum_used = 0;
JMF 12:0071cb144c7a 538 heap.maximum_header_count = 0;
JMF 12:0071cb144c7a 539 }
JMF 12:0071cb144c7a 540
JMF 12:0071cb144c7a 541 void mbedtls_memory_buffer_alloc_cur_get( size_t *cur_used, size_t *cur_blocks )
JMF 12:0071cb144c7a 542 {
JMF 12:0071cb144c7a 543 *cur_used = heap.total_used;
JMF 12:0071cb144c7a 544 *cur_blocks = heap.header_count;
JMF 12:0071cb144c7a 545 }
JMF 12:0071cb144c7a 546 #endif /* MBEDTLS_MEMORY_DEBUG */
JMF 12:0071cb144c7a 547
JMF 12:0071cb144c7a 548 #if defined(MBEDTLS_THREADING_C)
JMF 12:0071cb144c7a 549 static void *buffer_alloc_calloc_mutexed( size_t n, size_t size )
JMF 12:0071cb144c7a 550 {
JMF 12:0071cb144c7a 551 void *buf;
JMF 12:0071cb144c7a 552 if( mbedtls_mutex_lock( &heap.mutex ) != 0 )
JMF 12:0071cb144c7a 553 return( NULL );
JMF 12:0071cb144c7a 554 buf = buffer_alloc_calloc( n, size );
JMF 12:0071cb144c7a 555 if( mbedtls_mutex_unlock( &heap.mutex ) )
JMF 12:0071cb144c7a 556 return( NULL );
JMF 12:0071cb144c7a 557 return( buf );
JMF 12:0071cb144c7a 558 }
JMF 12:0071cb144c7a 559
JMF 12:0071cb144c7a 560 static void buffer_alloc_free_mutexed( void *ptr )
JMF 12:0071cb144c7a 561 {
JMF 12:0071cb144c7a 562 /* We have to good option here, but corrupting the heap seems
JMF 12:0071cb144c7a 563 * worse than loosing memory. */
JMF 12:0071cb144c7a 564 if( mbedtls_mutex_lock( &heap.mutex ) )
JMF 12:0071cb144c7a 565 return;
JMF 12:0071cb144c7a 566 buffer_alloc_free( ptr );
JMF 12:0071cb144c7a 567 (void) mbedtls_mutex_unlock( &heap.mutex );
JMF 12:0071cb144c7a 568 }
JMF 12:0071cb144c7a 569 #endif /* MBEDTLS_THREADING_C */
JMF 12:0071cb144c7a 570
JMF 12:0071cb144c7a 571 void mbedtls_memory_buffer_alloc_init( unsigned char *buf, size_t len )
JMF 12:0071cb144c7a 572 {
JMF 12:0071cb144c7a 573 memset( &heap, 0, sizeof(buffer_alloc_ctx) );
JMF 12:0071cb144c7a 574 memset( buf, 0, len );
JMF 12:0071cb144c7a 575
JMF 12:0071cb144c7a 576 #if defined(MBEDTLS_THREADING_C)
JMF 12:0071cb144c7a 577 mbedtls_mutex_init( &heap.mutex );
JMF 12:0071cb144c7a 578 mbedtls_platform_set_calloc_free( buffer_alloc_calloc_mutexed,
JMF 12:0071cb144c7a 579 buffer_alloc_free_mutexed );
JMF 12:0071cb144c7a 580 #else
JMF 12:0071cb144c7a 581 mbedtls_platform_set_calloc_free( buffer_alloc_calloc, buffer_alloc_free );
JMF 12:0071cb144c7a 582 #endif
JMF 12:0071cb144c7a 583
JMF 12:0071cb144c7a 584 if( (size_t) buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE )
JMF 12:0071cb144c7a 585 {
JMF 12:0071cb144c7a 586 /* Adjust len first since buf is used in the computation */
JMF 12:0071cb144c7a 587 len -= MBEDTLS_MEMORY_ALIGN_MULTIPLE
JMF 12:0071cb144c7a 588 - (size_t) buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
JMF 12:0071cb144c7a 589 buf += MBEDTLS_MEMORY_ALIGN_MULTIPLE
JMF 12:0071cb144c7a 590 - (size_t) buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
JMF 12:0071cb144c7a 591 }
JMF 12:0071cb144c7a 592
JMF 12:0071cb144c7a 593 heap.buf = buf;
JMF 12:0071cb144c7a 594 heap.len = len;
JMF 12:0071cb144c7a 595
JMF 12:0071cb144c7a 596 heap.first = (memory_header *) buf;
JMF 12:0071cb144c7a 597 heap.first->size = len - sizeof(memory_header);
JMF 12:0071cb144c7a 598 heap.first->magic1 = MAGIC1;
JMF 12:0071cb144c7a 599 heap.first->magic2 = MAGIC2;
JMF 12:0071cb144c7a 600 heap.first_free = heap.first;
JMF 12:0071cb144c7a 601 }
JMF 12:0071cb144c7a 602
JMF 12:0071cb144c7a 603 void mbedtls_memory_buffer_alloc_free()
JMF 12:0071cb144c7a 604 {
JMF 12:0071cb144c7a 605 #if defined(MBEDTLS_THREADING_C)
JMF 12:0071cb144c7a 606 mbedtls_mutex_free( &heap.mutex );
JMF 12:0071cb144c7a 607 #endif
JMF 12:0071cb144c7a 608 mbedtls_zeroize( &heap, sizeof(buffer_alloc_ctx) );
JMF 12:0071cb144c7a 609 }
JMF 12:0071cb144c7a 610
JMF 12:0071cb144c7a 611 #if defined(MBEDTLS_SELF_TEST)
JMF 12:0071cb144c7a 612 static int check_pointer( void *p )
JMF 12:0071cb144c7a 613 {
JMF 12:0071cb144c7a 614 if( p == NULL )
JMF 12:0071cb144c7a 615 return( -1 );
JMF 12:0071cb144c7a 616
JMF 12:0071cb144c7a 617 if( (size_t) p % MBEDTLS_MEMORY_ALIGN_MULTIPLE != 0 )
JMF 12:0071cb144c7a 618 return( -1 );
JMF 12:0071cb144c7a 619
JMF 12:0071cb144c7a 620 return( 0 );
JMF 12:0071cb144c7a 621 }
JMF 12:0071cb144c7a 622
JMF 12:0071cb144c7a 623 static int check_all_free( )
JMF 12:0071cb144c7a 624 {
JMF 12:0071cb144c7a 625 if(
JMF 12:0071cb144c7a 626 #if defined(MBEDTLS_MEMORY_DEBUG)
JMF 12:0071cb144c7a 627 heap.total_used != 0 ||
JMF 12:0071cb144c7a 628 #endif
JMF 12:0071cb144c7a 629 heap.first != heap.first_free ||
JMF 12:0071cb144c7a 630 (void *) heap.first != (void *) heap.buf )
JMF 12:0071cb144c7a 631 {
JMF 12:0071cb144c7a 632 return( -1 );
JMF 12:0071cb144c7a 633 }
JMF 12:0071cb144c7a 634
JMF 12:0071cb144c7a 635 return( 0 );
JMF 12:0071cb144c7a 636 }
JMF 12:0071cb144c7a 637
JMF 12:0071cb144c7a 638 #define TEST_ASSERT( condition ) \
JMF 12:0071cb144c7a 639 if( ! (condition) ) \
JMF 12:0071cb144c7a 640 { \
JMF 12:0071cb144c7a 641 if( verbose != 0 ) \
JMF 12:0071cb144c7a 642 mbedtls_printf( "failed\n" ); \
JMF 12:0071cb144c7a 643 \
JMF 12:0071cb144c7a 644 ret = 1; \
JMF 12:0071cb144c7a 645 goto cleanup; \
JMF 12:0071cb144c7a 646 }
JMF 12:0071cb144c7a 647
JMF 12:0071cb144c7a 648 int mbedtls_memory_buffer_alloc_self_test( int verbose )
JMF 12:0071cb144c7a 649 {
JMF 12:0071cb144c7a 650 unsigned char buf[1024];
JMF 12:0071cb144c7a 651 unsigned char *p, *q, *r, *end;
JMF 12:0071cb144c7a 652 int ret = 0;
JMF 12:0071cb144c7a 653
JMF 12:0071cb144c7a 654 if( verbose != 0 )
JMF 12:0071cb144c7a 655 mbedtls_printf( " MBA test #1 (basic alloc-free cycle): " );
JMF 12:0071cb144c7a 656
JMF 12:0071cb144c7a 657 mbedtls_memory_buffer_alloc_init( buf, sizeof( buf ) );
JMF 12:0071cb144c7a 658
JMF 12:0071cb144c7a 659 p = mbedtls_calloc( 1, 1 );
JMF 12:0071cb144c7a 660 q = mbedtls_calloc( 1, 128 );
JMF 12:0071cb144c7a 661 r = mbedtls_calloc( 1, 16 );
JMF 12:0071cb144c7a 662
JMF 12:0071cb144c7a 663 TEST_ASSERT( check_pointer( p ) == 0 &&
JMF 12:0071cb144c7a 664 check_pointer( q ) == 0 &&
JMF 12:0071cb144c7a 665 check_pointer( r ) == 0 );
JMF 12:0071cb144c7a 666
JMF 12:0071cb144c7a 667 mbedtls_free( r );
JMF 12:0071cb144c7a 668 mbedtls_free( q );
JMF 12:0071cb144c7a 669 mbedtls_free( p );
JMF 12:0071cb144c7a 670
JMF 12:0071cb144c7a 671 TEST_ASSERT( check_all_free( ) == 0 );
JMF 12:0071cb144c7a 672
JMF 12:0071cb144c7a 673 /* Memorize end to compare with the next test */
JMF 12:0071cb144c7a 674 end = heap.buf + heap.len;
JMF 12:0071cb144c7a 675
JMF 12:0071cb144c7a 676 mbedtls_memory_buffer_alloc_free( );
JMF 12:0071cb144c7a 677
JMF 12:0071cb144c7a 678 if( verbose != 0 )
JMF 12:0071cb144c7a 679 mbedtls_printf( "passed\n" );
JMF 12:0071cb144c7a 680
JMF 12:0071cb144c7a 681 if( verbose != 0 )
JMF 12:0071cb144c7a 682 mbedtls_printf( " MBA test #2 (buf not aligned): " );
JMF 12:0071cb144c7a 683
JMF 12:0071cb144c7a 684 mbedtls_memory_buffer_alloc_init( buf + 1, sizeof( buf ) - 1 );
JMF 12:0071cb144c7a 685
JMF 12:0071cb144c7a 686 TEST_ASSERT( heap.buf + heap.len == end );
JMF 12:0071cb144c7a 687
JMF 12:0071cb144c7a 688 p = mbedtls_calloc( 1, 1 );
JMF 12:0071cb144c7a 689 q = mbedtls_calloc( 1, 128 );
JMF 12:0071cb144c7a 690 r = mbedtls_calloc( 1, 16 );
JMF 12:0071cb144c7a 691
JMF 12:0071cb144c7a 692 TEST_ASSERT( check_pointer( p ) == 0 &&
JMF 12:0071cb144c7a 693 check_pointer( q ) == 0 &&
JMF 12:0071cb144c7a 694 check_pointer( r ) == 0 );
JMF 12:0071cb144c7a 695
JMF 12:0071cb144c7a 696 mbedtls_free( r );
JMF 12:0071cb144c7a 697 mbedtls_free( q );
JMF 12:0071cb144c7a 698 mbedtls_free( p );
JMF 12:0071cb144c7a 699
JMF 12:0071cb144c7a 700 TEST_ASSERT( check_all_free( ) == 0 );
JMF 12:0071cb144c7a 701
JMF 12:0071cb144c7a 702 mbedtls_memory_buffer_alloc_free( );
JMF 12:0071cb144c7a 703
JMF 12:0071cb144c7a 704 if( verbose != 0 )
JMF 12:0071cb144c7a 705 mbedtls_printf( "passed\n" );
JMF 12:0071cb144c7a 706
JMF 12:0071cb144c7a 707 if( verbose != 0 )
JMF 12:0071cb144c7a 708 mbedtls_printf( " MBA test #3 (full): " );
JMF 12:0071cb144c7a 709
JMF 12:0071cb144c7a 710 mbedtls_memory_buffer_alloc_init( buf, sizeof( buf ) );
JMF 12:0071cb144c7a 711
JMF 12:0071cb144c7a 712 p = mbedtls_calloc( 1, sizeof( buf ) - sizeof( memory_header ) );
JMF 12:0071cb144c7a 713
JMF 12:0071cb144c7a 714 TEST_ASSERT( check_pointer( p ) == 0 );
JMF 12:0071cb144c7a 715 TEST_ASSERT( mbedtls_calloc( 1, 1 ) == NULL );
JMF 12:0071cb144c7a 716
JMF 12:0071cb144c7a 717 mbedtls_free( p );
JMF 12:0071cb144c7a 718
JMF 12:0071cb144c7a 719 p = mbedtls_calloc( 1, sizeof( buf ) - 2 * sizeof( memory_header ) - 16 );
JMF 12:0071cb144c7a 720 q = mbedtls_calloc( 1, 16 );
JMF 12:0071cb144c7a 721
JMF 12:0071cb144c7a 722 TEST_ASSERT( check_pointer( p ) == 0 && check_pointer( q ) == 0 );
JMF 12:0071cb144c7a 723 TEST_ASSERT( mbedtls_calloc( 1, 1 ) == NULL );
JMF 12:0071cb144c7a 724
JMF 12:0071cb144c7a 725 mbedtls_free( q );
JMF 12:0071cb144c7a 726
JMF 12:0071cb144c7a 727 TEST_ASSERT( mbedtls_calloc( 1, 17 ) == NULL );
JMF 12:0071cb144c7a 728
JMF 12:0071cb144c7a 729 mbedtls_free( p );
JMF 12:0071cb144c7a 730
JMF 12:0071cb144c7a 731 TEST_ASSERT( check_all_free( ) == 0 );
JMF 12:0071cb144c7a 732
JMF 12:0071cb144c7a 733 mbedtls_memory_buffer_alloc_free( );
JMF 12:0071cb144c7a 734
JMF 12:0071cb144c7a 735 if( verbose != 0 )
JMF 12:0071cb144c7a 736 mbedtls_printf( "passed\n" );
JMF 12:0071cb144c7a 737
JMF 12:0071cb144c7a 738 cleanup:
JMF 12:0071cb144c7a 739 mbedtls_memory_buffer_alloc_free( );
JMF 12:0071cb144c7a 740
JMF 12:0071cb144c7a 741 return( ret );
JMF 12:0071cb144c7a 742 }
JMF 12:0071cb144c7a 743 #endif /* MBEDTLS_SELF_TEST */
JMF 12:0071cb144c7a 744
JMF 12:0071cb144c7a 745 #endif /* MBEDTLS_MEMORY_BUFFER_ALLOC_C */