udev: String substitutions can be done in ENV, too
[systemd_ALT.git] / src / basic / memory-util.h
blobd26a0918e1adfe0dc2593d1f910ea1da9b8a3443
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
4 #include <inttypes.h>
5 #include <malloc.h>
6 #include <stdbool.h>
7 #include <string.h>
8 #include <sys/types.h>
10 #include "alloc-util.h"
11 #include "macro.h"
12 #include "memory-util-fundamental.h"
14 size_t page_size(void) _pure_;
15 #define PAGE_ALIGN(l) ALIGN_TO((l), page_size())
16 #define PAGE_ALIGN_DOWN(l) ((l) & ~(page_size() - 1))
17 #define PAGE_OFFSET(l) ((l) & (page_size() - 1))
19 /* Normal memcpy() requires src to be nonnull. We do nothing if n is 0. */
20 static inline void *memcpy_safe(void *dst, const void *src, size_t n) {
21 if (n == 0)
22 return dst;
23 assert(src);
24 return memcpy(dst, src, n);
27 /* Normal mempcpy() requires src to be nonnull. We do nothing if n is 0. */
28 static inline void *mempcpy_safe(void *dst, const void *src, size_t n) {
29 if (n == 0)
30 return dst;
31 assert(src);
32 return mempcpy(dst, src, n);
35 /* Normal memcmp() requires s1 and s2 to be nonnull. We do nothing if n is 0. */
36 static inline int memcmp_safe(const void *s1, const void *s2, size_t n) {
37 if (n == 0)
38 return 0;
39 assert(s1);
40 assert(s2);
41 return memcmp(s1, s2, n);
44 /* Compare s1 (length n1) with s2 (length n2) in lexicographic order. */
45 static inline int memcmp_nn(const void *s1, size_t n1, const void *s2, size_t n2) {
46 return memcmp_safe(s1, s2, MIN(n1, n2))
47 ?: CMP(n1, n2);
50 #define memzero(x,l) \
51 ({ \
52 size_t _l_ = (l); \
53 if (_l_ > 0) \
54 memset(x, 0, _l_); \
57 #define zero(x) (memzero(&(x), sizeof(x)))
59 bool memeqbyte(uint8_t byte, const void *data, size_t length);
61 #define memeqzero(data, length) memeqbyte(0x00, data, length)
63 #define eqzero(x) memeqzero(x, sizeof(x))
65 static inline void *mempset(void *s, int c, size_t n) {
66 memset(s, c, n);
67 return (uint8_t*)s + n;
70 /* Normal memmem() requires haystack to be nonnull, which is annoying for zero-length buffers */
71 static inline void *memmem_safe(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen) {
73 if (needlelen <= 0)
74 return (void*) haystack;
76 if (haystacklen < needlelen)
77 return NULL;
79 assert(haystack);
80 assert(needle);
82 return memmem(haystack, haystacklen, needle, needlelen);
85 static inline void *mempmem_safe(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen) {
86 const uint8_t *p;
88 p = memmem_safe(haystack, haystacklen, needle, needlelen);
89 if (!p)
90 return NULL;
92 return (uint8_t*) p + needlelen;
95 static inline void* erase_and_free(void *p) {
96 size_t l;
98 if (!p)
99 return NULL;
101 l = MALLOC_SIZEOF_SAFE(p);
102 explicit_bzero_safe(p, l);
103 return mfree(p);
106 static inline void erase_and_freep(void *p) {
107 erase_and_free(*(void**) p);
110 /* Use with _cleanup_ to erase a single 'char' when leaving scope */
111 static inline void erase_char(char *p) {
112 explicit_bzero_safe(p, sizeof(char));
115 /* An automatic _cleanup_-like logic for destroy arrays (i.e. pointers + size) when leaving scope */
116 typedef struct ArrayCleanup {
117 void **parray;
118 size_t *pn;
119 free_array_func_t pfunc;
120 } ArrayCleanup;
122 static inline void array_cleanup(const ArrayCleanup *c) {
123 assert(c);
125 assert(!c->parray == !c->pn);
127 if (!c->parray)
128 return;
130 if (*c->parray) {
131 assert(c->pfunc);
132 c->pfunc(*c->parray, *c->pn);
133 *c->parray = NULL;
136 *c->pn = 0;
139 #define CLEANUP_ARRAY(array, n, func) \
140 _cleanup_(array_cleanup) _unused_ const ArrayCleanup CONCATENATE(_cleanup_array_, UNIQ) = { \
141 .parray = (void**) &(array), \
142 .pn = &(n), \
143 .pfunc = (free_array_func_t) ({ \
144 void (*_f)(typeof(array[0]) *a, size_t b) = func; \
145 _f; \
146 }), \