1
2
3
4
5
6
7#ifndef COMPILER_H
8#define COMPILER_H
9
10#define HOST_BIG_ENDIAN (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
11
12
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#if defined(_WIN32) && (defined(__x86_64__) || defined(__i386__))
26# define QEMU_PACKED __attribute__((gcc_struct, packed))
27#else
28# define QEMU_PACKED __attribute__((packed))
29#endif
30
31#define QEMU_ALIGNED(X) __attribute__((aligned(X)))
32
33#ifndef glue
34#define xglue(x, y) x ## y
35#define glue(x, y) xglue(x, y)
36#define stringify(s) tostring(s)
37#define tostring(s) #s
38#endif
39
40#ifndef likely
41#define likely(x) __builtin_expect(!!(x), 1)
42#define unlikely(x) __builtin_expect(!!(x), 0)
43#endif
44
45#ifndef container_of
46#define container_of(ptr, type, member) ({ \
47 const typeof(((type *) 0)->member) *__mptr = (ptr); \
48 (type *) ((char *) __mptr - offsetof(type, member));})
49#endif
50
51#define sizeof_field(type, field) sizeof(((type *)0)->field)
52
53
54
55
56
57#define endof(container, field) \
58 (offsetof(container, field) + sizeof_field(container, field))
59
60
61#define DO_UPCAST(type, field, dev) ( __extension__ ( { \
62 char __attribute__((unused)) offset_must_be_zero[ \
63 -offsetof(type, field)]; \
64 container_of(dev, type, field);}))
65
66#define typeof_field(type, field) typeof(((type *)0)->field)
67#define type_check(t1,t2) ((t1*)0 - (t2*)0)
68
69#define QEMU_BUILD_BUG_ON_STRUCT(x) \
70 struct { \
71 int:(x) ? -1 : 1; \
72 }
73
74#define QEMU_BUILD_BUG_MSG(x, msg) _Static_assert(!(x), msg)
75
76#define QEMU_BUILD_BUG_ON(x) QEMU_BUILD_BUG_MSG(x, "not expecting: " #x)
77
78#define QEMU_BUILD_BUG_ON_ZERO(x) (sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)) - \
79 sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)))
80
81#if !defined(__clang__) && defined(_WIN32)
82
83
84
85
86# define __printf__ __gnu_printf__
87#endif
88
89#ifndef __has_warning
90#define __has_warning(x) 0
91#endif
92
93#ifndef __has_feature
94#define __has_feature(x) 0
95#endif
96
97#ifndef __has_builtin
98#define __has_builtin(x) 0
99#endif
100
101#if __has_builtin(__builtin_assume_aligned) || !defined(__clang__)
102#define HAS_ASSUME_ALIGNED
103#endif
104
105#ifndef __has_attribute
106#define __has_attribute(x) 0
107#endif
108
109#if defined(__SANITIZE_ADDRESS__) || __has_feature(address_sanitizer)
110# define QEMU_SANITIZE_ADDRESS 1
111#endif
112
113#if defined(__SANITIZE_THREAD__) || __has_feature(thread_sanitizer)
114# define QEMU_SANITIZE_THREAD 1
115#endif
116
117
118
119
120
121
122#if __has_attribute(flatten) || !defined(__clang__)
123# define QEMU_FLATTEN __attribute__((flatten))
124#else
125# define QEMU_FLATTEN
126#endif
127
128
129
130
131
132
133#if __has_attribute(error)
134# define QEMU_ERROR(X) __attribute__((error(X)))
135#else
136# define QEMU_ERROR(X)
137#endif
138
139
140
141
142
143
144
145
146
147
148#if __has_attribute(nonstring)
149# define QEMU_NONSTRING __attribute__((nonstring))
150#else
151# define QEMU_NONSTRING
152#endif
153
154
155
156
157
158
159#if defined(__OPTIMIZE__)
160#define QEMU_ALWAYS_INLINE __attribute__((always_inline))
161#else
162#define QEMU_ALWAYS_INLINE
163#endif
164
165
166
167
168
169
170#if __has_attribute(fallthrough)
171# define QEMU_FALLTHROUGH __attribute__((fallthrough))
172#else
173# define QEMU_FALLTHROUGH do {} while (0)
174#endif
175
176#ifdef CONFIG_CFI
177
178
179
180
181#define QEMU_DISABLE_CFI __attribute__((no_sanitize("cfi-icall")))
182#else
183
184#define QEMU_DISABLE_CFI
185#endif
186
187#endif
188