diff options
Diffstat (limited to 'mem/include')
-rw-r--r-- | mem/include/mem.h | 37 |
1 files changed, 24 insertions, 13 deletions
diff --git a/mem/include/mem.h b/mem/include/mem.h index 892ea4f..224b069 100644 --- a/mem/include/mem.h +++ b/mem/include/mem.h | |||
@@ -86,9 +86,15 @@ | |||
86 | #define mem_get_chunk_handle(MEM, CHUNK_PTR) \ | 86 | #define mem_get_chunk_handle(MEM, CHUNK_PTR) \ |
87 | mem_get_chunk_handle_(&(MEM)->mem, CHUNK_PTR) | 87 | mem_get_chunk_handle_(&(MEM)->mem, CHUNK_PTR) |
88 | 88 | ||
89 | /// Return the total capacity of the allocator in bytes. | 89 | /// Return the block size in bytes. |
90 | #define mem_block_size_bytes(MEM) memp_block_size_bytes_(&(MEM)->pool) | ||
91 | |||
92 | /// Return the total capacity of the allocator. | ||
90 | #define mem_capacity(MEM) mem_capacity_(&(MEM)->mem) | 93 | #define mem_capacity(MEM) mem_capacity_(&(MEM)->mem) |
91 | 94 | ||
95 | /// Return the number of used blocks in the allocator. | ||
96 | #define mem_size(MEM) mem_size_(&(MEM)->mem) | ||
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 mem_enable_traps(MEM, enable) mem_enable_traps_(&(MEM)->mem, enable) | 99 | #define mem_enable_traps(MEM, enable) mem_enable_traps_(&(MEM)->mem, enable) |
94 | 100 | ||
@@ -97,18 +103,20 @@ | |||
97 | /// The caller can use 'i' as the index of the current chunk. | 103 | /// The caller can use 'i' as the index of the current chunk. |
98 | /// | 104 | /// |
99 | /// It is valid to mem_free() the chunk at each step of the iteration. | 105 | /// It is valid to mem_free() the chunk at each step of the iteration. |
100 | #define mem_foreach(MEM, ITER, BODY) \ | 106 | #define mem_foreach(MEM, ITER, BODY) \ |
101 | { \ | 107 | { \ |
102 | size_t i = 0; \ | 108 | size_t i = 0; \ |
103 | do { \ | 109 | if ((MEM)->mem.num_used_blocks > 0) { \ |
104 | if ((MEM)->mem.chunks[i].used) { \ | 110 | do { \ |
105 | __typeof__((MEM)->object[0])* ITER = \ | 111 | if ((MEM)->mem.chunks[i].used) { \ |
106 | &(((__typeof__((MEM)->object[0])*)(MEM)->mem.blocks))[i]; \ | 112 | __typeof__((MEM)->object[0])* ITER = \ |
107 | (void)ITER; \ | 113 | &(((__typeof__((MEM)->object[0])*)(MEM)->mem.blocks))[i]; \ |
108 | BODY; \ | 114 | (void)ITER; \ |
109 | } \ | 115 | BODY; \ |
110 | i = (MEM)->mem.chunks[i].next; \ | 116 | } \ |
111 | } while (i); \ | 117 | i = (MEM)->mem.chunks[i].next; \ |
118 | } while (i); \ | ||
119 | } \ | ||
112 | } | 120 | } |
113 | 121 | ||
114 | // ----------------------------------------------------------------------------- | 122 | // ----------------------------------------------------------------------------- |
@@ -137,6 +145,7 @@ typedef struct Chunk { | |||
137 | typedef struct Memory { | 145 | typedef struct Memory { |
138 | size_t block_size_bytes; | 146 | size_t block_size_bytes; |
139 | size_t num_blocks; | 147 | size_t num_blocks; |
148 | size_t num_used_blocks; | ||
140 | size_t next_free_chunk; | 149 | size_t next_free_chunk; |
141 | bool dynamic; /// True if blocks and chunks are dynamically-allocated. | 150 | bool dynamic; /// True if blocks and chunks are dynamically-allocated. |
142 | bool trap; /// Whether to trap when allocating beyond capacity. | 151 | bool trap; /// Whether to trap when allocating beyond capacity. |
@@ -164,5 +173,7 @@ void* mem_alloc_(Memory*, size_t num_blocks); | |||
164 | void mem_free_(Memory*, void** chunk_ptr); | 173 | void mem_free_(Memory*, void** chunk_ptr); |
165 | void* mem_get_chunk_(const Memory*, size_t chunk_handle); | 174 | void* mem_get_chunk_(const Memory*, size_t chunk_handle); |
166 | size_t mem_get_chunk_handle_(const Memory*, const void* chunk); | 175 | size_t mem_get_chunk_handle_(const Memory*, const void* chunk); |
176 | size_t mem_block_size_bytes_(const Memory*); | ||
167 | size_t mem_capacity_(const Memory*); | 177 | size_t mem_capacity_(const Memory*); |
178 | size_t mem_size_(const Memory*); | ||
168 | void mem_enable_traps_(Memory*, bool); | 179 | void mem_enable_traps_(Memory*, bool); |