diff options
| author | 3gg <3gg@shellblade.net> | 2025-06-30 19:29:07 -0700 |
|---|---|---|
| committer | 3gg <3gg@shellblade.net> | 2025-06-30 19:29:07 -0700 |
| commit | 1fc8bb7142868bbeac5bcbbd489cd23347716203 (patch) | |
| tree | 491b77b91dc4fa30d199cd56ec54c58e1d716e71 | |
| parent | b48f99a40b1a58c02ccbcb75bc18a158294c356f (diff) | |
Fix: set num used blocks to 0 on clear
| -rw-r--r-- | mem/src/mem.c | 1 | ||||
| -rw-r--r-- | mem/test/mem_test.c | 2 | ||||
| -rw-r--r-- | mempool/src/mempool.c | 5 | ||||
| -rw-r--r-- | mempool/test/mempool_test.c | 8 |
4 files changed, 11 insertions, 5 deletions
diff --git a/mem/src/mem.c b/mem/src/mem.c index c2af518..9169a9f 100644 --- a/mem/src/mem.c +++ b/mem/src/mem.c | |||
| @@ -57,6 +57,7 @@ void mem_del_(Memory* mem) { | |||
| 57 | 57 | ||
| 58 | void mem_clear_(Memory* mem) { | 58 | void mem_clear_(Memory* mem) { |
| 59 | assert(mem); | 59 | assert(mem); |
| 60 | mem->num_used_blocks = 0; | ||
| 60 | mem->next_free_chunk = 0; | 61 | mem->next_free_chunk = 0; |
| 61 | memset(mem->blocks, 0, mem->num_blocks * mem->block_size_bytes); | 62 | memset(mem->blocks, 0, mem->num_blocks * mem->block_size_bytes); |
| 62 | memset(mem->chunks, 0, mem->num_blocks * sizeof(Chunk)); | 63 | memset(mem->chunks, 0, mem->num_blocks * sizeof(Chunk)); |
diff --git a/mem/test/mem_test.c b/mem/test/mem_test.c index 14718a5..88c8b14 100644 --- a/mem/test/mem_test.c +++ b/mem/test/mem_test.c | |||
| @@ -220,6 +220,8 @@ TEST_CASE(mem_clear_then_reuse) { | |||
| 220 | } | 220 | } |
| 221 | 221 | ||
| 222 | mem_clear(&mem); | 222 | mem_clear(&mem); |
| 223 | TEST_EQUAL(mem_size(&mem), 0); | ||
| 224 | TEST_EQUAL(mem_capacity(&mem), NUM_BLOCKS); | ||
| 223 | 225 | ||
| 224 | // Allocate chunks and assign values 0..N. | 226 | // Allocate chunks and assign values 0..N. |
| 225 | for (int i = 0; i < NUM_BLOCKS; ++i) { | 227 | for (int i = 0; i < NUM_BLOCKS; ++i) { |
diff --git a/mempool/src/mempool.c b/mempool/src/mempool.c index 444d602..c398c4f 100644 --- a/mempool/src/mempool.c +++ b/mempool/src/mempool.c | |||
| @@ -66,8 +66,9 @@ void mempool_del_(mempool* pool) { | |||
| 66 | 66 | ||
| 67 | void mempool_clear_(mempool* pool) { | 67 | void mempool_clear_(mempool* pool) { |
| 68 | assert(pool); | 68 | assert(pool); |
| 69 | pool->head = 0; | 69 | pool->head = 0; |
| 70 | pool->used = 0; | 70 | pool->used = 0; |
| 71 | pool->num_used_blocks = 0; | ||
| 71 | memset(pool->blocks, 0, pool->num_blocks * pool->block_size_bytes); | 72 | memset(pool->blocks, 0, pool->num_blocks * pool->block_size_bytes); |
| 72 | memset(pool->block_info, 0, pool->num_blocks * sizeof(BlockInfo)); | 73 | memset(pool->block_info, 0, pool->num_blocks * sizeof(BlockInfo)); |
| 73 | init_free_list(pool); | 74 | init_free_list(pool); |
diff --git a/mempool/test/mempool_test.c b/mempool/test/mempool_test.c index 843f7e7..4c55e4b 100644 --- a/mempool/test/mempool_test.c +++ b/mempool/test/mempool_test.c | |||
| @@ -178,16 +178,18 @@ TEST_CASE(mem_clear_then_reuse) { | |||
| 178 | 178 | ||
| 179 | // Allocate chunks, contents not important. | 179 | // Allocate chunks, contents not important. |
| 180 | for (int i = 0; i < NUM_BLOCKS; ++i) { | 180 | for (int i = 0; i < NUM_BLOCKS; ++i) { |
| 181 | int* chunk = mempool_alloc(&mem); | 181 | const int* chunk = mempool_alloc(&mem); |
| 182 | TEST_TRUE(chunk != 0); | 182 | TEST_TRUE(chunk != nullptr); |
| 183 | } | 183 | } |
| 184 | 184 | ||
| 185 | mempool_clear(&mem); | 185 | mempool_clear(&mem); |
| 186 | TEST_EQUAL(mempool_size(&mem), 0); | ||
| 187 | TEST_EQUAL(mempool_capacity(&mem), NUM_BLOCKS); | ||
| 186 | 188 | ||
| 187 | // Allocate chunks and assign values 0..N. | 189 | // Allocate chunks and assign values 0..N. |
| 188 | for (int i = 0; i < NUM_BLOCKS; ++i) { | 190 | for (int i = 0; i < NUM_BLOCKS; ++i) { |
| 189 | int* chunk = mempool_alloc(&mem); | 191 | int* chunk = mempool_alloc(&mem); |
| 190 | TEST_TRUE(chunk != 0); | 192 | TEST_TRUE(chunk != nullptr); |
| 191 | *chunk = i + 1; | 193 | *chunk = i + 1; |
| 192 | } | 194 | } |
| 193 | 195 | ||
