MMgr v2.3.0
Zero-heap memory manager in C11. One borrowed buffer, carved by asserted offset.
Loading...
Searching...
No Matches
mmgr_string_shim.h
1/**
2 * @brief Redirects the <string.h> names onto MMgr's bounded implementations.
3 *
4 * @note Defines the usual <string.h> include guards. A later #include <string.h> contributes nothing.
5 * @warning strcmp, strcasecmp, strncmp and strncasecmp report equality only and never order.
6 * @warning An include of this header changes what those names mean for the whole translation unit.
7 */
8#ifndef MMGR_STRING_SHIM_H
9#define MMGR_STRING_SHIM_H
10
11/** @addtogroup mod_config
12 * @{ */
13
15#include "config/mmgr_config.h"
17
18/**
19 * @brief Read bound applied to the <string.h> names that take no length of their own.
20 *
21 * @note Defaults to MMGR_CARCER_MAX, the largest confinium a string can occupy.
22 * @warning strlen, strstr, strcmp and strchr stop at this many bytes even without a terminator.
23 */
24#ifndef MMGR_STR_MAX
25
26#define MMGR_STR_MAX MMGR_CARCER_MAX
27#endif
28
29/**
30 * @brief Claims the include guards several C libraries use for <string.h>.
31 *
32 * @note A later #include <string.h> then expands to nothing, and leaves these macros in place.
33 * @warning A library whose guard is not among these five will still declare the real functions.
34 */
35#ifndef _STRING_H
36#define _STRING_H 1
37#endif
38#ifndef _STRING_H_
39#define _STRING_H_ 1
40#endif
41#ifndef __STRING_H__
42#define __STRING_H__ 1
43#endif
44#ifndef _STRING_H_INCLUDED
45#define _STRING_H_INCLUDED 1
46#endif
47#ifndef _INC_STRING
48#define _INC_STRING 1
49#endif
50
51EMBED_BEGIN_DECLS
52
53/**
54 * @brief Copies bytes from source to dest and returns dest.
55 *
56 * @param[out] dest Destination to write [BORROWS].
57 * @param[in] source Bytes to read [BORROWS].
58 * @param[in] bytes Number of bytes to copy.
59 * @return dest [BORROWS].
60 * @warning The two regions must not overlap; use mmgr_shim_move when they might.
61 */
62EMBED_HOST_DEVICE EMBED_INLINE void *mmgr_shim_cpy(void *dest, const void *source, size_t bytes)
63{
64 EMBED_CALL(memor.cpy, MemoriaCfg, .dst = dest, .src = source, .bytes = bytes);
65 return dest;
66}
67
68/**
69 * @brief Copies bytes from source to dest, with overlap allowed, and returns dest.
70 *
71 * @param[out] dest Destination to write [BORROWS].
72 * @param[in] source Bytes to read [BORROWS].
73 * @param[in] bytes Number of bytes to copy.
74 * @return dest [BORROWS].
75 * @note Compares the two addresses and walks upwards or downwards so a read of each shared byte comes
76 * before the write to it.
77 */
78EMBED_HOST_DEVICE EMBED_INLINE void *mmgr_shim_move(void *dest, const void *source, size_t bytes)
79{
80 if ((const uint8_t *)dest <= (const uint8_t *)source)
81 {
82 EMBED_CALL(memor.move_down, MemoriaCfg, .dst = dest, .src = source, .bytes = bytes);
83 }
84 else
85 {
86 EMBED_CALL(memor.move_up, MemoriaCfg, .dst = dest, .src = source, .bytes = bytes);
87 }
88 return dest;
89}
90
91/**
92 * @brief Fills bytes of dest with the low byte of value and returns dest.
93 *
94 * @param[out] dest Destination to write [BORROWS].
95 * @param[in] value Fill value; only its low eight bits count.
96 * @param[in] bytes Number of bytes to write.
97 * @return dest [BORROWS].
98 * @note Explicit cast narrows value to uint8_t, the byte the fill writes.
99 */
100EMBED_HOST_DEVICE EMBED_INLINE void *mmgr_shim_set(void *dest, embed_iword value, size_t bytes)
101{
102 EMBED_CALL(memor.set, MemoriaCfg, .dst = dest, .bytes = bytes, .val = (uint8_t)value);
103 return dest;
104}
105
106/**
107 * @brief Compares bytes of left against right.
108 *
109 * @param[in] left First region [BORROWS].
110 * @param[in] right Second region [BORROWS].
111 * @param[in] bytes Number of bytes to compare.
112 * @return Difference of the first unequal byte pair, or 0 when all bytes match.
113 * @note Unlike the strcmp shims, this one does order: the sign follows the bytes that differ.
114 */
115EMBED_HOST_DEVICE EMBED_INLINE embed_iword mmgr_shim_cmp(const void *left, const void *right, size_t bytes)
116{
117 return EMBED_CALL(memor.cmp, MemoriaCfg, .src = left, .other = right, .bytes = bytes);
118}
119
120/**
121 * @brief Finds the first byte in region equal to the low byte of value.
122 *
123 * @param[in] region Bytes to search [BORROWS].
124 * @param[in] value Byte sought; only its low eight bits count.
125 * @param[in] bytes Number of bytes to search.
126 * @return Address of the match, or NULL when the byte does not occur [BORROWS].
127 * @note The cast through size_t drops the const the backend returns, as the memchr signature requires.
128 */
129EMBED_HOST_DEVICE EMBED_INLINE void *mmgr_shim_chr(const void *region, embed_iword value, size_t bytes)
130{
131 return (void *)(size_t)EMBED_CALL(memor.chr, MemoriaCfg, .src = region, .bytes = bytes, .val = (uint8_t)value);
132}
133
134/**
135 * @brief Replaces memcpy with mmgr_shim_cpy.
136 *
137 * @note Every argument sits in parentheses. Any expression may go in.
138 */
139#define memcpy(dest, source, bytes) mmgr_shim_cpy((dest), (source), (bytes))
140
141/**
142 * @brief Replaces memmove with mmgr_shim_move.
143 */
144#define memmove(dest, source, bytes) mmgr_shim_move((dest), (source), (bytes))
145
146/**
147 * @brief Replaces memset with mmgr_shim_set.
148 */
149#define memset(dest, value, bytes) mmgr_shim_set((dest), (value), (bytes))
150
151/**
152 * @brief Replaces memcmp with mmgr_shim_cmp.
153 *
154 * @note Retains the ordering the standard requires; the sign follows the first byte pair that differs.
155 */
156#define memcmp(left, right, bytes) mmgr_shim_cmp((left), (right), (bytes))
157
158/**
159 * @brief Replaces memchr with mmgr_shim_chr.
160 */
161#define memchr(region, value, bytes) mmgr_shim_chr((region), (value), (bytes))
162
163/**
164 * @brief Replaces strlen with a scan bounded at MMGR_STR_MAX.
165 *
166 * @warning Returns MMGR_STR_MAX when no terminator appears within it, and scans no further.
167 */
168#define strlen(text) EMBED_CALL(cellul.len, CatenaFinitaCfg, .src = (text), .cap = MMGR_STR_MAX)
169
170/**
171 * @brief Replaces strnlen with a scan bounded at limit.
172 *
173 * @note Returns limit when no terminator appears within it, as strnlen does.
174 */
175#define strnlen(text, limit) EMBED_CALL(cellul.len, CatenaFinitaCfg, .src = (text), .cap = (limit))
176
177/**
178 * @brief Replaces strstr with a case-sensitive search bounded at MMGR_STR_MAX.
179 *
180 * @note The cast through size_t drops the const the backend returns, as the strstr signature requires.
181 * @warning The read of both operands reaches MMGR_STR_MAX, and both must have a terminator within it.
182 */
183#define strstr(haystack, needle) \
184 ((char *)(size_t)EMBED_CALL(cellul.find, CatenaFinitaCfg, .src = (haystack), .cap = MMGR_STR_MAX, \
185 .other = (needle), .other_cap = MMGR_STR_MAX, .ci = EMBED_FALSE))
186
187/**
188 * @brief Replaces strcasestr with a case-folded search bounded at MMGR_STR_MAX.
189 *
190 * @note Differs from the strstr shim only in that it passes ci as EMBED_TRUE.
191 * @warning The read of both operands reaches MMGR_STR_MAX, and both must have a terminator within it.
192 */
193#define strcasestr(haystack, needle) \
194 ((char *)(size_t)EMBED_CALL(cellul.find, CatenaFinitaCfg, .src = (haystack), .cap = MMGR_STR_MAX, \
195 .other = (needle), .other_cap = MMGR_STR_MAX, .ci = EMBED_TRUE))
196
197/**
198 * @brief Replaces strcmp with an equality test bounded at MMGR_STR_MAX.
199 *
200 * @note Gives 0 when the two agree and 1 when they do not, and so == 0 and ! both test equality.
201 * @warning Never negative and never above 1: it cannot order strings.
202 */
203#define strcmp(left, right) \
204 (!EMBED_CALL(cellul.eq, CatenaFinitaCfg, .src = (left), .other = (right), .cap = MMGR_STR_MAX, .ci = EMBED_FALSE))
205
206/**
207 * @brief Replaces strcasecmp with a case-folded equality test bounded at MMGR_STR_MAX.
208 *
209 * @note Gives 0 when the two agree and 1 when they do not.
210 * @warning Never negative and never above 1: it cannot order strings.
211 */
212#define strcasecmp(left, right) \
213 (!EMBED_CALL(cellul.eq, CatenaFinitaCfg, .src = (left), .other = (right), .cap = MMGR_STR_MAX, .ci = EMBED_TRUE))
214
215/**
216 * @brief Replaces strncmp with an equality test over at max limit bytes.
217 *
218 * @note Gives 0 when the two agree through limit and 1 when they differ before it.
219 * @warning Never negative and never above 1: it cannot order strings.
220 * @warning A terminator does not end the comparison; all limit bytes must be readable in both operands.
221 */
222#define strncmp(left, right, limit) \
223 (EMBED_CALL(cellul.diff, CatenaFinitaCfg, .src = (left), .other = (right), .cap = (limit), .ci = EMBED_FALSE) < \
224 (limit))
225
226/**
227 * @brief Replaces strncasecmp with a case-folded equality test over at max limit bytes.
228 *
229 * @note Gives 0 when the two agree through limit and 1 when they differ before it.
230 * @warning Never negative and never above 1: it cannot order strings.
231 * @warning A terminator does not end the comparison; all limit bytes must be readable in both operands.
232 */
233#define strncasecmp(left, right, limit) \
234 (EMBED_CALL(cellul.diff, CatenaFinitaCfg, .src = (left), .other = (right), .cap = (limit), .ci = EMBED_TRUE) < \
235 (limit))
236
237/**
238 * @brief Replaces strlcpy with a bounded copy that always terminates unless limit is 0.
239 *
240 * @note Returns the bytes copied, the truncated length and not the source length.
241 */
242#define strlcpy(dest, source, limit) \
243 EMBED_CALL(cellul.copy, CatenaFinitaCfg, .dst = (dest), .src = (source), .cap = (limit))
244
245/**
246 * @brief Replaces strchr with a search bounded at MMGR_STR_MAX.
247 *
248 * @note A value of 0 returns the address of the terminator, as strchr does.
249 * @note The cast through size_t drops the const the backend returns, as the strchr signature requires.
250 * @warning The read of text reaches MMGR_STR_MAX, and so it must have a terminator within it.
251 */
252#define strchr(text, value) \
253 ((char *)(size_t)EMBED_CALL(cellul.chr, CatenaFinitaCfg, .src = (text), .cap = MMGR_STR_MAX, \
254 .byte = (uint8_t)(value)))
255
256EMBED_END_DECLS
257
258/** @} */
259
260#endif
Bounded string work: the three argument types, the calls, and the cellul dispatch table.
static EMBED_HOST_DEVICE embed_iword mmgr_shim_cmp(const void *left, const void *right, size_t bytes)
Compares bytes of left against right.
static EMBED_HOST_DEVICE void * mmgr_shim_chr(const void *region, embed_iword value, size_t bytes)
Finds the first byte in region equal to the low byte of value.
static EMBED_HOST_DEVICE void * mmgr_shim_set(void *dest, embed_iword value, size_t bytes)
Fills bytes of dest with the low byte of value and returns dest.
static EMBED_HOST_DEVICE void * mmgr_shim_move(void *dest, const void *source, size_t bytes)
Copies bytes from source to dest, with overlap allowed, and returns dest.
static EMBED_HOST_DEVICE void * mmgr_shim_cpy(void *dest, const void *source, size_t bytes)
Copies bytes from source to dest and returns dest.
Byte-level memory work: its arguments, the calls, and the memor dispatch table.
Arguments for the memor calls.
void *const dst
const void *const src