rlib
Convenience library for useful things
Loading...
Searching...
No Matches
rmemops.h
Go to the documentation of this file.
1/* RLIB - Convenience library for useful things
2 * Copyright (C) 2026 Haakon Sporsheim <haakon.sporsheim@gmail.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 3.0 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library.
16 * See the COPYING file at the root of the source repository.
17 */
18
51#ifndef __R_MEM_OPS_H__
52#define __R_MEM_OPS_H__
53
54#if !defined(__RLIB_H_INCLUDE_GUARD__) && !defined(RLIB_COMPILATION)
55#error "#include <rlib.h> only please."
56#endif
57
58#include <string.h>
59
61
75static inline int
77{
78 if (R_UNLIKELY (a == NULL)) return -(a != b);
79 if (R_UNLIKELY (b == NULL)) return a != b;
80 return memcmp (a, b, size);
81}
82
96static inline rpointer
97r_memset (rpointer a, int v, rsize size)
98{
99 if (a != NULL)
100 memset (a, v, size);
101 return a;
102}
103
105#define r_memclear(ptr, size) r_memset (ptr, 0, size)
106
120static inline rpointer
122 const void * R_ATTR_RESTRICT src, rsize size)
123{
124 if (dst != NULL && src != NULL)
125 return memcpy (dst, src, size);
126 return NULL;
127}
128
141static inline rpointer
143{
144 if (dst != NULL && src != NULL)
145 return memmove (dst, src, size);
146 return NULL;
147}
148
149R_END_DECLS
150
153#endif /* __R_MEM_OPS_H__ */
#define NULL
Null pointer constant (defined only if absent).
Definition rmacros.h:126
#define R_ATTR_RESTRICT
Pointer-aliasing hint (restrict) in the compiler's spelling.
Definition rmacros.h:354
#define R_BEGIN_DECLS
Open an extern "C" block under C++ (no-op in C).
Definition rmacros.h:196
static rpointer r_memset(rpointer a, int v, rsize size)
Fill size bytes at a with byte value v.
Definition rmemops.h:97
static rpointer r_memmove(rpointer dst, rconstpointer src, rsize size)
Copy size bytes from src to dst, allowing overlap.
Definition rmemops.h:142
static int r_memcmp(rconstpointer a, rconstpointer b, rsize size)
Lexicographically compare size bytes of a and b.
Definition rmemops.h:76
static rpointer r_memcpy(void *R_ATTR_RESTRICT dst, const void *R_ATTR_RESTRICT src, rsize size)
Copy size bytes from src to dst.
Definition rmemops.h:121
void * rpointer
Generic pointer (alias for void *).
Definition rtypes.h:327
const void * rconstpointer
Generic const pointer (alias for const void *).
Definition rtypes.h:329
unsigned long rsize
Unsigned pointer-sized size type (like size_t).
Definition rtypes.h:290