MMgr v2.3.0
Zero-heap memory manager in C11. One borrowed buffer, carved by asserted offset.
Loading...
Searching...
No Matches
endian.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 endian.h
9 * @brief Endian reads and writes: the width enum, the argument type, the five entry points and the two
10 * order tables.
11 * @author dstroy0 (Douglas Quigg) <dquigg123@gmail.com>
12 * @date 2026-08-29
13 *
14 * @note parva_extremitas moves bytes in the host's own order and magna_extremitas in the reverse of it.
15 * Nothing here consults EMBED_BIG_ENDIAN, and so the two names hold on a little endian host and
16 * are for the opposite orders on a big endian one.
17 * @note The two tables share the table type and the rev entry. Their wr and rd differ, since one pair
18 * reverses and the other does not.
19 */
20#ifndef MMGR_ENDIAN_H
21#define MMGR_ENDIAN_H
22
23/** @addtogroup mod_endian
24 * @{ */
25
26#include "mmgr.h"
27
28EMBED_BEGIN_DECLS
29
30/**
31 * @brief Width of one endian read or write, counted in bytes.
32 *
33 * @note The enumerators are the byte counts themselves, and so the implementation switches on them directly.
34 * @note One byte on every compiler, through EMBED_ENUM_BYTE.
35 * @warning Nothing holds a value to these three. The switches in endian.c test 2 and 4 and take
36 * everything else on the default arm, which moves eight bytes.
37 */
38EMBED_ENUM_BYTE(mmgr_endian_width, MMGR_ENDIAN_16 = 2, /**< Two bytes. */
39 MMGR_ENDIAN_32 = 4, /**< Four bytes. */
40 MMGR_ENDIAN_64 = 8 /**< Eight bytes. */
41);
42
43/**
44 * @brief Arguments for the endian calls, where each call reads only the members it needs.
45 *
46 * @note wr reads dst, val and width. rd reads src and width. rev reads val and width.
47 * @note width is what bounds both pointers. The wr and rd entries touch that many bytes of the one they
48 * use, or eight when width is not one of the mmgr_endian_width enumerators. The rev entry reads neither pointer.
49 * @warning Neither pointer has a check. endian.c passes them straight to proximus_operor, which
50 * dereferences them with no test and no assertion.
51 */
52typedef struct
53{
54 uint8_t *const dst; /**< Destination for wr [BORROWS]. */
55 const uint8_t *const src; /**< Source for rd [BORROWS]. */
56 const uint64_t val; /**< Value for wr, or the value rev reverses. */
57 const mmgr_endian_width width; /**< Bytes the call moves. */
58} EndianCfg;
59
60/**
61 * @brief Type of an endian dispatch table.
62 *
63 * @note EMBED_TABLE_LAYOUT asserts the three members sit at consecutive EMBED_FUNCTION_POINTER_BYTES offsets, with
64 * nothing else.
65 * @note Two instances share this type, one per byte order. Which one a call goes through is what sets
66 * the order, and no member takes it as an argument.
67 * @note wr passes back args->width as it came. The other two return the value they produced.
68 */
69typedef struct
70{
71 size_t (*wr)(const EndianCfg *args); /**< Writes width bytes of val to dst in the table's order. */
72 uint64_t (*rd)(const EndianCfg *args); /**< Reads width bytes from src in the table's order. */
73 uint64_t (*rev)(const EndianCfg *args); /**< Reverses val at width bytes. */
74} EndianNs;
75EMBED_TABLE_LAYOUT(EndianNs, wr, rd, rev);
76
77/**
78 * @brief Writes args->width bytes of args->val to args->dst and does not reverse them.
79 *
80 * @param[in,out] args Destination, value and width [BORROWS].
81 * @return args->width.
82 * @warning args->dst must be writable for args->width bytes. Nothing checks it. endian.c passes it to
83 * proxim.put16, put32 or put64, each of which stores through it with no test.
84 * @warning args->width must be one of the mmgr_endian_width enumerators. Any other value writes eight
85 * bytes.
86 */
87EMBED_HOST_DEVICE size_t mmgr_wr_le(const EndianCfg *args);
88
89/**
90 * @brief Reads args->width bytes from args->src and does not reverse them.
91 *
92 * @param[in] args Source and width [BORROWS].
93 * @return The value read, in the low args->width bytes.
94 * @warning args->src must be readable for args->width bytes. Nothing checks it. endian.c passes it to
95 * proxim.load16, load32 or load64, each of which reads through it with no test.
96 * @warning args->width must be one of the mmgr_endian_width enumerators. Any other value reads eight
97 * bytes.
98 */
99EMBED_HOST_DEVICE uint64_t mmgr_rd_le(const EndianCfg *args);
100
101/**
102 * @brief Reverses args->val, then writes args->width bytes of it to args->dst.
103 *
104 * @param[in,out] args Destination, value and width [BORROWS].
105 * @return args->width.
106 * @warning args->dst must be writable for args->width bytes. Nothing checks it. endian.c passes it to
107 * proxim.put16, put32 or put64, each of which stores through it with no test.
108 * @warning args->width must be one of the mmgr_endian_width enumerators. Any other value writes eight
109 * bytes. One above eight wraps the reversal's shift count, and a width of 0 shifts by 64,
110 * which has no definition.
111 */
112EMBED_HOST_DEVICE size_t mmgr_wr_be(const EndianCfg *args);
113
114/**
115 * @brief Reads args->width bytes from args->src, then reverses them.
116 *
117 * @param[in] args Source and width [BORROWS].
118 * @return The reversed value, in the low args->width bytes.
119 * @warning args->src must be readable for args->width bytes. Nothing checks it. endian.c passes it to
120 * proxim.load16, load32 or load64, each of which reads through it with no test.
121 * @warning args->width must be one of the mmgr_endian_width enumerators. Any other value reads eight
122 * bytes. One above eight wraps the reversal's shift count, and a width of 0 shifts by 64,
123 * which has no definition.
124 */
125EMBED_HOST_DEVICE uint64_t mmgr_rd_be(const EndianCfg *args);
126
127/**
128 * @brief Reverses the byte order of args->val at args->width bytes.
129 *
130 * @param[in] args Value and width [BORROWS].
131 * @return The reversed value, right-aligned into the low args->width bytes.
132 * @note Reads args->val and args->width, alone, and so it touches no buffer and dst and src take
133 * no part.
134 * @note Both tables point rev at this one function, which reverses whatever it receives with no
135 * reference to either order.
136 * @warning args->width must be one of the mmgr_endian_width enumerators. 8 minus it wraps as unsigned
137 * arithmetic does. A width above eight wraps into a very large shift count, and a width of 0
138 * shifts a 64-bit value by 64, which has no definition.
139 */
140EMBED_HOST_DEVICE uint64_t mmgr_endian_rev(const EndianCfg *args);
141
142/**
143 * @brief Dispatch table instance named parva_extremitas, the little endian order.
144 *
145 * @note wr and rd move bytes as they lie, and so they land in the host's own order. This module never
146 * consults EMBED_BIG_ENDIAN. So the name holds on a little endian host and this table writes
147 * big endian bytes on a big endian one.
148 * @note rev is the same function both tables use.
149 */
150EMBED_TABLE_STORAGE EndianNs parva_extremitas EMBED_UNUSED = {
151 .wr = mmgr_wr_le,
152 .rd = mmgr_rd_le,
153 .rev = mmgr_endian_rev,
154};
155
156/**
157 * @brief Dispatch table instance named magna_extremitas, the big endian order.
158 *
159 * @note wr reverses before its write and rd reverses after its read, and so the bytes land in the reverse of
160 * the host's own order. That is big endian on a little endian host, as the name
161 * records, and little endian on a big endian one.
162 * @note rev is the same function as in parva_extremitas.
163 */
164EMBED_TABLE_STORAGE EndianNs magna_extremitas EMBED_UNUSED = {
165 .wr = mmgr_wr_be,
166 .rd = mmgr_rd_be,
167 .rev = mmgr_endian_rev,
168};
169
170EMBED_END_DECLS
171
172/** @} */
173
174#endif
EMBED_HOST_DEVICE uint64_t mmgr_rd_le(const EndianCfg *args)
Reads args->width bytes from args->src and does not reverse them.
EMBED_HOST_DEVICE size_t mmgr_wr_be(const EndianCfg *args)
Reverses args->val, then writes args->width bytes of it to args->dst.
mmgr_endian_width
Width of one endian read or write, counted in bytes.
Definition endian.h:41
EMBED_HOST_DEVICE uint64_t mmgr_endian_rev(const EndianCfg *args)
Reverses the byte order of args->val at args->width bytes.
EMBED_HOST_DEVICE size_t mmgr_wr_le(const EndianCfg *args)
Writes args->width bytes of args->val to args->dst and does not reverse them.
static const EndianNs magna_extremitas
Dispatch table instance named magna_extremitas, the big endian order.
Definition endian.h:164
static const EndianNs parva_extremitas
Dispatch table instance named parva_extremitas, the little endian order.
Definition endian.h:150
EMBED_HOST_DEVICE uint64_t mmgr_rd_be(const EndianCfg *args)
Reads args->width bytes from args->src, then reverses them.
@ MMGR_ENDIAN_16
Definition endian.h:41
@ MMGR_ENDIAN_64
Definition endian.h:41
@ MMGR_ENDIAN_32
Definition endian.h:41
The embedded_types headers every module builds on.
Arguments for the endian calls, where each call reads only the members it needs.
Definition endian.h:53
const mmgr_endian_width width
Definition endian.h:57
const uint64_t val
Definition endian.h:56
uint8_t *const dst
Definition endian.h:54
const uint8_t *const src
Definition endian.h:55
Type of an endian dispatch table.
Definition endian.h:70
uint64_t(*) rd(const EndianCfg *args)
Definition endian.h:72
size_t(*) wr(const EndianCfg *args)
Definition endian.h:71
uint64_t(*) rev(const EndianCfg *args)
Definition endian.h:73