]>
git.saurik.com Git - apple/xnu.git/blob - libkern/os/base.h
2 * Copyright (c) 2008-2013 Apple Inc. All rights reserved.
4 * @APPLE_APACHE_LICENSE_HEADER_START@
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * @APPLE_APACHE_LICENSE_HEADER_END@
24 #include <sys/cdefs.h>
27 #define __has_builtin(x) 0
30 #define __has_include(x) 0
33 #define __has_feature(x) 0
35 #ifndef __has_attribute
36 #define __has_attribute(x) 0
38 #ifndef __has_extension
39 #define __has_extension(x) 0
42 #undef OS_INLINE // <sys/_types/_os_inline.h>
44 #define OS_NORETURN __attribute__((__noreturn__))
45 #define OS_NOTHROW __attribute__((__nothrow__))
46 #define OS_NONNULL1 __attribute__((__nonnull__(1)))
47 #define OS_NONNULL2 __attribute__((__nonnull__(2)))
48 #define OS_NONNULL3 __attribute__((__nonnull__(3)))
49 #define OS_NONNULL4 __attribute__((__nonnull__(4)))
50 #define OS_NONNULL5 __attribute__((__nonnull__(5)))
51 #define OS_NONNULL6 __attribute__((__nonnull__(6)))
52 #define OS_NONNULL7 __attribute__((__nonnull__(7)))
53 #define OS_NONNULL8 __attribute__((__nonnull__(8)))
54 #define OS_NONNULL9 __attribute__((__nonnull__(9)))
55 #define OS_NONNULL10 __attribute__((__nonnull__(10)))
56 #define OS_NONNULL11 __attribute__((__nonnull__(11)))
57 #define OS_NONNULL12 __attribute__((__nonnull__(12)))
58 #define OS_NONNULL13 __attribute__((__nonnull__(13)))
59 #define OS_NONNULL14 __attribute__((__nonnull__(14)))
60 #define OS_NONNULL15 __attribute__((__nonnull__(15)))
61 #define OS_NONNULL_ALL __attribute__((__nonnull__))
62 #define OS_SENTINEL __attribute__((__sentinel__))
63 #define OS_PURE __attribute__((__pure__))
64 #define OS_CONST __attribute__((__const__))
65 #define OS_WARN_RESULT __attribute__((__warn_unused_result__))
66 #define OS_MALLOC __attribute__((__malloc__))
67 #define OS_USED __attribute__((__used__))
68 #define OS_UNUSED __attribute__((__unused__))
69 #define OS_COLD __attribute__((__cold__))
70 #define OS_WEAK __attribute__((__weak__))
71 #define OS_WEAK_IMPORT __attribute__((__weak_import__))
72 #define OS_NOINLINE __attribute__((__noinline__))
73 #define OS_ALWAYS_INLINE __attribute__((__always_inline__))
74 #define OS_TRANSPARENT_UNION __attribute__((__transparent_union__))
75 #define OS_ALIGNED(n) __attribute__((__aligned__((n))))
76 #define OS_FORMAT_PRINTF(x, y) __attribute__((__format__(printf,x,y)))
77 #define OS_EXPORT extern __attribute__((__visibility__("default")))
78 #define OS_INLINE static __inline__
79 #define OS_EXPECT(x, v) __builtin_expect((x), (v))
98 #define OS_NONNULL_ALL
102 #define OS_WARN_RESULT
108 #define OS_WEAK_IMPORT
110 #define OS_ALWAYS_INLINE
111 #define OS_TRANSPARENT_UNION
112 #define OS_ALIGNED(n)
113 #define OS_FORMAT_PRINTF(x, y)
114 #define OS_EXPORT extern
115 #define OS_INLINE static inline
116 #define OS_EXPECT(x, v) (x)
119 #if __has_attribute(noescape)
120 #define OS_NOESCAPE __attribute__((__noescape__))
125 #if defined(__cplusplus) && defined(__clang__)
126 #define OS_FALLTHROUGH [[clang::fallthrough]]
128 #define OS_FALLTHROUGH
131 #if __has_feature(assume_nonnull)
132 #define OS_ASSUME_NONNULL_BEGIN _Pragma("clang assume_nonnull begin")
133 #define OS_ASSUME_NONNULL_END _Pragma("clang assume_nonnull end")
135 #define OS_ASSUME_NONNULL_BEGIN
136 #define OS_ASSUME_NONNULL_END
139 #if __has_builtin(__builtin_assume)
140 #define OS_COMPILER_CAN_ASSUME(expr) __builtin_assume(expr)
142 #define OS_COMPILER_CAN_ASSUME(expr) ((void)(expr))
145 #if __has_extension(attribute_overloadable)
146 #define OS_OVERLOADABLE __attribute__((__overloadable__))
148 #define OS_OVERLOADABLE
151 #if __has_attribute(enum_extensibility)
152 #define __OS_ENUM_ATTR __attribute__((enum_extensibility(open)))
153 #define __OS_ENUM_ATTR_CLOSED __attribute__((enum_extensibility(closed)))
155 #define __OS_ENUM_ATTR
156 #define __OS_ENUM_ATTR_CLOSED
157 #endif // __has_attribute(enum_extensibility)
159 #if __has_attribute(flag_enum)
161 * Compile with -Wflag-enum and -Wassign-enum to enforce at definition and
162 * assignment, respectively, i.e. -Wflag-enum prevents you from creating new
163 * enumeration values from illegal values within the enum definition, and
164 * -Wassign-enum prevents you from assigning illegal values to a variable of the
167 #define __OS_OPTIONS_ATTR __attribute__((flag_enum))
169 #define __OS_OPTIONS_ATTR
170 #endif // __has_attribute(flag_enum)
172 #if __has_feature(objc_fixed_enum) || __has_extension(cxx_fixed_enum) || \
173 __has_extension(cxx_strong_enums)
174 #define OS_ENUM(_name, _type, ...) \
175 typedef enum : _type { __VA_ARGS__ } _name##_t
176 #define OS_CLOSED_ENUM(_name, _type, ...) \
177 typedef enum : _type { __VA_ARGS__ } \
178 __OS_ENUM_ATTR_CLOSED _name##_t
179 #define OS_OPTIONS(_name, _type, ...) \
180 typedef enum : _type { __VA_ARGS__ } \
181 __OS_ENUM_ATTR __OS_OPTIONS_ATTR _name##_t
182 #define OS_CLOSED_OPTIONS(_name, _type, ...) \
183 typedef enum : _type { __VA_ARGS__ } \
184 __OS_ENUM_ATTR_CLOSED __OS_OPTIONS_ATTR _name##_t
187 * There is unfortunately no good way in plain C to have both fixed-type enums
188 * and enforcement for clang's enum_extensibility extensions. The primary goal
189 * of these macros is to allow you to define an enum and specify its width in a
190 * single statement, and for plain C that is accomplished by defining an
191 * anonymous enum and then separately typedef'ing the requested type name to the
192 * requested underlying integer type. So the type emitted actually has no
193 * relationship at all to the enum, and therefore while the compiler could
194 * enforce enum extensibility if you used the enum type, it cannot do so if you
195 * use the "_t" type resulting from this expression.
197 * But we still define a named enum type and decorate it appropriately for you,
198 * so if you really want the enum extensibility enforcement, you can use the
199 * enum type yourself, i.e. when compiling with a C compiler:
201 * OS_CLOSED_ENUM(my_type, uint64_t,
207 * my_type_t mt = 98; // legal
208 * enum my_type emt = 98; // illegal
210 * But be aware that the underlying enum type's width is subject only to the C
211 * language's guarantees -- namely that it will be compatible with int, char,
212 * and unsigned char. It is not safe to rely on the size of this type.
214 * When compiling in ObjC or C++, both of the above assignments are illegal.
216 #define __OS_ENUM_C_FALLBACK(_name, _type, ...) \
217 typedef _type _name##_t; enum _name { __VA_ARGS__ }
219 #define OS_ENUM(_name, _type, ...) \
220 typedef _type _name##_t; enum { __VA_ARGS__ }
221 #define OS_CLOSED_ENUM(_name, _type, ...) \
222 __OS_ENUM_C_FALLBACK(_name, _type, ## __VA_ARGS__) \
223 __OS_ENUM_ATTR_CLOSED
224 #define OS_OPTIONS(_name, _type, ...) \
225 __OS_ENUM_C_FALLBACK(_name, _type, ## __VA_ARGS__) \
226 __OS_ENUM_ATTR __OS_OPTIONS_ATTR
227 #define OS_CLOSED_OPTIONS(_name, _type, ...) \
228 __OS_ENUM_C_FALLBACK(_name, _type, ## __VA_ARGS__) \
229 __OS_ENUM_ATTR_CLOSED __OS_OPTIONS_ATTR
230 #endif // __has_feature(objc_fixed_enum) || __has_extension(cxx_strong_enums)
232 #if __has_feature(attribute_availability_swift)
233 // equivalent to __SWIFT_UNAVAILABLE from Availability.h
234 #define OS_SWIFT_UNAVAILABLE(_msg) \
235 __attribute__((__availability__(swift, unavailable, message=_msg)))
237 #define OS_SWIFT_UNAVAILABLE(_msg)
240 #if __has_attribute(swift_private)
241 # define OS_REFINED_FOR_SWIFT __attribute__((__swift_private__))
243 # define OS_REFINED_FOR_SWIFT
246 #if __has_attribute(swift_name)
247 # define OS_SWIFT_NAME(_name) __attribute__((__swift_name__(#_name)))
249 # define OS_SWIFT_NAME(_name)
252 #define __OS_STRINGIFY(s) #s
253 #define OS_STRINGIFY(s) __OS_STRINGIFY(s)
254 #define __OS_CONCAT(x, y) x ## y
255 #define OS_CONCAT(x, y) __OS_CONCAT(x, y)
258 #define os_prevent_tail_call_optimization() __asm__("")
259 #define os_is_compile_time_constant(expr) __builtin_constant_p(expr)
261 #define os_compiler_barrier() __asm__ __volatile__("" ::: "memory")
264 #define os_prevent_tail_call_optimization() do { } while (0)
265 #define os_is_compile_time_constant(expr) 0
267 #define os_compiler_barrier() do { } while (0)
271 #if __has_attribute(not_tail_called)
272 #define OS_NOT_TAIL_CALLED __attribute__((__not_tail_called__))
274 #define OS_NOT_TAIL_CALLED
278 * LIBKERN_ALWAYS_DESTROY attribute can be applied to global variables with
279 * destructors. It specifies that and object should have its exit-time
280 * destructor run. This attribute is the default unless clang was invoked with
281 * -fno-c++-static-destructors.
283 #if __has_attribute(always_destroy)
284 #define LIBKERN_ALWAYS_DESTROY __attribute__((always_destroy))
286 #define LIBKERN_ALWAYS_DESTROY
289 typedef void (*os_function_t
)(void *_Nullable
);
293 * @typedef os_block_t
296 * Generic type for a block taking no arguments and returning no value.
299 * When not building with Objective-C ARC, a block object allocated on or
300 * copied to the heap must be released with a -[release] message or the
301 * Block_release() function.
303 * The declaration of a block literal allocates storage on the stack.
304 * Therefore, this is an invalid construct:
308 * block = ^{ printf("true\n"); };
310 * block = ^{ printf("false\n"); };
312 * block(); // unsafe!!!
315 * What is happening behind the scenes:
318 * struct Block __tmp_1 = ...; // setup details
321 * struct Block __tmp_2 = ...; // setup details
326 * As the example demonstrates, the address of a stack variable is escaping the
327 * scope in which it is allocated. That is a classic C bug.
329 * Instead, the block literal must be copied to the heap with the Block_copy()
330 * function or by sending it a -[copy] message.
332 typedef void (^os_block_t
)(void);
335 #endif // __OS_BASE__