From e153be0be2fb8df6656292daab3fa59963c76737 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Tue, 13 Feb 2024 17:51:51 -0800 Subject: Let memory allocators trap by default when attempting to allocate beyond capacity. --- mempool/src/mempool.c | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'mempool/src') 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 @@ #include "mempool.h" +#include + #include #include @@ -24,6 +26,7 @@ bool mempool_make_( pool->num_blocks = num_blocks; pool->head = 0; pool->used = 0; + pool->trap = true; // Initialize blocks and block info. if (!block_info) { @@ -74,6 +77,9 @@ void* mempool_alloc_(mempool* pool) { BlockInfo* head = &pool->block_info[pool->head]; if (head->used) { + if (pool->trap) { + FAIL("mempool allocation failed, increase the pool's capacity."); + } return 0; // Pool is full. } @@ -134,3 +140,8 @@ size_t mempool_capacity_(const mempool* pool) { assert(pool); return pool->num_blocks * pool->block_size_bytes; } + +void mempool_enable_traps_(mempool* pool, bool enable) { + assert(pool); + pool->trap = enable; +} -- cgit v1.2.3