Free Lists and Reuse
Track and recycle blocks.
Beyond the Bump Allocator
To free individual blocks and reuse them, we need bookkeeping. A free list is a linked list of available blocks the allocator searches before grabbing fresh memory.
Each block carries a header so the allocator can find its size and link to the next block in the chain.
Block Header with a Link
We extend the header with a next pointer and a free flag. Together these turn our pool into a navigable list of blocks.
The payload follows immediately after the header in memory.
typedef struct block {
size_t size; /* payload bytes */
int free; /* 1 if reusable */
struct block *next; /* next block in pool */
} block_t;All lessons in this course
- How malloc Works
- A Simple Bump Allocator
- Free Lists and Reuse
- Alignment and Splitting