From b4427ae100aa2215dfb3c70fc3723a20c55b8ef9 Mon Sep 17 00:00:00 2001 From: Dario Gogliandolo Date: Mon, 3 Oct 2022 12:50:58 +0200 Subject: [PATCH] Implemented thread-safe realloc and calloc --- cores/nRF5/freertos/Source/include/portable.h | 2 + .../freertos/Source/portable/MemMang/heap_3.c | 63 +++++++++++++++++++ platform.txt | 2 +- 3 files changed, 66 insertions(+), 1 deletion(-) diff --git a/cores/nRF5/freertos/Source/include/portable.h b/cores/nRF5/freertos/Source/include/portable.h index 8aac353e..8209f050 100644 --- a/cores/nRF5/freertos/Source/include/portable.h +++ b/cores/nRF5/freertos/Source/include/portable.h @@ -128,6 +128,8 @@ void vPortDefineHeapRegions( const HeapRegion_t * const pxHeapRegions ) PRIVILEG */ void *pvPortMalloc( size_t xSize ) PRIVILEGED_FUNCTION; void vPortFree( void *pv ) PRIVILEGED_FUNCTION; +void *pvPortRealloc( void *ptr, size_t xWantedSize ) PRIVILEGED_FUNCTION; +void *pvPortCalloc( size_t nmemb, size_t xWantedSize ) PRIVILEGED_FUNCTION; void vPortInitialiseBlocks( void ) PRIVILEGED_FUNCTION; size_t xPortGetFreeHeapSize( void ) PRIVILEGED_FUNCTION; size_t xPortGetMinimumEverFreeHeapSize( void ) PRIVILEGED_FUNCTION; diff --git a/cores/nRF5/freertos/Source/portable/MemMang/heap_3.c b/cores/nRF5/freertos/Source/portable/MemMang/heap_3.c index 4edae90f..1d07d7f2 100644 --- a/cores/nRF5/freertos/Source/portable/MemMang/heap_3.c +++ b/cores/nRF5/freertos/Source/portable/MemMang/heap_3.c @@ -59,6 +59,8 @@ task.h is included from an application file. */ // require "-Wl,--wrap=malloc -Wl,--wrap=free" linker option extern void *__real_malloc(size_t size); extern void __real_free(void *ptr); +extern void *__real_realloc(void *ptr, size_t _size); +extern void *__real_calloc(size_t nmemb, size_t _size); void* __wrap_malloc (size_t c) { @@ -70,6 +72,17 @@ void __wrap_free (void *ptr) return (xTaskGetSchedulerState() == taskSCHEDULER_RUNNING) ? vPortFree(ptr) : __real_free(ptr); } +void* __wrap_realloc (void *ptr, size_t c) +{ + return (xTaskGetSchedulerState() == taskSCHEDULER_RUNNING) ? pvPortRealloc(ptr, c) : __real_realloc(ptr, c); +} + +void* __wrap_calloc (size_t nmemb, size_t c) +{ + return (xTaskGetSchedulerState() == taskSCHEDULER_RUNNING) ? pvPortCalloc(nmemb, c) : __real_calloc(nmemb, c); +} + + void *pvPortMalloc( size_t xWantedSize ) { void *pvReturn; @@ -107,3 +120,53 @@ void vPortFree( void *pv ) ( void ) xTaskResumeAll(); } } +/*-----------------------------------------------------------*/ + +void *pvPortRealloc( void *ptr, size_t xWantedSize ) +{ +void *pvReturn; + + vTaskSuspendAll(); + { + pvReturn = __real_realloc( ptr, xWantedSize ); + traceMALLOC( pvReturn, xWantedSize ); + } + ( void ) xTaskResumeAll(); + + #if( configUSE_MALLOC_FAILED_HOOK == 1 ) + { + if( pvReturn == NULL ) + { + extern void vApplicationMallocFailedHook( void ); + vApplicationMallocFailedHook(); + } + } + #endif + + return pvReturn; +} +/*-----------------------------------------------------------*/ + +void *pvPortCalloc( size_t nmemb, size_t xWantedSize ) +{ +void *pvReturn; + + vTaskSuspendAll(); + { + pvReturn = __real_calloc( nmemb, xWantedSize ); + traceMALLOC( pvReturn, xWantedSize ); + } + ( void ) xTaskResumeAll(); + + #if( configUSE_MALLOC_FAILED_HOOK == 1 ) + { + if( pvReturn == NULL ) + { + extern void vApplicationMallocFailedHook( void ); + vApplicationMallocFailedHook(); + } + } + #endif + + return pvReturn; +} \ No newline at end of file diff --git a/platform.txt b/platform.txt index 77411e28..a7b31bf8 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 -Wl,--wrap=malloc -Wl,--wrap=free --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 -Wl,--wrap=realloc -Wl,--wrap=calloc --specs=nano.specs --specs=nosys.specs compiler.size.cmd=arm-none-eabi-size # this can be overriden in boards.txt