PatchworkOS  19e446b
A non-POSIX operating system.
Loading...
Searching...
No Matches

Lock-free memory pool. More...

Collaboration diagram for Pool:

Detailed Description

Lock-free memory pool.

The memory pool system provides a lock-free allocator using a pre-allocated array. Its intended to be used for performance-critical structures and as a even more specialized alternative to the Object Cache.

In addition to its performance advantages, since the pool uses an array to store its objects, it is possible for certain structures to avoid storing full pointers to objects allocated from a pool instead using a pool_idx_t thus saving memory or allowing better caching.

Data Structures

struct  pool_t
 Pool structure. More...
 

Macros

#define POOL_IDX_MAX   UINT16_MAX
 The maximum index value for pool.
 
#define POOL_TAG_INC   ((uint64_t)(POOL_IDX_MAX) + 1)
 The amount to increment the tag by in the tagged free list.
 

Typedefs

typedef uint16_t pool_idx_t
 Pool index type.
 

Functions

void pool_init (pool_t *pool, void *elements, size_t capacity, size_t elementSize, size_t nextOffset)
 Initialize a pool.
 
pool_idx_t pool_alloc (pool_t *pool)
 Allocate an element from the pool.
 
void pool_free (pool_t *pool, pool_idx_t idx)
 Free an element back to the pool.
 

Macro Definition Documentation

◆ POOL_IDX_MAX

#define POOL_IDX_MAX   UINT16_MAX

The maximum index value for pool.

Definition at line 29 of file pool.h.

◆ POOL_TAG_INC

#define POOL_TAG_INC   ((uint64_t)(POOL_IDX_MAX) + 1)

The amount to increment the tag by in the tagged free list.

Definition at line 31 of file pool.h.

Typedef Documentation

◆ pool_idx_t

Pool index type.

Definition at line 27 of file pool.h.

Function Documentation

◆ pool_init()

void pool_init ( pool_t pool,
void *  elements,
size_t  capacity,
size_t  elementSize,
size_t  nextOffset 
)

Initialize a pool.

Parameters
poolPointer to the pool structure to initialize.
elementsPointer to the elements array.
capacityMaximum number of elements.
elementSizeSize of each element.
nextOffsetOffset of a pool_idx_t variable within each element used for the free list.

Definition at line 3 of file pool.c.

Here is the caller graph for this function:

◆ pool_alloc()

pool_idx_t pool_alloc ( pool_t pool)

Allocate an element from the pool.

Parameters
poolPointer to the pool to allocate from.
Returns
The index of the allocated element, or POOL_IDX_MAX if the pool is full.

Definition at line 20 of file pool.c.

Here is the caller graph for this function:

◆ pool_free()

void pool_free ( pool_t pool,
pool_idx_t  idx 
)

Free an element back to the pool.

Parameters
poolPointer to the pool to free to.
idxThe index of the element to free.

Definition at line 48 of file pool.c.

Here is the caller graph for this function: