aboutsummaryrefslogtreecommitdiff
path: root/listpool
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2023-01-23 18:32:49 -0800
committer3gg <3gg@shellblade.net>2023-01-23 18:32:49 -0800
commit470a25323ca89ffa3b0b697aeddcf184d12a1382 (patch)
treec5076734e0154d5a3189105f61dc5f9a526a8e07 /listpool
parentf5b77325c44649bb92fa379b7df77c275f3925dc (diff)
Update listpool's interface to be on par with mempool's.
Diffstat (limited to 'listpool')
-rw-r--r--listpool/include/listpool.h32
-rw-r--r--listpool/src/listpool.c13
-rw-r--r--listpool/test/listpool_test.c17
3 files changed, 56 insertions, 6 deletions
diff --git a/listpool/include/listpool.h b/listpool/include/listpool.h
index 1711449..85a3b27 100644
--- a/listpool/include/listpool.h
+++ b/listpool/include/listpool.h
@@ -45,13 +45,26 @@
45 }); \ 45 }); \
46 } 46 }
47 47
48/// Return the ith block.
49/// The block must have been allocated.
50#define listpool_get_block(POOL, INDEX) \
51 ((typeof((POOL)->blocks[0])*)listpool_get_block_(&(POOL)->pool, INDEX))
52
53/// Get the index to the given block.
54#define listpool_get_block_index(POOL, BLOCK_PTR) \
55 listpool_get_block_index_(&(POOL)->pool, BLOCK_PTR)
56
48/// Iterate over the used items of the pool. 57/// Iterate over the used items of the pool.
49#define listpool_foreach(POOL, ITER, BODY) \ 58///
50 for (list* it_ = (POOL)->pool.used; it_; it_ = it_->next) { \ 59/// The caller can use 'i' as the index of the current block.
51 typeof((POOL)->blocks[0])* ITER = \ 60///
52 &(POOL)->blocks[it_ - (POOL)->pool.nodes]; \ 61/// It is valid to mempool_free() the object at each step of the iteration.
53 (void)ITER; \ 62#define listpool_foreach(POOL, ITER, BODY) \
54 BODY; \ 63 for (list* it_ = (POOL)->pool.used; it_; it_ = it_->next) { \
64 const size_t i = it_ - (POOL)->pool.nodes; \
65 typeof((POOL)->blocks[0])* ITER = &(POOL)->blocks[i]; \
66 (void)ITER; \
67 BODY; \
55 } 68 }
56 69
57typedef struct listpool { 70typedef struct listpool {
@@ -78,3 +91,10 @@ void* listpool_alloc_(listpool* pool);
78/// Free the block. 91/// Free the block.
79/// The block pointer is conveniently set to 0. 92/// The block pointer is conveniently set to 0.
80void listpool_free_(listpool* pool, void** block_ptr); 93void listpool_free_(listpool* pool, void** block_ptr);
94
95/// Return the ith block.
96/// The block must have been allocated.
97void* listpool_get_block_(const listpool*, size_t block_index);
98
99/// Get the index to the given block.
100size_t listpool_get_block_index_(const listpool*, const void* block);
diff --git a/listpool/src/listpool.c b/listpool/src/listpool.c
index 8e49f32..758062c 100644
--- a/listpool/src/listpool.c
+++ b/listpool/src/listpool.c
@@ -77,3 +77,16 @@ void listpool_free_(listpool* pool, void** block_ptr) {
77 77
78 *block_ptr = 0; 78 *block_ptr = 0;
79} 79}
80
81void* listpool_get_block_(const listpool* pool, size_t block_index) {
82 assert(pool);
83 assert(block_index < pool->num_blocks);
84 return pool->blocks + block_index * pool->block_size_bytes;
85}
86
87size_t listpool_get_block_index_(const listpool* pool, const void* block) {
88 assert(pool);
89 const size_t block_byte_index = (const uint8_t*)block - pool->blocks;
90 assert(block_byte_index % pool->block_size_bytes == 0);
91 return block_byte_index / pool->block_size_bytes;
92}
diff --git a/listpool/test/listpool_test.c b/listpool/test/listpool_test.c
index cb54d00..7a7b0cf 100644
--- a/listpool/test/listpool_test.c
+++ b/listpool/test/listpool_test.c
@@ -137,6 +137,23 @@ TEST_CASE(listpool_traverse_full) {
137 TEST_EQUAL(sum(&pool), (NUM_BLOCKS) * (NUM_BLOCKS + 1) / 2); 137 TEST_EQUAL(sum(&pool), (NUM_BLOCKS) * (NUM_BLOCKS + 1) / 2);
138} 138}
139 139
140// Get the ith (allocated) block.
141TEST_CASE(listpool_get_block) {
142 test_pool pool;
143 listpool_make(&pool);
144
145 for (int i = 0; i < NUM_BLOCKS; ++i) {
146 int* block = listpool_alloc(&pool);
147 TEST_TRUE(block != 0);
148 *block = i;
149 TEST_EQUAL(listpool_get_block_index(&pool, block), (size_t)i);
150 }
151
152 for (int i = 0; i < NUM_BLOCKS; ++i) {
153 TEST_EQUAL(*listpool_get_block(&pool, i), i);
154 }
155}
156
140// Remove a value from the list. 157// Remove a value from the list.
141TEST_CASE(listpool_remove_value) { 158TEST_CASE(listpool_remove_value) {
142 test_pool pool; 159 test_pool pool;