blob: 4a5be479f832ac0c1b983770620bba152ad61f79 [file] [log] [blame]
Paul Bakker6e339b52013-07-03 13:37:05 +02001/*
2 * Buffer-based memory allocator
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakker6e339b52013-07-03 13:37:05 +02005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker6e339b52013-07-03 13:37:05 +020027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakker6e339b52013-07-03 13:37:05 +020031
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010032#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
Paul Bakker6e339b52013-07-03 13:37:05 +020033
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010034#include "polarssl/memory_buffer_alloc.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020035
36#include <string.h>
37
38#if defined(POLARSSL_MEMORY_DEBUG)
39#include <stdio.h>
Manuel Pégourié-Gonnard2fbf3112014-07-12 03:32:40 +020040#endif
Paul Bakker6e339b52013-07-03 13:37:05 +020041#if defined(POLARSSL_MEMORY_BACKTRACE)
42#include <execinfo.h>
43#endif
Paul Bakker6e339b52013-07-03 13:37:05 +020044
Paul Bakker1337aff2013-09-29 14:45:34 +020045#if defined(POLARSSL_THREADING_C)
46#include "polarssl/threading.h"
47#endif
48
Paul Bakker7dc4c442014-02-01 22:50:26 +010049#if defined(POLARSSL_PLATFORM_C)
50#include "polarssl/platform.h"
51#else
52#define polarssl_fprintf fprintf
53#endif
54
Paul Bakker34617722014-06-13 17:20:13 +020055/* Implementation that should never be optimized out by the compiler */
56static void polarssl_zeroize( void *v, size_t n ) {
57 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
58}
59
Paul Bakker6e339b52013-07-03 13:37:05 +020060#define MAGIC1 0xFF00AA55
61#define MAGIC2 0xEE119966
62#define MAX_BT 20
63
64typedef struct _memory_header memory_header;
65struct _memory_header
66{
67 size_t magic1;
68 size_t size;
69 size_t alloc;
70 memory_header *prev;
71 memory_header *next;
Paul Bakker1ef120f2013-07-03 17:20:39 +020072 memory_header *prev_free;
73 memory_header *next_free;
Paul Bakker6e339b52013-07-03 13:37:05 +020074#if defined(POLARSSL_MEMORY_BACKTRACE)
75 char **trace;
76 size_t trace_count;
77#endif
78 size_t magic2;
79};
80
81typedef struct
82{
83 unsigned char *buf;
84 size_t len;
85 memory_header *first;
Paul Bakker1ef120f2013-07-03 17:20:39 +020086 memory_header *first_free;
Paul Bakker6e339b52013-07-03 13:37:05 +020087 size_t current_alloc_size;
88 int verify;
Paul Bakker891998e2013-07-03 14:45:05 +020089#if defined(POLARSSL_MEMORY_DEBUG)
90 size_t malloc_count;
91 size_t free_count;
92 size_t total_used;
93 size_t maximum_used;
94 size_t header_count;
Manuel Pégourié-Gonnard70896a02013-12-30 18:06:41 +010095 size_t maximum_header_count;
Paul Bakker891998e2013-07-03 14:45:05 +020096#endif
Paul Bakker1337aff2013-09-29 14:45:34 +020097#if defined(POLARSSL_THREADING_C)
98 threading_mutex_t mutex;
99#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200100}
101buffer_alloc_ctx;
102
103static buffer_alloc_ctx heap;
104
105#if defined(POLARSSL_MEMORY_DEBUG)
106static void debug_header( memory_header *hdr )
107{
108#if defined(POLARSSL_MEMORY_BACKTRACE)
109 size_t i;
110#endif
111
Manuel Pégourié-Gonnard97884a32014-07-12 02:27:35 +0200112 polarssl_fprintf( stderr, "HDR: PTR(%10zu), PREV(%10zu), NEXT(%10zu), "
113 "ALLOC(%zu), SIZE(%10zu)\n",
Paul Bakker7dc4c442014-02-01 22:50:26 +0100114 (size_t) hdr, (size_t) hdr->prev, (size_t) hdr->next,
115 hdr->alloc, hdr->size );
Manuel Pégourié-Gonnard97884a32014-07-12 02:27:35 +0200116 polarssl_fprintf( stderr, " FPREV(%10zu), FNEXT(%10zu)\n",
Paul Bakker7dc4c442014-02-01 22:50:26 +0100117 (size_t) hdr->prev_free, (size_t) hdr->next_free );
Paul Bakker6e339b52013-07-03 13:37:05 +0200118
119#if defined(POLARSSL_MEMORY_BACKTRACE)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100120 polarssl_fprintf( stderr, "TRACE: \n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200121 for( i = 0; i < hdr->trace_count; i++ )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100122 polarssl_fprintf( stderr, "%s\n", hdr->trace[i] );
123 polarssl_fprintf( stderr, "\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200124#endif
125}
126
127static void debug_chain()
128{
129 memory_header *cur = heap.first;
130
Paul Bakker7dc4c442014-02-01 22:50:26 +0100131 polarssl_fprintf( stderr, "\nBlock list\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200132 while( cur != NULL )
133 {
134 debug_header( cur );
Paul Bakker6e339b52013-07-03 13:37:05 +0200135 cur = cur->next;
136 }
Paul Bakker1ef120f2013-07-03 17:20:39 +0200137
Paul Bakker7dc4c442014-02-01 22:50:26 +0100138 polarssl_fprintf( stderr, "Free list\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200139 cur = heap.first_free;
140
141 while( cur != NULL )
142 {
143 debug_header( cur );
144 cur = cur->next_free;
145 }
Paul Bakker6e339b52013-07-03 13:37:05 +0200146}
147#endif /* POLARSSL_MEMORY_DEBUG */
148
149static int verify_header( memory_header *hdr )
150{
151 if( hdr->magic1 != MAGIC1 )
152 {
153#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100154 polarssl_fprintf( stderr, "FATAL: MAGIC1 mismatch\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200155#endif
156 return( 1 );
157 }
158
159 if( hdr->magic2 != MAGIC2 )
160 {
161#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100162 polarssl_fprintf( stderr, "FATAL: MAGIC2 mismatch\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200163#endif
164 return( 1 );
165 }
166
167 if( hdr->alloc > 1 )
168 {
169#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100170 polarssl_fprintf( stderr, "FATAL: alloc has illegal value\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200171#endif
172 return( 1 );
173 }
174
Paul Bakker1ef120f2013-07-03 17:20:39 +0200175 if( hdr->prev != NULL && hdr->prev == hdr->next )
176 {
177#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100178 polarssl_fprintf( stderr, "FATAL: prev == next\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200179#endif
180 return( 1 );
181 }
182
183 if( hdr->prev_free != NULL && hdr->prev_free == hdr->next_free )
184 {
185#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100186 polarssl_fprintf( stderr, "FATAL: prev_free == next_free\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200187#endif
188 return( 1 );
189 }
190
Paul Bakker6e339b52013-07-03 13:37:05 +0200191 return( 0 );
192}
193
194static int verify_chain()
195{
196 memory_header *prv = heap.first, *cur = heap.first->next;
197
198 if( verify_header( heap.first ) != 0 )
199 {
200#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100201 polarssl_fprintf( stderr, "FATAL: verification of first header "
202 "failed\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200203#endif
204 return( 1 );
205 }
206
207 if( heap.first->prev != NULL )
208 {
209#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100210 polarssl_fprintf( stderr, "FATAL: verification failed: "
211 "first->prev != NULL\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200212#endif
213 return( 1 );
214 }
215
216 while( cur != NULL )
217 {
218 if( verify_header( cur ) != 0 )
219 {
220#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100221 polarssl_fprintf( stderr, "FATAL: verification of header "
222 "failed\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200223#endif
224 return( 1 );
225 }
226
227 if( cur->prev != prv )
228 {
229#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100230 polarssl_fprintf( stderr, "FATAL: verification failed: "
231 "cur->prev != prv\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200232#endif
233 return( 1 );
234 }
235
236 prv = cur;
237 cur = cur->next;
238 }
239
240 return( 0 );
241}
242
243static void *buffer_alloc_malloc( size_t len )
244{
Paul Bakker1ef120f2013-07-03 17:20:39 +0200245 memory_header *new, *cur = heap.first_free;
Paul Bakker6e339b52013-07-03 13:37:05 +0200246 unsigned char *p;
247#if defined(POLARSSL_MEMORY_BACKTRACE)
248 void *trace_buffer[MAX_BT];
249 size_t trace_cnt;
250#endif
251
252 if( heap.buf == NULL || heap.first == NULL )
253 return( NULL );
254
255 if( len % POLARSSL_MEMORY_ALIGN_MULTIPLE )
256 {
257 len -= len % POLARSSL_MEMORY_ALIGN_MULTIPLE;
258 len += POLARSSL_MEMORY_ALIGN_MULTIPLE;
259 }
260
261 // Find block that fits
262 //
263 while( cur != NULL )
264 {
Paul Bakker1ef120f2013-07-03 17:20:39 +0200265 if( cur->size >= len )
Paul Bakker6e339b52013-07-03 13:37:05 +0200266 break;
267
Paul Bakker1ef120f2013-07-03 17:20:39 +0200268 cur = cur->next_free;
Paul Bakker6e339b52013-07-03 13:37:05 +0200269 }
270
271 if( cur == NULL )
272 return( NULL );
273
Paul Bakker1ef120f2013-07-03 17:20:39 +0200274 if( cur->alloc != 0 )
275 {
276#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100277 polarssl_fprintf( stderr, "FATAL: block in free_list but allocated "
278 "data\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200279#endif
280 exit( 1 );
281 }
282
Paul Bakker891998e2013-07-03 14:45:05 +0200283#if defined(POLARSSL_MEMORY_DEBUG)
284 heap.malloc_count++;
285#endif
286
Paul Bakker6e339b52013-07-03 13:37:05 +0200287 // Found location, split block if > memory_header + 4 room left
288 //
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200289 if( cur->size - len < sizeof(memory_header) +
290 POLARSSL_MEMORY_ALIGN_MULTIPLE )
Paul Bakker6e339b52013-07-03 13:37:05 +0200291 {
292 cur->alloc = 1;
293
Paul Bakker1ef120f2013-07-03 17:20:39 +0200294 // Remove from free_list
295 //
296 if( cur->prev_free != NULL )
297 cur->prev_free->next_free = cur->next_free;
298 else
299 heap.first_free = cur->next_free;
300
301 if( cur->next_free != NULL )
302 cur->next_free->prev_free = cur->prev_free;
303
304 cur->prev_free = NULL;
305 cur->next_free = NULL;
306
Paul Bakker891998e2013-07-03 14:45:05 +0200307#if defined(POLARSSL_MEMORY_DEBUG)
308 heap.total_used += cur->size;
Paul Bakker66d5d072014-06-17 16:39:18 +0200309 if( heap.total_used > heap.maximum_used )
Paul Bakker891998e2013-07-03 14:45:05 +0200310 heap.maximum_used = heap.total_used;
311#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200312#if defined(POLARSSL_MEMORY_BACKTRACE)
313 trace_cnt = backtrace( trace_buffer, MAX_BT );
314 cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
315 cur->trace_count = trace_cnt;
316#endif
317
318 if( ( heap.verify & MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
319 exit( 1 );
320
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200321 return( ( (unsigned char *) cur ) + sizeof(memory_header) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200322 }
323
324 p = ( (unsigned char *) cur ) + sizeof(memory_header) + len;
325 new = (memory_header *) p;
326
327 new->size = cur->size - len - sizeof(memory_header);
328 new->alloc = 0;
329 new->prev = cur;
330 new->next = cur->next;
331#if defined(POLARSSL_MEMORY_BACKTRACE)
332 new->trace = NULL;
333 new->trace_count = 0;
334#endif
335 new->magic1 = MAGIC1;
336 new->magic2 = MAGIC2;
337
338 if( new->next != NULL )
339 new->next->prev = new;
340
Paul Bakker1ef120f2013-07-03 17:20:39 +0200341 // Replace cur with new in free_list
342 //
343 new->prev_free = cur->prev_free;
344 new->next_free = cur->next_free;
345 if( new->prev_free != NULL )
346 new->prev_free->next_free = new;
347 else
348 heap.first_free = new;
349
350 if( new->next_free != NULL )
351 new->next_free->prev_free = new;
352
Paul Bakker6e339b52013-07-03 13:37:05 +0200353 cur->alloc = 1;
354 cur->size = len;
355 cur->next = new;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200356 cur->prev_free = NULL;
357 cur->next_free = NULL;
Paul Bakker6e339b52013-07-03 13:37:05 +0200358
Paul Bakker891998e2013-07-03 14:45:05 +0200359#if defined(POLARSSL_MEMORY_DEBUG)
360 heap.header_count++;
Manuel Pégourié-Gonnard70896a02013-12-30 18:06:41 +0100361 if( heap.header_count > heap.maximum_header_count )
362 heap.maximum_header_count = heap.header_count;
Paul Bakker891998e2013-07-03 14:45:05 +0200363 heap.total_used += cur->size;
Paul Bakker66d5d072014-06-17 16:39:18 +0200364 if( heap.total_used > heap.maximum_used )
Paul Bakker891998e2013-07-03 14:45:05 +0200365 heap.maximum_used = heap.total_used;
366#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200367#if defined(POLARSSL_MEMORY_BACKTRACE)
368 trace_cnt = backtrace( trace_buffer, MAX_BT );
369 cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
370 cur->trace_count = trace_cnt;
371#endif
372
373 if( ( heap.verify & MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
374 exit( 1 );
375
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200376 return( ( (unsigned char *) cur ) + sizeof(memory_header) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200377}
378
379static void buffer_alloc_free( void *ptr )
380{
Paul Bakker1ef120f2013-07-03 17:20:39 +0200381 memory_header *hdr, *old = NULL;
Paul Bakker6e339b52013-07-03 13:37:05 +0200382 unsigned char *p = (unsigned char *) ptr;
383
Paul Bakker6e339b52013-07-03 13:37:05 +0200384 if( ptr == NULL || heap.buf == NULL || heap.first == NULL )
385 return;
386
387 if( p < heap.buf || p > heap.buf + heap.len )
388 {
389#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100390 polarssl_fprintf( stderr, "FATAL: polarssl_free() outside of managed "
391 "space\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200392#endif
Paul Bakker41350a92013-07-03 15:33:47 +0200393 exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200394 }
395
396 p -= sizeof(memory_header);
397 hdr = (memory_header *) p;
398
399 if( verify_header( hdr ) != 0 )
400 exit( 1 );
401
402 if( hdr->alloc != 1 )
403 {
404#if defined(POLARSSL_MEMORY_DEBUG)
Paul Bakker7dc4c442014-02-01 22:50:26 +0100405 polarssl_fprintf( stderr, "FATAL: polarssl_free() on unallocated "
406 "data\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200407#endif
Paul Bakker41350a92013-07-03 15:33:47 +0200408 exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200409 }
410
411 hdr->alloc = 0;
412
Paul Bakker891998e2013-07-03 14:45:05 +0200413#if defined(POLARSSL_MEMORY_DEBUG)
414 heap.free_count++;
415 heap.total_used -= hdr->size;
416#endif
417
Paul Bakker6e339b52013-07-03 13:37:05 +0200418 // Regroup with block before
419 //
420 if( hdr->prev != NULL && hdr->prev->alloc == 0 )
421 {
Paul Bakker891998e2013-07-03 14:45:05 +0200422#if defined(POLARSSL_MEMORY_DEBUG)
423 heap.header_count--;
424#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200425 hdr->prev->size += sizeof(memory_header) + hdr->size;
426 hdr->prev->next = hdr->next;
427 old = hdr;
428 hdr = hdr->prev;
429
430 if( hdr->next != NULL )
431 hdr->next->prev = hdr;
432
433#if defined(POLARSSL_MEMORY_BACKTRACE)
434 free( old->trace );
435#endif
436 memset( old, 0, sizeof(memory_header) );
437 }
438
439 // Regroup with block after
440 //
441 if( hdr->next != NULL && hdr->next->alloc == 0 )
442 {
Paul Bakker891998e2013-07-03 14:45:05 +0200443#if defined(POLARSSL_MEMORY_DEBUG)
444 heap.header_count--;
445#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200446 hdr->size += sizeof(memory_header) + hdr->next->size;
447 old = hdr->next;
448 hdr->next = hdr->next->next;
449
Paul Bakker1ef120f2013-07-03 17:20:39 +0200450 if( hdr->prev_free != NULL || hdr->next_free != NULL )
451 {
452 if( hdr->prev_free != NULL )
453 hdr->prev_free->next_free = hdr->next_free;
454 else
455 heap.first_free = hdr->next_free;
456
457 if( hdr->next_free != NULL )
458 hdr->next_free->prev_free = hdr->prev_free;
459 }
460
461 hdr->prev_free = old->prev_free;
462 hdr->next_free = old->next_free;
463
464 if( hdr->prev_free != NULL )
465 hdr->prev_free->next_free = hdr;
466 else
467 heap.first_free = hdr;
468
469 if( hdr->next_free != NULL )
470 hdr->next_free->prev_free = hdr;
471
Paul Bakker6e339b52013-07-03 13:37:05 +0200472 if( hdr->next != NULL )
473 hdr->next->prev = hdr;
474
475#if defined(POLARSSL_MEMORY_BACKTRACE)
476 free( old->trace );
477#endif
478 memset( old, 0, sizeof(memory_header) );
479 }
480
Paul Bakker1ef120f2013-07-03 17:20:39 +0200481 // Prepend to free_list if we have not merged
482 // (Does not have to stay in same order as prev / next list)
483 //
484 if( old == NULL )
485 {
486 hdr->next_free = heap.first_free;
Manuel Pégourié-Gonnard547ff662014-11-26 15:42:16 +0100487 if( heap.first_free != NULL )
488 heap.first_free->prev_free = hdr;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200489 heap.first_free = hdr;
490 }
491
Paul Bakker6e339b52013-07-03 13:37:05 +0200492#if defined(POLARSSL_MEMORY_BACKTRACE)
493 hdr->trace = NULL;
494 hdr->trace_count = 0;
495#endif
496
497 if( ( heap.verify & MEMORY_VERIFY_FREE ) && verify_chain() != 0 )
498 exit( 1 );
499}
500
Paul Bakkerbf796ac2013-09-28 11:06:38 +0200501void memory_buffer_set_verify( int verify )
502{
503 heap.verify = verify;
504}
505
Paul Bakker6e339b52013-07-03 13:37:05 +0200506int memory_buffer_alloc_verify()
507{
508 return verify_chain();
509}
510
511#if defined(POLARSSL_MEMORY_DEBUG)
512void memory_buffer_alloc_status()
513{
Paul Bakker7dc4c442014-02-01 22:50:26 +0100514 polarssl_fprintf( stderr,
Manuel Pégourié-Gonnard97884a32014-07-12 02:27:35 +0200515 "Current use: %zu blocks / %zu bytes, max: %zu blocks / "
516 "%zu bytes (total %zu bytes), malloc / free: %zu / %zu\n",
Paul Bakker7dc4c442014-02-01 22:50:26 +0100517 heap.header_count, heap.total_used,
518 heap.maximum_header_count, heap.maximum_used,
519 heap.maximum_header_count * sizeof( memory_header )
520 + heap.maximum_used,
521 heap.malloc_count, heap.free_count );
Paul Bakker891998e2013-07-03 14:45:05 +0200522
Paul Bakker6e339b52013-07-03 13:37:05 +0200523 if( heap.first->next == NULL )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100524 polarssl_fprintf( stderr, "All memory de-allocated in stack buffer\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200525 else
526 {
Paul Bakker7dc4c442014-02-01 22:50:26 +0100527 polarssl_fprintf( stderr, "Memory currently allocated:\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200528 debug_chain();
529 }
530}
Paul Bakker119602b2014-02-05 15:42:55 +0100531#endif /* POLARSSL_MEMORY_DEBUG */
Paul Bakker6e339b52013-07-03 13:37:05 +0200532
Paul Bakker1337aff2013-09-29 14:45:34 +0200533#if defined(POLARSSL_THREADING_C)
534static void *buffer_alloc_malloc_mutexed( size_t len )
535{
536 void *buf;
537 polarssl_mutex_lock( &heap.mutex );
538 buf = buffer_alloc_malloc( len );
539 polarssl_mutex_unlock( &heap.mutex );
540 return( buf );
541}
542
543static void buffer_alloc_free_mutexed( void *ptr )
544{
545 polarssl_mutex_lock( &heap.mutex );
546 buffer_alloc_free( ptr );
547 polarssl_mutex_unlock( &heap.mutex );
548}
Paul Bakker9af723c2014-05-01 13:03:14 +0200549#endif /* POLARSSL_THREADING_C */
Paul Bakker1337aff2013-09-29 14:45:34 +0200550
Paul Bakker6e339b52013-07-03 13:37:05 +0200551int memory_buffer_alloc_init( unsigned char *buf, size_t len )
552{
Paul Bakker6e339b52013-07-03 13:37:05 +0200553 memset( &heap, 0, sizeof(buffer_alloc_ctx) );
554 memset( buf, 0, len );
555
Paul Bakker1337aff2013-09-29 14:45:34 +0200556#if defined(POLARSSL_THREADING_C)
557 polarssl_mutex_init( &heap.mutex );
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100558 platform_set_malloc_free( buffer_alloc_malloc_mutexed,
559 buffer_alloc_free_mutexed );
Paul Bakker1337aff2013-09-29 14:45:34 +0200560#else
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100561 platform_set_malloc_free( buffer_alloc_malloc, buffer_alloc_free );
Paul Bakker1337aff2013-09-29 14:45:34 +0200562#endif
563
Manuel Pégourié-Gonnard82a5de72014-05-05 14:05:24 +0200564 if( (size_t) buf % POLARSSL_MEMORY_ALIGN_MULTIPLE )
565 {
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100566 /* Adjust len first since buf is used in the computation */
567 len -= POLARSSL_MEMORY_ALIGN_MULTIPLE
568 - (size_t) buf % POLARSSL_MEMORY_ALIGN_MULTIPLE;
Manuel Pégourié-Gonnard82a5de72014-05-05 14:05:24 +0200569 buf += POLARSSL_MEMORY_ALIGN_MULTIPLE
570 - (size_t) buf % POLARSSL_MEMORY_ALIGN_MULTIPLE;
Manuel Pégourié-Gonnard82a5de72014-05-05 14:05:24 +0200571 }
572
Paul Bakker6e339b52013-07-03 13:37:05 +0200573 heap.buf = buf;
574 heap.len = len;
575
576 heap.first = (memory_header *) buf;
577 heap.first->size = len - sizeof(memory_header);
578 heap.first->magic1 = MAGIC1;
579 heap.first->magic2 = MAGIC2;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200580 heap.first_free = heap.first;
Paul Bakker6e339b52013-07-03 13:37:05 +0200581 return( 0 );
582}
583
Paul Bakker1337aff2013-09-29 14:45:34 +0200584void memory_buffer_alloc_free()
585{
586#if defined(POLARSSL_THREADING_C)
587 polarssl_mutex_free( &heap.mutex );
588#endif
Paul Bakker34617722014-06-13 17:20:13 +0200589 polarssl_zeroize( &heap, sizeof(buffer_alloc_ctx) );
Paul Bakker1337aff2013-09-29 14:45:34 +0200590}
591
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100592#if defined(POLARSSL_SELF_TEST)
593static int check_pointer( void *p )
594{
595 if( p == NULL )
596 return( -1 );
597
598 if( (size_t) p % POLARSSL_MEMORY_ALIGN_MULTIPLE != 0 )
599 return( -1 );
600
601 return( 0 );
602}
603
604static int check_all_free( )
605{
606 if( heap.current_alloc_size != 0 ||
607 heap.first != heap.first_free ||
608 (void *) heap.first != (void *) heap.buf )
609 {
610 return( -1 );
611 }
612
613 return( 0 );
614}
615
616#define TEST_ASSERT( condition ) \
617 if( ! (condition) ) \
618 { \
619 if( verbose != 0 ) \
620 polarssl_printf( "failed\n" ); \
621 \
622 ret = 1; \
623 goto cleanup; \
624 }
625
626int memory_buffer_alloc_self_test( int verbose )
627{
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100628 unsigned char buf[1024];
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100629 unsigned char *p, *q, *r, *end;
630 int ret = 0;
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100631
632 if( verbose != 0 )
633 polarssl_printf( " MBA test #1 (basic alloc-free cycle): " );
634
635 memory_buffer_alloc_init( buf, sizeof( buf ) );
636
637 p = polarssl_malloc( 1 );
638 q = polarssl_malloc( 128 );
639 r = polarssl_malloc( 16 );
640
641 TEST_ASSERT( check_pointer( p ) == 0 &&
642 check_pointer( q ) == 0 &&
643 check_pointer( r ) == 0 );
644
645 polarssl_free( r );
646 polarssl_free( q );
647 polarssl_free( p );
648
649 TEST_ASSERT( check_all_free( ) == 0 );
650
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100651 /* Memorize end to compare with the next test */
652 end = heap.buf + heap.len;
653
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100654 memory_buffer_alloc_free( );
655
656 if( verbose != 0 )
657 polarssl_printf( "passed\n" );
658
659 if( verbose != 0 )
660 polarssl_printf( " MBA test #2 (buf not aligned): " );
661
662 memory_buffer_alloc_init( buf + 1, sizeof( buf ) - 1 );
663
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100664 TEST_ASSERT( heap.buf + heap.len == end );
665
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100666 p = polarssl_malloc( 1 );
667 q = polarssl_malloc( 128 );
668 r = polarssl_malloc( 16 );
669
670 TEST_ASSERT( check_pointer( p ) == 0 &&
671 check_pointer( q ) == 0 &&
672 check_pointer( r ) == 0 );
673
674 polarssl_free( r );
675 polarssl_free( q );
676 polarssl_free( p );
677
678 TEST_ASSERT( check_all_free( ) == 0 );
679
680 memory_buffer_alloc_free( );
681
682 if( verbose != 0 )
683 polarssl_printf( "passed\n" );
684
685 if( verbose != 0 )
686 polarssl_printf( " MBA test #3 (full): " );
687
688 memory_buffer_alloc_init( buf, sizeof( buf ) );
689
690 p = polarssl_malloc( sizeof( buf ) - sizeof( memory_header ) );
691
692 TEST_ASSERT( check_pointer( p ) == 0 );
693 TEST_ASSERT( polarssl_malloc( 1 ) == NULL );
694
695 polarssl_free( p );
696
697 p = polarssl_malloc( sizeof( buf ) - 2 * sizeof( memory_header ) - 16 );
698 q = polarssl_malloc( 16 );
699
700 TEST_ASSERT( check_pointer( p ) == 0 && check_pointer( q ) == 0 );
701 TEST_ASSERT( polarssl_malloc( 1 ) == NULL );
702
703 polarssl_free( q );
704
705 TEST_ASSERT( polarssl_malloc( 17 ) == NULL );
706
707 polarssl_free( p );
708
709 TEST_ASSERT( check_all_free( ) == 0 );
710
711 memory_buffer_alloc_free( );
712
713 if( verbose != 0 )
714 polarssl_printf( "passed\n" );
715
716cleanup:
717 memory_buffer_alloc_free( );
718
719 return( ret );
720}
721#endif /* POLARSSL_SELF_TEST */
722
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100723#endif /* POLARSSL_MEMORY_BUFFER_ALLOC_C */