Fast arena allocator for short-lived formatting data.
Memory is carved out of a fixed 256-byte in-object buffer first; once it is exhausted, additional blocks are obtained with malloc(). All blocks are chained in a singly linked list whose head is the in-object buffer. free() is a no-op: the formatter allocates many small pieces that all die together, so the heap blocks are released in one sweep by the destructor instead of being tracked individually.
| void* strformat_ns::QuickAlloc::alloc |
( |
size_t |
size | ) |
|
|
inline |
Allocate size bytes from the arena.
Only the first two blocks of the list are probed for free space: the in-object buffer and, if present, the newest standard heap block. The insertion policy below pushes exhausted blocks to third place and beyond, and a block only gets displaced after failing a request, i.e. when its remaining space is already smaller than that request. Blocks past the second therefore have little or no free space left, so scanning them would rarely pay off; capping the search keeps allocation O(1) no matter how many blocks have accumulated.
When both probes fail, a new block is added:
- A request that fits in a standard block (
default_buffer_size bytes including the header) gets one, inserted right behind the in-object buffer so that it becomes the primary heap block for subsequent allocations.
- A larger request gets a dedicated block sized exactly for it. Such a block is born completely full, so it is inserted behind the current heap block (third place) to keep the two-block search window free of blocks that can never satisfy anything.
Sizes are rounded up to alignof(std::max_align_t) granularity, but the usable memory starts at offset sizeof(Header), so returned pointers are only guaranteed to be aligned to alignof(Header) (pointer alignment) – sufficient for the Part records stored here, but this is not a general-purpose max-aligned allocator.