← API Reference Class

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.

Template Parameters
N

Total size of the memory buffer in bytes.

Public Types

#using size_type = std::size_t
#using marker_type = size_type

Private Attributes

#std::array< std::byte, N > m_Bufferprivate
#std::size_t m_Top {0}private

Static Private Member Functions

#static std::size_t Align(std::size_t inOffset, std::size_t inAlign) noexceptprivate

Rounds inOffset up to the next multiple of inAlign (must be a power of two).

Private Member Functions

#Header * GetHeader(void *inPtr) noexceptprivate

Public Member Functions

#StackAllocator()=default
#~StackAllocator()
#StackAllocator(const StackAllocator &)=delete
#StackAllocator & operator=(const StackAllocator &)=delete
#StackAllocator(StackAllocator &&)=delete
#StackAllocator & operator=(StackAllocator &&)=delete
#template<typename T, typename... _Args> T * Alloc(_Args &&...inArgs) noexcept

Allocates 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.

Template Parameters
T

Type of object to allocate.

Args

Constructor argument types.

Parameters
inArgs

Arguments forwarded to T's constructor.

Returns

Pointer to the constructed object, or nullptr if the buffer is exhausted.

#marker_type GetMarker() const noexcept

Returns the current top offset as a rewind checkpoint.

#template<typename T> void Free(T *inPtr) noexcept

Destroys 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.

Template Parameters
T

Type of the object to free.

Parameters
inPtr

Pointer previously returned by Alloc<T>(). Must not be nullptr.

#void FreeToMarker(marker_type inMarker) noexcept

Destroys 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.

Parameters
inMarker

A value previously returned by GetMarker(). Must be <= m_Top.

#void Reset() noexcept

Destructs all live objects and resets the allocator to its initial empty state.

#size_type Used() const noexcept
#size_type Remaining() const noexcept
#size_type Full() const noexcept
#size_type Empty() const noexcept