| 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 | #ifdef __cplusplus |
| 11 | extern "C" { |
| 12 | #endif |
| 13 | |
| 14 | #ifdef __cplusplus |
| 15 | #define BT_EXTERN_C extern "C" |
| 16 | #else |
| 17 | #define BT_EXTERN_C |
| 18 | #endif |
| 19 | |
| 20 | #define bt_max_t(type, a, b) \ |
| 21 | ((type) (a) > (type) (b) ? (type) (a) : (type) (b)) |
| 22 | |
| 23 | /* |
| 24 | * BT_EXPORT: set the visibility for exported functions. |
| 25 | */ |
| 26 | #if defined(_WIN32) || defined(__CYGWIN__) |
| 27 | #define BT_EXPORT |
| 28 | #else |
| 29 | #define BT_EXPORT __attribute__((visibility("default"))) |
| 30 | #endif |
| 31 | |
| 32 | /* |
| 33 | * Yield `ref`'s value while setting `ref` to NULL. |
| 34 | * |
| 35 | * This can be used to give a strong reference to a callee: |
| 36 | * |
| 37 | * add_foo_to_list(list, BT_MOVE_REF(foo)); |
| 38 | * |
| 39 | * or in a simple assignment: |
| 40 | * |
| 41 | * my_struct->foo = BT_MOVE_REF(foo); |
| 42 | * |
| 43 | * When moving a reference in a function call, the reference is given to the |
| 44 | * callee even if that function call fails, so make sure the called function |
| 45 | * is written accordingly. |
| 46 | */ |
| 47 | |
| 48 | #define BT_MOVE_REF(ref) \ |
| 49 | ({ \ |
| 50 | __typeof__(ref) _ref = ref; \ |
| 51 | ref = NULL; \ |
| 52 | _ref; \ |
| 53 | }) |
| 54 | |
| 55 | /* Wrapper for g_array_index that adds bound checking. */ |
| 56 | #define bt_g_array_index(a, t, i) \ |
| 57 | g_array_index((a), t, ({ BT_ASSERT_DBG((i) < (a)->len); (i); })) |
| 58 | |
| 59 | /* |
| 60 | * Copied from: |
| 61 | * <https://stackoverflow.com/questions/37411809/how-to-elegantly-fix-this-unused-variable-warning/37412551#37412551>: |
| 62 | * |
| 63 | * * sizeof() ensures that the expression is not evaluated at all, so |
| 64 | * its side-effects don't happen. That is to be consistent with the |
| 65 | * usual behaviour of debug-only constructs, such as assert(). |
| 66 | * |
| 67 | * * `((_expr), 0)` uses the comma operator to swallow the actual type |
| 68 | * of `(_expr)`. This is to prevent VLAs from triggering evaluation. |
| 69 | * |
| 70 | * * `(void)` explicitly ignores the result of `(_expr)` and sizeof() so |
| 71 | * no "unused value" warning appears. |
| 72 | */ |
| 73 | |
| 74 | #define BT_USE_EXPR(_expr) ((void) sizeof((void) (_expr), 0)) |
| 75 | #define BT_USE_EXPR2(_expr1, _expr2) \ |
| 76 | ((void) sizeof((void) (_expr1), (void) (_expr2), 0)) |
| 77 | #define BT_USE_EXPR3(_expr1, _expr2, _expr3) \ |
| 78 | ((void) sizeof((void) (_expr1), (void) (_expr2), (void) (_expr3), 0)) |
| 79 | #define BT_USE_EXPR4(_expr1, _expr2, _expr3, _expr4) \ |
| 80 | ((void) sizeof((void) (_expr1), (void) (_expr2), \ |
| 81 | (void) (_expr3), (void) (_expr4), 0)) |
| 82 | #define BT_USE_EXPR5(_expr1, _expr2, _expr3, _expr4, _expr5) \ |
| 83 | ((void) sizeof((void) (_expr1), (void) (_expr2), \ |
| 84 | (void) (_expr3), (void) (_expr4), (void) (_expr5), 0)) |
| 85 | |
| 86 | #if defined __clang__ |
| 87 | # if __has_warning("-Wunused-but-set-variable") |
| 88 | # define BT_DIAG_IGNORE_UNUSED_BUT_SET_VARIABLE \ |
| 89 | _Pragma("GCC diagnostic ignored \"-Wunused-but-set-variable\"") |
| 90 | # endif |
| 91 | #endif |
| 92 | |
| 93 | #if !defined BT_DIAG_IGNORE_UNUSED_BUT_SET_VARIABLE |
| 94 | # define BT_DIAG_IGNORE_UNUSED_BUT_SET_VARIABLE |
| 95 | #endif |
| 96 | |
| 97 | #ifdef __cplusplus |
| 98 | } |
| 99 | #endif |
| 100 | |
| 101 | #endif |