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. --- mem/src/mem.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'mem/src') diff --git a/mem/src/mem.c b/mem/src/mem.c index 2904035..4f5e5ef 100644 --- a/mem/src/mem.c +++ b/mem/src/mem.c @@ -14,6 +14,7 @@ bool mem_make_( mem->block_size_bytes = block_size_bytes; mem->num_blocks = num_blocks; + mem->num_used_blocks = 0; mem->next_free_chunk = 0; mem->trap = true; @@ -107,6 +108,7 @@ void* mem_alloc_(Memory* mem, size_t num_blocks) { } while (chunk_idx != start); if (found) { + mem->num_used_blocks++; mem->next_free_chunk = mem->chunks[chunk_idx].next; return &mem->blocks[chunk_idx * mem->block_size_bytes]; } else { @@ -168,6 +170,8 @@ void mem_free_(Memory* mem, void** chunk_ptr) { } } + mem->num_used_blocks--; + *chunk_ptr = 0; } @@ -189,9 +193,19 @@ size_t mem_get_chunk_handle_(const Memory* mem, const void* chunk) { return block_byte_index / mem->block_size_bytes; } +size_t mem_block_size_bytes_(const Memory* mem) { + assert(mem); + return mem->block_size_bytes; +} + size_t mem_capacity_(const Memory* mem) { assert(mem); - return mem->num_blocks * mem->block_size_bytes; + return mem->num_blocks; +} + +size_t mem_size_(const Memory* mem) { + assert(mem); + return mem->num_used_blocks; } void mem_enable_traps_(Memory* mem, bool enable) { -- cgit v1.2.3