aboutsummaryrefslogtreecommitdiff
path: root/mem/include/mem.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 /mem/include/mem.h
parent84bdfa4a23f5b8daa7921541b007518bc634be0f (diff)
Let memory allocators trap by default when attempting to allocate beyond capacity.
Diffstat (limited to 'mem/include/mem.h')
-rw-r--r--mem/include/mem.h11
1 files changed, 9 insertions, 2 deletions
diff --git a/mem/include/mem.h b/mem/include/mem.h
index bcff39f..892ea4f 100644
--- a/mem/include/mem.h
+++ b/mem/include/mem.h
@@ -66,8 +66,10 @@
66#define mem_clear(MEM) mem_clear_(&(MEM)->mem) 66#define mem_clear(MEM) mem_clear_(&(MEM)->mem)
67 67
68/// Allocate a new chunk of N blocks. 68/// Allocate a new chunk of N blocks.
69/// Return a pointer to the first block of the chunk, or 0 if there is no memory 69/// Return a pointer to the first block of the chunk.
70/// left. 70/// When there is no space left in the allocator, allocation can either trap
71/// (default) or gracefully return 0. Call mem_enable_traps() to toggle this
72/// behaviour.
71/// New chunks are conveniently zeroed out. 73/// New chunks are conveniently zeroed out.
72#define mem_alloc(MEM, num_blocks) mem_alloc_(&(MEM)->mem, num_blocks) 74#define mem_alloc(MEM, num_blocks) mem_alloc_(&(MEM)->mem, num_blocks)
73 75
@@ -87,6 +89,9 @@
87/// Return the total capacity of the allocator in bytes. 89/// Return the total capacity of the allocator in bytes.
88#define mem_capacity(MEM) mem_capacity_(&(MEM)->mem) 90#define mem_capacity(MEM) mem_capacity_(&(MEM)->mem)
89 91
92/// Set whether to trap when attempting to allocate beyond capacity.
93#define mem_enable_traps(MEM, enable) mem_enable_traps_(&(MEM)->mem, enable)
94
90/// Iterate over the used chunks of the allocator. 95/// Iterate over the used chunks of the allocator.
91/// 96///
92/// The caller can use 'i' as the index of the current chunk. 97/// The caller can use 'i' as the index of the current chunk.
@@ -134,6 +139,7 @@ typedef struct Memory {
134 size_t num_blocks; 139 size_t num_blocks;
135 size_t next_free_chunk; 140 size_t next_free_chunk;
136 bool dynamic; /// True if blocks and chunks are dynamically-allocated. 141 bool dynamic; /// True if blocks and chunks are dynamically-allocated.
142 bool trap; /// Whether to trap when allocating beyond capacity.
137 Chunk* chunks; /// Array of chunk information. 143 Chunk* chunks; /// Array of chunk information.
138 uint8_t* blocks; /// Array of blocks; 144 uint8_t* blocks; /// Array of blocks;
139} Memory; 145} Memory;
@@ -159,3 +165,4 @@ void mem_free_(Memory*, void** chunk_ptr);
159void* mem_get_chunk_(const Memory*, size_t chunk_handle); 165void* mem_get_chunk_(const Memory*, size_t chunk_handle);
160size_t mem_get_chunk_handle_(const Memory*, const void* chunk); 166size_t mem_get_chunk_handle_(const Memory*, const void* chunk);
161size_t mem_capacity_(const Memory*); 167size_t mem_capacity_(const Memory*);
168void mem_enable_traps_(Memory*, bool);