aboutsummaryrefslogtreecommitdiff
path: root/listpool/include
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2021-12-04 16:01:12 -0800
committer3gg <3gg@shellblade.net>2021-12-04 16:01:12 -0800
commitf8217d240d598f39f70047f7a623dd46312542c6 (patch)
tree4e40843d665e388416c1226f739c2b8c0b8da736 /listpool/include
parent5f6ea503cdb6ad4a95b679672a1ad324d96c89a5 (diff)
Initial commit.
Diffstat (limited to 'listpool/include')
-rw-r--r--listpool/include/listpool.h79
1 files changed, 79 insertions, 0 deletions
diff --git a/listpool/include/listpool.h b/listpool/include/listpool.h
new file mode 100644
index 0000000..a5e4955
--- /dev/null
+++ b/listpool/include/listpool.h
@@ -0,0 +1,79 @@
1#pragma once
2
3#include "list.h"
4
5#include <assert.h>
6#include <stddef.h>
7#include <stdint.h>
8
9/// Define a typed listpool of a given size.
10#define DEF_LISTPOOL(POOL, TYPE, NUM_BLOCKS) \
11 typedef struct POOL { \
12 listpool pool; \
13 list nodes[NUM_BLOCKS]; \
14 TYPE blocks[NUM_BLOCKS]; \
15 } POOL;
16
17/// Creates a new listpool.
18#define listpool_make(POOL) \
19 { \
20 assert(POOL); \
21 const size_t block_size = sizeof((POOL)->blocks[0]); \
22 const size_t num_blocks = sizeof((POOL)->blocks) / block_size; \
23 listpool_make_(&(POOL)->pool, (POOL)->nodes, (POOL)->blocks, num_blocks, \
24 block_size); \
25 }
26
27/// Allocate a new block.
28/// Return 0 if there is no memory left.
29#define listpool_alloc(POOL) listpool_alloc_(&(POOL)->pool)
30
31/// Free the block.
32/// The block pointer is conveniently set to 0.
33#define listpool_free(POOL, ITEM) listpool_free_(&(POOL)->pool, (void**)ITEM)
34
35/// Remove a value from the list.
36/// Defined here instead of DEF_LISTPOOL_IMPL() because not all types may have
37/// an operator==.
38#define listpool_remove(POOL, VAL) \
39 { \
40 listpool_foreach(POOL, iter, { \
41 if (*iter == VAL) { \
42 listpool_free(POOL, &iter); \
43 break; \
44 } \
45 }); \
46 }
47
48/// Iterate over the used items of the pool.
49#define listpool_foreach(POOL, ITER, BODY) \
50 for (list* it_ = (POOL)->pool.used; it_; it_ = it_->next) { \
51 typeof((POOL)->blocks[0])* ITER = \
52 &(POOL)->blocks[it_ - (POOL)->pool.nodes]; \
53 (void)ITER; \
54 BODY; \
55 }
56
57typedef struct listpool {
58 size_t block_size_bytes;
59 size_t num_blocks;
60 list* free; // Head of the free list.
61 list* used; // Head of the used list.
62 list* nodes; // Array of nodes.
63 uint8_t* blocks; // Array of blocks;
64} listpool;
65
66/// Create a new list pool from a user-provided array of memory.
67/// `nodes` must have at least `num_blocks` nodes.
68/// `blocks` must be at least `num_blocks` * `block_size_bytes` bytes.
69/// All blocks are zeroed out for convenience.
70void listpool_make_(listpool* pool, list* nodes, void* blocks,
71 size_t num_blocks, size_t block_size_bytes);
72
73/// Allocate a new block.
74/// Return 0 if there is no memory left.
75void* listpool_alloc_(listpool* pool);
76
77/// Free the block.
78/// The block pointer is conveniently set to 0.
79void listpool_free_(listpool* pool, void** block_ptr);