a memory pool / arena allocator
/ uh-REE-nuh /
Calling malloc and free thousands of times — once per little object — is both slow and a recipe for leaks and fragmentation. A memory pool, also called an arena or region allocator, is a different strategy: grab one big block of memory up front, then hand out small pieces of it yourself, and free them all together at the end in a single stroke. It trades per-object freedom for speed and simplicity.
Here is the simplest form, often called a bump allocator. You malloc one large block — say 1 MiB — and keep a pointer to the next free spot inside it, starting at the beginning. Each time you need a small allocation, you return the current position and bump the pointer forward by the requested size. That is the entire allocation: no searching a free list, no header, no coalescing — just add to a pointer, which is about as fast as memory management gets. The catch is the other half: in a pure arena you usually do not free individual objects at all. Instead, when you are done with the whole batch — at the end of a request, a frame, or a parse — you reset the pointer back to the start (or free the one big block), reclaiming everything at once. This is a perfect fit for workloads with a clear lifetime where many objects are created together and die together: parsing a document, handling one network request, building one frame of a game.
Why it matters: arenas eliminate per-object free calls (so you cannot forget one — no leaks within the arena, no double-frees, no dangling pointers between objects in the same batch) and they sidestep fragmentation because everything is packed contiguously and released together. The trade-off is loss of flexibility: you cannot easily free one object early while keeping its neighbors, and if your objects do not share a lifetime the model does not fit. Arenas are the most common kind of custom allocator, and a good example of the broader idea that you can build your own allocator on top of one malloc when you know your allocation pattern better than a general-purpose allocator can.
char *arena = malloc(1 << 20); /* one 1 MiB block */ size_t used = 0; /* bump-allocate: */ void *p = arena + used; used += size; /* no per-object free */ /* ... build many objects ... */ free(arena); /* reclaim everything at once */
Allocate is just bumping a pointer; the whole batch is freed together when its shared lifetime ends.
An arena trades the ability to free individual objects for speed and zero fragmentation — it fits only when objects share a lifetime and die together. It is not a general-purpose replacement for malloc/free.