From b9ef1182f379ca1835e1082d282490ec49cf128e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Tue, 26 May 2015 16:15:20 +0200 Subject: [PATCH] Adapt the platform layer from malloc to calloc --- include/mbedtls/config.h | 22 +++++++++++----------- include/mbedtls/platform.h | 22 +++++++++++----------- library/platform.c | 17 +++++++++-------- 3 files changed, 31 insertions(+), 30 deletions(-) diff --git a/include/mbedtls/config.h b/include/mbedtls/config.h index bd28406ec..6c841ae0e 100644 --- a/include/mbedtls/config.h +++ b/include/mbedtls/config.h @@ -78,17 +78,17 @@ * * Enable the memory allocation layer. * - * By default mbed TLS uses the system-provided malloc() and free(). + * By default mbed TLS uses the system-provided calloc() and free(). * This allows different allocators (self-implemented or provided) to be * provided to the platform abstraction layer. * * Enabling MBEDTLS_PLATFORM_MEMORY without the - * MBEDTLS_PLATFORM_{FREE,MALLOC}_MACROs will provide - * "mbedtls_platform_set_malloc_free()" allowing you to set an alternative malloc() and + * MBEDTLS_PLATFORM_{FREE,CALLOC}_MACROs will provide + * "mbedtls_platform_set_calloc_free()" allowing you to set an alternative calloc() and * free() function pointer at runtime. * * Enabling MBEDTLS_PLATFORM_MEMORY and specifying - * MBEDTLS_PLATFORM_{MALLOC,FREE}_MACROs will allow you to specify the + * MBEDTLS_PLATFORM_{CALLOC,FREE}_MACROs will allow you to specify the * alternate function at compile time. * * Requires: MBEDTLS_PLATFORM_C @@ -100,8 +100,8 @@ /** * \def MBEDTLS_PLATFORM_NO_STD_FUNCTIONS * - * Do not assign standard functions in the platform layer (e.g. malloc() to - * MBEDTLS_PLATFORM_STD_MALLOC and printf() to MBEDTLS_PLATFORM_STD_PRINTF) + * Do not assign standard functions in the platform layer (e.g. calloc() to + * MBEDTLS_PLATFORM_STD_CALLOC and printf() to MBEDTLS_PLATFORM_STD_PRINTF) * * This makes sure there are no linking errors on platforms that do not support * these functions. You will HAVE to provide alternatives, either at runtime @@ -1788,7 +1788,7 @@ * \def MBEDTLS_MEMORY_BUFFER_ALLOC_C * * Enable the buffer allocator implementation that makes use of a (stack) - * based buffer to 'allocate' dynamic memory. (replaces malloc() and free() + * based buffer to 'allocate' dynamic memory. (replaces calloc() and free() * calls) * * Module: library/memory_buffer_alloc.c @@ -1975,7 +1975,7 @@ * \def MBEDTLS_PLATFORM_C * * Enable the platform abstraction layer that allows you to re-assign - * functions like malloc(), free(), snprintf(), printf(), fprintf(), exit() + * functions like calloc(), free(), snprintf(), printf(), fprintf(), exit() * * Enabling MBEDTLS_PLATFORM_C enables to use of MBEDTLS_PLATFORM_XXX_ALT * or MBEDTLS_PLATFORM_XXX_MACRO directives, allowing the functions mentioned @@ -1986,7 +1986,7 @@ * * This module enables abstraction of common (libc) functions. */ -//#define MBEDTLS_PLATFORM_C +#define MBEDTLS_PLATFORM_C /** * \def MBEDTLS_RIPEMD160_C @@ -2345,7 +2345,7 @@ /* Platform options */ //#define MBEDTLS_PLATFORM_STD_MEM_HDR /**< Header to include if MBEDTLS_PLATFORM_NO_STD_FUNCTIONS is defined. Don't define if no header is needed. */ -//#define MBEDTLS_PLATFORM_STD_MALLOC malloc /**< Default allocator to use, can be undefined */ +//#define MBEDTLS_PLATFORM_STD_CALLOC calloc /**< Default allocator to use, can be undefined */ //#define MBEDTLS_PLATFORM_STD_FREE free /**< Default free to use, can be undefined */ //#define MBEDTLS_PLATFORM_STD_EXIT exit /**< Default exit to use, can be undefined */ //#define MBEDTLS_PLATFORM_STD_FPRINTF fprintf /**< Default fprintf to use, can be undefined */ @@ -2354,7 +2354,7 @@ /* To Use Function Macros MBEDTLS_PLATFORM_C must be enabled */ /* MBEDTLS_PLATFORM_XXX_MACRO and MBEDTLS_PLATFORM_XXX_ALT cannot both be defined */ -//#define MBEDTLS_PLATFORM_MALLOC_MACRO malloc /**< Default allocator macro to use, can be undefined */ +//#define MBEDTLS_PLATFORM_CALLOC_MACRO calloc /**< Default allocator macro to use, can be undefined */ //#define MBEDTLS_PLATFORM_FREE_MACRO free /**< Default free macro to use, can be undefined */ //#define MBEDTLS_PLATFORM_EXIT_MACRO exit /**< Default exit macro to use, can be undefined */ //#define MBEDTLS_PLATFORM_FPRINTF_MACRO fprintf /**< Default fprintf macro to use, can be undefined */ diff --git a/include/mbedtls/platform.h b/include/mbedtls/platform.h index 765c18fc0..aa97d8d40 100644 --- a/include/mbedtls/platform.h +++ b/include/mbedtls/platform.h @@ -54,8 +54,8 @@ extern "C" { #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF) #define MBEDTLS_PLATFORM_STD_FPRINTF fprintf /**< Default fprintf to use */ #endif -#if !defined(MBEDTLS_PLATFORM_STD_MALLOC) -#define MBEDTLS_PLATFORM_STD_MALLOC malloc /**< Default allocator to use */ +#if !defined(MBEDTLS_PLATFORM_STD_CALLOC) +#define MBEDTLS_PLATFORM_STD_CALLOC calloc /**< Default allocator to use */ #endif #if !defined(MBEDTLS_PLATFORM_STD_FREE) #define MBEDTLS_PLATFORM_STD_FREE free /**< Default free to use */ @@ -72,32 +72,32 @@ extern "C" { /* \} name SECTION: Module settings */ /* - * The function pointers for malloc and free + * The function pointers for calloc and free */ #if defined(MBEDTLS_PLATFORM_MEMORY) #if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \ - defined(MBEDTLS_PLATFORM_MALLOC_MACRO) + defined(MBEDTLS_PLATFORM_CALLOC_MACRO) #define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO -#define mbedtls_malloc MBEDTLS_PLATFORM_MALLOC_MACRO +#define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO #else -extern void * (*mbedtls_malloc)( size_t len ); +extern void * (*mbedtls_calloc)( size_t n, size_t size ); extern void (*mbedtls_free)( void *ptr ); /** * \brief Set your own memory implementation function pointers * - * \param malloc_func the malloc function implementation + * \param calloc_func the calloc function implementation * \param free_func the free function implementation * * \return 0 if successful */ -int mbedtls_platform_set_malloc_free( void * (*malloc_func)( size_t ), +int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ), void (*free_func)( void * ) ); -#endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_MALLOC_MACRO */ +#endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */ #else /* !MBEDTLS_PLATFORM_MEMORY */ #define mbedtls_free free -#define mbedtls_malloc malloc -#endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,MALLOC}_MACRO */ +#define mbedtls_calloc calloc +#endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */ /* * The function pointers for fprintf diff --git a/library/platform.c b/library/platform.c index 788cd1f70..e9ab3024e 100644 --- a/library/platform.c +++ b/library/platform.c @@ -31,15 +31,16 @@ #include "mbedtls/platform.h" #if defined(MBEDTLS_PLATFORM_MEMORY) -#if !defined(MBEDTLS_PLATFORM_STD_MALLOC) -static void *platform_malloc_uninit( size_t len ) +#if !defined(MBEDTLS_PLATFORM_STD_CALLOC) +static void *platform_calloc_uninit( size_t n, size_t size ) { - ((void) len); + ((void) n); + ((void) size); return( NULL ); } -#define MBEDTLS_PLATFORM_STD_MALLOC platform_malloc_uninit -#endif /* !MBEDTLS_PLATFORM_STD_MALLOC */ +#define MBEDTLS_PLATFORM_STD_CALLOC platform_calloc_uninit +#endif /* !MBEDTLS_PLATFORM_STD_CALLOC */ #if !defined(MBEDTLS_PLATFORM_STD_FREE) static void platform_free_uninit( void *ptr ) @@ -50,13 +51,13 @@ static void platform_free_uninit( void *ptr ) #define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit #endif /* !MBEDTLS_PLATFORM_STD_FREE */ -void * (*mbedtls_malloc)( size_t ) = MBEDTLS_PLATFORM_STD_MALLOC; +void * (*mbedtls_calloc)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC; void (*mbedtls_free)( void * ) = MBEDTLS_PLATFORM_STD_FREE; -int mbedtls_platform_set_malloc_free( void * (*malloc_func)( size_t ), +int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ), void (*free_func)( void * ) ) { - mbedtls_malloc = malloc_func; + mbedtls_calloc = calloc_func; mbedtls_free = free_func; return( 0 ); }