MMgr v2.3.0
Zero-heap memory manager in C11. One borrowed buffer, carved by asserted offset.
Loading...
Searching...
No Matches
mmgr.h
Go to the documentation of this file.
1/* MMgr - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2 * SPDX-License-Identifier: AGPL-3.0-or-later OR LicenseRef-Commercial OR LicenseRef-Educational
3 *
4 * Every use falls under AGPL-3.0-or-later unless you hold explicit permission, which is either a
5 * negotiated commercial licensing contract or an educator's license issued to you personally.
6 */
7/**
8 * @file mmgr.h
9 * @brief The embedded_types headers every module builds on.
10 * @author dstroy0 (Douglas Quigg) <dquigg123@gmail.com>
11 * @date 2026-08-30
12 *
13 * @note Every module header includes this one. The widths, the word, the static assertion, the
14 * attribute wrappers, the dispatch layout assertions and the entry point macros all come from
15 * embedded_types. Nothing here declares an MMgr copy of any of them.
16 * @note Only the two gated module headers below come in from here. A module includes what it
17 * needs itself, and an umbrella could not also be the header each module includes.
18 * @note mmgr_string_shim.h is separate. Its include changes what the <string.h> names mean, which
19 * is a decision a consumer makes for itself.
20 */
21#ifndef MMGR_H
22#define MMGR_H
23
24/**
25 * @brief The settings of a build that gives no command line flag. This is the hook of
26 * config/mmgr_config.h, for the module headers that reach this file first.
27 */
28#if defined(__has_include)
29#if __has_include("mmgr_settings.h")
30#include "mmgr_settings.h"
31#endif
32#endif
33
34#include "embed_compiler_directives.h"
35#include "embed_dispatch_layout.h"
36#include "embed_types.h"
37
38/**
39 * @brief The DMA module, reached only where MMGR_ENABLE_DMA holds.
40 *
41 * @note memoriam_praetereo.h guards its own contents on MMGR_ENABLE_DMA as well.
42 */
43#if MMGR_ENABLE_DMA
45#endif
46
47/**
48 * @brief The external memory module, reached only where MMGR_ENABLE_EXTRAM holds.
49 *
50 * @note memoria_externa.h guards its own contents on MMGR_ENABLE_EXTRAM as well.
51 */
52#if MMGR_ENABLE_EXTRAM
54#endif
55
56/**
57 * @brief Alignment a caller declares its own storage at before it passes those bytes to a module.
58 *
59 * @note Nothing under src reads it. The tests, the benches and the region-edges example are what
60 * write EMBED_ALIGN(MMGR_ALIGN_BYTES) on their arrays. The default is 16.
61 * @note Has to be a power of two, which the build checks below. It reaches an alignment specifier,
62 * and one given anything else is not legal C. The check is here to name the knob,
63 * where the diagnostic would otherwise point at whichever array carries it.
64 * @warning Not the alignment a locus_carcerum cell comes back at. That is MMGR_CARCER_ALIGN, which is
65 * sizeof(embed_word), and nothing asserts the two agree.
66 */
67#ifndef MMGR_ALIGN_BYTES
68
69#define MMGR_ALIGN_BYTES 16u
70#endif
71#if (MMGR_ALIGN_BYTES < 1) || ((MMGR_ALIGN_BYTES & (MMGR_ALIGN_BYTES - 1)) != 0)
72#error "MMGR_ALIGN_BYTES must be a power of two - an alignment specifier takes nothing else"
73#endif
74
75/**
76 * @brief Attribute that places a pool in external memory.
77 *
78 * @note Named here and filled by the port. Which section a part puts external memory in is the
79 * part's business, and so this library names the knob and supplies no value for it. A build
80 * defines it ahead of this header, or on the command line.
81 * @note Read only by ParsMemoriaeExternum, which exists only where MMGR_ENABLE_EXTRAM holds.
82 * @warning Empty by default, and an external pool then lands wherever the linker puts an ordinary
83 * one. Nothing diagnoses that. A build with external memory on has to supply this.
84 */
85#ifndef MMGR_EXTRAM_ATTR
86#define MMGR_EXTRAM_ATTR
87#endif
88
89/**
90 * @brief Marks a declaration so that a reference to it fails the build, with a message.
91 *
92 * @param[in] msg_ Text the diagnostic reports, as a string literal.
93 * @note Reported at the site that referenced the declaration, and so the message names what a caller did.
94 * The linker can only say what it found. This is the reason to reach for it over a token: the
95 * linker names a symbol, this names the mistake.
96 * @warning The diagnostic arrives only where a reference survives to the end of compilation, and so what
97 * this marks has to be something nothing legitimately calls.
98 * @warning Expands to nothing where EMBED_HAS_ATTRIBUTE(error) is 0. A reference then compiles and
99 * the misuse goes unreported.
100 */
101#if EMBED_HAS_ATTRIBUTE(error)
102#define MMGR_ERROR_ATTR(msg_) __attribute__((error(msg_)))
103#else
104#define MMGR_ERROR_ATTR(msg_)
105#endif
106
107/**
108 * @brief States which argument of an allocation entry gives the extent of what it returns.
109 *
110 * @param[in] arg_ One-based position of the byte count in the entry's parameter list.
111 * @note Feeds the compiler's object-size analysis, which can then report a write past the cell a
112 * caller received at the line that wrote it. Without it the returned pointer carries the
113 * extent of the pool it came out of, and a cell overrun reads as a legal access.
114 * @note Costs nothing. Measured byte-identical with and without, both for a direct call and for one
115 * through a dispatch table.
116 * @warning The bound survives only where the compiler does not fold the call away. An entry reached
117 * through its dispatch table retains it. The same entry inlined at a direct call collapses to an
118 * offset into the pool, and the cell-level extent goes.
119 * @warning Expands to nothing where EMBED_HAS_ATTRIBUTE(alloc_size) is 0, and arg_ plays no part. The
120 * diagnostics it would raise do not appear, and nothing reports that.
121 */
122#if EMBED_HAS_ATTRIBUTE(alloc_size)
123#define MMGR_ALLOC_SIZE(arg_) __attribute__((alloc_size(arg_)))
124#else
125#define MMGR_ALLOC_SIZE(arg_)
126#endif
127
128/**
129 * @brief Fails the link when a second translation unit declares a pool name.
130 *
131 * @param[in] name_ Pool under declaration.
132 * @note An initialized object with external linkage. So two units with one pool name give the
133 * linker two strong definitions of this symbol, and it refuses them by name. The enumerator
134 * guard cannot see across a unit boundary; this is what does.
135 * @note The pool's own array stays static, and so it is this token and not the bytes that collide. That
136 * retains the storage private to its unit while the name stays answerable across all of them.
137 * @warning Costs one object per pool, in .bss or .data as the toolchain decides. Fixed and small,
138 * but not nothing on a part where pools are the point.
139 */
140#define MMGR_PARS_TOKEN(name_) int mmgr_pars_token_##name_ = 0
141
142/**
143 * @brief Fails the build when a pool name appears in a second declaration.
144 *
145 * @param[in] name_ Pool under declaration.
146 * @note The enumerator is the guard. A second declaration of one name collides on it, and the
147 * compiler prints the identifier. So the identifier reads as the reason and names no
148 * mechanism.
149 * @note Two pools of one name would sit at unique addresses and the language would take them. The
150 * name is all a consumer gets. A name that means internal memory in one place and external in
151 * another leaves the placement out of reach of the line that uses it. Those two differ in what
152 * a DMA engine can address and in settle time.
153 * @warning One translation unit is as far as this guard reaches. The enumerator settles while a
154 * unit compiles. A pool of the same name declared in a separate unit does not collide
155 * on it. To catch that one is the linker's to do, over a symbol it can see.
156 */
157#define MMGR_PARS_DECLARED_ONCE(name_) \
158 enum \
159 { \
160 name_##_declared_twice_but_a_pool_symbol_names_one_region_only = 0 \
161 }
162
163/**
164 * @brief Fails the build when a second dresser claims a pool.
165 *
166 * @param[in] name_ Pool under claim.
167 * @note Emitted by whatever positions state over a pool: a cellblock, a ring, anything that writes its
168 * own records into those bytes. It sits here and in none of them, because the rule is the
169 * pool's and not a dresser's. A copy in each would let the next dresser come with none.
170 * @note Keyed on the pool alone, with no dresser's name in it. Every other symbol a site emits
171 * carries the site's name, and so two sites over one pool collide only there. Each would
172 * position its own records over the same bytes in the belief that it owned them.
173 * @note Costs nothing. An enumerator emits no storage and settles while the unit compiles.
174 * @warning Says one dresser claims a pool. It says nothing about a direct reach for the bytes,
175 * which is a separate question with separate guards.
176 */
177#define MMGR_PARS_CLAIMED_ONCE(name_) \
178 enum \
179 { \
180 name_##_claimed_twice_but_a_pool_is_dressed_once = 0 \
181 }
182
183/**
184 * @brief Declares a pool of bytes in internal memory.
185 *
186 * @param[in] name_ Name of the pool, which is also the array.
187 * @param[in] bytes_ Bytes in it.
188 * @note A pool is a block of bytes, alone. Hand it to LocusCarcerum for a cellblock, to
189 * mmgr_anular_init for a ring, or to a memor entry to work on it as the bytes it is. A pool
190 * nobody passes anywhere is a pool that exists with no claim on it.
191 * @note The count comes as name_##_bytes as well as the array. A consumer has the extent with no
192 * derivation. The assertion is what compares the two, and it has content only because the two
193 * come from separate places.
194 * @note Aligned to MMGR_ALIGN_BYTES, and that leaves that knob the contract the header states.
195 * No caller writes it out.
196 * @note EMBED_PASS_MEMORY gives each pass of nvcc its own bytes and its own name for them. A kernel takes from the
197 * pool in device memory, and host code from the one in host memory. The two pools are separate.
198 */
199#define ParsMemoriaeInternae(name_, bytes_) \
200 MMGR_PARS_DECLARED_ONCE(name_); \
201 MMGR_PARS_TOKEN(name_); \
202 enum \
203 { \
204 name_##_bytes = (bytes_) \
205 }; \
206 EMBED_ALIGN(MMGR_ALIGN_BYTES) static EMBED_PASS_MEMORY uint8_t mmgr_pars_storage_##name_[bytes_]; \
207 static EMBED_PASS_MEMORY const uint8_t *const name_ EMBED_UNUSED = mmgr_pars_storage_##name_; \
208 EMBED_STATIC_ASSERT(sizeof(mmgr_pars_storage_##name_) == name_##_bytes, #name_ " is not the size it was declared")
209
210/**
211 * @brief Declares a pool whose bytes the system gives at run time: its name, with no storage and no size.
212 *
213 * @param[in] name_ Name of the pool.
214 * @note The program asks the system for the bytes once, at the size it finds it needs, and passes them to the
215 * dresser. MemoriaAnularis over this pool gives a ring whose address the linker settles, whose state
216 * names the bytes wherever the system put them.
217 * @note For bytes that must come from the system. A graphics device that places a buffer over them
218 * opens the allocation they are, which an array in the program's image is not.
219 * @note A program declares a cellblock over this pool with MMGR_MINIMUM_SECURITY_HOSPES or
220 * MMGR_MAXIMUM_SECURITY_HOSPES, and places it once with mmgr_series_cellularum_placed before its first take.
221 * The cellblock refuses every take before that. The pages a take touched can then go back to the system through
222 * mmgr_series_cellularum_idle_each, which an array in the image does not allow.
223 */
224#define ParsMemoriaeHospes(name_) \
225 MMGR_PARS_DECLARED_ONCE(name_); \
226 MMGR_PARS_TOKEN(name_)
227
228/**
229 * @brief Declares a pool of bytes in external memory.
230 *
231 * @param[in] name_ Name of the pool, which is also the array.
232 * @param[in] bytes_ Bytes in it.
233 * @note The same declaration as ParsMemoriaeInternae, with MMGR_EXTRAM_ATTR. Which of the two a caller
234 * writes settles which memory a pool sits in, and nothing afterwards inspects an address to
235 * find out.
236 * @warning Exists only where MMGR_ENABLE_EXTRAM holds. A build without external memory fails
237 * on the name, where it would otherwise quietly place the bytes internally.
238 */
239#if MMGR_ENABLE_EXTRAM
240#define ParsMemoriaeExternum(name_, bytes_) \
241 MMGR_PARS_DECLARED_ONCE(name_); \
242 MMGR_PARS_TOKEN(name_); \
243 enum \
244 { \
245 name_##_bytes = (bytes_) \
246 }; \
247 EMBED_ALIGN(MMGR_ALIGN_BYTES) static EMBED_PASS_MEMORY uint8_t mmgr_pars_storage_##name_[bytes_] MMGR_EXTRAM_ATTR; \
248 static EMBED_PASS_MEMORY const uint8_t *const name_ EMBED_UNUSED = mmgr_pars_storage_##name_; \
249 EMBED_STATIC_ASSERT(sizeof(mmgr_pars_storage_##name_) == name_##_bytes, #name_ " is not the size it was declared")
250#endif
251
252/**
253 * @brief Runtime assertion hook, inert here.
254 *
255 * @param[in] cond_ Condition the caller expects to hold.
256 * @param[in] msg_ String literal that describes the expectation.
257 * @note An expectation the library asserts is one a correct caller cannot break, and so the form here
258 * pays nothing for it. It expands to a sizeof, which type checks cond_ and never evaluates it.
259 * @note A build that wants a failed expectation to report and stop defines its own ahead of this
260 * header, and the guard below is down. The file test/support/mmgr_host_traps.h does this
261 * for the checks environment. It is where the stdio and stdlib that a report needs live.
262 * @warning Not a runtime check, and nobody can read it as one. A failed expectation produces no
263 * diagnostic and no trap in this form.
264 * @warning cond_ must carry no side effect, since this form never evaluates it.
265 */
266#ifndef MMGR_ASSERT
267#define MMGR_ASSERT(cond_, msg_) ((void)sizeof((cond_) ? 1 : 0), (void)0)
268#endif
269
270/**
271 * @brief Stops the program on an illegal call, in every build.
272 *
273 * @param[in] msg_ String literal that names the broken rule.
274 * @note Separate from MMGR_ASSERT because the two answer different questions. MMGR_ASSERT states an
275 * expectation a correct caller cannot break, and it is inert in the build that ships. This one
276 * marks a call that is illegal to make, and so it has to hold in that build too.
277 * @note Reached where to go on would be worse than to stop. A release passed a prisoner from
278 * another cellblock is the case it exists for. The caller showed it does not know which
279 * cellblock owns that memory, and so its next release and its next allocation are both suspect.
280 * On a max security cellblock a quiet return would leave bytes unzeroed that the caller
281 * believes are clear.
282 * @note Does not return. Every call site rests on that basis and carries no fallback after it.
283 * @note The stop is unconditional and the diagnostic is not. The form here halts and names nothing,
284 * as retains this header free of libc.
285 * @warning A build may define its own before it includes this header, and the form below then is
286 * down. A target that reports a fault through its own handler wants that. So does a test
287 * harness, which would otherwise hang on this form and never report a failure.
288 * @warning This form spins. A target without abort has no other way to stop, and a halt is what a
289 * debugger can catch.
290 */
291#ifndef MMGR_FATAL
292/**
293 * @brief Expands to `__attribute__((noreturn))` where `EMBED_HAS_ATTRIBUTE(noreturn)` is not zero.
294 *
295 * @warning Expands to nothing where the attribute is absent. mmgr_fatal_halt still does not return, and
296 * the compiler then does not know it.
297 */
298#if EMBED_HAS_ATTRIBUTE(noreturn)
299#define MMGR_NORETURN_ATTR __attribute__((noreturn))
300#else
301#define MMGR_NORETURN_ATTR
302#endif
303
304/**
305 * @brief The halt of the default MMGR_FATAL: a spin that does not return.
306 *
307 * @note The spin is the whole body, and so no code follows it. A spin inside a do-while has the exit of
308 * the do-while after it, which no run reaches and which clang's coverage counts as a line.
309 */
310EMBED_HOST_DEVICE EMBED_INLINE MMGR_NORETURN_ATTR void mmgr_fatal_halt(void)
311{
312 for (;;)
313 {
314 }
315}
316
317// No libc here, and so the trap is a halt. The cast to void discards msg_, which nothing reads on this
318// arm and would otherwise be an unused argument
319#define MMGR_FATAL(msg_) ((void)(msg_), mmgr_fatal_halt())
320#endif
321
322#endif
Placement between internal and external memory, and a two-buffer index.
DMA channels: the completion event, the port hooks, the praet dispatch table, and the schedule and de...
static EMBED_HOST_DEVICE MMGR_NORETURN_ATTR void mmgr_fatal_halt(void)
The halt of the default MMGR_FATAL: a spin that does not return.
Definition mmgr.h:310
#define MMGR_NORETURN_ATTR
Stops the program on an illegal call, in every build.
Definition mmgr.h:301