MMgr v2.3.0
Zero-heap memory manager in C11. One borrowed buffer, carved by asserted offset.
Loading...
Searching...
No Matches
memoria_vector.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 memoria_vector.h
9 * @brief A vector behind a const token, with its elements in one movable cell, and its calls on many threads at once.
10 * @author dstroy0 (Douglas Quigg) <dquigg123@gmail.com>
11 * @date 2026-11-23
12 *
13 * @note The caller holds a guard and the token of the vector. The token is the address of the record, a const
14 * pointer that the linker resolves. The record holds the count, the movable cell of the elements, the
15 * capacity and one claim bit an element. No call gives back the cell or an address in it.
16 * @note Each call goes through the movable entries of the guard, which MMGR_VECTOR_THROUGH names. An element
17 * moves between the cell and a buffer of the caller, and never in place.
18 * @note at and set claim the bit of their element, then read the count. A push claims the bit of the element at
19 * the count, writes it, and one CAS takes the count up. A pop claims the bit of the last element, and one
20 * CAS takes the count down. LocusCarcerum/ConcurrentVector.lean proves these calls linearizable.
21 * @note A growth claims each bit below the count. It copies the elements a chunk at a time through a buffer on the
22 * stack, and it gives the old cell back. LocusCarcerum/Vector.lean proves that a relocation changes nothing
23 * that the caller can see.
24 * @note The ceiling bounds the capacity, and the vector refuses a push at the ceiling. A growth that the cellblock
25 * cannot meet refuses its push too.
26 */
27#ifndef MMGR_MEMORIA_VECTOR_H
28#define MMGR_MEMORIA_VECTOR_H
29
30/** @addtogroup mod_vector
31 * @{ */
32
33#include "mmgr.h"
34
36
37EMBED_BEGIN_DECLS
38
39/**
40 * @brief Bytes of the buffer on the stack that a growth copies the elements through, which a build may set before
41 * it includes this header.
42 *
43 * @note A growth copies the elements a chunk of these bytes at a time, and the last chunk takes the bytes left.
44 */
45#ifndef MMGR_VECTOR_CHUNK_BYTES
46
47#define MMGR_VECTOR_CHUNK_BYTES 64u
48#endif
49
50/** @brief Asserts that a growth copies at least one byte a chunk. */
51EMBED_STATIC_ASSERT(MMGR_VECTOR_CHUNK_BYTES > 0u, "MMGR_VECTOR_CHUNK_BYTES must be above 0");
52
53/** @brief The bits of one word of the claim bits of a vector: one an element. */
54#define MMGR_VECTOR_CLAIM_BITS ((size_t)(8u * sizeof(embed_atomic_usize)))
55
56/**
57 * @brief The words of claim bits for a ceiling of limit_ elements.
58 *
59 * @param[in] limit_ The ceiling of the capacity.
60 * @return One bit an element in whole words.
61 */
62#define MMGR_VECTOR_CLAIM_WORDS(limit_) (((size_t)(limit_) + MMGR_VECTOR_CLAIM_BITS - 1u) / MMGR_VECTOR_CLAIM_BITS)
63
64/**
65 * @brief The record of one vector, which its token names.
66 *
67 * @note The library's storage, positioned at compile time by MemoriaVector. Each member is MMgr's to write, and
68 * the caller reads none of them. The token is a const pointer to the record. It does not change while the
69 * program runs, however often a growth or a move puts the elements in another place.
70 * @note count is the word that a push and a pop change, each with one CAS. cell is the identity of the movable
71 * cell, as an integer, and 0 before the first push. capacity is the elements that the cell holds.
72 * @note claimed holds one bit an element. A call holds the bit of each element that it reads or writes, and so no
73 * two calls reach one element at once.
74 */
75typedef struct
76{
77 embed_atomic_usize count; /**< Elements in the vector. */
78 embed_atomic_uptr cell; /**< Identity of the movable cell of the elements, 0 where none. */
79 embed_atomic_usize capacity; /**< Elements that the cell holds. */
80 const size_t stride; /**< Bytes of one element. */
81 const size_t limit; /**< The ceiling of the capacity, which the claim bits cover. */
82 const CellOwner *const *const owner; /**< The owner that counts the cell [BORROWS]. */
83 embed_atomic_usize *const claimed; /**< One claim bit an element, in whole words [BORROWS]. */
85
86/**
87 * @brief Declares a vector of element_ with a ceiling of limit_ elements, its cell counted to owner_.
88 *
89 * @param[in] name_ The token of the vector: a const pointer to its record.
90 * @param[in] element_ The type of one element.
91 * @param[in] limit_ The ceiling of the capacity, a constant above 0.
92 * @param[in] owner_ The identity of the owner that counts the cell, as MMGR_OWNERS declares it [BORROWS].
93 * @note Emits the claim bits, the record and the token as data that the linker resolves. Nothing runs before the
94 * first call, and the vector starts with no element and no cell.
95 * @note The build fails where the ceiling is 0, where one cell of the ceiling passes MMGR_CARCER_BYTES_MAX, or
96 * where the alignment of the element does not divide MMGR_ALIGN_BYTES.
97 * @note EMBED_PASS_MEMORY gives each pass of nvcc its own claim bits, record and token. A kernel reaches the vector in
98 * device memory, and its owner_ is the device copy of the identity.
99 * @warning The vector names no cellblock. Each call names the guard that it goes through, and every call on one
100 * vector must name the same guard.
101 */
102#define MemoriaVector(name_, element_, limit_, owner_) \
103 EMBED_STATIC_ASSERT((size_t)(limit_) > 0u, #name_ " has a ceiling of 0 elements"); \
104 EMBED_STATIC_ASSERT((size_t)(limit_) <= (MMGR_CARCER_BYTES_MAX / sizeof(element_)), \
105 #name_ " has a ceiling whose cell passes MMGR_CARCER_BYTES_MAX"); \
106 EMBED_STATIC_ASSERT((MMGR_ALIGN_BYTES % EMBED_ALIGNOF(element_)) == 0u, \
107 #name_ " has an element whose alignment does not divide MMGR_ALIGN_BYTES"); \
108 static EMBED_PASS_MEMORY embed_atomic_usize mmgr_vector_claimed_##name_[MMGR_VECTOR_CLAIM_WORDS(limit_)]; \
109 static EMBED_PASS_MEMORY VectorRecord mmgr_vector_record_##name_ = { \
110 EMBED_ATOMIC_INIT(0u), EMBED_ATOMIC_INIT(0u), EMBED_ATOMIC_INIT(0u), sizeof(element_), \
111 (size_t)(limit_), &(owner_), mmgr_vector_claimed_##name_}; \
112 static EMBED_PASS_MEMORY VectorRecord *const name_ = &mmgr_vector_record_##name_
113
114/**
115 * @brief The movable entries of a guard, as the members of a MemoriaVectorCfg.
116 *
117 * @param[in] guard_ The guard of a cellblock with the movable option, MinimumSecurityGuard or MaxSecurityGuard.
118 * @note Both guards name the four entries the same way, so either one goes here.
119 */
120#define MMGR_VECTOR_THROUGH(guard_) \
121 .take = (guard_).movable_buf_alloc, .give = (guard_).movable_buf_release, .read = (guard_).movable_buf_read, \
122 .write = (guard_).movable_buf_write
123
124/**
125 * @brief Arguments for every mmgr_vector_ entry, each reading only the members it needs.
126 *
127 * @note Every entry reads vector, and every entry but mmgr_vector_size reads the four movable entries of the
128 * guard. at and set read index, set and push_back read src, and at and pop_back read dst.
129 * @note Members left unset are zero, and the entries that ignore them never read them.
130 */
131typedef struct
132{
133 VectorRecord *const vector; /**< The token of the vector [BORROWS]. */
134 const CarcerMovableEntry *(*const take)(size_t size, const CellOwner *owner); /**< The movable take of the guard. */
135 embed_bool (*const give)(const CarcerMovableEntry *cell,
136 const CellOwner *owner); /**< The movable release of the guard. */
137 embed_bool (*const read)(const CarcerMovableCfg *args); /**< The movable read of the guard. */
138 embed_bool (*const write)(const CarcerMovableCfg *args); /**< The movable write of the guard. */
139 const size_t index; /**< The element at and set reach. */
140 const void *const src; /**< The element set and push_back copy in [BORROWS]. */
141 void *const dst; /**< Where at and pop_back copy the element [BORROWS]. */
143
144/**
145 * @brief Type of the vector dispatch table.
146 *
147 * @note EMBED_TABLE_LAYOUT asserts the six members sit at consecutive EMBED_FUNCTION_POINTER_BYTES offsets, with
148 * nothing else.
149 */
150typedef struct
151{
152 embed_bool (*at)(const MemoriaVectorCfg *args); /**< Copies the element at index out. */
153 embed_bool (*set)(const MemoriaVectorCfg *args); /**< Copies an element in at index. */
154 embed_bool (*push_back)(const MemoriaVectorCfg *args); /**< Appends an element. */
155 embed_bool (*pop_back)(const MemoriaVectorCfg *args); /**< Removes the last element and copies it out. */
156 size_t (*size)(const MemoriaVectorCfg *args); /**< The count of elements. */
157 embed_bool (*release)(const MemoriaVectorCfg *args); /**< Gives the cell back and leaves no element. */
159EMBED_TABLE_LAYOUT(MemoriaVectorNs, at, set, push_back, pop_back, size, release);
160
161/**
162 * @brief Copies the element at args->index to args->dst.
163 *
164 * @param[in,out] args The vector, the guard, the index and dst [BORROWS].
165 * @return EMBED_TRUE where the element came out. EMBED_FALSE where args->index is at or past the count,
166 * and nothing changes.
167 * @note Claims the bit of the element, reads the count, copies the element, and gives the bit back. The read of
168 * the count is the point where the call takes effect.
169 * @warning args->dst must be writable for one element.
170 */
171EMBED_HOST_DEVICE embed_bool mmgr_vector_at(const MemoriaVectorCfg *args);
172
173/**
174 * @brief Copies the element at args->src to args->index.
175 *
176 * @param[in,out] args The vector, the guard, the index and src [BORROWS].
177 * @return EMBED_TRUE where the element went in. EMBED_FALSE where args->index is at or past the count,
178 * and nothing changes.
179 * @note Claims the bit of the element, reads the count, copies the element, and gives the bit back.
180 * @warning args->src must be readable for one element.
181 */
182EMBED_HOST_DEVICE embed_bool mmgr_vector_set(const MemoriaVectorCfg *args);
183
184/**
185 * @brief Appends the element at args->src.
186 *
187 * @param[in,out] args The vector, the guard and src [BORROWS].
188 * @return EMBED_TRUE where the element went in. EMBED_FALSE where the count is at the ceiling, where a
189 * growth found no cell, or where the guard refused the write, and nothing changes.
190 * @note Reads the count, claims the bit of the element at the count, and reads the count again. Where the cell is
191 * full, it grows the cell. It writes the element, and one CAS takes the count up by one, which is the point
192 * where the call takes effect.
193 * @note A CAS that finds another count gives the bit back and starts again, as does a claim of a held bit.
194 * @warning args->src must be readable for one element.
195 */
196EMBED_HOST_DEVICE embed_bool mmgr_vector_push_back(const MemoriaVectorCfg *args);
197
198/**
199 * @brief Removes the last element and copies it to args->dst.
200 *
201 * @param[in,out] args The vector, the guard and dst [BORROWS].
202 * @return EMBED_TRUE where an element came out. EMBED_FALSE where the vector has no element, and
203 * nothing changes.
204 * @note Reads the count, and claims the bit of the last element. One CAS takes the count down by one, which is the
205 * point where the call takes effect. Then it copies the element out and gives the bit back.
206 * @warning args->dst must be writable for one element.
207 * @warning A guard other than the guard of the cell can refuse the read after the CAS. The call then answers
208 * EMBED_FALSE, and the element is gone.
209 */
210EMBED_HOST_DEVICE embed_bool mmgr_vector_pop_back(const MemoriaVectorCfg *args);
211
212/**
213 * @brief Returns the count of elements.
214 *
215 * @param[in] args The vector [BORROWS].
216 * @return The count, off one word.
217 * @warning The answer is only a snapshot. A push or a pop on another thread can change it before the caller acts.
218 */
219EMBED_HOST_DEVICE size_t mmgr_vector_size(const MemoriaVectorCfg *args);
220
221/**
222 * @brief Gives the cell of the vector back, and leaves the vector with no element.
223 *
224 * @param[in,out] args The vector and the guard [BORROWS].
225 * @return EMBED_TRUE where the cell went back or the vector had none. EMBED_FALSE where the guard
226 * refused the release of the cell.
227 * @note Claims every bit, from the ceiling down, so no other call reaches an element meanwhile. A later push
228 * takes a new cell.
229 * @warning The claims are one for each element of the ceiling. A vector with a high ceiling pays that at each
230 * release.
231 */
232EMBED_HOST_DEVICE embed_bool mmgr_vector_release(const MemoriaVectorCfg *args);
233
234/**
235 * @brief Dispatch table instance named memoria_vector, whose members are the mmgr_vector_ entries.
236 */
237EMBED_TABLE_STORAGE MemoriaVectorNs memoria_vector EMBED_UNUSED = {
238 .at = mmgr_vector_at,
239 .set = mmgr_vector_set,
240 .push_back = mmgr_vector_push_back,
241 .pop_back = mmgr_vector_pop_back,
242 .size = mmgr_vector_size,
243 .release = mmgr_vector_release,
244};
245
246EMBED_END_DECLS
247
248/** @} */
249
250#endif
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 embed_bool mmgr_vector_set(const MemoriaVectorCfg *args)
Copies the element at args->src to args->index.
EMBED_HOST_DEVICE embed_bool mmgr_vector_release(const MemoriaVectorCfg *args)
Gives the cell of the vector back, and leaves the vector with no element.
EMBED_HOST_DEVICE embed_bool mmgr_vector_push_back(const MemoriaVectorCfg *args)
Appends the element at args->src.
static const MemoriaVectorNs memoria_vector
Dispatch table instance named memoria_vector, whose members are the mmgr_vector_ entries.
#define MMGR_VECTOR_CHUNK_BYTES
Bytes of the buffer on the stack that a growth copies the elements through, which a build may set bef...
EMBED_HOST_DEVICE embed_bool mmgr_vector_pop_back(const MemoriaVectorCfg *args)
Removes the last element and copies it to args->dst.
EMBED_HOST_DEVICE embed_bool mmgr_vector_at(const MemoriaVectorCfg *args)
Copies the element at args->index to args->dst.
EMBED_HOST_DEVICE size_t mmgr_vector_size(const MemoriaVectorCfg *args)
Returns the count of elements.
A prison site: a caller's storage divided into cellblocks, with the warden that holds them.
The embedded_types headers every module builds on.
Arguments for the calls that reach a movable cell's bytes, each of which reads the members it names.
Asserts MMGR_CARCER_ALIGN is a power of two.
Arguments for every mmgr_vector_ entry, each reading only the members it needs.
VectorRecord *const vector
const size_t index
embed_bool(*const) write(const CarcerMovableCfg *args)
const void *const src
embed_bool(*const) give(const CarcerMovableEntry *cell, const CellOwner *owner)
const CarcerMovableEntry *(*const) take(size_t size, const CellOwner *owner)
embed_bool(*const) read(const CarcerMovableCfg *args)
Type of the vector dispatch table.
embed_bool(*) pop_back(const MemoriaVectorCfg *args)
embed_bool(*) release(const MemoriaVectorCfg *args)
embed_bool(*) set(const MemoriaVectorCfg *args)
embed_bool(*) push_back(const MemoriaVectorCfg *args)
embed_bool(*) at(const MemoriaVectorCfg *args)
size_t(*) size(const MemoriaVectorCfg *args)
The record of one vector, which its token names.
const CellOwner *const *const owner
const size_t stride
embed_atomic_usize *const claimed
const size_t limit
embed_atomic_usize capacity
embed_atomic_usize count
embed_atomic_uptr cell