Veritable Lasagna
An Allocator & Data Structure Library for C.
|
#include "vl_numtypes.h"
Go to the source code of this file.
Data Structures | |
struct | vl_fixedpool |
A non-trivial pool allocator, useful for avoiding many smaller heap allocations. More... | |
Macros | |
#define | VL_FIXEDPOOL_DEFAULT_SIZE 16 |
#define | vlFixedPoolSample(pool, idx) (void*)(((vl_usmall_t*)((pool)->lookupTable[(idx) & VL_FIXEDPOOL_MASK])->elements) + (((idx) >> VL_FIXEDPOOL_SHIFT) * (pool)->elementSize)) |
Typedefs | |
typedef VL_FIXEDPOOL_INDEX_T | vl_fixedpool_idx |
Functions | |
void | vlFixedPoolInit (vl_fixedpool *pool, vl_ularge_t elementSize) |
Initializes the specified fixed pool instance. | |
void | vlFixedPoolFree (vl_fixedpool *pool) |
De-initializes the specified pool instance. | |
vl_fixedpool * | vlFixedPoolNew (vl_ularge_t elementSize) |
Allocates and initializes a fixed pool instance. | |
void | vlFixedPoolDelete (vl_fixedpool *pool) |
De-initializes and deletes the specified fixed pool. This will clear up all memory associated with members of the pool. This pool should have been initialized via vlFixedPoolNew. | |
vl_fixedpool_idx | vlFixedPoolTake (vl_fixedpool *pool) |
void | vlFixedPoolReturn (vl_fixedpool *pool, vl_fixedpool_idx idx) |
void | vlFixedPoolClear (vl_fixedpool *pool) |
void | vlFixedPoolReset (vl_fixedpool *pool) |
void | vlFixedPoolReserve (vl_fixedpool *pool, vl_dsidx_t n) |
Ensures space for n-many elements in the pool. | |
vl_fixedpool * | vlFixedPoolClone (const vl_fixedpool *src, vl_fixedpool *dest) |
Clones the specified source fixed pool. | |
struct vl_fixedpool |
A non-trivial pool allocator, useful for avoiding many smaller heap allocations.
The vl_fixedpool structure represents a collection of memory blocks. These memory blocks are allocated as-needed, doubling in size for geometric growth.
Elements are never moved or copied unless the pool is cleared or cloned. This makes it more suitable for use cases where pointer stability is required.
Fixed pools differ from linear pools in that the former offers pointer stability, whereas the latter does not. The trade-off is more overhead in comparison to linear pools, while spending less time on growing/copying a single underlying buffer.
This is implemented as an abstraction over a singly-linked list, lookup table, and stack. It offers O(1) element sampling, and O(n) element taking/returning (where n=1, in most cases).
If pointer stability is not necessary for your use case, or low overhead memory management is necessary to you, consider using a Linear Pool instead.
Data Fields | ||
---|---|---|
vl_ularge_t | elementSize | |
vl_dsidx_t | freeCapacity | |
vl_fixedpool_idx * | freeStack | |
vl_fixedpool_idx * | freeTop | |
vl_fixedpool_idx | growthIncrement | |
vl_dsidx_t | lookupCapacity | |
vl_fixed_pool_node * | lookupHead | |
vl_fixed_pool_node ** | lookupTable | |
vl_dsidx_t | lookupTotal |
#define VL_FIXEDPOOL_DEFAULT_SIZE 16 |
#define vlFixedPoolSample | ( | pool, | |
idx ) (void*)(((vl_usmall_t*)((pool)->lookupTable[(idx) & VL_FIXEDPOOL_MASK])->elements) + (((idx) >> VL_FIXEDPOOL_SHIFT) * (pool)->elementSize)) |
Samples the specified fixed pool and retrieves a pointer to the memory associated with the specified pool index.
pool | pointer to the fixed pool |
idx | numeric index of pooled memory instance |
typedef VL_FIXEDPOOL_INDEX_T vl_fixedpool_idx |
Configure fixed 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.
void vlFixedPoolClear | ( | vl_fixedpool * | pool | ) |
Clears the specified pool. This does not clear any buffers associated with the pool. Rather, this resets each underlying memory block's counter to 0, and resets the free stack.
pool | fixed pool pointer |
vl_fixedpool * vlFixedPoolClone | ( | const vl_fixedpool * | src, |
vl_fixedpool * | dest ) |
Clones the specified source fixed pool.
Clones the entirety of the src pool to the dest pool.
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 vlFixedPoolNew. Otherwise, its element size is set to the source's and the destination is reset via vlFixedPoolReset.
src | pointer to pool to clone |
dest | pointer to target pool, or NULL |
void vlFixedPoolDelete | ( | vl_fixedpool * | pool | ) |
De-initializes and deletes the specified fixed pool. This will clear up all memory associated with members of the pool. This pool should have been initialized via vlFixedPoolNew.
pool | pointer |
void vlFixedPoolFree | ( | vl_fixedpool * | pool | ) |
De-initializes the specified pool instance.
This will clear up all memory associated with members of the pool.
This pool should have been initialized via vlFixedPoolInit.
pool | pointer |
void vlFixedPoolInit | ( | vl_fixedpool * | pool, |
vl_ularge_t | elementSize ) |
Initializes the specified fixed pool instance.
This pool should be freed via vlFixedPoolFree.
pool | pointer |
elementSize | size of each element, in bytes. |
vl_fixedpool * vlFixedPoolNew | ( | vl_ularge_t | elementSize | ) |
Allocates and initializes a fixed pool instance.
This pool should later be deleted via vlFixedPoolDelete.
elementSize | size of each element, in bytes. |
void vlFixedPoolReserve | ( | vl_fixedpool * | pool, |
vl_dsidx_t | n ) |
Ensures space for n-many elements in the pool.
This is accomplished by using the standard buffer resizing method for this library, which is doubling the size of the underlying storage until it is greater than a specified minimum size. A new node may be created twice the size of all created before it.
This function will only sometimes result in allocation of a new node.
pool | pointer |
n | total elements to reserve space for |
void vlFixedPoolReset | ( | vl_fixedpool * | pool | ) |
Resets the specified pool. This deletes all but the initial memory block. This leaves the pool in a "new" state, freeing memory in the process.
pool |
void vlFixedPoolReturn | ( | vl_fixedpool * | pool, |
vl_fixedpool_idx | idx ) |
Gives the specified index back to the fixed pool, allowing it to be re-used.
pool | pointer |
idx | element index |
vl_fixedpool_idx vlFixedPoolTake | ( | vl_fixedpool * | pool | ) |
Takes a new index from the fixed pool, which corresponds to a valid memory location within the pool.
pool | pointer |