Veritable Lasagna
An Allocator & Data Structure Library for C.
Loading...
Searching...
No Matches
vl_pool.h
Go to the documentation of this file.
1
14#ifndef VL_FIXED_POOL
15#define VL_FIXED_POOL
16
17#include "vl_memory.h"
18#include "vl_numtypes.h"
19
20#ifndef VL_POOL_DEFAULT_SIZE
21#define VL_POOL_DEFAULT_SIZE 16
22#endif
23
31#if defined(VL_U64_T) && defined(VL_U32_T)
32#define VL_POOL_INDEX_T VL_U64_T
33#define VL_POOL_ORDINAL_T VL_U32_T
34#define VL_POOL_LOOKUP_MAX 0xFFFFFFFF
35#define VL_POOL_INVALID_IDX 0xFFFFFFFFFFFFFFFF
36#elif defined(VL_U32_T) && defined(VL_U16_T)
37#define VL_POOL_INDEX_T VL_U32_T
38#define VL_POOL_ORDINAL_T VL_U16_T
39#define VL_POOL_LOOKUP_MAX 0xFFFF
40#define VL_POOL_INVALID_IDX 0xFFFFFFFF
41#elif defined(VL_U16_T) && defined(VL_U8_T)
42#define VL_POOL_INDEX_T VL_U16_T
43#define VL_POOL_ORDINAL_T VL_U8_T
44#define VL_POOL_LOOKUP_MAX 0xFF
45#define VL_POOL_INVALID_IDX 0xFFFF
46#else
47#error Failed to configure vl_pool types.
48#endif
49
53typedef VL_POOL_INDEX_T vl_pool_idx;
54
55typedef struct vl_pool_node vl_pool_node;
56
65typedef struct vl_pool_node
66{
67 VL_POOL_ORDINAL_T totalTaken; // Total elements taken from this node.
68 VL_POOL_ORDINAL_T blockSize; // Size of this node, in elements
69 VL_POOL_ORDINAL_T blockOrdinal; // Ordinal location of this block.
70 vl_pool_node* nextLookup; // Pointer to the next element in the chain.
71 void* elements; // Pointer to element data
72} vl_pool_node;
73
157
185VL_API void vlPoolInitAligned(vl_pool* pool, vl_uint16_t elementSize, vl_uint16_t alignment);
186
197static inline void vlPoolInit(vl_pool* pool, vl_uint16_t elementSize)
198{
199 vlPoolInitAligned(pool, elementSize, VL_DEFAULT_MEMORY_ALIGN);
200}
201
223VL_API void vlPoolFree(vl_pool* pool);
224
248VL_API vl_pool* vlPoolNewAligned(vl_uint16_t elementSize, vl_uint16_t alignment);
249
260static inline vl_pool* vlPoolNew(vl_uint16_t elementSize)
261{
262 return vlPoolNewAligned(elementSize, VL_DEFAULT_MEMORY_ALIGN);
263}
264
286VL_API void vlPoolDelete(vl_pool* pool);
287
309VL_API vl_pool_idx vlPoolTake(vl_pool* pool);
310
328VL_API void vlPoolReturn(vl_pool* pool, vl_pool_idx idx);
329
351VL_API void* vlPoolSample(vl_pool* pool, vl_pool_idx idx);
352
372VL_API void vlPoolClear(vl_pool* pool);
373
392VL_API void vlPoolReset(vl_pool* pool);
393
413VL_API void vlPoolReserve(vl_pool* pool, vl_dsidx_t n);
414
447VL_API vl_pool* vlPoolClone(const vl_pool* src, vl_pool* dest);
448
449#endif
#define VL_DEFAULT_MEMORY_ALIGN
Default memory alignment. Defaults to maximum system word size.
Definition vl_memory.h:60
VL_STRUCTURE_INDEX_T vl_dsidx_t
Index type for data structures.
Definition vl_numtypes.h:75
vl_dsidx_t freeCapacity
Definition vl_pool.h:153
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.
Definition vl_pool.c:105
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.
Definition vl_pool.c:150
VL_API vl_pool * vlPoolNewAligned(vl_uint16_t elementSize, vl_uint16_t alignment)
Allocates and initializes a new fixed pool instance with specific alignment.
Definition vl_pool.c:326
VL_API vl_pool * vlPoolClone(const vl_pool *src, vl_pool *dest)
Clones the specified source fixed pool.
Definition vl_pool.c:268
vl_uint16_t elementAlign
Definition vl_pool.h:143
VL_API void vlPoolFree(vl_pool *pool)
De-initializes the specified pool instance.
Definition vl_pool.c:189
vl_pool_idx growthIncrement
Definition vl_pool.h:145
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.
Definition vl_pool.c:68
VL_API void vlPoolDelete(vl_pool *pool)
De-initializes and deletes the specified fixed pool instance.
Definition vl_pool.c:335
vl_pool_node ** lookupTable
Definition vl_pool.h:150
vl_pool_node * lookupHead
Definition vl_pool.h:151
vl_dsidx_t lookupTotal
Definition vl_pool.h:147
vl_pool_idx * freeTop
Definition vl_pool.h:155
VL_API void vlPoolReset(vl_pool *pool)
Resets the specified pool to its initial state.
Definition vl_pool.c:218
VL_API void vlPoolClear(vl_pool *pool)
Clears the specified pool.
Definition vl_pool.c:176
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 ...
Definition vl_pool.c:169
vl_dsidx_t lookupCapacity
Definition vl_pool.h:148
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.
Definition vl_pool.c:245
vl_pool_idx * freeStack
Definition vl_pool.h:154
vl_uint16_t elementSize
Definition vl_pool.h:142
VL_POOL_INDEX_T vl_pool_idx
Definition vl_pool.h:53
Fixed-size memory pool with stable indices and geometric growth.
Definition vl_pool.h:141