MMgr v2.3.0
Zero-heap memory manager in C11. One borrowed buffer, carved by asserted offset.
Loading...
Searching...
No Matches
mmgr.h File Reference

The embedded_types headers every module builds on. More...

#include "embed_compiler_directives.h"
#include "embed_dispatch_layout.h"
#include "embed_types.h"
#include "memoriam_praetereo/memoriam_praetereo.h"
#include "memoria_externa/memoria_externa.h"

Go to the source code of this file.

Macros

#define MMGR_ALIGN_BYTES
 The settings of a build that gives no command line flag. This is the hook of config/mmgr_config.h, for the module headers that reach this file first.
#define MMGR_ERROR_ATTR(msg_)
 Attribute that places a pool in external memory.
#define MMGR_ALLOC_SIZE(arg_)
 States which argument of an allocation entry gives the extent of what it returns.
#define MMGR_PARS_TOKEN(name_)
 Fails the link when a second translation unit declares a pool name.
#define MMGR_PARS_DECLARED_ONCE(name_)
 Fails the build when a pool name appears in a second declaration.
#define MMGR_PARS_CLAIMED_ONCE(name_)
 Fails the build when a second dresser claims a pool.
#define ParsMemoriaeInternae(name_, bytes_)
 Declares a pool of bytes in internal memory.
#define ParsMemoriaeHospes(name_)
 Declares a pool whose bytes the system gives at run time: its name, with no storage and no size.
#define ParsMemoriaeExternum(name_, bytes_)
 Declares a pool of bytes in external memory.
#define MMGR_ASSERT(cond_, msg_)
 Runtime assertion hook, inert here.
#define MMGR_NORETURN_ATTR
 Stops the program on an illegal call, in every build.
#define MMGR_FATAL(msg_)

Functions

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.

Detailed Description

The embedded_types headers every module builds on.

Author
dstroy0 (Douglas Quigg) dquig.nosp@m.g123.nosp@m.@gmai.nosp@m.l.co.nosp@m.m
Date
2026-08-30
Note
Every module header includes this one. The widths, the word, the static assertion, the attribute wrappers, the dispatch layout assertions and the entry point macros all come from embedded_types. Nothing here declares an MMgr copy of any of them.
Only the two gated module headers below come in from here. A module includes what it needs itself, and an umbrella could not also be the header each module includes.
mmgr_string_shim.h is separate. Its include changes what the <string.h> names mean, which is a decision a consumer makes for itself.

Definition in file mmgr.h.

Macro Definition Documentation

◆ MMGR_ALIGN_BYTES

#define MMGR_ALIGN_BYTES

The settings of a build that gives no command line flag. This is the hook of config/mmgr_config.h, for the module headers that reach this file first.

The DMA module, reached only where MMGR_ENABLE_DMA holds.

Note
memoriam_praetereo.h guards its own contents on MMGR_ENABLE_DMA as well.

The external memory module, reached only where MMGR_ENABLE_EXTRAM holds.

Note
memoria_externa.h guards its own contents on MMGR_ENABLE_EXTRAM as well.

Alignment a caller declares its own storage at before it passes those bytes to a module.

Note
Nothing under src reads it. The tests, the benches and the region-edges example are what write EMBED_ALIGN(MMGR_ALIGN_BYTES) on their arrays. The default is 16.
Has to be a power of two, which the build checks below. It reaches an alignment specifier, and one given anything else is not legal C. The check is here to name the knob, where the diagnostic would otherwise point at whichever array carries it.
Warning
Not the alignment a locus_carcerum cell comes back at. That is MMGR_CARCER_ALIGN, which is sizeof(embed_word), and nothing asserts the two agree.

Definition at line 69 of file mmgr.h.

◆ MMGR_ERROR_ATTR

#define MMGR_ERROR_ATTR ( msg_)

Attribute that places a pool in external memory.

Note
Named here and filled by the port. Which section a part puts external memory in is the part's business, and so this library names the knob and supplies no value for it. A build defines it ahead of this header, or on the command line.
Read only by ParsMemoriaeExternum, which exists only where MMGR_ENABLE_EXTRAM holds.
Warning
Empty by default, and an external pool then lands wherever the linker puts an ordinary one. Nothing diagnoses that. A build with external memory on has to supply this.

Marks a declaration so that a reference to it fails the build, with a message.

Parameters
[in]msg_Text the diagnostic reports, as a string literal.
Note
Reported at the site that referenced the declaration, and so the message names what a caller did. The linker can only say what it found. This is the reason to reach for it over a token: the linker names a symbol, this names the mistake.
Warning
The diagnostic arrives only where a reference survives to the end of compilation, and so what this marks has to be something nothing legitimately calls.
Expands to nothing where EMBED_HAS_ATTRIBUTE(error) is 0. A reference then compiles and the misuse goes unreported.

Definition at line 104 of file mmgr.h.

◆ MMGR_ALLOC_SIZE

#define MMGR_ALLOC_SIZE ( arg_)

States which argument of an allocation entry gives the extent of what it returns.

Parameters
[in]arg_One-based position of the byte count in the entry's parameter list.
Note
Feeds the compiler's object-size analysis, which can then report a write past the cell a caller received at the line that wrote it. Without it the returned pointer carries the extent of the pool it came out of, and a cell overrun reads as a legal access.
Costs nothing. Measured byte-identical with and without, both for a direct call and for one through a dispatch table.
Warning
The bound survives only where the compiler does not fold the call away. An entry reached through its dispatch table retains it. The same entry inlined at a direct call collapses to an offset into the pool, and the cell-level extent goes.
Expands to nothing where EMBED_HAS_ATTRIBUTE(alloc_size) is 0, and arg_ plays no part. The diagnostics it would raise do not appear, and nothing reports that.

Definition at line 125 of file mmgr.h.

Referenced by mmgr_buf_grow(), mmgr_persistent_buf_alloc(), mmgr_persistent_max_security_buf_release(), and mmgr_temporary_buf_alloc().

◆ MMGR_PARS_TOKEN

#define MMGR_PARS_TOKEN ( name_)

Fails the link when a second translation unit declares a pool name.

Parameters
[in]name_Pool under declaration.
Note
An initialized object with external linkage. So two units with one pool name give the linker two strong definitions of this symbol, and it refuses them by name. The enumerator guard cannot see across a unit boundary; this is what does.
The pool's own array stays static, and so it is this token and not the bytes that collide. That retains the storage private to its unit while the name stays answerable across all of them.
Warning
Costs one object per pool, in .bss or .data as the toolchain decides. Fixed and small, but not nothing on a part where pools are the point.

Definition at line 140 of file mmgr.h.

◆ MMGR_PARS_DECLARED_ONCE

#define MMGR_PARS_DECLARED_ONCE ( name_)

Fails the build when a pool name appears in a second declaration.

Parameters
[in]name_Pool under declaration.
Note
The enumerator is the guard. A second declaration of one name collides on it, and the compiler prints the identifier. So the identifier reads as the reason and names no mechanism.
Two pools of one name would sit at unique addresses and the language would take them. The name is all a consumer gets. A name that means internal memory in one place and external in another leaves the placement out of reach of the line that uses it. Those two differ in what a DMA engine can address and in settle time.
Warning
One translation unit is as far as this guard reaches. The enumerator settles while a unit compiles. A pool of the same name declared in a separate unit does not collide on it. To catch that one is the linker's to do, over a symbol it can see.

Definition at line 157 of file mmgr.h.

◆ MMGR_PARS_CLAIMED_ONCE

#define MMGR_PARS_CLAIMED_ONCE ( name_)

Fails the build when a second dresser claims a pool.

Parameters
[in]name_Pool under claim.
Note
Emitted by whatever positions state over a pool: a cellblock, a ring, anything that writes its own records into those bytes. It sits here and in none of them, because the rule is the pool's and not a dresser's. A copy in each would let the next dresser come with none.
Keyed on the pool alone, with no dresser's name in it. Every other symbol a site emits carries the site's name, and so two sites over one pool collide only there. Each would position its own records over the same bytes in the belief that it owned them.
Costs nothing. An enumerator emits no storage and settles while the unit compiles.
Warning
Says one dresser claims a pool. It says nothing about a direct reach for the bytes, which is a separate question with separate guards.

Definition at line 177 of file mmgr.h.

◆ ParsMemoriaeInternae

#define ParsMemoriaeInternae ( name_,
bytes_ )

Declares a pool of bytes in internal memory.

Parameters
[in]name_Name of the pool, which is also the array.
[in]bytes_Bytes in it.
Note
A pool is a block of bytes, alone. Hand it to LocusCarcerum for a cellblock, to mmgr_anular_init for a ring, or to a memor entry to work on it as the bytes it is. A pool nobody passes anywhere is a pool that exists with no claim on it.
The count comes as name_##_bytes as well as the array. A consumer has the extent with no derivation. The assertion is what compares the two, and it has content only because the two come from separate places.
Aligned to MMGR_ALIGN_BYTES, and that leaves that knob the contract the header states. No caller writes it out.
EMBED_PASS_MEMORY gives each pass of nvcc its own bytes and its own name for them. A kernel takes from the pool in device memory, and host code from the one in host memory. The two pools are separate.

Definition at line 199 of file mmgr.h.

◆ ParsMemoriaeHospes

#define ParsMemoriaeHospes ( name_)

Declares a pool whose bytes the system gives at run time: its name, with no storage and no size.

Parameters
[in]name_Name of the pool.
Note
The program asks the system for the bytes once, at the size it finds it needs, and passes them to the dresser. MemoriaAnularis over this pool gives a ring whose address the linker settles, whose state names the bytes wherever the system put them.
For bytes that must come from the system. A graphics device that places a buffer over them opens the allocation they are, which an array in the program's image is not.
A program declares a cellblock over this pool with MMGR_MINIMUM_SECURITY_HOSPES or MMGR_MAXIMUM_SECURITY_HOSPES, and places it once with mmgr_series_cellularum_placed before its first take. The cellblock refuses every take before that. The pages a take touched can then go back to the system through mmgr_series_cellularum_idle_each, which an array in the image does not allow.

Definition at line 224 of file mmgr.h.

◆ ParsMemoriaeExternum

#define ParsMemoriaeExternum ( name_,
bytes_ )

Declares a pool of bytes in external memory.

Parameters
[in]name_Name of the pool, which is also the array.
[in]bytes_Bytes in it.
Note
The same declaration as ParsMemoriaeInternae, with MMGR_EXTRAM_ATTR. Which of the two a caller writes settles which memory a pool sits in, and nothing afterwards inspects an address to find out.
Warning
Exists only where MMGR_ENABLE_EXTRAM holds. A build without external memory fails on the name, where it would otherwise quietly place the bytes internally.

Definition at line 240 of file mmgr.h.

◆ MMGR_ASSERT

#define MMGR_ASSERT ( cond_,
msg_ )

Runtime assertion hook, inert here.

Parameters
[in]cond_Condition the caller expects to hold.
[in]msg_String literal that describes the expectation.
Note
An expectation the library asserts is one a correct caller cannot break, and so the form here pays nothing for it. It expands to a sizeof, which type checks cond_ and never evaluates it.
A build that wants a failed expectation to report and stop defines its own ahead of this header, and the guard below is down. The file test/support/mmgr_host_traps.h does this for the checks environment. It is where the stdio and stdlib that a report needs live.
Warning
Not a runtime check, and nobody can read it as one. A failed expectation produces no diagnostic and no trap in this form.
cond_ must carry no side effect, since this form never evaluates it.

Definition at line 267 of file mmgr.h.

◆ MMGR_NORETURN_ATTR

#define MMGR_NORETURN_ATTR

Stops the program on an illegal call, in every build.

Parameters
[in]msg_String literal that names the broken rule.
Note
Separate from MMGR_ASSERT because the two answer different questions. MMGR_ASSERT states an expectation a correct caller cannot break, and it is inert in the build that ships. This one marks a call that is illegal to make, and so it has to hold in that build too.
Reached where to go on would be worse than to stop. A release passed a prisoner from another cellblock is the case it exists for. The caller showed it does not know which cellblock owns that memory, and so its next release and its next allocation are both suspect. On a max security cellblock a quiet return would leave bytes unzeroed that the caller believes are clear.
Does not return. Every call site rests on that basis and carries no fallback after it.
The stop is unconditional and the diagnostic is not. The form here halts and names nothing, as retains this header free of libc.
Warning
A build may define its own before it includes this header, and the form below then is down. A target that reports a fault through its own handler wants that. So does a test harness, which would otherwise hang on this form and never report a failure.
This form spins. A target without abort has no other way to stop, and a halt is what a debugger can catch.

Expands to __attribute__((noreturn)) where EMBED_HAS_ATTRIBUTE(noreturn) is not zero.

Warning
Expands to nothing where the attribute is absent. mmgr_fatal_halt still does not return, and the compiler then does not know it.

Definition at line 301 of file mmgr.h.

Referenced by mmgr_fatal_halt().

◆ MMGR_FATAL

#define MMGR_FATAL ( msg_)

Definition at line 319 of file mmgr.h.

Function Documentation

◆ mmgr_fatal_halt()

EMBED_HOST_DEVICE MMGR_NORETURN_ATTR void mmgr_fatal_halt ( void )
inlinestatic

The halt of the default MMGR_FATAL: a spin that does not return.

Note
The spin is the whole body, and so no code follows it. A spin inside a do-while has the exit of the do-while after it, which no run reaches and which clang's coverage counts as a line.

Definition at line 310 of file mmgr.h.

References mmgr_fatal_halt(), and MMGR_NORETURN_ATTR.

Referenced by mmgr_fatal_halt().