Veritable Lasagna
An Allocator & Data Structure Library for C.
|
#include "vl_buffer.h"
Go to the source code of this file.
Data Structures | |
struct | vl_linearpool |
A simple pool allocator, useful for avoiding many smaller heap allocations. More... | |
Macros | |
#define | VL_POOL_INVALID_IDX VL_STRUCTURE_INDEX_MAX |
#define | vlLinearPoolSample(poolPtr, poolIndex) (vl_transient*)(((vl_usmall_t*)((poolPtr)->buffer.data) + ((poolIndex) * (poolPtr)->elementSize))) |
Typedefs | |
typedef vl_dsidx_t | vl_linearpool_idx |
Functions | |
void | vlLinearPoolInit (vl_linearpool *pool, vl_memsize_t elementSize) |
Initializes the specified pool instance. | |
void | vlLinearPoolFree (vl_linearpool *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 vlLinearPoolInit. | |
vl_linearpool * | vlLinearPoolNew (vl_memsize_t elementSize) |
Allocates and initializes a pool instance. | |
void | vlLinearPoolDelete (vl_linearpool *pool) |
De-initializes and deletes the specified pool. This will clear up all memory associated with members of the pool. This pool should have been initialized via vlLinearPoolNew. | |
void | vlLinearPoolClear (vl_linearpool *pool) |
vl_linearpool * | vlLinearPoolClone (const vl_linearpool *src, vl_linearpool *dest) |
Clones the specified source pool. | |
void | vlLinearPoolReserve (vl_linearpool *pool, vl_memsize_t n) |
Reserves space for n-many elements in the underlying pool buffer. | |
vl_linearpool_idx | vlLinearPoolTellIndex (vl_linearpool *pool, const vl_transient *dataPtr) |
Calculates the index of the specified element pointer. May return VL_POOL_INVALID_IDX if the specified pointer is outside the pool buffer range. | |
vl_linearpool_idx | vlLinearPoolTake (vl_linearpool *pool) |
void | vlLinearPoolReturn (vl_linearpool *pool, vl_linearpool_idx offset) |
struct vl_linearpool |
A simple pool allocator, useful for avoiding many smaller heap allocations.
The vl_linearpool structure represents a collection of fixed-size memory blocks. These memory blocks are slices of a larger allocation. This is useful for certain algorithms and tasks, but also for other data structures which need many instances of the same size block of memory. Linked lists and trees are good examples of this requirement.
This is implemented as two instances of the vl_buffer structure, and thus allocates two separate blocks of underlying memory. One is used as the actual storage for the pool's elements, while the other behaves as a stack and is used to manage freed blocks of memory.
This structure DOES NOT offer pointer stability, in exchange for low-overhead memory management. Consider using a Fixed Pool if pointer stability is a requirement for your use-case.
Data Fields | ||
---|---|---|
vl_buffer | buffer | |
vl_memsize_t | elementSize | |
vl_buffer | freeStack | |
vl_dsidx_t | totalTaken |
#define VL_POOL_INVALID_IDX VL_STRUCTURE_INDEX_MAX |
#define vlLinearPoolSample | ( | poolPtr, | |
poolIndex ) (vl_transient*)(((vl_usmall_t*)((poolPtr)->buffer.data) + ((poolIndex) * (poolPtr)->elementSize))) |
Samples the specified pool and retrieves a pointer to the memory associated with the specified pool index.
poolPtr | pointer to the memory pool |
poolIndex | numeric index of pooled memory instance |
typedef vl_dsidx_t vl_linearpool_idx |
void vlLinearPoolClear | ( | vl_linearpool * | pool | ) |
Clears the specified pool. This does not clear any buffers associated with the pool. Rather, this function resets the "next" offset at which the next (new) pool index will be to 0.
pool | pointer |
vl_linearpool * vlLinearPoolClone | ( | const vl_linearpool * | src, |
vl_linearpool * | dest ) |
Clones the specified source 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 vlLinearPoolNew. Otherwise, its element size is set to the source's and all of its existing data is replaced.
src | pointer to pool to clone |
dest | pointer to target pool, or NULL |
void vlLinearPoolDelete | ( | vl_linearpool * | pool | ) |
De-initializes and deletes the specified pool. This will clear up all memory associated with members of the pool. This pool should have been initialized via vlLinearPoolNew.
pool | pointer |
void vlLinearPoolFree | ( | vl_linearpool * | 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 vlLinearPoolInit.
pool |
void vlLinearPoolInit | ( | vl_linearpool * | pool, |
vl_memsize_t | elementSize ) |
Initializes the specified pool instance.
This pool should be freed via vlLinearPoolFree.
pool | pointer |
elementSize | size of each element, in bytes. |
vl_linearpool * vlLinearPoolNew | ( | vl_memsize_t | elementSize | ) |
Allocates and initializes a pool instance.
This pool should later be deleted via vlLinearPoolDelete.
elementSize | size of each element, in bytes. |
void vlLinearPoolReserve | ( | vl_linearpool * | pool, |
vl_memsize_t | n ) |
Reserves space for n-many elements in the underlying pool buffer.
This is accomplished by using the standard buffer resizing method for this library, which is doubling the size of the underlying buffer until it is greater than a specified minimum size.
This function will always result in the reallocation of the underlying memory.
pool | pointer |
n | total elements to reserve space for |
void vlLinearPoolReturn | ( | vl_linearpool * | pool, |
vl_linearpool_idx | offset ) |
Gives the specified index back to the pool, allowing it to be re-used.
pool | |
offset |
vl_linearpool_idx vlLinearPoolTake | ( | vl_linearpool * | pool | ) |
Takes a new index from the pool, which corresponds to a valid memory location within the pool.
pool | pointer |
vl_linearpool_idx vlLinearPoolTellIndex | ( | vl_linearpool * | pool, |
const vl_transient * | dataPtr ) |
Calculates the index of the specified element pointer. May return VL_POOL_INVALID_IDX if the specified pointer is outside the pool buffer range.
If the underlying pool allocation is implicitly resized, make sure the specified pointer still refers to a current, relevant location in memory. Otherwise, this will return VL_POOL_INVALID_IDX.
pool | |
dataPtr |