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 : #define BF_BASE_10 10
84 : #define BF_BASE_16 16
85 :
86 : /**
87 : * @brief Generate a bitflag from multiple values.
88 : *
89 : * Enumeration are used extensively to define related values. Thanks to
90 : * enumeration's continuous values, they are used as array indexes to convert
91 : * them into strings.
92 : *
93 : * However, they can sometimes be combined, leading to very wordy code, e.g.
94 : * `1 << ENUM_VAL_1 | 1 << ENUM_VAL_5`.
95 : *
96 : * `BF_FLAGS` can be used to replace the wordy code with a simpler macro call,
97 : * e.g. `BF_FLAGS(ENUL_VAL_1, ENUM_VAL_5)`. It will automatically create an
98 : * integer with the enumeration values as a set bit index in the bitflag.
99 : *
100 : * @return Bitflag for variadic values.
101 : */
102 : #define BF_FLAGS(...) _BF_APPLY_ALL(BF_FLAG, |, __VA_ARGS__)
103 :
104 : /**
105 : * @brief Shift 1 by `n` to create a flag.
106 : *
107 : * @see `BF_FLAGS`
108 : *
109 : * @return `1 << n` to be used as a flag.
110 : */
111 : #define BF_FLAG(n) (1 << (n))
112 :
113 : #define bf_packed __attribute__((packed))
114 : #define bf_aligned(x) __attribute__((aligned(x)))
115 : #define bf_unused __attribute__((unused))
116 :
117 : #ifndef bf_assert
118 : #define bf_assert(x) assert(x)
119 : #endif
120 :
121 : #define BF_STR(s) _BF_STR(s)
122 : #define _BF_STR(s) #s
123 :
124 : /**
125 : * Mark a variable as unused, to prevent the compiler from emitting a warning.
126 : *
127 : * @param x The variable to mark as unused.
128 : */
129 : #define UNUSED(x) (void)(x)
130 :
131 : /**
132 : * Set @p ptr to NULL and return its previous value.
133 : *
134 : * Inspired from systemd's TAKE_PTR() macro, which is itself inspired from
135 : * Rust's Option::take() method:
136 : * https://doc.rust-lang.org/std/option/enum.Option.html#method.take
137 : *
138 : * @param var Variable to return the value of.
139 : * @param type Type of @p var.
140 : * @param nullvalue Value to set @p var to.
141 : * @return Value of @p var before it was set to @p nullvalue.
142 : */
143 : #define TAKE_GENERIC(var, type, nullvalue) \
144 : ({ \
145 : /* NOLINTBEGIN: do not enclose 'type' in parentheses */ \
146 : type *_pvar_ = &(var); \
147 : type _var_ = *_pvar_; \
148 : type _nullvalue_ = nullvalue; \
149 : /* NOLINTEND */ \
150 : *_pvar_ = _nullvalue_; \
151 : _var_; \
152 : })
153 :
154 : #define TAKE_PTR_TYPE(ptr, type) TAKE_GENERIC(ptr, type, NULL)
155 : #define TAKE_PTR(ptr) TAKE_PTR_TYPE(ptr, typeof(ptr))
156 : #define TAKE_STRUCT_TYPE(s, type) TAKE_GENERIC(s, type, {})
157 : #define TAKE_STRUCT(s) TAKE_STRUCT_TYPE(s, typeof(s))
158 : #define TAKE_FD(fd) TAKE_GENERIC(fd, int, -1)
159 :
160 : /**
161 : * Get the number of element in an array.
162 : *
163 : * @param x The array.
164 : * @return size_t The number of elements in the array.
165 : */
166 : #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
167 :
168 : #define _cleanup_free_ __attribute__((__cleanup__(freep)))
169 : #define _cleanup_close_ __attribute__((__cleanup__(closep)))
170 :
171 : /**
172 : * Return a string describing the given error code.
173 : *
174 : * This function must be used over strerror(), which is marked at mt-unsafe.
175 : *
176 : * @param v Error code, can be positive or negative.
177 : */
178 : #define bf_strerror(v) strerrordesc_np(abs(v))
179 :
180 : /**
181 : * Swap two values.
182 : *
183 : * @param a First value to swap.
184 : * @param b Second value to swap.
185 : */
186 : #define bf_swap(a, b) \
187 : do { \
188 : typeof(a) __a = (a); \
189 : (a) = (b); \
190 : (b) = __a; \
191 : } while (0)
192 :
193 : #define bf_min(a, b) \
194 : ({ \
195 : __typeof__(a) _a = (a); \
196 : __typeof__(b) _b = (b); \
197 : _a < _b ? _a : _b; \
198 : })
199 :
200 : #define bf_max(a, b) \
201 : ({ \
202 : __typeof__(a) _a = (a); \
203 : __typeof__(b) _b = (b); \
204 : _a > _b ? _a : _b; \
205 : })
206 :
207 : /**
208 : * Free a pointer and set it to NULL.
209 : *
210 : * @param ptr Pointer to free.
211 : */
212 84 : static inline void freep(void *ptr)
213 : {
214 604 : free(*(void **)ptr);
215 513 : *(void **)ptr = NULL;
216 141 : }
217 :
218 : /**
219 : * Close a file descriptor and set it to -1.
220 : *
221 : * `bpfilter` uses `-1` as neutral value for file descriptor, meaning it
222 : * doesn't represent an open file yet. Once closed, a file descriptor should
223 : * be reset to `-1`.
224 : *
225 : * `closep` is used to close a file descriptor. If the file descriptor is
226 : * `-1`, then nothing it done. Otherwise, it is closed and reset to `-1`.
227 : *
228 : * If the call to `close` fails, a warning is printed, and the file descriptor
229 : * is assumed to be already closed.
230 : *
231 : * @param fd File descriptor to close. Can't be NULL.
232 : */
233 : void closep(int *fd);
234 :
235 : /**
236 : * Duplicate a memory region.
237 : *
238 : * Allocate a new buffer of size @p len and copy @p src into it. Requirements
239 : * applicable to @p src and @p len:
240 : * - If @p src is NULL, @p len must be 0. In this case, NULL is returned.
241 : * - If @p src is non-NULL, a new buffer of @p len bytes will be allocated to
242 : * store the first @p len bytes of @p src. This new buffer is then returned.
243 : *
244 : * Unless NULL is returned, the new buffer is owned by the caller.
245 : *
246 : * @param src Source buffer to copy to @p dst.
247 : * @param len Number of bytes to copy to @p dst.
248 : * @return Pointer to the new buffer, or NULL on failure.
249 : */
250 0 : static inline void *bf_memdup(const void *src, size_t len)
251 : {
252 : void *dst;
253 :
254 0 : if (!src)
255 : return NULL;
256 :
257 0 : dst = malloc(len);
258 0 : if (!dst)
259 : return NULL;
260 :
261 0 : return memcpy(dst, src, len);
262 : }
263 :
264 : /**
265 : * Copy @p len bytes from @p src to @p dst.
266 : *
267 : * Allow for @p src to be NULL and/or @p len to be zero:
268 : * - If @p src is NULL, @p len must be equal 0. @p dst is not modified.
269 : * - If @p src is not NULL, @p len can be equal to 0, in which case @p dst is
270 : * not modified.
271 : *
272 : * @param dst Destination buffer. Can't be NULL, and must be big enough to store
273 : * @p len bytes from @p src.
274 : * @param src Source buffer to copy to @p dst.
275 : * @param len Number of bytes to copy to @p dst.
276 : * @return Pointer to @p dst.
277 : */
278 284 : static inline void *bf_memcpy(void *dst, const void *src, size_t len)
279 : {
280 284 : bf_assert(dst);
281 284 : bf_assert(src ? 1 : len == 0);
282 :
283 284 : if (!src || !len)
284 : return dst;
285 :
286 216 : return memcpy(dst, src, len);
287 : }
288 :
289 : /**
290 : * Reallocate @p ptr into a new buffer of size @p size.
291 : *
292 : * Behaves similarly to realloc(), except that @p ptr is left unchanged if
293 : * allocation fails, and an error is returned.
294 : *
295 : * @param ptr Memory buffer to grow. Can't be NULL.
296 : * @param size New size of the memory buffer.
297 : * @return 0 on success, or a negative errno value on failure.
298 : */
299 : int bf_realloc(void **ptr, size_t size);
300 :
301 : /**
302 : * @brief Check if strings are equal.
303 : *
304 : * If any of `lhs`, `rhs` is NULL, the strings are considered inequal.
305 : *
306 : * @param lhs First string.
307 : * @param rhs Second string.
308 : * @return True if both strings are equal.
309 : */
310 632 : static inline bool bf_streq(const char *lhs, const char *rhs)
311 : {
312 632 : if (!lhs || !rhs)
313 : return false;
314 :
315 632 : return strcmp(lhs, rhs) == 0;
316 : }
317 :
318 : /**
319 : * @brief Case insensitive alternative to `bf_streq`.
320 : */
321 0 : static inline bool bf_streq_i(const char *lhs, const char *rhs)
322 : {
323 0 : if (!lhs || !rhs)
324 : return false;
325 :
326 0 : return strcasecmp(lhs, rhs) == 0;
327 : }
328 :
329 : /**
330 : * Copy a string to a buffer.
331 : *
332 : * @p src is copied to @p dst . If @p src is too long, at most @p len bytes are
333 : * copied (including the termination character).
334 : *
335 : * @param dst Destination buffer. Can't be NULL.
336 : * @param len Length of the destination buffer. The function will not copy more
337 : * than @p len bytes to @p dst , including @c \0 . Can't be 0.
338 : * @param src Soucre buffer to copy from. Will only be copied up to the
339 : * termination character if it fits. Can't be NULL.
340 : * @return 0 on success, or @c -E2BIG if @p src can't fit in @p dst .
341 : */
342 : int bf_strncpy(char *dst, size_t len, const char *src);
343 :
344 : /**
345 : * Read the contents of a file into a buffer.
346 : *
347 : * @param path Path to the file to read. Can't be NULL.
348 : * @param buf Pointer to a pointer to a buffer. The buffer will be allocated
349 : * automatically. The caller is responsible to free it. If @ref bf_read_file
350 : * fails, @p buf is left unchanged.
351 : * @param len Length of the allocated buffer. Populated by the function.
352 : * @return 0 on success, negative errno value on error.
353 : */
354 : int bf_read_file(const char *path, void **buf, size_t *len);
355 :
356 : /**
357 : * Write the contents of a buffer into a file.
358 : *
359 : * @param path Path to the file to write. Can't be NULL.
360 : * @param buf Buffer to write.
361 : * @param len Number of bytes to write the to file.
362 : * @return 0 on success, negative errno value on error.
363 : */
364 : int bf_write_file(const char *path, const void *buf, size_t len);
|