718a3050ccb1b4b96b9a00cf84adeb35185bdae0
[babeltrace.git] / src / common / macros.h
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 */
6
7 #ifndef _BABELTRACE_INTERNAL_H
8 #define _BABELTRACE_INTERNAL_H
9
10 #define bt_max_t(type, a, b) \
11 ((type) (a) > (type) (b) ? (type) (a) : (type) (b))
12
13 /*
14 * BT_HIDDEN: set the hidden attribute for internal functions
15 * On Windows, symbols are local unless explicitly exported,
16 * see https://gcc.gnu.org/wiki/Visibility
17 */
18 #if defined(_WIN32) || defined(__CYGWIN__)
19 #define BT_HIDDEN
20 #else
21 #define BT_HIDDEN __attribute__((visibility("hidden")))
22 #endif
23
24 /*
25 * Yield `ref`'s value while setting `ref` to NULL.
26 *
27 * This can be used to give a strong reference to a callee:
28 *
29 * add_foo_to_list(list, BT_MOVE_REF(foo));
30 *
31 * or in a simple assignment:
32 *
33 * my_struct->foo = BT_MOVE_REF(foo);
34 *
35 * When moving a reference in a function call, the reference is given to the
36 * callee even if that function call fails, so make sure the called function
37 * is written accordingly.
38 */
39
40 #define BT_MOVE_REF(ref) \
41 ({ \
42 typeof(ref) _ref = ref; \
43 ref = NULL; \
44 _ref; \
45 })
46
47 /*
48 * Copied from:
49 * <https://stackoverflow.com/questions/37411809/how-to-elegantly-fix-this-unused-variable-warning/37412551#37412551>:
50 *
51 * * sizeof() ensures that the expression is not evaluated at all, so
52 * its side-effects don't happen. That is to be consistent with the
53 * usual behaviour of debug-only constructs, such as assert().
54 *
55 * * `((_expr), 0)` uses the comma operator to swallow the actual type
56 * of `(_expr)`. This is to prevent VLAs from triggering evaluation.
57 *
58 * * `(void)` explicitly ignores the result of `(_expr)` and sizeof() so
59 * no "unused value" warning appears.
60 */
61
62 #define BT_USE_EXPR(_expr) ((void) sizeof((void) (_expr), 0))
63 #define BT_USE_EXPR2(_expr1, _expr2) \
64 ((void) sizeof((void) (_expr1), (void) (_expr2), 0))
65 #define BT_USE_EXPR3(_expr1, _expr2, _expr3) \
66 ((void) sizeof((void) (_expr1), (void) (_expr2), (void) (_expr3), 0))
67 #define BT_USE_EXPR4(_expr1, _expr2, _expr3, _expr4) \
68 ((void) sizeof((void) (_expr1), (void) (_expr2), \
69 (void) (_expr3), (void) (_expr4), 0))
70 #define BT_USE_EXPR5(_expr1, _expr2, _expr3, _expr4, _expr5) \
71 ((void) sizeof((void) (_expr1), (void) (_expr2), \
72 (void) (_expr3), (void) (_expr4), (void) (_expr5), 0))
73
74 #endif
This page took 0.045048 seconds and 3 git commands to generate.