xref: /xnu-11215/bsd/sys/cdefs.h (revision 33de042d)
1 /*
2  * Copyright (c) 2000-2018 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 /* Copyright 1995 NeXT Computer, Inc. All rights reserved. */
29 /*
30  * Copyright (c) 1991, 1993
31  *	The Regents of the University of California.  All rights reserved.
32  *
33  * This code is derived from software contributed to Berkeley by
34  * Berkeley Software Design, Inc.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. All advertising materials mentioning features or use of this software
45  *    must display the following acknowledgement:
46  *	This product includes software developed by the University of
47  *	California, Berkeley and its contributors.
48  * 4. Neither the name of the University nor the names of its contributors
49  *    may be used to endorse or promote products derived from this software
50  *    without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62  * SUCH DAMAGE.
63  *
64  *	@(#)cdefs.h	8.8 (Berkeley) 1/9/95
65  */
66 
67 #ifndef _CDEFS_H_
68 #define _CDEFS_H_
69 
70 #if defined(__cplusplus)
71 #define __BEGIN_DECLS   extern "C" {
72 #define __END_DECLS     }
73 #else
74 #define __BEGIN_DECLS
75 #define __END_DECLS
76 #endif
77 
78 /* This SDK is designed to work with clang and specific versions of
79  * gcc >= 4.0 with Apple's patch sets */
80 #if !defined(__GNUC__) || __GNUC__ < 4
81 #warning "Unsupported compiler detected"
82 #endif
83 
84 /*
85  * Compatibility with compilers and environments that don't support compiler
86  * feature checking function-like macros.
87  */
88 #ifndef __has_builtin
89 #define __has_builtin(x) 0
90 #endif
91 #ifndef __has_include
92 #define __has_include(x) 0
93 #endif
94 #ifndef __has_feature
95 #define __has_feature(x) 0
96 #endif
97 #ifndef __has_attribute
98 #define __has_attribute(x) 0
99 #endif
100 #ifndef __has_cpp_attribute
101 #define __has_cpp_attribute(x) 0
102 #endif
103 #ifndef __has_extension
104 #define __has_extension(x) 0
105 #endif
106 
107 /*
108  * The __CONCAT macro is used to concatenate parts of symbol names, e.g.
109  * with "#define OLD(foo) __CONCAT(old,foo)", OLD(foo) produces oldfoo.
110  * The __CONCAT macro is a bit tricky -- make sure you don't put spaces
111  * in between its arguments.  __CONCAT can also concatenate double-quoted
112  * strings produced by the __STRING macro, but this only works with ANSI C.
113  */
114 #if defined(__STDC__) || defined(__cplusplus)
115 #define __P(protos)     protos          /* full-blown ANSI C */
116 #define __CONCAT(x, y)   x ## y
117 #define __STRING(x)     #x
118 
119 #define __const         const           /* define reserved names to standard */
120 #define __signed        signed
121 #define __volatile      volatile
122 #if defined(__cplusplus)
123 #define __inline        inline          /* convert to C++ keyword */
124 #else
125 #ifndef __GNUC__
126 #define __inline                        /* delete GCC keyword */
127 #endif /* !__GNUC__ */
128 #endif /* !__cplusplus */
129 
130 #else   /* !(__STDC__ || __cplusplus) */
131 #define __P(protos)     ()              /* traditional C preprocessor */
132 #define __CONCAT(x, y)   x /**/ y
133 #define __STRING(x)     "x"
134 
135 #ifndef __GNUC__
136 #define __const                         /* delete pseudo-ANSI C keywords */
137 #define __inline
138 #define __signed
139 #define __volatile
140 #endif  /* !__GNUC__ */
141 
142 /*
143  * In non-ANSI C environments, new programs will want ANSI-only C keywords
144  * deleted from the program and old programs will want them left alone.
145  * When using a compiler other than gcc, programs using the ANSI C keywords
146  * const, inline etc. as normal identifiers should define -DNO_ANSI_KEYWORDS.
147  * When using "gcc -traditional", we assume that this is the intent; if
148  * __GNUC__ is defined but __STDC__ is not, we leave the new keywords alone.
149  */
150 #ifndef NO_ANSI_KEYWORDS
151 #define const           __const                 /* convert ANSI C keywords */
152 #define inline          __inline
153 #define signed          __signed
154 #define volatile        __volatile
155 #endif /* !NO_ANSI_KEYWORDS */
156 #endif /* !(__STDC__ || __cplusplus) */
157 
158 /*
159  * __pure2 can be used for functions that are only a function of their scalar
160  * arguments (meaning they can't dereference pointers).
161  *
162  * __stateful_pure can be used for functions that have no side effects,
163  * but depend on the state of the memory.
164  */
165 #define __dead2         __attribute__((__noreturn__))
166 #define __pure2         __attribute__((__const__))
167 #define __stateful_pure __attribute__((__pure__))
168 
169 /* __unused denotes variables and functions that may not be used, preventing
170  * the compiler from warning about it if not used.
171  */
172 #define __unused        __attribute__((__unused__))
173 
174 /* __used forces variables and functions to be included even if it appears
175  * to the compiler that they are not used (and would thust be discarded).
176  */
177 #define __used          __attribute__((__used__))
178 
179 /* __cold marks code used for debugging or that is rarely taken
180  * and tells the compiler to optimize for size and outline code.
181  */
182 #if __has_attribute(cold)
183 #define __cold          __attribute__((__cold__))
184 #else
185 #define __cold
186 #endif
187 
188 /* __returns_nonnull marks functions that return a non-null pointer. */
189 #if __has_attribute(returns_nonnull)
190 #define __returns_nonnull __attribute((returns_nonnull))
191 #else
192 #define __returns_nonnull
193 #endif
194 
195 /* __exported denotes symbols that should be exported even when symbols
196  * are hidden by default.
197  * __exported_push/_exported_pop are pragmas used to delimit a range of
198  *  symbols that should be exported even when symbols are hidden by default.
199  */
200 #define __exported      __attribute__((__visibility__("default")))
201 #define __exported_push _Pragma("GCC visibility push(default)")
202 #define __exported_pop  _Pragma("GCC visibility pop")
203 
204 /* __deprecated causes the compiler to produce a warning when encountering
205  * code using the deprecated functionality.
206  * __deprecated_msg() does the same, and compilers that support it will print
207  * a message along with the deprecation warning.
208  * This may require turning on such warning with the -Wdeprecated flag.
209  * __deprecated_enum_msg() should be used on enums, and compilers that support
210  * it will print the deprecation warning.
211  * __kpi_deprecated() specifically indicates deprecation of kernel programming
212  * interfaces in Kernel.framework used by KEXTs.
213  */
214 #define __deprecated    __attribute__((__deprecated__))
215 
216 #if __has_extension(attribute_deprecated_with_message) || \
217         (defined(__GNUC__) && ((__GNUC__ >= 5) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 5))))
218 	#define __deprecated_msg(_msg) __attribute__((__deprecated__(_msg)))
219 #else
220 	#define __deprecated_msg(_msg) __attribute__((__deprecated__))
221 #endif
222 
223 #if __has_extension(enumerator_attributes)
224 	#define __deprecated_enum_msg(_msg) __deprecated_msg(_msg)
225 #else
226 	#define __deprecated_enum_msg(_msg)
227 #endif
228 
229 #if defined(KERNEL) && !defined(KERNEL_PRIVATE)
230 #define __kpi_deprecated(_msg) __deprecated_msg(_msg)
231 #else /* !defined(KERNEL) || defined(KERNEL_PRIVATE) */
232 #define __kpi_deprecated(_msg)
233 #endif /* !defined(KERNEL) || defined(KERNEL_PRIVATE) */
234 
235 /* __unavailable causes the compiler to error out when encountering
236  * code using the tagged function
237  */
238 #if __has_attribute(unavailable)
239 #define __unavailable __attribute__((__unavailable__))
240 #else
241 #define __unavailable
242 #endif
243 
244 #if defined(KERNEL) && !defined(KERNEL_PRIVATE)
245 #define __kpi_unavailable __unavailable
246 #else /* !defined(KERNEL) || defined(KERNEL_PRIVATE) */
247 #define __kpi_unavailable
248 #endif /* !defined(KERNEL) || defined(KERNEL_PRIVATE) */
249 
250 #if XNU_KERNEL_PRIVATE
251 /* This macro is meant to be used for kpi deprecated to x86 3rd parties
252  * but should be marked as unavailable for arm macOS devices.
253  * XNU:                         nothing (API is still available)
254  * 1st party kexts:             __deprecated
255  * 3rd party kexts macOS x86:   __deprecated
256  * 3rd party kexts macOS arm:   __unavailable
257  */
258 #define __kpi_deprecated_arm64_macos_unavailable
259 #elif !KERNEL || !XNU_PLATFORM_MacOSX
260 #define __kpi_deprecated_arm64_macos_unavailable
261 #elif KERNEL_PRIVATE
262 #define __kpi_deprecated_arm64_macos_unavailable __deprecated
263 #elif defined(__arm64__)
264 #define __kpi_deprecated_arm64_macos_unavailable __unavailable
265 #else
266 #define __kpi_deprecated_arm64_macos_unavailable __deprecated
267 #endif /* XNU_KERNEL_PRIVATE */
268 
269 /* Delete pseudo-keywords wherever they are not available or needed. */
270 #ifndef __dead
271 #define __dead
272 #define __pure
273 #endif
274 
275 /*
276  * We use `__restrict' as a way to define the `restrict' type qualifier
277  * without disturbing older software that is unaware of C99 keywords.
278  */
279 #if __STDC_VERSION__ < 199901
280 #define __restrict
281 #else
282 #define __restrict      restrict
283 #endif
284 
285 /* Compatibility with compilers and environments that don't support the
286  * nullability feature.
287  */
288 
289 #if !__has_feature(nullability)
290 #ifndef __nullable
291 #define __nullable
292 #endif
293 #ifndef __nonnull
294 #define __nonnull
295 #endif
296 #ifndef __null_unspecified
297 #define __null_unspecified
298 #endif
299 #ifndef _Nullable
300 #define _Nullable
301 #endif
302 #ifndef _Nonnull
303 #define _Nonnull
304 #endif
305 #ifndef _Null_unspecified
306 #define _Null_unspecified
307 #endif
308 #endif
309 
310 /*
311  * __disable_tail_calls causes the compiler to not perform tail call
312  * optimization inside the marked function.
313  */
314 #if __has_attribute(disable_tail_calls)
315 #define __disable_tail_calls    __attribute__((__disable_tail_calls__))
316 #else
317 #define __disable_tail_calls
318 #endif
319 
320 /*
321  * __not_tail_called causes the compiler to prevent tail call optimization
322  * on statically bound calls to the function.  It has no effect on indirect
323  * calls.  Virtual functions, objective-c methods, and functions marked as
324  * "always_inline" cannot be marked as __not_tail_called.
325  */
326 #if __has_attribute(not_tail_called)
327 #define __not_tail_called       __attribute__((__not_tail_called__))
328 #else
329 #define __not_tail_called
330 #endif
331 
332 /*
333  * __result_use_check warns callers of a function that not using the function
334  * return value is a bug, i.e. dismissing malloc() return value results in a
335  * memory leak.
336  */
337 #if __has_attribute(warn_unused_result)
338 #define __result_use_check __attribute__((__warn_unused_result__))
339 #else
340 #define __result_use_check
341 #endif
342 
343 /*
344  * __swift_unavailable causes the compiler to mark a symbol as specifically
345  * unavailable in Swift, regardless of any other availability in C.
346  */
347 #if __has_feature(attribute_availability_swift)
348 #define __swift_unavailable(_msg)       __attribute__((__availability__(swift, unavailable, message=_msg)))
349 #else
350 #define __swift_unavailable(_msg)
351 #endif
352 
353 /*
354  * Attributes to support Swift concurrency.
355  */
356 #if __has_attribute(__swift_attr__)
357 #define __swift_unavailable_from_async(_msg)    __attribute__((__swift_attr__("@_unavailableFromAsync(message: \"" _msg "\")")))
358 #define __swift_nonisolated                     __attribute__((__swift_attr__("nonisolated")))
359 #define __swift_nonisolated_unsafe              __attribute__((__swift_attr__("nonisolated(unsafe)")))
360 #else
361 #define __swift_unavailable_from_async(_msg)
362 #define __swift_nonisolated
363 #define __swift_nonisolated_unsafe
364 #endif
365 
366 /*
367  * __abortlike is the attribute to put on functions like abort() that are
368  * typically used to mark assertions. These optimize the codegen
369  * for outlining while still maintaining debugability.
370  */
371 #ifndef __abortlike
372 #define __abortlike __dead2 __cold __not_tail_called
373 #endif
374 
375 /* Declaring inline functions within headers is error-prone due to differences
376  * across various versions of the C language and extensions.  __header_inline
377  * can be used to declare inline functions within system headers.  In cases
378  * where you want to force inlining instead of letting the compiler make
379  * the decision, you can use __header_always_inline.
380  *
381  * Be aware that using inline for functions which compilers may also provide
382  * builtins can behave differently under various compilers.  If you intend to
383  * provide an inline version of such a function, you may want to use a macro
384  * instead.
385  *
386  * The check for !__GNUC__ || __clang__ is because gcc doesn't correctly
387  * support c99 inline in some cases:
388  * http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55965
389  */
390 
391 #if defined(__cplusplus) || \
392         (__STDC_VERSION__ >= 199901L && \
393         !defined(__GNUC_GNU_INLINE__) && \
394         (!defined(__GNUC__) || defined(__clang__)))
395 # define __header_inline           inline
396 #elif defined(__GNUC__) && defined(__GNUC_STDC_INLINE__)
397 # define __header_inline           extern __inline __attribute__((__gnu_inline__))
398 #elif defined(__GNUC__)
399 # define __header_inline           extern __inline
400 #else
401 /* If we land here, we've encountered an unsupported compiler,
402  * so hopefully it understands static __inline as a fallback.
403  */
404 # define __header_inline           static __inline
405 #endif
406 
407 #ifdef __GNUC__
408 # define __header_always_inline    __header_inline __attribute__ ((__always_inline__))
409 #else
410 /* Unfortunately, we're using a compiler that we don't know how to force to
411  * inline.  Oh well.
412  */
413 # define __header_always_inline    __header_inline
414 #endif
415 
416 /*
417  * Compiler-dependent macros that bracket portions of code where the
418  * "-Wunreachable-code" warning should be ignored. Please use sparingly.
419  */
420 #if defined(__clang__)
421 # define __unreachable_ok_push \
422 	 _Pragma("clang diagnostic push") \
423 	 _Pragma("clang diagnostic ignored \"-Wunreachable-code\"")
424 # define __unreachable_ok_pop \
425 	 _Pragma("clang diagnostic pop")
426 #elif defined(__GNUC__) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
427 # define __unreachable_ok_push \
428 	 _Pragma("GCC diagnostic push") \
429 	 _Pragma("GCC diagnostic ignored \"-Wunreachable-code\"")
430 # define __unreachable_ok_pop \
431 	 _Pragma("GCC diagnostic pop")
432 #else
433 # define __unreachable_ok_push
434 # define __unreachable_ok_pop
435 #endif
436 
437 /*
438  * Compiler-dependent macros to declare that functions take printf-like
439  * or scanf-like arguments.  They are null except for versions of gcc
440  * that are known to support the features properly.  Functions declared
441  * with these attributes will cause compilation warnings if there is a
442  * mismatch between the format string and subsequent function parameter
443  * types.
444  */
445 #define __printflike(fmtarg, firstvararg) \
446 	__attribute__((__format__ (__printf__, fmtarg, firstvararg)))
447 #define __printf0like(fmtarg, firstvararg) \
448 	__attribute__((__format__ (__printf0__, fmtarg, firstvararg)))
449 #define __scanflike(fmtarg, firstvararg) \
450 	__attribute__((__format__ (__scanf__, fmtarg, firstvararg)))
451 #define __osloglike(fmtarg, firstvararg) \
452 	__attribute__((__format__ (__os_log__, fmtarg, firstvararg)))
453 
454 #define __IDSTRING(name, string) static const char name[] __used = string
455 
456 #ifndef __COPYRIGHT
457 #define __COPYRIGHT(s) __IDSTRING(copyright,s)
458 #endif
459 
460 #ifndef __RCSID
461 #define __RCSID(s) __IDSTRING(rcsid,s)
462 #endif
463 
464 #ifndef __SCCSID
465 #define __SCCSID(s) __IDSTRING(sccsid,s)
466 #endif
467 
468 #ifndef __PROJECT_VERSION
469 #define __PROJECT_VERSION(s) __IDSTRING(project_version,s)
470 #endif
471 
472 /* Source compatibility only, ID string not emitted in object file */
473 #ifndef __FBSDID
474 #define __FBSDID(s)
475 #endif
476 
477 #ifndef __DECONST
478 #define __DECONST(type, var)    __CAST_AWAY_QUALIFIER(var, const, type)
479 #endif
480 
481 #ifndef __DEVOLATILE
482 #define __DEVOLATILE(type, var) __CAST_AWAY_QUALIFIER(var, volatile, type)
483 #endif
484 
485 #ifndef __DEQUALIFY
486 #define __DEQUALIFY(type, var)  __CAST_AWAY_QUALIFIER(var, const volatile, type)
487 #endif
488 
489 /*
490  * __alloc_align can be used to label function arguments that represent the
491  * alignment of the returned pointer.
492  */
493 #ifndef __alloc_align
494 #if __has_attribute(alloc_align)
495 #define __alloc_align(n) __attribute__((alloc_align(n)))
496 #else
497 #define __alloc_align(n)
498 #endif
499 #endif // __alloc_align
500 
501 /*
502  * __alloc_size can be used to label function arguments that represent the
503  * size of memory that the function allocates and returns. The one-argument
504  * form labels a single argument that gives the allocation size (where the
505  * arguments are numbered from 1):
506  *
507  * void	*malloc(size_t __size) __alloc_size(1);
508  *
509  * The two-argument form handles the case where the size is calculated as the
510  * product of two arguments:
511  *
512  * void	*calloc(size_t __count, size_t __size) __alloc_size(1,2);
513  */
514 #ifndef __alloc_size
515 #if __has_attribute(alloc_size)
516 #define __alloc_size(...) __attribute__((alloc_size(__VA_ARGS__)))
517 #else
518 #define __alloc_size(...)
519 #endif
520 #endif // __alloc_size
521 
522 /*
523  * Facilities below assist adoption of -Wunsafe-buffer-usage, an off-by-default
524  * Clang compiler warning that helps the developer minimize unsafe, raw
525  * buffer manipulation in the code that may lead to buffer overflow
526  * vulnerabilities.
527  *
528  * They are primarily designed for modern C++ code where -Wunsafe-buffer-usage
529  * comes with automatic fix-it hints that help the developer transform
530  * their code to use modern C++ containers, which may be made bounds-safe by
531  * linking against a version of the C++ standard library that offers
532  * bounds-checked containers.
533  * They can be used in plain C, but -fbounds-safety is the preferred solution
534  * for plain C (see also <ptrcheck.h>).
535  *
536  * Attribute __unsafe_buffer_usage can be used to label functions that should be
537  * avoided as they may perform or otherwise introduce unsafe buffer
538  * manipulation operations.
539  *
540  * Calls to such functions are flagged by -Wunsafe-buffer-usage, similarly to
541  * how unchecked buffer manipulation operations are flagged when observed
542  * by the compiler directly:
543  *
544  *   // An unsafe function that needs to be avoided.
545  *   __unsafe_buffer_usage
546  *   void foo(int *buf, size_t size);
547  *
548  *   // A safe alternative to foo().
549  *   void foo(std::span<int> buf);
550  *
551  *   void bar(size_t idx) {
552  *       int array[5];
553  *
554  *       // Direct unsafe buffer manipulation through subscript operator:
555  *       array[idx] = 3;  // warning [-Wunsafe-buffer-usage]
556  *       // Unsafe buffer manipulation through function foo():
557  *       foo(array, 5);   // warning [-Wunsafe-buffer-usage]
558  *       // Checked buffer manipulation, with bounds information automatically
559  *       // preserved for the purposes of runtime checks in standard library:
560  *       foo(array);      // no warning
561  *   }
562  *
563  * While annotating a function as __unsafe_buffer_usage has an effect similar
564  * to annotating it as __deprecated, the __unsafe_buffer_usage attribute
565  * should be used whenever the resulting warning needs to be controlled
566  * by the -Wunsafe-buffer-usage flag (which is turned off in codebases that
567  * don't attempt to achieve bounds safety this way) as opposed to -Wdeprecated
568  * (enabled in most codebases).
569  *
570  * The attribute does NOT suppress -Wunsafe-buffer-usage warnings inside
571  * the function's body; it simply introduces new warnings at each call site
572  * to help the developers avoid the function entirely. Most of the time
573  * it does not make sense to annotate a function as __unsafe_buffer_usage
574  * without providing the users with a safe alternative.
575  *
576  * Pragmas __unsafe_buffer_usage_begin and __unsafe_buffer_usage_end
577  * annotate a range of code as intentionally containing unsafe buffer
578  * operations. They suppress -Wunsafe-buffer-usage warnings
579  * for unsafe operations in range:
580  *
581  *   __unsafe_buffer_usage_begin
582  *   array[idx] = 3; // warning suppressed
583  *   foo(array, 5);  // warning suppressed
584  *   __unsafe_buffer_usage_end
585  *
586  * These pragmas are NOT a way to mass-annotate functions with the attribute
587  * __unsafe_buffer_usage. Functions declared within the pragma range
588  * do NOT get annotated automatically. In some rare situations it makes sense
589  * to do all three: put the attribute on the function, put pragmas inside
590  * the body of the function, and put pragmas around some call sites.
591  */
592 #if __has_cpp_attribute(clang::unsafe_buffer_usage)
593 #define __has_safe_buffers 1
594 #define __unsafe_buffer_usage [[clang::unsafe_buffer_usage]]
595 #elif __has_attribute(unsafe_buffer_usage)
596 #define __has_safe_buffers 1
597 #define __unsafe_buffer_usage __attribute__((__unsafe_buffer_usage__))
598 #else
599 #define __has_safe_buffers 0
600 #define __unsafe_buffer_usage
601 #endif
602 #if __has_safe_buffers
603 #define __unsafe_buffer_usage_begin _Pragma("clang unsafe_buffer_usage begin")
604 #define __unsafe_buffer_usage_end   _Pragma("clang unsafe_buffer_usage end")
605 #else
606 #define __unsafe_buffer_usage_begin
607 #define __unsafe_buffer_usage_end
608 #endif
609 
610 /*
611  * COMPILATION ENVIRONMENTS -- see compat(5) for additional detail
612  *
613  * DEFAULT	By default newly complied code will get POSIX APIs plus
614  *		Apple API extensions in scope.
615  *
616  *		Most users will use this compilation environment to avoid
617  *		behavioral differences between 32 and 64 bit code.
618  *
619  * LEGACY	Defining _NONSTD_SOURCE will get pre-POSIX APIs plus Apple
620  *		API extensions in scope.
621  *
622  *		This is generally equivalent to the Tiger release compilation
623  *		environment, except that it cannot be applied to 64 bit code;
624  *		its use is discouraged.
625  *
626  *		We expect this environment to be deprecated in the future.
627  *
628  * STRICT	Defining _POSIX_C_SOURCE or _XOPEN_SOURCE restricts the
629  *		available APIs to exactly the set of APIs defined by the
630  *		corresponding standard, based on the value defined.
631  *
632  *		A correct, portable definition for _POSIX_C_SOURCE is 200112L.
633  *		A correct, portable definition for _XOPEN_SOURCE is 600L.
634  *
635  *		Apple API extensions are not visible in this environment,
636  *		which can cause Apple specific code to fail to compile,
637  *		or behave incorrectly if prototypes are not in scope or
638  *		warnings about missing prototypes are not enabled or ignored.
639  *
640  * In any compilation environment, for correct symbol resolution to occur,
641  * function prototypes must be in scope.  It is recommended that all Apple
642  * tools users add either the "-Wall" or "-Wimplicit-function-declaration"
643  * compiler flags to their projects to be warned when a function is being
644  * used without a prototype in scope.
645  */
646 
647 /* These settings are particular to each product. */
648 #ifdef KERNEL
649 #define __DARWIN_ONLY_64_BIT_INO_T      0
650 #define __DARWIN_ONLY_UNIX_CONFORMANCE  0
651 #define __DARWIN_ONLY_VERS_1050         0
652 #if defined(__x86_64__)
653 #define __DARWIN_SUF_DARWIN14   "_darwin14"
654 #define __DARWIN14_ALIAS(sym)   __asm("_" __STRING(sym) __DARWIN_SUF_DARWIN14)
655 #else
656 #define __DARWIN14_ALIAS(sym)
657 #endif
658 #else /* !KERNEL */
659 #ifdef XNU_PLATFORM_iPhoneOS
660 /* Platform: iPhoneOS */
661 #define __DARWIN_ONLY_64_BIT_INO_T      1
662 #define __DARWIN_ONLY_UNIX_CONFORMANCE  1
663 #define __DARWIN_ONLY_VERS_1050         1
664 #endif /* XNU_PLATFORM_iPhoneOS */
665 #ifdef XNU_PLATFORM_iPhoneSimulator
666 /* Platform: iPhoneSimulator */
667 #define __DARWIN_ONLY_64_BIT_INO_T      1
668 #define __DARWIN_ONLY_UNIX_CONFORMANCE  1
669 #define __DARWIN_ONLY_VERS_1050         1
670 #endif /* XNU_PLATFORM_iPhoneSimulator */
671 #ifdef XNU_PLATFORM_tvOS
672 /* Platform: tvOS */
673 #define __DARWIN_ONLY_64_BIT_INO_T      1
674 #define __DARWIN_ONLY_UNIX_CONFORMANCE  1
675 #define __DARWIN_ONLY_VERS_1050         1
676 #endif /* XNU_PLATFORM_tvOS */
677 #ifdef XNU_PLATFORM_AppleTVOS
678 /* Platform: AppleTVOS */
679 #define __DARWIN_ONLY_64_BIT_INO_T      1
680 #define __DARWIN_ONLY_UNIX_CONFORMANCE  1
681 #define __DARWIN_ONLY_VERS_1050         1
682 #endif /* XNU_PLATFORM_AppleTVOS */
683 #ifdef XNU_PLATFORM_tvSimulator
684 /* Platform: tvSimulator */
685 #define __DARWIN_ONLY_64_BIT_INO_T      1
686 #define __DARWIN_ONLY_UNIX_CONFORMANCE  1
687 #define __DARWIN_ONLY_VERS_1050         1
688 #endif /* XNU_PLATFORM_tvSimulator */
689 #ifdef XNU_PLATFORM_AppleTVSimulator
690 /* Platform: AppleTVSimulator */
691 #define __DARWIN_ONLY_64_BIT_INO_T      1
692 #define __DARWIN_ONLY_UNIX_CONFORMANCE  1
693 #define __DARWIN_ONLY_VERS_1050         1
694 #endif /* XNU_PLATFORM_AppleTVSimulator */
695 #ifdef XNU_PLATFORM_iPhoneOSNano
696 /* Platform: iPhoneOSNano */
697 #define __DARWIN_ONLY_64_BIT_INO_T      1
698 #define __DARWIN_ONLY_UNIX_CONFORMANCE  1
699 #define __DARWIN_ONLY_VERS_1050         1
700 #endif /* XNU_PLATFORM_iPhoneOSNano */
701 #ifdef XNU_PLATFORM_iPhoneNanoSimulator
702 /* Platform: iPhoneNanoSimulator */
703 #define __DARWIN_ONLY_64_BIT_INO_T      1
704 #define __DARWIN_ONLY_UNIX_CONFORMANCE  1
705 #define __DARWIN_ONLY_VERS_1050         1
706 #endif /* XNU_PLATFORM_iPhoneNanoSimulator */
707 #ifdef XNU_PLATFORM_WatchOS
708 /* Platform: WatchOS */
709 #define __DARWIN_ONLY_64_BIT_INO_T      1
710 #define __DARWIN_ONLY_UNIX_CONFORMANCE  1
711 #define __DARWIN_ONLY_VERS_1050         1
712 #endif /* XNU_PLATFORM_WatchOS */
713 #ifdef XNU_PLATFORM_WatchSimulator
714 /* Platform: WatchSimulator */
715 #define __DARWIN_ONLY_64_BIT_INO_T      1
716 #define __DARWIN_ONLY_UNIX_CONFORMANCE  1
717 #define __DARWIN_ONLY_VERS_1050         1
718 #endif /* XNU_PLATFORM_WatchSimulator */
719 #ifdef XNU_PLATFORM_BridgeOS
720 /* Platform: BridgeOS */
721 #define __DARWIN_ONLY_64_BIT_INO_T      1
722 #define __DARWIN_ONLY_UNIX_CONFORMANCE  1
723 #define __DARWIN_ONLY_VERS_1050         1
724 #endif /* XNU_PLATFORM_BridgeOS */
725 #ifdef XNU_PLATFORM_DriverKit
726 /* Platform: DriverKit */
727 #define __DARWIN_ONLY_64_BIT_INO_T      1
728 #define __DARWIN_ONLY_UNIX_CONFORMANCE  1
729 #define __DARWIN_ONLY_VERS_1050         1
730 #endif /* XNU_PLATFORM_DriverKit */
731 #ifdef XNU_PLATFORM_MacOSX
732 /* Platform: MacOSX */
733 #if defined(__i386__)
734 #define __DARWIN_ONLY_64_BIT_INO_T      0
735 #define __DARWIN_ONLY_UNIX_CONFORMANCE  0
736 #define __DARWIN_ONLY_VERS_1050         0
737 #elif defined(__x86_64__)
738 #define __DARWIN_ONLY_64_BIT_INO_T      0
739 #define __DARWIN_ONLY_UNIX_CONFORMANCE  1
740 #define __DARWIN_ONLY_VERS_1050         0
741 #else
742 #define __DARWIN_ONLY_64_BIT_INO_T      1
743 #define __DARWIN_ONLY_UNIX_CONFORMANCE  1
744 #define __DARWIN_ONLY_VERS_1050         1
745 #endif
746 #endif /* XNU_PLATFORM_MacOSX */
747 #ifdef XNU_PLATFORM_XROS
748 /* Platform: XROS */
749 #define __DARWIN_ONLY_64_BIT_INO_T      1
750 #define __DARWIN_ONLY_UNIX_CONFORMANCE  1
751 #define __DARWIN_ONLY_VERS_1050         1
752 #endif /* XNU_PLATFORM_XROS */
753 #ifdef XNU_PLATFORM_XRSimulator
754 /* Platform: XRSimulator */
755 #define __DARWIN_ONLY_64_BIT_INO_T      1
756 #define __DARWIN_ONLY_UNIX_CONFORMANCE  1
757 #define __DARWIN_ONLY_VERS_1050         1
758 #endif /* XNU_PLATFORM_XRSimulator */
759 #endif /* KERNEL */
760 
761 /*
762  * The __DARWIN_ALIAS macros are used to do symbol renaming; they allow
763  * legacy code to use the old symbol, thus maintaining binary compatibility
764  * while new code can use a standards compliant version of the same function.
765  *
766  * __DARWIN_ALIAS is used by itself if the function signature has not
767  * changed, it is used along with a #ifdef check for __DARWIN_UNIX03
768  * if the signature has changed.  Because the __LP64__ environment
769  * only supports UNIX03 semantics it causes __DARWIN_UNIX03 to be
770  * defined, but causes __DARWIN_ALIAS to do no symbol mangling.
771  *
772  * As a special case, when XCode is used to target a specific version of the
773  * OS, the manifest constant __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__
774  * will be defined by the compiler, with the digits representing major version
775  * time 100 + minor version times 10 (e.g. 10.5 := 1050).  If we are targeting
776  * pre-10.5, and it is the default compilation environment, revert the
777  * compilation environment to pre-__DARWIN_UNIX03.
778  */
779 #if !defined(__DARWIN_UNIX03)
780 #  if defined(KERNEL)
781 #    define __DARWIN_UNIX03     0
782 #  elif __DARWIN_ONLY_UNIX_CONFORMANCE
783 #    if defined(_NONSTD_SOURCE)
784 #      error "Can't define _NONSTD_SOURCE when only UNIX conformance is available."
785 #    endif /* _NONSTD_SOURCE */
786 #    define __DARWIN_UNIX03     1
787 #  elif defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && ((__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ - 0) < 1040)
788 #    define __DARWIN_UNIX03     0
789 #  elif defined(_DARWIN_C_SOURCE) || defined(_XOPEN_SOURCE) || defined(_POSIX_C_SOURCE)
790 #    if defined(_NONSTD_SOURCE)
791 #      error "Can't define both _NONSTD_SOURCE and any of _DARWIN_C_SOURCE, _XOPEN_SOURCE or _POSIX_C_SOURCE."
792 #    endif /* _NONSTD_SOURCE */
793 #    define __DARWIN_UNIX03     1
794 #  elif defined(_NONSTD_SOURCE)
795 #    define __DARWIN_UNIX03     0
796 #  else /* default */
797 #    if defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && ((__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ - 0) < 1050)
798 #      define __DARWIN_UNIX03   0
799 #    else /* __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1050 */
800 #      define __DARWIN_UNIX03   1
801 #    endif /* __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1050 */
802 #  endif /* _DARWIN_C_SOURCE || _XOPEN_SOURCE || _POSIX_C_SOURCE || __LP64__ */
803 #endif /* !__DARWIN_UNIX03 */
804 
805 #if !defined(__DARWIN_64_BIT_INO_T)
806 #  if defined(KERNEL)
807 #    define __DARWIN_64_BIT_INO_T 0
808 #  elif defined(_DARWIN_USE_64_BIT_INODE)
809 #    if defined(_DARWIN_NO_64_BIT_INODE)
810 #      error "Can't define both _DARWIN_USE_64_BIT_INODE and _DARWIN_NO_64_BIT_INODE."
811 #    endif /* _DARWIN_NO_64_BIT_INODE */
812 #    define __DARWIN_64_BIT_INO_T 1
813 #  elif defined(_DARWIN_NO_64_BIT_INODE)
814 #    if __DARWIN_ONLY_64_BIT_INO_T
815 #      error "Can't define _DARWIN_NO_64_BIT_INODE when only 64-bit inodes are available."
816 #    endif /* __DARWIN_ONLY_64_BIT_INO_T */
817 #    define __DARWIN_64_BIT_INO_T 0
818 #  else /* default */
819 #    if __DARWIN_ONLY_64_BIT_INO_T
820 #      define __DARWIN_64_BIT_INO_T 1
821 #    elif defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && ((__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ - 0) < 1060) || __DARWIN_UNIX03 == 0
822 #      define __DARWIN_64_BIT_INO_T 0
823 #    else /* default */
824 #      define __DARWIN_64_BIT_INO_T 1
825 #    endif /* __DARWIN_ONLY_64_BIT_INO_T */
826 #  endif
827 #endif /* !__DARWIN_64_BIT_INO_T */
828 
829 #if !defined(__DARWIN_VERS_1050)
830 #  if defined(KERNEL)
831 #    define __DARWIN_VERS_1050 0
832 #  elif __DARWIN_ONLY_VERS_1050
833 #    define __DARWIN_VERS_1050 1
834 #  elif defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && ((__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ - 0) < 1050) || __DARWIN_UNIX03 == 0
835 #    define __DARWIN_VERS_1050 0
836 #  else /* default */
837 #    define __DARWIN_VERS_1050 1
838 #  endif
839 #endif /* !__DARWIN_VERS_1050 */
840 
841 #if !defined(__DARWIN_NON_CANCELABLE)
842 #  if defined(KERNEL)
843 #    define __DARWIN_NON_CANCELABLE 0
844 #  else /* default */
845 #    define __DARWIN_NON_CANCELABLE 0
846 #  endif
847 #endif /* !__DARWIN_NON_CANCELABLE */
848 
849 /*
850  * symbol suffixes used for symbol versioning
851  */
852 #if __DARWIN_UNIX03
853 #  if __DARWIN_ONLY_UNIX_CONFORMANCE
854 #    define __DARWIN_SUF_UNIX03         /* nothing */
855 #  else /* !__DARWIN_ONLY_UNIX_CONFORMANCE */
856 #    define __DARWIN_SUF_UNIX03         "$UNIX2003"
857 #  endif /* __DARWIN_ONLY_UNIX_CONFORMANCE */
858 
859 #  if __DARWIN_64_BIT_INO_T
860 #    if __DARWIN_ONLY_64_BIT_INO_T
861 #      define __DARWIN_SUF_64_BIT_INO_T /* nothing */
862 #    else /* !__DARWIN_ONLY_64_BIT_INO_T */
863 #      define __DARWIN_SUF_64_BIT_INO_T "$INODE64"
864 #    endif /* __DARWIN_ONLY_64_BIT_INO_T */
865 #  else /* !__DARWIN_64_BIT_INO_T */
866 #    define __DARWIN_SUF_64_BIT_INO_T   /* nothing */
867 #  endif /* __DARWIN_64_BIT_INO_T */
868 
869 #  if __DARWIN_VERS_1050
870 #    if __DARWIN_ONLY_VERS_1050
871 #      define __DARWIN_SUF_1050         /* nothing */
872 #    else /* !__DARWIN_ONLY_VERS_1050 */
873 #      define __DARWIN_SUF_1050         "$1050"
874 #    endif /* __DARWIN_ONLY_VERS_1050 */
875 #  else /* !__DARWIN_VERS_1050 */
876 #    define __DARWIN_SUF_1050           /* nothing */
877 #  endif /* __DARWIN_VERS_1050 */
878 
879 #  if __DARWIN_NON_CANCELABLE
880 #    define __DARWIN_SUF_NON_CANCELABLE "$NOCANCEL"
881 #  else /* !__DARWIN_NON_CANCELABLE */
882 #    define __DARWIN_SUF_NON_CANCELABLE /* nothing */
883 #  endif /* __DARWIN_NON_CANCELABLE */
884 
885 #else /* !__DARWIN_UNIX03 */
886 #  define __DARWIN_SUF_UNIX03           /* nothing */
887 #  define __DARWIN_SUF_64_BIT_INO_T     /* nothing */
888 #  define __DARWIN_SUF_NON_CANCELABLE   /* nothing */
889 #  define __DARWIN_SUF_1050             /* nothing */
890 #endif /* __DARWIN_UNIX03 */
891 
892 #define __DARWIN_SUF_EXTSN              "$DARWIN_EXTSN"
893 
894 /*
895  * symbol versioning macros
896  */
897 #define __DARWIN_ALIAS(sym)             __asm("_" __STRING(sym) __DARWIN_SUF_UNIX03)
898 #define __DARWIN_ALIAS_C(sym)           __asm("_" __STRING(sym) __DARWIN_SUF_NON_CANCELABLE __DARWIN_SUF_UNIX03)
899 #define __DARWIN_ALIAS_I(sym)           __asm("_" __STRING(sym) __DARWIN_SUF_64_BIT_INO_T __DARWIN_SUF_UNIX03)
900 #define __DARWIN_NOCANCEL(sym)          __asm("_" __STRING(sym) __DARWIN_SUF_NON_CANCELABLE)
901 #define __DARWIN_INODE64(sym)           __asm("_" __STRING(sym) __DARWIN_SUF_64_BIT_INO_T)
902 
903 #define __DARWIN_1050(sym)              __asm("_" __STRING(sym) __DARWIN_SUF_1050)
904 #define __DARWIN_1050ALIAS(sym)         __asm("_" __STRING(sym) __DARWIN_SUF_1050 __DARWIN_SUF_UNIX03)
905 #define __DARWIN_1050ALIAS_C(sym)       __asm("_" __STRING(sym) __DARWIN_SUF_1050 __DARWIN_SUF_NON_CANCELABLE __DARWIN_SUF_UNIX03)
906 #define __DARWIN_1050ALIAS_I(sym)       __asm("_" __STRING(sym) __DARWIN_SUF_1050 __DARWIN_SUF_64_BIT_INO_T __DARWIN_SUF_UNIX03)
907 #define __DARWIN_1050INODE64(sym)       __asm("_" __STRING(sym) __DARWIN_SUF_1050 __DARWIN_SUF_64_BIT_INO_T)
908 
909 #define __DARWIN_EXTSN(sym)             __asm("_" __STRING(sym) __DARWIN_SUF_EXTSN)
910 #define __DARWIN_EXTSN_C(sym)           __asm("_" __STRING(sym) __DARWIN_SUF_EXTSN __DARWIN_SUF_NON_CANCELABLE)
911 #if XNU_KERNEL_PRIVATE
912 #define __XNU_INTERNAL(sym)             __asm("_" __STRING(sym) "$XNU_INTERNAL") __attribute__((used))
913 #endif
914 
915 /*
916  * symbol release macros
917  */
918 #ifdef KERNEL
919 #define __DARWIN_ALIAS_STARTING(_mac, _iphone, x)
920 #else
921 #include <sys/_symbol_aliasing.h>
922 
923 #if defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__)
924 #define __DARWIN_ALIAS_STARTING(_mac, _iphone, x)   __DARWIN_ALIAS_STARTING_IPHONE_##_iphone(x)
925 #elif defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__)
926 #define __DARWIN_ALIAS_STARTING(_mac, _iphone, x)   __DARWIN_ALIAS_STARTING_MAC_##_mac(x)
927 #else
928 #define __DARWIN_ALIAS_STARTING(_mac, _iphone, x)   x
929 #endif
930 #endif /* KERNEL */
931 
932 
933 /*
934  * POSIX.1 requires that the macros we test be defined before any standard
935  * header file is included.  This permits us to convert values for feature
936  * testing, as necessary, using only _POSIX_C_SOURCE.
937  *
938  * Here's a quick run-down of the versions:
939  *  defined(_POSIX_SOURCE)		1003.1-1988
940  *  _POSIX_C_SOURCE == 1L		1003.1-1990
941  *  _POSIX_C_SOURCE == 2L		1003.2-1992 C Language Binding Option
942  *  _POSIX_C_SOURCE == 199309L		1003.1b-1993
943  *  _POSIX_C_SOURCE == 199506L		1003.1c-1995, 1003.1i-1995,
944  *					and the omnibus ISO/IEC 9945-1: 1996
945  *  _POSIX_C_SOURCE == 200112L		1003.1-2001
946  *  _POSIX_C_SOURCE == 200809L		1003.1-2008
947  *
948  * In addition, the X/Open Portability Guide, which is now the Single UNIX
949  * Specification, defines a feature-test macro which indicates the version of
950  * that specification, and which subsumes _POSIX_C_SOURCE.
951  */
952 
953 /* Deal with IEEE Std. 1003.1-1990, in which _POSIX_C_SOURCE == 1L. */
954 #if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE == 1L
955 #undef _POSIX_C_SOURCE
956 #define _POSIX_C_SOURCE         199009L
957 #endif
958 
959 /* Deal with IEEE Std. 1003.2-1992, in which _POSIX_C_SOURCE == 2L. */
960 #if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE == 2L
961 #undef _POSIX_C_SOURCE
962 #define _POSIX_C_SOURCE         199209L
963 #endif
964 
965 /* Deal with various X/Open Portability Guides and Single UNIX Spec. */
966 #ifdef _XOPEN_SOURCE
967 #if _XOPEN_SOURCE - 0L >= 700L && (!defined(_POSIX_C_SOURCE) || _POSIX_C_SOURCE - 0L < 200809L)
968 #undef _POSIX_C_SOURCE
969 #define _POSIX_C_SOURCE         200809L
970 #elif _XOPEN_SOURCE - 0L >= 600L && (!defined(_POSIX_C_SOURCE) || _POSIX_C_SOURCE - 0L < 200112L)
971 #undef _POSIX_C_SOURCE
972 #define _POSIX_C_SOURCE         200112L
973 #elif _XOPEN_SOURCE - 0L >= 500L && (!defined(_POSIX_C_SOURCE) || _POSIX_C_SOURCE - 0L < 199506L)
974 #undef _POSIX_C_SOURCE
975 #define _POSIX_C_SOURCE         199506L
976 #endif
977 #endif
978 
979 /*
980  * Deal with all versions of POSIX.  The ordering relative to the tests above is
981  * important.
982  */
983 #if defined(_POSIX_SOURCE) && !defined(_POSIX_C_SOURCE)
984 #define _POSIX_C_SOURCE         198808L
985 #endif
986 
987 /* POSIX C deprecation macros */
988 #ifdef KERNEL
989 #define __POSIX_C_DEPRECATED(ver)
990 #else
991 #include <sys/_posix_availability.h>
992 
993 #define __POSIX_C_DEPRECATED(ver) ___POSIX_C_DEPRECATED_STARTING_##ver
994 #endif
995 
996 /*
997  * Set a single macro which will always be defined and can be used to determine
998  * the appropriate namespace.  For POSIX, these values will correspond to
999  * _POSIX_C_SOURCE value.  Currently there are two additional levels corresponding
1000  * to ANSI (_ANSI_SOURCE) and Darwin extensions (_DARWIN_C_SOURCE)
1001  */
1002 #define __DARWIN_C_ANSI         010000L
1003 #define __DARWIN_C_FULL         900000L
1004 
1005 #if   defined(_ANSI_SOURCE)
1006 #define __DARWIN_C_LEVEL        __DARWIN_C_ANSI
1007 #elif defined(_POSIX_C_SOURCE) && !defined(_DARWIN_C_SOURCE) && !defined(_NONSTD_SOURCE)
1008 #define __DARWIN_C_LEVEL        _POSIX_C_SOURCE
1009 #else
1010 #define __DARWIN_C_LEVEL        __DARWIN_C_FULL
1011 #endif
1012 
1013 /* If the developer has neither requested a strict language mode nor a version
1014  * of POSIX, turn on functionality provided by __STDC_WANT_LIB_EXT1__ as part
1015  * of __DARWIN_C_FULL.
1016  */
1017 #if !defined(__STDC_WANT_LIB_EXT1__) && !defined(__STRICT_ANSI__) && __DARWIN_C_LEVEL >= __DARWIN_C_FULL
1018 #define __STDC_WANT_LIB_EXT1__ 1
1019 #endif
1020 
1021 /*
1022  * long long is not supported in c89 (__STRICT_ANSI__), but g++ -ansi and
1023  * c99 still want long longs.  While not perfect, we allow long longs for
1024  * g++.
1025  */
1026 #if (defined(__STRICT_ANSI__) && (__STDC_VERSION__ - 0 < 199901L) && !defined(__GNUG__))
1027 #define __DARWIN_NO_LONG_LONG 1
1028 #else
1029 #define __DARWIN_NO_LONG_LONG 0
1030 #endif
1031 
1032 /*****************************************
1033 *  Public darwin-specific feature macros
1034 *****************************************/
1035 
1036 /*
1037  * _DARWIN_FEATURE_64_BIT_INODE indicates that the ino_t type is 64-bit, and
1038  * structures modified for 64-bit inodes (like struct stat) will be used.
1039  */
1040 #if __DARWIN_64_BIT_INO_T
1041 #define _DARWIN_FEATURE_64_BIT_INODE            1
1042 #endif
1043 
1044 /*
1045  * _DARWIN_FEATURE_64_ONLY_BIT_INODE indicates that the ino_t type may only
1046  * be 64-bit; there is no support for 32-bit ino_t when this macro is defined
1047  * (and non-zero).  There is no struct stat64 either, as the regular
1048  * struct stat will already be the 64-bit version.
1049  */
1050 #if __DARWIN_ONLY_64_BIT_INO_T
1051 #define _DARWIN_FEATURE_ONLY_64_BIT_INODE       1
1052 #endif
1053 
1054 /*
1055  * _DARWIN_FEATURE_ONLY_VERS_1050 indicates that only those APIs updated
1056  * in 10.5 exists; no pre-10.5 variants are available.
1057  */
1058 #if __DARWIN_ONLY_VERS_1050
1059 #define _DARWIN_FEATURE_ONLY_VERS_1050          1
1060 #endif
1061 
1062 /*
1063  * _DARWIN_FEATURE_ONLY_UNIX_CONFORMANCE indicates only UNIX conforming API
1064  * are available (the legacy BSD APIs are not available)
1065  */
1066 #if __DARWIN_ONLY_UNIX_CONFORMANCE
1067 #define _DARWIN_FEATURE_ONLY_UNIX_CONFORMANCE   1
1068 #endif
1069 
1070 /*
1071  * _DARWIN_FEATURE_UNIX_CONFORMANCE indicates whether UNIX conformance is on,
1072  * and specifies the conformance level (3 is SUSv3)
1073  */
1074 #if __DARWIN_UNIX03
1075 #define _DARWIN_FEATURE_UNIX_CONFORMANCE        3
1076 #endif
1077 
1078 #if defined(DRIVERKIT) && !defined(KERNEL)
1079 /*
1080  * __DRIVERKIT_LIBC__ indicates to the C++ standard library headers and
1081  * similar components that only the restricted set of standard C library
1082  * functionality and headers for the DriverKit userspace driver environment
1083  * are available.
1084  */
1085 #define __DRIVERKIT_LIBC__                      1
1086 #endif /* defined(DRIVERKIT) && !defined(KERNEL) */
1087 
1088 /*
1089  * This macro casts away the qualifier from the variable
1090  *
1091  * Note: use at your own risk, removing qualifiers can result in
1092  * catastrophic run-time failures.
1093  */
1094 #ifndef __CAST_AWAY_QUALIFIER
1095 /*
1096  * XXX: this shouldn't ignore anything more than -Wcast-qual,
1097  * but the old implementation made it an almighty cast that
1098  * ignored everything, so things break left and right if you
1099  * make it only ignore -Wcast-qual.
1100  */
1101 #define __CAST_AWAY_QUALIFIER(variable, qualifier, type) \
1102 	_Pragma("GCC diagnostic push") \
1103 	_Pragma("GCC diagnostic ignored \"-Wcast-qual\"") \
1104 	_Pragma("GCC diagnostic ignored \"-Wcast-align\"") \
1105 	_Pragma("GCC diagnostic ignored \"-Waddress-of-packed-member\"") \
1106 	((type)(variable)) \
1107 	_Pragma("GCC diagnostic pop")
1108 #endif
1109 
1110 /*
1111  * __XNU_PRIVATE_EXTERN is a linkage decoration indicating that a symbol can be
1112  * used from other compilation units, but not other libraries or executables.
1113  */
1114 #ifndef __XNU_PRIVATE_EXTERN
1115 #define __XNU_PRIVATE_EXTERN __attribute__((visibility("hidden")))
1116 #endif
1117 
1118 #if __has_include(<ptrcheck.h>)
1119 #include <ptrcheck.h>
1120 #else
1121 /*
1122  * We intentionally define to nothing pointer attributes which do not have an
1123  * impact on the ABI. __indexable and __bidi_indexable are not defined because
1124  * of the ABI incompatibility that makes the diagnostic preferable.
1125  */
1126 #define __has_ptrcheck 0
1127 #define __single
1128 #define __unsafe_indexable
1129 #define __counted_by(N)
1130 #define __counted_by_or_null(N)
1131 #define __sized_by(N)
1132 #define __sized_by_or_null(N)
1133 #define __ended_by(E)
1134 #define __terminated_by(T)
1135 #define __null_terminated
1136 
1137 /*
1138  * Similarly, we intentionally define to nothing the
1139  * __ptrcheck_abi_assume_single and __ptrcheck_abi_assume_unsafe_indexable
1140  * macros because they do not lead to an ABI incompatibility. However, we do not
1141  * define the indexable and unsafe_indexable ones because the diagnostic is
1142  * better than the silent ABI break.
1143  */
1144 #define __ptrcheck_abi_assume_single()
1145 #define __ptrcheck_abi_assume_unsafe_indexable()
1146 
1147 /* __unsafe_forge intrinsics are defined as regular C casts. */
1148 #define __unsafe_forge_bidi_indexable(T, P, S) ((T)(P))
1149 #define __unsafe_forge_single(T, P) ((T)(P))
1150 #define __unsafe_forge_terminated_by(T, P, E) ((T)(P))
1151 #define __unsafe_forge_null_terminated(T, P) ((T)(P))
1152 #define __terminated_by_to_indexable(P) (P)
1153 #define __unsafe_terminated_by_to_indexable(P) (P)
1154 #define __null_terminated_to_indexable(P) (P)
1155 #define __unsafe_null_terminated_to_indexable(P) (P)
1156 #define __unsafe_terminated_by_from_indexable(T, P, ...) (P)
1157 #define __unsafe_null_terminated_from_indexable(P, ...) (P)
1158 
1159 /* decay operates normally; attribute is meaningless without pointer checks. */
1160 #define __array_decay_dicards_count_in_parameters
1161 
1162 /* this is a write-once variable; not useful without pointer checks. */
1163 #define __unsafe_late_const
1164 
1165 #define __ptrcheck_unavailable
1166 #define __ptrcheck_unavailable_r(REPLACEMENT)
1167 
1168 #endif /* !__has_include(<ptrcheck.h>) */
1169 
1170 #if KERNEL && !BOUND_CHECKS && !__has_ptrcheck
1171 /*
1172  * With pointer checks disabled, we define __indexable to allow source to still
1173  * contain these annotations. This is safe in builds which _uniformly_ disable
1174  * pointer checks (but not in builds which inconsistently have them enabled).
1175  */
1176 
1177 #define __indexable
1178 #define __bidi_indexable
1179 #endif
1180 
1181 #define __ASSUME_PTR_ABI_SINGLE_BEGIN       __ptrcheck_abi_assume_single()
1182 #define __ASSUME_PTR_ABI_SINGLE_END         __ptrcheck_abi_assume_unsafe_indexable()
1183 
1184 #if __has_ptrcheck
1185 #define __header_indexable                  __indexable
1186 #define __header_bidi_indexable             __bidi_indexable
1187 #else
1188 #define __header_indexable
1189 #define __header_bidi_indexable
1190 #endif
1191 
1192 /*
1193  * Architecture validation for current SDK
1194  */
1195 #if !defined(__sys_cdefs_arch_unknown__) && defined(__i386__)
1196 #elif !defined(__sys_cdefs_arch_unknown__) && defined(__x86_64__)
1197 #elif !defined(__sys_cdefs_arch_unknown__) && defined(__arm__)
1198 #elif !defined(__sys_cdefs_arch_unknown__) && defined(__arm64__)
1199 #else
1200 #error Unsupported architecture
1201 #endif
1202 
1203 #ifdef XNU_KERNEL_PRIVATE
1204 /*
1205  * Selectively ignore cast alignment warnings
1206  */
1207 #define __IGNORE_WCASTALIGN(x) _Pragma("clang diagnostic push")                     \
1208 	                       _Pragma("clang diagnostic ignored \"-Wcast-align\"") \
1209 	                       x                                                    \
1210 	                       _Pragma("clang diagnostic pop")
1211 #endif
1212 
1213 #if defined(PRIVATE) || defined(KERNEL)
1214 /*
1215  * Check if __probable and __improbable have already been defined elsewhere.
1216  * These macros inform the compiler (and humans) about which branches are likely
1217  * to be taken.
1218  */
1219 #if !defined(__probable) && !defined(__improbable)
1220 #define __probable(x)   __builtin_expect(!!(x), 1)
1221 #define __improbable(x) __builtin_expect(!!(x), 0)
1222 #endif /* !defined(__probable) && !defined(__improbable) */
1223 
1224 #define __container_of(ptr, type_t, field) __extension__({ \
1225 	const __typeof__(((type_t *)NULL)->field) *__ptr = (ptr);               \
1226 	uintptr_t __result = (uintptr_t)__ptr - offsetof(type_t, field);        \
1227 	if (__ptr) __builtin_assume(__result != 0);                             \
1228 	__unsafe_forge_single(type_t *, __result);                              \
1229 })
1230 
1231 #define __container_of_safe(ptr, type_t, field) __extension__({ \
1232 	const __typeof__(((type_t *)NULL)->field) *__ptr_or_null = (ptr);       \
1233 	__ptr_or_null ? __container_of(__ptr_or_null, type_t, field) : NULL;    \
1234 })
1235 
1236 /*
1237  * This forces the optimizer to materialize the specified variable value,
1238  * and prevents any reordering of operations done to it.
1239  */
1240 #define __compiler_materialize_and_prevent_reordering_on(var) \
1241 	__asm__ ("" : "=r"(var) : "0"(var))
1242 
1243 #endif /* KERNEL || PRIVATE */
1244 
1245 #define __compiler_barrier() __asm__ __volatile__("" ::: "memory")
1246 
1247 #if __has_attribute(enum_extensibility)
1248 #define __enum_open __attribute__((__enum_extensibility__(open)))
1249 #define __enum_closed __attribute__((__enum_extensibility__(closed)))
1250 #else
1251 #define __enum_open
1252 #define __enum_closed
1253 #endif // __has_attribute(enum_extensibility)
1254 
1255 #if __has_attribute(flag_enum)
1256 #define __enum_options __attribute__((__flag_enum__))
1257 #else
1258 #define __enum_options
1259 #endif
1260 
1261 /*
1262  * Similar to OS_ENUM/OS_CLOSED_ENUM/OS_OPTIONS/OS_CLOSED_OPTIONS
1263  *
1264  * This provides more advanced type checking on compilers supporting
1265  * the proper extensions, even in C.
1266  */
1267 #if __has_feature(objc_fixed_enum) || __has_extension(cxx_fixed_enum) || \
1268         __has_extension(cxx_strong_enums)
1269 #define __enum_decl(_name, _type, ...) \
1270 	        typedef enum : _type __VA_ARGS__ __enum_open _name
1271 #define __enum_closed_decl(_name, _type, ...) \
1272 	        typedef enum : _type __VA_ARGS__ __enum_closed _name
1273 #define __options_decl(_name, _type, ...) \
1274 	        typedef enum : _type __VA_ARGS__ __enum_open __enum_options _name
1275 #define __options_closed_decl(_name, _type, ...) \
1276 	        typedef enum : _type __VA_ARGS__ __enum_closed __enum_options _name
1277 #else
1278 #define __enum_decl(_name, _type, ...) \
1279 	        typedef _type _name; enum __VA_ARGS__ __enum_open
1280 #define __enum_closed_decl(_name, _type, ...) \
1281 	        typedef _type _name; enum __VA_ARGS__ __enum_closed
1282 #define __options_decl(_name, _type, ...) \
1283 	        typedef _type _name; enum __VA_ARGS__ __enum_open __enum_options
1284 #define __options_closed_decl(_name, _type, ...) \
1285 	        typedef _type _name; enum __VA_ARGS__ __enum_closed __enum_options
1286 #endif
1287 
1288 #if XNU_KERNEL_PRIVATE
1289 /*
1290  * __xnu_struct_group() can be used to declare a set of fields to be grouped
1291  * together logically in order to perform safer memory operations
1292  * (assignment, zeroing, ...) on them.
1293  */
1294 #ifdef __cplusplus
1295 #define __xnu_struct_group(group_type, group_name, ...) \
1296 	struct group_type __VA_ARGS__; \
1297 	union { \
1298 	    struct __VA_ARGS__; \
1299 	    struct group_type group_name; \
1300 	}
1301 #else
1302 #define __xnu_struct_group(group_type, group_name, ...) \
1303 	union { \
1304 	    struct __VA_ARGS__; \
1305 	    struct group_type __VA_ARGS__ group_name; \
1306 	}
1307 #endif
1308 #endif /* XNU_KERNEL_PRIVATE */
1309 
1310 #if defined(KERNEL) && __has_attribute(xnu_usage_semantics)
1311 /*
1312  * These macros can be used to annotate type definitions or scalar structure
1313  * fields to inform the compiler about which semantic they have with regards
1314  * to the content of the underlying memory represented by such type or field.
1315  *
1316  * This information is used in the analysis of the types performed by the
1317  * signature based type segregation implemented in kalloc.
1318  */
1319 #define __kernel_ptr_semantics __attribute__((xnu_usage_semantics("pointer")))
1320 #define __kernel_data_semantics __attribute__((xnu_usage_semantics("data")))
1321 #define __kernel_dual_semantics __attribute__((xnu_usage_semantics("pointer", "data")))
1322 
1323 #else  /* defined(KERNEL) && __has_attribute(xnu_usage_semantics) */
1324 
1325 #define __kernel_ptr_semantics
1326 #define __kernel_data_semantics
1327 #define __kernel_dual_semantics
1328 
1329 #endif /* defined(KERNEL) && __has_attribute(xnu_usage_semantics) */
1330 
1331 #if XNU_KERNEL_PRIVATE
1332 /*
1333  * Compiler-dependent macros that bracket portions of code where the
1334  * "-Wxnu-typed-allocators" warning should be ignored.
1335  */
1336 #if defined(__clang__)
1337 # define __typed_allocators_ignore_push \
1338 	 _Pragma("clang diagnostic push") \
1339 	 _Pragma("clang diagnostic ignored \"-Wxnu-typed-allocators\"")
1340 # define __typed_allocators_ignore_pop \
1341 	 _Pragma("clang diagnostic pop")
1342 # define __typed_allocators_ignore(x) __typed_allocators_ignore_push \
1343 	                              x                              \
1344 	                              __typed_allocators_ignore_pop
1345 #else
1346 # define __typed_allocators_ignore_push
1347 # define __typed_allocators_ignore_pop
1348 # define __typed_allocators_ignore(x) x
1349 #endif /* __clang */
1350 #endif /* XNU_KERNEL_PRIVATE */
1351 
1352 #if defined(KERNEL_PRIVATE) && \
1353         __has_attribute(xnu_data_size) && \
1354         __has_attribute(xnu_returns_data_pointer)
1355 /*
1356  * Annotate function parameters to specify that they semantically
1357  * represent the size of a data-only backing storage.
1358  */
1359 # define __xnu_data_size __attribute__((xnu_data_size))
1360 /*
1361  * Annotate function declarations to specify that the pointer they return
1362  * points to a data-only backing storage.
1363  */
1364 # define __xnu_returns_data_pointer __attribute__((xnu_returns_data_pointer))
1365 #else
1366 # define __xnu_data_size
1367 # define __xnu_returns_data_pointer
1368 #endif
1369 
1370 #endif /* !_CDEFS_H_ */
1371