diff options
author | 3gg <3gg@shellblade.net> | 2023-07-11 18:37:31 -0700 |
---|---|---|
committer | 3gg <3gg@shellblade.net> | 2023-07-11 18:37:31 -0700 |
commit | 2a016de1c2eb45fc5f9c8cebf6b3c726b01ec340 (patch) | |
tree | 17ca4f39a2777b201146d4c5f65f300a7ffeb045 /mempool/src | |
parent | 08ec3a7a1fdb16cbb52b05f934bd001ca38bd991 (diff) |
Add support for dynamically allocated mempools.
Diffstat (limited to 'mempool/src')
-rw-r--r-- | mempool/src/mempool.c | 44 |
1 files changed, 37 insertions, 7 deletions
diff --git a/mempool/src/mempool.c b/mempool/src/mempool.c index b4693a5..059db93 100644 --- a/mempool/src/mempool.c +++ b/mempool/src/mempool.c | |||
@@ -1,24 +1,54 @@ | |||
1 | #include "mempool.h" | 1 | #include "mempool.h" |
2 | 2 | ||
3 | #include <stdlib.h> | ||
3 | #include <string.h> | 4 | #include <string.h> |
4 | 5 | ||
5 | static inline size_t min(size_t a, size_t b) { return a < b ? a : b; } | 6 | static inline size_t min(size_t a, size_t b) { return a < b ? a : b; } |
6 | 7 | ||
7 | void mempool_make_( | 8 | bool mempool_make_( |
8 | mempool* pool, BlockInfo* block_info, void* blocks, size_t num_blocks, | 9 | mempool* pool, BlockInfo* block_info, void* blocks, size_t num_blocks, |
9 | size_t block_size_bytes) { | 10 | size_t block_size_bytes) { |
10 | assert(pool); | 11 | assert(pool); |
11 | assert(block_info); | 12 | assert((block_info && blocks) || (!block_info && !blocks)); |
12 | assert(blocks); | ||
13 | assert(num_blocks >= 1); | 13 | assert(num_blocks >= 1); |
14 | pool->block_size_bytes = block_size_bytes; | 14 | pool->block_size_bytes = block_size_bytes; |
15 | pool->num_blocks = num_blocks; | 15 | pool->num_blocks = num_blocks; |
16 | pool->next_free_block = 0; | 16 | pool->next_free_block = 0; |
17 | pool->full = false; | 17 | pool->full = false; |
18 | pool->block_info = block_info; | 18 | if (!block_info) { |
19 | pool->blocks = blocks; | 19 | block_info = calloc(num_blocks, sizeof(BlockInfo)); |
20 | memset(blocks, 0, num_blocks * block_size_bytes); | 20 | blocks = calloc(num_blocks, block_size_bytes); |
21 | memset(block_info, 0, num_blocks * sizeof(BlockInfo)); | 21 | pool->dynamic = true; |
22 | } else { | ||
23 | memset(blocks, 0, num_blocks * block_size_bytes); | ||
24 | memset(block_info, 0, num_blocks * sizeof(BlockInfo)); | ||
25 | pool->dynamic = false; | ||
26 | } | ||
27 | pool->block_info = block_info; | ||
28 | pool->blocks = blocks; | ||
29 | return (block_info != 0) && (blocks != 0); | ||
30 | } | ||
31 | |||
32 | void mempool_del_(mempool* pool) { | ||
33 | assert(pool); | ||
34 | if (pool->dynamic) { | ||
35 | if (pool->block_info) { | ||
36 | free(pool->block_info); | ||
37 | pool->block_info = 0; | ||
38 | } | ||
39 | if (pool->blocks) { | ||
40 | free(pool->blocks); | ||
41 | pool->blocks = 0; | ||
42 | } | ||
43 | } | ||
44 | } | ||
45 | |||
46 | void mempool_clear_(mempool* pool) { | ||
47 | assert(pool); | ||
48 | pool->next_free_block = 0; | ||
49 | pool->full = false; | ||
50 | memset(pool->blocks, 0, pool->num_blocks * pool->block_size_bytes); | ||
51 | memset(pool->block_info, 0, pool->num_blocks * sizeof(BlockInfo)); | ||
22 | } | 52 | } |
23 | 53 | ||
24 | void* mempool_alloc_(mempool* pool) { | 54 | void* mempool_alloc_(mempool* pool) { |