]>
Commit | Line | Data |
---|---|---|
a032b68d | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
bb4f798a MB |
2 | #pragma once |
3 | ||
4 | #include <inttypes.h> | |
f2dec872 | 5 | #include <malloc.h> |
bb4f798a MB |
6 | #include <stdbool.h> |
7 | #include <string.h> | |
8 | #include <sys/types.h> | |
9 | ||
46cdbd49 | 10 | #include "alloc-util.h" |
bb4f798a MB |
11 | #include "macro.h" |
12 | ||
13 | size_t page_size(void) _pure_; | |
14 | #define PAGE_ALIGN(l) ALIGN_TO((l), page_size()) | |
a10f5d05 MB |
15 | #define PAGE_ALIGN_DOWN(l) ((l) & ~(page_size() - 1)) |
16 | #define PAGE_OFFSET(l) ((l) & (page_size() - 1)) | |
bb4f798a MB |
17 | |
18 | /* Normal memcpy requires src to be nonnull. We do nothing if n is 0. */ | |
8b3d4ff0 | 19 | static inline void *memcpy_safe(void *dst, const void *src, size_t n) { |
bb4f798a | 20 | if (n == 0) |
8b3d4ff0 | 21 | return dst; |
bb4f798a | 22 | assert(src); |
8b3d4ff0 | 23 | return memcpy(dst, src, n); |
bb4f798a MB |
24 | } |
25 | ||
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) { | |
28 | if (n == 0) | |
29 | return 0; | |
30 | assert(s1); | |
31 | assert(s2); | |
32 | return memcmp(s1, s2, n); | |
33 | } | |
34 | ||
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)) | |
38 | ?: CMP(n1, n2); | |
39 | } | |
40 | ||
41 | #define memzero(x,l) \ | |
42 | ({ \ | |
43 | size_t _l_ = (l); \ | |
f2dec872 BR |
44 | if (_l_ > 0) \ |
45 | memset(x, 0, _l_); \ | |
bb4f798a MB |
46 | }) |
47 | ||
48 | #define zero(x) (memzero(&(x), sizeof(x))) | |
49 | ||
50 | bool memeqzero(const void *data, size_t length); | |
51 | ||
52 | #define eqzero(x) memeqzero(x, sizeof(x)) | |
53 | ||
54 | static inline void *mempset(void *s, int c, size_t n) { | |
55 | memset(s, c, n); | |
56 | return (uint8_t*)s + n; | |
57 | } | |
58 | ||
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) { | |
61 | ||
62 | if (needlelen <= 0) | |
63 | return (void*) haystack; | |
64 | ||
65 | if (haystacklen < needlelen) | |
66 | return NULL; | |
67 | ||
68 | assert(haystack); | |
69 | assert(needle); | |
70 | ||
71 | return memmem(haystack, haystacklen, needle, needlelen); | |
72 | } | |
73 | ||
74 | #if HAVE_EXPLICIT_BZERO | |
75 | static inline void* explicit_bzero_safe(void *p, size_t l) { | |
76 | if (l > 0) | |
77 | explicit_bzero(p, l); | |
78 | ||
79 | return p; | |
80 | } | |
81 | #else | |
82 | void *explicit_bzero_safe(void *p, size_t l); | |
83 | #endif | |
84 | ||
46cdbd49 BR |
85 | static inline void* erase_and_free(void *p) { |
86 | size_t l; | |
87 | ||
88 | if (!p) | |
89 | return NULL; | |
f2dec872 | 90 | |
8b3d4ff0 | 91 | l = MALLOC_SIZEOF_SAFE(p); |
46cdbd49 BR |
92 | explicit_bzero_safe(p, l); |
93 | return mfree(p); | |
94 | } | |
95 | ||
96 | static inline void erase_and_freep(void *p) { | |
97 | erase_and_free(*(void**) p); | |
f2dec872 BR |
98 | } |
99 | ||
bb4f798a MB |
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)); | |
103 | } |