Monado OpenXR Runtime
xrt_compiler.h
Go to the documentation of this file.
1 // Copyright 2019, Collabora, Ltd.
2 // SPDX-License-Identifier: BSL-1.0
3 /*!
4  * @file
5  * @brief Header holding common defines.
6  * @author Jakob Bornecrantz <jakob@collabora.com>
7  * @ingroup xrt_iface
8  */
9 
10 #pragma once
11 
12 
13 /*
14  * C99 is not a high bar to reach.
15  */
16 #include <stddef.h>
17 #include <stdint.h>
18 #include <stdbool.h>
19 
20 
21 /*!
22  * Array size helper.
23  */
24 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
25 
26 #if defined(__x86_64__) || defined(_M_X64) || defined(__aarch64__)
27 #define XRT_64_BIT
28 #else
29 #define XRT_32_BIT
30 #endif
31 
32 /*
33  * Printf helper attribute.
34  */
35 #if defined(__GNUC__)
36 #define XRT_PRINTF_FORMAT(fmt, list) __attribute__((format(printf, fmt, list)))
37 #else
38 #define XRT_PRINTF_FORMAT(fmt, list)
39 #endif
40 
41 
42 /*
43  * To silence unused warnings.
44  */
45 #if defined(__GNUC__)
46 #define XRT_MAYBE_UNUSED __attribute__((unused))
47 #else
48 #define XRT_MAYBE_UNUSED
49 #endif
50 
51 
52 /*
53  * To stop inlining.
54  */
55 #if defined(__GNUC__)
56 #define XRT_NO_INLINE __attribute__((noinline))
57 #else
58 #define XRT_NO_INLINE
59 #endif
60 
61 
62 #ifdef XRT_DOXYGEN
63 /*!
64  * To trigger a trap/break in the debugger.
65  *
66  * @ingroup xrt_iface
67  */
68 #define XRT_DEBUGBREAK()
69 #elif defined(__clang__) || defined(__GNUC__)
70 #define XRT_DEBUGBREAK() __builtin_trap()
71 #elif defined(_MSC_VER)
72 #include <intrin.h>
73 #define XRT_DEBUGBREAK() __debugbreak()
74 #else
75 #error "compiler not supported"
76 #endif
77 
78 
79 
80 #if defined(__GNUC__)
81 #define xrt_atomic_inc_return(v) __sync_add_and_fetch((v), 1)
82 #define xrt_atomic_dec_return(v) __sync_sub_and_fetch((v), 1)
83 #define xrt_atomic_cmpxchg(v, old, _new) \
84  __sync_val_compare_and_swap((v), (old), (_new))
85 #else
86 #error "compiler not supported"
87 #endif
88 
89 /*!
90  * Get the holder from a pointer to a field.
91  *
92  * @ingroup xrt_iface
93  */
94 #define container_of(ptr, type, field) \
95  (type *)((char *)ptr - offsetof(type, field))