Skip to content

Commit

Permalink
Merge pull request #744 from smartmeio/threadsafe_realloc-calloc
Browse files Browse the repository at this point in the history
Implemented thread-safe realloc and calloc
  • Loading branch information
hathach committed Nov 30, 2023
2 parents 6f5cee2 + b4427ae commit cdfed5a
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
2 changes: 2 additions & 0 deletions cores/nRF5/freertos/Source/include/portable.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
63 changes: 63 additions & 0 deletions cores/nRF5/freertos/Source/portable/MemMang/heap_3.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion platform.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cdfed5a

Please sign in to comment.