From afe1e1d12e42a0881aff63c766c14e48319b560c Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 9 Mar 2024 08:36:02 -0800 Subject: Define functions to get the number of used blocks. --- mempool/include/mempool.h | 45 ++++++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 17 deletions(-) (limited to 'mempool/include') diff --git a/mempool/include/mempool.h b/mempool/include/mempool.h index de9ea4f..245173b 100644 --- a/mempool/include/mempool.h +++ b/mempool/include/mempool.h @@ -86,9 +86,15 @@ #define mempool_get_block_index(POOL, BLOCK_PTR) \ mempool_get_block_index_(&(POOL)->pool, BLOCK_PTR) -/// Return the total capacity of the mempool in bytes. +/// Return the block size in bytes. +#define mempool_block_size_bytes(POOL) mempool_block_size_bytes_(&(POOL)->pool) + +/// Return the total capacity of the mempool. #define mempool_capacity(POOL) mempool_capacity_(&(POOL)->pool) +/// Return the number of used blocks in the mempool. +#define mempool_size(POOL) mempool_size_(&(POOL)->pool) + /// Set whether to trap when attempting to allocate beyond capacity. #define mempool_enable_traps(POOL, enable) \ mempool_enable_traps_(&(POOL)->pool, enable) @@ -98,22 +104,24 @@ /// The caller can use 'i' as the index of the current block. /// /// It is valid to mempool_free() the object at each step of the iteration. -#define mempool_foreach(POOL, ITER, BODY) \ - { \ - size_t i = (POOL)->pool.used; \ - do { \ - if ((POOL)->pool.block_info[i].used) { \ - __typeof__((POOL)->object[0])* ITER = \ - &(((__typeof__((POOL)->object[0])*)(POOL)->pool.blocks))[i]; \ - (void)ITER; \ - BODY; \ - } \ - const size_t next = (POOL)->pool.block_info[i].next_used; \ - if (next == i) { \ - break; \ - } \ - i = next; \ - } while (true); \ +#define mempool_foreach(POOL, ITER, BODY) \ + { \ + size_t i = (POOL)->pool.used; \ + if ((POOL)->pool.num_used_blocks > 0) { \ + do { \ + if ((POOL)->pool.block_info[i].used) { \ + __typeof__((POOL)->object[0])* ITER = \ + &(((__typeof__((POOL)->object[0])*)(POOL)->pool.blocks))[i]; \ + (void)ITER; \ + BODY; \ + } \ + const size_t next = (POOL)->pool.block_info[i].next_used; \ + if (next == i) { \ + break; \ + } \ + i = next; \ + } while (true); \ + } \ } // ----------------------------------------------------------------------------- @@ -133,6 +141,7 @@ typedef struct BlockInfo { typedef struct mempool { size_t block_size_bytes; size_t num_blocks; + size_t num_used_blocks; size_t head; /// Points to the first block in the free list. size_t used; /// Points to the first block in the used list. bool dynamic; /// True if blocks and info are dynamically-allocated. @@ -161,5 +170,7 @@ void* mempool_alloc_(mempool*); void mempool_free_(mempool*, void** block_ptr); void* mempool_get_block_(const mempool*, size_t block_index); size_t mempool_get_block_index_(const mempool*, const void* block); +size_t mempool_block_size_bytes_(const mempool*); size_t mempool_capacity_(const mempool*); +size_t mempool_size_(const mempool*); void mempool_enable_traps_(mempool*, bool); -- cgit v1.2.3