xref: /dragonfly/sys/sys/cdefs.h (revision 25b141f1)
1 /*
2  * Copyright (c) 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Berkeley Software Design, Inc.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)cdefs.h	8.8 (Berkeley) 1/9/95
33  * $FreeBSD: src/sys/sys/cdefs.h,v 1.28.2.8 2002/09/18 04:05:13 mikeh Exp $
34  */
35 
36 #ifndef	_SYS_CDEFS_H_
37 #define	_SYS_CDEFS_H_
38 
39 /*
40  * Testing against Clang-specific extensions.
41  */
42 #ifndef	__has_attribute
43 #define	__has_attribute(x)	0
44 #endif
45 #ifndef	__has_extension
46 #define	__has_extension		__has_feature
47 #endif
48 #ifndef	__has_feature
49 #define	__has_feature(x)	0
50 #endif
51 #ifndef	__has_include
52 #define	__has_include(x)	0
53 #endif
54 #ifndef	__has_builtin
55 #define	__has_builtin(x)	0
56 #endif
57 
58 /*
59  * Macro to test if we are using a specific version of gcc or later.
60  */
61 #if defined(__GNUC__)
62 #define	__GNUC_PREREQ__(ma, mi)	\
63         (__GNUC__ > (ma) || __GNUC__ == (ma) && __GNUC_MINOR__ >= (mi))
64 #else
65 #define	__GNUC_PREREQ__(ma, mi) 0
66 #endif
67 
68 #if defined(__cplusplus)
69 #define	__BEGIN_EXTERN_C	extern "C" {
70 #define	__END_EXTERN_C		}
71 #else
72 #define	__BEGIN_DECLS
73 #define	__END_DECLS
74 #define	__BEGIN_EXTERN_C
75 #define	__END_EXTERN_C
76 #endif
77 
78 #if __GNUC_PREREQ__(4, 0)
79 #define	__dso_public	__attribute__((__visibility__("default")))
80 #define	__dso_hidden	__attribute__((__visibility__("hidden")))
81 #define	__BEGIN_PUBLIC_DECLS \
82 			_Pragma("GCC visibility push(default)") __BEGIN_EXTERN_C
83 #define	__END_PUBLIC_DECLS \
84 			__END_EXTERN_C _Pragma("GCC visibility pop")
85 #define	__BEGIN_HIDDEN_DECLS \
86 			_Pragma("GCC visibility push(hidden)") __BEGIN_EXTERN_C
87 #define	__END_HIDDEN_DECLS \
88 			__END_EXTERN_C _Pragma("GCC visibility pop")
89 #else
90 #define	__dso_public
91 #define	__dso_hidden
92 #define	__BEGIN_PUBLIC_DECLS \
93 			__BEGIN_EXTERN_C
94 #define	__END_PUBLIC_DECLS \
95 			__END_EXTERN_C
96 #define	__BEGIN_HIDDEN_DECLS \
97 			__BEGIN_EXTERN_C
98 #define	__END_HIDDEN_DECLS \
99 			__END_EXTERN_C
100 #endif
101 
102 #if defined(__cplusplus)
103 #define	__BEGIN_DECLS	__BEGIN_PUBLIC_DECLS
104 #define	__END_DECLS	__END_PUBLIC_DECLS
105 #endif
106 
107 /*
108  * The __VM_CACHELINE_SIZE macro defines the common cache line alignment
109  * size that can be found across most recent and somewhat latest Intel
110  * hardware, i.e. L1 cache sizes etc.
111  *
112  * If needed, this value can be TUNED.  Suitable values for this macro
113  * are 32, 64 and 128 bytes.  The unit of measurement for this macro is
114  * bytes.
115  *
116  * XXX: This macro and related macros will eventually move to a MD
117  * header, but currently, we do need such a hierarchy.
118  */
119 #define	__VM_CACHELINE_SIZE	64
120 #define	__VM_CACHELINE_MASK	(__VM_CACHELINE_SIZE - 1)
121 #define	__VM_CACHELINE_ALIGN(n)	\
122 	(((n) + __VM_CACHELINE_MASK) & ~__VM_CACHELINE_MASK)
123 
124 /*
125  * The __CONCAT macro is used to concatenate parts of symbol names, e.g.
126  * with "#define OLD(foo) __CONCAT(old,foo)", OLD(foo) produces oldfoo.
127  * The __CONCAT macro is a bit tricky to use if it must work in non-ANSI
128  * mode -- there must be no spaces between its arguments, and for nested
129  * __CONCAT's, all the __CONCAT's must be at the left.  __CONCAT can also
130  * concatenate double-quoted strings produced by the __STRING macro, but
131  * this only works with ANSI C.
132  *
133  * __XSTRING is like __STRING, but it expands any macros in its argument
134  * first.  It is only available with ANSI C.
135  */
136 #if defined(__STDC__) || defined(__cplusplus)
137 #define	__P(protos)	protos		/* full-blown ANSI C */
138 #define	__CONCAT1(x,y)	x ## y
139 #define	__CONCAT(x,y)	__CONCAT1(x,y)
140 #define	__STRING(x)	#x		/* stringify without expanding x */
141 #define	__XSTRING(x)	__STRING(x)	/* expand x, then stringify */
142 
143 #define	__const		const		/* define reserved names to standard */
144 #define	__signed	signed
145 #define	__volatile	volatile
146 #if defined(__cplusplus)
147 #define	__inline	inline		/* convert to C++ keyword */
148 #else
149 #ifndef __GNUC__
150 #define	__inline			/* delete GCC keyword */
151 #endif /* !__GNUC__ */
152 #endif /* __cplusplus */
153 
154 #else	/* !(__STDC__ || __cplusplus) */
155 #define	__P(protos)	()		/* traditional C preprocessor */
156 #define	__CONCAT(x,y)	x/**/y
157 #define	__STRING(x)	"x"
158 
159 #ifndef __GNUC__
160 #define	__const				/* delete pseudo-ANSI C keywords */
161 #define	__inline
162 #define	__signed
163 #define	__volatile
164 /*
165  * In non-ANSI C environments, new programs will want ANSI-only C keywords
166  * deleted from the program and old programs will want them left alone.
167  * When using a compiler other than gcc, programs using the ANSI C keywords
168  * const, inline etc. as normal identifiers should define -DNO_ANSI_KEYWORDS.
169  * When using "gcc -traditional", we assume that this is the intent; if
170  * __GNUC__ is defined but __STDC__ is not, we leave the new keywords alone.
171  */
172 #ifndef	NO_ANSI_KEYWORDS
173 #define	const				/* delete ANSI C keywords */
174 #define	inline
175 #define	signed
176 #define	volatile
177 #endif	/* !NO_ANSI_KEYWORDS */
178 #endif	/* !__GNUC__ */
179 #endif	/* __STDC__ || __cplusplus */
180 
181 /*
182  * Compiler-dependent macros to help declare dead (non-returning) and
183  * pure (no side effects) functions, and unused variables.  They are
184  * null except for versions of gcc that are known to support the features
185  * properly (old versions of gcc-2 supported the dead and pure features
186  * in a different (wrong) way).
187  */
188 #define	__weak_symbol	__attribute__((__weak__))
189 #if __GNUC_PREREQ__(2, 7)
190 #define	__dead2		__attribute__((__noreturn__))
191 #define	__pure2		__attribute__((__const__))
192 #define	__unused	__attribute__((__unused__))
193 #define	__packed	__attribute__((__packed__))
194 #define	__aligned(x)	__attribute__((__aligned__(x)))
195 #define	__section(x)	__attribute__((__section__(x)))
196 #else
197 #define	__dead2
198 #define	__pure2
199 #define	__unused
200 #endif
201 
202 #if __GNUC_PREREQ__(2, 96)
203 #define	__malloclike	__attribute__((__malloc__))
204 #define	__pure		__attribute__((__pure__))
205 #else
206 #define	__malloclike
207 #define	__pure		__pure2
208 #endif
209 
210 #if __GNUC_PREREQ__(3, 1)
211 #define	__always_inline	__attribute__((__always_inline__))
212 #define	__noinline	__attribute__((__noinline__))
213 #else
214 #define	__always_inline
215 #define	__noinline
216 #endif
217 
218 #if __GNUC_PREREQ__(3, 3)
219 #define	__nonnull(...)	__attribute__((__nonnull__(__VA_ARGS__)))
220 #define	__used		__attribute__((__used__))
221 #else
222 #define	__nonnull(...)
223 #define	__used		__unused
224 #endif
225 
226 #if __GNUC_PREREQ__(3, 4)
227 #define	__heedresult	__attribute__((__warn_unused_result__))
228 #else
229 #define	__heedresult
230 #endif
231 
232 #if __GNUC_PREREQ__(4, 1)
233 #define	__returns_twice	__attribute__((__returns_twice__))
234 #else
235 #define	__returns_twice
236 #endif
237 
238 #if __GNUC_PREREQ__(4, 6) || __has_builtin(__builtin_unreachable)
239 #define	__unreachable()	__builtin_unreachable()
240 #else
241 #define	__unreachable()	((void)0)
242 #endif
243 
244 #if __GNUC_PREREQ__(4, 3) || __has_attribute(__alloc_size__)
245 #define	__alloc_size(x)		__attribute__((__alloc_size__(x)))
246 #define	__alloc_size2(n, x)	__attribute__((__alloc_size__(n, x)))
247 #else
248 #define	__alloc_size(x)
249 #define	__alloc_size2(n, x)
250 #endif
251 
252 #if __GNUC_PREREQ__(4, 9) || __has_attribute(__alloc_align__)
253 #define	__alloc_align(x)	__attribute__((__alloc_align__(x)))
254 #else
255 #define	__alloc_align(x)
256 #endif
257 
258 #if !__GNUC_PREREQ__(2, 7) && __STDC_VERSION__ < 199901
259 #define	__func__	NULL
260 #endif
261 
262 #if (__GNUC_PREREQ__(2, 0) && !defined(__STRICT_ANSI__)) || \
263     __STDC_VERSION__ >= 199901
264 #define	__LONG_LONG_SUPPORTED
265 #endif
266 
267 /* C++11 exposes a load of C99 stuff */
268 #if defined(__cplusplus) && __cplusplus >= 201103L
269 #define	__LONG_LONG_SUPPORTED
270 #ifndef	__STDC_LIMIT_MACROS
271 #define	__STDC_LIMIT_MACROS
272 #endif
273 #ifndef	__STDC_CONSTANT_MACROS
274 #define	__STDC_CONSTANT_MACROS
275 #endif
276 #endif
277 
278 /*
279  * GCC 2.95 and later provides `__restrict' as an extension to C90 to support
280  * the C99-specific `restrict' type qualifier.  We happen to use `__restrict'
281  * as a way to define the `restrict' type qualifier without disturbing older
282  * software that is unaware of C99 keywords.
283  */
284 #if !__GNUC_PREREQ__(2, 95)
285 #if __STDC_VERSION__ < 199901
286 #define	__restrict
287 #else
288 #define	__restrict	restrict
289 #endif
290 #endif
291 
292 /*
293  * C99 allows to declare arrays as non-overlapping.
294  */
295 #if __GNUC_PREREQ__(3, 1) && !defined(__GNUG__)
296 #define	__restrict_arr	__restrict
297 #else
298 #ifdef __GNUC__
299 #define	__restrict_arr
300 #else
301 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901
302 #define	__restrict_arr	restrict
303 #else
304 #define	__restrict_arr
305 #endif
306 #endif
307 #endif
308 
309 /*
310  * C99 allows the 'static' keyword in array declarators of function
311  * parameter lists, such as:
312  *	void bar(int myArray[static 10]);
313  * However, this is allowed in C++, so define this __min_size() macro
314  * to make headers usable in both languages.
315  */
316 #if !defined(__cplusplus) && \
317     (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901)
318 #define	__min_size(x)	static (x)
319 #else
320 #define	__min_size(x)	(x)
321 #endif
322 
323 /*
324  * GNU C version 2.96 adds explicit branch prediction so that
325  * the CPU back-end can hint the processor and also so that
326  * code blocks can be reordered such that the predicted path
327  * sees a more linear flow, thus improving cache behavior, etc.
328  *
329  * The following two macros provide us with a way to utilize this
330  * compiler feature.  Use __predict_true() if you expect the expression
331  * to evaluate to true, and __predict_false() if you expect the
332  * expression to evaluate to false.
333  *
334  * A few notes about usage:
335  *
336  *	* Generally, __predict_false() error condition checks (unless
337  *	  you have some _strong_ reason to do otherwise, in which case
338  *	  document it), and/or __predict_true() `no-error' condition
339  *	  checks, assuming you want to optimize for the no-error case.
340  *
341  *	* Other than that, if you don't know the likelihood of a test
342  *	  succeeding from empirical or other `hard' evidence, don't
343  *	  make predictions.
344  *
345  *	* These are meant to be used in places that are run `a lot'.
346  *	  It is wasteful to make predictions in code that is run
347  *	  seldomly (e.g. at subsystem initialization time) as the
348  *	  basic block reordering that this affects can often generate
349  *	  larger code.
350  */
351 #if __GNUC_PREREQ__(2, 96)
352 #define	__predict_true(exp)     __builtin_expect((exp), 1)
353 #define	__predict_false(exp)    __builtin_expect((exp), 0)
354 #else
355 #define	__predict_true(exp)     (exp)
356 #define	__predict_false(exp)    (exp)
357 #endif
358 
359 /*
360  * We define this here since <stddef.h>, <sys/queue.h>, and <sys/types.h>
361  * require it.
362  */
363 #if __GNUC_PREREQ__(4, 1)
364 #define	__offsetof(type, field)	__builtin_offsetof(type, field)
365 #else
366 #ifndef __cplusplus
367 #define	__offsetof(type, field) \
368 	((__size_t)(__uintptr_t)((const volatile void *)&((type *)0)->field))
369 #else
370 #define	__offsetof(type, field)					\
371 	(__offsetof__ (reinterpret_cast <__size_t>		\
372 		 (&reinterpret_cast <const volatile char &>	\
373 		  (static_cast<type *> (0)->field))))
374 #endif
375 #endif
376 
377 /*
378  * Given the pointer x to the member m of the struct s, return a pointer to
379  * the containing structure.  When using GCC, we first assign pointer x to
380  * a local variable, to check that its type is compatible with member m.
381  */
382 #if __GNUC_PREREQ__(3, 1)
383 #define	__containerof(x, s, m) ({					\
384 	const volatile __typeof(((s *)NULL)->m) *__x = (x);		\
385 	__DEQUALIFY(s *, (const volatile char *)__x - __offsetof(s, m)); \
386 })
387 #else
388 #define	__containerof(x, s, m)						\
389 	__DEQUALIFY(s *, (const volatile char *)(x) - __offsetof(s, m))
390 #endif
391 
392 /*
393  * Compiler-dependent macros to declare that functions take printf-like
394  * or scanf-like arguments.  They are null except for versions of gcc
395  * that are known to support the features properly (old versions of gcc-2
396  * didn't permit keeping the keywords out of the application namespace).
397  *
398  * The printf0like macro for GCC 2 uses DragonFly specific compiler extensions.
399  */
400 #if !__GNUC_PREREQ__(2, 7)
401 #define	__printflike(fmtarg, firstvararg)
402 #define	__scanflike(fmtarg, firstvararg)
403 #define	__printf0like(fmtarg, firstvararg)
404 #define	__format_arg(fmtarg)
405 #define	__strfmonlike(fmtarg, firstvararg)
406 #define	__strftimelike(fmtarg, firstvararg)
407 
408 #elif __GNUC_PREREQ__(3, 0)
409 #define	__printflike(fmtarg, firstvararg) \
410             __attribute__((__nonnull__(fmtarg), \
411 			  __format__ (__printf__, fmtarg, firstvararg)))
412 #define	__printf0like(fmtarg, firstvararg) \
413             __attribute__((__format__ (__printf__, fmtarg, firstvararg)))
414 #define	__scanflike(fmtarg, firstvararg) \
415 	    __attribute__((__format__ (__scanf__, fmtarg, firstvararg)))
416 #define	__format_arg(fmtarg) \
417 	    __attribute__((__format_arg__ (fmtarg)))
418 #define	__strfmonlike(fmtarg, firstvararg) \
419 	    __attribute__((__format__ (__strfmon__, fmtarg, firstvararg)))
420 #define	__strftimelike(fmtarg, firstvararg) \
421 	    __attribute__((__format__ (__strftime__, fmtarg, firstvararg)))
422 
423 #else
424 #define	__printflike(fmtarg, firstvararg) \
425 	    __attribute__((__format__ (__printf__, fmtarg, firstvararg)))
426 #define	__printf0like(fmtarg, firstvararg) \
427 	    __attribute__((__format__ (__printf0__, fmtarg, firstvararg)))
428 #define	__scanflike(fmtarg, firstvararg) \
429 	    __attribute__((__format__ (__scanf__, fmtarg, firstvararg)))
430 #define	__format_arg(fmtarg) \
431 	    __attribute__((__format_arg__ (fmtarg)))
432 #define	__strfmonlike(fmtarg, firstvararg) \
433 	    __attribute__((__format__ (__strfmon__, fmtarg, firstvararg)))
434 #define	__strftimelike(fmtarg, firstvararg) \
435 	    __attribute__((__format__ (__strftime__, fmtarg, firstvararg)))
436 #endif
437 
438 #if !__GNUC_PREREQ__(3, 0)
439 #define	__ARRAY_ZERO	0
440 #else
441 #define	__ARRAY_ZERO
442 #endif
443 
444 /*
445  * A convenient constructor macro, GCC 4.3.0 added priority support to
446  * constructors, provide a compatible interface for both.
447  */
448 #if __GNUC_PREREQ__(4, 3)
449 #define	__constructor(prio)	__attribute__((constructor(prio)))
450 #else
451 #define	__constructor(prio)	__attribute__((constructor))
452 #endif
453 
454 /*
455  * Handy GCC based macros:
456  *
457  * 	__cachealign:
458  *
459  * 	The __cachealign macro can be used for cache line aligning structures
460  * 	of small to medium size.  It aligns the particular structure or
461  * 	storage type to a system default cache line alignment, thus giving us
462  * 	a much more better cache utilization by making the hardware work at
463  * 	its best burst speeds.
464  *
465  * 	__usereg:
466  *
467  * 	The __usereg macro can/should be used when a function contains
468  * 	arguments not more than 3.  It can be very useful to us due to the
469  * 	message-passing nature of the kernel.
470  *
471  * !!NOTE - USAGE INFORMATION!!
472  *
473  * The __cachealign macro should not be used for data structures that are
474  * as big struct proc, struct vnode, struct thread, and other structs which
475  * are as big as them; simply because it will be useless in that case.
476  *
477  * The __usereg macro should be used whenever possible, i.e., when a function
478  * does not exceed more than 3 arguments, and should not be used for vararg
479  * type functions.
480  *
481  * In other words, AVOID MISUSE OF THESE MACROS. :-)
482  */
483 #ifdef __GNUC__
484 #define	__cachealign	__attribute__((__aligned__(__VM_CACHELINE_SIZE)))
485 #define	__usereg	__attribute__((__regparm__(3)))
486 #else
487 #define	__cachealign
488 #define	__usereg
489 #endif
490 
491 #ifdef __GNUC__
492 #define	__strong_reference(sym,aliassym)	\
493 	extern __typeof (sym) aliassym __attribute__ ((__alias__ (#sym)))
494 #define	__weak_reference(sym,aliassym)	\
495 	__strong_reference(sym,aliassym) __attribute__ ((__weak__))
496 #define	__weak_reference_asm(sym,alias)	\
497 	__asm__(".weak " #alias);	\
498 	__asm__(".equ "  #alias ", " #sym)
499 #define	__warn_references(sym,msg)	\
500 	__asm__(".section .gnu.warning." #sym);	\
501 	__asm__(".asciz \"" msg "\"");	\
502 	__asm__(".previous")
503 #define	__sym_compat(sym,impl,verid)	\
504 	__asm__(".symver " #impl ", " #sym "@" #verid)
505 #define	__sym_default(sym,impl,verid)	\
506 	__asm__(".symver " #impl ", " #sym "@@@" #verid)
507 #endif	/* __GNUC__ */
508 
509 #if defined(__GNUC__)
510 #define	__IDSTRING(name,string)	__asm__(".ident\t\"" string "\"")
511 #endif
512 
513 #ifndef	__RCSID
514 #define	__RCSID(s)		struct __hack
515 #endif
516 
517 #ifndef	__RCSID_SOURCE
518 #define	__RCSID_SOURCE(s)	struct __hack
519 #endif
520 
521 #ifndef	__SCCSID
522 #define	__SCCSID(s)		struct __hack
523 #endif
524 
525 #ifndef	__FBSDID
526 #define	__FBSDID(s)		struct __hack
527 #endif
528 
529 #ifndef	__COPYRIGHT
530 #define	__COPYRIGHT(s)  	struct __hack
531 #endif
532 
533 #ifndef	__DECONST
534 #define	__DECONST(type, var)	((type)(__uintptr_t)(const void *)(var))
535 #endif
536 
537 #ifndef	__DEVOLATILE
538 #define	__DEVOLATILE(type, var)	((type)(__uintptr_t)(volatile void *)(var))
539 #endif
540 
541 #ifndef	__DEQUALIFY
542 #define	__DEQUALIFY(type, var)	((type)(__uintptr_t)(const volatile void *)(var))
543 #endif
544 
545 /*
546  * Keywords added in C11.
547  */
548 
549 #if !__GNUC_PREREQ__(2, 95)
550 #define	__alignof(x)	__offsetof(struct { char __a; x __b; }, __b)
551 #endif
552 
553 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L
554 
555 #if !__has_extension(c_alignas)
556 #if (defined(__cplusplus) && __cplusplus >= 201103L) || \
557     __has_extension(cxx_alignas)
558 #define	_Alignas(x)		alignas(x)
559 #else
560 /* XXX: Only emulates _Alignas(constant-expression); not _Alignas(type-name). */
561 #define	_Alignas(x)		__aligned(x)
562 #endif
563 #endif
564 
565 #if defined(__cplusplus) && __cplusplus >= 201103L
566 #define	_Alignof(x)		alignof(x)
567 #else
568 #define	_Alignof(x)		__alignof(x)
569 #endif
570 
571 #if !defined(_Noreturn)
572 #define	_Noreturn		__dead2
573 #endif
574 
575 #if !__has_extension(c_static_assert)
576 #if (defined(__cplusplus) && __cplusplus >= 201103L) || \
577     __has_extension(cxx_static_assert)
578 #define	_Static_assert(x, y)	static_assert(x, y)
579 #elif !__GNUC_PREREQ__(4, 6)
580 #define	_Static_assert(x, y)	struct __hack
581 #ifdef _KERNEL
582 #define	CTASSERT(x)		_CTASSERT(x, __LINE__)
583 #define	_CTASSERT(x, y)		__CTASSERT(x, y)
584 #define	__CTASSERT(x, y)	typedef char __assert ## y[(x) ? 1 : -1]
585 #endif
586 #endif
587 
588 /*
589  * GCC 4.7 has -std=c++11 but does not support thread_local.
590  */
591 #if !__has_extension(c_thread_local)
592 #if (defined(__cplusplus) && __cplusplus >= 201103L && __GNUC_PREREQ__(4, 8)) || \
593     __has_extension(cxx_thread_local)
594 #define	_Thread_local		thread_local
595 #else
596 #define	_Thread_local		__thread
597 #endif
598 #endif
599 #endif
600 
601 #endif /* __STDC_VERSION__ || __STDC_VERSION__ < 201112L */
602 
603 #if defined(_KERNEL) && !defined(CTASSERT)
604 #define	CTASSERT(x)		_Static_assert(x, \
605 				    "compile-time assertion failed")
606 #endif
607 
608 /*
609  * Emulation of C11 _Generic().  Unlike the previously defined C11
610  * keywords, it is not possible to implement this using exactly the same
611  * syntax.  Therefore implement something similar under the name
612  * __generic().  Unlike _Generic(), this macro can only distinguish
613  * between a single type, so it requires nested invocations to
614  * distinguish multiple cases.
615  */
616 
617 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
618 #define	__generic(expr, t, yes, no)					\
619 	_Generic(expr, t: yes, default: no)
620 #elif __GNUC_PREREQ__(3, 1) && !defined(__cplusplus)
621 #define	__generic(expr, t, yes, no)					\
622 	__builtin_choose_expr(						\
623 	    __builtin_types_compatible_p(__typeof(expr), t), yes, no)
624 #endif
625 
626 /*-
627  * POSIX.1 requires that the macros we test be defined before any standard
628  * header file is included.
629  *
630  * Here's a quick run-down of the versions:
631  *  defined(_POSIX_SOURCE)		1003.1-1988
632  *  _POSIX_C_SOURCE == 1		1003.1-1990
633  *  _POSIX_C_SOURCE == 2		1003.2-1992 C Language Binding Option
634  *  _POSIX_C_SOURCE == 199309		1003.1b-1993
635  *  _POSIX_C_SOURCE == 199506		1003.1c-1995, 1003.1i-1995,
636  *					and the omnibus ISO/IEC 9945-1: 1996
637  *  _POSIX_C_SOURCE == 200112		1003.1-2001
638  *  _POSIX_C_SOURCE == 200809		1003.1-2008
639  *
640  * In addition, the X/Open Portability Guide, which is now the Single UNIX
641  * Specification, defines a feature-test macro which indicates the version of
642  * that specification, and which subsumes _POSIX_C_SOURCE.
643  *
644  * Our macros begin with two underscores to avoid namespace screwage.
645  */
646 
647 /* Deal with IEEE Std. 1003.1-1990, in which _POSIX_C_SOURCE == 1. */
648 #if defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE - 0) == 1
649 #undef _POSIX_C_SOURCE		/* Probably illegal, but beyond caring now. */
650 #define	_POSIX_C_SOURCE		199009
651 #endif
652 
653 /* Deal with IEEE Std. 1003.2-1992, in which _POSIX_C_SOURCE == 2. */
654 #if defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE - 0) == 2
655 #undef _POSIX_C_SOURCE
656 #define	_POSIX_C_SOURCE		199209
657 #endif
658 
659 /* Deal with various X/Open Portability Guides and Single UNIX Spec. */
660 #ifdef _XOPEN_SOURCE
661 #if _XOPEN_SOURCE - 0 >= 700
662 #define	__XSI_VISIBLE		700
663 #undef _POSIX_C_SOURCE
664 #define	_POSIX_C_SOURCE		200809
665 #elif _XOPEN_SOURCE - 0 >= 600
666 #define	__XSI_VISIBLE		600
667 #undef _POSIX_C_SOURCE
668 #define	_POSIX_C_SOURCE		200112
669 #elif _XOPEN_SOURCE - 0 >= 500
670 #define	__XSI_VISIBLE		500
671 #undef _POSIX_C_SOURCE
672 #define	_POSIX_C_SOURCE		199506
673 #endif
674 #endif
675 
676 /*
677  * Deal with all versions of POSIX.  The ordering relative to the tests above is
678  * important.
679  */
680 #if defined(_POSIX_SOURCE) && !defined(_POSIX_C_SOURCE)
681 #define	_POSIX_C_SOURCE		198808
682 #endif
683 #ifdef _POSIX_C_SOURCE
684 #if (_POSIX_C_SOURCE - 0) >= 200809
685 #define	__POSIX_VISIBLE		200809
686 #define	__ISO_C_VISIBLE		1999
687 #elif (_POSIX_C_SOURCE - 0) >= 200112
688 #define	__POSIX_VISIBLE		200112
689 #define	__ISO_C_VISIBLE		1999
690 #elif (_POSIX_C_SOURCE - 0) >= 199506
691 #define	__POSIX_VISIBLE		199506
692 #define	__ISO_C_VISIBLE		1990
693 #elif (_POSIX_C_SOURCE - 0) >= 199309
694 #define	__POSIX_VISIBLE		199309
695 #define	__ISO_C_VISIBLE		1990
696 #elif (_POSIX_C_SOURCE - 0) >= 199209
697 #define	__POSIX_VISIBLE		199209
698 #define	__ISO_C_VISIBLE		1990
699 #elif (_POSIX_C_SOURCE - 0) >= 199009
700 #define	__POSIX_VISIBLE		199009
701 #define	__ISO_C_VISIBLE		1990
702 #else
703 #define	__POSIX_VISIBLE		198808
704 #define	__ISO_C_VISIBLE		0
705 #endif /* _POSIX_C_SOURCE */
706 #else
707 /*-
708  * Deal with _ANSI_SOURCE:
709  * If it is defined, and no other compilation environment is explicitly
710  * requested, then define our internal feature-test macros to zero.  This
711  * makes no difference to the preprocessor (undefined symbols in preprocessing
712  * expressions are defined to have value zero), but makes it more convenient for
713  * a test program to print out the values.
714  *
715  * If a program mistakenly defines _ANSI_SOURCE and some other macro such as
716  * _POSIX_C_SOURCE, we will assume that it wants the broader compilation
717  * environment (and in fact we will never get here).
718  */
719 #if defined(_ANSI_SOURCE)	/* Hide almost everything. */
720 #define	__POSIX_VISIBLE		0
721 #define	__XSI_VISIBLE		0
722 #define	__BSD_VISIBLE		0
723 #define	__ISO_C_VISIBLE		1990
724 #define	__EXT1_VISIBLE		0
725 #elif defined(_C99_SOURCE)	/* Localism to specify strict C99 env. */
726 #define	__POSIX_VISIBLE		0
727 #define	__XSI_VISIBLE		0
728 #define	__BSD_VISIBLE		0
729 #define	__ISO_C_VISIBLE		1999
730 #define	__EXT1_VISIBLE		0
731 #elif defined(_C11_SOURCE)	/* Localism to specify strict C11 env. */
732 #define	__POSIX_VISIBLE		0
733 #define	__XSI_VISIBLE		0
734 #define	__BSD_VISIBLE		0
735 #define	__ISO_C_VISIBLE		2011
736 #define	__EXT1_VISIBLE		0
737 #else				/* Default environment: show everything. */
738 #define	__POSIX_VISIBLE		200809
739 #define	__XSI_VISIBLE		700
740 #define	__BSD_VISIBLE		1
741 #define	__ISO_C_VISIBLE		2011
742 #define	__EXT1_VISIBLE		1
743 #endif
744 #endif
745 
746 /* User override __EXT1_VISIBLE */
747 #if defined(__STDC_WANT_LIB_EXT1__)
748 #undef	__EXT1_VISIBLE
749 #if __STDC_WANT_LIB_EXT1__
750 #define	__EXT1_VISIBLE		1
751 #else
752 #define	__EXT1_VISIBLE		0
753 #endif
754 #endif /* __STDC_WANT_LIB_EXT1__ */
755 
756 #ifndef __BSD_VISIBLE
757 #define	__BSD_VISIBLE		0
758 #endif
759 #ifndef __EXT1_VISIBLE
760 #define	__EXT1_VISIBLE		0
761 #endif
762 
763 /*
764  * GLOBL macro exists to preserve __start_set_* and __stop_set_* sections
765  * of kernel modules which are discarded from binutils 2.17.50+ otherwise.
766  */
767 
768 #define	__GLOBL1(sym)	__asm__(".globl " #sym)
769 #define	__GLOBL(sym)	__GLOBL1(sym)
770 
771 #endif /* !_SYS_CDEFS_H_ */
772