hn.today

The Double Headed Stack Allocator

blog.demofox.org70 points24 comments
Screenshot of The Double Headed Stack Allocator

A stack (bump) allocator reserves a fixed block of memory and tracks a single top-of-stack (tos) index so allocation is just returning the current pointer and adding the requested size to tos; freeing all allocations is a single tos reset. This gives extremely fast allocation and deallocation, at the cost of not running destructors automatically - placement new for construction and manual destructor calls are required for complex types. The pattern supports partial deallocation by saving and restoring tos for nested operations and is usable in multithreaded contexts by making tos updates atomic, though atomic contention can increase cache traffic; an alternative is one allocator per thread.

A double-headed stack allocator runs two independent tos indices at opposite ends of the same block so each side grows toward the other. That prevents fragmentation when two subsystems with different allocation/deallocation cadences (for example streamed level geometry versus per-character data) interleave allocations: each side can reset independently until the heads cross. A common variant stores variable-sized payloads on one side and an index/offset table on the other, enabling random access, removals, and compaction by updating offsets. The design is simple, efficient in memory-constrained systems, and straightforward to implement for parallel usage.

Read on blog.demofox.org24 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Programming

OpenCode 2.0

OpenCode 2.0

OpenCode 2.0 introduces a major overhaul with a unified client/server system that runs automatically on users' machines, improving performance and usability. The update includes new features, an improved API, dark mode, and native apps, making the system more flexible and user-friendly. (anoma.ly)

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.