Question about pvPortMalloc of Heap2

Dear FreeRTOS.
My name is Hoseok who is a newcomer to FreeRTOS. I’m analyzing the Heap2.c source code for study purposes. I know Heap2 has been superseded by Heap4, but there is one code snippet I try to understand.

The code snippet below is from Heap2.c, and its purpose is to add additional bytes for the alignment of BlockLink_t size and the requested bytes from my understanding. When the requested size is 7 bytes, xAdditionalRequiredSize should be 8 bytes (BlockLink_t size) + 1 byte (the additional byte needed to align the requested 7 bytes to 8 bytes). This seems fine. However, when the requested size is 8 bytes, xAdditionalRequiredSize is 8 bytes (BlockLink_t size) + 8 bytes (portBYTE_ALIGNMENT (8)). I initially thought that when the requested size is 8 bytes, xAdditionalRequiredSize should be 8 bytes (BlockLink_t size) rather than 16 bytes, because the requested size is already aligned to 8 bytes. Could someone help me understand this? Why are 8 bytes added when the requested size is a multiple of 8? Looks like, 8 bytes are wasted or hidden in this case.

Which FreeRTOS version are you looking at? The latest code has this check which add extra bytes to align only if required.

1 Like

I was referring to 202212.01 not the STL version, because the instructor of an online lecture about FreeRTOS was using 202212.01 version. So, I was thinking of that there would not be differences between them. Thanks for letting me know that. I had to refer to 202406.01LTS version :slight_smile: