A fixed size pool of objects with no dynamic memory allocation. Provides validated access to its elements through opaque handles.
Inspired by this blog article.
Creates a new pool with the given size
- Parameters:
- size:
Inserts a new item in the first available slot.
- Parameters:
- item:
- Returns: An optional handler that points to the slot where the item has been inserted
- Throws:
StaticPoolError.NoAvailableSlots
: if the pool is full
Gets the item in the slot pointed by the provided handle
- Parameters:
- handle:
- Returns: The item in the corresponding slot
- Throws:
StaticPoolError.AccessOutOfBounds
: if the handle points outside the poolStaticPoolError.InvalidHandle
: If the handle points to an empty slotStaticPoolError.DanglingHandle
: If the handle doesn't match the current allocation of that slot
Returns the number of slots that can still be reused. If this gets to 0, you might want to consider resetting some slots.
- Returns: The number of slots that can still be reused
Releases the slot pointed by the provided handle.
- Parameters:
- handle:
- Throws:
StaticPoolError.DanglingHandle
: If the handle doesn't match the current allocation of that slot
Releases the slot at the provided index.
- Parameters:
- index:
Releases all slots and resets their allocation counters. This means that any dangling handles pointing to this slot might act as if they were still valid, so this must be used with care.
Releases the slot at the provided index and resets its allocation counter. This means that any dangling handles pointing to this slot might act as if they were still valid, so this must be used with care.
- Parameters:
- slot: