aboutsummaryrefslogtreecommitdiff
path: root/mempool/src
diff options
context:
space:
mode:
Diffstat (limited to 'mempool/src')
-rw-r--r--mempool/src/mempool.c35
1 files changed, 20 insertions, 15 deletions
diff --git a/mempool/src/mempool.c b/mempool/src/mempool.c
index 679f124..1100dad 100644
--- a/mempool/src/mempool.c
+++ b/mempool/src/mempool.c
@@ -3,18 +3,14 @@
3#include <stdlib.h> 3#include <stdlib.h>
4#include <string.h> 4#include <string.h>
5 5
6#define NO_BLOCK ((size_t)-1)
7
8static inline size_t min(size_t a, size_t b) { return a < b ? a : b; }
9
10/// Initialize the free list. 6/// Initialize the free list.
11/// All of the blocks in the pool are assumed free. 7/// All of the blocks in the pool are assumed free.
12static void init_free_list(mempool* pool) { 8static void init_free_list(mempool* pool) {
13 assert(pool); 9 assert(pool);
14 for (size_t i = 0; i < pool->num_blocks - 1; ++i) { 10 for (size_t i = 0; i < pool->num_blocks - 1; ++i) {
15 pool->block_info[i].next = i + 1; 11 pool->block_info[i].next_free = i + 1;
16 } 12 }
17 pool->block_info[pool->num_blocks - 1].next = NO_BLOCK; 13 pool->block_info[pool->num_blocks - 1].next_free = 0;
18} 14}
19 15
20bool mempool_make_( 16bool mempool_make_(
@@ -27,6 +23,7 @@ bool mempool_make_(
27 pool->block_size_bytes = block_size_bytes; 23 pool->block_size_bytes = block_size_bytes;
28 pool->num_blocks = num_blocks; 24 pool->num_blocks = num_blocks;
29 pool->head = 0; 25 pool->head = 0;
26 pool->used = 0;
30 27
31 // Initialize blocks and block info. 28 // Initialize blocks and block info.
32 if (!block_info) { 29 if (!block_info) {
@@ -66,6 +63,7 @@ void mempool_del_(mempool* pool) {
66void mempool_clear_(mempool* pool) { 63void mempool_clear_(mempool* pool) {
67 assert(pool); 64 assert(pool);
68 pool->head = 0; 65 pool->head = 0;
66 pool->used = 0;
69 memset(pool->blocks, 0, pool->num_blocks * pool->block_size_bytes); 67 memset(pool->blocks, 0, pool->num_blocks * pool->block_size_bytes);
70 memset(pool->block_info, 0, pool->num_blocks * sizeof(BlockInfo)); 68 memset(pool->block_info, 0, pool->num_blocks * sizeof(BlockInfo));
71 init_free_list(pool); 69 init_free_list(pool);
@@ -74,15 +72,18 @@ void mempool_clear_(mempool* pool) {
74void* mempool_alloc_(mempool* pool) { 72void* mempool_alloc_(mempool* pool) {
75 assert(pool); 73 assert(pool);
76 74
77 if (pool->head == NO_BLOCK) { 75 BlockInfo* head = &pool->block_info[pool->head];
78 return 0; 76 if (head->used) {
77 return 0; // Pool is full.
79 } 78 }
80 79
81 // Allocate the block. 80 // Allocate the block.
82 BlockInfo* head = &pool->block_info[pool->head]; 81 void* block = &pool->blocks[pool->head * pool->block_size_bytes];
83 void* block = &pool->blocks[pool->head * pool->block_size_bytes]; 82 head->used = true;
84 head->used = true; 83 head->next_used = pool->used;
85 pool->head = head->next; 84 pool->used = pool->head;
85 pool->head = head->next_free;
86 head->next_free = 0;
86 87
87 return block; 88 return block;
88} 89}
@@ -104,9 +105,13 @@ void mempool_free_(mempool* pool, void** block_ptr) {
104 memset(*block_ptr, 0, pool->block_size_bytes); 105 memset(*block_ptr, 0, pool->block_size_bytes);
105 106
106 // Free the block and add it to the head of the free list. 107 // Free the block and add it to the head of the free list.
107 info->used = false; 108 info->used = false;
108 info->next = pool->head; 109 info->next_used = 0;
109 pool->head = block_index; 110 info->next_free = pool->head;
111 pool->head = block_index;
112 if (pool->used == block_index) {
113 pool->used = 0;
114 }
110 115
111 *block_ptr = 0; 116 *block_ptr = 0;
112} 117}