rlib
Convenience library for useful things
Loading...
Searching...
No Matches
Inline byte-buffer primitives

Inline memcmp / memcpy / memmove / memset wrappers with rlib's NULL-tolerant convention. More...

Files

file  rmemops.h
 Inline byte-buffer primitives (see Inline byte-buffer primitives).
 

Macros

#define r_memclear(ptr, size)   r_memset (ptr, 0, size)
 Convenience wrapper: r_memset (ptr, 0, size).
 

Functions

static int r_memcmp (rconstpointer a, rconstpointer b, rsize size)
 Lexicographically compare size bytes of a and b.
 
static rpointer r_memset (rpointer a, int v, rsize size)
 Fill size bytes at a with byte value v.
 
static rpointer r_memcpy (void *R_ATTR_RESTRICT dst, const void *R_ATTR_RESTRICT src, rsize size)
 Copy size bytes from src to dst.
 
static rpointer r_memmove (rpointer dst, rconstpointer src, rsize size)
 Copy size bytes from src to dst, allowing overlap.
 

Detailed Description

Inline memcmp / memcpy / memmove / memset wrappers with rlib's NULL-tolerant convention.

These live in rlib/types/ rather than rlib/rmem.h because rendianness.h's load / store helpers (r_load_be{16,32,64} et al.) need them, and that header is processed earlier in the include chain than rmem.h. The bulk of rmem.h's API (allocators, vtables, scanners) continues to live there.

Each function is a static inline wrapper around the corresponding libc primitive plus rlib's NULL-tolerant convention. The libc primitive is a "magic" intrinsic on every modern compiler that gets replaced with inline movdqu / vld1q / rep movs for known constant sizes, so wrapping the call site behind a function-pointer indirection (or an extern R_API symbol) would defeat the point.

For the non-inline cousins:

both live in rmem.h.

Function Documentation

◆ r_memcmp()

static int r_memcmp ( rconstpointer  a,
rconstpointer  b,
rsize  size 
)
inlinestatic

Lexicographically compare size bytes of a and b.

Thin wrapper around libc memcmp with NULL-tolerant semantics: a NULL pointer compares less than a non-NULL pointer, two NULLs compare equal. For constant-time tag / digest comparison use r_memcmp_ct from rmem.h instead.

Parameters
aFirst buffer (may be NULL).
bSecond buffer (may be NULL).
sizeNumber of bytes to compare.
Returns
Negative if a<b, positive if a>b, zero if equal.

◆ r_memcpy()

static rpointer r_memcpy ( void *R_ATTR_RESTRICT  dst,
const void *R_ATTR_RESTRICT  src,
rsize  size 
)
inlinestatic

Copy size bytes from src to dst.

Thin wrapper around libc memcpy. Requires src and dst not to overlap (use r_memmove if they might). A NULL src or NULL dst is a no-op (returns NULL); otherwise returns dst.

Parameters
dstDestination buffer (may be NULL).
srcSource buffer (may be NULL).
sizeNumber of bytes to copy.
Returns
dst if both pointers were non-NULL, NULL otherwise.

◆ r_memmove()

static rpointer r_memmove ( rpointer  dst,
rconstpointer  src,
rsize  size 
)
inlinestatic

Copy size bytes from src to dst, allowing overlap.

Thin wrapper around libc memmove. Use when src and dst may overlap; r_memcpy is faster when they're known disjoint. A NULL pointer on either side is a no-op (returns NULL).

Parameters
dstDestination buffer (may be NULL).
srcSource buffer (may be NULL).
sizeNumber of bytes to copy.
Returns
dst if both pointers were non-NULL, NULL otherwise.

◆ r_memset()

static rpointer r_memset ( rpointer  a,
int  v,
rsize  size 
)
inlinestatic

Fill size bytes at a with byte value v.

Thin wrapper around libc memset. A NULL a is a no-op (returns NULL). For wiping secret material use r_memclear_secure from rmem.h - the optimiser is free to elide regular r_memset on a buffer that's about to be freed.

Parameters
aDestination buffer (may be NULL).
vByte value to write (low 8 bits used).
sizeNumber of bytes to write.
Returns
a.