xref: /freebsd/sys/sys/cdefs.h (revision 7a3af393d8ac2dfe72d24fe401344b60c4f87866)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Berkeley Software Design, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #ifndef	_SYS_CDEFS_H_
36 #define	_SYS_CDEFS_H_
37 
38 #if defined(_KERNEL) && defined(_STANDALONE)
39 #error "_KERNEL and _STANDALONE are mutually exclusive"
40 #endif
41 
42 /*
43  * Provide clang-compatible testing macros. All supported versions of gcc (10+)
44  * provide all of these except has_feature and has_extension which are new in
45  * gcc 14. Keep the older ifndefs, though, for non-gcc compilers that may lack
46  * them like tcc and pcc.
47  */
48 #ifndef	__has_attribute
49 #define	__has_attribute(x)	0
50 #endif
51 #ifndef	__has_extension
52 #define	__has_extension		__has_feature
53 #endif
54 #ifndef	__has_feature
55 #define	__has_feature(x)	0
56 #endif
57 #ifndef	__has_include
58 #define	__has_include(x)	0
59 #endif
60 #ifndef	__has_builtin
61 #define	__has_builtin(x)	0
62 #endif
63 
64 #if defined(__cplusplus)
65 #define	__BEGIN_DECLS	extern "C" {
66 #define	__END_DECLS	}
67 #else
68 #define	__BEGIN_DECLS
69 #define	__END_DECLS
70 #endif
71 
72 /*
73  * This code has been put in place to help reduce the addition of
74  * compiler specific defines in FreeBSD code.  It helps to aid in
75  * having a compiler-agnostic source tree.
76  */
77 
78 /*
79  * Macro to test if we're using a specific version of gcc or later.
80  */
81 #if defined(__GNUC__)
82 #define	__GNUC_PREREQ__(ma, mi)	\
83 	(__GNUC__ > (ma) || __GNUC__ == (ma) && __GNUC_MINOR__ >= (mi))
84 #else
85 #define	__GNUC_PREREQ__(ma, mi)	0
86 #endif
87 
88 #if defined(__GNUC__)
89 
90 /*
91  * Compiler memory barriers, specific to gcc and clang.
92  */
93 #define	__compiler_membar()	__asm __volatile(" " : : : "memory")
94 
95 #define	__CC_SUPPORTS___INLINE 1
96 #define	__CC_SUPPORTS_SYMVER 1
97 
98 #endif /* __GNUC__ */
99 
100 /*
101  * TinyC pretends to be gcc 9.3. This is generally good enough to support
102  * everything FreeBSD... except for the .symver assembler directive.
103  */
104 #ifdef __TINYC__
105 #undef	__CC_SUPPORTS_SYMVER
106 #endif
107 
108 /*
109  * The __CONCAT macro is used to concatenate parts of symbol names, e.g.
110  * with "#define OLD(foo) __CONCAT(old,foo)", OLD(foo) produces oldfoo.
111  * The __CONCAT macro is a bit tricky to use if it must work in non-ANSI
112  * mode -- there must be no spaces between its arguments, and for nested
113  * __CONCAT's, all the __CONCAT's must be at the left.  __CONCAT can also
114  * concatenate double-quoted strings produced by the __STRING macro, but
115  * this only works with ANSI C.
116  *
117  * __XSTRING is like __STRING, but it expands any macros in its argument
118  * first.  It is only available with ANSI C.
119  */
120 #if defined(__STDC__) || defined(__cplusplus)
121 #define	__P(protos)	protos		/* full-blown ANSI C */
122 #define	__CONCAT1(x,y)	x ## y
123 #define	__CONCAT(x,y)	__CONCAT1(x,y)
124 #define	__STRING(x)	#x		/* stringify without expanding x */
125 #define	__XSTRING(x)	__STRING(x)	/* expand x, then stringify */
126 
127 #define	__volatile	volatile
128 #if defined(__cplusplus)
129 #define	__inline	inline		/* convert to C++ keyword */
130 #else
131 #if !(defined(__CC_SUPPORTS___INLINE))
132 #define	__inline			/* delete GCC keyword */
133 #endif /* ! __CC_SUPPORTS___INLINE */
134 #endif /* !__cplusplus */
135 
136 #else	/* !(__STDC__ || __cplusplus) */
137 #define	__P(protos)	()		/* traditional C preprocessor */
138 #define	__CONCAT(x,y)	x/**/y
139 #define	__STRING(x)	"x"
140 #if !defined(__CC_SUPPORTS___INLINE)
141 /* Just delete these in a K&R environment */
142 #define	__inline
143 #define	__volatile
144 #endif	/* !__CC_SUPPORTS___INLINE */
145 #endif	/* !(__STDC__ || __cplusplus) */
146 
147 /*
148  * Compiler-dependent macros to help declare dead (non-returning) and pure (no
149  * side effects) functions, and unused variables. These attributes are supported
150  * by all current compilers, even pcc.
151  */
152 #define	__weak_symbol	__attribute__((__weak__))
153 #define	__dead2		__attribute__((__noreturn__))
154 #define	__pure2		__attribute__((__const__))
155 #define	__unused	__attribute__((__unused__))
156 #define	__used		__attribute__((__used__))
157 #define __deprecated	__attribute__((__deprecated__))
158 #define	__packed	__attribute__((__packed__))
159 #define	__aligned(x)	__attribute__((__aligned__(x)))
160 #define	__section(x)	__attribute__((__section__(x)))
161 #define	__writeonly	__unused
162 #define	__alloc_size(x)	__attribute__((__alloc_size__(x)))
163 #define	__alloc_size2(n, x)	__attribute__((__alloc_size__(n, x)))
164 #define	__alloc_align(x)	__attribute__((__alloc_align__(x)))
165 
166 /*
167  * Keywords added in C11.
168  */
169 
170 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L
171 
172 #if !__has_extension(c_alignas)
173 #if (defined(__cplusplus) && __cplusplus >= 201103L) || \
174     __has_extension(cxx_alignas)
175 #define	_Alignas(x)		alignas(x)
176 #else
177 /* XXX: Only emulates _Alignas(constant-expression); not _Alignas(type-name). */
178 #define	_Alignas(x)		__aligned(x)
179 #endif
180 #endif
181 
182 #if defined(__cplusplus) && __cplusplus >= 201103L
183 #define	_Alignof(x)		alignof(x)
184 #else
185 #define	_Alignof(x)		__alignof(x)
186 #endif
187 
188 #if defined(__cplusplus) && __cplusplus >= 201103L
189 #define	_Noreturn		[[noreturn]]
190 #else
191 #define	_Noreturn		__dead2
192 #endif
193 
194 #if !__has_extension(c_static_assert)
195 #if (defined(__cplusplus) && __cplusplus >= 201103L) || \
196     __has_extension(cxx_static_assert)
197 #define	_Static_assert(x, y)	static_assert(x, y)
198 #endif
199 #endif
200 
201 #if !__has_extension(c_thread_local)
202 #if (defined(__cplusplus) && __cplusplus >= 201103L) || \
203     __has_extension(cxx_thread_local)
204 #define	_Thread_local		thread_local
205 #else
206 #define	_Thread_local		__thread
207 #endif
208 #endif
209 
210 #endif /* __STDC_VERSION__ || __STDC_VERSION__ < 201112L */
211 
212 /*
213  * Emulation of C11 _Generic().  Unlike the previously defined C11
214  * keywords, it is not possible to implement this using exactly the same
215  * syntax.  Therefore implement something similar under the name
216  * __generic().  Unlike _Generic(), this macro can only distinguish
217  * between a single type, so it requires nested invocations to
218  * distinguish multiple cases.
219  *
220  * Note that the comma operator is used to force expr to decay in
221  * order to match _Generic().
222  */
223 
224 #if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) || \
225     __has_extension(c_generic_selections)
226 #define	__generic(expr, t, yes, no)					\
227 	_Generic(expr, t: yes, default: no)
228 #elif !defined(__cplusplus)
229 #define	__generic(expr, t, yes, no)					\
230 	__builtin_choose_expr(__builtin_types_compatible_p(		\
231 	    __typeof(((void)0, (expr))), t), yes, no)
232 #endif
233 
234 /*
235  * C99 Static array indices in function parameter declarations.  Syntax such as:
236  * void bar(int myArray[static 10]);
237  * is allowed in C99 but not in C++.  Define __min_size appropriately so
238  * headers using it can be compiled in either language.  Use like this:
239  * void bar(int myArray[__min_size(10)]);
240  */
241 #if !defined(__cplusplus) && \
242     (!defined(__STDC_VERSION__) || (__STDC_VERSION__ >= 199901))
243 #define __min_size(x)	static (x)
244 #else
245 #define __min_size(x)	(x)
246 #endif
247 
248 #define	__malloc_like	__attribute__((__malloc__))
249 #define	__pure		__attribute__((__pure__))
250 
251 #define	__always_inline	__inline __attribute__((__always_inline__))
252 #define	__noinline	__attribute__ ((__noinline__))
253 #define	__fastcall	__attribute__((__fastcall__))
254 #define	__result_use_check	__attribute__((__warn_unused_result__))
255 #ifdef __clang__
256 /*
257  * clang and gcc have different semantics for __warn_unused_result__: the latter
258  * does not permit the use of a void cast to suppress the warning.  Use
259  * __result_use_or_ignore_check in places where a void cast is acceptable.
260  * This can be implemented by [[nodiscard]] from C23.
261  */
262 #define	__result_use_or_ignore_check	__result_use_check
263 #else
264 #define	__result_use_or_ignore_check
265 #endif /* !__clang__ */
266 
267 #define	__returns_twice	__attribute__((__returns_twice__))
268 
269 #define	__unreachable()	__builtin_unreachable()
270 
271 #if !defined(__STRICT_ANSI__) || __STDC_VERSION__ >= 199901
272 #define	__LONG_LONG_SUPPORTED
273 #endif
274 
275 /* C++11 exposes a load of C99 stuff */
276 #if defined(__cplusplus) && __cplusplus >= 201103L
277 #define	__LONG_LONG_SUPPORTED
278 #ifndef	__STDC_LIMIT_MACROS
279 #define	__STDC_LIMIT_MACROS
280 #endif
281 #ifndef	__STDC_CONSTANT_MACROS
282 #define	__STDC_CONSTANT_MACROS
283 #endif
284 #endif
285 
286 /*
287  * noexcept keyword added in C++11.
288  */
289 #if defined(__cplusplus) && __cplusplus >= 201103L
290 #define __noexcept noexcept
291 #define __noexcept_if(__c) noexcept(__c)
292 #else
293 #define __noexcept
294 #define __noexcept_if(__c)
295 #endif
296 
297 /*
298  * We use `__restrict' as a way to define the `restrict' type qualifier
299  * without disturbing older software that is unaware of C99 keywords.
300  * GCC also provides `__restrict' as an extension to support C99-style
301  * restricted pointers in other language modes.
302  */
303 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901
304 #define	__restrict	restrict
305 #endif
306 
307 /*
308  * All modern compilers have explicit branch prediction so that the CPU back-end
309  * can hint to the processor and also so that code blocks can be reordered such
310  * that the predicted path sees a more linear flow, thus improving cache
311  * behavior, etc. Use sparingly, except in performance critical code where
312  * they make things measurably faster.
313  */
314 #define	__predict_true(exp)     __builtin_expect((exp), 1)
315 #define	__predict_false(exp)    __builtin_expect((exp), 0)
316 
317 #define	__null_sentinel	__attribute__((__sentinel__))
318 #define	__exported	__attribute__((__visibility__("default")))
319 #define	__hidden	__attribute__((__visibility__("hidden")))
320 
321 /*
322  * We define this here since <stddef.h>, <sys/queue.h>, and <sys/types.h>
323  * require it.
324  */
325 #define	__offsetof(type, field)	 __builtin_offsetof(type, field)
326 #define	__rangeof(type, start, end) \
327 	(__offsetof(type, end) - __offsetof(type, start))
328 
329 /*
330  * Given the pointer x to the member m of the struct s, return
331  * a pointer to the containing structure.  When using GCC, we first
332  * assign pointer x to a local variable, to check that its type is
333  * compatible with member m.
334  */
335 #define	__containerof(x, s, m) ({					\
336 	const volatile __typeof(((s *)0)->m) *__x = (x);		\
337 	__DEQUALIFY(s *, (const volatile char *)__x - __offsetof(s, m));\
338 })
339 
340 /*
341  * Compiler-dependent macros to declare that functions take printf-like
342  * or scanf-like arguments.
343  */
344 #define	__printflike(fmtarg, firstvararg) \
345 	    __attribute__((__format__ (__printf__, fmtarg, firstvararg)))
346 #define	__scanflike(fmtarg, firstvararg) \
347 	    __attribute__((__format__ (__scanf__, fmtarg, firstvararg)))
348 #define	__format_arg(fmtarg)	__attribute__((__format_arg__ (fmtarg)))
349 #define	__strfmonlike(fmtarg, firstvararg) \
350 	    __attribute__((__format__ (__strfmon__, fmtarg, firstvararg)))
351 #define	__strftimelike(fmtarg, firstvararg) \
352 	    __attribute__((__format__ (__strftime__, fmtarg, firstvararg)))
353 
354 /*
355  * Like __printflike, but allows fmtarg to be NULL. FreeBSD invented 'printf0'
356  * for this because older versions of gcc issued warnings for NULL first args.
357  * Clang has always had printf and printf0 as aliases. gcc 11.0 now follows
358  * clang. So now this is an alias for __printflike, or nothing. In the future
359  * _Nullable or _Nonnull will replace this.
360  * XXX Except that doesn't work, so for now revert to printf0 for clang and
361  * the FreeBSD gcc until I can work this out.
362  */
363 #if defined(__clang__) || (defined(__GNUC__) && defined (__FreeBSD_cc_version))
364 #define	__printf0like(fmtarg, firstvararg) \
365 	    __attribute__((__format__ (__printf0__, fmtarg, firstvararg)))
366 #else
367 #define	__printf0like(fmtarg, firstvararg)
368 #endif
369 
370 #define	__strong_reference(sym,aliassym)	\
371 	extern __typeof (sym) aliassym __attribute__ ((__alias__ (#sym)))
372 #ifdef __STDC__
373 #define	__weak_reference(sym,alias)	\
374 	__asm__(".weak " #alias);	\
375 	__asm__(".equ "  #alias ", " #sym)
376 #define	__warn_references(sym,msg)	\
377 	__asm__(".section .gnu.warning." #sym);	\
378 	__asm__(".asciz \"" msg "\"");	\
379 	__asm__(".previous")
380 #ifdef	__CC_SUPPORTS_SYMVER
381 #define	__sym_compat(sym,impl,verid)	\
382 	__asm__(".symver " #impl ", " #sym "@" #verid)
383 #define	__sym_default(sym,impl,verid)	\
384 	__asm__(".symver " #impl ", " #sym "@@@" #verid)
385 #endif
386 #else
387 #define	__weak_reference(sym,alias)	\
388 	__asm__(".weak alias");		\
389 	__asm__(".equ alias, sym")
390 #define	__warn_references(sym,msg)	\
391 	__asm__(".section .gnu.warning.sym"); \
392 	__asm__(".asciz \"msg\"");	\
393 	__asm__(".previous")
394 #ifdef	__CC_SUPPORTS_SYMVER
395 #define	__sym_compat(sym,impl,verid)	\
396 	__asm__(".symver impl, sym@verid")
397 #define	__sym_default(impl,sym,verid)	\
398 	__asm__(".symver impl, sym@@@verid")
399 #endif
400 #endif	/* __STDC__ */
401 
402 #define	__GLOBL(sym)	__asm__(".globl " __XSTRING(sym))
403 #define	__WEAK(sym)	__asm__(".weak " __XSTRING(sym))
404 
405 #define	__IDSTRING(name,string)	__asm__(".ident\t\"" string "\"")
406 
407 /*
408  * Embed the rcs id of a source file in the resulting library.  Note that in
409  * more recent ELF binutils, we use .ident allowing the ID to be stripped.
410  * Usage:
411  */
412 #ifndef	__FBSDID
413 #if !defined(STRIP_FBSDID)
414 #define	__FBSDID(s)	__IDSTRING(__CONCAT(__rcsid_,__LINE__),s)
415 #else
416 #define	__FBSDID(s)	struct __hack
417 #endif
418 #endif
419 
420 #ifndef	__RCSID
421 #ifndef	NO__RCSID
422 #define	__RCSID(s)	__IDSTRING(__CONCAT(__rcsid_,__LINE__),s)
423 #else
424 #define	__RCSID(s)	struct __hack
425 #endif
426 #endif
427 
428 #ifndef	__RCSID_SOURCE
429 #ifndef	NO__RCSID_SOURCE
430 #define	__RCSID_SOURCE(s)	__IDSTRING(__CONCAT(__rcsid_source_,__LINE__),s)
431 #else
432 #define	__RCSID_SOURCE(s)	struct __hack
433 #endif
434 #endif
435 
436 #ifndef	__SCCSID
437 #ifndef	NO__SCCSID
438 #define	__SCCSID(s)	__IDSTRING(__CONCAT(__sccsid_,__LINE__),s)
439 #else
440 #define	__SCCSID(s)	struct __hack
441 #endif
442 #endif
443 
444 #ifndef	__COPYRIGHT
445 #ifndef	NO__COPYRIGHT
446 #define	__COPYRIGHT(s)	__IDSTRING(__CONCAT(__copyright_,__LINE__),s)
447 #else
448 #define	__COPYRIGHT(s)	struct __hack
449 #endif
450 #endif
451 
452 #ifndef	__DECONST
453 #define	__DECONST(type, var)	((type)(__uintptr_t)(const void *)(var))
454 #endif
455 
456 #ifndef	__DEVOLATILE
457 #define	__DEVOLATILE(type, var)	((type)(__uintptr_t)(volatile void *)(var))
458 #endif
459 
460 #ifndef	__DEQUALIFY
461 #define	__DEQUALIFY(type, var)	((type)(__uintptr_t)(const volatile void *)(var))
462 #endif
463 
464 #if !defined(_STANDALONE) && !defined(_KERNEL)
465 #define	__RENAME(x)	__asm(__STRING(x))
466 #else /* _STANDALONE || _KERNEL */
467 #define	__RENAME(x)	no renaming in kernel/standalone environment
468 #endif
469 
470 /*-
471  * The following definitions are an extension of the behavior originally
472  * implemented in <sys/_posix.h>, but with a different level of granularity.
473  * POSIX.1 requires that the macros we test be defined before any standard
474  * header file is included.
475  *
476  * Here's a quick run-down of the versions (and some informal names)
477  *  defined(_POSIX_SOURCE)		1003.1-1988
478  *					encoded as 198808 below
479  *  _POSIX_C_SOURCE == 1		1003.1-1990
480  *					encoded as 199009 below
481  *  _POSIX_C_SOURCE == 2		1003.2-1992 C Language Binding Option
482  *					encoded as 199209 below
483  *  _POSIX_C_SOURCE == 199309		1003.1b-1993
484  *					(1003.1 Issue 4, Single Unix Spec v1, Unix 93)
485  *  _POSIX_C_SOURCE == 199506		1003.1c-1995, 1003.1i-1995,
486  *					and the omnibus ISO/IEC 9945-1: 1996
487  *					(1003.1 Issue 5, Single	Unix Spec v2, Unix 95)
488  *  _POSIX_C_SOURCE == 200112		1003.1-2001 (1003.1 Issue 6, Unix 03)
489  *					with _XOPEN_SOURCE=600
490  *  _POSIX_C_SOURCE == 200809		1003.1-2008 (1003.1 Issue 7)
491  *					IEEE Std 1003.1-2017 (Rev of 1003.1-2008) is
492  *					1003.1-2008 with two TCs applied and
493  *					_XOPEN_SOURCE=700
494  * _POSIX_C_SOURCE == 202405		1003.1-2004 (1003.1 Issue 8), IEEE Std 1003.1-2024
495  * 					with _XOPEN_SOURCE=800
496  *
497  * In addition, the X/Open Portability Guide, which is now the Single UNIX
498  * Specification, defines a feature-test macro which indicates the version of
499  * that specification, and which subsumes _POSIX_C_SOURCE.
500  *
501  * Our macros begin with two underscores to avoid namespace screwage.
502  */
503 
504 /* Deal with IEEE Std. 1003.1-1990, in which _POSIX_C_SOURCE == 1. */
505 #if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE == 1
506 #undef _POSIX_C_SOURCE		/* Probably illegal, but beyond caring now. */
507 #define	_POSIX_C_SOURCE		199009
508 #endif
509 
510 /* Deal with IEEE Std. 1003.2-1992, in which _POSIX_C_SOURCE == 2. */
511 #if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE == 2
512 #undef _POSIX_C_SOURCE
513 #define	_POSIX_C_SOURCE		199209
514 #endif
515 
516 /*
517  * Deal with various X/Open Portability Guides and Single UNIX Spec. We use the
518  * '- 0' construct so software that defines _XOPEN_SOURCE to nothing doesn't
519  * cause errors. X/Open CAE Specification, August 1994, System Interfaces and
520  * Headers, Issue 4, Version 2 section 2.2 states an empty definition means the
521  * same thing as _POSIX_C_SOURCE == 2. This broadly mirrors "System V Interface
522  * Definition, Fourth Edition", but earlier editions suggest some ambiguity.
523  * However, FreeBSD has histoically implemented this as a NOP, so we just
524  * document what it should be for now to not break ports gratuitously.
525  */
526 #ifdef _XOPEN_SOURCE
527 #if _XOPEN_SOURCE - 0 >= 800
528 #define	__XSI_VISIBLE		800
529 #undef _POSIX_C_SOURCE
530 #define	_POSIX_C_SOURCE		202405
531 #elif _XOPEN_SOURCE - 0 >= 700
532 #define	__XSI_VISIBLE		700
533 #undef _POSIX_C_SOURCE
534 #define	_POSIX_C_SOURCE		200809
535 #elif _XOPEN_SOURCE - 0 >= 600
536 #define	__XSI_VISIBLE		600
537 #undef _POSIX_C_SOURCE
538 #define	_POSIX_C_SOURCE		200112
539 #elif _XOPEN_SOURCE - 0 >= 500
540 #define	__XSI_VISIBLE		500
541 #undef _POSIX_C_SOURCE
542 #define	_POSIX_C_SOURCE		199506
543 #else
544 /* #define	_POSIX_C_SOURCE		199209 */
545 #endif
546 #endif
547 
548 /*
549  * Deal with all versions of POSIX.  The ordering relative to the tests above is
550  * important.
551  */
552 #if defined(_POSIX_SOURCE) && !defined(_POSIX_C_SOURCE)
553 #define	_POSIX_C_SOURCE		198808
554 #endif
555 #ifdef _POSIX_C_SOURCE
556 #if _POSIX_C_SOURCE >= 202405
557 #define	__POSIX_VISIBLE		202405
558 #define	__ISO_C_VISIBLE		2017
559 #elif _POSIX_C_SOURCE >= 200809
560 #define	__POSIX_VISIBLE		200809
561 #define	__ISO_C_VISIBLE		1999
562 #elif _POSIX_C_SOURCE >= 200112
563 #define	__POSIX_VISIBLE		200112
564 #define	__ISO_C_VISIBLE		1999
565 #elif _POSIX_C_SOURCE >= 199506
566 #define	__POSIX_VISIBLE		199506
567 #define	__ISO_C_VISIBLE		1990
568 #elif _POSIX_C_SOURCE >= 199309
569 #define	__POSIX_VISIBLE		199309
570 #define	__ISO_C_VISIBLE		1990
571 #elif _POSIX_C_SOURCE >= 199209
572 #define	__POSIX_VISIBLE		199209
573 #define	__ISO_C_VISIBLE		1990
574 #elif _POSIX_C_SOURCE >= 199009
575 #define	__POSIX_VISIBLE		199009
576 #define	__ISO_C_VISIBLE		1990
577 #else
578 #define	__POSIX_VISIBLE		198808
579 #define	__ISO_C_VISIBLE		0
580 #endif /* _POSIX_C_SOURCE */
581 
582 /*
583  * When we've explicitly asked for a newer C version, make the C variable
584  * visible by default. Also honor the glibc _ISOC{11,23}_SOURCE macros
585  * extensions. Both glibc and OpenBSD do this, even when a more strict
586  * _POSIX_C_SOURCE has been requested, and it makes good sense (especially for
587  * pre POSIX 2024, since C11 is much nicer than the old C99 base). Continue the
588  * practice with C23, though don't do older standards. Also, GLIBC doesn't have
589  * a _ISOC17_SOURCE, so it's not implemented here. glibc has earlier ISOCxx defines,
590  * but we don't implement those as they are not relevant enough.
591  */
592 #if _ISOC23_SOURCE || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L)
593 #undef __ISO_C_VISIBLE
594 #define __ISO_C_VISIBLE		2023
595 #elif _ISOC11_SOURCE || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L)
596 #undef __ISO_C_VISIBLE
597 #define __ISO_C_VISIBLE		2011
598 #endif
599 #else /* _POSIX_C_SOURCE */
600 /*-
601  * Deal with _ANSI_SOURCE:
602  * If it is defined, and no other compilation environment is explicitly
603  * requested, then define our internal feature-test macros to zero.  This
604  * makes no difference to the preprocessor (undefined symbols in preprocessing
605  * expressions are defined to have value zero), but makes it more convenient for
606  * a test program to print out the values.
607  *
608  * If a program mistakenly defines _ANSI_SOURCE and some other macro such as
609  * _POSIX_C_SOURCE, we will assume that it wants the broader compilation
610  * environment (and in fact we will never get here).
611  */
612 #if defined(_ANSI_SOURCE)	/* Hide almost everything. */
613 #define	__POSIX_VISIBLE		0
614 #define	__XSI_VISIBLE		0
615 #define	__BSD_VISIBLE		0
616 #define	__ISO_C_VISIBLE		1990
617 #define	__EXT1_VISIBLE		0
618 #elif defined(_C99_SOURCE)	/* Localism to specify strict C99 env. */
619 #define	__POSIX_VISIBLE		0
620 #define	__XSI_VISIBLE		0
621 #define	__BSD_VISIBLE		0
622 #define	__ISO_C_VISIBLE		1999
623 #define	__EXT1_VISIBLE		0
624 #elif defined(_C11_SOURCE)	/* Localism to specify strict C11 env. */
625 #define	__POSIX_VISIBLE		0
626 #define	__XSI_VISIBLE		0
627 #define	__BSD_VISIBLE		0
628 #define	__ISO_C_VISIBLE		2011
629 #define	__EXT1_VISIBLE		0
630 #elif defined(_C23_SOURCE)	/* Localism to specify strict C23 env. */
631 #define	__POSIX_VISIBLE		0
632 #define	__XSI_VISIBLE		0
633 #define	__BSD_VISIBLE		0
634 #define	__ISO_C_VISIBLE		2023
635 #define	__EXT1_VISIBLE		0
636 #else				/* Default environment: show everything. */
637 #define	__POSIX_VISIBLE		202405
638 #define	__XSI_VISIBLE		800
639 #define	__BSD_VISIBLE		1
640 #define	__ISO_C_VISIBLE		2023
641 #define	__EXT1_VISIBLE		1
642 #endif
643 #endif /* _POSIX_C_SOURCE */
644 
645 /* User override __EXT1_VISIBLE */
646 #if defined(__STDC_WANT_LIB_EXT1__)
647 #undef	__EXT1_VISIBLE
648 #if __STDC_WANT_LIB_EXT1__
649 #define	__EXT1_VISIBLE		1
650 #else
651 #define	__EXT1_VISIBLE		0
652 #endif
653 #endif /* __STDC_WANT_LIB_EXT1__ */
654 
655 /*
656  * Nullability qualifiers: currently only supported by Clang.
657  */
658 #if !(defined(__clang__) && __has_feature(nullability))
659 #define	_Nonnull
660 #define	_Nullable
661 #define	_Null_unspecified
662 #define	__NULLABILITY_PRAGMA_PUSH
663 #define	__NULLABILITY_PRAGMA_POP
664 #else
665 #define	__NULLABILITY_PRAGMA_PUSH _Pragma("clang diagnostic push")	\
666 	_Pragma("clang diagnostic ignored \"-Wnullability-completeness\"")
667 #define	__NULLABILITY_PRAGMA_POP _Pragma("clang diagnostic pop")
668 #endif
669 
670 /*
671  * Type Safety Checking
672  *
673  * Clang provides additional attributes to enable checking type safety
674  * properties that cannot be enforced by the C type system.
675  */
676 
677 #if __has_attribute(__argument_with_type_tag__) && \
678     __has_attribute(__type_tag_for_datatype__)
679 #define	__arg_type_tag(arg_kind, arg_idx, type_tag_idx) \
680 	    __attribute__((__argument_with_type_tag__(arg_kind, arg_idx, type_tag_idx)))
681 #define	__datatype_type_tag(kind, type) \
682 	    __attribute__((__type_tag_for_datatype__(kind, type)))
683 #else
684 #define	__arg_type_tag(arg_kind, arg_idx, type_tag_idx)
685 #define	__datatype_type_tag(kind, type)
686 #endif
687 
688 /*
689  * Lock annotations.
690  *
691  * Clang provides support for doing basic thread-safety tests at
692  * compile-time, by marking which locks will/should be held when
693  * entering/leaving a functions.
694  *
695  * Furthermore, it is also possible to annotate variables and structure
696  * members to enforce that they are only accessed when certain locks are
697  * held.
698  */
699 
700 #if __has_extension(c_thread_safety_attributes)
701 #define	__lock_annotate(x)	__attribute__((x))
702 #else
703 #define	__lock_annotate(x)
704 #endif
705 
706 /* Structure implements a lock. */
707 #define	__lockable		__lock_annotate(lockable)
708 
709 /* Function acquires an exclusive or shared lock. */
710 #define	__locks_exclusive(...) \
711 	__lock_annotate(exclusive_lock_function(__VA_ARGS__))
712 #define	__locks_shared(...) \
713 	__lock_annotate(shared_lock_function(__VA_ARGS__))
714 
715 /* Function attempts to acquire an exclusive or shared lock. */
716 #define	__trylocks_exclusive(...) \
717 	__lock_annotate(exclusive_trylock_function(__VA_ARGS__))
718 #define	__trylocks_shared(...) \
719 	__lock_annotate(shared_trylock_function(__VA_ARGS__))
720 
721 /* Function releases a lock. */
722 #define	__unlocks(...)		__lock_annotate(unlock_function(__VA_ARGS__))
723 
724 /* Function asserts that an exclusive or shared lock is held. */
725 #define	__asserts_exclusive(...) \
726 	__lock_annotate(assert_exclusive_lock(__VA_ARGS__))
727 #define	__asserts_shared(...) \
728 	__lock_annotate(assert_shared_lock(__VA_ARGS__))
729 
730 /* Function requires that an exclusive or shared lock is or is not held. */
731 #define	__requires_exclusive(...) \
732 	__lock_annotate(exclusive_locks_required(__VA_ARGS__))
733 #define	__requires_shared(...) \
734 	__lock_annotate(shared_locks_required(__VA_ARGS__))
735 #define	__requires_unlocked(...) \
736 	__lock_annotate(locks_excluded(__VA_ARGS__))
737 
738 /* Function should not be analyzed. */
739 #define	__no_lock_analysis	__lock_annotate(no_thread_safety_analysis)
740 
741 /*
742  * Function or variable should not be sanitized, e.g., by AddressSanitizer.
743  * GCC has the nosanitize attribute, but as a function attribute only, and
744  * warns on use as a variable attribute.
745  */
746 #if __has_feature(address_sanitizer) && defined(__clang__)
747 #ifdef _KERNEL
748 #define	__nosanitizeaddress	__attribute__((no_sanitize("kernel-address")))
749 #else
750 #define	__nosanitizeaddress	__attribute__((no_sanitize("address")))
751 #endif
752 #else
753 #define	__nosanitizeaddress
754 #endif
755 #if __has_feature(coverage_sanitizer) && defined(__clang__)
756 #define	__nosanitizecoverage	__attribute__((no_sanitize("coverage")))
757 #else
758 #define	__nosanitizecoverage
759 #endif
760 #if __has_feature(memory_sanitizer) && defined(__clang__)
761 #ifdef _KERNEL
762 #define	__nosanitizememory	__attribute__((no_sanitize("kernel-memory")))
763 #else
764 #define	__nosanitizememory	__attribute__((no_sanitize("memory")))
765 #endif
766 #else
767 #define	__nosanitizememory
768 #endif
769 #if __has_feature(thread_sanitizer) && defined(__clang__)
770 #define	__nosanitizethread	__attribute__((no_sanitize("thread")))
771 #else
772 #define	__nosanitizethread
773 #endif
774 
775 /*
776  * Make it possible to opt out of stack smashing protection.
777  */
778 #if __has_attribute(no_stack_protector)
779 #define	__nostackprotector	__attribute__((no_stack_protector))
780 #else
781 #define	__nostackprotector	\
782 	__attribute__((__optimize__("-fno-stack-protector")))
783 #endif
784 
785 /* Guard variables and structure members by lock. */
786 #define	__guarded_by(x)		__lock_annotate(guarded_by(x))
787 #define	__pt_guarded_by(x)	__lock_annotate(pt_guarded_by(x))
788 
789 /* Alignment builtins for better type checking and improved code generation. */
790 /* Provide fallback versions for other compilers (GCC/Clang < 10): */
791 #if !__has_builtin(__builtin_is_aligned)
792 #define __builtin_is_aligned(x, align)	\
793 	(((__uintptr_t)(x) & ((align) - 1)) == 0)
794 #endif
795 #if !__has_builtin(__builtin_align_up)
796 #define __builtin_align_up(x, align)	\
797 	((__typeof__(x))(((__uintptr_t)(x)+((align)-1))&(~((align)-1))))
798 #endif
799 #if !__has_builtin(__builtin_align_down)
800 #define __builtin_align_down(x, align)	\
801 	((__typeof__(x))((x)&(~((align)-1))))
802 #endif
803 
804 #define __align_up(x, y) __builtin_align_up(x, y)
805 #define __align_down(x, y) __builtin_align_down(x, y)
806 #define __is_aligned(x, y) __builtin_is_aligned(x, y)
807 
808 #endif /* !_SYS_CDEFS_H_ */
809