A Simple Bump Allocator
Hand out memory linearly.
The Bump Allocator Idea
A bump (or arena) allocator is the simplest design. You keep one big buffer and a single offset. Each allocation just returns the current offset, then "bumps" the offset forward by the requested size.
There is no per-block metadata and no search. Allocation is essentially one pointer addition, making it extremely fast.
A Static Backing Buffer
For a self-contained example we back the allocator with a static array instead of the OS heap. This compiles and runs anywhere, with no sbrk or mmap.
The array gives us a fixed pool of bytes to carve up.
#define POOL_SIZE 1024
static unsigned char pool[POOL_SIZE];
static size_t offset = 0;All lessons in this course
- How malloc Works
- A Simple Bump Allocator
- Free Lists and Reuse
- Alignment and Splitting