From ecc2645ba4c44005ef13e33c79059de69b76551f Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sun, 16 Jul 2023 16:31:59 -0700 Subject: Add function to query pool capacity. --- mem/include/mem.h | 4 ++++ mem/src/mem.c | 5 +++++ mempool/include/mempool.h | 4 ++++ mempool/src/mempool.c | 5 +++++ 4 files changed, 18 insertions(+) diff --git a/mem/include/mem.h b/mem/include/mem.h index 30c24fc..69f426f 100644 --- a/mem/include/mem.h +++ b/mem/include/mem.h @@ -79,6 +79,9 @@ #define mem_get_chunk_handle(MEM, CHUNK_PTR) \ mem_get_chunk_handle_(&(MEM)->mem, CHUNK_PTR) +/// Return the total capacity of the allocator in bytes. +#define mem_capacity(MEM) mem_capacity_(&(MEM)->mem) + /// Iterate over the used chunks of the allocator. /// /// The caller can use 'i' as the index of the current chunk. @@ -147,3 +150,4 @@ void* mem_alloc_(Memory*, size_t num_blocks); void mem_free_(Memory*, void** chunk_ptr); void* mem_get_chunk_(const Memory*, size_t chunk_handle); size_t mem_get_chunk_handle_(const Memory*, const void* chunk); +size_t mem_capacity_(const Memory*); diff --git a/mem/src/mem.c b/mem/src/mem.c index ff97f0f..056d947 100644 --- a/mem/src/mem.c +++ b/mem/src/mem.c @@ -181,3 +181,8 @@ size_t mem_get_chunk_handle_(const Memory* mem, const void* chunk) { assert(block_byte_index % mem->block_size_bytes == 0); return block_byte_index / mem->block_size_bytes; } + +size_t mem_capacity_(const Memory* mem) { + assert(mem); + return mem->num_blocks * mem->block_size_bytes; +} diff --git a/mempool/include/mempool.h b/mempool/include/mempool.h index b76ae7c..8251a70 100644 --- a/mempool/include/mempool.h +++ b/mempool/include/mempool.h @@ -80,6 +80,9 @@ #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. +#define mempool_capacity(POOL) mempool_capacity_(&(POOL)->pool) + /// Iterate over the used blocks of the pool. /// /// The caller can use 'i' as the index of the current block. @@ -133,3 +136,4 @@ 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_capacity_(const mempool*); diff --git a/mempool/src/mempool.c b/mempool/src/mempool.c index fdb5f8c..679f124 100644 --- a/mempool/src/mempool.c +++ b/mempool/src/mempool.c @@ -124,3 +124,8 @@ size_t mempool_get_block_index_(const mempool* pool, const void* block) { assert(block_byte_index % pool->block_size_bytes == 0); return block_byte_index / pool->block_size_bytes; } + +size_t mempool_capacity_(const mempool* pool) { + assert(pool); + return pool->num_blocks * pool->block_size_bytes; +} -- cgit v1.2.3