MMgr v2.3.0
Zero-heap memory manager in C11. One borrowed buffer, carved by asserted offset.
Loading...
Searching...
No Matches
memoria_operor.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_operor.h
9 * @brief Byte-level memory work: its arguments, the calls, and the memor dispatch table.
10 * @author dstroy0 (Douglas Quigg) <dquigg123@gmail.com>
11 * @date 2026-08-29
12 */
13#ifndef MMGR_MEMORIA_OPEROR_H
14#define MMGR_MEMORIA_OPEROR_H
15
16/** @addtogroup mod_memor
17 * @{ */
18
19#include "mmgr.h"
20
21EMBED_BEGIN_DECLS
22
23/**
24 * @brief Arguments for the memor calls.
25 *
26 * @note cpy and the two moves read dst, src and bytes. cmp reads src, other and bytes.
27 * @note chr, run and run_last read src, bytes and val. set reads dst, bytes and val.
28 * @note sort reads dst, scratch, count, bytes and order. bytes is then the size of one element.
29 */
30typedef struct
31{
32 void *const dst; /**< Destination for cpy, the moves and set, and the elements of sort [BORROWS]. */
33 const void *const src; /**< Source for cpy and the moves, region for cmp and chr [BORROWS]. */
34 const void *const other; /**< Second region for cmp [BORROWS]. */
35 const size_t bytes; /**< Bytes the call moves, examines or writes, or the bytes of one element of sort. */
36 const uint8_t val; /**< Byte chr looks for, the byte run and run_last count, or the byte set writes. */
37 void *const scratch; /**< Room for every element of sort, which its merges write [BORROWS]. */
38 const size_t count; /**< Elements sort orders. */
39 int (*const order)(const void *left, const void *right); /**< 0 or less where left comes at or before right. */
41
42/**
43 * @brief Type of the memor dispatch table.
44 *
45 * @note EMBED_TABLE_LAYOUT asserts the nine members sit at consecutive EMBED_FUNCTION_POINTER_BYTES
46 * offsets, with nothing else.
47 * @note cpy, move_down and move_up all copy, and differ in the overlap each one allows.
48 */
49typedef struct
50{
51 void (*cpy)(const MemoriaCfg *args); /**< Copies upward, for regions that do not overlap. */
52 void (*move_down)(const MemoriaCfg *args); /**< Copies upward, for a destination below the source. */
53 void (*move_up)(const MemoriaCfg *args); /**< Copies downward, for a destination above the source. */
54 embed_iword (*cmp)(const MemoriaCfg *args); /**< Orders two regions by their first difference. */
55 const void *(*chr)(const MemoriaCfg *args); /**< Finds the first byte equal to val. */
56 void (*set)(const MemoriaCfg *args); /**< Fills a region with val. */
57 embed_bool (*sort)(const MemoriaCfg *args); /**< Puts elements in the order of a compare. */
58 size_t (*run)(const MemoriaCfg *args); /**< Counts the bytes at the start equal to val. */
59 size_t (*run_last)(const MemoriaCfg *args); /**< Counts the bytes at the end equal to val. */
61EMBED_TABLE_LAYOUT(MemoriaOperorNs, cpy, move_down, move_up, cmp, chr, set, sort, run, run_last);
62
63/**
64 * @brief Copies args->bytes from args->src to args->dst, on an upward walk.
65 *
66 * @param[in] args Destination, source and count [BORROWS].
67 * @note Moves whole words first, then the rest of the bytes one at a time.
68 * @warning The backend's argument type qualifies both pointers restrict, and so the regions must not overlap.
69 * @warning args->dst must be writable and args->src readable for args->bytes.
70 */
71EMBED_HOST_DEVICE void mmgr_memor_cpy(const MemoriaCfg *args);
72
73/**
74 * @brief Copies args->bytes from args->src to args->dst, on a downward walk from the far end.
75 *
76 * @param[in] args Destination, source and count [BORROWS].
77 * @note Works back from the end. An args->dst above args->src is safe even when the regions overlap.
78 * @warning args->dst must be writable and args->src readable for args->bytes.
79 */
80EMBED_HOST_DEVICE void mmgr_memor_move_up(const MemoriaCfg *args);
81
82/**
83 * @brief Compares args->bytes of args->src against args->other.
84 *
85 * @param[in] args The two regions and the count [BORROWS].
86 * @return The difference of the first unequal byte pair, or 0 when every byte matches.
87 * @note The sign follows the bytes that differ, and so the result orders the two regions.
88 * @warning Both regions must be readable for args->bytes rounded up to a whole word. A count that
89 * does not fill the last one still reads a whole word at a time.
90 */
91EMBED_HOST_DEVICE embed_iword mmgr_memor_cmp(const MemoriaCfg *args);
92
93/**
94 * @brief Finds the first byte in args->src equal to args->val, within args->bytes.
95 *
96 * @param[in] args Region, count and the byte sought [BORROWS].
97 * @return Address of the match, or NULL when the byte does not occur [BORROWS].
98 * @note A terminator is not special. The search covers all args->bytes.
99 * @warning args->src must be readable for args->bytes rounded up to a whole word. A count that
100 * does not fill the last one still reads a whole word at a time.
101 */
102EMBED_HOST_DEVICE const void *mmgr_memor_chr(const MemoriaCfg *args);
103
104/**
105 * @brief Writes args->val into args->bytes of args->dst.
106 *
107 * @param[in] args Destination, count and the byte to write [BORROWS].
108 * @note Stores whole words built from args->val, then finishes byte by byte.
109 * @warning args->dst must be writable for args->bytes.
110 */
111EMBED_HOST_DEVICE void mmgr_memor_set(const MemoriaCfg *args);
112
113/**
114 * @brief Sorts args->count elements of args->bytes bytes each at args->dst in the order of args->order.
115 *
116 * @param[in] args The elements, the room, the count, the size of one element and the order [BORROWS].
117 * @return EMBED_TRUE where the elements are in order. EMBED_FALSE where args->bytes is 0 or a pointer is NULL,
118 * which leaves the elements as they were. Fewer than two elements are in order already.
119 * @note args->order answers 0 or less where its left element comes at or before its right one, as the compare of
120 * qsort answers. Two elements that order as equal keep the order that they had.
121 * @note A bottom-up merge sort: each pass merges runs of twice the width of the pass before, from args->dst into
122 * args->scratch and back. Every element moves log2 of args->count times, and MMgr takes no memory of its own.
123 * @note theory/linea/Linea/Sort.lean proves that the sort keeps the elements and leaves them in order, where the
124 * order is transitive and total.
125 * @warning args->dst and args->scratch must each hold args->count times args->bytes bytes, and must not overlap.
126 */
127EMBED_HOST_DEVICE embed_bool mmgr_memor_sort(const MemoriaCfg *args);
128
129/**
130 * @brief Counts the bytes at the start of args->src equal to args->val, within args->bytes.
131 *
132 * @param[in] args Region, count and the byte counted [BORROWS].
133 * @return The count of bytes before the first byte that is not args->val, or args->bytes.
134 * @note A terminator is not special. A byte of 0 counts as any other byte does.
135 * @note The walk reads aligned words, and a mask clears the lanes outside the region. No step tests a single byte.
136 * @warning The aligned words that hold the region's first and last bytes are read whole.
137 */
138EMBED_HOST_DEVICE size_t mmgr_memor_run(const MemoriaCfg *args);
139
140/**
141 * @brief Counts the bytes at the end of args->src equal to args->val, within args->bytes.
142 *
143 * @param[in] args Region, count and the byte counted [BORROWS].
144 * @return The count of bytes after the last byte that is not args->val, or args->bytes.
145 * @note A terminator is not special. A byte of 0 counts as any other byte does.
146 * @note The walk reads aligned words down from the end, and a mask clears the lanes outside the region. No step tests
147 * a single byte.
148 * @warning The aligned words that hold the region's first and last bytes are read whole.
149 */
150EMBED_HOST_DEVICE size_t mmgr_memor_run_last(const MemoriaCfg *args);
151
152/**
153 * @brief Dispatch table instance named memor.
154 *
155 * @note cpy and move_down both point at mmgr_memor_cpy. Every other member has its own function.
156 * @note There is no mmgr_memor_move_down. A destination below the source is what the upward walk
157 * already handles. So the table names that walk twice and carries no second copy of it.
158 */
159EMBED_TABLE_STORAGE MemoriaOperorNs memor EMBED_UNUSED = {
160 .cpy = mmgr_memor_cpy,
161 .move_down = mmgr_memor_cpy,
162 .move_up = mmgr_memor_move_up,
163 .cmp = mmgr_memor_cmp,
164 .chr = mmgr_memor_chr,
165 .set = mmgr_memor_set,
166 .sort = mmgr_memor_sort,
167 .run = mmgr_memor_run,
168 .run_last = mmgr_memor_run_last,
169};
170
171EMBED_END_DECLS
172
173/** @} */
174
175#endif
EMBED_HOST_DEVICE size_t mmgr_memor_run(const MemoriaCfg *args)
Counts the bytes at the start of args->src equal to args->val, within args->bytes.
EMBED_HOST_DEVICE embed_iword mmgr_memor_cmp(const MemoriaCfg *args)
Compares args->bytes of args->src against args->other.
EMBED_HOST_DEVICE void mmgr_memor_cpy(const MemoriaCfg *args)
Copies args->bytes from args->src to args->dst, on an upward walk.
EMBED_HOST_DEVICE embed_bool mmgr_memor_sort(const MemoriaCfg *args)
Sorts args->count elements of args->bytes bytes each at args->dst in the order of args->order.
EMBED_HOST_DEVICE void mmgr_memor_set(const MemoriaCfg *args)
Writes args->val into args->bytes of args->dst.
static const MemoriaOperorNs memor
Dispatch table instance named memor.
EMBED_HOST_DEVICE const void * mmgr_memor_chr(const MemoriaCfg *args)
Finds the first byte in args->src equal to args->val, within args->bytes.
EMBED_HOST_DEVICE size_t mmgr_memor_run_last(const MemoriaCfg *args)
Counts the bytes at the end of args->src equal to args->val, within args->bytes.
EMBED_HOST_DEVICE void mmgr_memor_move_up(const MemoriaCfg *args)
Copies args->bytes from args->src to args->dst, on a downward walk from the far end.
The embedded_types headers every module builds on.
Arguments for the memor calls.
void *const dst
const size_t bytes
int(*const) order(const void *left, const void *right)
const size_t count
const void *const other
void *const scratch
const uint8_t val
const void *const src
Type of the memor dispatch table.
void(*) move_up(const MemoriaCfg *args)
size_t(*) run(const MemoriaCfg *args)
size_t(*) run_last(const MemoriaCfg *args)
void(*) set(const MemoriaCfg *args)
void(*) move_down(const MemoriaCfg *args)
void(*) cpy(const MemoriaCfg *args)
embed_iword(*) cmp(const MemoriaCfg *args)
embed_bool(*) sort(const MemoriaCfg *args)