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  defined(_ARCH_PPC64) || defined(__s390x__) || \
28  (defined(__SIZEOF_POINTER__) && __SIZEOF_POINTER__ == 8)
29 #define XRT_64_BIT
30 #else
31 #define XRT_32_BIT
32 #endif
33 
34 /*
35  * Printf helper attribute.
36  */
37 #if defined(__GNUC__)
38 #define XRT_PRINTF_FORMAT(fmt, list) __attribute__((format(printf, fmt, list)))
39 #else
40 #define XRT_PRINTF_FORMAT(fmt, list)
41 #endif
42 
43 
44 /*
45  * To silence unused warnings.
46  */
47 #if defined(__GNUC__)
48 #define XRT_MAYBE_UNUSED __attribute__((unused))
49 #else
50 #define XRT_MAYBE_UNUSED
51 #endif
52 
53 
54 /*
55  * To stop inlining.
56  */
57 #if defined(__GNUC__)
58 #define XRT_NO_INLINE __attribute__((noinline))
59 #else
60 #define XRT_NO_INLINE
61 #endif
62 
63 
64 #ifdef XRT_DOXYGEN
65 /*!
66  * To trigger a trap/break in the debugger.
67  *
68  * @ingroup xrt_iface
69  */
70 #define XRT_DEBUGBREAK()
71 #elif defined(__clang__) || defined(__GNUC__)
72 #define XRT_DEBUGBREAK() __builtin_trap()
73 #elif defined(_MSC_VER)
74 #include <intrin.h>
75 #define XRT_DEBUGBREAK() __debugbreak()
76 #else
77 #error "compiler not supported"
78 #endif
79 
80 
81 
82 #if defined(__GNUC__)
83 #define xrt_atomic_inc_return(v) __sync_add_and_fetch((v), 1)
84 #define xrt_atomic_dec_return(v) __sync_sub_and_fetch((v), 1)
85 #define xrt_atomic_cmpxchg(v, old, _new) \
86  __sync_val_compare_and_swap((v), (old), (_new))
87 #else
88 #error "compiler not supported"
89 #endif
90 
91 /*!
92  * Get the holder from a pointer to a field.
93  *
94  * @ingroup xrt_iface
95  */
96 #define container_of(ptr, type, field) \
97  (type *)((char *)ptr - offsetof(type, field))