Lightweight slot allocator that issues generation-safe handles.
More...
#include <slot_allocator.h>
|
| StorageIndex | allocate (bool *recycled=nullptr) |
| | Allocate a new handle with a unique index and current generation.
|
| void | free (StorageIndex id) |
| | Free a handle, bumping its generation and recycling the slot.
|
| bool | has (StorageIndex id) const |
| | Test whether a handle is still valid.
|
| StorageIndex | handleAt (uint32_t index) const |
| | Rebuild the handle naming a sparse slot, generation included.
|
| bool | isAliveAtIndex (uint32_t index) const |
| | Check whether index currently holds a live slot.
|
| StorageIndex | allocateAt (uint32_t index) |
| | Allocate a slot at a specific index.
|
| template<typename Fn> |
| void | forEach (Fn &&fn) const |
| | Invoke fn(index) for every currently-alive slot.
|
|
void | clear () |
| | Reset every slot to dead, bumping generations so outstanding handles go stale.
|
| void | swap (SlotAllocator &other) noexcept |
| | Swap internal state with another allocator.
|
| size_t | size () const |
| | Number of live slots.
|
| size_t | extent () const |
| | Number of slots the table spans, live or free, slot 0 included.
|
|
| static constexpr uint32_t | MAX_CLAIMED_INDEX = 1u << 22 |
| | The highest index allocateAt will claim.
|
Lightweight slot allocator that issues generation-safe handles.
Generation counters detect stale handles; a free list recycles in O(1). Stores no per-slot data.
◆ allocate()
| StorageIndex Vkm::Engine::SlotAllocator::allocate |
( |
bool * | recycled = nullptr | ) |
|
|
inline |
Allocate a new handle with a unique index and current generation.
- Parameters
-
| [out] | recycled | True when the slot came off the free list (used before, or an allocateAt gap); false when freshly grown, so nothing keyed by its index can be under it. |
- Returns
- A handle with index > 0.
◆ free()
Free a handle, bumping its generation and recycling the slot.
A dead handle asserts and is refused: freeing twice underflows m_liveCount.
- Parameters
-
◆ has()
| bool Vkm::Engine::SlotAllocator::has |
( |
StorageIndex | id | ) |
const |
|
inline |
Test whether a handle is still valid.
- Parameters
-
- Returns
- True if alive and generation matches.
◆ handleAt()
| StorageIndex Vkm::Engine::SlotAllocator::handleAt |
( |
uint32_t | index | ) |
const |
|
inline |
Rebuild the handle naming a sparse slot, generation included.
A dead slot yields its real generation, so the handle fails has().
- Parameters
-
- Returns
- The handle for that slot, null when the index is out of reach.
◆ isAliveAtIndex()
| bool Vkm::Engine::SlotAllocator::isAliveAtIndex |
( |
uint32_t | index | ) |
const |
|
inline |
Check whether index currently holds a live slot.
- Parameters
-
| index | Sparse slot index; out of reach is fine. |
- Returns
- True when that slot is live; never for slot 0.
◆ allocateAt()
| StorageIndex Vkm::Engine::SlotAllocator::allocateAt |
( |
uint32_t | index | ) |
|
|
inline |
Allocate a slot at a specific index.
For a loader whose entities keep their saved slot indices. Gaps grown across go onto the free list. A claimed slot still in the free list is skipped when popped, not erased (a linear find per call).
- Parameters
-
| index | Slot to claim; must be free. |
- Returns
- The new handle, or null for slot 0, an index past MAX_CLAIMED_INDEX (test for these: the index often comes from outside the process), or a live slot (also asserts).
◆ forEach()
template<typename Fn>
| void Vkm::Engine::SlotAllocator::forEach |
( |
Fn && | fn | ) |
const |
|
inline |
Invoke fn(index) for every currently-alive slot.
- Template Parameters
-
| Fn | Callable taking a uint32_t slot index. |
- Parameters
-
| fn | Called once per live slot, ascending by index; never slot 0. |
◆ swap()
Swap internal state with another allocator.
For Scene's staging-then-swap load, without copying or moving.
- Parameters
-
| other | Allocator to trade state with. |
◆ size()
| size_t Vkm::Engine::SlotAllocator::size |
( |
| ) |
const |
|
inline |
Number of live slots.
- Returns
- The live slot count.
◆ extent()
| size_t Vkm::Engine::SlotAllocator::extent |
( |
| ) |
const |
|
inline |
Number of slots the table spans, live or free, slot 0 included.
- Returns
- One past the highest slot ever handed out.
◆ MAX_CLAIMED_INDEX
| uint32_t Vkm::Engine::SlotAllocator::MAX_CLAIMED_INDEX = 1u << 22 |
|
staticconstexpr |
The highest index allocateAt will claim.
allocateAt grows the table to an index often read from a file or a datagram, so this bounds what a file or a peer can make it allocate.
The documentation for this class was generated from the following file: