Veritable Lasagna
An Allocator & Data Structure Library for C.
Loading...
Searching...
No Matches
vl_linear_pool.h File Reference
#include "vl_buffer.h"
+ Include dependency graph for vl_linear_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_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_linearpoolvlLinearPoolNew (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_linearpoolvlLinearPoolClone (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)
 

Data Structure Documentation

◆ vl_linearpool

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.

See also
vl_linked_list
vl_set

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.

See also
vl_fixedpool
+ Collaboration diagram for vl_linearpool:
Data Fields
vl_buffer buffer
vl_memsize_t elementSize
vl_buffer freeStack
vl_dsidx_t totalTaken

Macro Definition Documentation

◆ VL_POOL_INVALID_IDX

#define VL_POOL_INVALID_IDX   VL_STRUCTURE_INDEX_MAX

◆ vlLinearPoolSample

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

Parameters
poolPtrpointer to the memory pool
poolIndexnumeric index of pooled memory instance
Returns
TRANSIENT POINTER to element data.

Typedef Documentation

◆ vl_linearpool_idx

Function Documentation

◆ vlLinearPoolClear()

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.

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

◆ vlLinearPoolClone()

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.

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

◆ vlLinearPoolDelete()

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.

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

◆ vlLinearPoolFree()

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.

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

◆ vlLinearPoolInit()

void vlLinearPoolInit ( vl_linearpool * pool,
vl_memsize_t elementSize )

Initializes the specified pool instance.

This pool should be freed via vlLinearPoolFree.

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

◆ vlLinearPoolNew()

vl_linearpool * vlLinearPoolNew ( vl_memsize_t elementSize)

Allocates and initializes a pool instance.

This pool should later be deleted via vlLinearPoolDelete.

See also
vlLinearPoolDelete
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:

◆ vlLinearPoolReserve()

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.

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

◆ vlLinearPoolReturn()

void vlLinearPoolReturn ( vl_linearpool * pool,
vl_linearpool_idx offset )

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

Parameters
pool
offset
Complexity O(1) constant.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ vlLinearPoolTake()

vl_linearpool_idx vlLinearPoolTake ( vl_linearpool * pool)

Takes a new index from the 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:

◆ vlLinearPoolTellIndex()

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.

Parameters
pool
dataPtr
Complexity O(1) constant.
Returns
index