diff options
Diffstat (limited to 'mem')
-rw-r--r-- | mem/src/mem.c | 14 | ||||
-rw-r--r-- | mem/test/mem_test.c | 30 |
2 files changed, 24 insertions, 20 deletions
diff --git a/mem/src/mem.c b/mem/src/mem.c index 4f5e5ef..9169a9f 100644 --- a/mem/src/mem.c +++ b/mem/src/mem.c | |||
@@ -46,17 +46,18 @@ void mem_del_(Memory* mem) { | |||
46 | if (mem->dynamic) { | 46 | if (mem->dynamic) { |
47 | if (mem->chunks) { | 47 | if (mem->chunks) { |
48 | free(mem->chunks); | 48 | free(mem->chunks); |
49 | mem->chunks = 0; | 49 | mem->chunks = nullptr; |
50 | } | 50 | } |
51 | if (mem->blocks) { | 51 | if (mem->blocks) { |
52 | free(mem->blocks); | 52 | free(mem->blocks); |
53 | mem->blocks = 0; | 53 | mem->blocks = nullptr; |
54 | } | 54 | } |
55 | } | 55 | } |
56 | } | 56 | } |
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)); |
@@ -113,10 +114,11 @@ void* mem_alloc_(Memory* mem, size_t num_blocks) { | |||
113 | return &mem->blocks[chunk_idx * mem->block_size_bytes]; | 114 | return &mem->blocks[chunk_idx * mem->block_size_bytes]; |
114 | } else { | 115 | } else { |
115 | if (mem->trap) { | 116 | if (mem->trap) { |
116 | FAIL("Memory allocation failed, increase the allocator's capacity or " | 117 | FAIL( |
117 | "avoid fragmentation."); | 118 | "Memory allocation failed, increase the allocator's capacity or " |
119 | "avoid fragmentation."); | ||
118 | } | 120 | } |
119 | return 0; // Large-enough free chunk not found. | 121 | return nullptr; // Large-enough free chunk not found. |
120 | } | 122 | } |
121 | } | 123 | } |
122 | 124 | ||
@@ -172,7 +174,7 @@ void mem_free_(Memory* mem, void** chunk_ptr) { | |||
172 | 174 | ||
173 | mem->num_used_blocks--; | 175 | mem->num_used_blocks--; |
174 | 176 | ||
175 | *chunk_ptr = 0; | 177 | *chunk_ptr = nullptr; |
176 | } | 178 | } |
177 | 179 | ||
178 | // The handle is the chunk's index. We don't call it an index in the public API | 180 | // The handle is the chunk's index. We don't call it an index in the public API |
diff --git a/mem/test/mem_test.c b/mem/test/mem_test.c index 14718a5..a8d482f 100644 --- a/mem/test/mem_test.c +++ b/mem/test/mem_test.c | |||
@@ -39,7 +39,7 @@ TEST_CASE(mem_fully_allocate) { | |||
39 | 39 | ||
40 | for (int i = 0; i < NUM_BLOCKS; ++i) { | 40 | for (int i = 0; i < NUM_BLOCKS; ++i) { |
41 | const int* block = mem_alloc(&mem, 1); | 41 | const int* block = mem_alloc(&mem, 1); |
42 | TEST_TRUE(block != 0); | 42 | TEST_TRUE(block != nullptr); |
43 | } | 43 | } |
44 | 44 | ||
45 | TEST_TRUE(mem_size(&mem) == NUM_BLOCKS); | 45 | TEST_TRUE(mem_size(&mem) == NUM_BLOCKS); |
@@ -50,15 +50,15 @@ TEST_CASE(mem_fill_then_free) { | |||
50 | test_mem mem; | 50 | test_mem mem; |
51 | mem_make(&mem); | 51 | mem_make(&mem); |
52 | 52 | ||
53 | int* blocks[NUM_BLOCKS] = {0}; | 53 | int* blocks[NUM_BLOCKS] = {nullptr}; |
54 | for (int i = 0; i < NUM_BLOCKS; i++) { | 54 | for (int i = 0; i < NUM_BLOCKS; i++) { |
55 | blocks[i] = mem_alloc(&mem, 1); | 55 | blocks[i] = mem_alloc(&mem, 1); |
56 | TEST_TRUE(blocks[i] != 0); | 56 | TEST_TRUE(blocks[i] != nullptr); |
57 | } | 57 | } |
58 | 58 | ||
59 | for (int i = 0; i < NUM_BLOCKS; i++) { | 59 | for (int i = 0; i < NUM_BLOCKS; i++) { |
60 | mem_free(&mem, &blocks[i]); | 60 | mem_free(&mem, &blocks[i]); |
61 | TEST_EQUAL(blocks[i], 0); // Pointer should be set to 0 on free. | 61 | TEST_EQUAL(blocks[i], nullptr); // Pointer should be set to 0 on free. |
62 | } | 62 | } |
63 | 63 | ||
64 | TEST_EQUAL(count(&mem), 0); | 64 | TEST_EQUAL(count(&mem), 0); |
@@ -74,12 +74,12 @@ TEST_CASE(mem_allocate_beyond_max_size) { | |||
74 | 74 | ||
75 | // Fully allocate the mem. | 75 | // Fully allocate the mem. |
76 | for (int i = 0; i < NUM_BLOCKS; ++i) { | 76 | for (int i = 0; i < NUM_BLOCKS; ++i) { |
77 | TEST_TRUE(mem_alloc(&mem, 1) != 0); | 77 | TEST_TRUE(mem_alloc(&mem, 1) != nullptr); |
78 | } | 78 | } |
79 | 79 | ||
80 | // Past the end. | 80 | // Past the end. |
81 | for (int i = 0; i < NUM_BLOCKS; ++i) { | 81 | for (int i = 0; i < NUM_BLOCKS; ++i) { |
82 | TEST_EQUAL(mem_alloc(&mem, 1), 0); | 82 | TEST_EQUAL(mem_alloc(&mem, 1), nullptr); |
83 | } | 83 | } |
84 | 84 | ||
85 | TEST_TRUE(mem_size(&mem) == NUM_BLOCKS); | 85 | TEST_TRUE(mem_size(&mem) == NUM_BLOCKS); |
@@ -105,7 +105,7 @@ TEST_CASE(mem_zero_free_block_after_free) { | |||
105 | mem_make(&mem); | 105 | mem_make(&mem); |
106 | 106 | ||
107 | int* val = mem_alloc(&mem, 1); | 107 | int* val = mem_alloc(&mem, 1); |
108 | TEST_TRUE(val != 0); | 108 | TEST_TRUE(val != nullptr); |
109 | *val = 177; | 109 | *val = 177; |
110 | 110 | ||
111 | int* old_val = val; | 111 | int* old_val = val; |
@@ -131,7 +131,7 @@ TEST_CASE(mem_traverse_partially_full) { | |||
131 | 131 | ||
132 | for (int i = 0; i < N; ++i) { | 132 | for (int i = 0; i < N; ++i) { |
133 | int* val = mem_alloc(&mem, 1); | 133 | int* val = mem_alloc(&mem, 1); |
134 | TEST_TRUE(val != 0); | 134 | TEST_TRUE(val != nullptr); |
135 | *val = i + 1; | 135 | *val = i + 1; |
136 | } | 136 | } |
137 | 137 | ||
@@ -146,7 +146,7 @@ TEST_CASE(mem_traverse_full) { | |||
146 | 146 | ||
147 | for (int i = 0; i < NUM_BLOCKS; ++i) { | 147 | for (int i = 0; i < NUM_BLOCKS; ++i) { |
148 | int* val = mem_alloc(&mem, 1); | 148 | int* val = mem_alloc(&mem, 1); |
149 | TEST_TRUE(val != 0); | 149 | TEST_TRUE(val != nullptr); |
150 | *val = i + 1; | 150 | *val = i + 1; |
151 | } | 151 | } |
152 | 152 | ||
@@ -161,7 +161,7 @@ TEST_CASE(mem_get_block) { | |||
161 | 161 | ||
162 | for (int i = 0; i < NUM_BLOCKS; ++i) { | 162 | for (int i = 0; i < NUM_BLOCKS; ++i) { |
163 | int* block = mem_alloc(&mem, 1); | 163 | int* block = mem_alloc(&mem, 1); |
164 | TEST_TRUE(block != 0); | 164 | TEST_TRUE(block != nullptr); |
165 | *block = i; | 165 | *block = i; |
166 | TEST_EQUAL(mem_get_chunk_handle(&mem, block), (size_t)i); | 166 | TEST_EQUAL(mem_get_chunk_handle(&mem, block), (size_t)i); |
167 | } | 167 | } |
@@ -179,7 +179,7 @@ TEST_CASE(mem_fragmentation) { | |||
179 | test_mem mem; | 179 | test_mem mem; |
180 | mem_make(&mem); | 180 | mem_make(&mem); |
181 | 181 | ||
182 | int* blocks[NUM_BLOCKS] = {0}; | 182 | int* blocks[NUM_BLOCKS] = {nullptr}; |
183 | int next_block = 0; | 183 | int next_block = 0; |
184 | 184 | ||
185 | #define ALLOC(num_blocks) \ | 185 | #define ALLOC(num_blocks) \ |
@@ -205,7 +205,7 @@ TEST_CASE(mem_fragmentation) { | |||
205 | 205 | ||
206 | // Should be able to allocate 1 chunk of N blocks. | 206 | // Should be able to allocate 1 chunk of N blocks. |
207 | const void* chunk = mem_alloc(&mem, NUM_BLOCKS); | 207 | const void* chunk = mem_alloc(&mem, NUM_BLOCKS); |
208 | TEST_TRUE(chunk != 0); | 208 | TEST_TRUE(chunk != nullptr); |
209 | } | 209 | } |
210 | 210 | ||
211 | // Clear and re-use an allocator. | 211 | // Clear and re-use an allocator. |
@@ -216,15 +216,17 @@ TEST_CASE(mem_clear_then_reuse) { | |||
216 | // Allocate chunks, contents not important. | 216 | // Allocate chunks, contents not important. |
217 | for (int i = 0; i < NUM_BLOCKS; ++i) { | 217 | for (int i = 0; i < NUM_BLOCKS; ++i) { |
218 | int* chunk = mem_alloc(&mem, 1); | 218 | int* chunk = mem_alloc(&mem, 1); |
219 | TEST_TRUE(chunk != 0); | 219 | TEST_TRUE(chunk != nullptr); |
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) { |
226 | int* chunk = mem_alloc(&mem, 1); | 228 | int* chunk = mem_alloc(&mem, 1); |
227 | TEST_TRUE(chunk != 0); | 229 | TEST_TRUE(chunk != nullptr); |
228 | *chunk = i + 1; | 230 | *chunk = i + 1; |
229 | } | 231 | } |
230 | 232 | ||