Merge pull request #744 from smartmeio/threadsafe_realloc-calloc

Implemented thread-safe realloc and calloc
This commit is contained in:
Ha Thach 2023-11-30 16:50:32 +07:00 committed by GitHub
commit cdfed5ad0a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 1 deletions

View file

@ -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;

View file

@ -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;
}

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 -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