Veritable Lasagna
An Allocator & Data Structure Library for C.
Loading...
Searching...
No Matches
vl_fixed_pool.h File Reference
#include "vl_numtypes.h"
+ Include dependency graph for vl_fixed_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_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_fixedpoolvlFixedPoolNew (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_fixedpoolvlFixedPoolClone (const vl_fixedpool *src, vl_fixedpool *dest)
 Clones the specified source fixed pool.
 

Data Structure Documentation

◆ vl_fixedpool

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.

See also
vl_linearpool
+ Collaboration diagram for vl_fixedpool:
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

Macro Definition Documentation

◆ VL_FIXEDPOOL_DEFAULT_SIZE

#define VL_FIXEDPOOL_DEFAULT_SIZE   16

◆ vlFixedPoolSample

#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.

Parameters
poolpointer to the fixed pool
idxnumeric index of pooled memory instance
Complexity O(1) constant.
Returns
FIXED POINTER to element data.

Typedef Documentation

◆ vl_fixedpool_idx

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.

Function Documentation

◆ vlFixedPoolClear()

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.

Parameters
poolfixed pool pointer
Complexity O(n) linear.

◆ vlFixedPoolClone()

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.

See also
vlFixedPoolNew
Parameters
srcpointer to pool to clone
destpointer to target pool, or NULL
Complexity O(n) linear.
Returns
dest, or pool initialized via vlFixedPoolNew
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ vlFixedPoolDelete()

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.

See also
vlFixedPoolNew
Parameters
poolpointer
Complexity O(1) constant.
+ Here is the call graph for this function:

◆ vlFixedPoolFree()

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.

See also
vlFixedPoolInit
Parameters
poolpointer
Complexity O(1) constant.
+ Here is the caller graph for this function:

◆ vlFixedPoolInit()

void vlFixedPoolInit ( vl_fixedpool * pool,
vl_ularge_t elementSize )

Initializes the specified fixed pool instance.

This pool should be freed via vlFixedPoolFree.

See also
vlFixedPoolFree
Parameters
poolpointer
elementSizesize of each element, in bytes.
Complexity O(1) constant.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ vlFixedPoolNew()

vl_fixedpool * vlFixedPoolNew ( vl_ularge_t elementSize)

Allocates and initializes a fixed pool instance.

This pool should later be deleted via vlFixedPoolDelete.

See also
vlFixedPoolDelete
Parameters
elementSizesize of each element, in bytes.
Complexity O(1) constant.
Returns
pointer to pool instance
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ vlFixedPoolReserve()

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.

Parameters
poolpointer
ntotal elements to reserve space for
+ Here is the call graph for this function:

◆ vlFixedPoolReset()

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.

Parameters
pool
Complexity O(n) linear.
+ Here is the caller graph for this function:

◆ vlFixedPoolReturn()

void vlFixedPoolReturn ( vl_fixedpool * pool,
vl_fixedpool_idx idx )

Gives the specified index back to the fixed pool, allowing it to be re-used.

Parameters
poolpointer
idxelement index
Complexity O(1) constant.
+ Here is the caller graph for this function:

◆ vlFixedPoolTake()

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.

Parameters
poolpointer
Complexity O(1) constant.
Returns
index of new pool item.
+ Here is the call graph for this function:
+ Here is the caller graph for this function: