Monado OpenXR Runtime
xrt_compiler.h
Go to the documentation of this file.
1// Copyright 2019, Collabora, Ltd.
2// Copyright 2025-2026, NVIDIA CORPORATION.
3// SPDX-License-Identifier: BSL-1.0
4/*!
5 * @file
6 * @brief Header holding common defines.
7 * @author Jakob Bornecrantz <jakob@collabora.com>
8 * @ingroup xrt_iface
9 */
10
11#pragma once
12
13
14/*
15 * C99 is not a high bar to reach.
16 */
17#include <stddef.h>
18#include <stdint.h>
19#include <stdbool.h>
20#include <inttypes.h>
21
22#ifdef _MSC_VER
23#include <intrin.h>
24// for atomic intrinsics
25#include "xrt_windows.h"
26// Older MSVC versions do not have stdalign.h, define it manually using __declspec(align).
27#define XRT_ALIGNAS(n) __declspec(align(n))
28#elif defined(XRT_DOXYGEN)
29#define XRT_ALIGNAS(align)
30#else // _MSC_VER
31#include <stdalign.h>
32#define XRT_ALIGNAS(align) alignas(align)
33#endif
34
35#if (defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN) || \
36 (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) || \
37 (defined(__STDC_ENDIAN_NATIVE__) && __STDC_ENDIAN_NATIVE__ == __STDC_ENDIAN_BIG__) || defined(__BIG_ENDIAN__) || \
38 defined(__ARMEB__) || defined(__THUMBEB__) || defined(__AARCH64EB__) || defined(_MIBSEB) || defined(__MIBSEB) || \
39 defined(__MIBSEB__)
40
41#define XRT_BIG_ENDIAN
42
43#elif (defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN) || \
44 (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || \
45 (defined(__STDC_ENDIAN_NATIVE__) && __STDC_ENDIAN_NATIVE__ == __STDC_ENDIAN_LITTLE__) || \
46 defined(__LITTLE_ENDIAN__) || defined(__ARMEL__) || defined(__THUMBEL__) || defined(__AARCH64EL__) || \
47 defined(_MIPSEL) || defined(__MIPSEL) || defined(__MIPSEL__) || defined(__x86_64__) || defined(_M_X64) || \
48 defined(i386) || defined(__i386__) || defined(__i386) || defined(_M_IX86)
49
50#undef XRT_BIG_ENDIAN
51
52#else
53#error "@todo: Unable to determine current architecture."
54#endif
55
56/*!
57 * Array size helper.
58 */
59#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
60
61#if defined(__x86_64__) || defined(_M_X64) || defined(__aarch64__) || defined(_ARCH_PPC64) || defined(__s390x__) || \
62 (defined(__SIZEOF_POINTER__) && __SIZEOF_POINTER__ == 8)
63#define XRT_64_BIT
64#else
65#define XRT_32_BIT
66#endif
67
68/*
69 * Printf helper attribute.
70 */
71#if defined(__GNUC__)
72#define XRT_PRINTF_FORMAT(fmt, list) __attribute__((format(printf, fmt, list)))
73#else
74#define XRT_PRINTF_FORMAT(fmt, list)
75#endif
76
77
78/*
79 * To silence unused warnings.
80 */
81#if defined(__GNUC__)
82#define XRT_MAYBE_UNUSED __attribute__((unused))
83#elif defined(_MSC_VER) && defined(__cplusplus)
84#define XRT_MAYBE_UNUSED [[maybe_unused]]
85#else
86#define XRT_MAYBE_UNUSED
87#endif
88
89
90/*
91 * To indicate that all pointer parameters must not be null.
92 *
93 * Note: MSVC does not have a function-level equivalent. To get similar checking
94 * on MSVC, individual parameters would need SAL annotations like _In_, _Out_, etc.
95 */
96#if defined(__GNUC__)
97#define XRT_NONNULL_ALL __attribute__((nonnull))
98#else
99#define XRT_NONNULL_ALL
100#endif
101
102
103/*
104 * To indicate that the first parameter must not be null.
105 *
106 * Note: MSVC does not have a function-level equivalent. To get similar checking
107 * on MSVC, individual parameters would need SAL annotations like _In_, _Out_, etc.
108 */
109#if defined(__GNUC__)
110#define XRT_NONNULL_FIRST __attribute__((nonnull(1)))
111#else
112#define XRT_NONNULL_FIRST
113#endif
114
115
116/*
117 * To make sure return values are checked.
118 */
119#if defined(__GNUC__) && (__GNUC__ >= 4)
120#define XRT_CHECK_RESULT __attribute__((warn_unused_result))
121#elif defined(_MSC_VER) && (_MSC_VER >= 1700)
122#define XRT_CHECK_RESULT _Check_return_
123#else
124#define XRT_CHECK_RESULT
125#endif
126
127
128/*
129 * To stop inlining.
130 */
131#if defined(__GNUC__)
132#define XRT_NO_INLINE __attribute__((noinline))
133#elif defined(_MSC_VER)
134#define XRT_NO_INLINE __declspec(noinline)
135#else
136#define XRT_NO_INLINE
137#endif
138
139
140#ifdef XRT_DOXYGEN
141/*!
142 * To trigger a trap/break in the debugger.
143 *
144 * @ingroup xrt_iface
145 */
146#define XRT_DEBUGBREAK()
147#elif defined(__clang__) || defined(__GNUC__)
148#define XRT_DEBUGBREAK() __builtin_trap()
149#elif defined(_MSC_VER)
150#define XRT_DEBUGBREAK() __debugbreak()
151#else
152#error "compiler not supported"
153#endif
154
155
156
157typedef volatile int32_t xrt_atomic_s32_t;
158
159static inline int32_t
160xrt_atomic_s32_inc_return(xrt_atomic_s32_t *p)
161{
162#if defined(__GNUC__)
163 return __sync_add_and_fetch(p, 1);
164#elif defined(_MSC_VER)
165 return InterlockedIncrement((volatile LONG *)p);
166#else
167#error "compiler not supported"
168#endif
169}
170static inline int32_t
171xrt_atomic_s32_dec_return(xrt_atomic_s32_t *p)
172{
173#if defined(__GNUC__)
174 return __sync_sub_and_fetch(p, 1);
175#elif defined(_MSC_VER)
176 return InterlockedDecrement((volatile LONG *)p);
177#else
178#error "compiler not supported"
179#endif
180}
181static inline int32_t
182xrt_atomic_s32_cmpxchg(xrt_atomic_s32_t *p, int32_t old_, int32_t new_)
183{
184#if defined(__GNUC__)
185 return __sync_val_compare_and_swap(p, old_, new_);
186#elif defined(_MSC_VER)
187 return InterlockedCompareExchange((volatile LONG *)p, old_, new_);
188#else
189#error "compiler not supported"
190#endif
191}
192static inline void
193xrt_atomic_s32_store(xrt_atomic_s32_t *p, int32_t v)
194{
195#if defined(__GNUC__)
196 __atomic_store_n(p, v, __ATOMIC_SEQ_CST);
197#elif defined(_MSC_VER)
198 InterlockedExchange((volatile LONG *)p, v);
199#else
200#error "compiler not supported"
201#endif
202}
203static inline int32_t
204xrt_atomic_s32_load(xrt_atomic_s32_t *p)
205{
206#if defined(__GNUC__)
207 return __atomic_load_n(p, __ATOMIC_SEQ_CST);
208#elif defined(_MSC_VER)
209 return InterlockedCompareExchange((volatile LONG *)p, 0, 0);
210#else
211#error "compiler not supported"
212#endif
213}
214
215#ifdef _MSC_VER
216typedef intptr_t ssize_t;
217#define _SSIZE_T_
218#define _SSIZE_T_DEFINED
219#endif
220
221/*!
222 * Get the holder from a pointer to a field.
223 *
224 * @ingroup xrt_iface
225 */
226#define container_of(ptr, type, field) (type *)((char *)ptr - offsetof(type, field))
227
228
229#ifdef XRT_DOXYGEN
230
231/*!
232 * Very small default init for structs that works in both C and C++. Helps with
233 * code that needs to be compiled with both C and C++.
234 *
235 * @ingroup xrt_iface
236 */
237
238// clang-format off
239#define XRT_STRUCT_INIT {}
240// clang-format on
241
242#elif defined(__cplusplus)
243
244// clang-format off
245#define XRT_STRUCT_INIT {}
246// clang-format on
247
248#else
249
250// clang-format off
251#define XRT_STRUCT_INIT {0}
252// clang-format on
253
254#endif
255
256/*
257 * C11 style designated initializers (as compound literals) causes warning messages
258 * in C++ (even in C++20), usage:
259 *
260 * @code{.c}
261 * static inline struct xrt_foo_bar
262 * xrt_foo_bar_make()
263 * {
264 * return XRT_C11_COMPOUND(struct xrt_foo_bar){
265 * .x = y,
266 * // ...
267 * };
268 * }
269 * @endcode
270 */
271#ifdef __cplusplus
272#define XRT_C11_COMPOUND(X)
273#else
274#define XRT_C11_COMPOUND(X) (X)
275#endif
A minimal way to include Windows.h.