From 660ca0724dc53a4e2a379fc6532a347439542815 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 25 Aug 2021 11:31:14 +0700 Subject: [PATCH] implemnent thread-safe malloc/free using --wrap linker option --- .../freertos/Source/portable/MemMang/heap_3.c | 19 +++++++++++++++++-- cores/nRF5/rtos.h | 11 ++--------- platform.txt | 2 +- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/cores/nRF5/freertos/Source/portable/MemMang/heap_3.c b/cores/nRF5/freertos/Source/portable/MemMang/heap_3.c index 99837499..4edae90f 100644 --- a/cores/nRF5/freertos/Source/portable/MemMang/heap_3.c +++ b/cores/nRF5/freertos/Source/portable/MemMang/heap_3.c @@ -55,13 +55,28 @@ task.h is included from an application file. */ /*-----------------------------------------------------------*/ +// link to libnano's malloc +// require "-Wl,--wrap=malloc -Wl,--wrap=free" linker option +extern void *__real_malloc(size_t size); +extern void __real_free(void *ptr); + +void* __wrap_malloc (size_t c) +{ + return (xTaskGetSchedulerState() == taskSCHEDULER_RUNNING) ? pvPortMalloc(c) : __real_malloc(c); +} + +void __wrap_free (void *ptr) +{ + return (xTaskGetSchedulerState() == taskSCHEDULER_RUNNING) ? vPortFree(ptr) : __real_free(ptr); +} + void *pvPortMalloc( size_t xWantedSize ) { void *pvReturn; vTaskSuspendAll(); { - pvReturn = malloc( xWantedSize ); + pvReturn = __real_malloc( xWantedSize ); traceMALLOC( pvReturn, xWantedSize ); } ( void ) xTaskResumeAll(); @@ -86,7 +101,7 @@ void vPortFree( void *pv ) { vTaskSuspendAll(); { - free( pv ); + __real_free( pv ); traceFREE( pv, 0 ); } ( void ) xTaskResumeAll(); diff --git a/cores/nRF5/rtos.h b/cores/nRF5/rtos.h index e193c143..5715cbdf 100644 --- a/cores/nRF5/rtos.h +++ b/cores/nRF5/rtos.h @@ -74,15 +74,8 @@ enum #define rtos_malloc_type(_type) ((_type*) rtos_malloc(sizeof(_type))) #endif -static inline void* rtos_malloc(size_t _size) -{ - return (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED) ? malloc(_size) : pvPortMalloc(_size); -} - -static inline void rtos_free( void *pv ) -{ - return (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED) ? free(pv) : vPortFree(pv); -} +#define rtos_malloc malloc +#define rtos_free free // Visible only with C++ #ifdef __cplusplus diff --git a/platform.txt b/platform.txt index 3e1ae680..104ea12b 100644 --- a/platform.txt +++ b/platform.txt @@ -49,7 +49,7 @@ compiler.elf2bin.flags=-O binary compiler.elf2bin.cmd=arm-none-eabi-objcopy compiler.elf2hex.flags=-O ihex compiler.elf2hex.cmd=arm-none-eabi-objcopy -compiler.ldflags=-mcpu={build.mcu} -mthumb {build.float_flags} -Wl,--cref -Wl,--check-sections -Wl,--gc-sections -Wl,--unresolved-symbols=report-all -Wl,--warn-common -Wl,--warn-section-align --specs=nano.specs --specs=nosys.specs +compiler.ldflags=-mcpu={build.mcu} -mthumb {build.float_flags} -Wl,--cref -Wl,--check-sections -Wl,--gc-sections -Wl,--unresolved-symbols=report-all -Wl,--warn-common -Wl,--warn-section-align -Wl,--wrap=malloc -Wl,--wrap=free --specs=nano.specs --specs=nosys.specs compiler.size.cmd=arm-none-eabi-size # this can be overriden in boards.txt