Skip to content

Commit 80297bb

Browse files
authored
Merge pull request #687 from adafruit/wrap-malloc
implemnent thread-safe malloc/free using --wrap linker option
2 parents c11f3e9 + 781f3dd commit 80297bb

File tree

6 files changed

+33
-49
lines changed

6 files changed

+33
-49
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/freertos/config/FreeRTOSConfig.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,15 @@ standard names - or at least those used in the unmodified vector table. */
184184

185185
/** Implementation note: Use this with caution and set this to 1 ONLY for debugging
186186
* ----------------------------------------------------------
187-
* Set the value of configUSE_DISABLE_TICK_AUTO_CORRECTION_DEBUG to below for enabling or disabling RTOS tick auto correction:
188-
* 0. This is default. If the RTC tick interrupt is masked for more than 1 tick by higher priority interrupts, then most likely
189-
* one or more RTC ticks are lost. The tick interrupt inside RTOS will detect this and make a correction needed. This is needed
190-
* for the RTOS internal timers to be more accurate.
191-
* 1. The auto correction for RTOS tick is disabled even though few RTC tick interrupts were lost. This feature is desirable when debugging
192-
* the RTOS application and stepping though the code. After stepping when the application is continued in debug mode, the auto-corrections of
193-
* 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
194-
* convenient for debugging.
195-
*/
187+
* Set the value of configUSE_DISABLE_TICK_AUTO_CORRECTION_DEBUG to below for enabling or disabling RTOS tick auto correction:
188+
* 0. This is default. If the RTC tick interrupt is masked for more than 1 tick by higher priority interrupts, then most likely
189+
* one or more RTC ticks are lost. The tick interrupt inside RTOS will detect this and make a correction needed. This is needed
190+
* for the RTOS internal timers to be more accurate.
191+
* 1. The auto correction for RTOS tick is disabled even though few RTC tick interrupts were lost. This feature is desirable when debugging
192+
* the RTOS application and stepping though the code. After stepping when the application is continued in debug mode, the auto-corrections of
193+
* 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
194+
* convenient for debugging.
195+
*/
196196
#define configUSE_DISABLE_TICK_AUTO_CORRECTION_DEBUG 0
197197

198198
// Sysview is enabled by default at debug level 3

cores/nRF5/new.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,4 @@ caddr_t _sbrk( int incr )
7676
return (caddr_t) prev_heap;
7777
}
7878

79-
void __malloc_lock(struct _reent *ptr)
80-
{
81-
(void) ptr;
82-
vTaskSuspendAll();
83-
}
84-
85-
void __malloc_unlock(struct _reent *ptr)
86-
{
87-
(void) ptr;
88-
xTaskResumeAll();
89-
}
90-
9179
}

cores/nRF5/rtos.h

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@
5050
#include "queue.h"
5151
#include "semphr.h"
5252

53-
#define DEBUG_MALLOC 1
54-
5553
#define DELAY_FOREVER portMAX_DELAY
5654

5755
enum
@@ -63,26 +61,13 @@ enum
6361
TASK_PRIO_HIGHEST = 4,
6462
};
6563

66-
#define ms2tick pdMS_TO_TICKS
67-
68-
#define tick2ms(tck) ( ( ((uint64_t)(tck)) * 1000) / configTICK_RATE_HZ )
69-
#define tick2us(tck) ( ( ((uint64_t)(tck)) * 1000000) / configTICK_RATE_HZ )
70-
71-
#if DEBUG_MALLOC
72-
#define rtos_malloc_type(_type) ({ LOG_LV2("MALLOC", #_type " = %d bytes", sizeof(_type)); ((_type*) rtos_malloc(sizeof(_type))); })
73-
#else
74-
#define rtos_malloc_type(_type) ((_type*) rtos_malloc(sizeof(_type)))
75-
#endif
64+
#define ms2tick pdMS_TO_TICKS
65+
#define tick2ms(tck) ( ( ((uint64_t)(tck)) * 1000) / configTICK_RATE_HZ )
66+
#define tick2us(tck) ( ( ((uint64_t)(tck)) * 1000000) / configTICK_RATE_HZ )
7667

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-
}
68+
// legacy thread-safe malloc/free
69+
#define rtos_malloc malloc
70+
#define rtos_free free
8671

8772
// Visible only with C++
8873
#ifdef __cplusplus

keywords.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@ delay_ns KEYWORD2
3636
analogOversampling KEYWORD2
3737
analogReadVDD KEYWORD2
3838

39-
# RTOS
40-
rtos_malloc KEYWORD2
41-
rtos_free KEYWORD2
42-
4339
nrf52_flash_init KEYWORD2
4440
nrf52_flash_erase_sector KEYWORD2
4541
nrf52_flash_write KEYWORD2

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)