Veritable Lasagna
An Allocator & Data Structure Library for C.
Loading...
Searching...
No Matches
vl_fixed_pool.h
Go to the documentation of this file.
1#ifndef VL_FIXED_POOL
2#define VL_FIXED_POOL
3
4#include "vl_numtypes.h"
5
6#ifndef VL_FIXEDPOOL_DEFAULT_SIZE
7#define VL_FIXEDPOOL_DEFAULT_SIZE 16
8#endif
9
17#if defined(VL_U64_T) && defined(VL_U32_T)
18#define VL_FIXEDPOOL_INDEX_T VL_U64_T
19#define VL_FIXEDPOOL_ORDINAL_T VL_U32_T
20#define VL_FIXEDPOOL_SHIFT 32
21#define VL_FIXEDPOOL_LOOKUP_MAX 0xFFFFFFFF
22#define VL_FIXEDPOOL_MASK 0x00000000FFFFFFFF
23#define VL_FIXEDPOOL_INVALID_IDX 0xFFFFFFFFFFFFFFFF
24#elif defined(VL_U32_T) && defined(VL_U16_T)
25#define VL_FIXEDPOOL_INDEX_T VL_U32_T
26#define VL_FIXEDPOOL_ORDINAL_T VL_U16_T
27#define VL_FIXEDPOOL_SHIFT 16
28#define VL_FIXEDPOOL_LOOKUP_MAX 0xFFFF
29#define VL_FIXEDPOOL_MASK 0x0000FFFF
30#define VL_FIXEDPOOL_INVALID_IDX 0xFFFFFFFF
31#elif defined(VL_U16_T) && defined(VL_U8_T)
32#define VL_FIXEDPOOL_INDEX_T VL_U16_T
33#define VL_FIXEDPOOL_ORDINAL_T VL_U8_T
34#define VL_FIXEDPOOL_SHIFT 8
35#define VL_FIXEDPOOL_LOOKUP_MAX 0xFF
36#define VL_FIXEDPOOL_MASK 0x00FF
37#define VL_FIXEDPOOL_INVALID_IDX 0xFFFF
38#else
39#error Failed to configure vl_linearpool types.
40#endif
41
45typedef VL_FIXEDPOOL_INDEX_T vl_fixedpool_idx;
46
47typedef struct vl_fixed_pool_node vl_fixed_pool_node;
48
57typedef struct vl_fixed_pool_node{
58 VL_FIXEDPOOL_ORDINAL_T totalTaken; //Total elements taken from this node.
59 VL_FIXEDPOOL_ORDINAL_T blockSize; //Size of this node, in elements
60 VL_FIXEDPOOL_ORDINAL_T blockOrdinal; //Ordinal location of this block.
61 vl_fixed_pool_node* nextLookup; //Pointer to next element in the chain.
62 void* elements; //Pointer to element data
63} vl_fixed_pool_node;
64
65
87typedef struct{
88 vl_ularge_t elementSize; //Size of each element, in bytes.
89 vl_fixedpool_idx growthIncrement; //Size of the next allocated block, in elements.
90
91 vl_dsidx_t lookupTotal, lookupCapacity; //Number of used locations in the lookup table, and capacity thereof.
92 vl_fixed_pool_node **lookupTable, *lookupHead; //Lookup table, and the "leading" lookup element.
93
94 vl_dsidx_t freeCapacity; //Size of the free stack, in elements.
95 vl_fixedpool_idx *freeStack, *freeTop; //Pointer to the base of the free stack, and the top.
97
108void vlFixedPoolInit(vl_fixedpool* pool, vl_ularge_t elementSize);
109
120void vlFixedPoolFree(vl_fixedpool* pool);
121
133
144
152
160
161#ifndef vlFixedPoolSample
169#define vlFixedPoolSample(pool, idx) (void*)(((vl_usmall_t*)((pool)->lookupTable[(idx) & VL_FIXEDPOOL_MASK])->elements) + (((idx) >> VL_FIXEDPOOL_SHIFT) * (pool)->elementSize))
170#endif
171
179
188
202
221
222#endif
void vlFixedPoolInit(vl_fixedpool *pool, vl_ularge_t elementSize)
Initializes the specified fixed pool instance.
Definition vl_fixed_pool.c:31
VL_FIXEDPOOL_INDEX_T vl_fixedpool_idx
Definition vl_fixed_pool.h:45
void vlFixedPoolDelete(vl_fixedpool *pool)
De-initializes and deletes the specified fixed pool. This will clear up all memory associated with me...
Definition vl_fixed_pool.c:242
vl_fixedpool_idx * freeStack
Definition vl_fixed_pool.h:95
vl_dsidx_t lookupCapacity
Definition vl_fixed_pool.h:91
vl_fixed_pool_node * lookupHead
Definition vl_fixed_pool.h:92
void vlFixedPoolFree(vl_fixedpool *pool)
De-initializes the specified pool instance.
Definition vl_fixed_pool.c:126
vl_dsidx_t freeCapacity
Definition vl_fixed_pool.h:94
void vlFixedPoolClear(vl_fixedpool *pool)
Definition vl_fixed_pool.c:115
vl_fixedpool_idx vlFixedPoolTake(vl_fixedpool *pool)
Definition vl_fixed_pool.c:55
vl_fixedpool * vlFixedPoolNew(vl_ularge_t elementSize)
Allocates and initializes a fixed pool instance.
Definition vl_fixed_pool.c:234
void vlFixedPoolReset(vl_fixedpool *pool)
Definition vl_fixed_pool.c:153
void vlFixedPoolReturn(vl_fixedpool *pool, vl_fixedpool_idx idx)
Definition vl_fixed_pool.c:91
vl_fixedpool_idx growthIncrement
Definition vl_fixed_pool.h:89
vl_fixedpool * vlFixedPoolClone(const vl_fixedpool *src, vl_fixedpool *dest)
Clones the specified source fixed pool.
Definition vl_fixed_pool.c:184
void vlFixedPoolReserve(vl_fixedpool *pool, vl_dsidx_t n)
Ensures space for n-many elements in the pool.
Definition vl_fixed_pool.c:174
vl_ularge_t elementSize
Definition vl_fixed_pool.h:88
A non-trivial pool allocator, useful for avoiding many smaller heap allocations.
Definition vl_fixed_pool.h:87
VL_STRUCTURE_INDEX_T vl_dsidx_t
Index type for data structures.
Definition vl_numtypes.h:13
VL_ULARGE_T vl_ularge_t
Largest available unsigned integer type.
Definition vl_numtypes.h:74