]> git.proxmox.com Git - efi-boot-shim.git/blob - include/compiler.h
New upstream version 15.6
[efi-boot-shim.git] / include / compiler.h
1 // SPDX-License-Identifier: BSD-2-Clause-Patent
2
3 #ifndef COMPILER_H_
4 #define COMPILER_H_
5
6 /*
7 * These are special ones that get our unit tests in trouble with the
8 * compiler optimizer dropping out tests...
9 */
10 #ifdef NONNULL
11 # undef NONNULL
12 #endif
13 #ifdef RETURNS_NONNULL
14 # undef RETURNS_NONNULL
15 #endif
16 #ifdef SHIM_UNIT_TEST
17 # define NONNULL(first, args...)
18 # define RETURNS_NONNULL
19 #else
20 # define NONNULL(first, args...) __attribute__((__nonnull__(first, ## args)))
21 #if (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9)))
22 # define RETURNS_NONNULL __attribute__((__returns_nonnull__))
23 #else
24 # define RETURNS_NONNULL
25 #endif
26 #endif
27
28 #ifndef UNUSED
29 #define UNUSED __attribute__((__unused__))
30 #endif
31 #ifndef HIDDEN
32 #define HIDDEN __attribute__((__visibility__ ("hidden")))
33 #endif
34 #ifndef PUBLIC
35 #define PUBLIC __attribute__((__visibility__ ("default")))
36 #endif
37 #ifndef DEPRECATED
38 #define DEPRECATED __attribute__((__deprecated__))
39 #endif
40 #ifndef DESTRUCTOR
41 #define DESTRUCTOR __attribute__((destructor))
42 #endif
43 #ifndef CONSTRUCTOR
44 #define CONSTRUCTOR __attribute__((constructor))
45 #endif
46 #ifndef ALIAS
47 #define ALIAS(x) __attribute__((weak, alias (#x)))
48 #endif
49 #ifndef ALLOCFUNC
50 #if defined(__COVERITY__)
51 #define ALLOCFUNC(a, b)
52 #else
53 #define ALLOCFUNC(dealloc, dealloc_arg) __attribute__((__malloc__(dealloc, dealloc_arg)))
54 #endif
55 #endif
56 #ifndef PRINTF
57 #define PRINTF(first, args...) __attribute__((__format__(printf, first, ## args)))
58 #endif
59 #ifndef PURE
60 #define PURE __attribute__((__pure__))
61 #endif
62 #ifndef FLATTEN
63 #define FLATTEN __attribute__((__flatten__))
64 #endif
65 #ifndef PACKED
66 #define PACKED __attribute__((__packed__))
67 #endif
68 #ifndef VERSION
69 #define VERSION(sym, ver) __asm__(".symver " # sym "," # ver)
70 #endif
71 #ifndef NORETURN
72 #define NORETURN __attribute__((__noreturn__))
73 #endif
74 #ifndef ALIGNED
75 #define ALIGNED(n) __attribute__((__aligned__(n)))
76 #endif
77 #ifndef CLEANUP_FUNC
78 #define CLEANUP_FUNC(x) __attribute__((__cleanup__(x)))
79 #endif
80 #ifndef USED
81 #define USED __attribute__((__used__))
82 #endif
83 #ifndef SECTION
84 #define SECTION(x) __attribute__((__section__(x)))
85 #endif
86 #ifndef OPTIMIZE
87 #define OPTIMIZE(x) __attribute__((__optimize__(x)))
88 #endif
89
90 #ifndef __CONCAT
91 #define __CONCAT(a, b) a ## b
92 #endif
93 #ifndef __CONCAT3
94 #define __CONCAT3(a, b, c) a ## b ## c
95 #endif
96 #ifndef CAT
97 #define CAT(a, b) __CONCAT(a, b)
98 #endif
99 #ifndef CAT3
100 #define CAT3(a, b, c) __CONCAT3(a, b, c)
101 #endif
102 #ifndef STRING
103 #define STRING(x) __STRING(x)
104 #endif
105
106 #ifndef WRITE_ONCE
107 #define WRITE_ONCE(var, val) \
108 (*((volatile typeof(val) *)(&(var))) = (val))
109 #endif
110
111 #ifndef READ_ONCE
112 #define READ_ONCE(var) (*((volatile typeof(var) *)(&(var))))
113 #endif
114
115 #ifndef likely
116 #define likely(x) __builtin_expect(!!(x), 1)
117 #endif
118
119 #ifndef unlikely
120 #define unlikely(x) __builtin_expect(!!(x), 0)
121 #endif
122
123 /* Are two types/vars the same type (ignoring qualifiers)? */
124 #ifndef __same_type
125 #define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
126 #endif
127
128 /* Compile time object size, -1 for unknown */
129 #ifndef __compiletime_object_size
130 # define __compiletime_object_size(obj) -1
131 #endif
132 #ifndef __compiletime_warning
133 # define __compiletime_warning(message)
134 #endif
135 #ifndef __compiletime_error
136 # define __compiletime_error(message)
137 #endif
138
139 #ifndef __compiletime_assert
140 #define __compiletime_assert(condition, msg, prefix, suffix) \
141 do { \
142 extern void prefix ## suffix(void) __compiletime_error(msg); \
143 if (!(condition)) \
144 prefix ## suffix(); \
145 } while (0)
146 #endif
147
148 #ifndef _compiletime_assert
149 #define _compiletime_assert(condition, msg, prefix, suffix) \
150 __compiletime_assert(condition, msg, prefix, suffix)
151 #endif
152
153 /**
154 * compiletime_assert - break build and emit msg if condition is false
155 * @condition: a compile-time constant condition to check
156 * @msg: a message to emit if condition is false
157 *
158 * In tradition of POSIX assert, this macro will break the build if the
159 * supplied condition is *false*, emitting the supplied error message if the
160 * compiler has support to do so.
161 */
162 #ifndef compiletime_assert
163 #define compiletime_assert(condition, msg) \
164 _compiletime_assert(condition, msg, __compiletime_assert_, __LINE__ - 1)
165 #endif
166
167 /**
168 * BUILD_BUG_ON_MSG - break compile if a condition is true & emit supplied
169 * error message.
170 * @condition: the condition which the compiler should know is false.
171 *
172 * See BUILD_BUG_ON for description.
173 */
174 #ifndef BUILD_BUG_ON_MSG
175 #define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg)
176 #endif
177
178 #ifndef ALIGN
179 #define __ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask))
180 #define __ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a) - 1)
181 #define ALIGN(x, a) __ALIGN((x), (a))
182 #endif
183 #ifndef ALIGN_DOWN
184 #define ALIGN_DOWN(x, a) __ALIGN((x) - ((a) - 1), (a))
185 #endif
186
187 #define MIN(a, b) ({(a) < (b) ? (a) : (b);})
188 #define MAX(a, b) ({(a) <= (b) ? (b) : (a);})
189
190 /**
191 * Builtins that don't go in string.h
192 */
193 #define unreachable() __builtin_unreachable()
194
195 #endif /* !COMPILER_H_ */
196 // vim:fenc=utf-8:tw=75:et