diff options
| author | 3gg <3gg@shellblade.net> | 2023-01-23 18:32:49 -0800 |
|---|---|---|
| committer | 3gg <3gg@shellblade.net> | 2023-01-23 18:32:49 -0800 |
| commit | 470a25323ca89ffa3b0b697aeddcf184d12a1382 (patch) | |
| tree | c5076734e0154d5a3189105f61dc5f9a526a8e07 /listpool/include | |
| parent | f5b77325c44649bb92fa379b7df77c275f3925dc (diff) | |
Update listpool's interface to be on par with mempool's.
Diffstat (limited to 'listpool/include')
| -rw-r--r-- | listpool/include/listpool.h | 32 |
1 files changed, 26 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 | ||
| 57 | typedef struct listpool { | 70 | typedef 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. |
| 80 | void listpool_free_(listpool* pool, void** block_ptr); | 93 | void listpool_free_(listpool* pool, void** block_ptr); |
| 94 | |||
| 95 | /// Return the ith block. | ||
| 96 | /// The block must have been allocated. | ||
| 97 | void* listpool_get_block_(const listpool*, size_t block_index); | ||
| 98 | |||
| 99 | /// Get the index to the given block. | ||
| 100 | size_t listpool_get_block_index_(const listpool*, const void* block); | ||
