Adapt the platform layer from malloc to calloc

This commit is contained in:
Manuel Pégourié-Gonnard 2015-05-26 16:15:20 +02:00
parent 7551cb9ee9
commit b9ef1182f3
3 changed files with 31 additions and 30 deletions

View File

@ -78,17 +78,17 @@
* *
* Enable the memory allocation layer. * 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 * This allows different allocators (self-implemented or provided) to be
* provided to the platform abstraction layer. * provided to the platform abstraction layer.
* *
* Enabling MBEDTLS_PLATFORM_MEMORY without the * Enabling MBEDTLS_PLATFORM_MEMORY without the
* MBEDTLS_PLATFORM_{FREE,MALLOC}_MACROs will provide * MBEDTLS_PLATFORM_{FREE,CALLOC}_MACROs will provide
* "mbedtls_platform_set_malloc_free()" allowing you to set an alternative malloc() and * "mbedtls_platform_set_calloc_free()" allowing you to set an alternative calloc() and
* free() function pointer at runtime. * free() function pointer at runtime.
* *
* Enabling MBEDTLS_PLATFORM_MEMORY and specifying * 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. * alternate function at compile time.
* *
* Requires: MBEDTLS_PLATFORM_C * Requires: MBEDTLS_PLATFORM_C
@ -100,8 +100,8 @@
/** /**
* \def MBEDTLS_PLATFORM_NO_STD_FUNCTIONS * \def MBEDTLS_PLATFORM_NO_STD_FUNCTIONS
* *
* Do not assign standard functions in the platform layer (e.g. malloc() to * Do not assign standard functions in the platform layer (e.g. calloc() to
* MBEDTLS_PLATFORM_STD_MALLOC and printf() to MBEDTLS_PLATFORM_STD_PRINTF) * 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 * 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 * these functions. You will HAVE to provide alternatives, either at runtime
@ -1788,7 +1788,7 @@
* \def MBEDTLS_MEMORY_BUFFER_ALLOC_C * \def MBEDTLS_MEMORY_BUFFER_ALLOC_C
* *
* Enable the buffer allocator implementation that makes use of a (stack) * 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) * calls)
* *
* Module: library/memory_buffer_alloc.c * Module: library/memory_buffer_alloc.c
@ -1975,7 +1975,7 @@
* \def MBEDTLS_PLATFORM_C * \def MBEDTLS_PLATFORM_C
* *
* Enable the platform abstraction layer that allows you to re-assign * 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 * Enabling MBEDTLS_PLATFORM_C enables to use of MBEDTLS_PLATFORM_XXX_ALT
* or MBEDTLS_PLATFORM_XXX_MACRO directives, allowing the functions mentioned * or MBEDTLS_PLATFORM_XXX_MACRO directives, allowing the functions mentioned
@ -1986,7 +1986,7 @@
* *
* This module enables abstraction of common (libc) functions. * This module enables abstraction of common (libc) functions.
*/ */
//#define MBEDTLS_PLATFORM_C #define MBEDTLS_PLATFORM_C
/** /**
* \def MBEDTLS_RIPEMD160_C * \def MBEDTLS_RIPEMD160_C
@ -2345,7 +2345,7 @@
/* Platform options */ /* Platform options */
//#define MBEDTLS_PLATFORM_STD_MEM_HDR <stdlib.h> /**< Header to include if MBEDTLS_PLATFORM_NO_STD_FUNCTIONS is defined. Don't define if no header is needed. */ //#define MBEDTLS_PLATFORM_STD_MEM_HDR <stdlib.h> /**< 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_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_EXIT exit /**< Default exit to use, can be undefined */
//#define MBEDTLS_PLATFORM_STD_FPRINTF fprintf /**< Default fprintf 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 */ /* To Use Function Macros MBEDTLS_PLATFORM_C must be enabled */
/* MBEDTLS_PLATFORM_XXX_MACRO and MBEDTLS_PLATFORM_XXX_ALT cannot both be defined */ /* 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_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_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 */ //#define MBEDTLS_PLATFORM_FPRINTF_MACRO fprintf /**< Default fprintf macro to use, can be undefined */

View File

@ -54,8 +54,8 @@ extern "C" {
#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF) #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
#define MBEDTLS_PLATFORM_STD_FPRINTF fprintf /**< Default fprintf to use */ #define MBEDTLS_PLATFORM_STD_FPRINTF fprintf /**< Default fprintf to use */
#endif #endif
#if !defined(MBEDTLS_PLATFORM_STD_MALLOC) #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
#define MBEDTLS_PLATFORM_STD_MALLOC malloc /**< Default allocator to use */ #define MBEDTLS_PLATFORM_STD_CALLOC calloc /**< Default allocator to use */
#endif #endif
#if !defined(MBEDTLS_PLATFORM_STD_FREE) #if !defined(MBEDTLS_PLATFORM_STD_FREE)
#define MBEDTLS_PLATFORM_STD_FREE free /**< Default free to use */ #define MBEDTLS_PLATFORM_STD_FREE free /**< Default free to use */
@ -72,32 +72,32 @@ extern "C" {
/* \} name SECTION: Module settings */ /* \} 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_MEMORY)
#if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \ #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_free MBEDTLS_PLATFORM_FREE_MACRO
#define mbedtls_malloc MBEDTLS_PLATFORM_MALLOC_MACRO #define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
#else #else
extern void * (*mbedtls_malloc)( size_t len ); extern void * (*mbedtls_calloc)( size_t n, size_t size );
extern void (*mbedtls_free)( void *ptr ); extern void (*mbedtls_free)( void *ptr );
/** /**
* \brief Set your own memory implementation function pointers * \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 * \param free_func the free function implementation
* *
* \return 0 if successful * \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 * ) ); 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 */ #else /* !MBEDTLS_PLATFORM_MEMORY */
#define mbedtls_free free #define mbedtls_free free
#define mbedtls_malloc malloc #define mbedtls_calloc calloc
#endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,MALLOC}_MACRO */ #endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
/* /*
* The function pointers for fprintf * The function pointers for fprintf

View File

@ -31,15 +31,16 @@
#include "mbedtls/platform.h" #include "mbedtls/platform.h"
#if defined(MBEDTLS_PLATFORM_MEMORY) #if defined(MBEDTLS_PLATFORM_MEMORY)
#if !defined(MBEDTLS_PLATFORM_STD_MALLOC) #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
static void *platform_malloc_uninit( size_t len ) static void *platform_calloc_uninit( size_t n, size_t size )
{ {
((void) len); ((void) n);
((void) size);
return( NULL ); return( NULL );
} }
#define MBEDTLS_PLATFORM_STD_MALLOC platform_malloc_uninit #define MBEDTLS_PLATFORM_STD_CALLOC platform_calloc_uninit
#endif /* !MBEDTLS_PLATFORM_STD_MALLOC */ #endif /* !MBEDTLS_PLATFORM_STD_CALLOC */
#if !defined(MBEDTLS_PLATFORM_STD_FREE) #if !defined(MBEDTLS_PLATFORM_STD_FREE)
static void platform_free_uninit( void *ptr ) 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 #define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit
#endif /* !MBEDTLS_PLATFORM_STD_FREE */ #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; 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 * ) ) void (*free_func)( void * ) )
{ {
mbedtls_malloc = malloc_func; mbedtls_calloc = calloc_func;
mbedtls_free = free_func; mbedtls_free = free_func;
return( 0 ); return( 0 );
} }