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