Beginner Question: pxFirstFreeBlock->xBlockSize initialization in prvHeapInit() in heap_2.c

Hello,

I’m a beginner learning FreeRTOS Kernel and currently studying the heap_2.c memory allocator in detail.

In prvHeapInit(), the first free block is initialized as follows:

/* To start with there is a single free block that is sized to take up the
 * entire heap space. */
pxFirstFreeBlock = ( BlockLink_t * ) pucAlignedHeap;
pxFirstFreeBlock->xBlockSize = configADJUSTED_HEAP_SIZE;
pxFirstFreeBlock->pxNextFreeBlock = &xEnd;

However, looking at the definition of BlockLink_t:

typedef struct A_BLOCK_LINK {
    struct A_BLOCK_LINK * pxNextFreeBlock; /*<< The next free block in the list. */
    size_t xBlockSize;  /*<< The size of the free block. */
} BlockLink_t;

The comment says xBlockSize is “the size of the free block”. But since the BlockLink_t header is placed at the very beginning of the heap, the actual remaining space available for user allocation is smaller than configADJUSTED_HEAP_SIZE.

So why pxFirstFreeBlock->xBlockSize is set to configADJUSTED_HEAP_SIZE?

Thank you in advance for your time and patience! :grin:

This works out because while allocating we add block size to the requested size - FreeRTOS-Kernel/portable/MemMang/heap_2.c at main · FreeRTOS/FreeRTOS-Kernel · GitHub.