diff options
Diffstat (limited to 'mempool/include/mempool.h')
-rw-r--r-- | mempool/include/mempool.h | 45 |
1 files changed, 28 insertions, 17 deletions
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 { | |||
133 | typedef struct mempool { | 141 | typedef 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*); | |||
161 | void mempool_free_(mempool*, void** block_ptr); | 170 | void mempool_free_(mempool*, void** block_ptr); |
162 | void* mempool_get_block_(const mempool*, size_t block_index); | 171 | void* mempool_get_block_(const mempool*, size_t block_index); |
163 | size_t mempool_get_block_index_(const mempool*, const void* block); | 172 | size_t mempool_get_block_index_(const mempool*, const void* block); |
173 | size_t mempool_block_size_bytes_(const mempool*); | ||
164 | size_t mempool_capacity_(const mempool*); | 174 | size_t mempool_capacity_(const mempool*); |
175 | size_t mempool_size_(const mempool*); | ||
165 | void mempool_enable_traps_(mempool*, bool); | 176 | void mempool_enable_traps_(mempool*, bool); |