Merge pull request #687 from adafruit/wrap-malloc

implemnent thread-safe malloc/free using --wrap linker option
This commit is contained in:
Ha Thach authored and GitHub committed 2021-08-25 12:51:53 +07:00
commit 80297bb1dc
6 files changed
+33 -49

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();
+9 -9
View File
@@ -184,15 +184,15 @@ standard names - or at least those used in the unmodified vector table. */
/** Implementation note: Use this with caution and set this to 1 ONLY for debugging
* ----------------------------------------------------------
* Set the value of configUSE_DISABLE_TICK_AUTO_CORRECTION_DEBUG to below for enabling or disabling RTOS tick auto correction:
* 0. This is default. If the RTC tick interrupt is masked for more than 1 tick by higher priority interrupts, then most likely
* one or more RTC ticks are lost. The tick interrupt inside RTOS will detect this and make a correction needed. This is needed
* for the RTOS internal timers to be more accurate.
* 1. The auto correction for RTOS tick is disabled even though few RTC tick interrupts were lost. This feature is desirable when debugging
* the RTOS application and stepping though the code. After stepping when the application is continued in debug mode, the auto-corrections of
* RTOS tick might cause asserts. Setting configUSE_DISABLE_TICK_AUTO_CORRECTION_DEBUG to 1 will make RTC and RTOS go out of sync but could be
* convenient for debugging.
*/
* Set the value of configUSE_DISABLE_TICK_AUTO_CORRECTION_DEBUG to below for enabling or disabling RTOS tick auto correction:
* 0. This is default. If the RTC tick interrupt is masked for more than 1 tick by higher priority interrupts, then most likely
* one or more RTC ticks are lost. The tick interrupt inside RTOS will detect this and make a correction needed. This is needed
* for the RTOS internal timers to be more accurate.
* 1. The auto correction for RTOS tick is disabled even though few RTC tick interrupts were lost. This feature is desirable when debugging
* the RTOS application and stepping though the code. After stepping when the application is continued in debug mode, the auto-corrections of
* RTOS tick might cause asserts. Setting configUSE_DISABLE_TICK_AUTO_CORRECTION_DEBUG to 1 will make RTC and RTOS go out of sync but could be
* convenient for debugging.
*/
#define configUSE_DISABLE_TICK_AUTO_CORRECTION_DEBUG 0
// Sysview is enabled by default at debug level 3
-12
View File
@@ -76,16 +76,4 @@ caddr_t _sbrk( int incr )
return (caddr_t) prev_heap;
}
void __malloc_lock(struct _reent *ptr)
{
(void) ptr;
vTaskSuspendAll();
}
void __malloc_unlock(struct _reent *ptr)
{
(void) ptr;
xTaskResumeAll();
}
}
+6 -21
View File
@@ -50,8 +50,6 @@
#include "queue.h"
#include "semphr.h"
#define DEBUG_MALLOC 1
#define DELAY_FOREVER portMAX_DELAY
enum
@@ -63,26 +61,13 @@ enum
TASK_PRIO_HIGHEST = 4,
};
#define ms2tick pdMS_TO_TICKS
#define ms2tick pdMS_TO_TICKS
#define tick2ms(tck) ( ( ((uint64_t)(tck)) * 1000) / configTICK_RATE_HZ )
#define tick2us(tck) ( ( ((uint64_t)(tck)) * 1000000) / configTICK_RATE_HZ )
#define tick2ms(tck) ( ( ((uint64_t)(tck)) * 1000) / configTICK_RATE_HZ )
#define tick2us(tck) ( ( ((uint64_t)(tck)) * 1000000) / configTICK_RATE_HZ )
#if DEBUG_MALLOC
#define rtos_malloc_type(_type) ({ LOG_LV2("MALLOC", #_type " = %d bytes", sizeof(_type)); ((_type*) rtos_malloc(sizeof(_type))); })
#else
#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);
}
// legacy thread-safe malloc/free
#define rtos_malloc malloc
#define rtos_free free
// Visible only with C++
#ifdef __cplusplus
-4
View File
@@ -36,10 +36,6 @@ delay_ns KEYWORD2
analogOversampling KEYWORD2
analogReadVDD KEYWORD2
# RTOS
rtos_malloc KEYWORD2
rtos_free KEYWORD2
nrf52_flash_init KEYWORD2
nrf52_flash_erase_sector KEYWORD2
nrf52_flash_write KEYWORD2
+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