aboutsummaryrefslogtreecommitdiff
path: root/mempool/include/mempool.h
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2024-02-13 17:51:51 -0800
committer3gg <3gg@shellblade.net>2024-02-13 17:51:51 -0800
commite153be0be2fb8df6656292daab3fa59963c76737 (patch)
tree7cca3fc134553017cb3c259db1dca33c98556109 /mempool/include/mempool.h
parent84bdfa4a23f5b8daa7921541b007518bc634be0f (diff)
Let memory allocators trap by default when attempting to allocate beyond capacity.
Diffstat (limited to 'mempool/include/mempool.h')
-rw-r--r--mempool/include/mempool.h9
1 files changed, 9 insertions, 0 deletions
diff --git a/mempool/include/mempool.h b/mempool/include/mempool.h
index bd4d4dd..de9ea4f 100644
--- a/mempool/include/mempool.h
+++ b/mempool/include/mempool.h
@@ -65,6 +65,9 @@
65 65
66/// Allocate a new block. 66/// Allocate a new block.
67/// Return 0 if there is no memory left. 67/// Return 0 if there is no memory left.
68/// When there is no space left in the pool, allocation can either trap
69/// (default) or gracefully return 0. Call mem_enable_traps() to toggle this
70/// behaviour.
68/// New blocks are conveniently zeroed out. 71/// New blocks are conveniently zeroed out.
69#define mempool_alloc(POOL) mempool_alloc_(&(POOL)->pool) 72#define mempool_alloc(POOL) mempool_alloc_(&(POOL)->pool)
70 73
@@ -86,6 +89,10 @@
86/// Return the total capacity of the mempool in bytes. 89/// Return the total capacity of the mempool in bytes.
87#define mempool_capacity(POOL) mempool_capacity_(&(POOL)->pool) 90#define mempool_capacity(POOL) mempool_capacity_(&(POOL)->pool)
88 91
92/// Set whether to trap when attempting to allocate beyond capacity.
93#define mempool_enable_traps(POOL, enable) \
94 mempool_enable_traps_(&(POOL)->pool, enable)
95
89/// Iterate over the used blocks of the pool. 96/// Iterate over the used blocks of the pool.
90/// 97///
91/// The caller can use 'i' as the index of the current block. 98/// The caller can use 'i' as the index of the current block.
@@ -129,6 +136,7 @@ typedef struct mempool {
129 size_t head; /// Points to the first block in the free list. 136 size_t head; /// Points to the first block in the free list.
130 size_t used; /// Points to the first block in the used list. 137 size_t used; /// Points to the first block in the used list.
131 bool dynamic; /// True if blocks and info are dynamically-allocated. 138 bool dynamic; /// True if blocks and info are dynamically-allocated.
139 bool trap; /// Whether to trap when allocating beyond capacity.
132 BlockInfo* block_info; 140 BlockInfo* block_info;
133 uint8_t* blocks; 141 uint8_t* blocks;
134} mempool; 142} mempool;
@@ -154,3 +162,4 @@ void mempool_free_(mempool*, void** block_ptr);
154void* mempool_get_block_(const mempool*, size_t block_index); 162void* mempool_get_block_(const mempool*, size_t block_index);
155size_t mempool_get_block_index_(const mempool*, const void* block); 163size_t mempool_get_block_index_(const mempool*, const void* block);
156size_t mempool_capacity_(const mempool*); 164size_t mempool_capacity_(const mempool*);
165void mempool_enable_traps_(mempool*, bool);