xref: /trueos/include/os/base.h (revision 0f8eb4123024ffec2f2cfcdb493793aea43f0cac)
1 /*
2  * Copyright (c) 2008-2013 Apple Inc. All rights reserved.
3  *
4  * @APPLE_APACHE_LICENSE_HEADER_START@
5  *
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * @APPLE_APACHE_LICENSE_HEADER_END@
19  */
20 
21 #ifndef __OS_BASE__
22 #define __OS_BASE__
23 
24 #include <sys/cdefs.h>
25 
26 #if __GNUC__
27 #define os_fastpath(x) ((__typeof__(x))(uintptr_t)__builtin_expect((uintptr_t)(x), ~0l))
28 #define os_slowpath(x) ((__typeof__(x))(uintptr_t)__builtin_expect((uintptr_t)(x), 0l))
29 #define os_constant(x) __builtin_constant_p((x))
30 #define os_hardware_trap() __asm__ __volatile__ (""); __builtin_trap()
31 
32 #define __OS_COMPILETIME_ASSERT__(e) __extension__({ \
33 	char __compile_time_assert__[(e) ? 1 : -1];	\
34 	(void)__compile_time_assert__; \
35 })
36 
37 #define __OS_CONST __attribute__((__const__))
38 #define __OS_PRINTFLIKE(x,y) __attribute__((__format__(printf,x,y)))
39 #else /* __GNUC__ */
40 #define os_fastpath(x) (x)
41 #define os_slowpath(x) (x)
42 #define os_constant(x) ((long)0)
43 #define os_hardware_trap() abort()
44 
45 #define __OS_COMPILETIME_ASSERT__(e) (e)
46 
47 #define __OS_CONST
48 #define __OS_PRINTFLIKE(x,y)
49 #endif /* __GNUC__ */
50 
51 
52 #ifndef __has_builtin
53 #define __has_builtin(x) 0
54 #endif
55 #ifndef __has_include
56 #define __has_include(x) 0
57 #endif
58 #ifndef __has_feature
59 #define __has_feature(x) 0
60 #endif
61 #ifndef __has_attribute
62 #define __has_attribute(x) 0
63 #endif
64 
65 #if __GNUC__
66 #define OS_NORETURN __attribute__((__noreturn__))
67 #define OS_NOTHROW __attribute__((__nothrow__))
68 #define OS_NONNULL1 __attribute__((__nonnull__(1)))
69 #define OS_NONNULL2 __attribute__((__nonnull__(2)))
70 #define OS_NONNULL3 __attribute__((__nonnull__(3)))
71 #define OS_NONNULL4 __attribute__((__nonnull__(4)))
72 #define OS_NONNULL5 __attribute__((__nonnull__(5)))
73 #define OS_NONNULL6 __attribute__((__nonnull__(6)))
74 #define OS_NONNULL7 __attribute__((__nonnull__(7)))
75 #define OS_NONNULL8 __attribute__((__nonnull__(8)))
76 #define OS_NONNULL9 __attribute__((__nonnull__(9)))
77 #define OS_NONNULL10 __attribute__((__nonnull__(10)))
78 #define OS_NONNULL11 __attribute__((__nonnull__(11)))
79 #define OS_NONNULL12 __attribute__((__nonnull__(12)))
80 #define OS_NONNULL13 __attribute__((__nonnull__(13)))
81 #define OS_NONNULL14 __attribute__((__nonnull__(14)))
82 #define OS_NONNULL15 __attribute__((__nonnull__(15)))
83 #define OS_NONNULL_ALL __attribute__((__nonnull__))
84 #define OS_SENTINEL __attribute__((__sentinel__))
85 #define OS_PURE __attribute__((__pure__))
86 #define OS_CONST __attribute__((__const__))
87 #define OS_WARN_RESULT __attribute__((__warn_unused_result__))
88 #define OS_MALLOC __attribute__((__malloc__))
89 #define OS_USED __attribute__((__used__))
90 #define OS_UNUSED __attribute__((__unused__))
91 #define OS_WEAK __attribute__((__weak__))
92 #define OS_WEAK_IMPORT __attribute__((__weak_import__))
93 #define OS_NOINLINE __attribute__((__noinline__))
94 #define OS_ALWAYS_INLINE __attribute__((__always_inline__))
95 #define OS_TRANSPARENT_UNION __attribute__((__transparent_union__))
96 #define OS_ALIGNED(n) __attribute__((__aligned__((n))))
97 #define OS_FORMAT_PRINTF(x,y) __attribute__((__format__(printf,x,y)))
98 #define OS_EXPORT extern __attribute__((__visibility__("default")))
99 #ifndef OS_INLINE
100 #define OS_INLINE static __inline__
101 #endif
102 #define OS_EXPECT(x, v) __builtin_expect((x), (v))
103 #else
104 #define OS_NORETURN
105 #define OS_NOTHROW
106 #define OS_NONNULL1
107 #define OS_NONNULL2
108 #define OS_NONNULL3
109 #define OS_NONNULL4
110 #define OS_NONNULL5
111 #define OS_NONNULL6
112 #define OS_NONNULL7
113 #define OS_NONNULL8
114 #define OS_NONNULL9
115 #define OS_NONNULL10
116 #define OS_NONNULL11
117 #define OS_NONNULL12
118 #define OS_NONNULL13
119 #define OS_NONNULL14
120 #define OS_NONNULL15
121 #define OS_NONNULL_ALL
122 #define OS_SENTINEL
123 #define OS_PURE
124 #define OS_CONST
125 #define OS_WARN_RESULT
126 #define OS_MALLOC
127 #define OS_USED
128 #define OS_UNUSED
129 #define OS_WEAK
130 #define OS_WEAK_IMPORT
131 #define OS_NOINLINE
132 #define OS_ALWAYS_INLINE
133 #define OS_TRANSPARENT_UNION
134 #define OS_ALIGNED(n)
135 #define OS_FORMAT_PRINTF(x,y)
136 #define OS_EXPORT extern
137 #ifndef OS_INLINE
138 #define OS_INLINE static inline
139 #endif
140 #define OS_EXPECT(x, v) (x)
141 #endif
142 
143 #if __has_extension(attribute_overloadable)
144 #define OS_OVERLOADABLE __attribute__((__overloadable__))
145 #else
146 #define OS_OVERLOADABLE
147 #endif
148 
149 #if __has_feature(objc_fixed_enum) || __has_extension(cxx_strong_enums)
150 #define OS_ENUM(_name, _type, ...) \
151 		typedef enum : _type { __VA_ARGS__ } _name##_t
152 #else
153 #define OS_ENUM(_name, _type, ...) \
154 		enum { __VA_ARGS__ }; typedef _type _name##_t
155 #endif
156 
157 #define __OS_STRINGIFY(s) #s
158 #define OS_STRINGIFY(s) __OS_STRINGIFY(s)
159 #define __OS_CONCAT(x, y) x ## y
160 #define OS_CONCAT(x, y) __OS_CONCAT(x, y)
161 
162 #endif // __OS_BASE__
163