Skip to content

Commit f7c63ad

Browse files
committed
Fix heapBytesRemaining default value
configTOTAL_HEAP_SIZE could be not constant Signed-off-by: Frederic.Pillon <[email protected]>
1 parent 1715641 commit f7c63ad

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

portable/MemMang/heap_useNewlib.c

+7-1
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,15 @@
7272
static int totalBytesProvidedBySBRK = 0;
7373
#endif
7474
extern char _end; // Defined in the linker script
75-
static int heapBytesRemaining = configTOTAL_HEAP_SIZE; // that's (&__HeapLimit)-(&__HeapBase)
75+
static int heapBytesRemaining = -1; // configTOTAL_HEAP_SIZE is not constant will be init later
7676

7777
//! sbrk/_sbrk version supporting reentrant newlib (depends upon above symbols defined by linker control file).
7878
char * sbrk(int incr) {
7979
static char *currentHeapEnd = &_end;
8080
vTaskSuspendAll(); // Note: safe to use before FreeRTOS scheduler started
81+
if (heapBytesRemaining == -1) {
82+
heapBytesRemaining = configTOTAL_HEAP_SIZE;
83+
}
8184
char *previousHeapEnd = currentHeapEnd;
8285
if (currentHeapEnd + incr > &_end + configTOTAL_HEAP_SIZE) {
8386
#if( configUSE_MALLOC_FAILED_HOOK == 1 )
@@ -145,6 +148,9 @@ void vPortFree( void *pv ) PRIVILEGED_FUNCTION {
145148

146149
size_t xPortGetFreeHeapSize( void ) PRIVILEGED_FUNCTION {
147150
struct mallinfo mi = mallinfo();
151+
if (heapBytesRemaining == -1) {
152+
heapBytesRemaining = configTOTAL_HEAP_SIZE;
153+
}
148154
return mi.fordblks + heapBytesRemaining;
149155
}
150156

0 commit comments

Comments
 (0)