diff options
| -rw-r--r-- | mempool/src/mempool.c | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/mempool/src/mempool.c b/mempool/src/mempool.c index 5772883..b4693a5 100644 --- a/mempool/src/mempool.c +++ b/mempool/src/mempool.c | |||
| @@ -18,6 +18,7 @@ void mempool_make_( | |||
| 18 | pool->block_info = block_info; | 18 | pool->block_info = block_info; |
| 19 | pool->blocks = blocks; | 19 | pool->blocks = blocks; |
| 20 | memset(blocks, 0, num_blocks * block_size_bytes); | 20 | memset(blocks, 0, num_blocks * block_size_bytes); |
| 21 | memset(block_info, 0, num_blocks * sizeof(BlockInfo)); | ||
| 21 | } | 22 | } |
| 22 | 23 | ||
| 23 | void* mempool_alloc_(mempool* pool) { | 24 | void* mempool_alloc_(mempool* pool) { |
| @@ -31,9 +32,14 @@ void* mempool_alloc_(mempool* pool) { | |||
| 31 | void* block = &pool->blocks[pool->next_free_block * pool->block_size_bytes]; | 32 | void* block = &pool->blocks[pool->next_free_block * pool->block_size_bytes]; |
| 32 | pool->block_info[pool->next_free_block].used = true; | 33 | pool->block_info[pool->next_free_block].used = true; |
| 33 | 34 | ||
| 34 | // Search for the next free block. If it does not exist, flag the pool full. | 35 | // Search for the next free block. If it does not exist, flag the pool |
| 36 | // full. | ||
| 37 | // | ||
| 38 | // The search starts near the current free block, where we might be more | ||
| 39 | // likely to find free blocks. The search starts with i=1 since we only | ||
| 40 | // need to test the other N-1 blocks in the pool. | ||
| 35 | pool->full = true; | 41 | pool->full = true; |
| 36 | for (size_t i = 1; i < pool->num_blocks && i != 0; i++) { | 42 | for (size_t i = 1; i < pool->num_blocks; i++) { |
| 37 | pool->next_free_block = (pool->next_free_block + 1) % pool->num_blocks; | 43 | pool->next_free_block = (pool->next_free_block + 1) % pool->num_blocks; |
| 38 | if (!pool->block_info[pool->next_free_block].used) { | 44 | if (!pool->block_info[pool->next_free_block].used) { |
| 39 | pool->full = false; | 45 | pool->full = false; |
