Five different heap implementations, named "heap 1-5" available in FreeRTOS.
Mohamed Elhadi
Software Developer | C\C++ | Python | Rust | Embedded Systems | Linux | RTOS | Network admin
Heap_1.c implementation:
Heap 1 Suitable Usage Scenarios:
Heap_2.c implementation:
Heap 2 Usage Considerations:
Heap_3 implementation:
Heap-4 implementation: (not deterministic- but is much more efficient)
→the most popular one.
→It uses first fit algorithm to allocate memory
→it is able to combine adjacent Free blocks into a single larger block, which minimize the risk of memory fragmentation.
→The memory array for the heap is declared within the heap.4.C file itself. the starting address of the heap memory array is automatically configured by the Linker
→if configAPPLICATION_ALLOCATED-HEAP is set to zero, a static declaration of the heap array occurs within heap.4.C, and its size is determined by Config TOTAL HEAP. SIZE
→FreeRTOS allows for custom heap allocation by the application writer.
→To use a custom memory area for the FreeRTOS heap:
领英推荐
? Set config APPLICATION-ALLOCATED-HEAP to 1 in the FreeRTOS Config.h File
? Declare the heap memory array within the user's code, specifying the desired start address and size using config TOTAL-HEAP-SIZE
→heap 4: heap memory is organized as a Linked List for better efficiency when dynamically allocating / Freeing memory.
→ When allocating "N" bytes in the heap memory using "PVPortMalloc " API, it Consumes:
? 8 bytes for the structure of the "Linked list heap block"
? N bytes for the data that needs to be allocated.
? Padding for 8 bytes alignment.
*ex: if we need to allocate 52 bytes
8+ 52 = 60 bytes aligned to 8 bytes it gives 64 bytes consumed From the heap.
Heap-5 implementation:
→the algorithm used by heap-5 to allocate and free memory is identical to that used by heap.4
→unlike heap.4, heap_s is not Limited to allocating memory from a single Statically declared array, it can allocate memory from multiple and separated memory spaces.
→heap 5 is used in case we have Mcu with separated RAM areas that not Visible as Continuous memory space.
→it is able to combine adjacent free memory blocks into a single block like Heap-4
→it is the only memory allocation scheme that must be explicitly initialized before any os object can be created before first call of PVPortMalloc() .
→to initialize this scheme vPortDefineHeapRegions () function should be called
-it specifies start address and size of each separate memory area that we Would like to dedicate as a heap for our as application.
FreeRTOS memory allocation depends on stack operation models:
→Cortex-M Processor uses the "Full Descending stack" model
-cach push operation, the processor first decrements the Sp. then stores the Value in the memory location pointed by sp
-the SP points to the memory location where the Last data was pushed to the stack
-each pop operation, the value of the memory location pointed by sp is read, and then the Value of Sp is incremented automatically.
→if the stack grows down then allocate the stack then the TCB so the stack does not grow into the TCB. Likewise if the stack grows up then allocate the TCB then the stack