diff options
author | 3gg <3gg@shellblade.net> | 2024-02-13 17:51:51 -0800 |
---|---|---|
committer | 3gg <3gg@shellblade.net> | 2024-02-13 17:51:51 -0800 |
commit | e153be0be2fb8df6656292daab3fa59963c76737 (patch) | |
tree | 7cca3fc134553017cb3c259db1dca33c98556109 /mempool/src/mempool.c | |
parent | 84bdfa4a23f5b8daa7921541b007518bc634be0f (diff) |
Let memory allocators trap by default when attempting to allocate beyond capacity.
Diffstat (limited to 'mempool/src/mempool.c')
-rw-r--r-- | mempool/src/mempool.c | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/mempool/src/mempool.c b/mempool/src/mempool.c index 1100dad..b09038b 100644 --- a/mempool/src/mempool.c +++ b/mempool/src/mempool.c | |||
@@ -1,5 +1,7 @@ | |||
1 | #include "mempool.h" | 1 | #include "mempool.h" |
2 | 2 | ||
3 | #include <cassert.h> | ||
4 | |||
3 | #include <stdlib.h> | 5 | #include <stdlib.h> |
4 | #include <string.h> | 6 | #include <string.h> |
5 | 7 | ||
@@ -24,6 +26,7 @@ bool mempool_make_( | |||
24 | pool->num_blocks = num_blocks; | 26 | pool->num_blocks = num_blocks; |
25 | pool->head = 0; | 27 | pool->head = 0; |
26 | pool->used = 0; | 28 | pool->used = 0; |
29 | pool->trap = true; | ||
27 | 30 | ||
28 | // Initialize blocks and block info. | 31 | // Initialize blocks and block info. |
29 | if (!block_info) { | 32 | if (!block_info) { |
@@ -74,6 +77,9 @@ void* mempool_alloc_(mempool* pool) { | |||
74 | 77 | ||
75 | BlockInfo* head = &pool->block_info[pool->head]; | 78 | BlockInfo* head = &pool->block_info[pool->head]; |
76 | if (head->used) { | 79 | if (head->used) { |
80 | if (pool->trap) { | ||
81 | FAIL("mempool allocation failed, increase the pool's capacity."); | ||
82 | } | ||
77 | return 0; // Pool is full. | 83 | return 0; // Pool is full. |
78 | } | 84 | } |
79 | 85 | ||
@@ -134,3 +140,8 @@ size_t mempool_capacity_(const mempool* pool) { | |||
134 | assert(pool); | 140 | assert(pool); |
135 | return pool->num_blocks * pool->block_size_bytes; | 141 | return pool->num_blocks * pool->block_size_bytes; |
136 | } | 142 | } |
143 | |||
144 | void mempool_enable_traps_(mempool* pool, bool enable) { | ||
145 | assert(pool); | ||
146 | pool->trap = enable; | ||
147 | } | ||