MMgr v2.3.0
Zero-heap memory manager in C11. One borrowed buffer, carved by asserted offset.
Loading...
Searching...
No Matches
bitorum_introitus_exitus.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 bitorum_introitus_exitus.h
9 * @brief Bit writer state, its arguments, and the bitio dispatch table.
10 * @author dstroy0 (Douglas Quigg) <dquigg123@gmail.com>
11 * @date 2026-08-29
12 *
13 * @note Packs bit fields of any width into a byte buffer and no bit ever crosses a call. The
14 * writer holds the partial byte. A caller states widths and values and never does its own
15 * shifts or masks.
16 * @note The writer is a value the caller owns and not state this module retains. Two streams are two
17 * writers, and neither module state nor a lock comes into it.
18 * @warning put writes whole bytes only. So bits that do not fill one stay in the writer's residue
19 * and align puts that last partial byte out. A stream whose length is not a
20 * multiple of eight and that never calls align ends one byte short. No flag rises
21 * and nothing shows at the call.
22 */
23#ifndef MMGR_BITORUM_INTROITUS_EXITUS_H
24#define MMGR_BITORUM_INTROITUS_EXITUS_H
25
26/** @addtogroup mod_bitio
27 * @{ */
28
29#include "mmgr.h"
30
31EMBED_BEGIN_DECLS
32
33/**
34 * @brief Bit writer state: the buffer, how much went out, and the partial byte.
35 *
36 * @note Built by mmgr_bitor_init, advanced by mmgr_bitor_put, and finished by mmgr_bitor_align.
37 */
38typedef struct
39{
40 uint8_t *out; /**< Destination buffer [BORROWS]. */
41 size_t cap; /**< Bytes available in out. */
42 size_t bytes_written; /**< Whole bytes written so far. */
43 uint8_t residue; /**< Bits not yet written, in the low bit_count positions. */
44 embed_word bit_count; /**< Bits stored in residue, always under 8. */
45 embed_bool overflow; /**< Set once a request would pass cap, which blocks later writes. */
47
48/**
49 * @brief Arguments for the bitor calls, where each call reads only the members it needs.
50 *
51 * @note mmgr_bitor_init reads out and cap. mmgr_bitor_put reads writer, val and bit_count.
52 * mmgr_bitor_align reads writer alone.
53 * @note One argument type for all three entries, and so every call has the same shape. An unnamed
54 * member reads zero and not an undefined value.
55 */
56typedef struct
57{
58 mmgr_bitor *const writer; /**< Writer for mmgr_bitor_put and mmgr_bitor_align [BORROWS]. */
59 uint8_t *const out; /**< Buffer for mmgr_bitor_init [BORROWS]. */
60 const size_t cap; /**< Bytes available in out. */
61 const uint64_t val; /**< Bits for mmgr_bitor_put, taken from the low end. */
62 const embed_word bit_count; /**< Bits of val to write, which must not exceed 64. */
64
65/**
66 * @brief Type of the bitio dispatch table.
67 *
68 * @note EMBED_TABLE_LAYOUT asserts the three members sit at consecutive EMBED_FUNCTION_POINTER_BYTES offsets, with
69 * nothing else.
70 */
71typedef struct
72{
73 mmgr_bitor (*init)(const BitorumCfg *args); /**< Builds a writer over a buffer. */
74 void (*put)(const BitorumCfg *args); /**< Appends bits, and writes whole bytes only. */
75 void (*align)(const BitorumCfg *args); /**< Writes the partial byte still in the residue. */
77EMBED_TABLE_LAYOUT(BitorumIntroitusExitusNs, init, put, align);
78
79/**
80 * @brief Builds a bit writer over args->out with capacity args->cap.
81 *
82 * @param[in] args Buffer and capacity [BORROWS].
83 * @return A writer with no bytes written and no residue.
84 * @note The returned writer retains args->out, which must outlive it [BORROWS].
85 * @warning args->out must not be null and args->cap must not be zero. Neither has a check outside a
86 * MMGR_DEBUG_CHECKS build, and a null out passes here. mmgr_bitor_put writes
87 * through it on the first whole byte.
88 */
89EMBED_HOST_DEVICE mmgr_bitor mmgr_bitor_init(const BitorumCfg *args);
90
91/**
92 * @brief Appends the low args->bit_count bits of args->val to args->writer.
93 *
94 * @param[in,out] args Writer, value and bit count [BORROWS].
95 * @note Writes whole bytes only. Leftover bits stay in the writer's residue.
96 * @note Does nothing when the writer's overflow is already set. A caller may write a whole
97 * stream and test overflow once at the end and not after every call.
98 * @note Sets the writer's overflow and clears its residue when the bytes would pass its cap.
99 * @warning args->bit_count must not exceed 64, and nothing holds it there outside a
100 * MMGR_DEBUG_CHECKS build. A larger count writes zeros past the sixty-fourth bit and
101 * advances the writer as though they were data.
102 */
103EMBED_HOST_DEVICE void mmgr_bitor_put(const BitorumCfg *args);
104
105/**
106 * @brief Writes the partial byte the writer still holds, padded with zeros above its bits.
107 *
108 * @param[in,out] args Writer to finish [BORROWS].
109 * @note mmgr_bitor_put writes whole bytes only. Without this call the residue is never written.
110 * @note Does nothing when the residue is empty. A second call writes nothing.
111 * @note Does nothing when the writer's overflow is already set.
112 * @note It reads only args->writer.
113 * @warning Sets the writer's overflow when the byte would pass its cap.
114 */
115EMBED_HOST_DEVICE void mmgr_bitor_align(const BitorumCfg *args);
116
117/**
118 * @brief Dispatch table instance named bitio, with each member set to its mmgr_bitor_ function.
119 */
120EMBED_TABLE_STORAGE BitorumIntroitusExitusNs bitio EMBED_UNUSED = {
121 .init = mmgr_bitor_init,
122 .put = mmgr_bitor_put,
123 .align = mmgr_bitor_align,
124};
125
126EMBED_END_DECLS
127
128/** @} */
129
130#endif
static const BitorumIntroitusExitusNs bitio
Dispatch table instance named bitio, with each member set to its mmgr_bitor_ function.
EMBED_HOST_DEVICE mmgr_bitor mmgr_bitor_init(const BitorumCfg *args)
Builds a bit writer over args->out with capacity args->cap.
EMBED_HOST_DEVICE void mmgr_bitor_align(const BitorumCfg *args)
Writes the partial byte the writer still holds, padded with zeros above its bits.
EMBED_HOST_DEVICE void mmgr_bitor_put(const BitorumCfg *args)
Appends the low args->bit_count bits of args->val to args->writer.
The embedded_types headers every module builds on.
Arguments for the bitor calls, where each call reads only the members it needs.
const embed_word bit_count
mmgr_bitor *const writer
Type of the bitio dispatch table.
void(*) put(const BitorumCfg *args)
void(*) align(const BitorumCfg *args)
mmgr_bitor(*) init(const BitorumCfg *args)
Bit writer state: the buffer, how much went out, and the partial byte.