Skip to content

Commit 660ca07

Browse files
committed
implemnent thread-safe malloc/free using --wrap linker option
1 parent c11f3e9 commit 660ca07

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

cores/nRF5/freertos/Source/portable/MemMang/heap_3.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,28 @@ task.h is included from an application file. */
5555

5656
/*-----------------------------------------------------------*/
5757

58+
// link to libnano's malloc
59+
// require "-Wl,--wrap=malloc -Wl,--wrap=free" linker option
60+
extern void *__real_malloc(size_t size);
61+
extern void __real_free(void *ptr);
62+
63+
void* __wrap_malloc (size_t c)
64+
{
65+
return (xTaskGetSchedulerState() == taskSCHEDULER_RUNNING) ? pvPortMalloc(c) : __real_malloc(c);
66+
}
67+
68+
void __wrap_free (void *ptr)
69+
{
70+
return (xTaskGetSchedulerState() == taskSCHEDULER_RUNNING) ? vPortFree(ptr) : __real_free(ptr);
71+
}
72+
5873
void *pvPortMalloc( size_t xWantedSize )
5974
{
6075
void *pvReturn;
6176

6277
vTaskSuspendAll();
6378
{
64-
pvReturn = malloc( xWantedSize );
79+
pvReturn = __real_malloc( xWantedSize );
6580
traceMALLOC( pvReturn, xWantedSize );
6681
}
6782
( void ) xTaskResumeAll();
@@ -86,7 +101,7 @@ void vPortFree( void *pv )
86101
{
87102
vTaskSuspendAll();
88103
{
89-
free( pv );
104+
__real_free( pv );
90105
traceFREE( pv, 0 );
91106
}
92107
( void ) xTaskResumeAll();

cores/nRF5/rtos.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,8 @@ enum
7474
#define rtos_malloc_type(_type) ((_type*) rtos_malloc(sizeof(_type)))
7575
#endif
7676

77-
static inline void* rtos_malloc(size_t _size)
78-
{
79-
return (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED) ? malloc(_size) : pvPortMalloc(_size);
80-
}
81-
82-
static inline void rtos_free( void *pv )
83-
{
84-
return (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED) ? free(pv) : vPortFree(pv);
85-
}
77+
#define rtos_malloc malloc
78+
#define rtos_free free
8679

8780
// Visible only with C++
8881
#ifdef __cplusplus

platform.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ compiler.elf2bin.flags=-O binary
4949
compiler.elf2bin.cmd=arm-none-eabi-objcopy
5050
compiler.elf2hex.flags=-O ihex
5151
compiler.elf2hex.cmd=arm-none-eabi-objcopy
52-
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
52+
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
5353
compiler.size.cmd=arm-none-eabi-size
5454

5555
# this can be overriden in boards.txt

0 commit comments

Comments
 (0)