aboutsummaryrefslogtreecommitdiff
path: root/mempool/include/mempool.h
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2023-07-16 17:39:54 -0700
committer3gg <3gg@shellblade.net>2023-07-16 17:39:54 -0700
commit7ab7d7d5b836d2595c5dc2c6db90c489f6768246 (patch)
treed87f7b7f6f7eedbfc4eb6133369d9fb0c0cc6fb2 /mempool/include/mempool.h
parentecc2645ba4c44005ef13e33c79059de69b76551f (diff)
Fix mem and mempool iteration.
Diffstat (limited to 'mempool/include/mempool.h')
-rw-r--r--mempool/include/mempool.h44
1 files changed, 23 insertions, 21 deletions
diff --git a/mempool/include/mempool.h b/mempool/include/mempool.h
index 8251a70..23786b3 100644
--- a/mempool/include/mempool.h
+++ b/mempool/include/mempool.h
@@ -19,19 +19,22 @@
19#include <stdint.h> 19#include <stdint.h>
20 20
21/// Define a statically-allocated, typed pool of the given number of blocks. 21/// Define a statically-allocated, typed pool of the given number of blocks.
22#define DEF_MEMPOOL(POOL, TYPE, NUM_BLOCKS) \ 22#define DEF_MEMPOOL(POOL, TYPE, NUM_BLOCKS) \
23 typedef struct POOL { \ 23 typedef struct POOL { \
24 mempool pool; \ 24 mempool pool; \
25 BlockInfo block_info[NUM_BLOCKS]; \ 25 BlockInfo block_info[NUM_BLOCKS]; \
26 TYPE blocks[NUM_BLOCKS]; \ 26 TYPE blocks[NUM_BLOCKS]; \
27 /* For uniformity with the dynamically-allocated pool. */ \
28 TYPE* object; \
27 } POOL; 29 } POOL;
28 30
29/// Define a dynamically-allocated, typed pool. 31/// Define a dynamically-allocated, typed pool.
30#define DEF_MEMPOOL_DYN(POOL, TYPE) \ 32#define DEF_MEMPOOL_DYN(POOL, TYPE) \
31 typedef struct POOL { \ 33 typedef struct POOL { \
32 mempool pool; \ 34 mempool pool; \
33 BlockInfo* block_info; \ 35 /* Does not point anywhere. Storing a pointer here so that we can recall \
34 TYPE* blocks; \ 36 * the type. */ \
37 TYPE* object; \
35 } POOL; 38 } POOL;
36 39
37/// Initialize a statically-allocated pool. 40/// Initialize a statically-allocated pool.
@@ -74,7 +77,7 @@
74/// Return the ith block. 77/// Return the ith block.
75/// The block must have been allocated. 78/// The block must have been allocated.
76#define mempool_get_block(POOL, INDEX) \ 79#define mempool_get_block(POOL, INDEX) \
77 ((__typeof__((POOL)->blocks[0])*)mempool_get_block_(&(POOL)->pool, INDEX)) 80 ((__typeof__((POOL)->object[0])*)mempool_get_block_(&(POOL)->pool, INDEX))
78 81
79/// Get the index to the given block. 82/// Get the index to the given block.
80#define mempool_get_block_index(POOL, BLOCK_PTR) \ 83#define mempool_get_block_index(POOL, BLOCK_PTR) \
@@ -88,16 +91,15 @@
88/// The caller can use 'i' as the index of the current block. 91/// The caller can use 'i' as the index of the current block.
89/// 92///
90/// It is valid to mempool_free() the object at each step of the iteration. 93/// It is valid to mempool_free() the object at each step of the iteration.
91#define mempool_foreach(POOL, ITER, BODY) \ 94#define mempool_foreach(POOL, ITER, BODY) \
92 for (size_t i = 0; \ 95 for (size_t i = 0; i < (POOL)->pool.num_blocks; ++i) { \
93 i < (sizeof((POOL)->blocks) / sizeof(__typeof__((POOL)->blocks[0]))); \ 96 if (!(POOL)->pool.block_info[i].used) { \
94 ++i) { \ 97 continue; \
95 if (!(POOL)->block_info[i].used) { \ 98 } \
96 continue; \ 99 __typeof__((POOL)->object[0])* ITER = \
97 } \ 100 &(((__typeof__((POOL)->object[0])*)(POOL)->pool.blocks))[i]; \
98 __typeof__((POOL)->blocks[0])* ITER = &(POOL)->blocks[i]; \ 101 (void)ITER; \
99 (void)ITER; \ 102 BODY; \
100 BODY; \
101 } 103 }
102 104
103// ----------------------------------------------------------------------------- 105// -----------------------------------------------------------------------------