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 : #define bf_max(a, b) \
198 : ({ \
199 : __typeof__(a) _a = (a); \
200 : __typeof__(b) _b = (b); \
201 : _a > _b ? _a : _b; \
202 : })
203 :
204 : /**
205 : * Free a pointer and set it to NULL.
206 : *
207 : * @param ptr Pointer to free.
208 : */
209 84 : static inline void freep(void *ptr)
210 : {
211 589 : free(*(void **)ptr);
212 499 : *(void **)ptr = NULL;
213 130 : }
214 :
215 : /**
216 : * Close a file descriptor and set it to -1.
217 : *
218 : * `bpfilter` uses `-1` as neutral value for file descriptor, meaning it
219 : * doesn't represent an open file yet. Once closed, a file descriptor should
220 : * be reset to `-1`.
221 : *
222 : * `closep` is used to close a file descriptor. If the file descriptor is
223 : * `-1`, then nothing it done. Otherwise, it is closed and reset to `-1`.
224 : *
225 : * If the call to `close` fails, a warning is printed, and the file descriptor
226 : * is assumed to be already closed.
227 : *
228 : * @param fd File descriptor to close. Can't be NULL.
229 : */
230 : void closep(int *fd);
231 :
232 : /**
233 : * Duplicate a memory region.
234 : *
235 : * Allocate a new buffer of size @p len and copy @p src into it. Requirements
236 : * applicable to @p src and @p len:
237 : * - If @p src is NULL, @p len must be 0. In this case, NULL is returned.
238 : * - If @p src is non-NULL, a new buffer of @p len bytes will be allocated to
239 : * store the first @p len bytes of @p src. This new buffer is then returned.
240 : *
241 : * Unless NULL is returned, the new buffer is owned by the caller.
242 : *
243 : * @param src Source buffer to copy to @p dst.
244 : * @param len Number of bytes to copy to @p dst.
245 : * @return Pointer to the new buffer, or NULL on failure.
246 : */
247 0 : static inline void *bf_memdup(const void *src, size_t len)
248 : {
249 : void *dst;
250 :
251 0 : if (!src)
252 : return NULL;
253 :
254 0 : dst = malloc(len);
255 0 : if (!dst)
256 : return NULL;
257 :
258 0 : return memcpy(dst, src, len);
259 : }
260 :
261 : /**
262 : * Copy @p len bytes from @p src to @p dst.
263 : *
264 : * Allow for @p src to be NULL and/or @p len to be zero:
265 : * - If @p src is NULL, @p len must be equal 0. @p dst is not modified.
266 : * - If @p src is not NULL, @p len can be equal to 0, in which case @p dst is
267 : * not modified.
268 : *
269 : * @param dst Destination buffer. Can't be NULL, and must be big enough to store
270 : * @p len bytes from @p src.
271 : * @param src Source buffer to copy to @p dst.
272 : * @param len Number of bytes to copy to @p dst.
273 : * @return Pointer to @p dst.
274 : */
275 284 : static inline void *bf_memcpy(void *dst, const void *src, size_t len)
276 : {
277 284 : bf_assert(dst);
278 284 : bf_assert(src ? 1 : len == 0);
279 :
280 284 : if (!src || !len)
281 : return dst;
282 :
283 216 : return memcpy(dst, src, len);
284 : }
285 :
286 : /**
287 : * Reallocate @p ptr into a new buffer of size @p size.
288 : *
289 : * Behaves similarly to realloc(), except that @p ptr is left unchanged if
290 : * allocation fails, and an error is returned.
291 : *
292 : * @param ptr Memory buffer to grow. Can't be NULL.
293 : * @param size New size of the memory buffer.
294 : * @return 0 on success, or a negative errno value on failure.
295 : */
296 4 : static inline int bf_realloc(void **ptr, size_t size)
297 : {
298 : _cleanup_free_ void *_ptr;
299 :
300 4 : bf_assert(ptr);
301 :
302 4 : _ptr = realloc(*ptr, size);
303 4 : if (!_ptr)
304 : return -ENOMEM;
305 :
306 4 : *ptr = TAKE_PTR(_ptr);
307 :
308 4 : return 0;
309 : }
310 :
311 : /**
312 : * Returns true if @p a is equal to @p b.
313 : *
314 : * @param lhs First string.
315 : * @param rhs Second string.
316 : * @return True if @p a == @p b, false otherwise.
317 : */
318 : static inline bool bf_streq(const char *lhs, const char *rhs) // NOLINT
319 : {
320 268 : return strcmp(lhs, rhs) == 0;
321 : }
322 :
323 : /**
324 : * Copy a string to a buffer.
325 : *
326 : * @p src is copied to @p dst . If @p src is too long, at most @p len bytes are
327 : * copied (including the termination character).
328 : *
329 : * @param dst Destination buffer. Can't be NULL.
330 : * @param len Length of the destination buffer. The function will not copy more
331 : * than @p len bytes to @p dst , including @c \0 . Can't be 0.
332 : * @param src Soucre buffer to copy from. Will only be copied up to the
333 : * termination character if it fits. Can't be NULL.
334 : * @return 0 on success, or @c -E2BIG if @p src can't fit in @p dst .
335 : */
336 : int bf_strncpy(char *dst, size_t len, const char *src);
337 :
338 : /**
339 : * Read the contents of a file into a buffer.
340 : *
341 : * @param path Path to the file to read. Can't be NULL.
342 : * @param buf Pointer to a pointer to a buffer. The buffer will be allocated
343 : * automatically. The caller is responsible to free it. If @ref bf_read_file
344 : * fails, @p buf is left unchanged.
345 : * @param len Length of the allocated buffer. Populated by the function.
346 : * @return 0 on success, negative errno value on error.
347 : */
348 : int bf_read_file(const char *path, void **buf, size_t *len);
349 :
350 : /**
351 : * Write the contents of a buffer into a file.
352 : *
353 : * @param path Path to the file to write. Can't be NULL.
354 : * @param buf Buffer to write.
355 : * @param len Number of bytes to write the to file.
356 : * @return 0 on success, negative errno value on error.
357 : */
358 : int bf_write_file(const char *path, const void *buf, size_t len);
|