MMgr v2.3.0
Zero-heap memory manager in C11. One borrowed buffer, carved by asserted offset.
Loading...
Searching...
No Matches
praet_tabula_vexillorum.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 praet_tabula_vexillorum.h
9 * @brief Every bit of the channel flag word, and the assertions that retain the map from drift.
10 * @author dstroy0 (Douglas Quigg) <dquigg123@gmail.com>
11 * @date 2026-09-01
12 *
13 * @note The map is ours, and for that reason it needs a check. Nothing outside this library assigns
14 * these bits. A status added at the wrong offset or a region moved one byte down is a change
15 * no compiler would question. No test would necessarily reach it.
16 * @note A token id names each status and its bit derives from it. A name and its bit cannot
17 * disagree. What the assertions below check is the id set. It is dense, no two ids collide,
18 * and the three fields do not overlap each other.
19 * @note The map is the same on every build. A status a build never sets still owns its bit. A flag
20 * word read on one part has to mean what it means on another.
21 */
22#ifndef MMGR_PRAET_TABULA_VEXILLORUM_H
23#define MMGR_PRAET_TABULA_VEXILLORUM_H
24
25/** @addtogroup mod_praet
26 * @{ */
27
28#include "mmgr.h"
29
30EMBED_BEGIN_DECLS
31
32/**
33 * @brief Detached: the channel holds no engine and no vector.
34 */
35#define PRAET_SEPARATUS 0u
36
37/**
38 * @brief Attached: the channel holds the engine and will take a transfer.
39 */
40#define PRAET_ADNEXUS 1u
41
42/**
43 * @brief Busy: the engine moves bytes on this channel.
44 *
45 * @note The DMA busy, not the reader/setter's lock. The two are separate concerns and the lock lives
46 * in its own volatile.
47 */
48#define PRAET_BUSY 2u
49
50/**
51 * @brief Ok: the last transfer finished and the channel is still attached.
52 *
53 * @note The transfer's result, not the channel's health. A channel that failed carries PRAET_ERROR
54 * alongside, and so the two are separate bits.
55 */
56#define PRAET_OK 3u
57
58/**
59 * @brief Bits the quaternary core occupies, at the bottom of the word.
60 */
61#define PRAET_CORE_BITS 2u
62
63/**
64 * @brief The two bits the quaternary core occupies.
65 *
66 * @note The core sits at the bottom with every status above it. So a read of the state is a mask and
67 * a read of a status is a test. Nothing shifts.
68 */
69#define PRAET_CORE_MASK ((uint32_t)((1u << PRAET_CORE_BITS) - 1u))
70
71/**
72 * @brief Token id of the status set while the channel holds an interrupt vector.
73 */
74#define PRAET_ID_CLAIMED 0u
75
76/**
77 * @brief Token id of the status set while the engine settles.
78 */
79#define PRAET_ID_SETTLING 1u
80
81/**
82 * @brief Token id of the status set while a detach is in progress.
83 */
84#define PRAET_ID_SEPARANS 2u
85
86/**
87 * @brief Token id of the status set where the last transfer ended in an error.
88 */
89#define PRAET_ID_ERROR 3u
90
91/**
92 * @brief Token id of the status set where an active channel went unkicked for a whole window.
93 */
94#define PRAET_ID_STALLED 4u
95
96/**
97 * @brief Token id of the status set where a stalled transfer went back.
98 */
99#define PRAET_ID_ABANDONED 5u
100
101/**
102 * @brief Token id of the status set where a stalled transfer's bytes counted as unsafe.
103 */
104#define PRAET_ID_SCRUBBED 6u
105
106/**
107 * @brief Token id of the status set where a recovery measured the boundary word.
108 */
109#define PRAET_ID_MEASURED 7u
110
111/**
112 * @brief How many status token ids there are.
113 *
114 * @note Raise this with the addition of a status, and the assertion on PRAET_EVERY_STATUS fails until the
115 * new one appears there too.
116 */
117#define PRAET_STATUS_COUNT 8u
118
119/**
120 * @brief First bit the statuses occupy, the bit above the core.
121 */
122#define PRAET_STATUS_SHIFT PRAET_CORE_BITS
123
124/**
125 * @brief The bit a status token id owns.
126 *
127 * @param[in] id_ Status token id, below PRAET_STATUS_COUNT.
128 * @return That id's bit, placed above the core.
129 * @note Every status below comes through this, and so no status carries a hand-written bit that
130 * could disagree with the id beside it.
131 */
132#define PRAET_STATUS_BIT(id_) ((uint32_t)1u << (PRAET_STATUS_SHIFT + (id_)))
133
134/**
135 * @brief Every bit the statuses occupy, derived from how many there are.
136 */
137#define PRAET_STATUS_MASK ((uint32_t)(((1u << PRAET_STATUS_COUNT) - 1u) << PRAET_STATUS_SHIFT))
138
139/**
140 * @brief Set while the channel holds an interrupt vector.
141 *
142 * @note Claimed at attach and released at detach. There is no reason to hold a vector for a memory
143 * mover that moves nothing, and the claim gives the context one owner.
144 */
145#define PRAET_CLAIMED PRAET_STATUS_BIT(PRAET_ID_CLAIMED)
146
147/**
148 * @brief Set while the engine settles and will take no transfer.
149 */
150#define PRAET_SETTLING PRAET_STATUS_BIT(PRAET_ID_SETTLING)
151
152/**
153 * @brief Set where a detach is in progress and the channel did not finish it.
154 */
155#define PRAET_SEPARANS PRAET_STATUS_BIT(PRAET_ID_SEPARANS)
156
157/**
158 * @brief Set where the last transfer ended in an error, clear where it completed.
159 */
160#define PRAET_ERROR PRAET_STATUS_BIT(PRAET_ID_ERROR)
161
162/**
163 * @brief Set where an active channel went a whole keepalive window with no kick.
164 *
165 * @note Says the engine stopped, and says nothing about whether the transfer finished. A
166 * stalled channel is still busy, because nothing told this library otherwise.
167 */
168#define PRAET_STALLED PRAET_STATUS_BIT(PRAET_ID_STALLED)
169
170/**
171 * @brief Set where a stalled transfer went back and its bytes stayed as they were.
172 *
173 * @note Owns its bit on every build. A build with recovery off never sets it, and the bit stays
174 * reserved so a flag word means one thing everywhere.
175 */
176#define PRAET_ABANDONED PRAET_STATUS_BIT(PRAET_ID_ABANDONED)
177
178/**
179 * @brief Set where a stalled transfer resolved with its bytes counted as unsafe.
180 *
181 * @note The bytes are the caller's and this context has no pointer to them, and so nothing here scrubs
182 * anything. The flag records the decision; the caller zeroes its own storage.
183 */
184#define PRAET_SCRUBBED PRAET_STATUS_BIT(PRAET_ID_SCRUBBED)
185
186/**
187 * @brief Set where a recovery measured the word the engine was in the middle of.
188 *
189 * @note What tells a reader that the boundary checksum means anything. A checksum of zero is a legal
190 * one. A reader that took the value alone could not tell a measured word from an unmeasured
191 * context.
192 * @note A context whose declaration chose the DISABLE token never sets this. Neither does a
193 * recovery whose sample already landed on a word boundary, since there is no partial word to
194 * measure.
195 */
196#define PRAET_MEASURED PRAET_STATUS_BIT(PRAET_ID_MEASURED)
197
198/**
199 * @brief Every status, listed by name.
200 *
201 * @note Written out by hand, because this is the half of the check that has to come from
202 * somewhere other than PRAET_STATUS_COUNT. A compare of a list of names against a count neither
203 * one produced is what catches a duplicated id or a status nobody placed.
204 */
205#define PRAET_EVERY_STATUS \
206 (PRAET_CLAIMED | PRAET_SETTLING | PRAET_SEPARANS | PRAET_ERROR | PRAET_STALLED | PRAET_ABANDONED | \
207 PRAET_SCRUBBED | PRAET_MEASURED)
208
209/**
210 * @brief First bit of the region descriptor.
211 *
212 * @note A byte on a byte boundary. The descriptor is a number derived from the address space behind
213 * the region, and so nothing here stores an address. Its place at a byte edge lets a build
214 * that wants to compress it to eight bits do so and move no status.
215 * @note The third byte and not the second. Statuses grow upward from the core and the region has to
216 * start above all of them. At bit 8 it would share a bit with PRAET_SCRUBBED, which would read
217 * as a region whose low bit flips when a recovery marks a transfer scrubbed.
218 */
219#define PRAET_REGION_SHIFT 16u
220
221/**
222 * @brief How wide the region descriptor is.
223 */
224#define PRAET_REGION_BITS 8u
225
226/**
227 * @brief The largest descriptor the region field holds.
228 */
229#define PRAET_REGION_MAX ((uint32_t)((1u << PRAET_REGION_BITS) - 1u))
230
231/**
232 * @brief The bits the region descriptor occupies once shifted into place.
233 */
234#define PRAET_REGION_MASK (PRAET_REGION_MAX << PRAET_REGION_SHIFT)
235
236/**
237 * @brief A region descriptor placed in the field the map gives it.
238 *
239 * @param[in] region_ Region descriptor, narrowed to what the field holds.
240 * @return region_ in position, and never a bit outside PRAET_REGION_MASK.
241 * @note The one way to write the field. A narrow cast at the write site instead would put the field's
242 * width in two places. The copy that drifts is the single one nothing checks.
243 */
244#define PRAET_REGION_FIELD(region_) (((uint32_t)(region_) & PRAET_REGION_MAX) << PRAET_REGION_SHIFT)
245
246/**
247 * @brief The regions a channel can attach over.
248 *
249 * @note Token ids, the same way the statuses above are token ids. A region arrives at an attach as one
250 * of these names. A mistyped one is an undeclared identifier that carries the name as
251 * written, and a caller cannot pass a byte that means nothing.
252 * @note Two of them, because ParsMemoriaeInternae and ParsMemoriaeExternum are the two declarations a
253 * pool can have. A part with more address spaces than that adds ids here and the
254 * assertion below retains them inside the field.
255 * @note The values are ordinals. A build that wants a descriptor it can infer an address space from
256 * replaces the numbers and changes nothing else. The field is a byte and every read of it
257 * goes through PRAET_REGION_FIELD.
258 */
259typedef enum
260{
261 PRAET_REGION_INTERNAL = 0,
262 PRAET_REGION_EXTERNAL = 1
264
265/**
266 * @brief The largest region token id there is.
267 *
268 * @note Raise this with the addition of a region, and the assertion below fails until the field is wide
269 * enough to hold it.
270 */
271#define PRAET_REGION_ID_HIGHEST PRAET_REGION_EXTERNAL
272
273/**
274 * @brief Every bit the map accounts for.
275 *
276 * @note What a flag word may carry. A build whose writes stay inside this cannot produce a word with
277 * a bit nobody named. So nothing has to look at a word at run time to find that out.
278 */
279#define PRAET_MAP_MASK (PRAET_CORE_MASK | PRAET_STATUS_MASK | PRAET_REGION_MASK)
280
281// Every value the core mask can hold has a name, and the four names are distinct. One bit per
282// core value turns both halves of that into one comparison
283EMBED_STATIC_ASSERT(((1u << PRAET_SEPARATUS) | (1u << PRAET_ADNEXUS) | (1u << PRAET_BUSY) | (1u << PRAET_OK)) ==
284 ((1u << (PRAET_CORE_MASK + 1u)) - 1u),
285 "the four core states are not exactly the values PRAET_CORE_MASK holds");
286
287// The id set is dense, nothing collides, and nothing is absent. A duplicated id drops a bit out of
288// the list. An id at or above the count puts one outside the mask, and a status left out of
289// PRAET_EVERY_STATUS leaves a hole. All three come out as this one inequality
290EMBED_STATIC_ASSERT(PRAET_EVERY_STATUS == PRAET_STATUS_MASK,
291 "the status token ids are not dense and distinct, or PRAET_EVERY_STATUS is missing one");
292
293EMBED_STATIC_ASSERT((PRAET_CORE_MASK & PRAET_STATUS_MASK) == 0u, "a status sits on top of the quaternary core");
294
295EMBED_STATIC_ASSERT((PRAET_STATUS_MASK & PRAET_REGION_MASK) == 0u, "a status sits on top of the region descriptor");
296
297EMBED_STATIC_ASSERT((PRAET_CORE_MASK & PRAET_REGION_MASK) == 0u,
298 "the region descriptor sits on top of the quaternary core");
299
301 "the region descriptor starts below the highest status");
302
303// A descriptor a build wants to read or write as a byte has to be addressable as one
304EMBED_STATIC_ASSERT((PRAET_REGION_SHIFT % 8u) == 0u, "the region descriptor is not on a byte boundary");
305
306EMBED_STATIC_ASSERT((PRAET_REGION_SHIFT + PRAET_REGION_BITS) <= 32u,
307 "the map runs off the top of the 32 bit flag word");
308
309// The write reaches the whole field and never a bit past it, whatever the caller passes over. Both
310// halves matter: a field that cannot fill loses descriptors, and one that overruns lands in a
311// status
313 "PRAET_REGION_FIELD cannot fill the field the map gives it");
314
315EMBED_STATIC_ASSERT((PRAET_REGION_FIELD(0xFFFFFFFFu) & (uint32_t)~PRAET_REGION_MASK) == 0u,
316 "PRAET_REGION_FIELD writes outside the region descriptor");
317
318// Nothing a flag word carries sits outside the map, and the map has nothing in it that no field
319// claims. A word is one of these three things end to end
321 "the map is not exactly the core, the statuses and the region");
322
323// A region token id that does not fit the field would land truncated, and the channel would
324// report a region nobody asked for
325EMBED_STATIC_ASSERT((uint32_t)PRAET_REGION_ID_HIGHEST <= PRAET_REGION_MAX,
326 "there are more region token ids than the region descriptor field can hold");
327
328EMBED_END_DECLS
329
330/** @} */
331
332#endif
PraetRegion
The regions a channel can attach over.
#define PRAET_STATUS_MASK
Every bit the statuses occupy, derived from how many there are.
#define PRAET_SEPARATUS
Detached: the channel holds no engine and no vector.
#define PRAET_BUSY
Busy: the engine moves bytes on this channel.
#define PRAET_MAP_MASK
Every bit the map accounts for.
#define PRAET_OK
Ok: the last transfer finished and the channel is still attached.
#define PRAET_CORE_MASK
The two bits the quaternary core occupies.
#define PRAET_REGION_FIELD(region_)
A region descriptor placed in the field the map gives it.
#define PRAET_STATUS_COUNT
How many status token ids there are.
#define PRAET_REGION_BITS
How wide the region descriptor is.
#define PRAET_ADNEXUS
Attached: the channel holds the engine and will take a transfer.
#define PRAET_EVERY_STATUS
Every status, listed by name.
#define PRAET_REGION_MAX
The largest descriptor the region field holds.
#define PRAET_REGION_SHIFT
First bit of the region descriptor.
#define PRAET_REGION_MASK
The bits the region descriptor occupies once shifted into place.
#define PRAET_STATUS_SHIFT
First bit the statuses occupy, the bit above the core.
#define PRAET_REGION_ID_HIGHEST
The largest region token id there is.
The embedded_types headers every module builds on.