MMgr v2.3.0
Zero-heap memory manager in C11. One borrowed buffer, carved by asserted offset.
Loading...
Searching...
No Matches
octetus_introitus_exitus.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 octetus_introitus_exitus.h
9 * @brief Byte verbs that append into a caller's span or read out of it.
10 * @author dstroy0 (Douglas Quigg) <dquigg123@gmail.com>
11 * @date 2026-08-29
12 *
13 * @note These act on a caller's span and hold nothing of their own. The span carries the cursor and
14 * the sticky flag. A caller may append a whole message and test once at the end.
15 * @note mmgr_byteio_put, mmgr_byteio_put_be and mmgr_byteio_raw return nothing. What a writer emits
16 * and how big its buffer is are both fixed before the build. So a write past the end means the
17 * program is wrong. Those three assert, store nothing, and latch overflow to retain a wrong
18 * program off the end. None of that leaves a caller anything to act on.
19 * @note mmgr_byteio_take_be and mmgr_byteio_rd_str answer EMBED_FALSE when the bytes are not there,
20 * set the read span's err, and leave the cursor and the output where they were. A reader
21 * takes whatever arrives. A short read is a runtime fact and not a build
22 * failure. The cursor does not advance on failure, because a caller that retains reading still
23 * needs it to mean something.
24 * @note mmgr_byteio_mpint_fixed is neither of those. It writes a whole field and does not append,
25 * and answers whether the integer fit, and so its own declaration carries the contract.
26 * @note Whole values move a word at a time. The big endian entries reverse once and then store or
27 * load at the widest step the count allows, and walk no bytes.
28 */
29#ifndef MMGR_OCTETUS_INTROITUS_EXITUS_H
30#define MMGR_OCTETUS_INTROITUS_EXITUS_H
31
32/** @addtogroup mod_byteio
33 * @{ */
34
35#include "spatium/spatium.h"
36
37#include "mmgr.h"
38
39EMBED_BEGIN_DECLS
40
41/**
42 * @brief Arguments for the byteio calls, where each call reads only the members it needs.
43 *
44 * @note put reads write_span and byte. put_be reads write_span, value and bytes. raw reads
45 * write_span, src and bytes. take_be reads read_span, bytes and out. rd_str reads read_span,
46 * blob and blob_bytes. mpint_fixed reads write_span, src and bytes, where bytes is the
47 * integer's length and not a big endian width.
48 */
49typedef struct
50{
51 mmgr_span *const write_span; /**< Span put, put_be and raw append into, or mpint_fixed fills [BORROWS]. */
52 mmgr_cspan *const read_span; /**< Span take_be and rd_str read from [BORROWS]. */
53 const uint8_t *const src; /**< Bytes raw appends, or the integer mpint_fixed reads [BORROWS]. */
54 uint64_t *const out; /**< Where take_be stores the value it read [BORROWS]. */
55 const uint8_t **const blob; /**< Where rd_str points at the payload it found [BORROWS]. */
56 size_t *const blob_bytes; /**< Where rd_str stores that payload's length [BORROWS]. */
57 const uint64_t value; /**< Value put_be writes, taken from its low bytes. */
58 const size_t bytes; /**< Bytes the call moves, 1 through 8 for the big endian entries. */
59 const uint8_t byte; /**< The single byte put appends. */
61
62/**
63 * @brief Type of the byteio dispatch table.
64 *
65 * @note EMBED_TABLE_LAYOUT asserts the six members sit at consecutive EMBED_FUNCTION_POINTER_BYTES offsets, with
66 * nothing else.
67 * @note put, put_be and raw answer nothing, because a span latches its own failure and leaves a
68 * caller nothing to act on.
69 * @note take_be and rd_str answer, because a read that did not happen has to be distinguishable
70 * from one that read a zero. mpint_fixed answers whether the integer fit the field.
71 */
72typedef struct
73{
74 void (*put)(const OctetusCfg *args); /**< Appends one byte. */
75 void (*put_be)(const OctetusCfg *args); /**< Appends bytes of a value, most significant first. */
76 void (*raw)(const OctetusCfg *args); /**< Appends a run of bytes as they are. */
77 embed_bool (*take_be)(const OctetusCfg *args); /**< Reads a big endian value and advances past it. */
78 embed_bool (*rd_str)(const OctetusCfg *args); /**< Reads a length-prefixed run and points at it. */
79 embed_bool (*mpint_fixed)(const OctetusCfg *args); /**< Right-aligns an integer into a fixed field. */
81EMBED_TABLE_LAYOUT(OctetusIntroitusExitusNs, put, put_be, raw, take_be, rd_str, mpint_fixed);
82
83/**
84 * @brief Appends args->byte to args->write_span.
85 *
86 * @param[in,out] args Span and the byte to append [BORROWS].
87 * @warning An append past the span's cap is a build failure. It asserts, stores nothing and latches
88 * overflow. pos counts the byte either way.
89 */
90EMBED_HOST_DEVICE void mmgr_byteio_put(const OctetusCfg *args);
91
92/**
93 * @brief Appends the low args->bytes of args->value to args->write_span, most significant byte first.
94 *
95 * @param[in,out] args Span, value and byte count [BORROWS].
96 * @note The value reverses once and then goes out at the widest step the count allows. A count of
97 * eight is one store and a count of seven is three.
98 * @warning An append past the span's cap is a build failure. It asserts, stores nothing and latches
99 * overflow. pos advances either way.
100 * @warning args->bytes must be 1 through 8.
101 */
102EMBED_HOST_DEVICE void mmgr_byteio_put_be(const OctetusCfg *args);
103
104/**
105 * @brief Appends args->bytes from args->src to args->write_span as they are.
106 *
107 * @param[in,out] args Span, source and byte count [BORROWS].
108 * @warning An append past the span's cap is a build failure. It asserts, stores nothing and latches
109 * overflow. pos advances either way.
110 * @warning args->src must be readable for args->bytes, and must not overlap the span's buffer.
111 */
112EMBED_HOST_DEVICE void mmgr_byteio_raw(const OctetusCfg *args);
113
114/**
115 * @brief Reads a big endian value of args->bytes at args->read_span's cursor and advances past it.
116 *
117 * @param[in,out] args Span, byte count and where to store the value [BORROWS].
118 * @return EMBED_TRUE when the bytes were there, EMBED_FALSE when the span was short.
119 * @note Reads at the cursor and nowhere else. A codec that leads with a tag advances past it itself.
120 * @note A read that reaches past the end sets the span's err and leaves the cursor and args->out untouched.
121 * @warning args->bytes must be 1 through 8.
122 */
123EMBED_HOST_DEVICE embed_bool mmgr_byteio_take_be(const OctetusCfg *args);
124
125/**
126 * @brief Reads a big endian 32-bit length at args->read_span's cursor, then points args->blob at the
127 * run behind it.
128 *
129 * @param[in,out] args Span, and where to report the run and its length [BORROWS].
130 * @return EMBED_TRUE when the length and its run both position within the span.
131 * @note No copy. args->blob points into the span's own bytes, and so it lives only as long as
132 * they do [BORROWS].
133 * @note The cursor advances past the length and the run together. A caller reading a sequence of
134 * these needs to track nothing between them.
135 * @note A run that reaches past the end leaves the cursor where it started, sets the span's err, and
136 * writes nothing through args->blob or args->blob_bytes. The length alone read does
137 * not move the cursor either, since a partial read is not a read.
138 */
139EMBED_HOST_DEVICE embed_bool mmgr_byteio_rd_str(const OctetusCfg *args);
140
141/**
142 * @brief Right-aligns the big endian integer at args->src into args->write_span's whole buffer, with a
143 * zero fill ahead of it.
144 *
145 * @param[in,out] args The integer with its length in args->src and args->bytes, and the field as
146 * args->write_span [BORROWS].
147 * @return EMBED_TRUE when the integer fits the field, EMBED_FALSE when it does not.
148 * @note The width test does not count leading zero bytes of the integer. A value with
149 * a sign byte still fits a field of its own size.
150 * @note The field fills whole with no append. On success args->write_span's cursor ends
151 * at its cap.
152 * @note Nothing goes into the field when it returns EMBED_FALSE, but args->write_span's overflow
153 * stays set. A span tested later reports the failure too.
154 * @warning args->src must be readable for args->bytes, and must not overlap args->write_span's buffer.
155 */
156EMBED_HOST_DEVICE embed_bool mmgr_byteio_mpint_fixed(const OctetusCfg *args);
157
158/**
159 * @brief Dispatch table instance named byteio, with each member set to its mmgr_byteio_ function.
160 */
161EMBED_TABLE_STORAGE OctetusIntroitusExitusNs byteio EMBED_UNUSED = {
162 .put = mmgr_byteio_put,
163 .put_be = mmgr_byteio_put_be,
164 .raw = mmgr_byteio_raw,
165 .take_be = mmgr_byteio_take_be,
166 .rd_str = mmgr_byteio_rd_str,
167 .mpint_fixed = mmgr_byteio_mpint_fixed,
168};
169
170EMBED_END_DECLS
171
172/** @} */
173
174#endif
EMBED_HOST_DEVICE void mmgr_byteio_raw(const OctetusCfg *args)
Appends args->bytes from args->src to args->write_span as they are.
EMBED_HOST_DEVICE embed_bool mmgr_byteio_mpint_fixed(const OctetusCfg *args)
Right-aligns the big endian integer at args->src into args->write_span's whole buffer,...
static const OctetusIntroitusExitusNs byteio
Dispatch table instance named byteio, with each member set to its mmgr_byteio_ function.
EMBED_HOST_DEVICE void mmgr_byteio_put_be(const OctetusCfg *args)
Appends the low args->bytes of args->value to args->write_span, most significant byte first.
EMBED_HOST_DEVICE embed_bool mmgr_byteio_rd_str(const OctetusCfg *args)
Reads a big endian 32-bit length at args->read_span's cursor, then points args->blob at the run behin...
EMBED_HOST_DEVICE void mmgr_byteio_put(const OctetusCfg *args)
Appends args->byte to args->write_span.
EMBED_HOST_DEVICE embed_bool mmgr_byteio_take_be(const OctetusCfg *args)
Reads a big endian value of args->bytes at args->read_span's cursor and advances past it.
The embedded_types headers every module builds on.
Spans over a caller's buffer, the two span types, and the spat dispatch table.
Arguments for the byteio calls, where each call reads only the members it needs.
mmgr_cspan *const read_span
const uint8_t **const blob
uint64_t *const out
const uint8_t *const src
mmgr_span *const write_span
const uint64_t value
size_t *const blob_bytes
Type of the byteio dispatch table.
embed_bool(*) take_be(const OctetusCfg *args)
void(*) put(const OctetusCfg *args)
void(*) put_be(const OctetusCfg *args)
embed_bool(*) mpint_fixed(const OctetusCfg *args)
embed_bool(*) rd_str(const OctetusCfg *args)
void(*) raw(const OctetusCfg *args)
A buffer under read: its bytes, how far the reader went, and whether it ran out.
Definition spatium.h:67
A buffer under fill: its bytes, how far the writer went, and whether it ran out.
Definition spatium.h:51