|
Veritable Lasagna
An Allocator & Data Structure Library for C.
|
Include dependency graph for vl_pool.h:
This graph shows which files directly or indirectly include this file:Go to the source code of this file.
Data Structures | |
| struct | vl_pool |
| Fixed-size memory pool with stable indices and geometric growth. More... | |
Macros | |
| #define | VL_POOL_DEFAULT_SIZE 16 |
Typedefs | |
| typedef VL_POOL_INDEX_T | vl_pool_idx |
Functions | |
| VL_API void | vlPoolInitAligned (vl_pool *pool, vl_uint16_t elementSize, vl_uint16_t alignment) |
| Initializes the specified fixed pool instance with specified element size and alignment. | |
| VL_API void | vlPoolFree (vl_pool *pool) |
| De-initializes the specified pool instance. | |
| VL_API vl_pool * | vlPoolNewAligned (vl_uint16_t elementSize, vl_uint16_t alignment) |
| Allocates and initializes a new fixed pool instance with specific alignment. | |
| VL_API void | vlPoolDelete (vl_pool *pool) |
| De-initializes and deletes the specified fixed pool instance. | |
| VL_API vl_pool_idx | vlPoolTake (vl_pool *pool) |
| Takes a new index from the fixed pool, which corresponds to a valid memory location within the pool. | |
| VL_API void | vlPoolReturn (vl_pool *pool, vl_pool_idx idx) |
| Gives the specified index back to the fixed pool, allowing it to be re-used. | |
| VL_API void * | vlPoolSample (vl_pool *pool, vl_pool_idx idx) |
| Samples the specified fixed pool and retrieves a pointer to the memory associated with the specified pool index. | |
| VL_API void | vlPoolClear (vl_pool *pool) |
| Clears the specified pool. | |
| VL_API void | vlPoolReset (vl_pool *pool) |
| Resets the specified pool to its initial state. | |
| VL_API void | vlPoolReserve (vl_pool *pool, vl_dsidx_t n) |
| Ensures that at least n additional elements can be taken without triggering further allocations. | |
| VL_API vl_pool * | vlPoolClone (const vl_pool *src, vl_pool *dest) |
| Clones the specified source fixed pool. | |
| struct vl_pool |
Fixed-size memory pool with stable indices and geometric growth.
The vl_pool structure implements a memory allocator optimized for managing large numbers of fixed-size elements with O(1) allocation, deallocation, and access by index.
Elements are allocated from internally managed blocks that grow geometrically. Each element is identified by a compact integer index (vl_pool_idx) which encodes both the block ordinal and the element ordinal within that block.
vlPoolSampleEach element is identified by a vl_pool_idx, which is an opaque packed value containing:
Indices are valid from the time they are returned by vlPoolTake until they are returned via vlPoolReturn. Indices must not be reused or dereferenced after:
vlPoolClearvlPoolResetvlPoolFreeNo bounds checking is performed when sampling indices; supplying an invalid index results in undefined behavior.
VL_POOL_DEFAULT_SIZE elementsvlPoolReset or vlPoolFreeThis growth strategy provides amortized O(1) allocation while limiting fragmentation and allocation frequency.
Returned indices are stored in an internal free stack and reused before allocating new elements from blocks. This ensures efficient reuse of memory without relocating elements.
This structure is not thread-safe.
Concurrent calls to vlPoolTake, vlPoolReturn, or vlPoolSample must be externally synchronized.
vlPoolSample remain valid until the element is returned or the pool is cleared/reset/freed
Collaboration diagram for vl_pool:| Data Fields | ||
|---|---|---|
| vl_uint16_t | elementAlign |
Alignment of each element in bytes. |
| vl_uint16_t | elementSize |
Size of each element in bytes (aligned). |
| vl_dsidx_t | freeCapacity |
Capacity of the free index stack. |
| vl_pool_idx * | freeStack |
Base of the free index stack. |
| vl_pool_idx * | freeTop |
Pointer to the next free slot in the stack. |
| vl_pool_idx | growthIncrement |
Size (in elements) of the next block to allocate. |
| vl_dsidx_t | lookupCapacity |
Allocated capacity of the lookup table. |
| vl_pool_node * | lookupHead |
Head of the internal block list. |
| vl_pool_node ** | lookupTable |
Direct-access table mapping block ordinals to blocks. |
| vl_dsidx_t | lookupTotal |
Number of active block entries in the lookup table. |
| #define VL_POOL_DEFAULT_SIZE 16 |
██ ██ ██ █████ ███████ █████ ██████ ███ ██ █████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██ ██ ██ ██ ██ ███████ ███████ ███████ ██ ███ ██ ██ ██ ███████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ███████ ██ ██ ███████ ██ ██ ██████ ██ ████ ██ ██ ====—: A Data Structure and Algorithms library for C11. :—====
Copyright 2026 Jesse Walker, released under the MIT license. Git Repository: https://github.com/walkerje/veritable_lasagna
| typedef VL_POOL_INDEX_T vl_pool_idx |
Configure pool index type based on availability of integer types.
64-bit configurations use a 32-bit index and 32-bit ordinal. 32-bit configurations use a 16-bit index and 16-bit ordinal. 16-bit configurations use a 8-bit index and 8-bit ordinal. Integer index type for a given pool.
| VL_API void vlPoolClear | ( | vl_pool * | pool | ) |
Clears the specified pool.
Resets each underlying memory block's counter to 0 and resets the free index stack. This effectively marks all pooled slots as available for reuse.
vlPoolSample become invalid.pool must not be NULL.| pool | pool pointer |
Here is the caller graph for this function:Clones the specified source fixed pool.
Clones the entirety of the src pool to the dest pool, including all management state and element data.
The 'src' pool pointer must be non-null and initialized. The 'dest' pool pointer may be null, but if it is not null it must be initialized.
If the 'dest' pool pointer is null, a new pool is initialized via vlPoolNew. Otherwise, its element size is set to the source's and the destination is reset via vlPoolReset.
dest is NULL, the caller owns the returned vl_pool. If dest is provided, ownership remains with the caller.dest pool remains valid until deleted or freed.src must not be NULL. dest can be NULL.NULL on allocation failure.vl_pool struct (if dest is NULL) and multiple memory block nodes.dest or a new instance), or NULL on failure.| src | pointer to pool to clone |
| dest | pointer to target pool, or NULL |
Here is the call graph for this function:
Here is the caller graph for this function:| VL_API void vlPoolDelete | ( | vl_pool * | pool | ) |
De-initializes and deletes the specified fixed pool instance.
This frees all internally allocated memory blocks, management structures, and the vl_pool struct itself. This pool should have been initialized via vlPoolNew(Ext).
vl_pool struct.pool should not be NULL.vlMemFree and free.| pool | pointer |
Here is the call graph for this function:| VL_API void vlPoolFree | ( | vl_pool * | pool | ) |
De-initializes the specified pool instance.
This will clear up all memory associated with members of the pool, including all allocated block nodes and management tables. This pool should have been initialized via vlPoolInit(Ext).
pool struct itself.vlPoolTake and all pointers from vlPoolSample become invalid.pool must not be NULL.vlMemFree.| pool | pointer |
Here is the call graph for this function:
Here is the caller graph for this function:| VL_API void vlPoolInitAligned | ( | vl_pool * | pool, |
| vl_uint16_t | elementSize, | ||
| vl_uint16_t | alignment | ||
| ) |
Initializes the specified fixed pool instance with specified element size and alignment.
This pool should be de-initialized via vlPoolFree.
pool struct. The function initializes internal management structures.pool struct must remain valid for the duration of its use. Internal allocations are valid until vlPoolFree or vlPoolDelete.pool is NULL, the function returns immediately (no-op).alignment is not a power of 2. Passing an already initialized pool without first calling vlPoolFree (causes memory leak).vlMemAlloc and vlMemAllocAligned.| pool | pointer |
| elementSize | size of each element, in bytes. |
| alignment | alignment of each element, in bytes. |
Here is the call graph for this function:
Here is the caller graph for this function:| VL_API vl_pool * vlPoolNewAligned | ( | vl_uint16_t | elementSize, |
| vl_uint16_t | alignment | ||
| ) |
Allocates and initializes a new fixed pool instance with specific alignment.
This pool should later be deleted via vlPoolDelete.
vl_pool pointer and is responsible for calling vlPoolDelete.vlPoolDelete.NULL if heap allocation for the vl_pool struct fails.NULL on allocation failure.alignment is not a power of 2.vl_pool struct and its management structures.NULL.| elementSize | size of each element, in bytes. |
| alignment | alignment of each element, in bytes. |
Here is the call graph for this function:
Here is the caller graph for this function:| VL_API void vlPoolReserve | ( | vl_pool * | pool, |
| vl_dsidx_t | n | ||
| ) |
Ensures that at least n additional elements can be taken without triggering further allocations.
This may create one or more new block nodes, following the geometric growth strategy (doubling size).
pool must not be NULL.| pool | pointer |
| n | total additional elements to reserve space for |
Here is the call graph for this function:
Here is the caller graph for this function:| VL_API void vlPoolReset | ( | vl_pool * | pool | ) |
Resets the specified pool to its initial state.
Deletes all memory block nodes except for the initial one, and resets all counters and the free stack.
pool must not be NULL.vlMemFree.| pool |
Here is the call graph for this function:
Here is the caller graph for this function:| VL_API void vlPoolReturn | ( | vl_pool * | pool, |
| vl_pool_idx | idx | ||
| ) |
Gives the specified index back to the fixed pool, allowing it to be re-used.
idx can be VL_POOL_INVALID_IDX (no-op).| pool | pointer |
| idx | element index |
Here is the call graph for this function:
Here is the caller graph for this function:| VL_API void * vlPoolSample | ( | vl_pool * | pool, |
| vl_pool_idx | idx | ||
| ) |
Samples the specified fixed pool and retrieves a pointer to the memory associated with the specified pool index.
NULL if idx is VL_POOL_INVALID_IDX or if the block associated with the index is not found.NULL.| pool | pointer to the fixed pool |
| idx | numeric index of pooled memory instance |
Here is the caller graph for this function:| VL_API vl_pool_idx vlPoolTake | ( | vl_pool * | pool | ) |
Takes a new index from the fixed pool, which corresponds to a valid memory location within the pool.
If no free slots are available in existing blocks, a new memory block node is automatically allocated.
vlPoolReturn or the pool is destroyed/reset.VL_POOL_INVALID_IDX if allocation fails when growing the pool.VL_POOL_INVALID_IDX on allocation failure.vlMemAllocAligned and vlMemRealloc.vl_pool_idx) to an element, or VL_POOL_INVALID_IDX on failure.| pool | pointer |
Here is the call graph for this function:
Here is the caller graph for this function: