diff options
Diffstat (limited to 'mem')
-rw-r--r-- | mem/src/mem.c | 18 | ||||
-rw-r--r-- | mem/test/mem_test.c | 6 |
2 files changed, 16 insertions, 8 deletions
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) { | |||
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 | if (mem->num_blocks > 0) { |
61 | mem->next_free_chunk = 0; | 61 | mem->num_used_blocks = 0; |
62 | memset(mem->blocks, 0, mem->num_blocks * mem->block_size_bytes); | 62 | mem->next_free_chunk = 0; |
63 | memset(mem->chunks, 0, mem->num_blocks * sizeof(Chunk)); | 63 | memset(mem->blocks, 0, mem->num_blocks * mem->block_size_bytes); |
64 | 64 | memset(mem->chunks, 0, mem->num_blocks * sizeof(Chunk)); | |
65 | // Initialize the head as one large free chunk. | 65 | |
66 | Chunk* head = &mem->chunks[0]; | 66 | // Initialize the head as one large free chunk. |
67 | head->num_blocks = mem->num_blocks; | 67 | Chunk* head = &mem->chunks[0]; |
68 | head->num_blocks = mem->num_blocks; | ||
69 | } | ||
68 | } | 70 | } |
69 | 71 | ||
70 | void* mem_alloc_(Memory* mem, size_t num_blocks) { | 72 | void* mem_alloc_(Memory* mem, size_t num_blocks) { |
diff --git a/mem/test/mem_test.c b/mem/test/mem_test.c index a8d482f..52ce5a9 100644 --- a/mem/test/mem_test.c +++ b/mem/test/mem_test.c | |||
@@ -32,6 +32,12 @@ TEST_CASE(mem_create_dyn) { | |||
32 | mem_make_dyn(&mem, NUM_BLOCKS, sizeof(int)); | 32 | mem_make_dyn(&mem, NUM_BLOCKS, sizeof(int)); |
33 | } | 33 | } |
34 | 34 | ||
35 | // Clear an uninitialized allocator. | ||
36 | TEST_CASE(mem_clear_uninitialized) { | ||
37 | test_mem mem = {0}; | ||
38 | mem_clear(&mem); | ||
39 | } | ||
40 | |||
35 | // Allocate N chunks of 1 block each. | 41 | // Allocate N chunks of 1 block each. |
36 | TEST_CASE(mem_fully_allocate) { | 42 | TEST_CASE(mem_fully_allocate) { |
37 | test_mem mem; | 43 | test_mem mem; |