| 574 | #endif /* MBEDTLS_THREADING_C */ |
| 575 | |
| 576 | void mbedtls_memory_buffer_alloc_init( unsigned char *buf, size_t len ) |
| 577 | { |
| 578 | memset( &heap, 0, sizeof(buffer_alloc_ctx) ); |
| 579 | memset( buf, 0, len ); |
| 580 | |
| 581 | #if defined(MBEDTLS_THREADING_C) |
| 582 | mbedtls_mutex_init( &heap.mutex ); |
| 583 | mbedtls_platform_set_calloc_free( buffer_alloc_calloc_mutexed, |
| 584 | buffer_alloc_free_mutexed ); |
| 585 | #else |
| 586 | mbedtls_platform_set_calloc_free( buffer_alloc_calloc, buffer_alloc_free ); |
| 587 | #endif |
| 588 | |
| 589 | if( (size_t) buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE ) |
| 590 | { |
| 591 | /* Adjust len first since buf is used in the computation */ |
| 592 | len -= MBEDTLS_MEMORY_ALIGN_MULTIPLE |
| 593 | - (size_t) buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE; |
| 594 | buf += MBEDTLS_MEMORY_ALIGN_MULTIPLE |
| 595 | - (size_t) buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE; |
| 596 | } |
| 597 | |
| 598 | heap.buf = buf; |
| 599 | heap.len = len; |
| 600 | |
| 601 | heap.first = (memory_header *) buf; |
| 602 | heap.first->size = len - sizeof(memory_header); |
| 603 | heap.first->magic1 = MAGIC1; |
| 604 | heap.first->magic2 = MAGIC2; |
| 605 | heap.first_free = heap.first; |
| 606 | } |
| 607 | |
| 608 | void mbedtls_memory_buffer_alloc_free() |
| 609 | { |