]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
0a970718 LP |
2 | #pragma once |
3 | ||
4 | #include <inttypes.h> | |
44c786f0 | 5 | #include <malloc.h> |
0a970718 LP |
6 | #include <stdbool.h> |
7 | #include <string.h> | |
8 | #include <sys/types.h> | |
9 | ||
e514aa1e | 10 | #include "alloc-util.h" |
0a970718 | 11 | #include "macro.h" |
3f92dc2f | 12 | #include "memory-util-fundamental.h" |
0a970718 LP |
13 | |
14 | size_t page_size(void) _pure_; | |
2977904c YW |
15 | #define PAGE_ALIGN(l) ALIGN_TO(l, page_size()) |
16 | #define PAGE_ALIGN_U64(l) ALIGN_TO_U64(l, page_size()) | |
17 | #define PAGE_ALIGN_DOWN(l) ALIGN_DOWN(l, page_size()) | |
18 | #define PAGE_ALIGN_DOWN_U64(l) ALIGN_DOWN_U64(l, page_size()) | |
19 | #define PAGE_OFFSET(l) ALIGN_OFFSET(l, page_size()) | |
20 | #define PAGE_OFFSET_U64(l) ALIGN_OFFSET_U64(l, page_size()) | |
0a970718 | 21 | |
507cd760 | 22 | /* Normal memcpy() requires src to be nonnull. We do nothing if n is 0. */ |
d1f3b080 | 23 | static inline void *memcpy_safe(void *dst, const void *src, size_t n) { |
0a970718 | 24 | if (n == 0) |
d1f3b080 | 25 | return dst; |
0a970718 | 26 | assert(src); |
d1f3b080 | 27 | return memcpy(dst, src, n); |
0a970718 LP |
28 | } |
29 | ||
507cd760 YW |
30 | /* Normal mempcpy() requires src to be nonnull. We do nothing if n is 0. */ |
31 | static inline void *mempcpy_safe(void *dst, const void *src, size_t n) { | |
32 | if (n == 0) | |
33 | return dst; | |
34 | assert(src); | |
35 | return mempcpy(dst, src, n); | |
36 | } | |
37 | ||
38 | /* Normal memcmp() requires s1 and s2 to be nonnull. We do nothing if n is 0. */ | |
0a970718 LP |
39 | static inline int memcmp_safe(const void *s1, const void *s2, size_t n) { |
40 | if (n == 0) | |
41 | return 0; | |
42 | assert(s1); | |
43 | assert(s2); | |
44 | return memcmp(s1, s2, n); | |
45 | } | |
46 | ||
47 | /* Compare s1 (length n1) with s2 (length n2) in lexicographic order. */ | |
48 | static inline int memcmp_nn(const void *s1, size_t n1, const void *s2, size_t n2) { | |
49 | return memcmp_safe(s1, s2, MIN(n1, n2)) | |
50 | ?: CMP(n1, n2); | |
51 | } | |
52 | ||
0a970718 LP |
53 | #define zero(x) (memzero(&(x), sizeof(x))) |
54 | ||
3f9992d8 LP |
55 | bool memeqbyte(uint8_t byte, const void *data, size_t length); |
56 | ||
57 | #define memeqzero(data, length) memeqbyte(0x00, data, length) | |
0a970718 LP |
58 | |
59 | #define eqzero(x) memeqzero(x, sizeof(x)) | |
60 | ||
61 | static inline void *mempset(void *s, int c, size_t n) { | |
62 | memset(s, c, n); | |
63 | return (uint8_t*)s + n; | |
64 | } | |
090a9c1e LP |
65 | |
66 | /* Normal memmem() requires haystack to be nonnull, which is annoying for zero-length buffers */ | |
67 | static inline void *memmem_safe(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen) { | |
68 | ||
69 | if (needlelen <= 0) | |
70 | return (void*) haystack; | |
71 | ||
72 | if (haystacklen < needlelen) | |
73 | return NULL; | |
74 | ||
75 | assert(haystack); | |
76 | assert(needle); | |
77 | ||
78 | return memmem(haystack, haystacklen, needle, needlelen); | |
79 | } | |
80 | ||
d8782cc5 LP |
81 | static inline void *mempmem_safe(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen) { |
82 | const uint8_t *p; | |
83 | ||
84 | p = memmem_safe(haystack, haystacklen, needle, needlelen); | |
85 | if (!p) | |
86 | return NULL; | |
87 | ||
88 | return (uint8_t*) p + needlelen; | |
89 | } | |
90 | ||
282bde10 LP |
91 | static inline void* erase_and_free(void *p) { |
92 | size_t l; | |
93 | ||
94 | if (!p) | |
95 | return NULL; | |
96 | ||
6df28e1f | 97 | l = MALLOC_SIZEOF_SAFE(p); |
282bde10 | 98 | explicit_bzero_safe(p, l); |
e514aa1e | 99 | return mfree(p); |
282bde10 LP |
100 | } |
101 | ||
102 | static inline void erase_and_freep(void *p) { | |
103 | erase_and_free(*(void**) p); | |
44c786f0 ZJS |
104 | } |
105 | ||
e1ed99c8 LP |
106 | /* Use with _cleanup_ to erase a single 'char' when leaving scope */ |
107 | static inline void erase_char(char *p) { | |
108 | explicit_bzero_safe(p, sizeof(char)); | |
109 | } | |
85686b37 VS |
110 | |
111 | /* Makes a copy of the buffer with reversed order of bytes */ | |
112 | void *memdup_reverse(const void *mem, size_t size); |