Skip to content

Commit cdfed5a

Browse files
authored
Merge pull request #744 from smartmeio/threadsafe_realloc-calloc
Implemented thread-safe realloc and calloc
2 parents 6f5cee2 + b4427ae commit cdfed5a

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

cores/nRF5/freertos/Source/include/portable.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ void vPortDefineHeapRegions( const HeapRegion_t * const pxHeapRegions ) PRIVILEG
128128
*/
129129
void *pvPortMalloc( size_t xSize ) PRIVILEGED_FUNCTION;
130130
void vPortFree( void *pv ) PRIVILEGED_FUNCTION;
131+
void *pvPortRealloc( void *ptr, size_t xWantedSize ) PRIVILEGED_FUNCTION;
132+
void *pvPortCalloc( size_t nmemb, size_t xWantedSize ) PRIVILEGED_FUNCTION;
131133
void vPortInitialiseBlocks( void ) PRIVILEGED_FUNCTION;
132134
size_t xPortGetFreeHeapSize( void ) PRIVILEGED_FUNCTION;
133135
size_t xPortGetMinimumEverFreeHeapSize( void ) PRIVILEGED_FUNCTION;

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ task.h is included from an application file. */
5959
// require "-Wl,--wrap=malloc -Wl,--wrap=free" linker option
6060
extern void *__real_malloc(size_t size);
6161
extern void __real_free(void *ptr);
62+
extern void *__real_realloc(void *ptr, size_t _size);
63+
extern void *__real_calloc(size_t nmemb, size_t _size);
6264

6365
void* __wrap_malloc (size_t c)
6466
{
@@ -70,6 +72,17 @@ void __wrap_free (void *ptr)
7072
return (xTaskGetSchedulerState() == taskSCHEDULER_RUNNING) ? vPortFree(ptr) : __real_free(ptr);
7173
}
7274

75+
void* __wrap_realloc (void *ptr, size_t c)
76+
{
77+
return (xTaskGetSchedulerState() == taskSCHEDULER_RUNNING) ? pvPortRealloc(ptr, c) : __real_realloc(ptr, c);
78+
}
79+
80+
void* __wrap_calloc (size_t nmemb, size_t c)
81+
{
82+
return (xTaskGetSchedulerState() == taskSCHEDULER_RUNNING) ? pvPortCalloc(nmemb, c) : __real_calloc(nmemb, c);
83+
}
84+
85+
7386
void *pvPortMalloc( size_t xWantedSize )
7487
{
7588
void *pvReturn;
@@ -107,3 +120,53 @@ void vPortFree( void *pv )
107120
( void ) xTaskResumeAll();
108121
}
109122
}
123+
/*-----------------------------------------------------------*/
124+
125+
void *pvPortRealloc( void *ptr, size_t xWantedSize )
126+
{
127+
void *pvReturn;
128+
129+
vTaskSuspendAll();
130+
{
131+
pvReturn = __real_realloc( ptr, xWantedSize );
132+
traceMALLOC( pvReturn, xWantedSize );
133+
}
134+
( void ) xTaskResumeAll();
135+
136+
#if( configUSE_MALLOC_FAILED_HOOK == 1 )
137+
{
138+
if( pvReturn == NULL )
139+
{
140+
extern void vApplicationMallocFailedHook( void );
141+
vApplicationMallocFailedHook();
142+
}
143+
}
144+
#endif
145+
146+
return pvReturn;
147+
}
148+
/*-----------------------------------------------------------*/
149+
150+
void *pvPortCalloc( size_t nmemb, size_t xWantedSize )
151+
{
152+
void *pvReturn;
153+
154+
vTaskSuspendAll();
155+
{
156+
pvReturn = __real_calloc( nmemb, xWantedSize );
157+
traceMALLOC( pvReturn, xWantedSize );
158+
}
159+
( void ) xTaskResumeAll();
160+
161+
#if( configUSE_MALLOC_FAILED_HOOK == 1 )
162+
{
163+
if( pvReturn == NULL )
164+
{
165+
extern void vApplicationMallocFailedHook( void );
166+
vApplicationMallocFailedHook();
167+
}
168+
}
169+
#endif
170+
171+
return pvReturn;
172+
}

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 -Wl,--wrap=malloc -Wl,--wrap=free --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 -Wl,--wrap=realloc -Wl,--wrap=calloc --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)