MMgr v2.3.0
Zero-heap memory manager in C11. One borrowed buffer, carved by asserted offset.
Loading...
Searching...
No Matches
Verba scribo: the string builder

Text out, without printf and without a heap.

When to reach for it

Any time you would reach for snprintf and prefer not to link a formatter, or cannot afford one.

Worked example

size_t at = 0;
at = EMBED_CALL(verba_textus.put, VerbaTextusCfg, .out = buf, .cap = sizeof buf, .at = at, .text = "id=");
at = EMBED_CALL(verba_numerus.u32, VerbaNumerusCfg, .out = buf, .cap = sizeof buf, .at = at, .val = id);
at = EMBED_CALL(verba_textus.put, VerbaTextusCfg, .out = buf, .cap = sizeof buf, .at = at, .text = " rate=");
at = EMBED_CALL(verba_fractio.fixed, VerbaFractioCfg, .out = buf, .cap = sizeof buf, .at = at, .real = rate,
.decimals = 2u);
at = EMBED_CALL(verba_littera.ch, VerbaLitteraCfg, .out = buf, .cap = sizeof buf, .at = at, .ch = '\n');
const size_t len = EMBED_CALL(verba_finis.finish, VerbaFinisCfg, .out = buf, .cap = sizeof buf, .at = at);
if (len == 0u) {
/* the line did not fit */
}
Arguments for the entries that write the buffer itself, with no value to place in it.
char *const out
Arguments for the entries that write a double.
char *const out
Arguments for the entries that write one character.
char *const out
Arguments for the entries that write an integer.
char *const out
Arguments for the entries that write text into a buffer.
char *const out

The writers hold no state. Each one takes the position it should write at and returns the position after what it wrote. So the cursor is the caller's at, and nothing carries over between calls. A writer with no room returns cap, which every later writer also returns. An overflow propagates to finish without a flag and finish reports it as a length of zero.

There is no format string anywhere. Nothing parses d at runtime, and so nothing can disagree with the argument you passed.

The entries, by job

Each table takes its own argument struct.

job table and struct entries
raw text verba_textus, VerbaTextusCfg put, put_n, put_clip
escaping verba_textus, VerbaTextusCfg xml, json
one character verba_littera, VerbaLitteraCfg ch
numbers verba_numerus, VerbaNumerusCfg uint, u32, u32w, u64, u64_clip, hex, i64
floating point verba_fractio, VerbaFractioCfg g, fixed, sign_bit, is_inf, is_nan
finishing verba_finis, VerbaFinisCfg finish, ok

put_n takes the length with the text, in .text_len. A literal costs no scan: pass sizeof "literal" - 1u. put measures what it gets.

A line that carries its own cursor

verba_scribo/verba_linea.h retains the buffer, the cursor and an optional flush together in a VerbaLinea, and its mmgr_line_* entries take that line and the value as plain arguments. The header includes mmgr.h alone and defines no table, and therefore a C++ translation unit calls them as they are.

char out[128];
VerbaLinea line = {out, sizeof out, 0u, NULL, NULL, 0u};
mmgr_line_put(&line, "id=");
mmgr_line_decimal(&line, id, 1u);
mmgr_line_put(&line, " addr=");
mmgr_line_hex(&line, addr, 8u);
const size_t len = mmgr_line_finish(&line);
EMBED_HOST_DEVICE size_t mmgr_line_finish(VerbaLinea *line)
Stores the terminator, the one store a line no write reached yet still needs.
EMBED_HOST_DEVICE size_t mmgr_line_put(VerbaLinea *line, const char *text)
Writes a terminated text.
EMBED_HOST_DEVICE void mmgr_line_hex(VerbaLinea *line, uint64_t value, unsigned int digits_least)
Writes value in lower case base sixteen, padded on the left with zeros to digits_least digits.
EMBED_HOST_DEVICE void mmgr_line_decimal(VerbaLinea *line, uint64_t value, unsigned int digits_least)
Writes value in base ten, padded on the left with zeros to digits_least digits.
A line written into out, at bytes so far, and passed to flush as it fills.
Definition verba_linea.h:56

Every write leaves the buffer terminated, at the cost of one store on every call. A written line whose buffer holds no terminator shows corruption from something other than the line. A line with no flush behaves as snprintf does. A write that does not fit retains what fits, terminated in the last byte, and leaves the line full, and finish reports zero.

mmgr_line_put, mmgr_line_put_n and mmgr_line_put_max answer the bytes of the text the line took. A caller that sees fewer than the text holds goes on from text + taken. It uses the same buffer, with at set to zero once something reads those bytes. A line with a flush passes what it holds to the flush as it fills and goes on. So a text of any length passes through a buffer of any size. A flush answers 1 where it took the bytes and 0 where it refused, which leaves the line full.

mmgr_line_put reads its text a room at a time and never past the terminator, and so it needs no bound on the text's length.

job entries
raw text mmgr_line_put, mmgr_line_put_n, mmgr_line_put_max, mmgr_line_ch
columns mmgr_line_put_columns, mmgr_line_decimal_columns
numbers mmgr_line_decimal, mmgr_line_decimal_signed, mmgr_line_hex
making mmgr_line_over
finishing mmgr_line_flush, mmgr_line_finish, mmgr_line_ok

Gotchas

Overflow propagates, and so check once at the end. A writer with no room returns cap and every later writer returns cap too, and so the writes after an overflow are safe no-ops and finish reports zero. See How failure is reported.

xml and json escape, they do not quote. You supply the quotes around them.

put_clip truncates deliberately. It is for a field with a max width, and it is not the same as running out of room.

A float goes out by exact bit work, not by printf. It goes through Fractio: IEEE-754 fields.

Generated reference


Numeros scribo: the field formatter

A declarative layout. The format is data and not a string.

When to reach for it

A record with a fixed shape emitted many times: a log line, a telemetry frame, a fixed-width table. Describe the layout once as an array and fill values in.

Worked example

Two arrays. The spec says what the record looks like and holds no data. The values come on their own, and build takes them in order.

static const mmgr_field row[] = {
{MMGR_FK_LIT, 0, 3, "id="},
{MMGR_FK_LIT, 0, 5, " hex="},
{MMGR_FK_HEX, 0u, 0u, NULL},
};
const mmgr_fval vals[] = { MMGR_VU32(id), MMGR_VHEX(flags) };
EMBED_CALL(numer.build, NumerosCfg, .out = buf, .cap = sizeof buf,
.spec = row, .vals = vals, .nvals = 2u);
#define MMGR_END
Expands to an mmgr_field of type MMGR_FK_END, with width 0, bytes 0 and literal NULL.
#define MMGR_VU32(value_)
Expands to an mmgr_fval of type MMGR_FK_U32, with value_ in as.u32 and width 0.
#define MMGR_VHEX(value_)
Expands to an mmgr_fval of type MMGR_FK_HEX, with value_ in as.u64 and width 0.
#define MMGR_U32
Expands to an mmgr_field of type MMGR_FK_U32, with width 0, bytes 0 and literal NULL.
@ MMGR_FK_LIT
@ MMGR_FK_HEX
Arguments for the four numer calls.
char *const out
One field of a spec.
One value, tagged with the type that selects which arm of as holds it.

A literal is a MMGR_FK_LIT field that carries its own text and length, and so it costs no scan. Every other spec entry is a bare type that takes no argument. There is a one-word macro for some of them, MMGR_STR, MMGR_U32, MMGR_U64, MMGR_I64, MMGR_CH, MMGR_JSON, MMGR_XML, MMGR_END (src/numeros_scribo/numeros_scribo.h:60-85), and the rest use the brace form above. MMGR_FK_HEX, MMGR_FK_DEC, MMGR_FK_OCT, MMGR_FK_G and MMGR_FK_FIX have no such macro.

Four entries: build writes a record from a spec, emit writes values with no spec at all, and append and emit_append add to a record already in the buffer.

Why a spec array and not a format string

A program parses a format string at runtime, and nothing checks it against the arguments. Here the spec is a const array in flash, the type is an enum, and every value carries its own type tag. build compares the two and refuses the record if they disagree.

Gotchas

MMGR_END terminates the spec. Without it, the walk runs off the end of the array.

A type mismatch returns 0 and empties the buffer. So do too few values, or too many. The record is complete or absent; there is no partial record.

emit takes no spec. Each value carries its own type and width, and so it is the entry to use when the layout changes.

Generated reference


Memoriam praetereo: transfer submission

Note
The build compiles it only when MMGR_ENABLE_DMA is on, which defaults off. With it off the module is not in the library, and the build skips its suite with a message and does not pass it empty.

What it is

A thin, portable surface over a DMA controller: open a channel, submit a transfer, poll or take a callback, close it.

/* peripheral is a plain uint8_t the port assigns a meaning to; the library carries no
enum of peripherals, because which ones exist is the part's business and not its own. */
const PraetCfg ch = {
.channel = 0,
.peripheral = PORT_UART0,
};
if (EMBED_CALL(praet.open, PraetCfg, .channel = ch.channel, .peripheral = ch.peripheral))
{
EMBED_CALL(praet.tx_submit, PraetTransferCfg, .channel = 0, .buf = buf, .bytes = len);
EMBED_CALL(praet.poll, PraetCfg, .channel = 0);
EMBED_CALL(praet.close, PraetTransferCfg, .channel = 0);
}
Arguments for a channel open, and for a poll of one.
const uint8_t channel
const uint8_t peripheral
Arguments for a transfer submit, and for a channel close.
const uint8_t channel

There is no handle. The channel number identifies the transfer, and so nothing needs storage between open and close. open and tx_submit return false when the channel is busy or out of range.

A completion callback is optional: point PraetCfg::on_complete at a PraetCallbackCfg, and the module calls it with a mmgr_praet_event that describes what finished.

The hardware hooks are weak

The functions that actually touch a controller are EMBED_WEAK. A board support file overrides one with a definition of a symbol of the same name. There is no registration, no function pointer table, and no init order to get right.

Without an override they are present and refuse every request. So memoriam_praetereo compiles, and its tests link on a host with no DMA controller at all.

Gotchas

A submitted buffer must stay valid until the transfer completes [BORROWS]. The module does not copy it and cannot tell when the controller finishes: poll and the completion callback report. Everywhere else in MMgr a lifetime ends at a mark you control; here it ends when the hardware says so.

This module does not handle cache coherency. On a part with a data cache and a DMA engine that does not snoop it, the clean and invalidate are the board file's job.

The module reads the schedule's knobs only when the gate is on, and an unset one stops the build. PRAET_CHANNELS and the rest arrive through MMGR_PRAET_KNOBS. See Every compile-time knob.

praet_hw_progress is a fifth hook, with a weak default in praet_ordo.c that reports no movement. A port that leaves it in place still gets the watchdog. A running channel that nothing reports on gets the stalled mark once its keepalive window passes.

Generated reference