PatchworkOS  19e446b
A non-POSIX operating system.
Loading...
Searching...
No Matches
pool.h
Go to the documentation of this file.
1#pragma once
2
3#include <stdatomic.h>
4#include <stddef.h>
5#include <stdint.h>
6
7#include <sys/defs.h>
8
9/**
10 * @brief Lock-free memory pool.
11 * @defgroup kernel_mem_pool Pool
12 * @ingroup kernel_mem
13 *
14 * The memory pool system provides a lock-free allocator using a pre-allocated array. Its intended to be used for
15 * performance-critical structures and as a even more specialized alternative to the Object Cache.
16 *
17 * In addition to its performance advantages, since the pool uses an array to store its objects, it is possible for
18 * certain structures to avoid storing full pointers to objects allocated from a pool instead using a `pool_idx_t` thus
19 * saving memory or allowing better caching.
20 *
21 * @{
22 */
23
24/**
25 * @brief Pool index type.
26 */
28
29#define POOL_IDX_MAX UINT16_MAX ///< The maximum index value for pool.
30
31#define POOL_TAG_INC ((uint64_t)(POOL_IDX_MAX) + 1) ///< The amount to increment the tag by in the tagged free list.
32
33/**
34 * @brief Pool structure.
35 * @struct pool_t
36 */
37typedef struct
38{
39 atomic_size_t used; ///< Number of used elements.
40 atomic_uint64_t free; ///< The tagged head of the free list.
41 void* elements; ///< Pointer to the elements array.
42 size_t elementSize; ///< Size of each element.
43 size_t nextOffset; ///< Offset of a `pool_idx_t` variable within each element used for the free list.
44 size_t capacity; ///< Maximum number of elements.
45} pool_t;
46
47/**
48 * @brief Initialize a pool.
49 *
50 * @param pool Pointer to the pool structure to initialize.
51 * @param elements Pointer to the elements array.
52 * @param capacity Maximum number of elements.
53 * @param elementSize Size of each element.
54 * @param nextOffset Offset of a `pool_idx_t` variable within each element used for the free list.
55 */
56void pool_init(pool_t* pool, void* elements, size_t capacity, size_t elementSize, size_t nextOffset);
57
58/**
59 * @brief Allocate an element from the pool.
60 *
61 * @param pool Pointer to the pool to allocate from.
62 * @return The index of the allocated element, or `POOL_IDX_MAX` if the pool is full.
63 */
65
66/**
67 * @brief Free an element back to the pool.
68 *
69 * @param pool Pointer to the pool to free to.
70 * @param idx The index of the element to free.
71 */
72void pool_free(pool_t* pool, pool_idx_t idx);
73
74/** @} */
pool_idx_t pool_alloc(pool_t *pool)
Allocate an element from the pool.
Definition pool.c:20
void pool_free(pool_t *pool, pool_idx_t idx)
Free an element back to the pool.
Definition pool.c:48
void pool_init(pool_t *pool, void *elements, size_t capacity, size_t elementSize, size_t nextOffset)
Initialize a pool.
Definition pool.c:3
uint16_t pool_idx_t
Pool index type.
Definition pool.h:27
__UINT16_TYPE__ uint16_t
Definition stdint.h:13
Pool structure.
Definition pool.h:38
atomic_uint64_t free
The tagged head of the free list.
Definition pool.h:40
size_t nextOffset
Offset of a pool_idx_t variable within each element used for the free list.
Definition pool.h:43
size_t capacity
Maximum number of elements.
Definition pool.h:44
atomic_size_t used
Number of used elements.
Definition pool.h:39
size_t elementSize
Size of each element.
Definition pool.h:42
void * elements
Pointer to the elements array.
Definition pool.h:41