aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--mem/include/mem.h37
-rw-r--r--mem/src/mem.c16
-rw-r--r--mem/test/mem_test.c8
-rw-r--r--mempool/include/mempool.h45
-rw-r--r--mempool/src/mempool.c17
-rw-r--r--mempool/test/mempool_test.c8
6 files changed, 99 insertions, 32 deletions
diff --git a/mem/include/mem.h b/mem/include/mem.h
index 892ea4f..224b069 100644
--- a/mem/include/mem.h
+++ b/mem/include/mem.h
@@ -86,9 +86,15 @@
86#define mem_get_chunk_handle(MEM, CHUNK_PTR) \ 86#define mem_get_chunk_handle(MEM, CHUNK_PTR) \
87 mem_get_chunk_handle_(&(MEM)->mem, CHUNK_PTR) 87 mem_get_chunk_handle_(&(MEM)->mem, CHUNK_PTR)
88 88
89/// Return the total capacity of the allocator in bytes. 89/// Return the block size in bytes.
90#define mem_block_size_bytes(MEM) memp_block_size_bytes_(&(MEM)->pool)
91
92/// Return the total capacity of the allocator.
90#define mem_capacity(MEM) mem_capacity_(&(MEM)->mem) 93#define mem_capacity(MEM) mem_capacity_(&(MEM)->mem)
91 94
95/// Return the number of used blocks in the allocator.
96#define mem_size(MEM) mem_size_(&(MEM)->mem)
97
92/// Set whether to trap when attempting to allocate beyond capacity. 98/// Set whether to trap when attempting to allocate beyond capacity.
93#define mem_enable_traps(MEM, enable) mem_enable_traps_(&(MEM)->mem, enable) 99#define mem_enable_traps(MEM, enable) mem_enable_traps_(&(MEM)->mem, enable)
94 100
@@ -97,18 +103,20 @@
97/// The caller can use 'i' as the index of the current chunk. 103/// The caller can use 'i' as the index of the current chunk.
98/// 104///
99/// It is valid to mem_free() the chunk at each step of the iteration. 105/// It is valid to mem_free() the chunk at each step of the iteration.
100#define mem_foreach(MEM, ITER, BODY) \ 106#define mem_foreach(MEM, ITER, BODY) \
101 { \ 107 { \
102 size_t i = 0; \ 108 size_t i = 0; \
103 do { \ 109 if ((MEM)->mem.num_used_blocks > 0) { \
104 if ((MEM)->mem.chunks[i].used) { \ 110 do { \
105 __typeof__((MEM)->object[0])* ITER = \ 111 if ((MEM)->mem.chunks[i].used) { \
106 &(((__typeof__((MEM)->object[0])*)(MEM)->mem.blocks))[i]; \ 112 __typeof__((MEM)->object[0])* ITER = \
107 (void)ITER; \ 113 &(((__typeof__((MEM)->object[0])*)(MEM)->mem.blocks))[i]; \
108 BODY; \ 114 (void)ITER; \
109 } \ 115 BODY; \
110 i = (MEM)->mem.chunks[i].next; \ 116 } \
111 } while (i); \ 117 i = (MEM)->mem.chunks[i].next; \
118 } while (i); \
119 } \
112 } 120 }
113 121
114// ----------------------------------------------------------------------------- 122// -----------------------------------------------------------------------------
@@ -137,6 +145,7 @@ typedef struct Chunk {
137typedef struct Memory { 145typedef struct Memory {
138 size_t block_size_bytes; 146 size_t block_size_bytes;
139 size_t num_blocks; 147 size_t num_blocks;
148 size_t num_used_blocks;
140 size_t next_free_chunk; 149 size_t next_free_chunk;
141 bool dynamic; /// True if blocks and chunks are dynamically-allocated. 150 bool dynamic; /// True if blocks and chunks are dynamically-allocated.
142 bool trap; /// Whether to trap when allocating beyond capacity. 151 bool trap; /// Whether to trap when allocating beyond capacity.
@@ -164,5 +173,7 @@ void* mem_alloc_(Memory*, size_t num_blocks);
164void mem_free_(Memory*, void** chunk_ptr); 173void mem_free_(Memory*, void** chunk_ptr);
165void* mem_get_chunk_(const Memory*, size_t chunk_handle); 174void* mem_get_chunk_(const Memory*, size_t chunk_handle);
166size_t mem_get_chunk_handle_(const Memory*, const void* chunk); 175size_t mem_get_chunk_handle_(const Memory*, const void* chunk);
176size_t mem_block_size_bytes_(const Memory*);
167size_t mem_capacity_(const Memory*); 177size_t mem_capacity_(const Memory*);
178size_t mem_size_(const Memory*);
168void mem_enable_traps_(Memory*, bool); 179void mem_enable_traps_(Memory*, bool);
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) {
diff --git a/mem/test/mem_test.c b/mem/test/mem_test.c
index d3c43b9..97db079 100644
--- a/mem/test/mem_test.c
+++ b/mem/test/mem_test.c
@@ -41,6 +41,8 @@ TEST_CASE(mem_fully_allocate) {
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 != 0);
43 } 43 }
44
45 TEST_TRUE(mem_size(&mem) == NUM_BLOCKS);
44} 46}
45 47
46// Allocate N chunks of 1 block each, then free them. 48// Allocate N chunks of 1 block each, then free them.
@@ -60,6 +62,7 @@ TEST_CASE(mem_fill_then_free) {
60 } 62 }
61 63
62 TEST_EQUAL(count(&mem), 0); 64 TEST_EQUAL(count(&mem), 0);
65 TEST_TRUE(mem_size(&mem) == 0);
63} 66}
64 67
65// Attempt to allocate blocks past the maximum allocator size. 68// Attempt to allocate blocks past the maximum allocator size.
@@ -78,6 +81,8 @@ TEST_CASE(mem_allocate_beyond_max_size) {
78 for (int i = 0; i < NUM_BLOCKS; ++i) { 81 for (int i = 0; i < NUM_BLOCKS; ++i) {
79 TEST_EQUAL(mem_alloc(&mem, 1), 0); 82 TEST_EQUAL(mem_alloc(&mem, 1), 0);
80 } 83 }
84
85 TEST_TRUE(mem_size(&mem) == NUM_BLOCKS);
81} 86}
82 87
83// Free blocks should always remain zeroed out. 88// Free blocks should always remain zeroed out.
@@ -114,6 +119,7 @@ TEST_CASE(mem_traverse_empty) {
114 mem_make(&mem); 119 mem_make(&mem);
115 120
116 TEST_EQUAL(count(&mem), 0); 121 TEST_EQUAL(count(&mem), 0);
122 TEST_TRUE(mem_size(&mem) == 0);
117} 123}
118 124
119// Traverse a partially full allocator. 125// Traverse a partially full allocator.
@@ -130,6 +136,7 @@ TEST_CASE(mem_traverse_partially_full) {
130 } 136 }
131 137
132 TEST_EQUAL(sum(&mem), (N) * (N + 1) / 2); 138 TEST_EQUAL(sum(&mem), (N) * (N + 1) / 2);
139 TEST_TRUE(mem_size(&mem) == N);
133} 140}
134 141
135// Traverse a full allocator. 142// Traverse a full allocator.
@@ -144,6 +151,7 @@ TEST_CASE(mem_traverse_full) {
144 } 151 }
145 152
146 TEST_EQUAL(sum(&mem), (NUM_BLOCKS) * (NUM_BLOCKS + 1) / 2); 153 TEST_EQUAL(sum(&mem), (NUM_BLOCKS) * (NUM_BLOCKS + 1) / 2);
154 TEST_TRUE(mem_size(&mem) == NUM_BLOCKS);
147} 155}
148 156
149// Get the ith (allocated) chunk. 157// Get the ith (allocated) chunk.
diff --git a/mempool/include/mempool.h b/mempool/include/mempool.h
index de9ea4f..245173b 100644
--- a/mempool/include/mempool.h
+++ b/mempool/include/mempool.h
@@ -86,9 +86,15 @@
86#define mempool_get_block_index(POOL, BLOCK_PTR) \ 86#define mempool_get_block_index(POOL, BLOCK_PTR) \
87 mempool_get_block_index_(&(POOL)->pool, BLOCK_PTR) 87 mempool_get_block_index_(&(POOL)->pool, BLOCK_PTR)
88 88
89/// Return the total capacity of the mempool in bytes. 89/// Return the block size in bytes.
90#define mempool_block_size_bytes(POOL) mempool_block_size_bytes_(&(POOL)->pool)
91
92/// Return the total capacity of the mempool.
90#define mempool_capacity(POOL) mempool_capacity_(&(POOL)->pool) 93#define mempool_capacity(POOL) mempool_capacity_(&(POOL)->pool)
91 94
95/// Return the number of used blocks in the mempool.
96#define mempool_size(POOL) mempool_size_(&(POOL)->pool)
97
92/// Set whether to trap when attempting to allocate beyond capacity. 98/// Set whether to trap when attempting to allocate beyond capacity.
93#define mempool_enable_traps(POOL, enable) \ 99#define mempool_enable_traps(POOL, enable) \
94 mempool_enable_traps_(&(POOL)->pool, enable) 100 mempool_enable_traps_(&(POOL)->pool, enable)
@@ -98,22 +104,24 @@
98/// The caller can use 'i' as the index of the current block. 104/// The caller can use 'i' as the index of the current block.
99/// 105///
100/// It is valid to mempool_free() the object at each step of the iteration. 106/// It is valid to mempool_free() the object at each step of the iteration.
101#define mempool_foreach(POOL, ITER, BODY) \ 107#define mempool_foreach(POOL, ITER, BODY) \
102 { \ 108 { \
103 size_t i = (POOL)->pool.used; \ 109 size_t i = (POOL)->pool.used; \
104 do { \ 110 if ((POOL)->pool.num_used_blocks > 0) { \
105 if ((POOL)->pool.block_info[i].used) { \ 111 do { \
106 __typeof__((POOL)->object[0])* ITER = \ 112 if ((POOL)->pool.block_info[i].used) { \
107 &(((__typeof__((POOL)->object[0])*)(POOL)->pool.blocks))[i]; \ 113 __typeof__((POOL)->object[0])* ITER = \
108 (void)ITER; \ 114 &(((__typeof__((POOL)->object[0])*)(POOL)->pool.blocks))[i]; \
109 BODY; \ 115 (void)ITER; \
110 } \ 116 BODY; \
111 const size_t next = (POOL)->pool.block_info[i].next_used; \ 117 } \
112 if (next == i) { \ 118 const size_t next = (POOL)->pool.block_info[i].next_used; \
113 break; \ 119 if (next == i) { \
114 } \ 120 break; \
115 i = next; \ 121 } \
116 } while (true); \ 122 i = next; \
123 } while (true); \
124 } \
117 } 125 }
118 126
119// ----------------------------------------------------------------------------- 127// -----------------------------------------------------------------------------
@@ -133,6 +141,7 @@ typedef struct BlockInfo {
133typedef struct mempool { 141typedef struct mempool {
134 size_t block_size_bytes; 142 size_t block_size_bytes;
135 size_t num_blocks; 143 size_t num_blocks;
144 size_t num_used_blocks;
136 size_t head; /// Points to the first block in the free list. 145 size_t head; /// Points to the first block in the free list.
137 size_t used; /// Points to the first block in the used list. 146 size_t used; /// Points to the first block in the used list.
138 bool dynamic; /// True if blocks and info are dynamically-allocated. 147 bool dynamic; /// True if blocks and info are dynamically-allocated.
@@ -161,5 +170,7 @@ void* mempool_alloc_(mempool*);
161void mempool_free_(mempool*, void** block_ptr); 170void mempool_free_(mempool*, void** block_ptr);
162void* mempool_get_block_(const mempool*, size_t block_index); 171void* mempool_get_block_(const mempool*, size_t block_index);
163size_t mempool_get_block_index_(const mempool*, const void* block); 172size_t mempool_get_block_index_(const mempool*, const void* block);
173size_t mempool_block_size_bytes_(const mempool*);
164size_t mempool_capacity_(const mempool*); 174size_t mempool_capacity_(const mempool*);
175size_t mempool_size_(const mempool*);
165void mempool_enable_traps_(mempool*, bool); 176void mempool_enable_traps_(mempool*, bool);
diff --git a/mempool/src/mempool.c b/mempool/src/mempool.c
index b09038b..46f1053 100644
--- a/mempool/src/mempool.c
+++ b/mempool/src/mempool.c
@@ -24,6 +24,7 @@ bool mempool_make_(
24 24
25 pool->block_size_bytes = block_size_bytes; 25 pool->block_size_bytes = block_size_bytes;
26 pool->num_blocks = num_blocks; 26 pool->num_blocks = num_blocks;
27 pool->num_used_blocks = 0;
27 pool->head = 0; 28 pool->head = 0;
28 pool->used = 0; 29 pool->used = 0;
29 pool->trap = true; 30 pool->trap = true;
@@ -91,6 +92,8 @@ void* mempool_alloc_(mempool* pool) {
91 pool->head = head->next_free; 92 pool->head = head->next_free;
92 head->next_free = 0; 93 head->next_free = 0;
93 94
95 pool->num_used_blocks++;
96
94 return block; 97 return block;
95} 98}
96 99
@@ -119,6 +122,8 @@ void mempool_free_(mempool* pool, void** block_ptr) {
119 pool->used = 0; 122 pool->used = 0;
120 } 123 }
121 124
125 pool->num_used_blocks--;
126
122 *block_ptr = 0; 127 *block_ptr = 0;
123} 128}
124 129
@@ -136,9 +141,19 @@ size_t mempool_get_block_index_(const mempool* pool, const void* block) {
136 return block_byte_index / pool->block_size_bytes; 141 return block_byte_index / pool->block_size_bytes;
137} 142}
138 143
144size_t mempool_block_size_bytes_(const mempool* pool) {
145 assert(pool);
146 return pool->block_size_bytes;
147}
148
139size_t mempool_capacity_(const mempool* pool) { 149size_t mempool_capacity_(const mempool* pool) {
140 assert(pool); 150 assert(pool);
141 return pool->num_blocks * pool->block_size_bytes; 151 return pool->num_blocks;
152}
153
154size_t mempool_size_(const mempool* pool) {
155 assert(pool);
156 return pool->num_used_blocks;
142} 157}
143 158
144void mempool_enable_traps_(mempool* pool, bool enable) { 159void mempool_enable_traps_(mempool* pool, bool enable) {
diff --git a/mempool/test/mempool_test.c b/mempool/test/mempool_test.c
index 6c48a2a..5eaee51 100644
--- a/mempool/test/mempool_test.c
+++ b/mempool/test/mempool_test.c
@@ -41,6 +41,8 @@ TEST_CASE(mempool_allocate_until_full) {
41 const int* block = mempool_alloc(&pool); 41 const int* block = mempool_alloc(&pool);
42 TEST_TRUE(block != 0); 42 TEST_TRUE(block != 0);
43 } 43 }
44
45 TEST_TRUE(mempool_size(&pool) == NUM_BLOCKS);
44} 46}
45 47
46// Allocate all N blocks, then free them. 48// Allocate all N blocks, then free them.
@@ -60,6 +62,7 @@ TEST_CASE(mempool_fill_then_free) {
60 } 62 }
61 63
62 TEST_EQUAL(count(&pool), 0); 64 TEST_EQUAL(count(&pool), 0);
65 TEST_TRUE(mempool_size(&pool) == 0);
63} 66}
64 67
65// Attempt to allocate blocks past the maximum pool size. 68// Attempt to allocate blocks past the maximum pool size.
@@ -78,6 +81,8 @@ TEST_CASE(mempool_allocate_beyond_max_size) {
78 for (int i = 0; i < NUM_BLOCKS; ++i) { 81 for (int i = 0; i < NUM_BLOCKS; ++i) {
79 TEST_EQUAL(mempool_alloc(&pool), 0); 82 TEST_EQUAL(mempool_alloc(&pool), 0);
80 } 83 }
84
85 TEST_TRUE(mempool_size(&pool) == NUM_BLOCKS);
81} 86}
82 87
83// Free blocks should always remain zeroed out. 88// Free blocks should always remain zeroed out.
@@ -114,6 +119,7 @@ TEST_CASE(mempool_traverse_empty) {
114 mempool_make(&pool); 119 mempool_make(&pool);
115 120
116 TEST_EQUAL(count(&pool), 0); 121 TEST_EQUAL(count(&pool), 0);
122 TEST_TRUE(mempool_size(&pool) == 0);
117} 123}
118 124
119// Traverse a partially full pool. 125// Traverse a partially full pool.
@@ -130,6 +136,7 @@ TEST_CASE(mempool_traverse_partially_full) {
130 } 136 }
131 137
132 TEST_EQUAL(sum(&pool), N * (N + 1) / 2); 138 TEST_EQUAL(sum(&pool), N * (N + 1) / 2);
139 TEST_TRUE(mempool_size(&pool) == N);
133} 140}
134 141
135// Traverse a full pool. 142// Traverse a full pool.
@@ -144,6 +151,7 @@ TEST_CASE(mempool_traverse_full) {
144 } 151 }
145 152
146 TEST_EQUAL(sum(&pool), NUM_BLOCKS * (NUM_BLOCKS + 1) / 2); 153 TEST_EQUAL(sum(&pool), NUM_BLOCKS * (NUM_BLOCKS + 1) / 2);
154 TEST_TRUE(mempool_size(&pool) == NUM_BLOCKS);
147} 155}
148 156
149// Get the ith (allocated) block. 157// Get the ith (allocated) block.