diff options
Diffstat (limited to 'listpool/include/listpool.h')
-rw-r--r-- | listpool/include/listpool.h | 57 |
1 files changed, 29 insertions, 28 deletions
diff --git a/listpool/include/listpool.h b/listpool/include/listpool.h index a5e4955..1711449 100644 --- a/listpool/include/listpool.h +++ b/listpool/include/listpool.h | |||
@@ -7,11 +7,11 @@ | |||
7 | #include <stdint.h> | 7 | #include <stdint.h> |
8 | 8 | ||
9 | /// Define a typed listpool of a given size. | 9 | /// Define a typed listpool of a given size. |
10 | #define DEF_LISTPOOL(POOL, TYPE, NUM_BLOCKS) \ | 10 | #define DEF_LISTPOOL(POOL, TYPE, NUM_BLOCKS) \ |
11 | typedef struct POOL { \ | 11 | typedef struct POOL { \ |
12 | listpool pool; \ | 12 | listpool pool; \ |
13 | list nodes[NUM_BLOCKS]; \ | 13 | list nodes[NUM_BLOCKS]; \ |
14 | TYPE blocks[NUM_BLOCKS]; \ | 14 | TYPE blocks[NUM_BLOCKS]; \ |
15 | } POOL; | 15 | } POOL; |
16 | 16 | ||
17 | /// Creates a new listpool. | 17 | /// Creates a new listpool. |
@@ -20,8 +20,8 @@ | |||
20 | assert(POOL); \ | 20 | assert(POOL); \ |
21 | const size_t block_size = sizeof((POOL)->blocks[0]); \ | 21 | const size_t block_size = sizeof((POOL)->blocks[0]); \ |
22 | const size_t num_blocks = sizeof((POOL)->blocks) / block_size; \ | 22 | const size_t num_blocks = sizeof((POOL)->blocks) / block_size; \ |
23 | listpool_make_(&(POOL)->pool, (POOL)->nodes, (POOL)->blocks, num_blocks, \ | 23 | listpool_make_( \ |
24 | block_size); \ | 24 | &(POOL)->pool, (POOL)->nodes, (POOL)->blocks, num_blocks, block_size); \ |
25 | } | 25 | } |
26 | 26 | ||
27 | /// Allocate a new block. | 27 | /// Allocate a new block. |
@@ -35,31 +35,31 @@ | |||
35 | /// Remove a value from the list. | 35 | /// Remove a value from the list. |
36 | /// Defined here instead of DEF_LISTPOOL_IMPL() because not all types may have | 36 | /// Defined here instead of DEF_LISTPOOL_IMPL() because not all types may have |
37 | /// an operator==. | 37 | /// an operator==. |
38 | #define listpool_remove(POOL, VAL) \ | 38 | #define listpool_remove(POOL, VAL) \ |
39 | { \ | 39 | { \ |
40 | listpool_foreach(POOL, iter, { \ | 40 | listpool_foreach(POOL, iter, { \ |
41 | if (*iter == VAL) { \ | 41 | if (*iter == VAL) { \ |
42 | listpool_free(POOL, &iter); \ | 42 | listpool_free(POOL, &iter); \ |
43 | break; \ | 43 | break; \ |
44 | } \ | 44 | } \ |
45 | }); \ | 45 | }); \ |
46 | } | 46 | } |
47 | 47 | ||
48 | /// Iterate over the used items of the pool. | 48 | /// Iterate over the used items of the pool. |
49 | #define listpool_foreach(POOL, ITER, BODY) \ | 49 | #define listpool_foreach(POOL, ITER, BODY) \ |
50 | for (list* it_ = (POOL)->pool.used; it_; it_ = it_->next) { \ | 50 | for (list* it_ = (POOL)->pool.used; it_; it_ = it_->next) { \ |
51 | typeof((POOL)->blocks[0])* ITER = \ | 51 | typeof((POOL)->blocks[0])* ITER = \ |
52 | &(POOL)->blocks[it_ - (POOL)->pool.nodes]; \ | 52 | &(POOL)->blocks[it_ - (POOL)->pool.nodes]; \ |
53 | (void)ITER; \ | 53 | (void)ITER; \ |
54 | BODY; \ | 54 | BODY; \ |
55 | } | 55 | } |
56 | 56 | ||
57 | typedef struct listpool { | 57 | typedef struct listpool { |
58 | size_t block_size_bytes; | 58 | size_t block_size_bytes; |
59 | size_t num_blocks; | 59 | size_t num_blocks; |
60 | list* free; // Head of the free list. | 60 | list* free; // Head of the free list. |
61 | list* used; // Head of the used list. | 61 | list* used; // Head of the used list. |
62 | list* nodes; // Array of nodes. | 62 | list* nodes; // Array of nodes. |
63 | uint8_t* blocks; // Array of blocks; | 63 | uint8_t* blocks; // Array of blocks; |
64 | } listpool; | 64 | } listpool; |
65 | 65 | ||
@@ -67,8 +67,9 @@ typedef struct listpool { | |||
67 | /// `nodes` must have at least `num_blocks` nodes. | 67 | /// `nodes` must have at least `num_blocks` nodes. |
68 | /// `blocks` must be at least `num_blocks` * `block_size_bytes` bytes. | 68 | /// `blocks` must be at least `num_blocks` * `block_size_bytes` bytes. |
69 | /// All blocks are zeroed out for convenience. | 69 | /// All blocks are zeroed out for convenience. |
70 | void listpool_make_(listpool* pool, list* nodes, void* blocks, | 70 | void listpool_make_( |
71 | size_t num_blocks, size_t block_size_bytes); | 71 | listpool* pool, list* nodes, void* blocks, size_t num_blocks, |
72 | size_t block_size_bytes); | ||
72 | 73 | ||
73 | /// Allocate a new block. | 74 | /// Allocate a new block. |
74 | /// Return 0 if there is no memory left. | 75 | /// Return 0 if there is no memory left. |