MMgr v2.3.0
Zero-heap memory manager in C11. One borrowed buffer, carved by asserted offset.
Loading...
Searching...
No Matches
spatium.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 spatium.h
9 * @brief Spans over a caller's buffer, the two span types, and the spat dispatch table.
10 * @author dstroy0 (Douglas Quigg) <dquigg123@gmail.com>
11 * @date 2026-08-29
12 *
13 * @note A span is a region and a cursor into it, with a sticky flag that latches once a walk runs
14 * past the end. It carries no storage of its own.
15 * @note The entries take an argument pack, as every other module's do. A span still travels by value
16 * inside that pack. SpatiumCfg holds one and does not point at one. A walk cannot change
17 * the span it received.
18 * @note There are two span types and not one. Writes go through mmgr_span and it latches overflow.
19 * Reads come from mmgr_cspan and it latches err. Their extents have the names cap and len, and
20 * so one in the place of the other is a compile error.
21 * @note The sticky flag is the reason to pass a span at all. A caller may walk one through several
22 * steps and test once at the end and not after each.
23 * @note overflow and err do not mean the same type of thing. A fill span overruns only when what a
24 * writer emits has the wrong size against its buffer. Both of those are before the
25 * build, and so overflow marks a wrong program. A read span runs out because whatever sent the
26 * bytes sent fewer, which is a runtime fact and nothing built wrong.
27 */
28#ifndef MMGR_SPATIUM_H
29#define MMGR_SPATIUM_H
30
31/** @addtogroup mod_spat
32 * @{ */
33
34#include "mmgr.h"
35
36EMBED_BEGIN_DECLS
37
38/**
39 * @brief A buffer under fill: its bytes, how far the writer went, and whether it ran out.
40 *
41 * @note Returned by value. This module retains no span of its own.
42 * @note overflow is sticky. Once a write runs past cap it stays set, and only mmgr_spat_reset
43 * clears it.
44 * @note To reach it at all is a build failure. So a read of it finds out that something is wrong
45 * and does not decide what to do next. It latches so that a wrong program stops its writes
46 * and does not walk off the end. It also latches so that one test after a run of appends
47 * catches it.
48 * @warning buf points at the caller's storage, which must outlive every use of the span [BORROWS].
49 */
50typedef struct
51{
52 uint8_t *buf; /**< The buffer the span covers [BORROWS]. */
53 size_t cap; /**< Bytes in buf. */
54 size_t pos; /**< Bytes written so far, which mmgr_spat_from sets to 0. */
55 embed_bool overflow; /**< Set once a write runs past cap, and only mmgr_spat_reset clears it. */
56} mmgr_span;
57
58/**
59 * @brief A buffer under read: its bytes, how far the reader went, and whether it ran out.
60 *
61 * @note The read-only counterpart of mmgr_span. buf is const here. A span passed out for a read
62 * takes no write through it.
63 * @note err is sticky, for the same reason overflow is.
64 * @warning buf points at storage that must outlive every use of the span [BORROWS].
65 */
66typedef struct
67{
68 const uint8_t *buf; /**< First byte, or NULL when there is nothing to read [BORROWS]. */
69 size_t len; /**< Readable bytes at buf. */
70 size_t pos; /**< Bytes read so far. */
71 embed_bool err; /**< Set once a read runs past len, and never cleared after. */
73
74/**
75 * @brief Arguments for every spat call, where each call reads only the members it needs.
76 *
77 * @note Members left unset are zero, and the calls that ignore them never read them.
78 * @note span and cspan hold a span and do not point at one. A walk reads the caller's span and
79 * cannot change it. reset is the single one entry that must change one, and it takes at.
80 * @note cap carries the extent for both constructors: bytes at buf for from, bytes at cbuf for cfrom.
81 * @note count carries the byte count for after, first and read.
82 */
83typedef struct
84{
85 const mmgr_span span; /**< Fill span the walks read. */
86 const mmgr_cspan cspan; /**< Read span cok tests. */
87 mmgr_span *const at; /**< Fill span reset rewinds [BORROWS]. */
88 uint8_t *const buf; /**< Buffer from builds over [BORROWS]. */
89 const uint8_t *const cbuf; /**< Buffer cfrom builds over [BORROWS]. */
90 const size_t cap; /**< Bytes at buf, or at cbuf. */
91 const size_t count; /**< Byte count after, first and read take. */
93
94/**
95 * @brief Type of the spat dispatch table.
96 *
97 * @note EMBED_TABLE_LAYOUT asserts the ten members sit at consecutive EMBED_FUNCTION_POINTER_BYTES offsets, with
98 * nothing else.
99 * @note There is no len or room entry. mmgr_span is the caller's own value, and so span.pos and
100 * span.cap - span.pos are already in hand. A call to fetch them would duplicate what
101 * a read of the member already gives.
102 */
103typedef struct
104{
105 mmgr_span (*from)(const SpatiumCfg *args); /**< Builds a fill span over a buffer. */
106 mmgr_cspan (*cfrom)(const SpatiumCfg *args); /**< Builds a read span over a buffer. */
107 embed_bool (*ok)(const SpatiumCfg *args); /**< Whether a fill span is still usable. */
108 embed_bool (*cok)(const SpatiumCfg *args); /**< Whether a read span is still usable. */
109 embed_bool (*has_storage)(const SpatiumCfg *args); /**< Whether the span covers any bytes at all. */
110 void (*reset)(const SpatiumCfg *args); /**< Returns pos to 0 and clears overflow. */
111 mmgr_span (*after)(const SpatiumCfg *args); /**< The span that begins count bytes in. */
112 mmgr_span (*first)(const SpatiumCfg *args); /**< The span that covers only the first count bytes. */
113 mmgr_cspan (*produced)(const SpatiumCfg *args); /**< A read span over everything written. */
114 mmgr_cspan (*read)(const SpatiumCfg *args); /**< A read span over the first count written. */
115} SpatiumNs;
116EMBED_TABLE_LAYOUT(SpatiumNs, from, cfrom, ok, cok, has_storage, reset, after, first, produced, read);
117
118/**
119 * @brief Builds a fill span over args->buf, with pos at 0 and overflow clear.
120 *
121 * @param[in] args Buffer buf and its extent cap [BORROWS].
122 * @return The span, by value.
123 * @note Asserts args->buf is not NULL and args->cap is not 0. The default MMGR_ASSERT leaves both
124 * unevaluated.
125 * @warning The span carries args->buf away, so that buffer must outlive every use of the span [BORROWS].
126 */
127EMBED_HOST_DEVICE mmgr_span mmgr_spat_from(const SpatiumCfg *args);
128
129/**
130 * @brief Builds a read span over args->cbuf, with pos at 0 and err clear.
131 *
132 * @param[in] args Buffer cbuf and its extent cap [BORROWS].
133 * @return The span, by value.
134 * @note cbuf and not buf. A buffer closed to writes cannot reach the fill constructor.
135 * @note Takes cbuf and cap as passed over, with none of the asserts mmgr_spat_from makes. A null cbuf
136 * or a zero cap still builds, and mmgr_spat_cok is what reports the span unusable.
137 * @warning The span carries args->cbuf away, so that buffer must outlive every use of the span [BORROWS].
138 */
139EMBED_HOST_DEVICE mmgr_cspan mmgr_spat_cfrom(const SpatiumCfg *args);
140
141/**
142 * @brief Returns whether args->span has storage and has not overflowed.
143 *
144 * @param[in] args Span to test, as args->span [BORROWS].
145 * @return EMBED_TRUE when the span is still usable.
146 */
147EMBED_HOST_DEVICE embed_bool mmgr_spat_ok(const SpatiumCfg *args);
148
149/**
150 * @brief Returns whether args->cspan has storage and recorded no error.
151 *
152 * @param[in] args Read span to test, as args->cspan [BORROWS].
153 * @return EMBED_TRUE when the span is still usable.
154 */
155EMBED_HOST_DEVICE embed_bool mmgr_spat_cok(const SpatiumCfg *args);
156
157/**
158 * @brief Returns whether args->span covers any bytes at all.
159 *
160 * @param[in] args Span to test, as args->span [BORROWS].
161 * @return EMBED_TRUE when buf is not NULL and cap is not 0.
162 */
163EMBED_HOST_DEVICE embed_bool mmgr_spat_has_storage(const SpatiumCfg *args);
164
165/**
166 * @brief Returns the span at args->at to its start and clears its overflow.
167 *
168 * @param[in,out] args Span to rewind, as args->at [BORROWS].
169 * @note The one entry that takes a span by pointer, because it is the single one that changes one.
170 * @note The one call that clears overflow, which is otherwise sticky for the span's whole life.
171 * @warning The write through args->at has no check of its own, and so it must point at a live span. The
172 * const on the pack covers the pack, not the span at the far end of at.
173 */
174EMBED_HOST_DEVICE void mmgr_spat_reset(const SpatiumCfg *args);
175
176/**
177 * @brief Returns the span that begins args->count bytes into args->span.
178 *
179 * @param[in] args Span to walk, as args->span, and the bytes to skip as args->count [BORROWS].
180 * @return A span over the rest, or a failed span when args->count is past cap.
181 * @note The span that comes back covers the same buffer args->span does [BORROWS]: a second view of
182 * those bytes and not a copy. A write through either shows in both.
183 * @note An args->count of exactly cap gives an empty span that has not failed: nothing remains, but
184 * nothing went wrong either.
185 */
186EMBED_HOST_DEVICE mmgr_span mmgr_spat_after(const SpatiumCfg *args);
187
188/**
189 * @brief Returns the span that covers only the first args->count bytes of args->span.
190 *
191 * @param[in] args Span to narrow, as args->span, and the bytes to retain as args->count [BORROWS].
192 * @return A span over those bytes, or a failed span when args->count is past cap.
193 * @note The span that comes back starts on the same byte args->span starts on [BORROWS], with a
194 * shorter cap over the same storage. A fill through one shows in a read through the other.
195 */
196EMBED_HOST_DEVICE mmgr_span mmgr_spat_first(const SpatiumCfg *args);
197
198/**
199 * @brief Returns a read span over everything written into args->span.
200 *
201 * @param[in] args Span to read back, as args->span [BORROWS].
202 * @return A read span over its first pos bytes, with span's overflow as its err.
203 * @note The same step as mmgr_spat_read against span's own pos. That case cannot ask for
204 * more than the bytes written and so cannot fail on its own account.
205 */
206EMBED_HOST_DEVICE mmgr_cspan mmgr_spat_produced(const SpatiumCfg *args);
207
208/**
209 * @brief Returns a read span over the first args->count bytes written into args->span.
210 *
211 * @param[in] args Span to read back, as args->span, and the bytes to cover as args->count [BORROWS].
212 * @return A read span over them, marked err when args->count is past the bytes written.
213 * @warning The read span points at the fill span's own bytes [BORROWS], and the const on it binds this
214 * view and not the storage. A second fill of args->span moves what the reader sees, and so read it back
215 * before the next fill or copy the bytes out.
216 */
217EMBED_HOST_DEVICE mmgr_cspan mmgr_spat_read(const SpatiumCfg *args);
218
219/**
220 * @brief Dispatch table instance named spat, with each member set to its mmgr_spat_ function.
221 */
222EMBED_TABLE_STORAGE SpatiumNs spat EMBED_UNUSED = {
223 .from = mmgr_spat_from,
224 .cfrom = mmgr_spat_cfrom,
225 .ok = mmgr_spat_ok,
226 .cok = mmgr_spat_cok,
227 .has_storage = mmgr_spat_has_storage,
228 .reset = mmgr_spat_reset,
229 .after = mmgr_spat_after,
230 .first = mmgr_spat_first,
231 .produced = mmgr_spat_produced,
232 .read = mmgr_spat_read,
233};
234
235EMBED_END_DECLS
236
237/** @} */
238
239#endif
EMBED_HOST_DEVICE embed_bool mmgr_spat_has_storage(const SpatiumCfg *args)
Returns whether args->span covers any bytes at all.
EMBED_HOST_DEVICE mmgr_span mmgr_spat_after(const SpatiumCfg *args)
Returns the span that begins args->count bytes into args->span.
EMBED_HOST_DEVICE embed_bool mmgr_spat_ok(const SpatiumCfg *args)
Returns whether args->span has storage and has not overflowed.
static const SpatiumNs spat
Dispatch table instance named spat, with each member set to its mmgr_spat_ function.
Definition spatium.h:222
EMBED_HOST_DEVICE embed_bool mmgr_spat_cok(const SpatiumCfg *args)
Returns whether args->cspan has storage and recorded no error.
EMBED_HOST_DEVICE mmgr_cspan mmgr_spat_cfrom(const SpatiumCfg *args)
Builds a read span over args->cbuf, with pos at 0 and err clear.
EMBED_HOST_DEVICE void mmgr_spat_reset(const SpatiumCfg *args)
Returns the span at args->at to its start and clears its overflow.
EMBED_HOST_DEVICE mmgr_cspan mmgr_spat_read(const SpatiumCfg *args)
Returns a read span over the first args->count bytes written into args->span.
EMBED_HOST_DEVICE mmgr_cspan mmgr_spat_produced(const SpatiumCfg *args)
Returns a read span over everything written into args->span.
EMBED_HOST_DEVICE mmgr_span mmgr_spat_from(const SpatiumCfg *args)
Builds a fill span over args->buf, with pos at 0 and overflow clear.
EMBED_HOST_DEVICE mmgr_span mmgr_spat_first(const SpatiumCfg *args)
Returns the span that covers only the first args->count bytes of args->span.
The embedded_types headers every module builds on.
Arguments for every spat call, where each call reads only the members it needs.
Definition spatium.h:84
const uint8_t *const cbuf
Definition spatium.h:89
const size_t cap
Definition spatium.h:90
const mmgr_cspan cspan
Definition spatium.h:86
mmgr_span *const at
Definition spatium.h:87
uint8_t *const buf
Definition spatium.h:88
const mmgr_span span
Definition spatium.h:85
const size_t count
Definition spatium.h:91
Type of the spat dispatch table.
Definition spatium.h:104
mmgr_cspan(*) read(const SpatiumCfg *args)
Definition spatium.h:114
mmgr_span(*) from(const SpatiumCfg *args)
Definition spatium.h:105
mmgr_span(*) first(const SpatiumCfg *args)
Definition spatium.h:112
mmgr_cspan(*) cfrom(const SpatiumCfg *args)
Definition spatium.h:106
embed_bool(*) has_storage(const SpatiumCfg *args)
Definition spatium.h:109
embed_bool(*) ok(const SpatiumCfg *args)
Definition spatium.h:107
mmgr_cspan(*) produced(const SpatiumCfg *args)
Definition spatium.h:113
void(*) reset(const SpatiumCfg *args)
Definition spatium.h:110
mmgr_span(*) after(const SpatiumCfg *args)
Definition spatium.h:111
embed_bool(*) cok(const SpatiumCfg *args)
Definition spatium.h:108
A buffer under read: its bytes, how far the reader went, and whether it ran out.
Definition spatium.h:67
embed_bool err
Definition spatium.h:71
size_t len
Definition spatium.h:69
size_t pos
Definition spatium.h:70
const uint8_t * buf
Definition spatium.h:68
A buffer under fill: its bytes, how far the writer went, and whether it ran out.
Definition spatium.h:51
size_t pos
Definition spatium.h:54
uint8_t * buf
Definition spatium.h:52
size_t cap
Definition spatium.h:53
embed_bool overflow
Definition spatium.h:55