From e5eb3845eff1ea080ffdc08102f7d1a6dee1179f Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Tue, 19 Aug 2025 18:22:34 +0200 Subject: Add tests for clearing uninitialized memory allocators --- mem/src/mem.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'mem/src/mem.c') diff --git a/mem/src/mem.c b/mem/src/mem.c index 9169a9f..70648c9 100644 --- a/mem/src/mem.c +++ b/mem/src/mem.c @@ -57,14 +57,16 @@ void mem_del_(Memory* mem) { void mem_clear_(Memory* mem) { assert(mem); - mem->num_used_blocks = 0; - mem->next_free_chunk = 0; - memset(mem->blocks, 0, mem->num_blocks * mem->block_size_bytes); - memset(mem->chunks, 0, mem->num_blocks * sizeof(Chunk)); - - // Initialize the head as one large free chunk. - Chunk* head = &mem->chunks[0]; - head->num_blocks = mem->num_blocks; + if (mem->num_blocks > 0) { + mem->num_used_blocks = 0; + mem->next_free_chunk = 0; + memset(mem->blocks, 0, mem->num_blocks * mem->block_size_bytes); + memset(mem->chunks, 0, mem->num_blocks * sizeof(Chunk)); + + // Initialize the head as one large free chunk. + Chunk* head = &mem->chunks[0]; + head->num_blocks = mem->num_blocks; + } } void* mem_alloc_(Memory* mem, size_t num_blocks) { -- cgit v1.2.3