]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/memory-util.h
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
10 #include "alloc-util.h"
13 size_t page_size(void) _pure_
;
14 #define PAGE_ALIGN(l) ALIGN_TO((l), page_size())
15 #define PAGE_ALIGN_DOWN(l) ((l) & ~(page_size() - 1))
16 #define PAGE_OFFSET(l) ((l) & (page_size() - 1))
18 /* Normal memcpy requires src to be nonnull. We do nothing if n is 0. */
19 static inline void memcpy_safe(void *dst
, const void *src
, size_t n
) {
26 /* Normal memcmp requires s1 and s2 to be nonnull. We do nothing if n is 0. */
27 static inline int memcmp_safe(const void *s1
, const void *s2
, size_t n
) {
32 return memcmp(s1
, s2
, n
);
35 /* Compare s1 (length n1) with s2 (length n2) in lexicographic order. */
36 static inline int memcmp_nn(const void *s1
, size_t n1
, const void *s2
, size_t n2
) {
37 return memcmp_safe(s1
, s2
, MIN(n1
, n2
))
41 #define memzero(x,l) \
48 #define zero(x) (memzero(&(x), sizeof(x)))
50 bool memeqzero(const void *data
, size_t length
);
52 #define eqzero(x) memeqzero(x, sizeof(x))
54 static inline void *mempset(void *s
, int c
, size_t n
) {
56 return (uint8_t*)s
+ n
;
59 /* Normal memmem() requires haystack to be nonnull, which is annoying for zero-length buffers */
60 static inline void *memmem_safe(const void *haystack
, size_t haystacklen
, const void *needle
, size_t needlelen
) {
63 return (void*) haystack
;
65 if (haystacklen
< needlelen
)
71 return memmem(haystack
, haystacklen
, needle
, needlelen
);
74 #if HAVE_EXPLICIT_BZERO
75 static inline void* explicit_bzero_safe(void *p
, size_t l
) {
82 void *explicit_bzero_safe(void *p
, size_t l
);
85 static inline void* erase_and_free(void *p
) {
91 l
= malloc_usable_size(p
);
92 explicit_bzero_safe(p
, l
);
96 static inline void erase_and_freep(void *p
) {
97 erase_and_free(*(void**) p
);
100 /* Use with _cleanup_ to erase a single 'char' when leaving scope */
101 static inline void erase_char(char *p
) {
102 explicit_bzero_safe(p
, sizeof(char));