Line data Source code
1 : /* SPDX-License-Identifier: GPL-2.0-only */
2 : /*
3 : * Copyright (c) 2023 Meta Platforms, Inc. and affiliates.
4 : */
5 :
6 : #pragma once
7 :
8 : #include <assert.h>
9 : #include <errno.h>
10 : #include <stdbool.h>
11 : #include <stddef.h>
12 : #include <stdlib.h>
13 : #include <string.h>
14 :
15 : extern const char *strerrordesc_np(int errnum);
16 :
17 : #define _BF_APPLY0(t, s, dummy)
18 : #define _BF_APPLY1(t, s, a) t(a)
19 : #define _BF_APPLY2(t, s, a, b) t(a) s t(b)
20 : #define _BF_APPLY3(t, s, a, b, c) \
21 : t(a) s t(b) \
22 : s t(c)
23 : #define _BF_APPLY4(t, s, a, b, c, d) \
24 : t(a) s t(b) \
25 : s t(c) \
26 : s t(d)
27 : #define _BF_APPLY5(t, s, a, b, c, d, e) \
28 : t(a) s t(b) \
29 : s t(c) \
30 : s t(d) \
31 : s t(e)
32 : #define _BF_APPLY6(t, s, a, b, c, d, e, f) \
33 : t(a) s t(b) \
34 : s t(c) \
35 : s t(d) \
36 : s t(e) \
37 : s t(f)
38 : #define _BF_APPLY7(t, s, a, b, c, d, e, f, g) \
39 : t(a) s t(b) \
40 : s t(c) \
41 : s t(d) \
42 : s t(e) \
43 : s t(f) \
44 : s t(g)
45 : #define _BF_APPLY8(t, s, a, b, c, d, e, f, g, h) \
46 : t(a) s t(b) \
47 : s t(c) \
48 : s t(d) \
49 : s t(e) \
50 : s t(f) \
51 : s t(g) \
52 : s t(h)
53 : #define _BF_APPLY9(t, s, a, b, c, d, e, f, g, h, i) \
54 : t(a) s t(b) \
55 : s t(c) \
56 : s t(d) \
57 : s t(e) \
58 : s t(f) \
59 : s t(g) \
60 : s t(h) \
61 : s t(i)
62 : #define _BF_APPLY10(t, s, a, b, c, d, e, f, g, h, i, j) \
63 : t(a) s t(b) \
64 : s t(c) \
65 : s t(d) \
66 : s t(e) \
67 : s t(f) \
68 : s t(g) \
69 : s t(h) \
70 : s t(i) \
71 : s t(j)
72 :
73 : #define __BF_NUM_ARGS1(dummy, x10, x9, x8, x7, x6, x5, x4, x3, x2, x1, x0, \
74 : ...) \
75 : x0
76 : #define _BF_NUM_ARGS(...) \
77 : __BF_NUM_ARGS1(dummy, ##__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
78 : #define ___BF_APPLY_ALL(t, s, n, ...) _BF_APPLY##n(t, s, __VA_ARGS__)
79 : #define __BF_APPLY_ALL(t, s, n, ...) ___BF_APPLY_ALL(t, s, n, __VA_ARGS__)
80 : #define _BF_APPLY_ALL(t, s, ...) \
81 : __BF_APPLY_ALL(t, s, _BF_NUM_ARGS(__VA_ARGS__), __VA_ARGS__)
82 :
83 : /**
84 : * @brief Generate a bitflag from multiple values.
85 : *
86 : * Enumeration are used extensively to define related values. Thanks to
87 : * enumeration's continuous values, they are used as array indexes to convert
88 : * them into strings.
89 : *
90 : * However, they can sometimes be combined, leading to very wordy code, e.g.
91 : * `1 << ENUM_VAL_1 | 1 << ENUM_VAL_5`.
92 : *
93 : * `BF_FLAGS` can be used to replace the wordy code with a simpler macro call,
94 : * e.g. `BF_FLAGS(ENUL_VAL_1, ENUM_VAL_5)`. It will automatically create an
95 : * integer with the enumeration values as a set bit index in the bitflag.
96 : *
97 : * @return Bitflag for variadic values.
98 : */
99 : #define BF_FLAGS(...) _BF_APPLY_ALL(BF_FLAG, |, __VA_ARGS__)
100 :
101 : /**
102 : * @brief Shift 1 by `n` to create a flag.
103 : *
104 : * @see `BF_FLAGS`
105 : *
106 : * @return `1 << n` to be used as a flag.
107 : */
108 : #define BF_FLAG(n) (1 << (n))
109 :
110 : #define bf_packed __attribute__((packed))
111 : #define bf_aligned(x) __attribute__((aligned(x)))
112 : #define bf_unused __attribute__((unused))
113 :
114 : #ifndef bf_assert
115 : #define bf_assert(x) assert(x)
116 : #endif
117 :
118 : #define BF_STR(s) _BF_STR(s)
119 : #define _BF_STR(s) #s
120 :
121 : /**
122 : * Mark a variable as unused, to prevent the compiler from emitting a warning.
123 : *
124 : * @param x The variable to mark as unused.
125 : */
126 : #define UNUSED(x) (void)(x)
127 :
128 : /**
129 : * Set @p ptr to NULL and return its previous value.
130 : *
131 : * Inspired from systemd's TAKE_PTR() macro, which is itself inspired from
132 : * Rust's Option::take() method:
133 : * https://doc.rust-lang.org/std/option/enum.Option.html#method.take
134 : *
135 : * @param var Variable to return the value of.
136 : * @param type Type of @p var.
137 : * @param nullvalue Value to set @p var to.
138 : * @return Value of @p var before it was set to @p nullvalue.
139 : */
140 : #define TAKE_GENERIC(var, type, nullvalue) \
141 : ({ \
142 : /* NOLINTBEGIN: do not enclose 'type' in parentheses */ \
143 : type *_pvar_ = &(var); \
144 : type _var_ = *_pvar_; \
145 : type _nullvalue_ = nullvalue; \
146 : /* NOLINTEND */ \
147 : *_pvar_ = _nullvalue_; \
148 : _var_; \
149 : })
150 :
151 : #define TAKE_PTR_TYPE(ptr, type) TAKE_GENERIC(ptr, type, NULL)
152 : #define TAKE_PTR(ptr) TAKE_PTR_TYPE(ptr, typeof(ptr))
153 : #define TAKE_STRUCT_TYPE(s, type) TAKE_GENERIC(s, type, {})
154 : #define TAKE_STRUCT(s) TAKE_STRUCT_TYPE(s, typeof(s))
155 : #define TAKE_FD(fd) TAKE_GENERIC(fd, int, -1)
156 :
157 : /**
158 : * Get the number of element in an array.
159 : *
160 : * @param x The array.
161 : * @return size_t The number of elements in the array.
162 : */
163 : #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
164 :
165 : #define _cleanup_free_ __attribute__((__cleanup__(freep)))
166 : #define _cleanup_close_ __attribute__((__cleanup__(closep)))
167 :
168 : /**
169 : * Return a string describing the given error code.
170 : *
171 : * This function must be used over strerror(), which is marked at mt-unsafe.
172 : *
173 : * @param v Error code, can be positive or negative.
174 : */
175 : #define bf_strerror(v) strerrordesc_np(abs(v))
176 :
177 : /**
178 : * Swap two values.
179 : *
180 : * @param a First value to swap.
181 : * @param b Second value to swap.
182 : */
183 : #define bf_swap(a, b) \
184 : do { \
185 : typeof(a) __a = (a); \
186 : (a) = (b); \
187 : (b) = __a; \
188 : } while (0)
189 :
190 : #define bf_min(a, b) \
191 : ({ \
192 : __typeof__(a) _a = (a); \
193 : __typeof__(b) _b = (b); \
194 : _a < _b ? _a : _b; \
195 : })
196 :
197 : /**
198 : * Free a pointer and set it to NULL.
199 : *
200 : * @param ptr Pointer to free.
201 : */
202 84 : static inline void freep(void *ptr)
203 : {
204 544 : free(*(void **)ptr);
205 451 : *(void **)ptr = NULL;
206 126 : }
207 :
208 : /**
209 : * Close a file descriptor and set it to -1.
210 : *
211 : * `bpfilter` uses `-1` as neutral value for file descriptor, meaning it
212 : * doesn't represent an open file yet. Once closed, a file descriptor should
213 : * be reset to `-1`.
214 : *
215 : * `closep` is used to close a file descriptor. If the file descriptor is
216 : * `-1`, then nothing it done. Otherwise, it is closed and reset to `-1`.
217 : *
218 : * If the call to `close` fails, a warning is printed, and the file descriptor
219 : * is assumed to be already closed.
220 : *
221 : * @param fd File descriptor to close. Can't be NULL.
222 : */
223 : void closep(int *fd);
224 :
225 : /**
226 : * Duplicate a memory region.
227 : *
228 : * Allocate a new buffer of size @p len and copy @p src into it. Requirements
229 : * applicable to @p src and @p len:
230 : * - If @p src is NULL, @p len must be 0. In this case, NULL is returned.
231 : * - If @p src is non-NULL, a new buffer of @p len bytes will be allocated to
232 : * store the first @p len bytes of @p src. This new buffer is then returned.
233 : *
234 : * Unless NULL is returned, the new buffer is owned by the caller.
235 : *
236 : * @param src Source buffer to copy to @p dst.
237 : * @param len Number of bytes to copy to @p dst.
238 : * @return Pointer to the new buffer, or NULL on failure.
239 : */
240 0 : static inline void *bf_memdup(const void *src, size_t len)
241 : {
242 : void *dst;
243 :
244 0 : if (!src)
245 : return NULL;
246 :
247 0 : dst = malloc(len);
248 0 : if (!dst)
249 : return NULL;
250 :
251 0 : return memcpy(dst, src, len);
252 : }
253 :
254 : /**
255 : * Copy @p len bytes from @p src to @p dst.
256 : *
257 : * Allow for @p src to be NULL and/or @p len to be zero:
258 : * - If @p src is NULL, @p len must be equal 0. @p dst is not modified.
259 : * - If @p src is not NULL, @p len can be equal to 0, in which case @p dst is
260 : * not modified.
261 : *
262 : * @param dst Destination buffer. Can't be NULL, and must be big enough to store
263 : * @p len bytes from @p src.
264 : * @param src Source buffer to copy to @p dst.
265 : * @param len Number of bytes to copy to @p dst.
266 : * @return Pointer to @p dst.
267 : */
268 168 : static inline void *bf_memcpy(void *dst, const void *src, size_t len)
269 : {
270 168 : bf_assert(dst);
271 168 : bf_assert(src ? 1 : len == 0);
272 :
273 168 : if (!src || !len)
274 : return dst;
275 :
276 123 : return memcpy(dst, src, len);
277 : }
278 :
279 : /**
280 : * Reallocate @p ptr into a new buffer of size @p size.
281 : *
282 : * Behaves similarly to realloc(), except that @p ptr is left unchanged if
283 : * allocation fails, and an error is returned.
284 : *
285 : * @param ptr Memory buffer to grow. Can't be NULL.
286 : * @param size New size of the memory buffer.
287 : * @return 0 on success, or a negative errno value on failure.
288 : */
289 4 : static inline int bf_realloc(void **ptr, size_t size)
290 : {
291 : _cleanup_free_ void *_ptr;
292 :
293 4 : bf_assert(ptr);
294 :
295 4 : _ptr = realloc(*ptr, size);
296 4 : if (!_ptr)
297 : return -ENOMEM;
298 :
299 4 : *ptr = TAKE_PTR(_ptr);
300 :
301 4 : return 0;
302 : }
303 :
304 : /**
305 : * Returns true if @p a is equal to @p b.
306 : *
307 : * @param lhs First string.
308 : * @param rhs Second string.
309 : * @return True if @p a == @p b, false otherwise.
310 : */
311 : static inline bool bf_streq(const char *lhs, const char *rhs) // NOLINT
312 : {
313 268 : return strcmp(lhs, rhs) == 0;
314 : }
315 :
316 : /**
317 : * Copy a string to a buffer.
318 : *
319 : * @p src is copied to @p dst . If @p src is too long, at most @p len bytes are
320 : * copied (including the termination character).
321 : *
322 : * @param dst Destination buffer. Can't be NULL.
323 : * @param len Length of the destination buffer. The function will not copy more
324 : * than @p len bytes to @p dst , including @c \0 . Can't be 0.
325 : * @param src Soucre buffer to copy from. Will only be copied up to the
326 : * termination character if it fits. Can't be NULL.
327 : * @return 0 on success, or @c -E2BIG if @p src can't fit in @p dst .
328 : */
329 : int bf_strncpy(char *dst, size_t len, const char *src);
330 :
331 : /**
332 : * Read the contents of a file into a buffer.
333 : *
334 : * @param path Path to the file to read. Can't be NULL.
335 : * @param buf Pointer to a pointer to a buffer. The buffer will be allocated
336 : * automatically. The caller is responsible to free it. If @ref bf_read_file
337 : * fails, @p buf is left unchanged.
338 : * @param len Length of the allocated buffer. Populated by the function.
339 : * @return 0 on success, negative errno value on error.
340 : */
341 : int bf_read_file(const char *path, void **buf, size_t *len);
342 :
343 : /**
344 : * Write the contents of a buffer into a file.
345 : *
346 : * @param path Path to the file to write. Can't be NULL.
347 : * @param buf Buffer to write.
348 : * @param len Number of bytes to write the to file.
349 : * @return 0 on success, negative errno value on error.
350 : */
351 : int bf_write_file(const char *path, const void *buf, size_t len);
|