MMgr v1.0.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#include "mmgr.h"
17
18EMBED_BEGIN_DECLS
19
20/**
21 * @brief Arguments for the memor calls.
22 *
23 * @note cpy and the two moves read dst, src and bytes. cmp reads src, other and bytes.
24 * @note chr reads src, bytes and val. set reads dst, bytes and val.
25 */
26typedef struct
27{
28 void *const dst; /**< Destination for cpy, the moves and set [BORROWS]. */
29 const void *const src; /**< Source for cpy and the moves, region for cmp and chr [BORROWS]. */
30 const void *const other; /**< Second region for cmp [BORROWS]. */
31 const size_t bytes; /**< Bytes the call moves, examines or writes. */
32 const uint8_t val; /**< Byte chr looks for, or the byte set writes. */
34
35/**
36 * @brief Type of the memor dispatch table.
37 *
38 * @note EMBED_TABLE_LAYOUT asserts the six members sit at consecutive EMBED_FUNCTION_POINTER_BYTES
39 * offsets, with nothing else.
40 * @note cpy, move_down and move_up all copy, and differ in the overlap each one allows.
41 */
42typedef struct
43{
44 void (*cpy)(const MemoriaCfg *args); /**< Copies upward, for regions that do not overlap. */
45 void (*move_down)(const MemoriaCfg *args); /**< Copies upward, for a destination below the source. */
46 void (*move_up)(const MemoriaCfg *args); /**< Copies downward, for a destination above the source. */
47 embed_iword (*cmp)(const MemoriaCfg *args); /**< Orders two regions by their first difference. */
48 const void *(*chr)(const MemoriaCfg *args); /**< Finds the first byte equal to val. */
49 void (*set)(const MemoriaCfg *args); /**< Fills a region with val. */
51EMBED_TABLE_LAYOUT(MemoriaOperorNs, cpy, move_down, move_up, cmp, chr, set);
52
53/**
54 * @brief Copies args->bytes from args->src to args->dst, walking upward.
55 *
56 * @param[in] args Destination, source and count [BORROWS].
57 * @note Moves whole words first, then the remaining bytes one at a time.
58 * @warning The backend's argument type qualifies both pointers restrict, and so the regions must not overlap.
59 * @warning args->dst must be writable and args->src readable for args->bytes.
60 */
61void mmgr_memor_cpy(const MemoriaCfg *args);
62
63/**
64 * @brief Copies args->bytes from args->src to args->dst, walking downward from the far end.
65 *
66 * @param[in] args Destination, source and count [BORROWS].
67 * @note Works back from the end. An args->dst above args->src is safe even when the regions overlap.
68 * @warning args->dst must be writable and args->src readable for args->bytes.
69 */
71
72/**
73 * @brief Compares args->bytes of args->src against args->other.
74 *
75 * @param[in] args The two regions and the count [BORROWS].
76 * @return The difference of the first unequal byte pair, or 0 when every byte matches.
77 * @note The sign follows the differing bytes, and so the result orders the two regions.
78 * @warning Both regions must be readable for args->bytes rounded up to a whole word, since a count that
79 * does not fill the last one is still read a whole word at a time.
80 */
81embed_iword mmgr_memor_cmp(const MemoriaCfg *args);
82
83/**
84 * @brief Finds the first byte in args->src equal to args->val, within args->bytes.
85 *
86 * @param[in] args Region, count and the byte sought [BORROWS].
87 * @return Address of the match, or NULL when the byte does not occur [BORROWS].
88 * @note A terminator is not special. All args->bytes are searched.
89 * @warning args->src must be readable for args->bytes rounded up to a whole word, since a count that
90 * does not fill the last one is still read a whole word at a time.
91 */
92const void *mmgr_memor_chr(const MemoriaCfg *args);
93
94/**
95 * @brief Writes args->val into args->bytes of args->dst.
96 *
97 * @param[in] args Destination, count and the byte to write [BORROWS].
98 * @note Stores whole words built from args->val, then finishes byte by byte.
99 * @warning args->dst must be writable for args->bytes.
100 */
101void mmgr_memor_set(const MemoriaCfg *args);
102
103/**
104 * @brief Dispatch table instance named memor.
105 *
106 * @note cpy and move_down both point at mmgr_memor_cpy. Every other member has its own function.
107 * @note mmgr_memor_move_down is not declared. A destination below the source is what the upward walk
108 * already handles, and so the table names that walk twice instead of carrying a second copy of it.
109 */
110EMBED_TABLE_STORAGE MemoriaOperorNs memor EMBED_UNUSED = {
111 .cpy = mmgr_memor_cpy,
112 .move_down = mmgr_memor_cpy,
113 .move_up = mmgr_memor_move_up,
114 .cmp = mmgr_memor_cmp,
115 .chr = mmgr_memor_chr,
116 .set = mmgr_memor_set,
117};
118
119EMBED_END_DECLS
120
121#endif
embed_iword mmgr_memor_cmp(const MemoriaCfg *args)
Compares args->bytes of args->src against args->other.
void mmgr_memor_set(const MemoriaCfg *args)
Writes args->val into args->bytes of args->dst.
void mmgr_memor_move_up(const MemoriaCfg *args)
Copies args->bytes from args->src to args->dst, walking downward from the far end.
const void * mmgr_memor_chr(const MemoriaCfg *args)
Finds the first byte in args->src equal to args->val, within args->bytes.
static const MemoriaOperorNs memor
Dispatch table instance named memor.
void mmgr_memor_cpy(const MemoriaCfg *args)
Copies args->bytes from args->src to args->dst, walking upward.
The embedded_types headers every module builds on.
Arguments for the memor calls.
void *const dst
const size_t bytes
const void *const other
const uint8_t val
const void *const src
Type of the memor dispatch table.
void(*) move_up(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)