Adapt memory_buffer_alloc to calloc

This commit is contained in:
Manuel Pégourié-Gonnard 2015-05-26 17:42:13 +02:00
parent b9ef1182f3
commit 200e73179e
2 changed files with 27 additions and 14 deletions

View File

@ -58,10 +58,10 @@ extern "C" {
/** /**
* \brief Initialize use of stack-based memory allocator. * \brief Initialize use of stack-based memory allocator.
* The stack-based allocator does memory management inside the * The stack-based allocator does memory management inside the
* presented buffer and does not call malloc() and free(). * presented buffer and does not call calloc() and free().
* It sets the global mbedtls_malloc() and mbedtls_free() pointers * It sets the global mbedtls_calloc() and mbedtls_free() pointers
* to its own functions. * to its own functions.
* (Provided mbedtls_malloc() and mbedtls_free() are thread-safe if * (Provided mbedtls_calloc() and mbedtls_free() are thread-safe if
* MBEDTLS_THREADING_C is defined) * MBEDTLS_THREADING_C is defined)
* *
* \note This code is not optimized and provides a straight-forward * \note This code is not optimized and provides a straight-forward

View File

@ -77,7 +77,7 @@ typedef struct
memory_header *first_free; memory_header *first_free;
int verify; int verify;
#if defined(MBEDTLS_MEMORY_DEBUG) #if defined(MBEDTLS_MEMORY_DEBUG)
size_t malloc_count; size_t alloc_count;
size_t free_count; size_t free_count;
size_t total_used; size_t total_used;
size_t maximum_used; size_t maximum_used;
@ -230,10 +230,12 @@ static int verify_chain()
return( 0 ); return( 0 );
} }
static void *buffer_alloc_malloc( size_t len ) static void *buffer_alloc_calloc( size_t n, size_t size )
{ {
memory_header *new, *cur = heap.first_free; memory_header *new, *cur = heap.first_free;
unsigned char *p; unsigned char *p;
void *ret;
size_t original_len, len;
#if defined(MBEDTLS_MEMORY_BACKTRACE) #if defined(MBEDTLS_MEMORY_BACKTRACE)
void *trace_buffer[MAX_BT]; void *trace_buffer[MAX_BT];
size_t trace_cnt; size_t trace_cnt;
@ -242,6 +244,11 @@ static void *buffer_alloc_malloc( size_t len )
if( heap.buf == NULL || heap.first == NULL ) if( heap.buf == NULL || heap.first == NULL )
return( NULL ); return( NULL );
original_len = len = n * size;
if( n != 0 && len / n != size )
return( NULL );
if( len % MBEDTLS_MEMORY_ALIGN_MULTIPLE ) if( len % MBEDTLS_MEMORY_ALIGN_MULTIPLE )
{ {
len -= len % MBEDTLS_MEMORY_ALIGN_MULTIPLE; len -= len % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
@ -271,7 +278,7 @@ static void *buffer_alloc_malloc( size_t len )
} }
#if defined(MBEDTLS_MEMORY_DEBUG) #if defined(MBEDTLS_MEMORY_DEBUG)
heap.malloc_count++; heap.calloc_count++;
#endif #endif
// Found location, split block if > memory_header + 4 room left // Found location, split block if > memory_header + 4 room left
@ -308,7 +315,10 @@ static void *buffer_alloc_malloc( size_t len )
if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 ) if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
mbedtls_exit( 1 ); mbedtls_exit( 1 );
return( ( (unsigned char *) cur ) + sizeof(memory_header) ); ret = (unsigned char *) cur + sizeof( memory_header );
memset( ret, 0, original_len );
return( ret );
} }
p = ( (unsigned char *) cur ) + sizeof(memory_header) + len; p = ( (unsigned char *) cur ) + sizeof(memory_header) + len;
@ -363,7 +373,10 @@ static void *buffer_alloc_malloc( size_t len )
if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 ) if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
mbedtls_exit( 1 ); mbedtls_exit( 1 );
return( ( (unsigned char *) cur ) + sizeof(memory_header) ); ret = (unsigned char *) cur + sizeof( memory_header );
memset( ret, 0, original_len );
return( ret );
} }
static void buffer_alloc_free( void *ptr ) static void buffer_alloc_free( void *ptr )
@ -503,12 +516,12 @@ void mbedtls_memory_buffer_alloc_status()
{ {
mbedtls_fprintf( stderr, mbedtls_fprintf( stderr,
"Current use: %zu blocks / %zu bytes, max: %zu blocks / " "Current use: %zu blocks / %zu bytes, max: %zu blocks / "
"%zu bytes (total %zu bytes), malloc / free: %zu / %zu\n", "%zu bytes (total %zu bytes), alloc / free: %zu / %zu\n",
heap.header_count, heap.total_used, heap.header_count, heap.total_used,
heap.maximum_header_count, heap.maximum_used, heap.maximum_header_count, heap.maximum_used,
heap.maximum_header_count * sizeof( memory_header ) heap.maximum_header_count * sizeof( memory_header )
+ heap.maximum_used, + heap.maximum_used,
heap.malloc_count, heap.free_count ); heap.alloc_count, heap.free_count );
if( heap.first->next == NULL ) if( heap.first->next == NULL )
mbedtls_fprintf( stderr, "All memory de-allocated in stack buffer\n" ); mbedtls_fprintf( stderr, "All memory de-allocated in stack buffer\n" );
@ -539,12 +552,12 @@ void mbedtls_memory_buffer_alloc_cur_get( size_t *cur_used, size_t *cur_blocks )
#endif /* MBEDTLS_MEMORY_DEBUG */ #endif /* MBEDTLS_MEMORY_DEBUG */
#if defined(MBEDTLS_THREADING_C) #if defined(MBEDTLS_THREADING_C)
static void *buffer_alloc_malloc_mutexed( size_t len ) static void *buffer_alloc_calloc_mutexed( size_t n, size_t size )
{ {
void *buf; void *buf;
if( mbedtls_mutex_lock( &heap.mutex ) != 0 ) if( mbedtls_mutex_lock( &heap.mutex ) != 0 )
return( NULL ); return( NULL );
buf = buffer_alloc_malloc( len ); buf = buffer_alloc_calloc( n, size );
if( mbedtls_mutex_unlock( &heap.mutex ) ) if( mbedtls_mutex_unlock( &heap.mutex ) )
return( NULL ); return( NULL );
return( buf ); return( buf );
@ -568,10 +581,10 @@ void mbedtls_memory_buffer_alloc_init( unsigned char *buf, size_t len )
#if defined(MBEDTLS_THREADING_C) #if defined(MBEDTLS_THREADING_C)
mbedtls_mutex_init( &heap.mutex ); mbedtls_mutex_init( &heap.mutex );
mbedtls_platform_set_malloc_free( buffer_alloc_malloc_mutexed, mbedtls_platform_set_calloc_free( buffer_alloc_calloc_mutexed,
buffer_alloc_free_mutexed ); buffer_alloc_free_mutexed );
#else #else
mbedtls_platform_set_malloc_free( buffer_alloc_malloc, buffer_alloc_free ); mbedtls_platform_set_calloc_free( buffer_alloc_calloc, buffer_alloc_free );
#endif #endif
if( (size_t) buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE ) if( (size_t) buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE )