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/src/mempool.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'mempool/src') diff --git a/mempool/src/mempool.c b/mempool/src/mempool.c index b09038b..46f1053 100644 --- a/mempool/src/mempool.c +++ b/mempool/src/mempool.c @@ -24,6 +24,7 @@ bool mempool_make_( pool->block_size_bytes = block_size_bytes; pool->num_blocks = num_blocks; + pool->num_used_blocks = 0; pool->head = 0; pool->used = 0; pool->trap = true; @@ -91,6 +92,8 @@ void* mempool_alloc_(mempool* pool) { pool->head = head->next_free; head->next_free = 0; + pool->num_used_blocks++; + return block; } @@ -119,6 +122,8 @@ void mempool_free_(mempool* pool, void** block_ptr) { pool->used = 0; } + pool->num_used_blocks--; + *block_ptr = 0; } @@ -136,9 +141,19 @@ size_t mempool_get_block_index_(const mempool* pool, const void* block) { return block_byte_index / pool->block_size_bytes; } +size_t mempool_block_size_bytes_(const mempool* pool) { + assert(pool); + return pool->block_size_bytes; +} + size_t mempool_capacity_(const mempool* pool) { assert(pool); - return pool->num_blocks * pool->block_size_bytes; + return pool->num_blocks; +} + +size_t mempool_size_(const mempool* pool) { + assert(pool); + return pool->num_used_blocks; } void mempool_enable_traps_(mempool* pool, bool enable) { -- cgit v1.2.3