xref: /freebsd/tools/build/cross-build/include/common/sys/cdefs.h (revision f9fd7337f63698f33239c58c07bf430198235a22)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright 2018-2020 Alex Richardson <arichardson@FreeBSD.org>
5  *
6  * This software was developed by SRI International and the University of
7  * Cambridge Computer Laboratory (Department of Computer Science and
8  * Technology) under DARPA contract HR0011-18-C-0016 ("ECATS"), as part of the
9  * DARPA SSITH research programme.
10  *
11  * This software was developed by SRI International and the University of
12  * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
13  * ("CTSRD"), as part of the DARPA CRASH research programme.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * $FreeBSD$
37  */
38 #pragma once
39 /* musl libc does not provide a sys/cdefs.h header */
40 #if __has_include_next(<sys/cdefs.h>)
41 #include_next <sys/cdefs.h>
42 #else
43 /* No sys/cdefs.h header exists so we have to provide some basic macros */
44 #ifdef __cplusplus
45 #define __BEGIN_DECLS extern "C" {
46 #define __END_DECLS }
47 #else
48 #define __BEGIN_DECLS
49 #define __END_DECLS
50 #endif
51 
52 #endif
53 
54 #ifndef __FBSDID
55 #define __FBSDID(id)
56 #endif
57 
58 #ifndef __IDSTRING
59 #define __IDSTRING(name, string)
60 #endif
61 
62 #ifndef rounddown
63 #define rounddown(x, y) (((x) / (y)) * (y))
64 #define rounddown2(x, y) ((x) & (~((y)-1))) /* if y is power of two */
65 #define roundup(x, y) ((((x) + ((y)-1)) / (y)) * (y)) /* to any y */
66 #define roundup2(x, y) \
67 	(((x) + ((y)-1)) & (~((y)-1))) /* if y is powers of two */
68 #define powerof2(x) ((((x)-1) & (x)) == 0)
69 #endif
70 
71 #ifndef __pure
72 #define __pure __attribute__((__pure__))
73 #endif
74 #ifndef __packed
75 #define __packed __attribute__((__packed__))
76 #endif
77 #ifndef __dead2
78 #define __dead2 __attribute__((__noreturn__))
79 #endif
80 #ifndef __pure2
81 #define __pure2 __attribute__((__const__))
82 #endif
83 #ifndef __used
84 #define __used __attribute__((__used__))
85 #endif
86 #ifndef __aligned
87 #define __aligned(x) __attribute__((__aligned__(x)))
88 #endif
89 #ifndef __section
90 #define __section(x) __attribute__((__section__(x)))
91 #endif
92 
93 #ifndef __alloc_size
94 #define __alloc_size(...) __attribute__((__alloc_size__(__VA_ARGS__)))
95 #endif
96 #ifndef __alloc_size2
97 #define __alloc_size2(n, x) __attribute__((__alloc_size__(n, x)))
98 #endif
99 #ifndef __alloc_align
100 #define __alloc_align(x) __attribute__((__alloc_align__(x)))
101 #endif
102 #ifndef __result_use_check
103 #define __result_use_check __attribute__((__warn_unused_result__))
104 #endif
105 #ifndef __printflike
106 #define __printflike(fmtarg, firstvararg) \
107 	__attribute__((__format__(__printf__, fmtarg, firstvararg)))
108 #endif
109 #ifndef __printf0like
110 #define __printf0like(fmtarg, firstvararg) \
111 	__attribute__((__format__(__printf0__, fmtarg, firstvararg)))
112 #endif
113 
114 #ifndef __predict_true
115 #define __predict_true(exp) __builtin_expect((exp), 1)
116 #endif
117 #ifndef __predict_false
118 #define __predict_false(exp) __builtin_expect((exp), 0)
119 #endif
120 
121 #ifndef __weak_reference
122 #ifdef __ELF__
123 #define __weak_reference(sym, alias) \
124 	__asm__(".weak " #alias);    \
125 	__asm__(".equ " #alias ", " #sym)
126 #else
127 #define __weak_reference(sym, alias) \
128 	static int alias() __attribute__((weakref(#sym)));
129 #endif
130 #endif
131 
132 /* Some files built as part of the bootstrap libegacy use these macros, but
133  * since we aren't actually building libc.so, we can defined them to be empty */
134 #ifndef __sym_compat
135 #define __sym_compat(sym, impl, verid) /* not needed for bootstrapping */
136 #endif
137 #ifndef __sym_default
138 #define __sym_default(sym, impl, verid) /* not needed for bootstrapping */
139 #endif
140 #ifndef __sym_default
141 #define __warn_references(sym, msg) /* not needed for bootstrapping */
142 #endif
143 
144 #ifndef __malloc_like
145 #define __malloc_like __attribute__((__malloc__))
146 #endif
147 
148 #ifndef nitems
149 // https://stackoverflow.com/questions/1598773/is-there-a-standard-function-in-c-that-would-return-the-length-of-an-array/1598827#1598827
150 #define nitems(x) \
151 	((sizeof(x) / sizeof(0 [x])) / ((size_t)(!(sizeof(x) % sizeof(0 [x])))))
152 #endif
153 
154 #ifndef __min_size
155 #if !defined(__cplusplus)
156 #define __min_size(x) static(x)
157 #else
158 #define __min_size(x) (x)
159 #endif
160 #endif
161 
162 #ifndef __unused
163 #define __unused __attribute__((unused))
164 #endif
165 #define __format_arg(fmtarg) __attribute__((__format_arg__(fmtarg)))
166 
167 #ifndef __exported
168 #define __exported __attribute__((__visibility__("default")))
169 #endif
170 #ifndef __hidden
171 #define __hidden __attribute__((__visibility__("hidden")))
172 #endif
173 
174 #ifndef __unreachable
175 #define __unreachable() __builtin_unreachable()
176 #endif
177 
178 #ifndef __clang__
179 /* GCC doesn't like the printf0 format specifier. Clang treats it the same as
180  * printf so add the compatibility macro here. */
181 #define __printf0__ __printf__
182 #endif
183 
184 /*
185  * These should probably be in sys/types.h but mtree expects them to exist
186  * without including <sys/types.h>
187  */
188 typedef unsigned char u_char;
189 typedef unsigned short u_short;
190 typedef unsigned int u_int;
191 typedef unsigned long u_long;
192 
193 /* On MacOS __CONCAT is defined as x ## y, which won't expand macros */
194 #undef __CONCAT
195 #define __CONCAT1(x, y) x##y
196 #define __CONCAT(x, y) __CONCAT1(x, y)
197 
198 #ifndef __STRING
199 #define __STRING(x) #x /* stringify without expanding x */
200 #endif
201 #ifndef __XSTRING
202 #define __XSTRING(x) __STRING(x) /* expand x, then stringify */
203 #endif
204 
205 #ifndef __has_feature
206 #define __has_feature(...) 0
207 #endif
208 
209 #ifndef __has_builtin
210 #define __has_builtin(...) 0
211 #endif
212 
213 /*
214  * Nullability qualifiers: currently only supported by Clang.
215  */
216 #if !(defined(__clang__) && __has_feature(nullability))
217 #define _Nonnull
218 #define _Nullable
219 #define _Null_unspecified
220 #define __NULLABILITY_PRAGMA_PUSH
221 #define __NULLABILITY_PRAGMA_POP
222 #else
223 #define __NULLABILITY_PRAGMA_PUSH        \
224 	_Pragma("clang diagnostic push") \
225 	    _Pragma("clang diagnostic ignored \"-Wnullability-completeness\"")
226 #define __NULLABILITY_PRAGMA_POP _Pragma("clang diagnostic pop")
227 #endif
228 
229 #ifndef __offsetof
230 #define __offsetof(type, field) __builtin_offsetof(type, field)
231 #endif
232 
233 #define __rangeof(type, start, end) \
234 	(__offsetof(type, end) - __offsetof(type, start))
235 
236 #ifndef __containerof
237 #define __containerof(x, s, m)                                           \
238 	({                                                               \
239 		const volatile __typeof(((s *)0)->m) *__x = (x);         \
240 		__DEQUALIFY(                                             \
241 		    s *, (const volatile char *)__x - __offsetof(s, m)); \
242 	})
243 #endif
244 
245 #ifndef __RCSID
246 #define __RCSID(x)
247 #endif
248 #ifndef __FBSDID
249 #define __FBSDID(x)
250 #endif
251 #ifndef __RCSID
252 #define __RCSID(x)
253 #endif
254 #ifndef __RCSID_SOURCE
255 #define __RCSID_SOURCE(x)
256 #endif
257 #ifndef __SCCSID
258 #define __SCCSID(x)
259 #endif
260 #ifndef __COPYRIGHT
261 #define __COPYRIGHT(x)
262 #endif
263 #ifndef __DECONST
264 #define __DECONST(type, var) ((type)(__uintptr_t)(const void *)(var))
265 #endif
266 
267 #ifndef __DEVOLATILE
268 #define __DEVOLATILE(type, var) ((type)(__uintptr_t)(volatile void *)(var))
269 #endif
270 
271 #ifndef __DEQUALIFY
272 #define __DEQUALIFY(type, var) ((type)(__uintptr_t)(const volatile void *)(var))
273 #endif
274 
275 
276 /* Expose all declarations when using FreeBSD headers */
277 #define	__POSIX_VISIBLE		200809
278 #define	__XSI_VISIBLE		700
279 #define	__BSD_VISIBLE		1
280 #define	__ISO_C_VISIBLE		2011
281 #define	__EXT1_VISIBLE		1
282