MMgr v2.3.0
Zero-heap memory manager in C11. One borrowed buffer, carved by asserted offset.
Loading...
Searching...
No Matches
mmgr_config.h
1/**
2 * @brief Build-time settings: widths, region sizes, feature switches and the region cut macros.
3 *
4 * @note Each tunable sits behind #ifndef, which lets a build set it first.
5 * @note embedded_types gives every platform fact and every type: the word, the index, the widths, the byte order,
6 * the unaligned load, the attributes and the dispatch layout. This header gives none of them.
7 */
8#ifndef MMGR_CONFIG_H
9#define MMGR_CONFIG_H
10
11/** @addtogroup mod_config
12 * @{ */
13
14/**
15 * @brief The settings of a build that gives no command line flag.
16 *
17 * A build sets a tunable on its command line, or in the header mmgr_settings.h on its include path. This
18 * tree holds no such header. Where the compiler finds one, this file reads it before the first tunable.
19 *
20 * @note The Arduino IDE gives a library no command line flag. The header is thus the only way to set a
21 * tunable there.
22 * @note The file mmgr.h reads the same header, for the module headers that reach mmgr.h before this file.
23 */
24#if defined(__has_include)
25#if __has_include("mmgr_settings.h")
26#include "mmgr_settings.h"
27#endif
28#endif
29
30#include "embed_compiler_directives.h"
31#include "embed_dispatch_layout.h"
32#include "embed_types.h"
33
34/**
35 * @brief Alignment applied to every carved region, and the granularity each pool must be a multiple of.
36 *
37 * @note MMGR_CARCER_CHECK asserts each pool is a whole number of these and at least two of them.
38 */
39#ifndef MMGR_ALIGN_BYTES
40
41#define MMGR_ALIGN_BYTES 16u
42#endif
43
44/**
45 * @brief Set to 1 to enable the library's debug-only checks.
46 *
47 * @note Declared ahead of MMGR_ASSERT because it selects which of the two the assert becomes.
48 */
49#ifndef MMGR_DEBUG_CHECKS
50#define MMGR_DEBUG_CHECKS 0
51#endif
52
53/**
54 * @brief Runtime assertion hook: a trap under MMGR_DEBUG_CHECKS, inert otherwise.
55 *
56 * @param[in] cond Condition the caller expects to hold.
57 * @param[in] msg String literal that describes the expectation.
58 * @note An expectation the library asserts is one a correct caller cannot break, and so the plain
59 * build pays nothing for it. It expands to a sizeof, which type checks cond and never evaluates
60 * it. The checks build evaluates it instead and stops on the spot. A caller that broke one
61 * fails a test and does not carry on with the damage done.
62 * @note A build may define its own before it includes this header, and neither form below then applies.
63 * A test harness wants that. The form here halts and names nothing, and a harness that reports
64 * the case defines its own with the report in it.
65 * @warning With MMGR_DEBUG_CHECKS at 0, a failed expectation produces no diagnostic and no trap. It
66 * is not a runtime check and cannot are as one.
67 */
68#ifndef MMGR_ASSERT
69#if MMGR_DEBUG_CHECKS
70// No libc here, and so the trap is a halt, as MMGR_FATAL in mmgr.h is. The spin is what a debugger
71// catches, and the message is the string in the source beside it
72#define MMGR_ASSERT(cond, msg) \
73 do \
74 { \
75 if (!(cond)) \
76 { \
77 (void)(msg); \
78 for (;;) \
79 { \
80 } \
81 } \
82 } while (0)
83#else
84#define MMGR_ASSERT(cond, msg) ((void)sizeof((cond) ? 1 : 0), (void)0)
85#endif
86#endif
87
88/**
89 * @brief Bytes in the largest plaintext confinium this build will declare.
90 *
91 * @note Allocates nothing and sizes no pool. A pool's extent is an argument to mmgr_carcer_init, and
92 * nothing in carceribus reads this. It feeds MMGR_CARCER_MAX below, which is a
93 * bound other modules size their worst case against. So it is a statement of intent about the
94 * regions you will declare, and it must rise if you declare a bigger one.
95 */
96#ifndef MMGR_PLAINTEXT_CONFIN_SIZE
97#define MMGR_PLAINTEXT_CONFIN_SIZE 4096u
98#endif
99
100/**
101 * @brief Bytes in the largest secure confinium this build will declare.
102 *
103 * @note The same type of number as MMGR_PLAINTEXT_CONFIN_SIZE, and it sizes no pool either.
104 */
105#ifndef MMGR_SECURE_CONFIN_SIZE
106#define MMGR_SECURE_CONFIN_SIZE 4096u
107#endif
108
109/**
110 * @brief Bytes in the larger of the two confinia, the bound on the worst case a scan or a read must cover.
111 *
112 * @note verbum_scrutor.h derives MMGR_SCAN_MAX_WORDS from this, and mmgr_string_shim.h uses it as MMGR_STR_MAX.
113 * @note The bound exists because these modules size a worst case at compile time and test no
114 * length at run time. A string cannot be longer than the confinium that holds it, and the
115 * confinium is the cap. So the scanner can know how many words that is before it
116 * ever runs.
117 * @warning Declare a region larger than both knobs and this bound falls short. The static asserts
118 * in verbum_scrutor.h check that the word count covers this value, not that this value
119 * covers your regions, which nothing here can see.
120 */
121#ifndef MMGR_CARCER_MAX
122#if MMGR_PLAINTEXT_CONFIN_SIZE >= MMGR_SECURE_CONFIN_SIZE
123#define MMGR_CARCER_MAX ((size_t)MMGR_PLAINTEXT_CONFIN_SIZE)
124#else
125#define MMGR_CARCER_MAX ((size_t)MMGR_SECURE_CONFIN_SIZE)
126#endif
127#endif
128
129/**
130 * @brief Set to 1 to build the memoriam_praetereo DMA path.
131 *
132 * @note mmgr.h includes memoriam_praetereo.h only when this holds. The schedule's knobs have their reads in
133 * memoriam_praetereo/praet_praefinitum.h and embed_clock.h.
134 */
135#ifndef MMGR_ENABLE_DMA
136#define MMGR_ENABLE_DMA 0
137#endif
138/**
139 * @brief Set to 1 to build the memoria_externa external memory path.
140 *
141 * @note mmgr.h includes memoria_externa.h only when this holds.
142 */
143#ifndef MMGR_ENABLE_EXTRAM
144
145#define MMGR_ENABLE_EXTRAM 0
146#endif
147
148/**
149 * @brief Set to 1 to build the time-based ring behavior.
150 *
151 * @note MMGR_RING_ATTACH_US exists only when this holds.
152 */
153#ifndef MMGR_ENABLE_CLOCK
154#define MMGR_ENABLE_CLOCK 0
155#endif
156
157/**
158 * @brief Microseconds a ring waits before its attach.
159 *
160 * @warning Defined only when MMGR_ENABLE_CLOCK holds.
161 */
162#if MMGR_ENABLE_CLOCK
163#ifndef MMGR_RING_ATTACH_US
164#define MMGR_RING_ATTACH_US 100u
165#endif
166#endif
167
168/**
169 * @brief Fails the build unless x_ has type size_t.
170 *
171 * @param[in] x_ Expression under a type check.
172 * @note Evaluates to void; the _Generic association list admits size_t only.
173 */
174#define MMGR_MEMOR_IS_SIZE(x_) ((void)_Generic((x_), size_t: 0))
175
176/**
177 * @brief Fails the build unless x_ has type uint8_t.
178 *
179 * @param[in] x_ Expression under a type check.
180 * @note Evaluates to void; the _Generic association list admits uint8_t only.
181 */
182#define MMGR_MEMOR_IS_BYTE(x_) ((void)_Generic((x_), uint8_t: 0))
183
184/** @} */
185
186#endif