Memory Allocators in FreeRTOS: heap1 to heap5 (Part 3)

heap1  

Design Concept  Using fixed-size memory blocks divided into multiple equal-sized memory blocks.    

Usage Process  

1  Initialization  2  Call  Using xNextFreeByte as a marker, find the starting address of the unallocated bytes and return the byte position

3  Release  Not supported

Understanding pvPortMalloc()  

Memory allocation needs to consider two aspects of byte alignment    

1  Memory allocation byte alignment  2  Memory heap starting address byte alignment  

Approach: Use modulus to achieve byte alignment     heap2  

Design Concept  Static memory allocator, using a linked list to manage allocated memory blocks      

Usage Process  

1  Initialization  Divide memory block sizes, mark unallocated status, link into a free linked list

2  Call  Traverse the linked list, remove this block, return the memory address

3  Release  This memory block is re-added to the free linked list

Understanding pvPortMalloc()  

When traversing the linked list, be familiar with *next / xEND usage.         Using xFreeBytesRemaining as a marker, find the starting address of the unallocated bytes and return the byte position         Xend is the end of the free linked list, not the end of the heap; if new memory allocation occurs, it can continue to append.         Best fit algorithm:  Traverse the free block linked list, find a suitable size block, and split as needed. Notably, in vPortfree, re-add to the free linked list based on block size. (However, in the later heap4, blocks are inserted into the linked list by address to facilitate merging adjacent free blocks.)     The problem is that memory fragmentation increases, making it suitable only for taskTCB that requires allocation of the same memory size. It does not reduce memory usage.   heap3  

Design Concept  Using library functions malloc/free for implementation. Not thread-safe. heap4  

Design Concept  Using fixed-size memory blocks divided into multiple equal-sized memory blocks.    

Usage Process  

1  Initialization  Divide memory block sizes, mark unallocated status, link into a free linked list 2  Call  Traverse the linked list, remove this block, return the memory address 3  Release  The size of free memory blocks can be adjusted. If the inserted free block is contiguous with the previous and next blocks, it can be automatically merged. This avoids fragmented memory.

Understanding pvPortMalloc()  

First fit algorithm first fit vPortGetHeapStats(): There is a dedicated monitoring function to obtain status information.   heap5  

Design Concept  Compared to heap4, it adds a new feature; it is not limited to managing a large array: it can manage multiple, separated memory blocks.    

Self-Reflection

Differences between dynamic and static memory, besides malloc/free being dynamic memory, there are other characteristics that can be summarized. Further understanding of how different memory allocations are specifically implemented regarding thread safety.

Leave a Comment