aboutsummaryrefslogtreecommitdiff
path: root/mem/src/mem.c
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2024-03-09 08:36:02 -0800
committer3gg <3gg@shellblade.net>2024-03-09 08:36:02 -0800
commitafe1e1d12e42a0881aff63c766c14e48319b560c (patch)
treea17d08200e0046eb8929812fd2d9116a364ca04f /mem/src/mem.c
parent3b3c2c14a1eda5894d5db27bbaf4dd5f9a8c67db (diff)
Define functions to get the number of used blocks.
Diffstat (limited to 'mem/src/mem.c')
-rw-r--r--mem/src/mem.c16
1 files changed, 15 insertions, 1 deletions
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_(
14 14
15 mem->block_size_bytes = block_size_bytes; 15 mem->block_size_bytes = block_size_bytes;
16 mem->num_blocks = num_blocks; 16 mem->num_blocks = num_blocks;
17 mem->num_used_blocks = 0;
17 mem->next_free_chunk = 0; 18 mem->next_free_chunk = 0;
18 mem->trap = true; 19 mem->trap = true;
19 20
@@ -107,6 +108,7 @@ void* mem_alloc_(Memory* mem, size_t num_blocks) {
107 } while (chunk_idx != start); 108 } while (chunk_idx != start);
108 109
109 if (found) { 110 if (found) {
111 mem->num_used_blocks++;
110 mem->next_free_chunk = mem->chunks[chunk_idx].next; 112 mem->next_free_chunk = mem->chunks[chunk_idx].next;
111 return &mem->blocks[chunk_idx * mem->block_size_bytes]; 113 return &mem->blocks[chunk_idx * mem->block_size_bytes];
112 } else { 114 } else {
@@ -168,6 +170,8 @@ void mem_free_(Memory* mem, void** chunk_ptr) {
168 } 170 }
169 } 171 }
170 172
173 mem->num_used_blocks--;
174
171 *chunk_ptr = 0; 175 *chunk_ptr = 0;
172} 176}
173 177
@@ -189,9 +193,19 @@ size_t mem_get_chunk_handle_(const Memory* mem, const void* chunk) {
189 return block_byte_index / mem->block_size_bytes; 193 return block_byte_index / mem->block_size_bytes;
190} 194}
191 195
196size_t mem_block_size_bytes_(const Memory* mem) {
197 assert(mem);
198 return mem->block_size_bytes;
199}
200
192size_t mem_capacity_(const Memory* mem) { 201size_t mem_capacity_(const Memory* mem) {
193 assert(mem); 202 assert(mem);
194 return mem->num_blocks * mem->block_size_bytes; 203 return mem->num_blocks;
204}
205
206size_t mem_size_(const Memory* mem) {
207 assert(mem);
208 return mem->num_used_blocks;
195} 209}
196 210
197void mem_enable_traps_(Memory* mem, bool enable) { 211void mem_enable_traps_(Memory* mem, bool enable) {