Description
The problem
In my microcontroller application (ARM Cortex-M4, 32K RAM) I would like to allocate all remaining RAM to FreeRTOS' heap. This could be achieved in the linker script:
SECTIONS {
/* ... */
.rtos_heap : {
. = ALIGN(8);
__FreeRTOSHeapBase = .;
PROVIDE(ucHeap = __FreeRTOSHeapBase);
} > RAM
/* ... */
__FreeRTOSHeapTop = __StackLimit;
}
I intended to use these addresses from inside FreeRTOSConfig.h
like below:
/* These are defined in the linker script */
extern const uint8_t __FreeRTOSHeapBase;
extern const uint8_t __FreeRTOSHeapTop;
/* Memory allocation related definitions. */
#define configAPPLICATION_ALLOCATED_HEAP 1
#define configTOTAL_HEAP_SIZE \
( (size_t) ( (uint32_t)&__FreeRTOSHeapTop - (uint32_t)&__FreeRTOSHeapBase ) )
That failed, because the configTOTAL_HEAP_SIZE
macro referenced in the declaration below is not constant at compile time.
FreeRTOS-Kernel/portable/MemMang/heap_4.c
Line 64 in eec4233
The solution I'd like
The compiler (or at least my GCC with my set of compile flags, I'm not sure 🤔) does not need a compile-time constant value to declare a reference to an external array, for the compiler does not have to assign memory to it. Therefore, removing configTOTAL_HEAP_SIZE
from the external declaration would work:
/* Allocate the memory for the heap. */
#if ( configAPPLICATION_ALLOCATED_HEAP == 1 )
/* The application writer has already defined the array used for the RTOS
* heap - probably so it can be placed in a special segment or address. */
extern uint8_t ucHeap[ ];
#else
PRIVILEGED_DATA static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
#endif /* configAPPLICATION_ALLOCATED_HEAP */
The other location configTOTAL_HEAP_SIZE
is used in heap_4.c
is inside prvHeapInit(void)
, which does not need to be known at compile time.
FreeRTOS-Kernel/portable/MemMang/heap_4.c
Line 349 in eec4233