MMgr v2.3.0
Zero-heap memory manager in C11. One borrowed buffer, carved by asserted offset.
Loading...
Searching...
No Matches
locus_carcerum.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 locus_carcerum.h
9 * @brief A prison site: a caller's storage divided into cellblocks, with the warden that holds them.
10 * @author dstroy0 (Douglas Quigg) <dquigg123@gmail.com>
11 * @date 2026-08-29
12 *
13 * @note LocusCarcerum() emits each cellblock's state and the warden that holds them and that every
14 * call goes through, all as initialized data. Nothing runs at startup. A configuration the
15 * asserts reject fails the build.
16 * @note The storage is the caller's. A cellblock is over a pool, ParsMemoriaeInternae or
17 * ParsMemoriaeExternum in mmgr.h, and its SeriesCellularum record holds that pool's address.
18 * The linker resolves it. No call computes a cellblock's address from a site and an index.
19 * The declaration of the pool settles which memory a cellblock sits in.
20 * @note A cellblock has two tiers. The persistent tier runs up from base and the temporary tier runs
21 * down from size. Both boundaries are the two halves of one atomic word. A take moves its own
22 * half by one fetch-add whose old value carries the other, through the cellblock's turnstiles.
23 * @note A cellblock's declaration picks its security level. MMGR_MINIMUM_SECURITY leaves a cell's
24 * bytes as they are on release. MMGR_MAXIMUM_SECURITY zeroes them. The warden is const:
25 * nothing after the declaration can change the level a cellblock runs at.
26 * @note The public entries are English and the internals are Latin, this filename included.
27 * @note spatium provides the spans over those bytes.
28 */
29#ifndef MMGR_LOCUS_CARCERUM_H
30#define MMGR_LOCUS_CARCERUM_H
31
32/** @addtogroup mod_confin
33 * @{ */
34
35#include "mmgr.h"
36
37EMBED_BEGIN_DECLS
38
39/**
40 * @brief Set to 1 to track each tier's high-water mark.
41 *
42 * @note Off by default because the two marks cost a size_t each in every SeriesCellularum, and a
43 * build that never reads them should not carry them.
44 * @note Adds SeriesCellularum::persistent_hw and SeriesCellularum::temporary_hw, and the update that
45 * raises them in carcer_grow, which both tiers reach. One mark per tier: neither counts the
46 * other's bytes.
47 * @note The #ifndef leaves a build's own definition in place, whether it arrives on the command line
48 * or from a header included ahead of this one.
49 * @note Takes 0 or 1 alone. Both readers are #if, which would take any non-zero as set. The check
50 * below retains a mistyped value from a quiet growth of every SeriesCellularum.
51 */
52#ifndef MMGR_ENABLE_HW_MEM_CAPACITY_CB
53#define MMGR_ENABLE_HW_MEM_CAPACITY_CB 0
54#endif
55#if (MMGR_ENABLE_HW_MEM_CAPACITY_CB != 0) && (MMGR_ENABLE_HW_MEM_CAPACITY_CB != 1)
56#error "MMGR_ENABLE_HW_MEM_CAPACITY_CB must be 0 or 1"
57#endif
58
59/**
60 * @brief The max owners a program's owner table holds.
61 *
62 * @note The table is the program's one list of owners, positioned by MMGR_OWNERS in the order of their
63 * declaration. An owner's place in it is its key. A declaration past this count fails the build.
64 */
65#ifndef MMGR_CARCER_OWNERS
66#define MMGR_CARCER_OWNERS 128u
67#endif
68
69/**
70 * @brief The fragmentation at which a cellblock's mover sorts its movable cells, as a percentage of the
71 * bytes its persistent tier occupies.
72 *
73 * @note mmgr_series_cellularum_fragmentation reads what the build compares against this.
74 */
75#ifndef MMGR_MOVABLE_SETTLE_PERCENT
76#define MMGR_MOVABLE_SETTLE_PERCENT 25u
77#endif
78#if (MMGR_MOVABLE_SETTLE_PERCENT < 1u) || (MMGR_MOVABLE_SETTLE_PERCENT > 100u)
79#error "MMGR_MOVABLE_SETTLE_PERCENT must be from 1 to 100"
80#endif
81
82/** @brief The milliseconds between the mover's readings of its cellblock. */
83#ifndef MMGR_MOVABLE_READ_MS
84#define MMGR_MOVABLE_READ_MS 100u
85#endif
86
87/** @brief The readings in a row that must find the cellblock quiet before the mover sorts. */
88#ifndef MMGR_MOVABLE_QUIET_READINGS
89#define MMGR_MOVABLE_QUIET_READINGS 3u
90#endif
91
92/** @brief The longest the mover waits between readings after a sort it backed off from. */
93#ifndef MMGR_MOVABLE_BACKOFF_MAX_MS
94#define MMGR_MOVABLE_BACKOFF_MAX_MS 25600u
95#endif
96
97/** @brief The longest the mover waits between readings of a settled cellblock that no pass touched since the last
98 * reading. Each such reading doubles the wait up to this. A pass or a rise brings the next reading back to
99 * MMGR_MOVABLE_READ_MS. */
100#ifndef MMGR_MOVABLE_IDLE_MAX_MS
101#define MMGR_MOVABLE_IDLE_MAX_MS 1000u
102#endif
103
104/**
105 * @brief The regions of the temporary tier: one stack of cells for each place the host gives a thread, modulo this
106 * count. 0 makes the temporary tier one stack that every thread shares.
107 *
108 * @note A take goes to the region that the thread's place selects, embed_thread_place() modulo this count. A give
109 * goes to the region that holds the cell, from any thread. A region's depth never passes
110 * MMGR_CARCER_REGION_BYTES. A take the region cannot hold sweeps the region, takes the last free run that
111 * holds it, and else goes to the large region, where the build declares one, which refuses it the same way.
112 * theory/locus_carcerum/LocusCarcerum/Regions.lean proves the bound, and Stacks.lean proves the sinking of
113 * one shared stack that the regions stop.
114 * @note The regions lie at the top of the cellblock, MMGR_CARCER_REGION_AREA bytes in all, and the gap lies below
115 * them. A take over a region's room goes to the large region, MMGR_CARCER_LARGE_REGION_BYTES, where the
116 * build declares one. The host defines embed_thread_place beside embed_clock_ns, as embed_turnstile.h says.
117 */
118#ifndef MMGR_CARCER_REGIONS
119#define MMGR_CARCER_REGIONS 0u
120#endif
121
122/**
123 * @brief Bytes of each region: its cells with their headers and owner words. A multiple of MMGR_CARCER_ALIGN.
124 *
125 * @note The max payload of one temporary take is this less a cell's two words.
126 */
127#ifndef MMGR_CARCER_REGION_BYTES
128#define MMGR_CARCER_REGION_BYTES 0u
129#endif
130
131/**
132 * @brief Bytes of the one large region, at the top of the cellblock, that every temporary take over a region's room
133 * goes to, from any thread. 0 refuses such a take.
134 *
135 * @note The large region is one more region, the last, shared by every thread. A take goes there where its payload
136 * and its two words do not fit MMGR_CARCER_REGION_BYTES. Its depth never passes this size. Threads share it,
137 * so it sinks as one shared stack does, to this size at most, and the sweep then reclaims it.
138 */
139#ifndef MMGR_CARCER_LARGE_REGION_BYTES
140#define MMGR_CARCER_LARGE_REGION_BYTES 0u
141#endif
142
143/** @brief 1 where the cellblock has the large region, 0 where it has none. */
144#define MMGR_CARCER_LARGE_REGIONS ((size_t)(MMGR_CARCER_LARGE_REGION_BYTES > 0u))
145
146/** @brief The regions in all: one for each place, and the large one where the build declares it. */
147#define MMGR_CARCER_REGION_COUNT ((size_t)MMGR_CARCER_REGIONS + MMGR_CARCER_LARGE_REGIONS)
148
149/** @brief Bytes the regions take off the top of every cellblock, 0 where the temporary tier is one stack. */
150#define MMGR_CARCER_REGION_AREA \
151 (((size_t)MMGR_CARCER_REGIONS * (size_t)MMGR_CARCER_REGION_BYTES) + (size_t)MMGR_CARCER_LARGE_REGION_BYTES)
152
153#include "embed_atomic.h"
154#include "embed_lane.h"
155#include "embed_turnstile.h"
156
157/**
158 * @brief Alignment of every prisoner on the way out: one machine word, or the atomic size word's alignment where
159 * that is wider.
160 *
161 * @note Derived from the two types in place of a number. Every cell carries an atomic size word at its header
162 * and at its foot. A cell's offset and payload are whole multiples of this, and each of those words
163 * lands at its own alignment at every width.
164 * @note LocusCarcerum() puts this alignment on the storage it declares. This rounds only
165 * the offsets inside a cellblock.
166 * @note EMBED_ALIGNOF is _Alignof in C and alignof in C++, the language nvcc compiles.
167 */
168#define MMGR_CARCER_ALIGN \
169 ((sizeof(embed_word) > EMBED_ALIGNOF(embed_atomic_usize)) ? (size_t)sizeof(embed_word) \
170 : (size_t)EMBED_ALIGNOF(embed_atomic_usize))
171
172/**
173 * @brief Asserts MMGR_CARCER_ALIGN is a power of two.
174 *
175 * @note A round of an offset masks off its low bits, which lands on a multiple only for a power
176 * of two. Both carcer_round and CARCER_HDR in locus_carcerum.c round that way.
177 */
178EMBED_STATIC_ASSERT((MMGR_CARCER_ALIGN & (MMGR_CARCER_ALIGN - 1u)) == 0u,
179 "the cellblock rounds offsets by masking, which needs a power of two alignment");
180
181/**
182 * @brief Asserts the storage a pool declaration aligns is on the cell grain.
183 *
184 * @note A cell's words lie at whole grains from the storage's first byte, and they are at their alignment only
185 * where that byte does.
186 */
187EMBED_STATIC_ASSERT((MMGR_ALIGN_BYTES % MMGR_CARCER_ALIGN) == 0u,
188 "MMGR_ALIGN_BYTES must be a multiple of MMGR_CARCER_ALIGN, the grain a cell's words are on");
189
190#if MMGR_CARCER_REGIONS > 0u
191/**
192 * @brief Asserts each region is whole grains and holds one cell: its two words and one grain of payload.
193 */
194EMBED_STATIC_ASSERT(((size_t)MMGR_CARCER_REGION_BYTES % MMGR_CARCER_ALIGN) == 0u,
195 "MMGR_CARCER_REGION_BYTES must be a multiple of MMGR_CARCER_ALIGN");
196EMBED_STATIC_ASSERT((size_t)MMGR_CARCER_REGION_BYTES >= (4u * MMGR_CARCER_ALIGN),
197 "MMGR_CARCER_REGION_BYTES is too small for one cell");
198EMBED_STATIC_ASSERT(((size_t)MMGR_CARCER_LARGE_REGION_BYTES % MMGR_CARCER_ALIGN) == 0u,
199 "MMGR_CARCER_LARGE_REGION_BYTES must be a multiple of MMGR_CARCER_ALIGN");
200EMBED_STATIC_ASSERT((MMGR_CARCER_LARGE_REGION_BYTES == 0u) ||
202 "MMGR_CARCER_LARGE_REGION_BYTES must be above MMGR_CARCER_REGION_BYTES, or 0");
203#else
204EMBED_STATIC_ASSERT(MMGR_CARCER_LARGE_REGION_BYTES == 0u,
205 "MMGR_CARCER_LARGE_REGION_BYTES needs MMGR_CARCER_REGIONS above 0");
206#endif
207
208/**
209 * @brief One owner of cells: its name, its key, the payload bytes of its cells now on both tiers, and
210 * the max it had at once.
211 *
212 * @note The library's storage, positioned at compile time. The owner table is the list of these records
213 * that MMGR_OWNERS declares in the program's one translation unit for it. The same declaration
214 * defines each owner's identity: a const pointer into the table, which the linker resolves. The
215 * owner's module names it on every take, give and growth, as it names a movable cell's entry.
216 * @note Nothing registers at run time. The pointer's const says only that the holder does not write;
217 * the record is MMgr's to write. Every entry refuses NULL in place of an owner, and every take
218 * refuses a pointer that is not one of the table's.
219 * @note The key is the owner's place in the table, counted from 1.
220 * @note A take adds the cell's payload bytes to in_use and raises hw to it; a give takes the same
221 * bytes off. Every count is an atomic word, added to and raised in one step each, and an owner
222 * shared between contexts counts exactly.
223 * @note The bytes counted are the cell's own: the request rounded to a whole word. The take counts them
224 * on, the give reads them off the cell's word, and a matched pair returns in_use to where it was.
225 * @note Every cell carries its owner in the word behind its payload, and comes off that owner as a give
226 * drops it. A give or a growth that names another owner fails, and mmgr_carcer_hw_misnamed hears
227 * of it.
228 */
229typedef struct
230{
231 const char *name; /**< The owner's name, as a reader prints it, unique in the table [BORROWS]. */
232 size_t place; /**< The owner's place in the table, from 1. */
233 embed_atomic_usize in_use; /**< Payload bytes of the cells the owner has now. */
234 embed_atomic_usize hw; /**< The highest value of in_use so far. */
235} CellOwner;
236
237/**
238 * @brief The owner word a cell carries behind its payload, one machine word positioned as fields, high to low. The
239 * fields are the owner's place in the table, the completion bit, the signal bits, and the nanosecond of
240 * the take. The clock is the library's, and the nanosecond rolls over its bits.
241 *
242 * @note The owner test is one AND: the word's place field against an owner's place. A word whose place field
243 * is 0 names no owner, as a free cell's does. The one context whose swap lands sets the completion bit
244 * once, when the cell's bytes come off its owner. Two gives that reach the same cell count it off once
245 * between them. The signal bits are the cellblock's to raise and read with one atomic step each. The
246 * timestamp is what the cell's take read on mmgr_carcer_hw_clock_ns, in the bits left, and a reader
247 * of lifetimes knows it rolls over.
248 * @note MMGR_CARCER_OWNERS sizes the place field: the fewest bits every owner's place fits, from 4 to 16.
249 * Then come 1 completion bit, 3 signal bits, and the timestamp in the bits left, the word's width
250 * less those. 1024 owners on a 64-bit word leave 49 timestamp bits. On a 32-bit word they leave 17,
251 * which roll over every 131 microseconds, and on a 16-bit word 1. The layout fails the build where
252 * the word has no timestamp bit.
253 */
254#if MMGR_CARCER_OWNERS < 0x10u
255#define MMGR_OWNER_PLACE_BITS 4u
256#elif MMGR_CARCER_OWNERS < 0x20u
257#define MMGR_OWNER_PLACE_BITS 5u
258#elif MMGR_CARCER_OWNERS < 0x40u
259#define MMGR_OWNER_PLACE_BITS 6u
260#elif MMGR_CARCER_OWNERS < 0x80u
261#define MMGR_OWNER_PLACE_BITS 7u
262#elif MMGR_CARCER_OWNERS < 0x100u
263#define MMGR_OWNER_PLACE_BITS 8u
264#elif MMGR_CARCER_OWNERS < 0x200u
265#define MMGR_OWNER_PLACE_BITS 9u
266#elif MMGR_CARCER_OWNERS < 0x400u
267#define MMGR_OWNER_PLACE_BITS 10u
268#elif MMGR_CARCER_OWNERS < 0x800u
269#define MMGR_OWNER_PLACE_BITS 11u
270#elif MMGR_CARCER_OWNERS < 0x1000u
271#define MMGR_OWNER_PLACE_BITS 12u
272#elif MMGR_CARCER_OWNERS < 0x2000u
273#define MMGR_OWNER_PLACE_BITS 13u
274#elif MMGR_CARCER_OWNERS < 0x4000u
275#define MMGR_OWNER_PLACE_BITS 14u
276#elif MMGR_CARCER_OWNERS < 0x8000u
277#define MMGR_OWNER_PLACE_BITS 15u
278#elif MMGR_CARCER_OWNERS < 0x10000u
279#define MMGR_OWNER_PLACE_BITS 16u
280#else
281#error "MMGR_CARCER_OWNERS must be below 65536: the owner word's place field is 16 bits at max"
282#endif
283#define MMGR_OWNER_SIGNAL_BITS 3u
284EMBED_STATIC_ASSERT(EMBED_WORD_BITS > (MMGR_OWNER_PLACE_BITS + 1u + MMGR_OWNER_SIGNAL_BITS),
285 "the owner word must have a timestamp bit left after its place, completion and signaling bits");
286#define MMGR_OWNER_TIME_BITS (EMBED_WORD_BITS - MMGR_OWNER_PLACE_BITS - 1u - MMGR_OWNER_SIGNAL_BITS)
287#define MMGR_OWNER_TIME_MASK (((embed_atomic_usize_value)1u << MMGR_OWNER_TIME_BITS) - 1u)
288#define MMGR_OWNER_SIGNAL_SHIFT MMGR_OWNER_TIME_BITS
289#define MMGR_OWNER_COMPLETE ((embed_atomic_usize_value)1u << (MMGR_OWNER_TIME_BITS + MMGR_OWNER_SIGNAL_BITS))
290#define MMGR_OWNER_PLACE_SHIFT (MMGR_OWNER_TIME_BITS + MMGR_OWNER_SIGNAL_BITS + 1u)
291#define MMGR_OWNER_PLACE_MASK ((((embed_atomic_usize_value)1u << MMGR_OWNER_PLACE_BITS) - 1u) << MMGR_OWNER_PLACE_SHIFT)
292EMBED_STATIC_ASSERT((size_t)MMGR_CARCER_OWNERS < ((size_t)1u << MMGR_OWNER_PLACE_BITS),
293 "MMGR_CARCER_OWNERS must fit the owner word's place field");
294
295/**
296 * @brief The library's clock in nanoseconds, which a cell's owner word carries the take on.
297 *
298 * @return The nanoseconds since a start the port chooses, which wrap at 2^64.
299 * @note Weak, and the default answers 0 every time: a build that defines none stamps every take 0. A port
300 * defines this name to replace it.
301 */
302EMBED_HOST_DEVICE uint64_t mmgr_carcer_hw_clock_ns(void);
303
304/**
305 * @brief The program's owner table and the owners in it, which MMGR_OWNERS defines once.
306 *
307 * @note A program that reaches a cellblock declares its owners with MMGR_OWNERS in one translation unit.
308 * A build with none fails at the link, and the link names these.
309 * @note Under nvcc each has a device twin, with `_device` after its name, that MMGR_OWNERS defines from the same
310 * initializer. A read names it through EMBED_PASS_NAME, and so a kernel counts its cells in the device table.
311 */
312EMBED_PASS_DECLARE(CellOwner, mmgr_owner_table, []);
313EMBED_PASS_DECLARE(const size_t, mmgr_owner_table_count, );
314
315/**
316 * @brief One owner's place in the table, counted from 0, as the enumeration MMGR_OWNERS positions names it.
317 *
318 * @param[in] name_ The owner's identity, as declared.
319 * @param[in] text_ The owner's name as a reader prints it, unread here.
320 */
321#define MMGR_OWNER_PLACE(name_, text_) mmgr_owner_place_##name_,
322
323/**
324 * @brief One owner's record as the table's initializer positions it: its name, its place from 1, nothing counted.
325 *
326 * @param[in] name_ The owner's identity, as declared.
327 * @param[in] text_ The owner's name as a reader prints it [BORROWS].
328 */
329#define MMGR_OWNER_RECORD(name_, text_) \
330 {(text_), (size_t)mmgr_owner_place_##name_ + 1u, EMBED_ATOMIC_INIT(0u), EMBED_ATOMIC_INIT(0u)},
331
332/**
333 * @brief One owner's identity defined: a const pointer to its record in the table, resolved by the linker.
334 *
335 * @param[in] name_ The owner's identity, as declared.
336 * @param[in] text_ The owner's name as a reader prints it, unread here.
337 * @note EMBED_PASS_LOCAL gives each pass of nvcc its own identity, which points into the table of that pass.
338 * @warning Under nvcc the identity is local to the unit that expands MMGR_OWNERS, as EMBED_PASS_LOCAL states.
339 */
340#define MMGR_OWNER_POINTER(name_, text_) \
341 EMBED_PASS_LOCAL(const CellOwner *const, name_, &EMBED_PASS_NAME(mmgr_owner_table)[mmgr_owner_place_##name_]);
342
343/**
344 * @brief One owner's identity declared, for the header the owner's module reads it through.
345 *
346 * @param[in] name_ The owner's identity, as declared.
347 * @param[in] text_ The owner's name as a reader prints it, unread here.
348 */
349#define MMGR_OWNER_EXTERN(name_, text_) extern const CellOwner *const name_;
350
351/**
352 * @brief The program's owners declared as data: the table positioned, its count, and each owner's identity defined.
353 *
354 * @param[in] list_ The program's list of owners, a macro that takes one macro and applies it to each
355 * owner. The form list_(macro_) expands to macro_(name_, text_) for every owner. Here
356 * name_ is the identity the owner's module names and text_ the name a reader prints.
357 * @note Expanded once, in the translation unit that owns the program's memory. The enumeration gives each
358 * owner its place. The table has room for the owners declared and holds their records in that
359 * order. Each identity is a pointer into it that the linker resolves: there is no registration and
360 * nothing runs before the first take. A list longer than MMGR_CARCER_OWNERS fails the build.
361 * @note EMBED_PASS_OBJECT defines the table and its count, and under nvcc their device twins from the same
362 * initializers.
363 * @note The initializer sets the extent of the table, one record for each owner. The host pass of nvcc declares
364 * the device twin again at the declaration in this header, where the enumeration does not exist yet, and so
365 * the extent names no enumerator.
366 */
367#define MMGR_OWNERS(list_) \
368 enum \
369 { \
370 list_(MMGR_OWNER_PLACE) mmgr_owner_table_positioned \
371 }; \
372 EMBED_STATIC_ASSERT((size_t)mmgr_owner_table_positioned <= (size_t)MMGR_CARCER_OWNERS, \
373 "more owners are declared than MMGR_CARCER_OWNERS allows"); \
374 EMBED_PASS_OBJECT(CellOwner, mmgr_owner_table, [], {list_(MMGR_OWNER_RECORD)}); \
375 EMBED_PASS_OBJECT(const size_t, mmgr_owner_table_count, , (size_t)mmgr_owner_table_positioned); \
376 list_(MMGR_OWNER_POINTER)
377
378/**
379 * @brief Whether an owner is one of the table's.
380 *
381 * @param[in] owner The owner, which may be NULL [BORROWS].
382 * @return EMBED_TRUE where it is the record at its own place in the table.
383 */
384EMBED_HOST_DEVICE embed_bool mmgr_owner_listed(const CellOwner *owner);
385
386/**
387 * @brief The payload bytes of the cells an owner has now.
388 *
389 * @param[in] owner The owner [BORROWS].
390 * @return The bytes, off one word.
391 */
392EMBED_HOST_DEVICE EMBED_INLINE size_t mmgr_owner_in_use(const CellOwner *owner)
393{
394 // Explicit cast reads the word as the bytes it counts
395 return (size_t)EMBED_CALL(embed_atomic_usize_table.load, EmbedAtomicUsizeLoadCfg, .word = &owner->in_use);
396}
397
398/**
399 * @brief The max payload bytes an owner has had at once.
400 *
401 * @param[in] owner The owner [BORROWS].
402 * @return The bytes, off one word.
403 */
404EMBED_HOST_DEVICE EMBED_INLINE size_t mmgr_owner_hw(const CellOwner *owner)
405{
406 // Explicit cast reads the word as the bytes it counts
407 return (size_t)EMBED_CALL(embed_atomic_usize_table.load, EmbedAtomicUsizeLoadCfg, .word = &owner->hw);
408}
409
410/**
411 * @brief Told of each give or growth that names an owner other than the one the cell counts to.
412 *
413 * @param[in] named The owner the give or the growth named [BORROWS].
414 * @param[in] cell_owner The owner the cell counts to, NULL where its owner word names no registered
415 * owner [BORROWS].
416 * @param[in] bytes The cell's payload bytes.
417 * @note Such a give or growth fails and changes nothing: the cell stays with its holder, counted
418 * to its owner. A program that sees this has a give in the wrong place, and a port logs it.
419 * @note Weak, and the default does nothing. A port defines this name to replace it.
420 */
421EMBED_HOST_DEVICE void mmgr_carcer_hw_misnamed(const CellOwner *named, const CellOwner *cell_owner, size_t bytes);
422
423/**
424 * @brief The bits each boundary has in the word that carries both, half the atomic size word, and the mask of a
425 * half.
426 */
427#define MMGR_CARCER_HALF_BITS ((unsigned int)((sizeof(embed_atomic_usize_value) * 8u) / 2u))
428#define MMGR_CARCER_HALF_MASK ((embed_atomic_usize_value)(((embed_atomic_usize_value)1u << MMGR_CARCER_HALF_BITS) - 1u))
429
430/**
431 * @brief The reach of a position in a boundary half: half the half's values. The other half is the headroom the
432 * bumps a round refuses move the position into before the next writer settles it.
433 *
434 * @note A bump up reads its position at or below this before its fetch-add, and a temporary position past it is a
435 * bump down that wrapped. A cell's header carries a size this large in the bits above its two state bits.
436 */
437#define MMGR_CARCER_BYTES_MAX ((size_t)1u << (MMGR_CARCER_HALF_BITS - 1u))
438
439/**
440 * @brief The max payload one bump may ask. It is the headroom the bumps in flight on the turnstiles' lanes
441 * share. Each of them may move its boundary once past MMGR_CARCER_BYTES_MAX before the refusal stops
442 * the rest. A take past it goes to the writer's path at once.
443 */
444#define MMGR_CARCER_WANT_MAX ((size_t)1u << (MMGR_CARCER_HALF_BITS - 8u))
445
446/**
447 * @brief The bits of a bank's bytes: a bank is half the reach of a position. A test build can set fewer, so that a
448 * small pool spans many banks.
449 *
450 * @note A pool is a run of banks, and bank k starts at k shifted left by this. Each tier's position counts from the
451 * base of its frame, a bank, which the closed word names. A position reaches MMGR_CARCER_BYTES_MAX, so a tier
452 * crosses into the next bank with no writer. A writer that stores a boundary picks the frame that leaves the
453 * position a bank of room, at most MMGR_CARCER_FRAME_CAP. A pass reads the frames in its one read of the closed
454 * word. A writer reads them in its fetch-or of the closed bit. theory/locus_carcerum/LocusCarcerum/Banks.lean
455 * proves the frames.
456 */
457#ifndef MMGR_CARCER_BANK_SHIFT
458#define MMGR_CARCER_BANK_SHIFT (MMGR_CARCER_HALF_BITS - 2u)
459#endif
460
461/** @brief The bytes of one bank. */
462#define MMGR_CARCER_BANK_BYTES ((size_t)1u << MMGR_CARCER_BANK_SHIFT)
463
464/**
465 * @brief The bits of each field of the closed word above its four flag bits. The fields are the bank count, the
466 * frame of the persistent tier and the frame of the temporary tier, in that order.
467 */
468#define MMGR_CARCER_BANK_FIELD_BITS ((unsigned int)(((sizeof(embed_atomic_word_value) * 8u) - 4u) / 3u))
469
470/** @brief The mask of one field, at bit 0. */
471#define MMGR_CARCER_BANK_FIELD_MASK \
472 ((embed_atomic_word_value)(((embed_atomic_word_value)1u << MMGR_CARCER_BANK_FIELD_BITS) - 1u))
473
474/** @brief The bit of the closed word that each field starts at. */
475#define MMGR_CARCER_BANK_COUNT_AT 4u
476#define MMGR_CARCER_BANK_END_AT (4u + MMGR_CARCER_BANK_FIELD_BITS)
477#define MMGR_CARCER_BANK_TOP_AT (4u + (2u * MMGR_CARCER_BANK_FIELD_BITS))
478
479/**
480 * @brief The max bytes one cellblock may hold: one less than the values of a field, times the bytes of a bank. So
481 * every frame and the bank count fit their fields.
482 *
483 * @note LocusCarcerum() asserts each pool against this, and mmgr_series_cellularum_placed refuses a pool past it.
484 */
485#define MMGR_CARCER_POOL_BYTES_MAX \
486 ((size_t)((((size_t)1u << MMGR_CARCER_BANK_FIELD_BITS) - 1u) << MMGR_CARCER_BANK_SHIFT))
487
488/** @brief The bank an offset is in. */
489#define MMGR_CARCER_BANK_OF(off_) (((size_t)(off_)) >> MMGR_CARCER_BANK_SHIFT)
490
491/** @brief The banks a cellblock of bytes_ bytes takes: its bytes rounded up to a whole bank. */
492#define MMGR_CARCER_BANK_COUNT(bytes_) MMGR_CARCER_BANK_OF((size_t)(bytes_) + (MMGR_CARCER_BANK_BYTES - 1u))
493
494/**
495 * @brief The highest frame of a cellblock of bytes_ bytes: two banks below its bank count, or bank 0.
496 *
497 * @note A position from this frame reaches the cellblock's end. So a cellblock of two banks or less keeps both
498 * frames at bank 0, and its writer never changes them (Banks.frames_still).
499 */
500#define MMGR_CARCER_FRAME_CAP(bytes_) \
501 ((MMGR_CARCER_BANK_COUNT(bytes_) > 2u) ? (MMGR_CARCER_BANK_COUNT(bytes_) - 2u) : (size_t)0u)
502
503/** @brief The frame a writer gives a persistent end in a cellblock of bytes_ bytes: the end's bank, or the cap. */
504#define MMGR_CARCER_END_FRAME(end_, bytes_) \
505 ((MMGR_CARCER_BANK_OF(end_) < MMGR_CARCER_FRAME_CAP(bytes_)) ? MMGR_CARCER_BANK_OF(end_) \
506 : MMGR_CARCER_FRAME_CAP(bytes_))
507
508/** @brief The bank below a top's own bank, or bank 0. */
509#define MMGR_CARCER_BANK_BELOW(top_) (MMGR_CARCER_BANK_OF(top_) - (size_t)(MMGR_CARCER_BANK_OF(top_) != 0u))
510
511/**
512 * @brief The frame a writer gives a temporary top in a cellblock of bytes_ bytes: the bank below the top's own, or
513 * the cap.
514 */
515#define MMGR_CARCER_TOP_FRAME(top_, bytes_) \
516 ((MMGR_CARCER_BANK_BELOW(top_) < MMGR_CARCER_FRAME_CAP(bytes_)) ? MMGR_CARCER_BANK_BELOW(top_) \
517 : MMGR_CARCER_FRAME_CAP(bytes_))
518
519/**
520 * @brief The closed word of a cellblock at rest over bytes_ bytes. It holds no flag, the bank count, the persistent
521 * frame at bank 0, and the temporary frame for the top at the regions' start.
522 */
523#define MMGR_CARCER_CLOSED_AT_REST(bytes_) \
524 ((embed_atomic_word_value)((((embed_atomic_word_value)MMGR_CARCER_BANK_COUNT(bytes_)) << MMGR_CARCER_BANK_COUNT_AT) | \
525 ((embed_atomic_word_value)MMGR_CARCER_TOP_FRAME( \
526 (size_t)(bytes_) - MMGR_CARCER_REGION_AREA, bytes_) \
527 << MMGR_CARCER_BANK_TOP_AT)))
528
529/** @brief The boundary word of a cellblock at rest over bytes_ bytes: the end at 0, the top's position in its frame. */
530#define MMGR_CARCER_BOUNDS_AT_REST(bytes_) \
531 ((embed_atomic_usize_value)(((size_t)(bytes_) - MMGR_CARCER_REGION_AREA) - \
532 (MMGR_CARCER_TOP_FRAME((size_t)(bytes_) - MMGR_CARCER_REGION_AREA, bytes_) \
533 << MMGR_CARCER_BANK_SHIFT)) \
534 << MMGR_CARCER_HALF_BITS)
535
536/**
537 * @brief One cellblock's state: its bytes, the two boundaries that grow toward each other, and the closed
538 * word a writer holds. Also the sink a refused store lands on, and the turnstiles every pass goes
539 * through.
540 *
541 * @note Written by the declaration that emits the cellblock and by the entries that cellblock owns. The base is
542 * the cellblock's own storage, and nothing derives an address from a site and an index.
543 * @note bounds is the two boundaries in one word, each a position in bytes from the base of its tier's frame. Its
544 * low half names the offset just past the last persistent cell, and its high half the offset of the lowest
545 * temporary cell. A take bumps its tier's half by one fetch-add on the word. The old value carries the other boundary as it was at that same
546 * instant: the room the take reads. A bump the gap cannot meet leaves the boundary where it moved it and
547 * adds its bytes to the tier's overrun, persistent_overrun or temporary_overrun. Every later bump of
548 * either tier fails, since the boundaries only move toward each other between writers and every bump
549 * reads them as one word.
550 * @note The next writer takes the overrun back off the boundary before it reads the tier. Between a refusal
551 * and that writer the boundary is past the tier's cells by the overrun. The page
552 * docs/concepts/locus-carcerum-algorithm.md writes the algorithm out and theory/locus_carcerum checks it.
553 * @note closed is the writer's word: bit 0 while a compaction, a growth or a move works alone, bit 1
554 * while a context sleeps on the word. A pass that reads bit 0 set touches nothing and tries once more after
555 * a backoff step. The word sink is the one every store of a pass that touches nothing lands on.
556 * @note Above its four flag bits, closed holds the bank count and the frame of each tier, the bank its position
557 * counts from. A pass reads them with its permit, and only a writer with every pass drained changes them. The
558 * member epoch is odd while a writer changes them, so a reader outside a pass reads a frame and a position as
559 * one.
560 * @note turnstiles is the bank of lanes a pass badges in at, EmbedTurnstiles' way, which a writer drains after
561 * it sets the closed bit. Zero at rest, as the last member, which a declaration leaves to the zero fill
562 * of static storage.
563 * @note The member in_use is this cellblock's total, counted where each owner's is. The member
564 * persistent_in_use is the part of it in the persistent tier, which a sort reads. The member movable is
565 * the option mmgr_movable_init assigns, and it reads zero without it.
566 * @note Where MMGR_CARCER_REGIONS is above 0, the temporary tier is the regions, and the top in bounds stays at
567 * the regions' start. Each region's depth is the bytes its stack holds from its end down, on a lane of its
568 * own. A take bumps the depth of the region the thread's place selects by one fetch-add, and a bump the
569 * region cannot hold goes back by one fetch-sub before the take goes on to the writer's path.
570 */
571#if MMGR_CARCER_REGIONS > 0u
572/** @brief One region of the temporary tier: the bytes its stack holds from its end down, on a lane of its own. */
573typedef struct
574{
575 EMBED_ALIGN(EMBED_LANE_BYTES) embed_atomic_usize depth; /**< Bytes positioned from the region's end down. */
576} CarcerRegion;
577#endif
578
579typedef struct
580{
581 uint8_t *base; /**< First byte of the cellblock [BORROWS]; NULL over a hospes pool until placed. */
582 size_t size; /**< Bytes in the cellblock; 0 over a hospes pool until placed. */
583 embed_atomic_usize bounds; /**< The persistent position in the low half, the temporary position in the high. */
584 embed_atomic_usize
585 persistent_overrun; /**< Bytes refused takes moved the persistent end by since the last writer. */
586 embed_atomic_usize temporary_overrun; /**< Bytes refused takes moved the temporary top by since the last writer. */
587 embed_atomic_usize in_use; /**< Payload bytes of the cells taken and not given back. */
588 embed_atomic_usize persistent_in_use; /**< The part of in_use in the persistent tier. */
589 embed_atomic_uptr movable; /**< The movable option's state, as an integer, zero without it. */
590 embed_atomic_word closed; /**< The writer's bit, the sleeper flag, the trim bits and the frames. */
591 embed_atomic_word epoch; /**< Odd while a writer changes the frames. */
592 embed_atomic_usize sink; /**< Where a store that must not land lands. */
593#if MMGR_ENABLE_HW_MEM_CAPACITY_CB
594 embed_atomic_usize persistent_hw; /**< The highest value of the persistent end so far. */
595 embed_atomic_usize temporary_hw; /**< The max bytes taken from the top so far. */
596#endif
597 EmbedTurnstiles turnstiles; /**< The lanes a pass badges in at. */
598#if MMGR_CARCER_REGIONS > 0u
599 CarcerRegion regions[MMGR_CARCER_REGION_COUNT]; /**< The depth of each region's stack, the large one last. */
600#endif
602
603/** @brief The high-water members' first values, or nothing where the build tracks none. */
604#if MMGR_ENABLE_HW_MEM_CAPACITY_CB
605#define MMGR_CARCER_STATE_HW , EMBED_ATOMIC_INIT(0u), EMBED_ATOMIC_INIT(0u)
606#else
607#define MMGR_CARCER_STATE_HW
608#endif
609
610/** @brief The regions' first depths, every one 0, or nothing where the temporary tier is one stack. */
611#if MMGR_CARCER_REGIONS > 0u
612#define MMGR_CARCER_STATE_REGIONS \
613 , \
614 { \
615 { \
616 EMBED_ATOMIC_INIT(0u) \
617 } \
618 }
619#else
620#define MMGR_CARCER_STATE_REGIONS
621#endif
622
623/**
624 * @brief The turnstiles' first values: each lane word, the top open lane and the queue length at 0.
625 *
626 * @note Each level of EmbedTurnstiles has its own brace: the struct, the array of lanes, one lane and its word.
627 * A bare {0} inside an initializer is a -Wmissing-braces finding, and -Wall turns that warning on in C.
628 */
629#define MMGR_CARCER_STATE_TURNSTILES {{{EMBED_ATOMIC_INIT(0u)}}, EMBED_ATOMIC_INIT(0u), EMBED_ATOMIC_INIT(0u)}
630
631/**
632 * @brief A cellblock's state as it is over bytes_ bytes at base_ with no cell taken. Both tiers are
633 * empty, every mark is at zero, and the zero fill of the storage covers the turnstiles.
634 *
635 * @param[in] base_ First byte of the cellblock [BORROWS].
636 * @param[in] bytes_ Bytes in the cellblock.
637 * @note The temporary top starts at the regions' start, which is the cellblock's size where there is no region. The
638 * closed word starts with the bank count and the frames of that top and of an end at 0.
639 */
640#define MMGR_CARCER_STATE(base_, bytes_) \
641 {(base_), \
642 (bytes_), \
643 EMBED_ATOMIC_INIT(MMGR_CARCER_BOUNDS_AT_REST(bytes_)), \
644 EMBED_ATOMIC_INIT(0u), \
645 EMBED_ATOMIC_INIT(0u), \
646 EMBED_ATOMIC_INIT(0u), \
647 EMBED_ATOMIC_INIT(0u), \
648 EMBED_ATOMIC_INIT(0u), \
649 EMBED_ATOMIC_INIT(MMGR_CARCER_CLOSED_AT_REST(bytes_)), \
650 EMBED_ATOMIC_INIT(0u), \
651 EMBED_ATOMIC_INIT(0u) MMGR_CARCER_STATE_HW, \
652 MMGR_CARCER_STATE_TURNSTILES MMGR_CARCER_STATE_REGIONS}
653
654/**
655 * @brief The record of a cellblock over a hospes pool before the program places it: no base, no size, both
656 * boundaries at 0, so every take is refused until mmgr_series_cellularum_placed gives the storage.
657 */
658#define MMGR_CARCER_STATE_HOSPES \
659 {NULL, \
660 0u, \
661 EMBED_ATOMIC_INIT(0u), \
662 EMBED_ATOMIC_INIT(0u), \
663 EMBED_ATOMIC_INIT(0u), \
664 EMBED_ATOMIC_INIT(0u), \
665 EMBED_ATOMIC_INIT(0u), \
666 EMBED_ATOMIC_INIT(0u), \
667 EMBED_ATOMIC_INIT(0u), \
668 EMBED_ATOMIC_INIT(0u), \
669 EMBED_ATOMIC_INIT(0u) MMGR_CARCER_STATE_HW, \
670 MMGR_CARCER_STATE_TURNSTILES MMGR_CARCER_STATE_REGIONS}
671
672/**
673 * @brief The identity a movable cell's taker holds: a const pointer to the cell's entry in the
674 * cellblock's translation table.
675 *
676 * @note The entry is the mutable word, and it is MMgr's alone: it names where the cell is now, and a
677 * move changes it. The pointer the caller holds never changes while the cell lives, however often
678 * the cell moves. No header a caller can reach declares the layout.
679 */
681
682/**
683 * @brief What a program gives a cellblock for the movable option, once, through its guard's movable_init.
684 *
685 * @note The option is the program's to choose. A cellblock with none takes and gives as it always does,
686 * each entry tests one word, and its movable_buf_alloc answers NULL.
687 * @note storage holds the table, its claim bits and the rows. It lies outside the prison, and so nothing of the
688 * option pins a tier or counts as a cell. A pool declared at compile time gives it a link-time address.
689 * @note width bounds the lanes the cellblock's turnstiles may have open while the mover reads it quiet. Quiet
690 * is twice the lanes open at or under width plus one, with no pass since the mover's last reading. The
691 * system's logical processors bound it: no more contexts than that run at once.
692 * @note rows are the rows of touches, one a lane and the last for the lanes past them, which
693 * mmgr_movable_touches reads.
694 * @note thread_started and thread_wait together are the mover's thread. With either unset, nothing moves a cell
695 * on its own, and a caller can still call mmgr_movable_buf_move. The member thread_id badges a pass
696 * that names no owner. The member thread_yield runs between a sort's moves, with the cellblock open.
697 * @note thread_wait answers EMBED_FALSE to end the mover. The member thread_moving, where set, hears as each
698 * sort's moves start and end. A mover at low priority can then raise itself across the copies a reader
699 * may wait on.
700 */
701typedef struct
702{
703 void *const storage; /**< Bytes for the option, zero as given [BORROWS]. */
704 const size_t storage_bytes; /**< Bytes at storage, MMGR_MOVABLE_STORAGE_BYTES at least. */
705 const size_t cells; /**< Movable cells the table can name at once. */
706 const size_t width; /**< The lanes open bound the mover reads quiet under, one at the fewest. */
707 const size_t rows; /**< Rows of touches, one a lane, the last shared by the lanes past it. */
708 embed_bool (*const thread_started)(void (*run)(void *context), void *context); /**< Starts the mover. */
709 size_t (*const thread_id)(void); /**< The id of the thread that calls, for a pass that names no owner. */
710 embed_bool (*const thread_wait)(uint32_t milliseconds); /**< Sleeps the mover, EMBED_FALSE to end it. */
711 void (*const thread_yield)(void); /**< Gives up the processor between a sort's moves, where set. */
712 void (*const thread_moving)(embed_bool moving); /**< Told as each move starts and ends, where set. */
714
715/**
716 * @brief Arguments for the calls that reach a movable cell's bytes, each of which reads the members it names.
717 *
718 * @note A check holds offset and bytes against the cell, and a span past its end fails.
719 */
720typedef struct
721{
722 const CarcerMovableEntry *const cell; /**< The cell [BORROWS]. */
723 const CarcerMovableEntry *const other_cell; /**< The cell compare orders it against, NULL for src. */
724 const size_t offset; /**< Where in the cell the call starts. */
725 const size_t other_offset; /**< Where in other_cell compare starts. */
726 void *const dst; /**< Where read copies to [BORROWS]. */
727 const void *const src; /**< What write copies from and compare orders against [BORROWS]. */
728 const size_t bytes; /**< Bytes the call covers. */
730
731/** @brief Bytes of the option's state ahead of its turnstiles, which locus_carcerum.c asserts it fits. */
732#define MMGR_MOVABLE_HEAD_BYTES 192u
733
734/** @brief The bits of one word of the option's claim bits: one an entry. */
735#define MMGR_MOVABLE_CLAIM_BITS ((size_t)(8u * sizeof(embed_atomic_usize)))
736
737/**
738 * @brief The storage the movable option takes for a table of cells_ entries and rows_ rows of touches.
739 *
740 * @param[in] cells_ Movable cells the table can name at once.
741 * @param[in] width_ The lanes open bound the mover reads quiet under, which takes no storage.
742 * @param[in] rows_ Rows of touches.
743 * @note A constant expression, so that a pool declaration can use this size. The size covers the state, one
744 * word an entry, one claim bit an entry in whole words, and two words a row.
745 */
746#define MMGR_MOVABLE_STORAGE_BYTES(cells_, width_, rows_) \
747 (MMGR_MOVABLE_HEAD_BYTES + \
748 (((cells_) + (((cells_) + MMGR_MOVABLE_CLAIM_BITS - 1u) / MMGR_MOVABLE_CLAIM_BITS) + (2u * (rows_))) * \
749 sizeof(embed_atomic_usize)))
750
751/**
752 * @brief Offset just past the last persistent cell.
753 *
754 * @param[in] series Cellblock to read [BORROWS].
755 * @return The persistent end, in bytes.
756 * @note The end as the word and its frame hold it, read under the epoch as one pair. A trim a give left owed on the
757 * open word does not run here; mmgr_buf_available and the next take run it.
758 */
759EMBED_HOST_DEVICE size_t mmgr_series_cellularum_persistent_end(const SeriesCellularum *series);
760
761/**
762 * @brief Offset of the lowest temporary cell.
763 *
764 * @param[in] series Cellblock to read [BORROWS].
765 * @return The temporary top, in bytes.
766 * @note The top as the word and its frame hold it, read under the epoch as one pair. A trim a give left owed on the
767 * open word does not run here; mmgr_buf_available and the next take run it.
768 */
769EMBED_HOST_DEVICE size_t mmgr_series_cellularum_temporary_top(const SeriesCellularum *series);
770
771/**
772 * @brief The banks a cellblock's bytes take, as its closed word holds the count.
773 *
774 * @param[in] series Cellblock to read [BORROWS].
775 * @return The bank count, 0 over a hospes pool not yet placed.
776 */
777EMBED_HOST_DEVICE EMBED_INLINE size_t mmgr_series_cellularum_banks(const SeriesCellularum *series)
778{
779 // Explicit cast reads the field as the count it holds
780 return (size_t)((EMBED_CALL(embed_atomic_word_table.load, EmbedAtomicWordLoadCfg, .word = &series->closed) >>
783}
784
785/**
786 * @brief The bytes one region's stack holds from its end down.
787 *
788 * @param[in] series Cellblock to read [BORROWS].
789 * @param[in] region The region, below MMGR_CARCER_REGION_COUNT; the large region is the last.
790 * @return The depth, in bytes, 0 where the temporary tier is one stack.
791 */
792EMBED_HOST_DEVICE EMBED_INLINE size_t mmgr_series_cellularum_region_depth(const SeriesCellularum *series, size_t region)
793{
794#if MMGR_CARCER_REGIONS > 0u
795 // Explicit cast reads the word as the bytes it counts
796 return (size_t)EMBED_CALL(embed_atomic_usize_table.load, EmbedAtomicUsizeLoadCfg,
797 .word = &series->regions[region].depth);
798#else
799 (void)series;
800 (void)region;
801 return 0u;
802#endif
803}
804
805/**
806 * @brief Whether every region is empty.
807 *
808 * @param[in] series Cellblock to read [BORROWS].
809 * @return EMBED_TRUE where no region holds a cell, and where the temporary tier is one stack.
810 */
811EMBED_HOST_DEVICE EMBED_INLINE embed_bool mmgr_series_cellularum_regions_idle(const SeriesCellularum *series)
812{
813#if MMGR_CARCER_REGIONS > 0u
814 size_t held = 0u;
815 size_t region;
816
817 for (region = 0u; region < MMGR_CARCER_REGION_COUNT; region++)
818 {
819 held |= mmgr_series_cellularum_region_depth(series, region);
820 }
821 // Explicit cast narrows the int result of the test to the embed_bool container
822 return (embed_bool)(held == 0u);
823#else
824 // A cellblock with no region has no loop to run: MMGR_CARCER_REGION_COUNT is 0.
825 (void)series;
826 return EMBED_TRUE;
827#endif
828}
829
830/**
831 * @brief Whether a cellblock has no cell out.
832 *
833 * @param[in] series Cellblock to read [BORROWS].
834 * @return EMBED_TRUE where both tiers are empty.
835 */
836EMBED_HOST_DEVICE EMBED_INLINE embed_bool mmgr_series_cellularum_idle(const SeriesCellularum *series)
837{
838 // Explicit cast narrows the int result of the test to the embed_bool container. A cellblock over a hospes pool
839 // not yet placed has size 0 and no cell out
840 return (embed_bool)((series->size == 0u) ||
841 ((mmgr_series_cellularum_persistent_end(series) == 0u) &&
844}
845
846/**
847 * @brief The furthest point of a cellblock's persistent tier so far.
848 *
849 * @param[in] series Cellblock to read [BORROWS].
850 * @return The bytes, 0 where the build tracks none (MMGR_ENABLE_HW_MEM_CAPACITY_CB).
851 */
852EMBED_HOST_DEVICE EMBED_INLINE size_t mmgr_series_cellularum_persistent_hw(const SeriesCellularum *series)
853{
854#if MMGR_ENABLE_HW_MEM_CAPACITY_CB
855 // Explicit cast reads the word as the bytes it counts
856 return (size_t)EMBED_CALL(embed_atomic_usize_table.load, EmbedAtomicUsizeLoadCfg, .word = &series->persistent_hw);
857#else
858 (void)series;
859 return 0u;
860#endif
861}
862
863/**
864 * @brief The max bytes a cellblock's temporary tier took from the top at one time.
865 *
866 * @param[in] series Cellblock to read [BORROWS].
867 * @return The bytes, 0 where the build tracks none (MMGR_ENABLE_HW_MEM_CAPACITY_CB).
868 */
869EMBED_HOST_DEVICE EMBED_INLINE size_t mmgr_series_cellularum_temporary_hw(const SeriesCellularum *series)
870{
871#if MMGR_ENABLE_HW_MEM_CAPACITY_CB
872 // Explicit cast reads the word as the bytes it counts
873 return (size_t)EMBED_CALL(embed_atomic_usize_table.load, EmbedAtomicUsizeLoadCfg, .word = &series->temporary_hw);
874#else
875 (void)series;
876 return 0u;
877#endif
878}
879
880/**
881 * @brief The two movable members both guards hold ahead of their movable release.
882 *
883 * @note movable_init assigns the movable option, once. movable_buf_alloc takes a cell a move may move: its identity,
884 * NULL where refused.
885 */
886#define MMGR_GUARD_MOVABLE_TAKE_MEMBERS \
887 embed_bool (*movable_init)(const CarcerMovableInitCfg *args); \
888 const CarcerMovableEntry *(*movable_buf_alloc)(size_t size, const CellOwner *owner);
889
890/**
891 * @brief The four movable members both guards hold after their movable release.
892 *
893 * @note movable_buf_read copies out of a movable cell, and movable_buf_write copies into it. movable_buf_compare
894 * orders a movable cell's bytes. movable_buf_measure counts the bytes before a movable cell's next zero.
895 */
896#define MMGR_GUARD_MOVABLE_SPAN_MEMBERS \
897 embed_bool (*movable_buf_read)(const CarcerMovableCfg *args); \
898 embed_bool (*movable_buf_write)(const CarcerMovableCfg *args); \
899 embed_iword (*movable_buf_compare)(const CarcerMovableCfg *args); \
900 size_t (*movable_buf_measure)(const CarcerMovableCfg *args);
901
902/**
903 * @brief The thirteen member names of both guards, in their order, for EMBED_TABLE_LAYOUT.
904 */
905#define MMGR_GUARD_MEMBER_NAMES \
906 persistent_buf_alloc, persistent_buf_release, temporary_buf_alloc, who_owns_buf, buf_available, buf_grow, \
907 movable_init, movable_buf_alloc, movable_buf_release, movable_buf_read, movable_buf_write, \
908 movable_buf_compare, movable_buf_measure
909
910/**
911 * @brief The minimum security guard over one cellblock: a released cell retains whatever is in it.
912 *
913 * @note EMBED_TABLE_LAYOUT asserts the thirteen members sit at consecutive EMBED_FUNCTION_POINTER_BYTES offsets,
914 * with nothing else.
915 * @note Every entry is bound to the cellblock its declaration named. No call carries a cellblock
916 * argument and there is nothing to pass wrongly. A cellblock at this level has no release that
917 * zeroes. The difference between the two levels is what exists, not what a caller remembers
918 * to reach for.
919 * @note A cell of either tier goes back the same way, through persistent_buf_release, which reads the tier off
920 * the cell's offset.
921 */
922typedef struct
923{
924 void *(*persistent_buf_alloc)(
925 size_t size,
926 const CellOwner *owner); /**< Takes size bytes from the bottom for owner, unzeroed [RETURNS OWNERSHIP]. */
928 void *prisoner,
929 const CellOwner *owner); /**< Releases owner's prisoner of either tier, cell left as it is [TAKES OWNERSHIP]. */
930 void *(*temporary_buf_alloc)(
931 size_t size,
932 const CellOwner *owner); /**< Takes size bytes from the top for owner, unzeroed [RETURNS OWNERSHIP]. */
933 embed_bool (*who_owns_buf)(const void *at); /**< Whether at lies in this cellblock's bytes [BORROWS]. */
934 size_t (*buf_available)(void); /**< Bytes between the two tiers. */
935 void *(*buf_grow)(
936 void *prisoner, size_t size,
937 const CellOwner *owner); /**< Grows the newest cell of its tier to size bytes in place, unzeroed. */
939 embed_bool (*movable_buf_release)(const CarcerMovableEntry *cell,
940 const CellOwner *owner); /**< Releases owner's movable cell, left as it is. */
944
945/**
946 * @brief The max security guard over one cellblock: every release zeroes the cell first.
947 *
948 * @note EMBED_TABLE_LAYOUT asserts the thirteen members sit at consecutive EMBED_FUNCTION_POINTER_BYTES offsets,
949 * with nothing else.
950 * @note The same thirteen entries, and no unzeroed release among them. Each zero fill runs before the
951 * cell's header goes free: the bytes are zero at the instant they become available.
952 */
953typedef struct
954{
955 void *(*persistent_buf_alloc)(
956 size_t size, const CellOwner *owner); /**< Takes size bytes from the bottom for owner [RETURNS OWNERSHIP]. */
958 void *prisoner,
959 const CellOwner *owner); /**< Zeroes owner's cell of either tier, then releases it [TAKES OWNERSHIP]. */
960 void *(*temporary_buf_alloc)(
961 size_t size, const CellOwner *owner); /**< Takes size bytes from the top for owner [RETURNS OWNERSHIP]. */
962 embed_bool (*who_owns_buf)(const void *at); /**< Whether at lies in this cellblock's bytes [BORROWS]. */
963 size_t (*buf_available)(void); /**< Bytes between the two tiers. */
964 void *(*buf_grow)(void *prisoner, size_t size,
965 const CellOwner *owner); /**< Grows the newest cell of its tier to size bytes in place, the added
966 bytes zeroed. */
968 embed_bool (*movable_buf_release)(const CarcerMovableEntry *cell,
969 const CellOwner *owner); /**< Zeroes owner's movable cell, then releases it. */
972EMBED_TABLE_LAYOUT(MaxSecurityGuard, MMGR_GUARD_MEMBER_NAMES);
973
974/**
975 * @brief The movable entries of one cellblock, bound to it through series_cellularum_.
976 *
977 * @param[in] prisonsite_ Site whose name every entry symbol carries.
978 * @param[in] name_ Pool whose name the symbols carry alongside the site's.
979 * @param[in] wipe_to_zero_on_release_ Security flag, which the option retains for its releases and moves.
980 * @param[in] series_cellularum_ The cellblock's record, as a pointer expression.
981 */
982#define MMGR_CARCER_MOVABLE_ENTRIES(prisonsite_, name_, wipe_to_zero_on_release_, series_cellularum_) \
983 EMBED_HOST_DEVICE static embed_bool prisonsite_##_##name_##_movable_init(const CarcerMovableInitCfg *args) \
984 { \
985 return mmgr_movable_init((series_cellularum_), args, (wipe_to_zero_on_release_)); \
986 } \
987 EMBED_HOST_DEVICE static const CarcerMovableEntry *prisonsite_##_##name_##_movable_buf_alloc( \
988 size_t size, const CellOwner *owner) \
989 { \
990 return mmgr_movable_buf_alloc((series_cellularum_), size, owner); \
991 } \
992 EMBED_HOST_DEVICE static embed_bool prisonsite_##_##name_##_movable_buf_release(const CarcerMovableEntry *cell, \
993 const CellOwner *owner) \
994 { \
995 return mmgr_movable_buf_release((series_cellularum_), cell, owner); \
996 } \
997 EMBED_HOST_DEVICE static embed_bool prisonsite_##_##name_##_movable_buf_read(const CarcerMovableCfg *args) \
998 { \
999 return mmgr_movable_buf_read((series_cellularum_), args); \
1000 } \
1001 EMBED_HOST_DEVICE static embed_bool prisonsite_##_##name_##_movable_buf_write(const CarcerMovableCfg *args) \
1002 { \
1003 return mmgr_movable_buf_write((series_cellularum_), args); \
1004 } \
1005 EMBED_HOST_DEVICE static embed_iword prisonsite_##_##name_##_movable_buf_compare(const CarcerMovableCfg *args) \
1006 { \
1007 return mmgr_movable_buf_compare((series_cellularum_), args); \
1008 } \
1009 EMBED_HOST_DEVICE static size_t prisonsite_##_##name_##_movable_buf_measure(const CarcerMovableCfg *args) \
1010 { \
1011 return mmgr_movable_buf_measure((series_cellularum_), args); \
1012 }
1013
1014/**
1015 * @brief Everything one cellblock is: its bytes, its state, and the entries bound to it.
1016 *
1017 * @param[in] prisonsite_ Site of the cellblock, which every emitted symbol carries.
1018 * @param[in] type_ MinimumSecurityGuard or MaxSecurityGuard, read by MMGR_CARCER_MEM.
1019 * @param[in] name_ Pool under the cellblock, which is also its name as a member of its site
1020 * [BORROWS].
1021 * @param[in] wipe_to_zero_on_release_ 1 where a release zeroes the cell first, 0 where it does not.
1022 * @param[in] hospes_ 1 where the pool is a hospes pool the program places at run time, 0 where its
1023 * storage is the array its declaration emitted.
1024 * @note The site name is part of every symbol. Two sites may each hold a cellblock of the same name,
1025 * at different security levels, with no collision.
1026 * @note wipe_to_zero_on_release_ is a literal: the branch it guards folds away and the security
1027 * level costs nothing to read. hospes_ is a literal as well: it selects the record's initializer.
1028 * @note The extent comes from the pool two ways. The count name_##_bytes is the one its declaration
1029 * received and is what the record carries, and sizeof is what the compiler positioned down. The
1030 * asserts test the second: they say something the first cannot say about itself. A hospes pool has
1031 * neither: its record starts empty, and mmgr_series_cellularum_placed tests the extent it is given.
1032 * @warning A pool too small to hold one cell fails the build. Only the alignment is a power of two,
1033 * and nothing reads the size as a mask. A bad size is a mistake the language would
1034 * otherwise accept. A wrong name or guard type fails on its own. Two pools are separate
1035 * objects: they cannot share an address and there is no overlap to check.
1036 * @note No upper bound. The pool states how much storage the cellblock gets, and nothing in the
1037 * library sizes anything from a ceiling over it.
1038 */
1039#define MMGR_CARCER_BODY(prisonsite_, type_, name_, wipe_to_zero_on_release_, hospes_) \
1040 MMGR_PARS_CLAIMED_ONCE(name_); \
1041 EMBED_CAT(MMGR_CARCER_STORAGE_, hospes_)(prisonsite_, name_) MMGR_ALLOC_SIZE(1) EMBED_HOST_DEVICE static void * \
1042 prisonsite_##_##name_##_persistent_buf_alloc(size_t size, const CellOwner *owner) \
1043 { \
1044 if (wipe_to_zero_on_release_) \
1045 { \
1046 return mmgr_persistent_max_security_buf_alloc(&prisonsite_##_##name_##_ctx, size, owner); \
1047 } \
1048 return mmgr_persistent_buf_alloc(&prisonsite_##_##name_##_ctx, size, owner); \
1049 } \
1050 EMBED_HOST_DEVICE static void prisonsite_##_##name_##_persistent_buf_release(void *prisoner, \
1051 const CellOwner *owner) \
1052 { \
1053 if (wipe_to_zero_on_release_) \
1054 { \
1055 mmgr_persistent_max_security_buf_release(&prisonsite_##_##name_##_ctx, prisoner, owner); \
1056 } \
1057 else \
1058 { \
1059 mmgr_persistent_buf_release(&prisonsite_##_##name_##_ctx, prisoner, owner); \
1060 } \
1061 } \
1062 MMGR_ALLOC_SIZE(1) \
1063 EMBED_HOST_DEVICE static void *prisonsite_##_##name_##_temporary_buf_alloc(size_t size, const CellOwner *owner) \
1064 { \
1065 if (wipe_to_zero_on_release_) \
1066 { \
1067 return mmgr_temporary_max_security_buf_alloc(&prisonsite_##_##name_##_ctx, size, owner); \
1068 } \
1069 return mmgr_temporary_buf_alloc(&prisonsite_##_##name_##_ctx, size, owner); \
1070 } \
1071 EMBED_HOST_DEVICE static embed_bool prisonsite_##_##name_##_who_owns_buf(const void *at) \
1072 { \
1073 return mmgr_who_owns_buf(&prisonsite_##_##name_##_ctx, at); \
1074 } \
1075 EMBED_HOST_DEVICE static size_t prisonsite_##_##name_##_buf_available(void) \
1076 { \
1077 const size_t available = mmgr_buf_available(&prisonsite_##_##name_##_ctx); \
1078 return available; \
1079 } \
1080 MMGR_ALLOC_SIZE(2) \
1081 EMBED_HOST_DEVICE static void *prisonsite_##_##name_##_buf_grow(void *prisoner, size_t size, \
1082 const CellOwner *owner) \
1083 { \
1084 if (wipe_to_zero_on_release_) \
1085 { \
1086 return mmgr_max_security_buf_grow(&prisonsite_##_##name_##_ctx, prisoner, size, owner); \
1087 } \
1088 return mmgr_buf_grow(&prisonsite_##_##name_##_ctx, prisoner, size, owner); \
1089 } \
1090 MMGR_CARCER_MOVABLE_ENTRIES(prisonsite_, name_, wipe_to_zero_on_release_, &prisonsite_##_##name_##_ctx)
1091
1092/**
1093 * @brief A cellblock's record over the array its pool's declaration emitted: the asserts on the array's extent, and
1094 * the record with the array as its base and the pool's count as its size.
1095 *
1096 * @param[in] prisonsite_ Site of the cellblock, which the record's symbol carries.
1097 * @param[in] name_ Pool under the cellblock.
1098 */
1099#define MMGR_CARCER_STORAGE_0(prisonsite_, name_) \
1100 EMBED_STATIC_ASSERT(sizeof(mmgr_pars_storage_##name_) >= (MMGR_CARCER_REGION_AREA + (2u * MMGR_CARCER_ALIGN)), \
1101 #prisonsite_ "." #name_ " is too small for its regions and one cell"); \
1102 EMBED_STATIC_ASSERT(sizeof(mmgr_pars_storage_##name_) <= MMGR_CARCER_POOL_BYTES_MAX, \
1103 #prisonsite_ "." #name_ " is larger than the closed word's banks can name"); \
1104 static EMBED_PASS_MEMORY SeriesCellularum prisonsite_##_##name_##_ctx = \
1105 MMGR_CARCER_STATE(mmgr_pars_storage_##name_, name_##_bytes);
1106
1107/**
1108 * @brief A cellblock's record over a hospes pool: empty until the program places it.
1109 *
1110 * @param[in] prisonsite_ Site of the cellblock, which the record's symbol carries.
1111 * @param[in] name_ Pool under the cellblock.
1112 */
1113#define MMGR_CARCER_STORAGE_1(prisonsite_, name_) \
1114 static EMBED_PASS_MEMORY SeriesCellularum prisonsite_##_##name_##_ctx = MMGR_CARCER_STATE_HOSPES;
1115
1116/**
1117 * @brief One cellblock as a member of its site's type.
1118 *
1119 * @param[in] prisonsite_ Site of the cellblock, unused here.
1120 * @param[in] type_ MinimumSecurityGuard or MaxSecurityGuard, the member's type.
1121 * @param[in] name_ Pool under the cellblock, and the name of the member.
1122 * @param[in] wipe_to_zero_on_release_ Security flag, unused here.
1123 * @param[in] hospes_ Hospes flag, unused here.
1124 * @note Takes all five because the three readers share one tuple shape, as MMGR_MINIMUM_SECURITY
1125 * describes.
1126 */
1127#define MMGR_CARCER_MEM(prisonsite_, type_, name_, wipe_to_zero_on_release_, hospes_) type_ name_;
1128
1129/**
1130 * @brief One cellblock's entries, in the order its guard declares them.
1131 *
1132 * @param[in] prisonsite_ Site whose name every entry symbol carries.
1133 * @param[in] type_ Guard type, unused here.
1134 * @param[in] name_ Pool whose name the symbols carry alongside the site's.
1135 * @param[in] wipe_to_zero_on_release_ Security flag, unused here.
1136 * @param[in] hospes_ Hospes flag, unused here.
1137 * @note MinimumSecurityGuard and MaxSecurityGuard declare the same thirteen members in the same
1138 * order. One initializer serves either level.
1139 */
1140#define MMGR_CARCER_SEAT(prisonsite_, type_, name_, wipe_to_zero_on_release_, hospes_) \
1141 {prisonsite_##_##name_##_persistent_buf_alloc, prisonsite_##_##name_##_persistent_buf_release, \
1142 prisonsite_##_##name_##_temporary_buf_alloc, prisonsite_##_##name_##_who_owns_buf, \
1143 prisonsite_##_##name_##_buf_available, prisonsite_##_##name_##_buf_grow, \
1144 prisonsite_##_##name_##_movable_init, prisonsite_##_##name_##_movable_buf_alloc, \
1145 prisonsite_##_##name_##_movable_buf_release, prisonsite_##_##name_##_movable_buf_read, \
1146 prisonsite_##_##name_##_movable_buf_write, prisonsite_##_##name_##_movable_buf_compare, \
1147 prisonsite_##_##name_##_movable_buf_measure},
1148
1149/**
1150 * @brief Declares a minimum security cellblock over a pool.
1151 *
1152 * @param[in] name_ Pool under the cellblock, which is also its name as a member of its site
1153 * [BORROWS].
1154 * @note The pool carries its own extent: no size appears here. A cellblock is a layer over bytes
1155 * that already exist. Which of ParsMemoriaeInternae or ParsMemoriaeExternum declared the
1156 * bytes settles where they live.
1157 * @note Expands to a tuple in place of code. The site reads the same list three times, once for
1158 * the cellblocks' bodies, once for its own members, once for their entries. An element has to
1159 * stay data until the site says which of the three readings it gets.
1160 * @note The tuple's last element says whether the pool is a hospes pool: 0 here, the storage the array
1161 * the pool's declaration emitted.
1162 */
1163#define MMGR_MINIMUM_SECURITY(name_) (MinimumSecurityGuard, name_, 0, 0)
1164
1165/**
1166 * @brief Declares a max security cellblock over a pool.
1167 *
1168 * @param[in] name_ Pool under the cellblock, which is also its name as a member of its site
1169 * [BORROWS].
1170 * @note The 1 after the name is the wipe flag MMGR_CARCER_BODY reads to pick the release that zeroes on
1171 * both tiers. The choice of this over MMGR_MINIMUM_SECURITY settles a cellblock's level, and
1172 * nothing after the declaration can change it.
1173 * @note Carries no size, for the reason MMGR_MINIMUM_SECURITY gives.
1174 */
1175#define MMGR_MAXIMUM_SECURITY(name_) (MaxSecurityGuard, name_, 1, 0)
1176
1177/**
1178 * @brief Declares a minimum security cellblock over a hospes pool, one ParsMemoriaeHospes declared: the program
1179 * gives the storage once, with mmgr_series_cellularum_placed, before the first take.
1180 *
1181 * @param[in] name_ Hospes pool under the cellblock, which is also its name as a member of its site
1182 * [BORROWS].
1183 * @note The cellblock refuses every take until it is placed. The storage comes from the system, outside the
1184 * program's image, where a program wants the pages a take touched given back while no cell holds them.
1185 */
1186#define MMGR_MINIMUM_SECURITY_HOSPES(name_) (MinimumSecurityGuard, name_, 0, 1)
1187
1188/**
1189 * @brief Declares a max security cellblock over a hospes pool, as MMGR_MINIMUM_SECURITY_HOSPES with the wipe on
1190 * release.
1191 *
1192 * @param[in] name_ Hospes pool under the cellblock [BORROWS].
1193 */
1194#define MMGR_MAXIMUM_SECURITY_HOSPES(name_) (MaxSecurityGuard, name_, 1, 1)
1195
1196/**
1197 * @brief Strips a tuple's parentheses.
1198 *
1199 * @param[in] ... The tuple's elements, which its own parentheses deliver as this macro's arguments.
1200 * @return Those elements, comma separated, with nothing around them.
1201 * @note Written MMGR_UNTUPLE tuple_, with no parentheses of its own: the tuple supplies them.
1202 */
1203#define MMGR_UNTUPLE(...) __VA_ARGS__
1204
1205/**
1206 * @brief Applies one reader to one cellblock tuple, with the site spliced in ahead of it.
1207 *
1208 * @param[in] what_ Reader to apply: MMGR_CARCER_BODY, MMGR_CARCER_MEM or MMGR_CARCER_SEAT.
1209 * @param[in] prisonsite_ Site, which reaches the reader ahead of the tuple's own elements.
1210 * @param[in] tuple_ One MMGR_MINIMUM_SECURITY or MMGR_MAXIMUM_SECURITY tuple.
1211 * @return What the reader expands to.
1212 * @note Two steps, because the preprocessor counts a macro's arguments before it expands them. The inner call
1213 * lets the flattened tuple reach the reader as separate arguments.
1214 */
1215#define MMGR_CARCER_APPLY(what_, prisonsite_, tuple_) MMGR_CARCER_APPLY_(what_, prisonsite_, MMGR_UNTUPLE tuple_)
1216
1217/**
1218 * @brief Expands to what_(prisonsite_, __VA_ARGS__).
1219 *
1220 * @param[in] what_ Reader to apply.
1221 * @param[in] prisonsite_ Site, which reaches the reader ahead of the elements.
1222 * @param[in] ... The tuple's elements, already flattened by MMGR_UNTUPLE.
1223 * @return What the reader expands to.
1224 * @note The inner half of MMGR_CARCER_APPLY's two steps. The preprocessor expands its arguments before
1225 * it puts them in. MMGR_UNTUPLE runs here and the tuple's elements reach the reader as separate
1226 * arguments in place of one.
1227 */
1228#define MMGR_CARCER_APPLY_(what_, prisonsite_, ...) what_(prisonsite_, __VA_ARGS__)
1229
1230/**
1231 * @brief Applies the reader to a site's one cellblock tuple.
1232 *
1233 * @param[in] what_ Reader to apply: MMGR_CARCER_BODY, MMGR_CARCER_MEM or MMGR_CARCER_SEAT.
1234 * @param[in] prisonsite_ Site, which reaches the reader ahead of the tuple's own elements.
1235 * @param[in] SeriesCellularum1_ The cellblock's tuple.
1236 * @note The base of the expansion. Every longer line ends in this one, and it alone names no other.
1237 */
1238#define MMGR_CARCER_W1(what_, prisonsite_, SeriesCellularum1_) MMGR_CARCER_APPLY(what_, prisonsite_, SeriesCellularum1_)
1239
1240/**
1241 * @brief Applies the reader to a site's two cellblock tuples.
1242 *
1243 * @param[in] what_ Reader to apply.
1244 * @param[in] prisonsite_ Site, which reaches the reader with every tuple.
1245 * @param[in] SeriesCellularum1_ First cellblock's tuple, which MMGR_CARCER_W1 takes.
1246 * @param[in] SeriesCellularum2_ Second cellblock's tuple, applied after it.
1247 * @note The step every longer line repeats: expand the line one shorter, then apply the reader once
1248 * more. The preprocessor cannot step through a list: each cellblock count needs a line of its
1249 * own.
1250 */
1251#define MMGR_CARCER_W2(what_, prisonsite_, SeriesCellularum1_, SeriesCellularum2_) \
1252 MMGR_CARCER_W1(what_, prisonsite_, SeriesCellularum1_) \
1253 MMGR_CARCER_APPLY(what_, prisonsite_, SeriesCellularum2_)
1254
1255/**
1256 * @brief Applies the reader to a site's three cellblock tuples.
1257 *
1258 * @param[in] what_ Reader to apply.
1259 * @param[in] prisonsite_ Site, which reaches the reader with every tuple.
1260 * @param[in] SeriesCellularum1_ First cellblock's tuple.
1261 * @param[in] SeriesCellularum2_ Second cellblock's tuple.
1262 * @param[in] SeriesCellularum3_ Third cellblock's tuple, applied after MMGR_CARCER_W2 expands the first two.
1263 * @note MMGR_CARCER_WALK selects this line for a site that declares three cellblocks.
1264 */
1265#define MMGR_CARCER_W3(what_, prisonsite_, SeriesCellularum1_, SeriesCellularum2_, SeriesCellularum3_) \
1266 MMGR_CARCER_W2(what_, prisonsite_, SeriesCellularum1_, SeriesCellularum2_) \
1267 MMGR_CARCER_APPLY(what_, prisonsite_, SeriesCellularum3_)
1268
1269/**
1270 * @brief Applies the reader to a site's four cellblock tuples.
1271 *
1272 * @param[in] what_ Reader to apply.
1273 * @param[in] prisonsite_ Site, which reaches the reader with every tuple.
1274 * @param[in] SeriesCellularum1_ First cellblock's tuple.
1275 * @param[in] SeriesCellularum2_ Second cellblock's tuple.
1276 * @param[in] SeriesCellularum3_ Third cellblock's tuple.
1277 * @param[in] SeriesCellularum4_ Fourth cellblock's tuple, applied after MMGR_CARCER_W3 expands the first three.
1278 * @note MMGR_CARCER_WALK selects this line for a site that declares four cellblocks.
1279 */
1280#define MMGR_CARCER_W4(what_, prisonsite_, SeriesCellularum1_, SeriesCellularum2_, SeriesCellularum3_, \
1281 SeriesCellularum4_) \
1282 MMGR_CARCER_W3(what_, prisonsite_, SeriesCellularum1_, SeriesCellularum2_, SeriesCellularum3_) \
1283 MMGR_CARCER_APPLY(what_, prisonsite_, SeriesCellularum4_)
1284
1285/**
1286 * @brief Applies the reader to a site's five cellblock tuples.
1287 *
1288 * @param[in] what_ Reader to apply.
1289 * @param[in] prisonsite_ Site, which reaches the reader with every tuple.
1290 * @param[in] SeriesCellularum1_ First cellblock's tuple.
1291 * @param[in] SeriesCellularum2_ Second cellblock's tuple.
1292 * @param[in] SeriesCellularum3_ Third cellblock's tuple.
1293 * @param[in] SeriesCellularum4_ Fourth cellblock's tuple.
1294 * @param[in] SeriesCellularum5_ Fifth cellblock's tuple, applied after MMGR_CARCER_W4 expands the first four.
1295 * @note MMGR_CARCER_WALK selects this line for a site that declares five cellblocks.
1296 */
1297#define MMGR_CARCER_W5(what_, prisonsite_, SeriesCellularum1_, SeriesCellularum2_, SeriesCellularum3_, \
1298 SeriesCellularum4_, SeriesCellularum5_) \
1299 MMGR_CARCER_W4(what_, prisonsite_, SeriesCellularum1_, SeriesCellularum2_, SeriesCellularum3_, SeriesCellularum4_) \
1300 MMGR_CARCER_APPLY(what_, prisonsite_, SeriesCellularum5_)
1301
1302/**
1303 * @brief Applies the reader to a site's six cellblock tuples.
1304 *
1305 * @param[in] what_ Reader to apply.
1306 * @param[in] prisonsite_ Site, which reaches the reader with every tuple.
1307 * @param[in] SeriesCellularum1_ First cellblock's tuple.
1308 * @param[in] SeriesCellularum2_ Second cellblock's tuple.
1309 * @param[in] SeriesCellularum3_ Third cellblock's tuple.
1310 * @param[in] SeriesCellularum4_ Fourth cellblock's tuple.
1311 * @param[in] SeriesCellularum5_ Fifth cellblock's tuple.
1312 * @param[in] SeriesCellularum6_ Sixth cellblock's tuple, applied after MMGR_CARCER_W5 expands the first five.
1313 * @note MMGR_CARCER_WALK selects this line for a site that declares six cellblocks.
1314 */
1315#define MMGR_CARCER_W6(what_, prisonsite_, SeriesCellularum1_, SeriesCellularum2_, SeriesCellularum3_, \
1316 SeriesCellularum4_, SeriesCellularum5_, SeriesCellularum6_) \
1317 MMGR_CARCER_W5(what_, prisonsite_, SeriesCellularum1_, SeriesCellularum2_, SeriesCellularum3_, SeriesCellularum4_, \
1318 SeriesCellularum5_) \
1319 MMGR_CARCER_APPLY(what_, prisonsite_, SeriesCellularum6_)
1320
1321/**
1322 * @brief Applies the reader to a site's seven cellblock tuples.
1323 *
1324 * @param[in] what_ Reader to apply.
1325 * @param[in] prisonsite_ Site, which reaches the reader with every tuple.
1326 * @param[in] SeriesCellularum1_ First cellblock's tuple.
1327 * @param[in] SeriesCellularum2_ Second cellblock's tuple.
1328 * @param[in] SeriesCellularum3_ Third cellblock's tuple.
1329 * @param[in] SeriesCellularum4_ Fourth cellblock's tuple.
1330 * @param[in] SeriesCellularum5_ Fifth cellblock's tuple.
1331 * @param[in] SeriesCellularum6_ Sixth cellblock's tuple.
1332 * @param[in] SeriesCellularum7_ Seventh cellblock's tuple, applied after MMGR_CARCER_W6 expands the first six.
1333 * @note MMGR_CARCER_WALK selects this line for a site that declares seven cellblocks.
1334 */
1335#define MMGR_CARCER_W7(what_, prisonsite_, SeriesCellularum1_, SeriesCellularum2_, SeriesCellularum3_, \
1336 SeriesCellularum4_, SeriesCellularum5_, SeriesCellularum6_, SeriesCellularum7_) \
1337 MMGR_CARCER_W6(what_, prisonsite_, SeriesCellularum1_, SeriesCellularum2_, SeriesCellularum3_, SeriesCellularum4_, \
1338 SeriesCellularum5_, SeriesCellularum6_) \
1339 MMGR_CARCER_APPLY(what_, prisonsite_, SeriesCellularum7_)
1340
1341/**
1342 * @brief Applies the reader to a site's eight cellblock tuples.
1343 *
1344 * @param[in] what_ Reader to apply.
1345 * @param[in] prisonsite_ Site, which reaches the reader with every tuple.
1346 * @param[in] SeriesCellularum1_ First cellblock's tuple.
1347 * @param[in] SeriesCellularum2_ Second cellblock's tuple.
1348 * @param[in] SeriesCellularum3_ Third cellblock's tuple.
1349 * @param[in] SeriesCellularum4_ Fourth cellblock's tuple.
1350 * @param[in] SeriesCellularum5_ Fifth cellblock's tuple.
1351 * @param[in] SeriesCellularum6_ Sixth cellblock's tuple.
1352 * @param[in] SeriesCellularum7_ Seventh cellblock's tuple.
1353 * @param[in] SeriesCellularum8_ Eighth cellblock's tuple, applied after MMGR_CARCER_W7 expands the first seven.
1354 * @note MMGR_CARCER_WALK selects this line for a site that declares eight cellblocks.
1355 * @warning The last line written. A site that declares nine cellblocks pastes MMGR_CARCER_W9, which does
1356 * not exist. That line is the whole fix. Nothing else changes and no configured ceiling
1357 * has a part in it.
1358 */
1359#define MMGR_CARCER_W8(what_, prisonsite_, SeriesCellularum1_, SeriesCellularum2_, SeriesCellularum3_, \
1360 SeriesCellularum4_, SeriesCellularum5_, SeriesCellularum6_, SeriesCellularum7_, \
1361 SeriesCellularum8_) \
1362 MMGR_CARCER_W7(what_, prisonsite_, SeriesCellularum1_, SeriesCellularum2_, SeriesCellularum3_, SeriesCellularum4_, \
1363 SeriesCellularum5_, SeriesCellularum6_, SeriesCellularum7_) \
1364 MMGR_CARCER_APPLY(what_, prisonsite_, SeriesCellularum8_)
1365
1366/**
1367 * @brief Expands the walk that matches the cellblock count.
1368 *
1369 * @param[in] what_ Reader to apply: MMGR_CARCER_BODY, MMGR_CARCER_MEM or MMGR_CARCER_SEAT.
1370 * @param[in] prisonsite_ Site, forwarded to the walk ahead of the tuples.
1371 * @param[in] ... MMGR_MINIMUM_SECURITY and MMGR_MAXIMUM_SECURITY tuples, one per cellblock.
1372 * @note EMBED_CAT builds the line's name from EMBED_NARG's count of the tuples.
1373 * @warning EMBED_NARG gives 1 for an empty list. A site that declares no cellblocks reaches
1374 * MMGR_CARCER_W1 and fails there with too few arguments for the reader.
1375 */
1376#define MMGR_CARCER_WALK(what_, prisonsite_, ...) \
1377 EMBED_CAT(MMGR_CARCER_W, EMBED_NARG(__VA_ARGS__))(what_, prisonsite_, __VA_ARGS__)
1378
1379/**
1380 * @brief Declares a prison site and the cellblocks built at it.
1381 *
1382 * @param[in] prisonsite_ Name of the site. Its cellblocks are members of it.
1383 * @param[in] ... MMGR_MINIMUM_SECURITY and MMGR_MAXIMUM_SECURITY declarations, one each.
1384 * @note Everything it emits is data with its initial value. Nothing runs at startup, and a cellblock's first
1385 * byte is the address of its own storage, which the linker resolves.
1386 * @note The warden is the const struct this emits under the site's name. Every call reaches a
1387 * cellblock through it.
1388 * @note Every emitted symbol carries the site's name. A program may declare as many sites as it
1389 * likes and two of them may each hold a cellblock called the same thing.
1390 * @note A cellblock's entries are bound to that cellblock. No call names one and none can reach
1391 * another's bytes. A minimum security cellblock has no release that zeroes, and a max
1392 * security one has no plain release.
1393 * @warning The bytes, the state and the warden are all static. A declaration in a header gives
1394 * every translation unit that includes it a site of its own in place of one they share.
1395 */
1396#define LocusCarcerum(prisonsite_, ...) \
1397 MMGR_CARCER_WALK(MMGR_CARCER_BODY, prisonsite_, __VA_ARGS__) \
1398 EMBED_TABLE_STORAGE struct \
1399 { \
1400 MMGR_CARCER_WALK(MMGR_CARCER_MEM, prisonsite_, __VA_ARGS__) \
1401 } prisonsite_ EMBED_UNUSED = {MMGR_CARCER_WALK(MMGR_CARCER_SEAT, prisonsite_, __VA_ARGS__)}
1402
1403/**
1404 * @brief Takes size bytes from a cellblock's persistent tier.
1405 *
1406 * @param[in,out] series Cellblock to take from [BORROWS].
1407 * @param[in] size Bytes wanted.
1408 * @param[in] owner The registered owner that counts the cell [BORROWS].
1409 * @return Start of the cell, or NULL when owner is NULL or not registered or the
1410 * cellblock cannot meet the take [RETURNS OWNERSHIP].
1411 * @note Reached through the cellblock's own guard, never called by name.
1412 * @note A persistent cell is until the same cellblock's persistent_buf_release takes it.
1413 * @note A cell passed out adds its payload bytes, the request rounded to a word, to owner's in_use
1414 * and raises owner's hw to it. A refused take counts nothing.
1415 * @note The take is one pass through the cellblock's turnstiles and one fetch-add on the persistent end. The
1416 * cell is the bytes the bump covered, and its header and owner word go down once the take reads the
1417 * room off the top. A bump the gap cannot meet adds its bytes to the tier's overrun. The take then
1418 * closes the cellblock, drains the turnstiles and walks the tier alone.
1419 * @note The walk merges every run of free cells into one. It takes the first run that holds the take and
1420 * splits it where its rest holds a cell. It returns the free run at the end to the gap, and bumps the
1421 * gap where no run fits. Only then does the take fail, and no path branches on what another context
1422 * did.
1423 * @warning The bytes stay as they are, and a cell positioned over bytes a prisoner occupied before still
1424 * carries what that prisoner left. A cellblock declared MMGR_MAXIMUM_SECURITY zeroes, and it
1425 * zeroes on release.
1426 */
1427MMGR_ALLOC_SIZE(2) EMBED_HOST_DEVICE void *mmgr_persistent_buf_alloc(SeriesCellularum *series, size_t size, const CellOwner *owner);
1428
1429/**
1430 * @brief Takes size bytes from a max security cellblock's persistent tier.
1431 *
1432 * @param[in,out] series Cellblock to take from [BORROWS].
1433 * @param[in] size Bytes wanted.
1434 * @param[in] owner The registered owner that counts the cell [BORROWS].
1435 * @return As mmgr_persistent_buf_alloc answers [RETURNS OWNERSHIP].
1436 * @note The take mmgr_persistent_buf_alloc makes. A max security cellblock zeroes on release.
1437 */
1439EMBED_HOST_DEVICE void *mmgr_persistent_max_security_buf_alloc(SeriesCellularum *series, size_t size, const CellOwner *owner);
1440
1441/**
1442 * @brief Releases a prisoner and leaves the cell's bytes as they are.
1443 *
1444 * @param[in,out] series Cellblock the prisoner came from [BORROWS].
1445 * @param[in] prisoner First byte of the cell [TAKES OWNERSHIP].
1446 * @param[in] owner The owner the cell counts to, as its owner word names it [BORROWS].
1447 * @note The cell's offset against the persistent end says which tier it lies in. A NULL prisoner or a
1448 * NULL owner changes nothing.
1449 * @note The give is one fetch-or on the cell's owner word, which sets its completion bit, and one store on the
1450 * cell's header, which goes free. The cell's payload bytes, read off its header, come off owner's in_use
1451 * in the context whose fetch-or found the bit clear. A release that names an owner other than the cell's
1452 * changes nothing, and mmgr_carcer_hw_misnamed hears of it. One of a cell given back already changes
1453 * nothing either. No pass and no wait: the cell's words are its holder's alone.
1454 * @note A free cell stays in its tier for the compaction that a take the gap cannot meet runs. The compaction
1455 * merges it with its free neighbors, takes it where it fits, or returns it to the gap where it reaches
1456 * the end.
1457 * @warning A prisoner outside this cellblock's own bytes stops the program with MMGR_FATAL: there is no state
1458 * to carry on from.
1459 * @warning prisoner is dead once this returns and its bytes stay as they are. The cellblock may hand
1460 * them out again.
1461 * @warning The bound is the cellblock's storage and never a cell boundary. An address inside these
1462 * bytes that is not the first byte of a cell is still read as a cell's word.
1463 */
1464EMBED_HOST_DEVICE void mmgr_persistent_buf_release(SeriesCellularum *series, void *prisoner, const CellOwner *owner);
1465
1466/**
1467 * @brief Zeroes a cell, then releases the prisoner.
1468 *
1469 * @param[in,out] series Cellblock the prisoner came from [BORROWS].
1470 * @param[in,out] prisoner First byte of the cell, zeroed before release [TAKES OWNERSHIP].
1471 * @param[in] owner The owner the cell counts to, as its owner word names it [BORROWS].
1472 * @note The extent comes from the cell's own header. A caller cannot under-zero a cell with a count of
1473 * fewer bytes than it carries. A NULL prisoner or a NULL owner changes nothing.
1474 * @note The zero fill is the only step that separates this from mmgr_persistent_buf_release. It runs
1475 * once the owner named proves to be the cell's and the cell still counts. A release that names
1476 * another owner, or one of a cell given back already, zeroes nothing.
1477 * @warning A prisoner outside this cellblock's own bytes stops the program with MMGR_FATAL.
1478 * @warning prisoner is dead once this returns. The cellblock may hand those bytes out again.
1479 * @warning The bound is the cellblock's storage and never a cell boundary. An address inside these
1480 * bytes that is not the first byte of a cell still gets its extent from the bytes ahead
1481 * of it. The zero fill then runs for whatever length those hold.
1482 */
1483EMBED_HOST_DEVICE void mmgr_persistent_max_security_buf_release(SeriesCellularum *series, void *prisoner, const CellOwner *owner);
1484
1485/**
1486 * @brief Grows a cell to size payload bytes in place, where it is the newest cell of its tier.
1487 *
1488 * @param[in,out] series Cellblock the prisoner came from [BORROWS].
1489 * @param[in,out] prisoner First byte of the cell [BORROWS].
1490 * @param[in] size Payload bytes the cell is to carry, rounded as a take rounds them.
1491 * @param[in] owner The owner the cell counts to, as its owner word names it, which also counts
1492 * the growth [BORROWS].
1493 * @return The cell's first byte after the growth: the prisoner itself for a persistent
1494 * cell, or for one that carries size already. A temporary cell, whose bytes
1495 * moved down, gets a lower address. NULL with nothing changed where the cell is
1496 * not the newest of its tier, since a later take is above it. NULL also
1497 * where the gap cannot meet the growth. A NULL prisoner, a NULL owner, or an
1498 * owner other than the cell's returns NULL. The wrong owner reaches
1499 * mmgr_carcer_hw_misnamed [RETURNS OWNERSHIP].
1500 * @note The growth closes the cellblock, drains the turnstiles and works alone. The newest cell of a tier is
1501 * the cell its boundary is against. That is the persistent end at the cell's end, or the temporary
1502 * top at the cell's header. The growth reads the gap exactly, stores the boundary past the growth, and
1503 * moves no other cell. A caller whose growth fails takes a larger cell, copies into it and releases
1504 * the first, as it would without this entry.
1505 * @note The added bytes are as they lie, and mmgr_max_security_buf_grow zeroes them. A persistent growth's
1506 * hold the word that was behind the payload, a temporary growth's the cell's own old header and
1507 * payload.
1508 * @warning The prisoner's address is dead after a temporary growth that answers a lower one: the
1509 * answered address is the cell's from then on. A prisoner outside this cellblock's own
1510 * bytes stops with MMGR_FATAL, as a release does.
1511 */
1513EMBED_HOST_DEVICE void *mmgr_buf_grow(SeriesCellularum *series, void *prisoner, size_t size, const CellOwner *owner);
1514
1515/**
1516 * @brief Grows a cell in place as mmgr_buf_grow does, and zeroes the bytes the growth adds.
1517 *
1518 * @param[in,out] series Cellblock the prisoner came from [BORROWS].
1519 * @param[in,out] prisoner First byte of the cell [BORROWS].
1520 * @param[in] size Payload bytes the cell is to carry.
1521 * @param[in] owner The owner that counts a persistent cell's growth [BORROWS].
1522 * @return As mmgr_buf_grow answers [RETURNS OWNERSHIP].
1523 * @note The zero fill covers the growth's bytes alone: the payload the cell carried retains its bytes, moved
1524 * for a temporary cell.
1525 */
1527EMBED_HOST_DEVICE void *mmgr_max_security_buf_grow(SeriesCellularum *series, void *prisoner, size_t size, const CellOwner *owner);
1528
1529/**
1530 * @brief Takes size bytes from a cellblock's temporary tier.
1531 *
1532 * @param[in,out] series Cellblock to take from [BORROWS].
1533 * @param[in] size Bytes wanted.
1534 * @param[in] owner The registered owner that counts the cell [BORROWS].
1535 * @return Start of the cell, or NULL when owner is NULL or not registered or the
1536 * cellblock cannot meet the take [RETURNS OWNERSHIP].
1537 * @note The caller gives the cell back through mmgr_persistent_buf_release, as with a persistent cell,
1538 * which takes the bytes off owner. The cell carries owner in the word behind its payload.
1539 * @note The take is mmgr_persistent_buf_alloc's with the arithmetic mirrored. It is one pass through the
1540 * cellblock's turnstiles and one fetch-add on the temporary top, which moves it down by the cell. The
1541 * take reads the room off the persistent end, and a bump that crosses it adds its bytes to the tier's
1542 * overrun. The take then closes the cellblock, drains the turnstiles and walks the tier alone.
1543 * @note The walk merges every run of free cells into one. It takes the run that fits nearest the cellblock's
1544 * size and splits it where its rest holds a cell. It returns the free run at the top to the gap, and
1545 * bumps the top where no run fits. Only then does the take fail.
1546 * @warning The bytes stay as they are, and an allocation returns whatever the last prisoner left. A
1547 * cellblock declared MMGR_MAXIMUM_SECURITY zeroes, and it zeroes on release.
1548 */
1549EMBED_HOST_DEVICE void *mmgr_temporary_buf_alloc(SeriesCellularum *series, size_t size, const CellOwner *owner);
1550
1551/**
1552 * @brief Takes size bytes from a max security cellblock's temporary tier.
1553 *
1554 * @param[in,out] series Cellblock to take from [BORROWS].
1555 * @param[in] size Bytes wanted.
1556 * @param[in] owner The registered owner that counts the cell [BORROWS].
1557 * @return As mmgr_temporary_buf_alloc answers [RETURNS OWNERSHIP].
1558 * @note The take mmgr_temporary_buf_alloc makes. A max security cellblock zeroes on release.
1559 */
1561EMBED_HOST_DEVICE void *mmgr_temporary_max_security_buf_alloc(SeriesCellularum *series, size_t size,
1562 const CellOwner *owner);
1563
1564/**
1565 * @brief Whether at lies inside the cellblock's bytes.
1566 *
1567 * @param[in] series Cellblock to test against [BORROWS].
1568 * @param[in] at Address to test [BORROWS].
1569 * @return EMBED_TRUE when at lies in the cellblock's storage, which is [base, base + size).
1570 * @note One unsigned compare covers both ends, since an address below base wraps to a difference
1571 * larger than any size.
1572 * @warning Any address in the cellblock answers true, the first byte of a cell or any other. This says
1573 * where an address is and nothing of what is there. A true answer is not a warrant for a
1574 * release of at. The releases read a cell's word from whatever address they receive.
1575 */
1576EMBED_HOST_DEVICE embed_bool mmgr_who_owns_buf(const SeriesCellularum *series, const void *at);
1577
1578/**
1579 * @brief The owner a cell counts to, read off the word behind its payload.
1580 *
1581 * @param[in] series Cellblock the prisoner came from [BORROWS].
1582 * @param[in] prisoner First byte of the cell [BORROWS].
1583 * @return The registered owner the cell's owner word names. NULL where prisoner is NULL, lies
1584 * outside the cellblock, is a cell no context holds, or its word names no registered owner
1585 * [BORROWS].
1586 * @note A holder that grows or gives back a cell another part of the program took names the owner this
1587 * answers. Such a line grows in the passes of whoever writes it.
1588 * @warning The bound is the cellblock's storage and never a cell boundary, as for a release. An address
1589 * inside these bytes that is not the first byte of a cell reads as a cell's word.
1590 */
1591EMBED_HOST_DEVICE const CellOwner *mmgr_owner_of_buf(const SeriesCellularum *series, const void *prisoner);
1592
1593/**
1594 * @brief The bytes between the two tiers.
1595 *
1596 * @param[in,out] series Cellblock to read [BORROWS].
1597 * @return The temporary top less the persistent end, read off one word.
1598 * @note An allocation out of that gap needs a cell header from the same bytes, and the request
1599 * rounds up to a whole word first. The gap cannot meet a request of exactly this many bytes.
1600 * @note The trims a give left owed on the open word run first, and the gap read has every owed trim run.
1601 * @warning The answer is a snapshot. Any allocation at all leaves it stale before the caller can act
1602 * on it.
1603 */
1604EMBED_HOST_DEVICE size_t mmgr_buf_available(SeriesCellularum *series);
1605
1606/**
1607 * @brief Zeroes size bytes at prisoner and releases nothing.
1608 *
1609 * @param[in,out] prisoner First byte to clear [BORROWS].
1610 * @param[in] size Bytes to clear.
1611 * @note The stores are volatile: the compiler cannot drop a clear of bytes nothing reads afterwards
1612 * as dead work. The middle goes a word at a time, the two edges a byte at a time.
1613 * @note The extent is the caller's to state. A release that zeroes takes it from the cell's own word.
1614 * @warning prisoner must be writable for size bytes.
1615 */
1616EMBED_HOST_DEVICE void mmgr_zero_buf(void *prisoner, size_t size);
1617
1618/**
1619 * @brief Rounds size up to a whole machine word.
1620 *
1621 * @param[in] size Count to round.
1622 * @return size rounded up to a multiple of MMGR_CARCER_ALIGN, and size itself when it already
1623 * is one.
1624 * @note A size of 0 rounds to 0. The allocators do not round a request this way. They carry a
1625 * request of 0 up to one word first: no cell goes out empty.
1626 * @warning The round adds MMGR_CARCER_ALIGN - 1 before the mask. A size within a word of SIZE_MAX wraps
1627 * to 0.
1628 */
1629EMBED_HOST_DEVICE size_t mmgr_align_up_buf(size_t size);
1630
1631/**
1632 * @brief Puts a cellblock back to the state of its declaration. Both tiers are empty, the closed word and
1633 * the turnstiles at rest, the option unassigned, and no byte of its storage touched.
1634 *
1635 * @param[in,out] series Cellblock to empty [BORROWS].
1636 * @note Every cell the cellblock passed out is dead once this returns. A suite calls it between
1637 * cases over one declared cellblock. A program with a cellblock to empty while another context
1638 * reaches it has a cellblock whose cells that context still holds.
1639 * @note Nothing outside the tiers reads as a cell, and so no cell's words change.
1640 */
1641EMBED_HOST_DEVICE void mmgr_series_cellularum_emptied(SeriesCellularum *series);
1642
1643/**
1644 * @brief Places a cellblock declared over a hospes pool on the storage the program gives: its base and its size
1645 * set, and the cellblock emptied over them.
1646 *
1647 * @param[in,out] series Cellblock to place [BORROWS].
1648 * @param[in] storage First byte of the storage, aligned to MMGR_ALIGN_BYTES [BORROWS].
1649 * @param[in] bytes Bytes of the storage.
1650 * @return EMBED_TRUE where the cellblock is placed. EMBED_FALSE where storage is NULL, or the bytes
1651 * are fewer than the regions and one cell need or more than MMGR_CARCER_POOL_BYTES_MAX.
1652 * Then nothing changed, and the cellblock refuses every take as before.
1653 * @note Called once, at a time no context reaches the cellblock, before its first take; the program keeps the
1654 * storage for as long as the cellblock lives. A cellblock over an array its declaration emitted has its base
1655 * from the linker and is never placed. theory/locus_carcerum/LocusCarcerum/Idle.lean proves that a cellblock
1656 * of size 0 refuses every take.
1657 */
1658EMBED_HOST_DEVICE embed_bool mmgr_series_cellularum_placed(SeriesCellularum *series, uint8_t *storage, size_t bytes);
1659
1660/** @brief A span of a cellblock's storage no cell holds, passed to the program: where it starts and its bytes. */
1661typedef void (*CarcerIdleEach)(void *context, void *at, size_t bytes);
1662
1663/**
1664 * @brief Passes each span of the cellblock's storage that no cell holds to `each`, with the cellblock closed: the
1665 * gap between the tiers, and the room of each region above its depth.
1666 *
1667 * @param[in,out] series Cellblock to read [BORROWS].
1668 * @param[in] each The call for each span, run with the cellblock closed.
1669 * @param[in] context What `each` is passed first [BORROWS].
1670 * @note The program gives a span's pages back to the system where it wishes, and the next take that reaches them
1671 * reads what a page the system gives afresh holds. No cell's words lie in a span, and no take or give runs
1672 * while the cellblock is closed, so a span the call reads is idle for as long as the call runs.
1673 * theory/locus_carcerum/LocusCarcerum/Idle.lean proves that no byte of a span lies in a cell.
1674 * @note A free cell inside a tier, under a busy one, is no span: its words are the tier's.
1675 */
1676EMBED_HOST_DEVICE void mmgr_series_cellularum_idle_each(SeriesCellularum *series, CarcerIdleEach each, void *context);
1677
1678/**
1679 * @brief Counts bytes that lie in no cellblock to an owner, as a ring's or a page's are.
1680 *
1681 * @param[in] owner The registered owner [BORROWS].
1682 * @param[in] bytes The bytes.
1683 * @note An owner's counts are the program's total, and memory a program takes outside a
1684 * cellblock belongs in it. No cellblock's own total moves.
1685 */
1686EMBED_HOST_DEVICE void mmgr_owner_bytes_taken(const CellOwner *owner, size_t bytes);
1687
1688/**
1689 * @brief Takes bytes mmgr_owner_bytes_taken counted off their owner.
1690 *
1691 * @param[in] owner The owner that counted them [BORROWS].
1692 * @param[in] bytes The bytes.
1693 */
1694EMBED_HOST_DEVICE void mmgr_owner_bytes_given(const CellOwner *owner, size_t bytes);
1695
1696/**
1697 * @brief A cellblock's fragmentation: the bytes its persistent tier occupies and no cell's payload holds, as
1698 * a percentage of the bytes it occupies.
1699 *
1700 * @param[in] series Cellblock to read [BORROWS].
1701 * @return The percentage, 0 to 100, read against the hundredth of the bytes occupied rounded up, and 0
1702 * below a hundred bytes occupied.
1703 * @note Read off the persistent end and the tier's total, with no walk. Each cell's words count among
1704 * the bytes no payload holds, and a tier of small cells reads some fragmentation with no free cell.
1705 * @note The temporary tier reads nothing: its cells last one call, and its top comes back as they end. A
1706 * free cell no movable cell is above is no work for a mover either. The mover remembers a sort
1707 * that finds nothing to move, and does not run it again until the reading rises.
1708 */
1709EMBED_HOST_DEVICE size_t mmgr_series_cellularum_fragmentation(const SeriesCellularum *series);
1710
1711/**
1712 * @brief Assigns a cellblock's movable option: the translation table, its claim bits, the rows of touches and
1713 * the mover's thread. A cellblock's guard calls this, as movable_init.
1714 *
1715 * @param[in,out] series Cellblock the option serves [BORROWS].
1716 * @param[in] args The storage, the sizes and the thread's calls [BORROWS].
1717 * @param[in] wipes Whether a cell a move leaves or a release gives back gets a zero fill first.
1718 * @return EMBED_TRUE where the option is on and, where the caller gave a thread, the
1719 * thread started. EMBED_FALSE where it is on already, the storage is short or a
1720 * size is zero, nothing changed. EMBED_FALSE also where the thread did not
1721 * start, which leaves the option on and nothing to move a cell.
1722 * @note Assigned once, at a time no context reaches a movable cell. The cellblock's turnstiles are what the
1723 * mover reads as occupancy: the lanes open against the option's width.
1724 */
1725EMBED_HOST_DEVICE embed_bool mmgr_movable_init(SeriesCellularum *series, const CarcerMovableInitCfg *args,
1726 embed_bool wipes);
1727
1728/**
1729 * @brief Takes a persistent cell a move may move, and names it in the table.
1730 *
1731 * @param[in,out] series Cellblock to take from [BORROWS].
1732 * @param[in] size Bytes wanted.
1733 * @param[in] owner The registered owner that counts the cell [BORROWS].
1734 * @return The cell's identity. NULL where the option is off, the cellblock cannot meet
1735 * the take or the table has no free entry, nothing counted.
1736 * @note The identity is a const pointer to the cell's entry. It is for the cell's life whatever moves
1737 * the cell, and the movable_buf_ calls are the only way from it to the bytes.
1738 * @warning A pointer retained past mmgr_movable_buf_release names an entry a later take may reuse.
1739 */
1740EMBED_HOST_DEVICE const CarcerMovableEntry *mmgr_movable_buf_alloc(SeriesCellularum *series, size_t size, const CellOwner *owner);
1741
1742/**
1743 * @brief Gives a movable cell back and frees its entry.
1744 *
1745 * @param[in,out] series Cellblock the cell is in [BORROWS].
1746 * @param[in] cell The cell [BORROWS].
1747 * @param[in] owner The owner that counts the cell [BORROWS].
1748 * @return EMBED_TRUE where it went back. EMBED_FALSE where cell is NULL, the entry is free or
1749 * owner is not the cell's, of which mmgr_carcer_hw_misnamed hears, nothing changed.
1750 * @note One pass through the cellblock's turnstiles: the entry read, the cell given back as a release does,
1751 * the entry's word and claim bit cleared. A move is a writer, which drains the passes before it and
1752 * closes the cellblock to those after. The two never reach the entry at once.
1753 */
1754EMBED_HOST_DEVICE embed_bool mmgr_movable_buf_release(SeriesCellularum *series, const CarcerMovableEntry *cell, const CellOwner *owner);
1755
1756/**
1757 * @brief Copies args->bytes from args->offset of a movable cell to args->dst.
1758 *
1759 * @param[in,out] series Cellblock the cell is in [BORROWS].
1760 * @param[in] args The cell, offset, dst and bytes [BORROWS].
1761 * @return EMBED_TRUE where copied, EMBED_FALSE where the span passes the cell's end or
1762 * the entry is free, nothing copied.
1763 * @note Thin: one pass through the cellblock's turnstiles, badged with the id of the thread that calls. The
1764 * entry's word read, the bytes copied, and the lane let go. A pass that finds the cellblock closed to a move
1765 * backs off one step and tries once more.
1766 */
1767EMBED_HOST_DEVICE embed_bool mmgr_movable_buf_read(const SeriesCellularum *series, const CarcerMovableCfg *args);
1768
1769/**
1770 * @brief Copies args->bytes from args->src to args->offset of a movable cell.
1771 *
1772 * @param[in,out] series Cellblock the cell is in [BORROWS].
1773 * @param[in] args The cell, offset, src and bytes [BORROWS].
1774 * @return As mmgr_movable_buf_read answers.
1775 * @note Atomic against a move and not against another writer: two contexts that write the same bytes order
1776 * themselves.
1777 */
1778EMBED_HOST_DEVICE embed_bool mmgr_movable_buf_write(SeriesCellularum *series, const CarcerMovableCfg *args);
1779
1780/**
1781 * @brief Orders args->bytes of a movable cell against args->src, or against another cell where
1782 * args->other_cell holds one.
1783 *
1784 * @param[in,out] series Cellblock the cell is in [BORROWS].
1785 * @param[in] args The cell, offset, and src or other_cell and other_offset, and bytes [BORROWS].
1786 * @return As memor.cmp orders them, and 1 where a span passes its cell's end or an entry is
1787 * free.
1788 * @note Two cells are the max one call reaches, both read under the one pass.
1789 */
1790EMBED_HOST_DEVICE embed_iword mmgr_movable_buf_compare(const SeriesCellularum *series, const CarcerMovableCfg *args);
1791
1792/**
1793 * @brief The bytes from args->offset of a movable cell to the next zero byte.
1794 *
1795 * @param[in,out] series Cellblock the cell is in [BORROWS].
1796 * @param[in] args The cell and offset [BORROWS].
1797 * @return The bytes, or the bytes to the cell's end where no zero is before it.
1798 */
1799EMBED_HOST_DEVICE size_t mmgr_movable_buf_measure(const SeriesCellularum *series, const CarcerMovableCfg *args);
1800
1801/**
1802 * @brief Moves one movable cell into the first free cell below it, highest cell first.
1803 *
1804 * @param[in,out] series Cellblock whose cells to move [BORROWS].
1805 * @return EMBED_TRUE where a cell moved. EMBED_FALSE where none could at this reading: no
1806 * free run fits below any of them.
1807 * @note The mover's step, a writer's. It closes the cellblock, drains the turnstiles, and reads the highest
1808 * named cell. It walks the persistent tier below it for the first free run that holds the cell. It
1809 * copies the cell, carries its owner word over, flips the entry to the copy, frees the cell left behind,
1810 * and opens the cellblock. The same state moves the same cell to the same place.
1811 * @note Called until it answers EMBED_FALSE, the cells are as low as the free runs let them. The free run
1812 * that reaches the tier's end returns to the gap with each walk.
1813 * @note The copy carries the cell's owner word, and so the owner's count is as it was through the move.
1814 * @note A cell no free run below takes rules out every cell at least its size under it. Those look
1815 * through the same free runs for as much room.
1816 * @warning Each cell moved, and each smaller size refused, costs a read of the named part of the table.
1817 * It costs a walk of the tier below the cell too.
1818 */
1819EMBED_HOST_DEVICE embed_bool mmgr_movable_buf_move(SeriesCellularum *series);
1820
1821/**
1822 * @brief The mover: what the thread the program gave runs, until its thread_wait answers EMBED_FALSE.
1823 *
1824 * @param[in,out] series Cellblock with the option assigned and thread_wait given [BORROWS].
1825 * @note Each MMGR_MOVABLE_READ_MS it reads the cellblock, and fragmentation under MMGR_MOVABLE_SETTLE_PERCENT
1826 * ends the reading. Over it, MMGR_MOVABLE_QUIET_READINGS in a row must find the cellblock quiet. Quiet
1827 * is twice the lanes its turnstiles have open at or under the option's width plus one, with no pass
1828 * since the last reading. It then moves cells one at a time, the cellblock open and the processor
1829 * yielded between moves. It reads the same after each: a pass, or lanes open past quiet, says the moves
1830 * meet pressure, and it backs off.
1831 * @note Each backoff doubles the wait, up to MMGR_MOVABLE_BACKOFF_MAX_MS. A sort that ends with nothing left
1832 * to move resets the wait, and the mover remembers the fragmentation it left. Nothing runs again until
1833 * the reading rises past that or falls under the threshold first.
1834 */
1835EMBED_HOST_DEVICE void mmgr_movable_run(SeriesCellularum *series);
1836
1837/**
1838 * @brief The contexts queued at the cellblock's turnstiles now: those that found every open lane busy and wait for
1839 * a lane.
1840 *
1841 * @param[in] series Cellblock to read [BORROWS].
1842 * @return The queued contexts, 0 without the option.
1843 */
1844EMBED_HOST_DEVICE size_t mmgr_movable_queue_depth(const SeriesCellularum *series);
1845
1846/**
1847 * @brief One row of touches a lane, counted as passes go through it.
1848 *
1849 * @param[in,out] series Cellblock to read [BORROWS].
1850 * @param[in] at The row, from 0: the lane of the same place.
1851 * @param[out] id The id of the last badge through the lane. That is an owner's place, or a thread's id
1852 * plus one for a pass that names no owner [BORROWS].
1853 * @param[out] touches The passes through the lane so far [BORROWS].
1854 * @return EMBED_TRUE where the row holds a lane's passes, EMBED_FALSE past the rows in use or
1855 * for a lane with no pass through it yet.
1856 * @note A telemetry row, counted by one add as each pass badges in, and nothing decides on it. The option's last
1857 * row counts every lane past the rest, and answers an id of SIZE_MAX.
1858 */
1859EMBED_HOST_DEVICE embed_bool mmgr_movable_touches(const SeriesCellularum *series, size_t at, size_t *id,
1860 size_t *touches);
1861
1862EMBED_END_DECLS
1863
1864/** @} */
1865
1866#endif
EMBED_PASS_DECLARE(const uint8_t, mmgr_ancorae_impensa_table, [256])
Cost of each byte value under the profile this build links, indexed by the byte itself.
static EMBED_HOST_DEVICE size_t mmgr_series_cellularum_temporary_hw(const SeriesCellularum *series)
The max bytes a cellblock's temporary tier took from the top at one time.
EMBED_HOST_DEVICE embed_bool mmgr_movable_buf_read(const SeriesCellularum *series, const CarcerMovableCfg *args)
Copies args->bytes from args->offset of a movable cell to args->dst.
EMBED_HOST_DEVICE void mmgr_movable_run(SeriesCellularum *series)
The mover: what the thread the program gave runs, until its thread_wait answers EMBED_FALSE.
#define MMGR_GUARD_MOVABLE_TAKE_MEMBERS
The two movable members both guards hold ahead of their movable release.
EMBED_HOST_DEVICE embed_bool mmgr_movable_buf_write(SeriesCellularum *series, const CarcerMovableCfg *args)
Copies args->bytes from args->src to args->offset of a movable cell.
EMBED_HOST_DEVICE embed_bool mmgr_movable_touches(const SeriesCellularum *series, size_t at, size_t *id, size_t *touches)
One row of touches a lane, counted as passes go through it.
EMBED_HOST_DEVICE void mmgr_series_cellularum_emptied(SeriesCellularum *series)
Puts a cellblock back to the state of its declaration. Both tiers are empty, the closed word and the ...
EMBED_HOST_DEVICE void * mmgr_temporary_max_security_buf_alloc(SeriesCellularum *series, size_t size, const CellOwner *owner)
Takes size bytes from a max security cellblock's temporary tier.
EMBED_HOST_DEVICE void mmgr_zero_buf(void *prisoner, size_t size)
Zeroes size bytes at prisoner and releases nothing.
void(*) CarcerIdleEach(void *context, void *at, size_t bytes)
A span of a cellblock's storage no cell holds, passed to the program: where it starts and its bytes.
EMBED_HOST_DEVICE void mmgr_persistent_max_security_buf_release(SeriesCellularum *series, void *prisoner, const CellOwner *owner)
Zeroes a cell, then releases the prisoner.
EMBED_HOST_DEVICE void * mmgr_temporary_buf_alloc(SeriesCellularum *series, size_t size, const CellOwner *owner)
Takes size bytes from a cellblock's temporary tier.
static EMBED_HOST_DEVICE size_t mmgr_owner_hw(const CellOwner *owner)
The max payload bytes an owner has had at once.
EMBED_HOST_DEVICE void mmgr_owner_bytes_given(const CellOwner *owner, size_t bytes)
Takes bytes mmgr_owner_bytes_taken counted off their owner.
EMBED_HOST_DEVICE void mmgr_owner_bytes_taken(const CellOwner *owner, size_t bytes)
Counts bytes that lie in no cellblock to an owner, as a ring's or a page's are.
EMBED_HOST_DEVICE uint64_t mmgr_carcer_hw_clock_ns(void)
The library's clock in nanoseconds, which a cell's owner word carries the take on.
EMBED_HOST_DEVICE const CarcerMovableEntry * mmgr_movable_buf_alloc(SeriesCellularum *series, size_t size, const CellOwner *owner)
Takes a persistent cell a move may move, and names it in the table.
static EMBED_HOST_DEVICE size_t mmgr_series_cellularum_banks(const SeriesCellularum *series)
The banks a cellblock's bytes take, as its closed word holds the count.
EMBED_HOST_DEVICE embed_bool mmgr_series_cellularum_placed(SeriesCellularum *series, uint8_t *storage, size_t bytes)
Places a cellblock declared over a hospes pool on the storage the program gives: its base and its siz...
#define MMGR_CARCER_REGION_BYTES
Bytes of each region: its cells with their headers and owner words. A multiple of MMGR_CARCER_ALIGN.
EMBED_HOST_DEVICE embed_iword mmgr_movable_buf_compare(const SeriesCellularum *series, const CarcerMovableCfg *args)
Orders args->bytes of a movable cell against args->src, or against another cell where args->other_cel...
EMBED_HOST_DEVICE embed_bool mmgr_owner_listed(const CellOwner *owner)
Whether an owner is one of the table's.
static EMBED_HOST_DEVICE size_t mmgr_series_cellularum_region_depth(const SeriesCellularum *series, size_t region)
The bytes one region's stack holds from its end down.
#define MMGR_OWNER_PLACE_BITS
The owner word a cell carries behind its payload, one machine word positioned as fields,...
EMBED_HOST_DEVICE size_t mmgr_buf_available(SeriesCellularum *series)
The bytes between the two tiers.
EMBED_HOST_DEVICE embed_bool mmgr_who_owns_buf(const SeriesCellularum *series, const void *at)
Whether at lies inside the cellblock's bytes.
EMBED_HOST_DEVICE const CellOwner * mmgr_owner_of_buf(const SeriesCellularum *series, const void *prisoner)
The owner a cell counts to, read off the word behind its payload.
#define MMGR_GUARD_MEMBER_NAMES
The thirteen member names of both guards, in their order, for EMBED_TABLE_LAYOUT.
EMBED_HOST_DEVICE embed_bool mmgr_movable_buf_release(SeriesCellularum *series, const CarcerMovableEntry *cell, const CellOwner *owner)
Gives a movable cell back and frees its entry.
EMBED_HOST_DEVICE void mmgr_carcer_hw_misnamed(const CellOwner *named, const CellOwner *cell_owner, size_t bytes)
Told of each give or growth that names an owner other than the one the cell counts to.
#define MMGR_CARCER_LARGE_REGION_BYTES
Bytes of the one large region, at the top of the cellblock, that every temporary take over a region's...
EMBED_HOST_DEVICE void * mmgr_persistent_max_security_buf_alloc(SeriesCellularum *series, size_t size, const CellOwner *owner)
Takes size bytes from a max security cellblock's persistent tier.
#define MMGR_CARCER_ALIGN
Alignment of every prisoner on the way out: one machine word, or the atomic size word's alignment whe...
EMBED_HOST_DEVICE embed_bool mmgr_movable_init(SeriesCellularum *series, const CarcerMovableInitCfg *args, embed_bool wipes)
Assigns a cellblock's movable option: the translation table, its claim bits, the rows of touches and ...
static EMBED_HOST_DEVICE size_t mmgr_series_cellularum_persistent_hw(const SeriesCellularum *series)
The furthest point of a cellblock's persistent tier so far.
EMBED_HOST_DEVICE size_t mmgr_movable_buf_measure(const SeriesCellularum *series, const CarcerMovableCfg *args)
The bytes from args->offset of a movable cell to the next zero byte.
EMBED_HOST_DEVICE size_t mmgr_align_up_buf(size_t size)
Rounds size up to a whole machine word.
#define MMGR_CARCER_REGION_COUNT
The regions in all: one for each place, and the large one where the build declares it.
EMBED_HOST_DEVICE size_t mmgr_movable_queue_depth(const SeriesCellularum *series)
The contexts queued at the cellblock's turnstiles now: those that found every open lane busy and wait...
struct CarcerMovableEntry CarcerMovableEntry
The identity a movable cell's taker holds: a const pointer to the cell's entry in the cellblock's tra...
EMBED_HOST_DEVICE void mmgr_persistent_buf_release(SeriesCellularum *series, void *prisoner, const CellOwner *owner)
Releases a prisoner and leaves the cell's bytes as they are.
#define MMGR_GUARD_MOVABLE_SPAN_MEMBERS
The four movable members both guards hold after their movable release.
EMBED_HOST_DEVICE void * mmgr_buf_grow(SeriesCellularum *series, void *prisoner, size_t size, const CellOwner *owner)
Grows a cell to size payload bytes in place, where it is the newest cell of its tier.
EMBED_HOST_DEVICE embed_bool mmgr_movable_buf_move(SeriesCellularum *series)
Moves one movable cell into the first free cell below it, highest cell first.
#define MMGR_CARCER_BANK_FIELD_MASK
The mask of one field, at bit 0.
EMBED_HOST_DEVICE size_t mmgr_series_cellularum_fragmentation(const SeriesCellularum *series)
A cellblock's fragmentation: the bytes its persistent tier occupies and no cell's payload holds,...
EMBED_HOST_DEVICE size_t mmgr_series_cellularum_temporary_top(const SeriesCellularum *series)
Offset of the lowest temporary cell.
static EMBED_HOST_DEVICE embed_bool mmgr_series_cellularum_idle(const SeriesCellularum *series)
Whether a cellblock has no cell out.
EMBED_HOST_DEVICE void * mmgr_max_security_buf_grow(SeriesCellularum *series, void *prisoner, size_t size, const CellOwner *owner)
Grows a cell in place as mmgr_buf_grow does, and zeroes the bytes the growth adds.
static EMBED_HOST_DEVICE embed_bool mmgr_series_cellularum_regions_idle(const SeriesCellularum *series)
Whether every region is empty.
#define MMGR_CARCER_REGION_AREA
Bytes the regions take off the top of every cellblock, 0 where the temporary tier is one stack.
EMBED_HOST_DEVICE void * mmgr_persistent_buf_alloc(SeriesCellularum *series, size_t size, const CellOwner *owner)
Takes size bytes from a cellblock's persistent tier.
EMBED_HOST_DEVICE size_t mmgr_series_cellularum_persistent_end(const SeriesCellularum *series)
Offset just past the last persistent cell.
EMBED_HOST_DEVICE void mmgr_series_cellularum_idle_each(SeriesCellularum *series, CarcerIdleEach each, void *context)
Passes each span of the cellblock's storage that no cell holds to each, with the cellblock closed: th...
#define MMGR_CARCER_OWNERS
The max owners a program's owner table holds.
static EMBED_HOST_DEVICE size_t mmgr_owner_in_use(const CellOwner *owner)
The payload bytes of the cells an owner has now.
#define MMGR_CARCER_BANK_COUNT_AT
The bit of the closed word that each field starts at.
The embedded_types headers every module builds on.
#define MMGR_ALIGN_BYTES
The settings of a build that gives no command line flag. This is the hook of config/mmgr_config....
Definition mmgr.h:69
#define MMGR_ALLOC_SIZE(arg_)
States which argument of an allocation entry gives the extent of what it returns.
Definition mmgr.h:125
Arguments for the calls that reach a movable cell's bytes, each of which reads the members it names.
const CarcerMovableEntry *const cell
const void *const src
const CarcerMovableEntry *const other_cell
const size_t offset
const size_t bytes
const size_t other_offset
What a program gives a cellblock for the movable option, once, through its guard's movable_init.
void(*const) thread_yield(void)
embed_bool(*const) thread_wait(uint32_t milliseconds)
void(*const) thread_moving(embed_bool moving)
embed_bool(*const) thread_started(void(*run)(void *context), void *context)
size_t(*const) thread_id(void)
const size_t storage_bytes
Asserts MMGR_CARCER_ALIGN is a power of two.
embed_atomic_usize hw
embed_atomic_usize in_use
const char * name
The max security guard over one cellblock: every release zeroes the cell first.
size_t(*) buf_available(void)
void(*) persistent_buf_release(void *prisoner, const CellOwner *owner)
embed_bool(*) who_owns_buf(const void *at)
embed_bool(*) movable_buf_release(const CarcerMovableEntry *cell, const CellOwner *owner)
The minimum security guard over one cellblock: a released cell retains whatever is in it.
void(*) persistent_buf_release(void *prisoner, const CellOwner *owner)
embed_bool(*) movable_buf_release(const CarcerMovableEntry *cell, const CellOwner *owner)
size_t(*) buf_available(void)
embed_bool(*) who_owns_buf(const void *at)
One cellblock's state: its bytes, the two boundaries that grow toward each other, and the closed word...
embed_atomic_word epoch
embed_atomic_uptr movable
embed_atomic_usize in_use
embed_atomic_usize sink
EmbedTurnstiles turnstiles
embed_atomic_usize persistent_overrun
embed_atomic_word closed
embed_atomic_usize persistent_in_use
embed_atomic_usize bounds
embed_atomic_usize temporary_overrun