pool.c
April 3, 2026 ยท View on GitHub
Purpose
Chained bump allocator with optional malloc/realloc/free wrappers.
API Reference
XSTATUS XPool_Init(xpool_t *pPool, size_t nSize)
- Arguments:
pPool: pool object to initialize.nSize: initial chunk size, defaults toXPOOL_DEFAULT_SIZEwhen zero.
- Does:
- aligns the chunk size to 8 bytes.
- allocates the first pool buffer.
- clears usage counters and chain link.
- Returns:
XSTDOKon success.XSTDERRon allocation failure.
xpool_t *XPool_Create(size_t nSize)
- Arguments:
nSize: initial chunk size.
- Does:
- heap-allocates an
xpool_tand initializes it.
- heap-allocates an
- Returns:
- allocated pool on success.
NULLon allocation/init failure.
void XPool_Destroy(xpool_t *pPool)
- Arguments:
pPool: pool chain root.
- Does:
- frees the current chunk.
- recursively destroys
pNext. - frees the pool struct itself when
bAllocis set.
- Returns:
- no return value.
void XPool_Reset(xpool_t *pPool)
- Arguments:
pPool: pool chain root.
- Does:
- resets
nUsedto zero in the current chunk and all chained chunks.
- resets
- Returns:
- no return value.
void *XPool_Alloc(xpool_t *pPool, size_t nSize)
- Arguments:
pPool: pool chain root.nSize: requested allocation size.
- Does:
- allocates from the current chunk when space is available.
- otherwise creates or reuses
pNextand continues there.
- Returns:
- pointer to pool-owned memory.
NULLon invalid args or allocation failure.
void *XPool_Realloc(xpool_t *pPool, void *pData, size_t nDataSize, size_t nNewSize)
- Arguments:
- pool, old pointer, old size and new size.
- Does:
- allocates a new region from the pool.
- copies
min(nDataSize, nNewSize)bytes. - calls
XPool_Free()on the old region.
- Returns:
- new pointer on success.
NULLon invalid args or allocation failure.
void XPool_Free(xpool_t *pPool, void *pData, size_t nSize)
- Arguments:
- pool chain, pointer and allocation size.
- Does:
- when
pPool == NULL, falls back tofree(pData). - otherwise reclaims memory only in two cases:
- the region spans the whole used prefix of a chunk
- the region is exactly the last allocation in a chunk
- middle-of-pool frees are ignored intentionally.
- when
- Returns:
- no return value.
void *xalloc(xpool_t *pPool, size_t nSize)
void *xrealloc(xpool_t *pPool, void *pData, size_t nDataSize, size_t nNewSize)
- Arguments:
- optional pool plus allocation parameters.
- Does:
- dispatches to
malloc/reallocwhenpPool == NULL. - otherwise dispatches to
XPool_Alloc()/XPool_Realloc().
- dispatches to
- Returns:
- allocated pointer or
NULL.
- allocated pointer or
void xfree(xpool_t *pPool, void *pData)
void xfreen(xpool_t *pPool, void *pData, size_t nSize)
- Arguments:
- optional pool, pointer and optional known size.
- Does:
xfree()callsfree()only whenpPool == NULL.xfreen()forwards toxfree()whennSize == 0, otherwise usesXPool_Free().
- Returns:
- no return value.
size_t XPool_GetSize(xpool_t *pPool)
size_t XPool_GetUsed(xpool_t *pPool)
- Arguments:
- pool object.
- Does:
- returns current chunk capacity or used bytes.
- Returns:
- size/used count.
0whenpPool == NULL.
Important Notes
- This is not a general-purpose free-store allocator.
XPool_Free()is intentionally conservative; many frees are no-ops.