include/bedrock/pinned_colony.h file

Fixed-capacity colony with stable element addresses.

Typedefs

using PinnedColony = struct PinnedColony
using PinnedColonyDtor = void(*)(void*element, void*user)
Called once for a live element on erase, clear, or destroy.

Functions

auto pinned_colony_create(size_t capacity, size_t elem_size, size_t elem_align, PinnedColonyDtor dtor, void* user) -> PinnedColony*
Create a colony with a fixed number of slots.
void pinned_colony_destroy(PinnedColony* colony)
Destroy a colony, run destructors for live elements, and release its memory.
auto pinned_colony_acquire(PinnedColony* colony) -> void*
Take an uninitialized live slot.
auto pinned_colony_insert(PinnedColony* colony, const void* src) -> void*
Copy elem_size bytes into a new live slot.
auto pinned_colony_erase(PinnedColony* colony, const void* element) -> int
Destroy one live element and return its slot to the free list.
void pinned_colony_clear(PinnedColony* colony)
Destroy every live element and make those slots reusable.
auto pinned_colony_size(const PinnedColony* colony) -> size_t
Live element count.
auto pinned_colony_capacity(const PinnedColony* colony) -> size_t
Maximum slot capacity.
auto pinned_colony_empty(const PinnedColony* colony) -> int
Whether the colony has no live elements.
auto pinned_colony_at(PinnedColony* colony, size_t index) -> void*
Pointer to a live element.
auto pinned_colony_next_index(const PinnedColony* colony, size_t index) -> size_t
Next live index after index, in increasing order.
auto pinned_colony_prev_index(const PinnedColony* colony, size_t index) -> size_t
Previous live index before index.
auto pinned_colony_index(const PinnedColony* colony, const void* element) -> size_t
Slot index of a live element pointer.

Typedef documentation

typedef void(*PinnedColonyDtor)(void*element, void*user)

Called once for a live element on erase, clear, or destroy.

element is the slot about to be released. user is the pointer passed to pinned_colony_create.

Function documentation

PinnedColony* pinned_colony_create(size_t capacity, size_t elem_size, size_t elem_align, PinnedColonyDtor dtor, void* user)

Create a colony with a fixed number of slots.

Parameters
capacity Maximum number of live elements. Zero is an empty colony.
elem_size Size of one element in bytes. Must be non-zero.
elem_align Alignment of each slot. Zero means 1. Must be a power of two no greater than the system page size.
dtor Optional destructor, or NULL.
user Pointer passed through to dtor.
Returns New colony, or NULL if the arguments are invalid or memory cannot be reserved.

void pinned_colony_destroy(PinnedColony* colony)

Destroy a colony, run destructors for live elements, and release its memory.

Parameters
colony Colony to destroy. NULL is ignored.

void* pinned_colony_acquire(PinnedColony* colony)

Take an uninitialized live slot.

Parameters
colony Colony to allocate from.
Returns Pointer to the slot, or NULL if colony is NULL, it is full, or pages cannot be committed.

void* pinned_colony_insert(PinnedColony* colony, const void* src)

Copy elem_size bytes into a new live slot.

Parameters
colony Colony to insert into.
src Bytes to copy. NULL is rejected.
Returns Pointer to the new element, or NULL on failure.

int pinned_colony_erase(PinnedColony* colony, const void* element)

Destroy one live element and return its slot to the free list.

Parameters
colony Colony that owns the element.
element Pointer previously returned by acquire, insert, or at.
Returns 0 on success, or -1 if element is not a live element of colony.

void pinned_colony_clear(PinnedColony* colony)

Destroy every live element and make those slots reusable.

Parameters
colony Colony to clear. NULL is ignored.

size_t pinned_colony_size(const PinnedColony* colony)

Live element count.

Parameters
colony Colony to query.
Returns Number of live elements, or 0 if colony is NULL.

size_t pinned_colony_capacity(const PinnedColony* colony)

Maximum slot capacity.

Parameters
colony Colony to query.
Returns Slot capacity, or 0 if colony is NULL.

int pinned_colony_empty(const PinnedColony* colony)

Whether the colony has no live elements.

Parameters
colony Colony to query.
Returns Non-zero when there are no live elements. A NULL colony is empty.

void* pinned_colony_at(PinnedColony* colony, size_t index)

Pointer to a live element.

Parameters
colony Colony to query.
index Slot index.
Returns Element pointer, or NULL if the index is not live.

size_t pinned_colony_next_index(const PinnedColony* colony, size_t index)

Next live index after index, in increasing order.

Parameters
colony Colony to walk.
index Previous index, or SIZE_MAX to start.
Returns The next live index, or SIZE_MAX when colony is NULL or the walk is finished.

Pass SIZE_MAX to get the first live index. The end of the walk is SIZE_MAX.

size_t pinned_colony_prev_index(const PinnedColony* colony, size_t index)

Previous live index before index.

Parameters
colony Colony to walk.
index Later index, or SIZE_MAX to start from the end.
Returns The previous live index, or SIZE_MAX when colony is NULL or the walk is finished.

Pass SIZE_MAX to get the last live index. The beginning of the walk is SIZE_MAX.

size_t pinned_colony_index(const PinnedColony* colony, const void* element)

Slot index of a live element pointer.

Parameters
colony Colony that owns the element.
element Pointer previously returned by acquire, insert, or at.
Returns Index, or SIZE_MAX if element is not an aligned live element of colony.