template<std::size_t N> asge::mem::FreeList
Fixed-capacity index allocator backed by an intrusive free-list.
#include <FreeList.hpp>Hands out and reclaims indices in the range [0, N) in O(1), with no heap allocation. The N slots are pre-linked into a singly-linked free chain at construction; Get() pops the head of the chain and Free() pushes an index back onto it (LIFO reuse order). The chain is terminated by the sentinel value N (not a real slot): once m_FreeHead equals N every slot is in use and Get() reports exhaustion.
Each slot also tracks whether it is currently in use, so Free() can reject indices that were never handed out or have already been freed, guarding against double-frees.
This class does not store any data itself — it only manages indices. It is meant to be paired with an external, equally-sized container (e.g. a sparse array) that the returned indices key into.
N | Number of allocatable slots; fixed at compile time. |
Private Attributes
Public Member Functions
FreeList()FreeList(FreeList const &)=deleteFreeList(FreeList &&)=default~FreeList()=defaultClaims the next free index and marks it in use.
The claimed index on success, or an error with std::errc::result_out_of_range if all N slots are in use.
bool IsUsed(std::size_t inIndex) const noexceptChecks whether an index is currently claimed via Get().
inIndex | Index to check. |
true if inIndex is in range and currently in use.
BoolResult Free(std::size_t inIndex) noexceptReleases a previously-claimed index back to the free-list.
inIndex | Index previously returned by |
Ok on success, or an error with std::errc::invalid_argument if inIndex is out of range, was never claimed, or has already been freed.