When a system creates and destroys many small objects, such as orders, it can take them from memory it already owns instead of the general heap. Two classic shapes:
- An object pool holds a fixed number of slots and a free list, a stack of the slots not in use. Taking a slot pops the list and giving it back pushes it, a few instructions each, and the pool never calls
new after it is built. When the free list is empty, the pool is full and says so instead of growing.
- An arena, or bump allocator, hands out memory by moving an offset forward through one big block. Allocating is an addition; there is no individual free. Everything is released at once with
reset(), which suits data that lives for one message or one request. Each allocation must start at an address that is a multiple of the type's alignment, so the address is rounded up first: (addr + align - 1) & ~(align - 1).
To build an object in memory you already have, use placement new, new (where) T(args...), and end its life with an explicit destructor call, p->~T(). The tests check behavior and count heap allocations: after construction, a pool or arena must not touch the heap at all.
Placement new builds an object in memory you own; an explicit destructor call ends it.