MMgr v1.0.0
Zero-heap memory manager in C11. One borrowed buffer, carved by asserted offset.
Loading...
Searching...
No Matches
mmgr_string_shim.h File Reference

Redirects the <string.h> names onto MMgr's bounded implementations. More...

Go to the source code of this file.

Macros

#define memcpy(dest, source, bytes)
 Replaces memcpy with mmgr_shim_cpy.
#define memmove(dest, source, bytes)
 Replaces memmove with mmgr_shim_move.
#define memset(dest, value, bytes)
 Replaces memset with mmgr_shim_set.
#define memcmp(left, right, bytes)
 Replaces memcmp with mmgr_shim_cmp.
#define memchr(region, value, bytes)
 Replaces memchr with mmgr_shim_chr.
#define strlen(text)
 Replaces strlen with a scan bounded at MMGR_STR_MAX.
#define strnlen(text, limit)
 Replaces strnlen with a scan bounded at limit.
#define strstr(haystack, needle)
 Replaces strstr with a case-sensitive search bounded at MMGR_STR_MAX.
#define strcasestr(haystack, needle)
 Replaces strcasestr with a case-folded search bounded at MMGR_STR_MAX.
#define strcmp(left, right)
 Replaces strcmp with an equality test bounded at MMGR_STR_MAX.
#define strcasecmp(left, right)
 Replaces strcasecmp with a case-folded equality test bounded at MMGR_STR_MAX.
#define strncmp(left, right, limit)
 Replaces strncmp with an equality test over at most limit bytes.
#define strncasecmp(left, right, limit)
 Replaces strncasecmp with a case-folded equality test over at most limit bytes.
#define strlcpy(dest, source, limit)
 Replaces strlcpy with a bounded copy that always terminates unless limit is 0.
#define strchr(text, value)
 Replaces strchr with a search bounded at MMGR_STR_MAX.

Functions

static void * mmgr_shim_cpy (void *dest, const void *source, size_t bytes)
 Copies bytes from source to dest and returns dest.
static void * mmgr_shim_move (void *dest, const void *source, size_t bytes)
 Copies bytes from source to dest, tolerating overlap, and returns dest.
static 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_iword mmgr_shim_cmp (const void *left, const void *right, size_t bytes)
 Compares bytes of left against right.
static 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.

Detailed Description

Redirects the <string.h> names onto MMgr's bounded implementations.

Author
dstroy0 (Douglas Quigg) dquig.nosp@m.g123.nosp@m.@gmai.nosp@m.l.co.nosp@m.m
Date
2026-08-30
Note
For code already written against <string.h> that has to run where there is no libc, or where an unbounded read is not acceptable. The call sites do not change. What the names mean does.
Defines the usual <string.h> include guards. A later #include <string.h> contributes nothing and the real declarations never arrive to conflict with these.
Not reached from mmgr.h. Including this changes what the <string.h> names mean, which is a decision a consumer makes for itself.
Warning
strcmp, strcasecmp, strncmp and strncasecmp report equality only and never order. Code that sorts with them is wrong here and compiles anyway.
Including this header changes the meaning of those names for the whole translation unit, including in headers it includes afterwards.
The six names bounded by MMGR_STR_MAX need the build to define it. Nothing here supplies a default, because the caller is what states how much storage there is.

Definition in file mmgr_string_shim.h.

Macro Definition Documentation

◆ memcpy

#define memcpy ( dest,
source,
bytes )

Replaces memcpy with mmgr_shim_cpy.

Note
Every argument is parenthesized. Any expression may be passed.

Definition at line 152 of file mmgr_string_shim.h.

◆ memmove

#define memmove ( dest,
source,
bytes )

Replaces memmove with mmgr_shim_move.

Note
Every argument is parenthesized, as in the memcpy shim.

Definition at line 159 of file mmgr_string_shim.h.

◆ memset

#define memset ( dest,
value,
bytes )

Replaces memset with mmgr_shim_set.

Note
value reaches mmgr_shim_set as embed_iword and is narrowed there, the signature the real memset has and the reason the shim takes a word and not a byte.

Definition at line 167 of file mmgr_string_shim.h.

◆ memcmp

#define memcmp ( left,
right,
bytes )

Replaces memcmp with mmgr_shim_cmp.

Note
Keeps the ordering the standard requires. The sign follows the first differing byte pair.

Definition at line 174 of file mmgr_string_shim.h.

◆ memchr

#define memchr ( region,
value,
bytes )

Replaces memchr with mmgr_shim_chr.

Note
The only one of the five mem shims that is bounded by its own argument and not by a terminator, and so it needs no MMGR_STR_MAX and cannot stop short of what it was asked for.

Definition at line 182 of file mmgr_string_shim.h.

◆ strlen

#define strlen ( text)

Replaces strlen with a scan bounded at MMGR_STR_MAX.

Returns
Bytes before the terminator, at most MMGR_STR_MAX.
Warning
Returns MMGR_STR_MAX when no terminator appears within it, instead of scanning further.
text must be readable until its terminator, or for MMGR_STR_MAX bytes when it has none, with the tail word read whole as mmgr_cellul_len describes.

Definition at line 192 of file mmgr_string_shim.h.

◆ strnlen

#define strnlen ( text,
limit )

Replaces strnlen with a scan bounded at limit.

Returns
Bytes before the terminator, at most limit.
Note
Returns limit when no terminator appears within it, as strnlen does.
Warning
text must be readable until its terminator, or for limit bytes when it has none, with the tail word read whole as mmgr_cellul_len describes.

Definition at line 202 of file mmgr_string_shim.h.

◆ strstr

#define strstr ( haystack,
needle )

Replaces strstr with a case-sensitive search bounded at MMGR_STR_MAX.

Returns
Address inside haystack where needle begins, or NULL when it does not occur [BORROWS].
Note
The cast through size_t drops the const the backend returns, matching the strstr signature.
Warning
Both operands must be terminated within MMGR_STR_MAX. Without a terminator the search reads that many bytes from them.

Definition at line 212 of file mmgr_string_shim.h.

◆ strcasestr

#define strcasestr ( haystack,
needle )

Replaces strcasestr with a case-folded search bounded at MMGR_STR_MAX.

Returns
Address inside haystack where needle begins, or NULL when it does not occur [BORROWS].
Note
Differs from the strstr shim only in passing ci as EMBED_TRUE.
Warning
Both operands must be terminated within MMGR_STR_MAX. Without a terminator the search reads that many bytes from them.

Definition at line 224 of file mmgr_string_shim.h.

◆ strcmp

#define strcmp ( left,
right )

Replaces strcmp with an equality test bounded at MMGR_STR_MAX.

Returns
0 when left and right hold the same string, 1 when they differ.
Note
Both == 0 and ! therefore test equality, as they do with the real strcmp.
Warning
Never negative and never above 1: it cannot be used to order strings.
Both operands must be terminated within MMGR_STR_MAX. Without a terminator the compare reads that many bytes from them.

Definition at line 237 of file mmgr_string_shim.h.

◆ strcasecmp

#define strcasecmp ( left,
right )

Replaces strcasecmp with a case-folded equality test bounded at MMGR_STR_MAX.

Returns
0 when left and right hold the same string ignoring case, 1 when they differ.
Warning
Never negative and never above 1: it cannot be used to order strings.
Both operands must be terminated within MMGR_STR_MAX. Without a terminator the compare reads that many bytes from them.

Definition at line 248 of file mmgr_string_shim.h.

◆ strncmp

#define strncmp ( left,
right,
limit )

Replaces strncmp with an equality test over at most limit bytes.

Returns
0 when left and right agree through limit bytes, 1 when they differ before it.
Warning
Never negative and never above 1: it cannot be used to order strings.
A terminator does not end the comparison. All limit bytes must be readable in both operands, with the tail word read whole as mmgr_cellul_diff describes.
limit appears twice in the expansion. An argument with a side effect is evaluated twice.

Definition at line 260 of file mmgr_string_shim.h.

◆ strncasecmp

#define strncasecmp ( left,
right,
limit )

Replaces strncasecmp with a case-folded equality test over at most limit bytes.

Returns
0 when left and right agree through limit bytes ignoring case, 1 when they differ before it.
Warning
Never negative and never above 1: it cannot be used to order strings.
A terminator does not end the comparison. All limit bytes must be readable in both operands, with the tail word read whole as mmgr_cellul_diff describes.
limit appears twice in the expansion. An argument with a side effect is evaluated twice.

Definition at line 273 of file mmgr_string_shim.h.

◆ strlcpy

#define strlcpy ( dest,
source,
limit )

Replaces strlcpy with a bounded copy that always terminates unless limit is 0.

Returns
Bytes copied and not counting the terminator.
Warning
Not the source length the real strlcpy returns. A caller cannot detect truncation by comparing the result against limit.
dest must be writable for limit bytes and source readable for limit minus one.

Definition at line 285 of file mmgr_string_shim.h.

◆ strchr

#define strchr ( text,
value )

Replaces strchr with a search bounded at MMGR_STR_MAX.

Returns
Address inside text where the byte occurs, or NULL when it does not [BORROWS].
Note
A value of 0 returns the address of the terminator, as strchr does.
The cast through size_t drops the const the backend returns, matching the strchr signature, and value is narrowed to uint8_t, the byte the search compares against.
Warning
text must be terminated within MMGR_STR_MAX. Without a terminator the search reads that many bytes from it.

Definition at line 298 of file mmgr_string_shim.h.

Function Documentation

◆ mmgr_shim_cpy()

void * mmgr_shim_cpy ( void * dest,
const void * source,
size_t bytes )
inlinestatic

Copies bytes from source to dest and returns dest.

Parameters
[out]destDestination to write [BORROWS].
[in]sourceBytes to read [BORROWS].
[in]bytesNumber of bytes to copy.
Returns
dest [BORROWS].
Warning
The two regions must not overlap. Use mmgr_shim_move when they might.
dest must be writable for bytes and source readable for the same.

Definition at line 69 of file mmgr_string_shim.h.

References MemoriaCfg::dst, and mmgr_shim_cpy().

Referenced by mmgr_shim_cpy().

◆ mmgr_shim_move()

void * mmgr_shim_move ( void * dest,
const void * source,
size_t bytes )
inlinestatic

Copies bytes from source to dest, tolerating overlap, and returns dest.

Parameters
[out]destDestination to write [BORROWS].
[in]sourceBytes to read [BORROWS].
[in]bytesNumber of bytes to copy.
Returns
dest [BORROWS].
Note
Compares the two addresses and walks upwards or downwards so overlapping bytes are read before they are written.
Warning
dest must be writable for bytes and source readable for the same.

Definition at line 86 of file mmgr_string_shim.h.

References MemoriaCfg::dst, and mmgr_shim_move().

Referenced by mmgr_shim_move().

◆ mmgr_shim_set()

void * mmgr_shim_set ( void * dest,
embed_iword value,
size_t bytes )
inlinestatic

Fills bytes of dest with the low byte of value and returns dest.

Parameters
[out]destDestination to write [BORROWS].
[in]valueFill value, of which only the low eight bits are used.
[in]bytesNumber of bytes to write.
Returns
dest [BORROWS].
Note
Explicit cast narrows value to uint8_t, matching the byte the fill writes.
Warning
dest must be writable for bytes.

Definition at line 111 of file mmgr_string_shim.h.

References MemoriaCfg::dst, and mmgr_shim_set().

Referenced by mmgr_shim_set().

◆ mmgr_shim_cmp()

embed_iword mmgr_shim_cmp ( const void * left,
const void * right,
size_t bytes )
inlinestatic

Compares bytes of left against right.

Parameters
[in]leftFirst region [BORROWS].
[in]rightSecond region [BORROWS].
[in]bytesNumber of bytes to compare.
Returns
Difference of the first unequal byte pair, or 0 when all bytes match.
Note
Unlike the strcmp shims, this one does order: the sign follows the differing bytes.
Warning
Both left and right must be readable for bytes.

Definition at line 127 of file mmgr_string_shim.h.

References mmgr_shim_cmp(), and MemoriaCfg::src.

Referenced by mmgr_shim_cmp().

◆ mmgr_shim_chr()

void * mmgr_shim_chr ( const void * region,
embed_iword value,
size_t bytes )
inlinestatic

Finds the first byte in region equal to the low byte of value.

Parameters
[in]regionBytes to search [BORROWS].
[in]valueByte sought, of which only the low eight bits are used.
[in]bytesNumber of bytes to search.
Returns
Address of the match, or NULL when the byte does not occur [BORROWS].
Note
The cast through size_t drops the const the backend returns, matching the memchr signature.
Warning
region must be readable for bytes.

Definition at line 142 of file mmgr_string_shim.h.

References mmgr_shim_chr(), and MemoriaCfg::src.

Referenced by mmgr_shim_chr().