implemnent thread-safe malloc/free using --wrap linker option

This commit is contained in:
hathach committed 2021-08-25 11:31:14 +07:00
1 parent c11f3e9ff3
commit 660ca0724d
3 files changed
+20 -12

No files matched your search

@@ -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();
+2 -9
View File
@@ -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
+1 -1
View File
@@ -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