diff options
Diffstat (limited to 'mempool/src')
| -rw-r--r-- | mempool/src/mempool.c | 19 |
1 files changed, 11 insertions, 8 deletions
diff --git a/mempool/src/mempool.c b/mempool/src/mempool.c index b7e8705..5772883 100644 --- a/mempool/src/mempool.c +++ b/mempool/src/mempool.c | |||
| @@ -4,18 +4,19 @@ | |||
| 4 | 4 | ||
| 5 | static inline size_t min(size_t a, size_t b) { return a < b ? a : b; } | 5 | static inline size_t min(size_t a, size_t b) { return a < b ? a : b; } |
| 6 | 6 | ||
| 7 | void mempool_make_(mempool* pool, BlockInfo* block_info, void* blocks, | 7 | void mempool_make_( |
| 8 | size_t num_blocks, size_t block_size_bytes) { | 8 | mempool* pool, BlockInfo* block_info, void* blocks, size_t num_blocks, |
| 9 | size_t block_size_bytes) { | ||
| 9 | assert(pool); | 10 | assert(pool); |
| 10 | assert(block_info); | 11 | assert(block_info); |
| 11 | assert(blocks); | 12 | assert(blocks); |
| 12 | assert(num_blocks >= 1); | 13 | assert(num_blocks >= 1); |
| 13 | pool->block_size_bytes = block_size_bytes; | 14 | pool->block_size_bytes = block_size_bytes; |
| 14 | pool->num_blocks = num_blocks; | 15 | pool->num_blocks = num_blocks; |
| 15 | pool->next_free_block = 0; | 16 | pool->next_free_block = 0; |
| 16 | pool->full = false; | 17 | pool->full = false; |
| 17 | pool->block_info = block_info; | 18 | pool->block_info = block_info; |
| 18 | pool->blocks = blocks; | 19 | pool->blocks = blocks; |
| 19 | memset(blocks, 0, num_blocks * block_size_bytes); | 20 | memset(blocks, 0, num_blocks * block_size_bytes); |
| 20 | } | 21 | } |
| 21 | 22 | ||
| @@ -47,6 +48,8 @@ void mempool_free_(mempool* pool, void** block_ptr) { | |||
| 47 | assert(pool); | 48 | assert(pool); |
| 48 | assert(block_ptr); | 49 | assert(block_ptr); |
| 49 | 50 | ||
| 51 | // Zero out the block so that we don't get stray values the next time it is | ||
| 52 | // allocated. | ||
| 50 | memset(*block_ptr, 0, pool->block_size_bytes); | 53 | memset(*block_ptr, 0, pool->block_size_bytes); |
| 51 | 54 | ||
| 52 | const size_t block_index = | 55 | const size_t block_index = |
| @@ -59,7 +62,7 @@ void mempool_free_(mempool* pool, void** block_ptr) { | |||
| 59 | pool->block_info[block_index].used = false; | 62 | pool->block_info[block_index].used = false; |
| 60 | if (pool->full) { | 63 | if (pool->full) { |
| 61 | pool->next_free_block = block_index; | 64 | pool->next_free_block = block_index; |
| 62 | pool->full = false; | 65 | pool->full = false; |
| 63 | } else { | 66 | } else { |
| 64 | // Prefer to allocate blocks towards the start of the pool. This way, blocks | 67 | // Prefer to allocate blocks towards the start of the pool. This way, blocks |
| 65 | // should cluster around this area and the pool should offer better memory | 68 | // should cluster around this area and the pool should offer better memory |
