diff options
Diffstat (limited to 'listpool/include')
-rw-r--r-- | listpool/include/listpool.h | 79 |
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 | |||
57 | typedef 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. | ||
70 | void 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. | ||
75 | void* listpool_alloc_(listpool* pool); | ||
76 | |||
77 | /// Free the block. | ||
78 | /// The block pointer is conveniently set to 0. | ||
79 | void listpool_free_(listpool* pool, void** block_ptr); | ||