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 * Testing against Clang-specific extensions.
44 */
45#ifndef	__has_attribute
46#define	__has_attribute(x)	0
47#endif
48#ifndef	__has_extension
49#define	__has_extension		__has_feature
50#endif
51#ifndef	__has_feature
52#define	__has_feature(x)	0
53#endif
54#ifndef	__has_include
55#define	__has_include(x)	0
56#endif
57#ifndef	__has_builtin
58#define	__has_builtin(x)	0
59#endif
60
61#if defined(__cplusplus)
62#define	__BEGIN_DECLS	extern "C" {
63#define	__END_DECLS	}
64#else
65#define	__BEGIN_DECLS
66#define	__END_DECLS
67#endif
68
69/*
70 * This code has been put in place to help reduce the addition of
71 * compiler specific defines in FreeBSD code.  It helps to aid in
72 * having a compiler-agnostic source tree.
73 */
74
75/*
76 * Macro to test if we're using a specific version of gcc or later.
77 */
78#if defined(__GNUC__)
79#define	__GNUC_PREREQ__(ma, mi)	\
80	(__GNUC__ > (ma) || __GNUC__ == (ma) && __GNUC_MINOR__ >= (mi))
81#else
82#define	__GNUC_PREREQ__(ma, mi)	0
83#endif
84
85#if defined(__GNUC__)
86
87/*
88 * Compiler memory barriers, specific to gcc and clang.
89 */
90#define	__compiler_membar()	__asm __volatile(" " : : : "memory")
91
92#define	__CC_SUPPORTS___INLINE 1
93
94#endif /* __GNUC__ */
95
96/*
97 * The __CONCAT macro is used to concatenate parts of symbol names, e.g.
98 * with "#define OLD(foo) __CONCAT(old,foo)", OLD(foo) produces oldfoo.
99 * The __CONCAT macro is a bit tricky to use if it must work in non-ANSI
100 * mode -- there must be no spaces between its arguments, and for nested
101 * __CONCAT's, all the __CONCAT's must be at the left.  __CONCAT can also
102 * concatenate double-quoted strings produced by the __STRING macro, but
103 * this only works with ANSI C.
104 *
105 * __XSTRING is like __STRING, but it expands any macros in its argument
106 * first.  It is only available with ANSI C.
107 */
108#if defined(__STDC__) || defined(__cplusplus)
109#define	__P(protos)	protos		/* full-blown ANSI C */
110#define	__CONCAT1(x,y)	x ## y
111#define	__CONCAT(x,y)	__CONCAT1(x,y)
112#define	__STRING(x)	#x		/* stringify without expanding x */
113#define	__XSTRING(x)	__STRING(x)	/* expand x, then stringify */
114
115#define	__const		const		/* define reserved names to standard */
116#define	__signed	signed
117#define	__volatile	volatile
118#if defined(__cplusplus)
119#define	__inline	inline		/* convert to C++ keyword */
120#else
121#if !(defined(__CC_SUPPORTS___INLINE))
122#define	__inline			/* delete GCC keyword */
123#endif /* ! __CC_SUPPORTS___INLINE */
124#endif /* !__cplusplus */
125
126#else	/* !(__STDC__ || __cplusplus) */
127#define	__P(protos)	()		/* traditional C preprocessor */
128#define	__CONCAT(x,y)	x/**/y
129#define	__STRING(x)	"x"
130
131#if !defined(__CC_SUPPORTS___INLINE)
132#define	__const				/* delete pseudo-ANSI C keywords */
133#define	__inline
134#define	__signed
135#define	__volatile
136/*
137 * In non-ANSI C environments, new programs will want ANSI-only C keywords
138 * deleted from the program and old programs will want them left alone.
139 * When using a compiler other than gcc, programs using the ANSI C keywords
140 * const, inline etc. as normal identifiers should define -DNO_ANSI_KEYWORDS.
141 * When using "gcc -traditional", we assume that this is the intent; if
142 * __GNUC__ is defined but __STDC__ is not, we leave the new keywords alone.
143 */
144#ifndef	NO_ANSI_KEYWORDS
145#define	const				/* delete ANSI C keywords */
146#define	inline
147#define	signed
148#define	volatile
149#endif	/* !NO_ANSI_KEYWORDS */
150#endif	/* !__CC_SUPPORTS___INLINE */
151#endif	/* !(__STDC__ || __cplusplus) */
152
153/*
154 * Compiler-dependent macros to help declare dead (non-returning) and
155 * pure (no side effects) functions, and unused variables.  They are
156 * null except for versions of gcc that are known to support the features
157 * properly (old versions of gcc-2 supported the dead and pure features
158 * in a different (wrong) way).  If we do not provide an implementation
159 * for a given compiler, let the compile fail if it is told to use
160 * a feature that we cannot live without.
161 */
162#define	__weak_symbol	__attribute__((__weak__))
163#if !__GNUC_PREREQ__(2, 5)
164#define	__dead2
165#define	__pure2
166#define	__unused
167#endif
168#if __GNUC__ == 2 && __GNUC_MINOR__ >= 5 && __GNUC_MINOR__ < 7
169#define	__dead2		__attribute__((__noreturn__))
170#define	__pure2		__attribute__((__const__))
171#define	__unused
172/* XXX Find out what to do for __packed, __aligned and __section */
173#endif
174#if __GNUC_PREREQ__(2, 7)
175#define	__dead2		__attribute__((__noreturn__))
176#define	__pure2		__attribute__((__const__))
177#define	__unused	__attribute__((__unused__))
178#define	__used		__attribute__((__used__))
179#define	__packed	__attribute__((__packed__))
180#define	__aligned(x)	__attribute__((__aligned__(x)))
181#define	__section(x)	__attribute__((__section__(x)))
182#endif
183#if __GNUC_PREREQ__(4, 3) || __has_attribute(__alloc_size__)
184#define	__alloc_size(x)	__attribute__((__alloc_size__(x)))
185#define	__alloc_size2(n, x)	__attribute__((__alloc_size__(n, x)))
186#else
187#define	__alloc_size(x)
188#define	__alloc_size2(n, x)
189#endif
190#if __GNUC_PREREQ__(4, 9) || __has_attribute(__alloc_align__)
191#define	__alloc_align(x)	__attribute__((__alloc_align__(x)))
192#else
193#define	__alloc_align(x)
194#endif
195
196#if !__GNUC_PREREQ__(2, 95)
197#define	__alignof(x)	__offsetof(struct { char __a; x __b; }, __b)
198#endif
199
200/*
201 * Keywords added in C11.
202 */
203
204#if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L
205
206#if !__has_extension(c_alignas)
207#if (defined(__cplusplus) && __cplusplus >= 201103L) || \
208    __has_extension(cxx_alignas)
209#define	_Alignas(x)		alignas(x)
210#else
211/* XXX: Only emulates _Alignas(constant-expression); not _Alignas(type-name). */
212#define	_Alignas(x)		__aligned(x)
213#endif
214#endif
215
216#if defined(__cplusplus) && __cplusplus >= 201103L
217#define	_Alignof(x)		alignof(x)
218#else
219#define	_Alignof(x)		__alignof(x)
220#endif
221
222#if !defined(__cplusplus) && !__has_extension(c_atomic) && \
223	!__has_extension(cxx_atomic) && !__GNUC_PREREQ__(4, 7)
224/*
225 * No native support for _Atomic(). Place object in structure to prevent
226 * most forms of direct non-atomic access.
227 */
228#define	_Atomic(T)		struct { T volatile __val; }
229#endif
230
231#if defined(__cplusplus) && __cplusplus >= 201103L
232#define	_Noreturn		[[noreturn]]
233#else
234#define	_Noreturn		__dead2
235#endif
236
237#if !__has_extension(c_static_assert)
238#if (defined(__cplusplus) && __cplusplus >= 201103L) || \
239    __has_extension(cxx_static_assert)
240#define	_Static_assert(x, y)	static_assert(x, y)
241#elif __GNUC_PREREQ__(4,6) && !defined(__cplusplus)
242/* Nothing, gcc 4.6 and higher has _Static_assert built-in */
243#elif defined(__COUNTER__)
244#define	_Static_assert(x, y)	__Static_assert(x, __COUNTER__)
245#define	__Static_assert(x, y)	___Static_assert(x, y)
246#define	___Static_assert(x, y)	typedef char __assert_ ## y[(x) ? 1 : -1] \
247				__unused
248#else
249#define	_Static_assert(x, y)	struct __hack
250#endif
251#endif
252
253#if !__has_extension(c_thread_local)
254/*
255 * XXX: Some compilers (Clang 3.3, GCC 4.7) falsely announce C++11 mode
256 * without actually supporting the thread_local keyword. Don't check for
257 * the presence of C++11 when defining _Thread_local.
258 */
259#if /* (defined(__cplusplus) && __cplusplus >= 201103L) || */ \
260    __has_extension(cxx_thread_local)
261#define	_Thread_local		thread_local
262#else
263#define	_Thread_local		__thread
264#endif
265#endif
266
267#endif /* __STDC_VERSION__ || __STDC_VERSION__ < 201112L */
268
269/*
270 * Emulation of C11 _Generic().  Unlike the previously defined C11
271 * keywords, it is not possible to implement this using exactly the same
272 * syntax.  Therefore implement something similar under the name
273 * __generic().  Unlike _Generic(), this macro can only distinguish
274 * between a single type, so it requires nested invocations to
275 * distinguish multiple cases.
276 *
277 * Note that the comma operator is used to force expr to decay in
278 * order to match _Generic().
279 */
280
281#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) || \
282    __has_extension(c_generic_selections)
283#define	__generic(expr, t, yes, no)					\
284	_Generic(expr, t: yes, default: no)
285#elif __GNUC_PREREQ__(3, 1) && !defined(__cplusplus)
286#define	__generic(expr, t, yes, no)					\
287	__builtin_choose_expr(						\
288	    __builtin_types_compatible_p(__typeof((0, (expr))), t), yes, no)
289#endif
290
291/*
292 * C99 Static array indices in function parameter declarations.  Syntax such as:
293 * void bar(int myArray[static 10]);
294 * is allowed in C99 but not in C++.  Define __min_size appropriately so
295 * headers using it can be compiled in either language.  Use like this:
296 * void bar(int myArray[__min_size(10)]);
297 */
298#if !defined(__cplusplus) && \
299    (defined(__clang__) || __GNUC_PREREQ__(4, 6)) && \
300    (!defined(__STDC_VERSION__) || (__STDC_VERSION__ >= 199901))
301#define __min_size(x)	static (x)
302#else
303#define __min_size(x)	(x)
304#endif
305
306#if __GNUC_PREREQ__(2, 96)
307#define	__malloc_like	__attribute__((__malloc__))
308#define	__pure		__attribute__((__pure__))
309#else
310#define	__malloc_like
311#define	__pure
312#endif
313
314#if __GNUC_PREREQ__(3, 1)
315#define	__always_inline	__attribute__((__always_inline__))
316#else
317#define	__always_inline
318#endif
319
320#if __GNUC_PREREQ__(3, 1)
321#define	__noinline	__attribute__ ((__noinline__))
322#else
323#define	__noinline
324#endif
325
326#if __GNUC_PREREQ__(3, 4)
327#define	__fastcall	__attribute__((__fastcall__))
328#define	__result_use_check	__attribute__((__warn_unused_result__))
329#ifdef __clang__
330/*
331 * clang and gcc have different semantics for __warn_unused_result__: the latter
332 * does not permit the use of a void cast to suppress the warning.  Use
333 * __result_use_or_ignore_check in places where a void cast is acceptable.
334 * This can be implemented by [[nodiscard]] from C23.
335 */
336#define	__result_use_or_ignore_check	__result_use_check
337#else
338#define	__result_use_or_ignore_check
339#endif /* !__clang__ */
340#else
341#define	__fastcall
342#define	__result_use_check
343#endif
344
345#if __GNUC_PREREQ__(4, 1)
346#define	__returns_twice	__attribute__((__returns_twice__))
347#else
348#define	__returns_twice
349#endif
350
351#if __GNUC_PREREQ__(4, 6) || __has_builtin(__builtin_unreachable)
352#define	__unreachable()	__builtin_unreachable()
353#else
354#define	__unreachable()	((void)0)
355#endif
356
357#if (defined(__GNUC__) && __GNUC__ >= 2) && !defined(__STRICT_ANSI__) || __STDC_VERSION__ >= 199901
358#define	__LONG_LONG_SUPPORTED
359#endif
360
361/* C++11 exposes a load of C99 stuff */
362#if defined(__cplusplus) && __cplusplus >= 201103L
363#define	__LONG_LONG_SUPPORTED
364#ifndef	__STDC_LIMIT_MACROS
365#define	__STDC_LIMIT_MACROS
366#endif
367#ifndef	__STDC_CONSTANT_MACROS
368#define	__STDC_CONSTANT_MACROS
369#endif
370#endif
371
372/*
373 * noexcept keyword added in C++11.
374 */
375#if defined(__cplusplus) && __cplusplus >= 201103L
376#define __noexcept noexcept
377#define __noexcept_if(__c) noexcept(__c)
378#else
379#define __noexcept
380#define __noexcept_if(__c)
381#endif
382
383/*
384 * We use `__restrict' as a way to define the `restrict' type qualifier
385 * without disturbing older software that is unaware of C99 keywords.
386 * GCC also provides `__restrict' as an extension to support C99-style
387 * restricted pointers in other language modes.
388 */
389#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901
390#define	__restrict	restrict
391#elif !__GNUC_PREREQ__(2, 95)
392#define	__restrict
393#endif
394
395/*
396 * GNU C version 2.96 adds explicit branch prediction so that
397 * the CPU back-end can hint the processor and also so that
398 * code blocks can be reordered such that the predicted path
399 * sees a more linear flow, thus improving cache behavior, etc.
400 *
401 * The following two macros provide us with a way to utilize this
402 * compiler feature.  Use __predict_true() if you expect the expression
403 * to evaluate to true, and __predict_false() if you expect the
404 * expression to evaluate to false.
405 *
406 * A few notes about usage:
407 *
408 *	* Generally, __predict_false() error condition checks (unless
409 *	  you have some _strong_ reason to do otherwise, in which case
410 *	  document it), and/or __predict_true() `no-error' condition
411 *	  checks, assuming you want to optimize for the no-error case.
412 *
413 *	* Other than that, if you don't know the likelihood of a test
414 *	  succeeding from empirical or other `hard' evidence, don't
415 *	  make predictions.
416 *
417 *	* These are meant to be used in places that are run `a lot'.
418 *	  It is wasteful to make predictions in code that is run
419 *	  seldomly (e.g. at subsystem initialization time) as the
420 *	  basic block reordering that this affects can often generate
421 *	  larger code.
422 */
423#if __GNUC_PREREQ__(2, 96)
424#define	__predict_true(exp)     __builtin_expect((exp), 1)
425#define	__predict_false(exp)    __builtin_expect((exp), 0)
426#else
427#define	__predict_true(exp)     (exp)
428#define	__predict_false(exp)    (exp)
429#endif
430
431#if __GNUC_PREREQ__(4, 0)
432#define	__null_sentinel	__attribute__((__sentinel__))
433#define	__exported	__attribute__((__visibility__("default")))
434#define	__hidden	__attribute__((__visibility__("hidden")))
435#else
436#define	__null_sentinel
437#define	__exported
438#define	__hidden
439#endif
440
441/*
442 * We define this here since <stddef.h>, <sys/queue.h>, and <sys/types.h>
443 * require it.
444 */
445#if __GNUC_PREREQ__(4, 1)
446#define	__offsetof(type, field)	 __builtin_offsetof(type, field)
447#else
448#ifndef __cplusplus
449#define	__offsetof(type, field) \
450	((__size_t)(__uintptr_t)((const volatile void *)&((type *)0)->field))
451#else
452#define	__offsetof(type, field)					\
453  (__offsetof__ (reinterpret_cast <__size_t>			\
454                 (&reinterpret_cast <const volatile char &>	\
455                  (static_cast<type *> (0)->field))))
456#endif
457#endif
458#define	__rangeof(type, start, end) \
459	(__offsetof(type, end) - __offsetof(type, start))
460
461/*
462 * Given the pointer x to the member m of the struct s, return
463 * a pointer to the containing structure.  When using GCC, we first
464 * assign pointer x to a local variable, to check that its type is
465 * compatible with member m.
466 */
467#if __GNUC_PREREQ__(3, 1)
468#define	__containerof(x, s, m) ({					\
469	const volatile __typeof(((s *)0)->m) *__x = (x);		\
470	__DEQUALIFY(s *, (const volatile char *)__x - __offsetof(s, m));\
471})
472#else
473#define	__containerof(x, s, m)						\
474	__DEQUALIFY(s *, (const volatile char *)(x) - __offsetof(s, m))
475#endif
476
477/*
478 * Compiler-dependent macros to declare that functions take printf-like
479 * or scanf-like arguments.  They are null except for versions of gcc
480 * that are known to support the features properly (old versions of gcc-2
481 * didn't permit keeping the keywords out of the application namespace).
482 */
483#if !__GNUC_PREREQ__(2, 7)
484#define	__printflike(fmtarg, firstvararg)
485#define	__scanflike(fmtarg, firstvararg)
486#define	__format_arg(fmtarg)
487#define	__strfmonlike(fmtarg, firstvararg)
488#define	__strftimelike(fmtarg, firstvararg)
489#else
490#define	__printflike(fmtarg, firstvararg) \
491	    __attribute__((__format__ (__printf__, fmtarg, firstvararg)))
492#define	__scanflike(fmtarg, firstvararg) \
493	    __attribute__((__format__ (__scanf__, fmtarg, firstvararg)))
494#define	__format_arg(fmtarg)	__attribute__((__format_arg__ (fmtarg)))
495#define	__strfmonlike(fmtarg, firstvararg) \
496	    __attribute__((__format__ (__strfmon__, fmtarg, firstvararg)))
497#define	__strftimelike(fmtarg, firstvararg) \
498	    __attribute__((__format__ (__strftime__, fmtarg, firstvararg)))
499#endif
500
501/* Compiler-dependent macros that rely on FreeBSD-specific extensions. */
502#if defined(__FreeBSD_cc_version) && __FreeBSD_cc_version >= 300001 && \
503    defined(__GNUC__)
504#define	__printf0like(fmtarg, firstvararg) \
505	    __attribute__((__format__ (__printf0__, fmtarg, firstvararg)))
506#else
507#define	__printf0like(fmtarg, firstvararg)
508#endif
509
510#if defined(__GNUC__)
511#define	__strong_reference(sym,aliassym)	\
512	extern __typeof (sym) aliassym __attribute__ ((__alias__ (#sym)))
513#ifdef __STDC__
514#define	__weak_reference(sym,alias)	\
515	__asm__(".weak " #alias);	\
516	__asm__(".equ "  #alias ", " #sym)
517#define	__warn_references(sym,msg)	\
518	__asm__(".section .gnu.warning." #sym);	\
519	__asm__(".asciz \"" msg "\"");	\
520	__asm__(".previous")
521#define	__sym_compat(sym,impl,verid)	\
522	__asm__(".symver " #impl ", " #sym "@" #verid)
523#define	__sym_default(sym,impl,verid)	\
524	__asm__(".symver " #impl ", " #sym "@@@" #verid)
525#else
526#define	__weak_reference(sym,alias)	\
527	__asm__(".weak alias");		\
528	__asm__(".equ alias, sym")
529#define	__warn_references(sym,msg)	\
530	__asm__(".section .gnu.warning.sym"); \
531	__asm__(".asciz \"msg\"");	\
532	__asm__(".previous")
533#define	__sym_compat(sym,impl,verid)	\
534	__asm__(".symver impl, sym@verid")
535#define	__sym_default(impl,sym,verid)	\
536	__asm__(".symver impl, sym@@@verid")
537#endif	/* __STDC__ */
538#endif	/* __GNUC__ */
539
540#define	__GLOBL(sym)	__asm__(".globl " __XSTRING(sym))
541#define	__WEAK(sym)	__asm__(".weak " __XSTRING(sym))
542
543#if defined(__GNUC__)
544#define	__IDSTRING(name,string)	__asm__(".ident\t\"" string "\"")
545#else
546/*
547 * The following definition might not work well if used in header files,
548 * but it should be better than nothing.  If you want a "do nothing"
549 * version, then it should generate some harmless declaration, such as:
550 *    #define	__IDSTRING(name,string)	struct __hack
551 */
552#define	__IDSTRING(name,string)	static const char name[] __unused = string
553#endif
554
555/*
556 * Embed the rcs id of a source file in the resulting library.  Note that in
557 * more recent ELF binutils, we use .ident allowing the ID to be stripped.
558 * Usage:
559 */
560#ifndef	__FBSDID
561#if !defined(STRIP_FBSDID)
562#define	__FBSDID(s)	__IDSTRING(__CONCAT(__rcsid_,__LINE__),s)
563#else
564#define	__FBSDID(s)	struct __hack
565#endif
566#endif
567
568#ifndef	__RCSID
569#ifndef	NO__RCSID
570#define	__RCSID(s)	__IDSTRING(__CONCAT(__rcsid_,__LINE__),s)
571#else
572#define	__RCSID(s)	struct __hack
573#endif
574#endif
575
576#ifndef	__RCSID_SOURCE
577#ifndef	NO__RCSID_SOURCE
578#define	__RCSID_SOURCE(s)	__IDSTRING(__CONCAT(__rcsid_source_,__LINE__),s)
579#else
580#define	__RCSID_SOURCE(s)	struct __hack
581#endif
582#endif
583
584#ifndef	__SCCSID
585#ifndef	NO__SCCSID
586#define	__SCCSID(s)	__IDSTRING(__CONCAT(__sccsid_,__LINE__),s)
587#else
588#define	__SCCSID(s)	struct __hack
589#endif
590#endif
591
592#ifndef	__COPYRIGHT
593#ifndef	NO__COPYRIGHT
594#define	__COPYRIGHT(s)	__IDSTRING(__CONCAT(__copyright_,__LINE__),s)
595#else
596#define	__COPYRIGHT(s)	struct __hack
597#endif
598#endif
599
600#ifndef	__DECONST
601#define	__DECONST(type, var)	((type)(__uintptr_t)(const void *)(var))
602#endif
603
604#ifndef	__DEVOLATILE
605#define	__DEVOLATILE(type, var)	((type)(__uintptr_t)(volatile void *)(var))
606#endif
607
608#ifndef	__DEQUALIFY
609#define	__DEQUALIFY(type, var)	((type)(__uintptr_t)(const volatile void *)(var))
610#endif
611
612#if !defined(_STANDALONE) && !defined(_KERNEL)
613#if defined(__GNUC__) || defined(__PCC__)
614#define	__RENAME(x)	__asm(__STRING(x))
615#else
616#define	__RENAME(x)	no renaming support for compiler in use
617#endif /* __GNUC__ */
618#else /* _STANDALONE || _KERNEL */
619#define	__RENAME(x)	no renaming in kernel/standalone environment
620#endif
621
622/*-
623 * The following definitions are an extension of the behavior originally
624 * implemented in <sys/_posix.h>, but with a different level of granularity.
625 * POSIX.1 requires that the macros we test be defined before any standard
626 * header file is included.
627 *
628 * Here's a quick run-down of the versions (and some informal names)
629 *  defined(_POSIX_SOURCE)		1003.1-1988
630 *					encoded as 198808 below
631 *  _POSIX_C_SOURCE == 1		1003.1-1990
632 *					encoded as 199009 below
633 *  _POSIX_C_SOURCE == 2		1003.2-1992 C Language Binding Option
634 *					encoded as 199209 below
635 *  _POSIX_C_SOURCE == 199309		1003.1b-1993
636 *					(1003.1 Issue 4, Single Unix Spec v1, Unix 93)
637 *  _POSIX_C_SOURCE == 199506		1003.1c-1995, 1003.1i-1995,
638 *					and the omnibus ISO/IEC 9945-1: 1996
639 *					(1003.1 Issue 5, Single	Unix Spec v2, Unix 95)
640 *  _POSIX_C_SOURCE == 200112		1003.1-2001 (1003.1 Issue 6, Unix 03)
641 *  _POSIX_C_SOURCE == 200809		1003.1-2008 (1003.1 Issue 7)
642 *					IEEE Std 1003.1-2017 (Rev of 1003.1-2008) is
643 *					1003.1-2008 with two TCs applied with
644 *					_POSIX_C_SOURCE=200809 and _XOPEN_SOURCE=700
645 *
646 * In addition, the X/Open Portability Guide, which is now the Single UNIX
647 * Specification, defines a feature-test macro which indicates the version of
648 * that specification, and which subsumes _POSIX_C_SOURCE.
649 *
650 * Our macros begin with two underscores to avoid namespace screwage.
651 */
652
653/* Deal with IEEE Std. 1003.1-1990, in which _POSIX_C_SOURCE == 1. */
654#if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE == 1
655#undef _POSIX_C_SOURCE		/* Probably illegal, but beyond caring now. */
656#define	_POSIX_C_SOURCE		199009
657#endif
658
659/* Deal with IEEE Std. 1003.2-1992, in which _POSIX_C_SOURCE == 2. */
660#if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE == 2
661#undef _POSIX_C_SOURCE
662#define	_POSIX_C_SOURCE		199209
663#endif
664
665/* Deal with various X/Open Portability Guides and Single UNIX Spec. */
666#ifdef _XOPEN_SOURCE
667#if _XOPEN_SOURCE - 0 >= 700
668#define	__XSI_VISIBLE		700
669#undef _POSIX_C_SOURCE
670#define	_POSIX_C_SOURCE		200809
671#elif _XOPEN_SOURCE - 0 >= 600
672#define	__XSI_VISIBLE		600
673#undef _POSIX_C_SOURCE
674#define	_POSIX_C_SOURCE		200112
675#elif _XOPEN_SOURCE - 0 >= 500
676#define	__XSI_VISIBLE		500
677#undef _POSIX_C_SOURCE
678#define	_POSIX_C_SOURCE		199506
679#endif
680#endif
681
682/*
683 * Deal with all versions of POSIX.  The ordering relative to the tests above is
684 * important.
685 */
686#if defined(_POSIX_SOURCE) && !defined(_POSIX_C_SOURCE)
687#define	_POSIX_C_SOURCE		198808
688#endif
689#ifdef _POSIX_C_SOURCE
690#if _POSIX_C_SOURCE >= 200809
691#define	__POSIX_VISIBLE		200809
692#define	__ISO_C_VISIBLE		1999
693#elif _POSIX_C_SOURCE >= 200112
694#define	__POSIX_VISIBLE		200112
695#define	__ISO_C_VISIBLE		1999
696#elif _POSIX_C_SOURCE >= 199506
697#define	__POSIX_VISIBLE		199506
698#define	__ISO_C_VISIBLE		1990
699#elif _POSIX_C_SOURCE >= 199309
700#define	__POSIX_VISIBLE		199309
701#define	__ISO_C_VISIBLE		1990
702#elif _POSIX_C_SOURCE >= 199209
703#define	__POSIX_VISIBLE		199209
704#define	__ISO_C_VISIBLE		1990
705#elif _POSIX_C_SOURCE >= 199009
706#define	__POSIX_VISIBLE		199009
707#define	__ISO_C_VISIBLE		1990
708#else
709#define	__POSIX_VISIBLE		198808
710#define	__ISO_C_VISIBLE		0
711#endif /* _POSIX_C_SOURCE */
712/*
713 * Both glibc and OpenBSD enable c11 features when _ISOC11_SOURCE is defined, or
714 * when compiling with -stdc=c11. A strict reading of the standard would suggest
715 * doing it only for the former. However, a strict reading also requires C99
716 * mode only, so building with C11 is already undefined. Follow glibc's and
717 * OpenBSD's lead for this non-standard configuration for maximum compatibility.
718 */
719#if _ISOC11_SOURCE || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L)
720#undef __ISO_C_VISIBLE
721#define __ISO_C_VISIBLE		2011
722#endif
723#else
724/*-
725 * Deal with _ANSI_SOURCE:
726 * If it is defined, and no other compilation environment is explicitly
727 * requested, then define our internal feature-test macros to zero.  This
728 * makes no difference to the preprocessor (undefined symbols in preprocessing
729 * expressions are defined to have value zero), but makes it more convenient for
730 * a test program to print out the values.
731 *
732 * If a program mistakenly defines _ANSI_SOURCE and some other macro such as
733 * _POSIX_C_SOURCE, we will assume that it wants the broader compilation
734 * environment (and in fact we will never get here).
735 */
736#if defined(_ANSI_SOURCE)	/* Hide almost everything. */
737#define	__POSIX_VISIBLE		0
738#define	__XSI_VISIBLE		0
739#define	__BSD_VISIBLE		0
740#define	__ISO_C_VISIBLE		1990
741#define	__EXT1_VISIBLE		0
742#elif defined(_C99_SOURCE)	/* Localism to specify strict C99 env. */
743#define	__POSIX_VISIBLE		0
744#define	__XSI_VISIBLE		0
745#define	__BSD_VISIBLE		0
746#define	__ISO_C_VISIBLE		1999
747#define	__EXT1_VISIBLE		0
748#elif defined(_C11_SOURCE)	/* Localism to specify strict C11 env. */
749#define	__POSIX_VISIBLE		0
750#define	__XSI_VISIBLE		0
751#define	__BSD_VISIBLE		0
752#define	__ISO_C_VISIBLE		2011
753#define	__EXT1_VISIBLE		0
754#else				/* Default environment: show everything. */
755#define	__POSIX_VISIBLE		200809
756#define	__XSI_VISIBLE		700
757#define	__BSD_VISIBLE		1
758#define	__ISO_C_VISIBLE		2011
759#define	__EXT1_VISIBLE		1
760#endif
761#endif
762
763/* User override __EXT1_VISIBLE */
764#if defined(__STDC_WANT_LIB_EXT1__)
765#undef	__EXT1_VISIBLE
766#if __STDC_WANT_LIB_EXT1__
767#define	__EXT1_VISIBLE		1
768#else
769#define	__EXT1_VISIBLE		0
770#endif
771#endif /* __STDC_WANT_LIB_EXT1__ */
772
773/*
774 * Old versions of GCC use non-standard ARM arch symbols; acle-compat.h
775 * translates them to __ARM_ARCH and the modern feature symbols defined by ARM.
776 */
777#if defined(__arm__) && !defined(__ARM_ARCH)
778#include <machine/acle-compat.h>
779#endif
780
781/*
782 * Nullability qualifiers: currently only supported by Clang.
783 */
784#if !(defined(__clang__) && __has_feature(nullability))
785#define	_Nonnull
786#define	_Nullable
787#define	_Null_unspecified
788#define	__NULLABILITY_PRAGMA_PUSH
789#define	__NULLABILITY_PRAGMA_POP
790#else
791#define	__NULLABILITY_PRAGMA_PUSH _Pragma("clang diagnostic push")	\
792	_Pragma("clang diagnostic ignored \"-Wnullability-completeness\"")
793#define	__NULLABILITY_PRAGMA_POP _Pragma("clang diagnostic pop")
794#endif
795
796/*
797 * Type Safety Checking
798 *
799 * Clang provides additional attributes to enable checking type safety
800 * properties that cannot be enforced by the C type system.
801 */
802
803#if __has_attribute(__argument_with_type_tag__) && \
804    __has_attribute(__type_tag_for_datatype__)
805#define	__arg_type_tag(arg_kind, arg_idx, type_tag_idx) \
806	    __attribute__((__argument_with_type_tag__(arg_kind, arg_idx, type_tag_idx)))
807#define	__datatype_type_tag(kind, type) \
808	    __attribute__((__type_tag_for_datatype__(kind, type)))
809#else
810#define	__arg_type_tag(arg_kind, arg_idx, type_tag_idx)
811#define	__datatype_type_tag(kind, type)
812#endif
813
814/*
815 * Lock annotations.
816 *
817 * Clang provides support for doing basic thread-safety tests at
818 * compile-time, by marking which locks will/should be held when
819 * entering/leaving a functions.
820 *
821 * Furthermore, it is also possible to annotate variables and structure
822 * members to enforce that they are only accessed when certain locks are
823 * held.
824 */
825
826#if __has_extension(c_thread_safety_attributes)
827#define	__lock_annotate(x)	__attribute__((x))
828#else
829#define	__lock_annotate(x)
830#endif
831
832/* Structure implements a lock. */
833#define	__lockable		__lock_annotate(lockable)
834
835/* Function acquires an exclusive or shared lock. */
836#define	__locks_exclusive(...) \
837	__lock_annotate(exclusive_lock_function(__VA_ARGS__))
838#define	__locks_shared(...) \
839	__lock_annotate(shared_lock_function(__VA_ARGS__))
840
841/* Function attempts to acquire an exclusive or shared lock. */
842#define	__trylocks_exclusive(...) \
843	__lock_annotate(exclusive_trylock_function(__VA_ARGS__))
844#define	__trylocks_shared(...) \
845	__lock_annotate(shared_trylock_function(__VA_ARGS__))
846
847/* Function releases a lock. */
848#define	__unlocks(...)		__lock_annotate(unlock_function(__VA_ARGS__))
849
850/* Function asserts that an exclusive or shared lock is held. */
851#define	__asserts_exclusive(...) \
852	__lock_annotate(assert_exclusive_lock(__VA_ARGS__))
853#define	__asserts_shared(...) \
854	__lock_annotate(assert_shared_lock(__VA_ARGS__))
855
856/* Function requires that an exclusive or shared lock is or is not held. */
857#define	__requires_exclusive(...) \
858	__lock_annotate(exclusive_locks_required(__VA_ARGS__))
859#define	__requires_shared(...) \
860	__lock_annotate(shared_locks_required(__VA_ARGS__))
861#define	__requires_unlocked(...) \
862	__lock_annotate(locks_excluded(__VA_ARGS__))
863
864/* Function should not be analyzed. */
865#define	__no_lock_analysis	__lock_annotate(no_thread_safety_analysis)
866
867/*
868 * Function or variable should not be sanitized, e.g., by AddressSanitizer.
869 * GCC has the nosanitize attribute, but as a function attribute only, and
870 * warns on use as a variable attribute.
871 */
872#if __has_attribute(no_sanitize) && defined(__clang__)
873#ifdef _KERNEL
874#define __nosanitizeaddress	__attribute__((no_sanitize("kernel-address")))
875#define __nosanitizememory	__attribute__((no_sanitize("kernel-memory")))
876#else
877#define __nosanitizeaddress	__attribute__((no_sanitize("address")))
878#define __nosanitizememory	__attribute__((no_sanitize("memory")))
879#endif
880#define __nosanitizethread	__attribute__((no_sanitize("thread")))
881#else
882#define __nosanitizeaddress
883#define __nosanitizememory
884#define __nosanitizethread
885#endif
886
887/*
888 * Make it possible to opt out of stack smashing protection.
889 */
890#if __has_attribute(no_stack_protector)
891#define	__nostackprotector	__attribute__((no_stack_protector))
892#else
893#define	__nostackprotector	\
894	__attribute__((__optimize__("-fno-stack-protector")))
895#endif
896
897/* Guard variables and structure members by lock. */
898#define	__guarded_by(x)		__lock_annotate(guarded_by(x))
899#define	__pt_guarded_by(x)	__lock_annotate(pt_guarded_by(x))
900
901/* Alignment builtins for better type checking and improved code generation. */
902/* Provide fallback versions for other compilers (GCC/Clang < 10): */
903#if !__has_builtin(__builtin_is_aligned)
904#define __builtin_is_aligned(x, align)	\
905	(((__uintptr_t)(x) & ((align) - 1)) == 0)
906#endif
907#if !__has_builtin(__builtin_align_up)
908#define __builtin_align_up(x, align)	\
909	((__typeof__(x))(((__uintptr_t)(x)+((align)-1))&(~((align)-1))))
910#endif
911#if !__has_builtin(__builtin_align_down)
912#define __builtin_align_down(x, align)	\
913	((__typeof__(x))((x)&(~((align)-1))))
914#endif
915
916#define __align_up(x, y) __builtin_align_up(x, y)
917#define __align_down(x, y) __builtin_align_down(x, y)
918#define __is_aligned(x, y) __builtin_is_aligned(x, y)
919
920#endif /* !_SYS_CDEFS_H_ */
921