master
h 382 lines 13.3 KB
Raw
1 /* compiler.h: macros to abstract away compiler specifics
2 *
3 * This work is licensed under the terms of the GNU GPL, version 2 or later.
4 * See the COPYING file in the top-level directory.
5 */
6
7 #ifndef COMPILER_H
8 #define COMPILER_H
9
10 #define HOST_BIG_ENDIAN (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
11
12 /* HOST_LONG_BITS is the size of a native pointer in bits. */
13 #define HOST_LONG_BITS (__SIZEOF_POINTER__ * 8)
14
15 #if defined __clang_analyzer__ || defined __COVERITY__
16 #define QEMU_STATIC_ANALYSIS 1
17 #endif
18
19 #ifdef __cplusplus
20 #define QEMU_EXTERN_C extern "C"
21 #else
22 #define QEMU_EXTERN_C extern
23 #endif
24
25 #define QEMU_PACKED __attribute__((packed))
26 #define QEMU_ALIGNED(X) __attribute__((aligned(X)))
27
28 #ifndef glue
29 #define xglue(x, y) x ## y
30 #define glue(x, y) xglue(x, y)
31 #define stringify(s) tostring(s)
32 #define tostring(s) #s
33 #endif
34
35 /* Expands into an identifier stemN, where N is another number each time */
36 #define MAKE_IDENTIFIER(stem) glue(stem, __COUNTER__)
37
38 #ifndef likely
39 #define likely(x) __builtin_expect(!!(x), 1)
40 #define unlikely(x) __builtin_expect(!!(x), 0)
41 #endif
42
43 #ifndef container_of
44 #define container_of(ptr, type, member) ({ \
45 const typeof(((type *) 0)->member) *__mptr = (ptr); \
46 (type *) ((char *) __mptr - offsetof(type, member));})
47 #endif
48
49 #define sizeof_field(type, field) sizeof(((type *)0)->field)
50
51 /*
52 * Calculate the number of bytes up to and including the given 'field' of
53 * 'container'.
54 */
55 #define endof(container, field) \
56 (offsetof(container, field) + sizeof_field(container, field))
57
58 /* Convert from a base type to a parent type, with compile time checking. */
59 #define DO_UPCAST(type, field, dev) ( __extension__ ( { \
60 char __attribute__((unused)) offset_must_be_zero[ \
61 -offsetof(type, field)]; \
62 container_of(dev, type, field);}))
63
64 #define typeof_field(type, field) typeof(((type *)0)->field)
65 #define type_check(t1,t2) ((t1*)0 - (t2*)0)
66
67 #define QEMU_BUILD_BUG_ON_STRUCT(x) \
68 struct { \
69 int:(x) ? -1 : 1; \
70 }
71
72 #define QEMU_BUILD_BUG_MSG(x, msg) _Static_assert(!(x), msg)
73
74 #define QEMU_BUILD_BUG_ON(x) QEMU_BUILD_BUG_MSG(x, "not expecting: " #x)
75
76 #define QEMU_BUILD_BUG_ON_ZERO(x) (sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)) - \
77 sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)))
78
79 #if !defined(__clang__) && defined(_WIN32)
80 /*
81 * Map __printf__ to __gnu_printf__ because we want standard format strings even
82 * when MinGW or GLib include files use __printf__.
83 */
84 # define __printf__ __gnu_printf__
85 #endif
86
87 #ifndef __has_warning
88 #define __has_warning(x) 0 /* compatibility with non-clang compilers */
89 #endif
90
91 #ifndef __has_feature
92 #define __has_feature(x) 0 /* compatibility with non-clang compilers */
93 #endif
94
95 #ifndef __has_builtin
96 #define __has_builtin(x) 0 /* compatibility with non-clang compilers */
97 #endif
98
99 #if __has_builtin(__builtin_assume_aligned) || !defined(__clang__)
100 #define HAS_ASSUME_ALIGNED
101 #endif
102
103 #ifndef __has_attribute
104 #define __has_attribute(x) 0 /* compatibility with older GCC */
105 #endif
106
107 #if defined(__SANITIZE_ADDRESS__) || __has_feature(address_sanitizer)
108 # define QEMU_SANITIZE_ADDRESS 1
109 #endif
110
111 #if defined(__SANITIZE_THREAD__) || __has_feature(thread_sanitizer)
112 # define QEMU_SANITIZE_THREAD 1
113 #endif
114
115 /*
116 * GCC doesn't provide __has_attribute() until GCC 5, but we know all the GCC
117 * versions we support have the "flatten" attribute. Clang may not have the
118 * "flatten" attribute but always has __has_attribute() to check for it.
119 */
120 #if __has_attribute(flatten) || !defined(__clang__)
121 # define QEMU_FLATTEN __attribute__((flatten))
122 #else
123 # define QEMU_FLATTEN
124 #endif
125
126 /*
127 * If __attribute__((error)) is present, use it to produce an error at
128 * compile time. Otherwise, one must wait for the linker to diagnose
129 * the missing symbol.
130 */
131 #if __has_attribute(error)
132 # define QEMU_ERROR(X) __attribute__((error(X)))
133 #else
134 # define QEMU_ERROR(X)
135 #endif
136
137 /*
138 * The nonstring variable attribute specifies that an object or member
139 * declaration with type array of char or pointer to char is intended
140 * to store character arrays that do not necessarily contain a terminating
141 * NUL character. This is useful in detecting uses of such arrays or pointers
142 * with functions that expect NUL-terminated strings, and to avoid warnings
143 * when such an array or pointer is used as an argument to a bounded string
144 * manipulation function such as strncpy.
145 */
146 #if __has_attribute(nonstring)
147 # define QEMU_NONSTRING __attribute__((nonstring))
148 #else
149 # define QEMU_NONSTRING
150 #endif
151
152 /*
153 * Forced inlining may be desired to encourage constant propagation
154 * of function parameters. However, it can also make debugging harder,
155 * so disable it for a non-optimizing build.
156 */
157 #if defined(__OPTIMIZE__)
158 #define QEMU_ALWAYS_INLINE __attribute__((always_inline))
159 #else
160 #define QEMU_ALWAYS_INLINE
161 #endif
162
163 /**
164 * In most cases, normal "fallthrough" comments are good enough for
165 * switch-case statements, but sometimes the compiler has problems
166 * with those. In that case you can use QEMU_FALLTHROUGH instead.
167 */
168 #if __has_attribute(fallthrough)
169 # define QEMU_FALLTHROUGH __attribute__((fallthrough))
170 #else
171 # define QEMU_FALLTHROUGH do {} while (0) /* fallthrough */
172 #endif
173
174 #ifdef CONFIG_CFI
175 /*
176 * If CFI is enabled, use an attribute to disable cfi-icall on the following
177 * function
178 */
179 #define QEMU_DISABLE_CFI __attribute__((no_sanitize("cfi-icall")))
180 #else
181 /* If CFI is not enabled, use an empty define to not change the behavior */
182 #define QEMU_DISABLE_CFI
183 #endif
184
185 #if __has_attribute(annotate)
186 #define QEMU_ANNOTATE(x) __attribute__((annotate(x)))
187 #else
188 #define QEMU_ANNOTATE(x)
189 #endif
190
191 #if __has_attribute(used)
192 # define QEMU_USED __attribute__((used))
193 #else
194 # define QEMU_USED
195 #endif
196
197 /*
198 * A priority for __attribute__((constructor(...))) that
199 * will run earlier than the default constructors. Must
200 * only be used for functions that have no dependency
201 * on global initialization of other QEMU subsystems.
202 */
203 #define QEMU_CONSTRUCTOR_EARLY 101
204
205 /*
206 * Disable -ftrivial-auto-var-init on a local variable.
207 *
208 * Use this in cases where there a method in the device I/O path (or other
209 * important hot paths), that has large variables on the stack. A rule of
210 * thumb is that "large" means a method with 4kb data in the local stack
211 * frame. Any variables which are KB in size, should be annotated with this
212 * attribute, to pre-emptively eliminate any potential overhead from the
213 * compiler's implicit zero'ing of memory.
214 *
215 * Given that this turns off a security hardening feature, when using this
216 * to flag variables, it is important that the code is double-checked to
217 * ensure there is no possible use of uninitialized data in the method.
218 */
219 #if __has_attribute(uninitialized)
220 # define QEMU_UNINITIALIZED __attribute__((uninitialized))
221 #else
222 # define QEMU_UNINITIALIZED
223 #endif
224
225 /*
226 * http://clang.llvm.org/docs/ThreadSafetyAnalysis.html
227 *
228 * TSA is available since clang 3.6-ish.
229 */
230 #ifdef __clang__
231 # define TSA(x) __attribute__((x))
232 #else
233 # define TSA(x) /* No TSA, make TSA attributes no-ops. */
234 #endif
235
236 /*
237 * TSA_CAPABILITY() is used to annotate typedefs:
238 *
239 * typedef pthread_mutex_t TSA_CAPABILITY("mutex") tsa_mutex;
240 */
241 #define TSA_CAPABILITY(x) TSA(capability(x))
242
243 /*
244 * TSA_GUARDED_BY() is used to annotate global variables,
245 * the data is guarded:
246 *
247 * Foo foo TSA_GUARDED_BY(mutex);
248 */
249 #define TSA_GUARDED_BY(x) TSA(guarded_by(x))
250
251 /*
252 * TSA_PT_GUARDED_BY() is used to annotate global pointers, the data
253 * behind the pointer is guarded.
254 *
255 * Foo* ptr TSA_PT_GUARDED_BY(mutex);
256 */
257 #define TSA_PT_GUARDED_BY(x) TSA(pt_guarded_by(x))
258
259 /*
260 * The TSA_REQUIRES() is used to annotate functions: the caller of the
261 * function MUST hold the resource, the function will NOT release it.
262 *
263 * More than one mutex may be specified, comma-separated.
264 *
265 * void Foo(void) TSA_REQUIRES(mutex);
266 */
267 #define TSA_REQUIRES(...) TSA(requires_capability(__VA_ARGS__))
268 #define TSA_REQUIRES_SHARED(...) TSA(requires_shared_capability(__VA_ARGS__))
269
270 /*
271 * TSA_EXCLUDES() is used to annotate functions: the caller of the
272 * function MUST NOT hold resource, the function first acquires the
273 * resource, and then releases it.
274 *
275 * More than one mutex may be specified, comma-separated.
276 *
277 * void Foo(void) TSA_EXCLUDES(mutex);
278 */
279 #define TSA_EXCLUDES(...) TSA(locks_excluded(__VA_ARGS__))
280
281 /*
282 * TSA_ACQUIRE() is used to annotate functions: the caller of the
283 * function MUST NOT hold the resource, the function will acquire the
284 * resource, but NOT release it.
285 *
286 * More than one mutex may be specified, comma-separated.
287 *
288 * void Foo(void) TSA_ACQUIRE(mutex);
289 */
290 #define TSA_ACQUIRE(...) TSA(acquire_capability(__VA_ARGS__))
291 #define TSA_ACQUIRE_SHARED(...) TSA(acquire_shared_capability(__VA_ARGS__))
292
293 /*
294 * TSA_RELEASE() is used to annotate functions: the caller of the
295 * function MUST hold the resource, but the function will then release it.
296 *
297 * More than one mutex may be specified, comma-separated.
298 *
299 * void Foo(void) TSA_RELEASE(mutex);
300 */
301 #define TSA_RELEASE(...) TSA(release_capability(__VA_ARGS__))
302 #define TSA_RELEASE_SHARED(...) TSA(release_shared_capability(__VA_ARGS__))
303
304 /*
305 * TSA_NO_TSA is used to annotate functions. Use only when you need to.
306 *
307 * void Foo(void) TSA_NO_TSA;
308 */
309 #define TSA_NO_TSA TSA(no_thread_safety_analysis)
310
311 /*
312 * TSA_ASSERT() is used to annotate functions: This function will assert that
313 * the lock is held. When it returns, the caller of the function is assumed to
314 * already hold the resource.
315 *
316 * More than one mutex may be specified, comma-separated.
317 */
318 #define TSA_ASSERT(...) TSA(assert_capability(__VA_ARGS__))
319 #define TSA_ASSERT_SHARED(...) TSA(assert_shared_capability(__VA_ARGS__))
320
321 /*
322 * Ugly CPP trick that is like "defined FOO", but also works in C
323 * code. Useful to replace #ifdef with "if" statements; assumes
324 * the symbol was defined with Meson's "config.set()", so it is empty
325 * if defined.
326 */
327 #define IS_ENABLED(x) IS_EMPTY(x)
328
329 #define IS_EMPTY_JUNK_ junk,
330 #define IS_EMPTY(value) IS_EMPTY_(IS_EMPTY_JUNK_##value)
331
332 /* Expands to either SECOND_ARG(junk, 1, 0) or SECOND_ARG(IS_EMPTY_JUNK_CONFIG_FOO 1, 0) */
333 #define SECOND_ARG(first, second, ...) second
334 #define IS_EMPTY_(junk_maybecomma) SECOND_ARG(junk_maybecomma 1, 0)
335
336 #ifndef __cplusplus
337 /*
338 * Useful in macros that need to declare temporary variables. For example,
339 * the variable that receives the old value of an atomically-accessed
340 * variable must be non-qualified, because atomic builtins return values
341 * through a pointer-type argument as in __atomic_load(&var, &old, MODEL).
342 *
343 * This macro has to handle types smaller than int manually, because of
344 * implicit promotion. int and larger types, as well as pointers, can be
345 * converted to a non-qualified type just by applying a binary operator.
346 */
347 #define typeof_strip_qual(expr) \
348 typeof( \
349 __builtin_choose_expr( \
350 __builtin_types_compatible_p(typeof(expr), bool) || \
351 __builtin_types_compatible_p(typeof(expr), const bool) || \
352 __builtin_types_compatible_p(typeof(expr), volatile bool) || \
353 __builtin_types_compatible_p(typeof(expr), const volatile bool), \
354 (bool)1, \
355 __builtin_choose_expr( \
356 __builtin_types_compatible_p(typeof(expr), signed char) || \
357 __builtin_types_compatible_p(typeof(expr), const signed char) || \
358 __builtin_types_compatible_p(typeof(expr), volatile signed char) || \
359 __builtin_types_compatible_p(typeof(expr), const volatile signed char), \
360 (signed char)1, \
361 __builtin_choose_expr( \
362 __builtin_types_compatible_p(typeof(expr), unsigned char) || \
363 __builtin_types_compatible_p(typeof(expr), const unsigned char) || \
364 __builtin_types_compatible_p(typeof(expr), volatile unsigned char) || \
365 __builtin_types_compatible_p(typeof(expr), const volatile unsigned char), \
366 (unsigned char)1, \
367 __builtin_choose_expr( \
368 __builtin_types_compatible_p(typeof(expr), signed short) || \
369 __builtin_types_compatible_p(typeof(expr), const signed short) || \
370 __builtin_types_compatible_p(typeof(expr), volatile signed short) || \
371 __builtin_types_compatible_p(typeof(expr), const volatile signed short), \
372 (signed short)1, \
373 __builtin_choose_expr( \
374 __builtin_types_compatible_p(typeof(expr), unsigned short) || \
375 __builtin_types_compatible_p(typeof(expr), const unsigned short) || \
376 __builtin_types_compatible_p(typeof(expr), volatile unsigned short) || \
377 __builtin_types_compatible_p(typeof(expr), const volatile unsigned short), \
378 (unsigned short)1, \
379 (expr)+0))))))
380 #endif
381
382 #endif /* COMPILER_H */