Debian Codegraph @

src/fundamental/memory-util.h

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include <stddef.h>
5
6 #if SD_BOOT
7 # include "efi-string.h"
8 #else
9 # include <string.h>
10 #endif
11
12 #include "assert-util.h" /* IWYU pragma: keep */
13 #include "macro.h"
14
15 #define memzero(x, l) \
16 ({ \
17 size_t _l_ = (l); \
18 _l_ > 0 ? memset((x), 0, _l_) : (x); \
19 })
20
21 #if !SD_BOOT
22 static inline void *explicit_bzero_safe(void *p, size_t l) {
23 if (p && l > 0)
24 explicit_bzero(p, l);
25
26 return p;
27 }
28 #else
29 static inline void *explicit_bzero_safe(void *p, size_t l) {
30 if (p && l > 0) {
31 memset(p, 0, l);
32 __asm__ __volatile__("" : : "r"(p) : "memory");
33 }
34 return p;
35 }
36 #endif
37
38 struct VarEraser {
39 /* NB: This is a pointer to memory to erase in case of CLEANUP_ERASE(). Pointer to pointer to memory
40 * to erase in case of CLEANUP_ERASE_PTR() */
41 void *p;
42 size_t size;
43 };
44
45 static inline void erase_var(struct VarEraser *e) {
46 explicit_bzero_safe(e->p, e->size);
47 }
48
49 /* Mark var to be erased when leaving scope. */
50 #define CLEANUP_ERASE(var) \
51 _cleanup_(erase_var) _unused_ struct VarEraser CONCATENATE(_eraser_, UNIQ) = { \
52 .p = &(var), \
53 .size = sizeof(var), \
54 }
55
56 static inline void erase_varp(struct VarEraser *e) {
57
58 /* Very similar to erase_var(), but assumes `p` is a pointer to a pointer whose memory shall be destructed. */
59 if (!e->p)
60 return;
61
62 explicit_bzero_safe(*(void**) e->p, e->size);
63 }
64
65 /* Mark pointer so that memory pointed to is erased when leaving scope. Note: this takes a pointer to the
66 * specified pointer, instead of just a copy of it. This is to allow callers to invalidate the pointer after
67 * use, if they like, disabling our automatic erasure (for example because they succeeded with whatever they
68 * wanted to do and now intend to return the allocated buffer to their caller without it being erased). */
69 #define CLEANUP_ERASE_PTR(ptr, sz) \
70 _cleanup_(erase_varp) _unused_ struct VarEraser CONCATENATE(_eraser_, UNIQ) = { \
71 .p = (ptr), \
72 .size = (sz), \
73 }
74
75 static inline size_t ALIGN_TO(size_t l, size_t ali) {
76 assert(ISPOWEROF2(ali));
77
78 if (l > SIZE_MAX - (ali - 1))
79 return SIZE_MAX; /* indicate overflow */
80
81 return ((l + (ali - 1)) & ~(ali - 1));
82 }
83
84 static inline uint64_t ALIGN_TO_U64(uint64_t l, uint64_t ali) {
85 assert(ISPOWEROF2(ali));
86
87 if (l > UINT64_MAX - (ali - 1))
88 return UINT64_MAX; /* indicate overflow */
89
90 return ((l + (ali - 1)) & ~(ali - 1));
91 }
92
93 static inline size_t ALIGN_DOWN(size_t l, size_t ali) {
94 assert(ISPOWEROF2(ali));
95
96 return l & ~(ali - 1);
97 }
98
99 static inline uint64_t ALIGN_DOWN_U64(uint64_t l, uint64_t ali) {
100 assert(ISPOWEROF2(ali));
101
102 return l & ~(ali - 1);
103 }
104
105 static inline size_t ALIGN_OFFSET(size_t l, size_t ali) {
106 assert(ISPOWEROF2(ali));
107
108 return l & (ali - 1);
109 }
110
111 static inline uint64_t ALIGN_OFFSET_U64(uint64_t l, uint64_t ali) {
112 assert(ISPOWEROF2(ali));
113
114 return l & (ali - 1);
115 }
116
117 #define ALIGN2(l) ALIGN_TO(l, 2)
118 #define ALIGN4(l) ALIGN_TO(l, 4)
119 #define ALIGN8(l) ALIGN_TO(l, 8)
120 #define ALIGN2_PTR(p) ((void*) ALIGN2((uintptr_t) p))
121 #define ALIGN4_PTR(p) ((void*) ALIGN4((uintptr_t) p))
122 #define ALIGN8_PTR(p) ((void*) ALIGN8((uintptr_t) p))
123 #define ALIGN(l) ALIGN_TO(l, sizeof(void*))
124 #define ALIGN_PTR(p) ((void*) ALIGN((uintptr_t) (p)))
125
126 /* Checks if the specified pointer is aligned as appropriate for the specific type */
127 #define IS_ALIGNED16(p) (((uintptr_t) p) % alignof(uint16_t) == 0)
128 #define IS_ALIGNED32(p) (((uintptr_t) p) % alignof(uint32_t) == 0)
129 #define IS_ALIGNED64(p) (((uintptr_t) p) % alignof(uint64_t) == 0)
130
131 /* Same as ALIGN_TO but callable in constant contexts. */
132 #define CONST_ALIGN_TO(l, ali) \
133 __builtin_choose_expr( \
134 __builtin_constant_p(l) && \
135 __builtin_constant_p(ali) && \
136 CONST_ISPOWEROF2(ali) && \
137 (l <= SIZE_MAX - (ali - 1)), /* overflow? */ \
138 ((l) + (ali) - 1) & ~((ali) - 1), \
139 VOID_0)
140
141 /* Similar to ((t *) (void *) (p)) to cast a pointer. The macro asserts that the pointer has a suitable
142 * alignment for type "t". This exists for places where otherwise "-Wcast-align=strict" would issue a
143 * warning or if you want to assert that the cast gives a pointer of suitable alignment. */
144 #define CAST_ALIGN_PTR(t, p) \
145 ({ \
146 const void *_p = (p); \
147 assert(((uintptr_t) _p) % alignof(t) == 0); \
148 (t *) _p; \
149 })
150
151 bool memeqbyte(uint8_t byte, const void *data, size_t length) _nonnull_if_nonzero_(2, 3);
152 #define memeqzero(data, length) memeqbyte(0x00, data, length)
153 #define eqzero(x) memeqzero(x, sizeof(x))