template<std::size_t N> asge::mem::StackAllocator
A stack-based allocator that manages a fixed-size memory buffer.
#include <StackAllocator.hpp>Allocates objects of any type sequentially from a contiguous buffer of N bytes. Each allocation stores a Header, a back pointer, the object itself, and a footer, allowing both individual LIFO frees and bulk rollback via markers.
Memory layout per allocation: [ Header | backPtr | object | footer ]
Constraints:
Non-copyable and non-movable. The allocator must outlive all pointers it returns.
Free(T*) must be called in strict LIFO order. Out-of-order frees are caught by an assert in debug builds.
FreeToMarker() and Reset() call destructors on all unwound objects automatically.
N | Total size of the memory buffer in bytes. |
Public Types
using size_type = std::size_tPrivate Attributes
Static Private Member Functions
Rounds inOffset up to the next multiple of inAlign (must be a power of two).
Private Member Functions
Public Member Functions
StackAllocator()=default~StackAllocator()StackAllocator(const StackAllocator &)=deleteStackAllocator & operator=(const StackAllocator &)=deleteStackAllocator(StackAllocator &&)=deleteStackAllocator & operator=(StackAllocator &&)=deletetemplate<typename T, typename... _Args> T * Alloc(_Args &&...inArgs) noexceptAllocates and constructs an object of type T in the buffer.
Computes the correct alignment for the Header, back pointer, object, and footer before placing them sequentially in the buffer.
T | Type of object to allocate. |
Args | Constructor argument types. |
inArgs | Arguments forwarded to T's constructor. |
Pointer to the constructed object, or nullptr if the buffer is exhausted.
marker_type GetMarker() const noexceptReturns the current top offset as a rewind checkpoint.
template<typename T> void Free(T *inPtr) noexceptDestroys and frees the most recently allocated object.
Calls T's destructor and restores m_Top to its value before this allocation. Must be called in strict LIFO order — asserts if inPtr is not the most recent allocation.
T | Type of the object to free. |
inPtr | Pointer previously returned by Alloc<T>(). Must not be nullptr. |
void FreeToMarker(marker_type inMarker) noexceptDestroys all objects allocated after the given marker and rolls back the stack.
Walks the allocation chain backwards from m_Top to inMarker, calling each object's destructor in reverse allocation order.
inMarker | A value previously returned by GetMarker(). Must be <= m_Top. |
void Reset() noexceptDestructs all live objects and resets the allocator to its initial empty state.