<sys/cdefs.h>: Add __containerof(x, s, m) macro
[dragonfly.git] / sys / sys / cdefs.h
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  * GNU C version 2.96 adds explicit branch prediction so that
311  * the CPU back-end can hint the processor and also so that
312  * code blocks can be reordered such that the predicted path
313  * sees a more linear flow, thus improving cache behavior, etc.
314  *
315  * The following two macros provide us with a way to utilize this
316  * compiler feature.  Use __predict_true() if you expect the expression
317  * to evaluate to true, and __predict_false() if you expect the
318  * expression to evaluate to false.
319  *
320  * A few notes about usage:
321  *
322  *      * Generally, __predict_false() error condition checks (unless
323  *        you have some _strong_ reason to do otherwise, in which case
324  *        document it), and/or __predict_true() `no-error' condition
325  *        checks, assuming you want to optimize for the no-error case.
326  *
327  *      * Other than that, if you don't know the likelihood of a test
328  *        succeeding from empirical or other `hard' evidence, don't
329  *        make predictions.
330  *
331  *      * These are meant to be used in places that are run `a lot'.
332  *        It is wasteful to make predictions in code that is run
333  *        seldomly (e.g. at subsystem initialization time) as the
334  *        basic block reordering that this affects can often generate
335  *        larger code.
336  */
337 #if __GNUC_PREREQ__(2, 96)
338 #define __predict_true(exp)     __builtin_expect((exp), 1)
339 #define __predict_false(exp)    __builtin_expect((exp), 0)
340 #else
341 #define __predict_true(exp)     (exp)
342 #define __predict_false(exp)    (exp)
343 #endif
344
345 /*
346  * We define this here since <stddef.h>, <sys/queue.h>, and <sys/types.h>
347  * require it.
348  */
349 #if __GNUC_PREREQ__(4, 1)
350 #define __offsetof(type, field) __builtin_offsetof(type, field)
351 #else
352 #ifndef __cplusplus
353 #define __offsetof(type, field) \
354         ((__size_t)(__uintptr_t)((const volatile void *)&((type *)0)->field))
355 #else
356 #define __offsetof(type, field)                                 \
357         (__offsetof__ (reinterpret_cast <__size_t>              \
358                  (&reinterpret_cast <const volatile char &>     \
359                   (static_cast<type *> (0)->field))))
360 #endif
361 #endif
362
363 /*
364  * Given the pointer x to the member m of the struct s, return a pointer to
365  * the containing structure.  When using GCC, we first assign pointer x to
366  * a local variable, to check that its type is compatible with member m.
367  */
368 #if __GNUC_PREREQ__(3, 1)
369 #define __containerof(x, s, m) ({                                       \
370         const volatile __typeof(((s *)NULL)->m) *__x = (x);             \
371         __DEQUALIFY(s *, (const volatile char *)__x - __offsetof(s, m)); \
372 })
373 #else
374 #define __containerof(x, s, m)                                          \
375         __DEQUALIFY(s *, (const volatile char *)(x) - __offsetof(s, m))
376 #endif
377
378 /*
379  * Compiler-dependent macros to declare that functions take printf-like
380  * or scanf-like arguments.  They are null except for versions of gcc
381  * that are known to support the features properly (old versions of gcc-2
382  * didn't permit keeping the keywords out of the application namespace).
383  *
384  * The printf0like macro for GCC 2 uses DragonFly specific compiler extensions.
385  */
386 #if !__GNUC_PREREQ__(2, 7)
387 #define __printflike(fmtarg, firstvararg)
388 #define __scanflike(fmtarg, firstvararg)
389 #define __printf0like(fmtarg, firstvararg)
390 #define __format_arg(fmtarg)
391 #define __strfmonlike(fmtarg, firstvararg)
392 #define __strftimelike(fmtarg, firstvararg)
393
394 #elif __GNUC_PREREQ__(3, 0)
395 #define __printflike(fmtarg, firstvararg) \
396             __attribute__((__nonnull__(fmtarg), \
397                           __format__ (__printf__, fmtarg, firstvararg)))
398 #define __printf0like(fmtarg, firstvararg) \
399             __attribute__((__format__ (__printf__, fmtarg, firstvararg)))
400 #define __scanflike(fmtarg, firstvararg) \
401             __attribute__((__format__ (__scanf__, fmtarg, firstvararg)))
402 #define __format_arg(fmtarg) \
403             __attribute__((__format_arg__ (fmtarg)))
404 #define __strfmonlike(fmtarg, firstvararg) \
405             __attribute__((__format__ (__strfmon__, fmtarg, firstvararg)))
406 #define __strftimelike(fmtarg, firstvararg) \
407             __attribute__((__format__ (__strftime__, fmtarg, firstvararg)))
408
409 #else
410 #define __printflike(fmtarg, firstvararg) \
411             __attribute__((__format__ (__printf__, fmtarg, firstvararg)))
412 #define __printf0like(fmtarg, firstvararg) \
413             __attribute__((__format__ (__printf0__, 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 #endif
423
424 #if !__GNUC_PREREQ__(3, 0)
425 #define __ARRAY_ZERO    0
426 #else
427 #define __ARRAY_ZERO
428 #endif
429
430 /*
431  * A convenient constructor macro, GCC 4.3.0 added priority support to
432  * constructors, provide a compatible interface for both.
433  */
434 #if __GNUC_PREREQ__(4, 3)
435 #define __constructor(prio)     __attribute__((constructor(prio)))
436 #else
437 #define __constructor(prio)     __attribute__((constructor))
438 #endif
439
440 /*
441  * Handy GCC based macros:
442  *
443  *      __cachealign:
444  *
445  *      The __cachealign macro can be used for cache line aligning structures
446  *      of small to medium size.  It aligns the particular structure or
447  *      storage type to a system default cache line alignment, thus giving us
448  *      a much more better cache utilization by making the hardware work at
449  *      its best burst speeds.
450  *
451  *      __usereg:
452  *
453  *      The __usereg macro can/should be used when a function contains
454  *      arguments not more than 3.  It can be very useful to us due to the
455  *      message-passing nature of the kernel.
456  *
457  * !!NOTE - USAGE INFORMATION!!
458  *
459  * The __cachealign macro should not be used for data structures that are
460  * as big struct proc, struct vnode, struct thread, and other structs which
461  * are as big as them; simply because it will be useless in that case.
462  *
463  * The __usereg macro should be used whenever possible, i.e., when a function
464  * does not exceed more than 3 arguments, and should not be used for vararg
465  * type functions.
466  *
467  * In other words, AVOID MISUSE OF THESE MACROS. :-)
468  */
469 #ifdef __GNUC__
470 #define __cachealign    __attribute__((__aligned__(__VM_CACHELINE_SIZE)))
471 #define __usereg        __attribute__((__regparm__(3)))
472 #else
473 #define __cachealign
474 #define __usereg
475 #endif
476
477 #ifdef __GNUC__
478 #define __strong_reference(sym,aliassym)        \
479         extern __typeof (sym) aliassym __attribute__ ((__alias__ (#sym)))
480 #define __weak_reference(sym,aliassym)  \
481         __strong_reference(sym,aliassym) __attribute__ ((__weak__))
482 #define __weak_reference_asm(sym,alias) \
483         __asm__(".weak " #alias);       \
484         __asm__(".equ "  #alias ", " #sym)
485 #define __warn_references(sym,msg)      \
486         __asm__(".section .gnu.warning." #sym); \
487         __asm__(".asciz \"" msg "\"");  \
488         __asm__(".previous")
489 #define __sym_compat(sym,impl,verid)    \
490         __asm__(".symver " #impl ", " #sym "@" #verid)
491 #define __sym_default(sym,impl,verid)   \
492         __asm__(".symver " #impl ", " #sym "@@@" #verid)
493 #endif  /* __GNUC__ */
494
495 #if defined(__GNUC__)
496 #define __IDSTRING(name,string) __asm__(".ident\t\"" string "\"")
497 #endif
498
499 #ifndef __RCSID
500 #define __RCSID(s)              struct __hack
501 #endif
502
503 #ifndef __RCSID_SOURCE
504 #define __RCSID_SOURCE(s)       struct __hack
505 #endif
506
507 #ifndef __SCCSID
508 #define __SCCSID(s)             struct __hack
509 #endif
510
511 #ifndef __FBSDID
512 #define __FBSDID(s)             struct __hack
513 #endif
514
515 #ifndef __COPYRIGHT
516 #define __COPYRIGHT(s)          struct __hack
517 #endif
518
519 #ifndef __DECONST
520 #define __DECONST(type, var)    ((type)(__uintptr_t)(const void *)(var))
521 #endif
522
523 #ifndef __DEVOLATILE
524 #define __DEVOLATILE(type, var) ((type)(__uintptr_t)(volatile void *)(var))
525 #endif
526
527 #ifndef __DEQUALIFY
528 #define __DEQUALIFY(type, var)  ((type)(__uintptr_t)(const volatile void *)(var))
529 #endif
530
531 /*
532  * Keywords added in C11.
533  */
534
535 #if !__GNUC_PREREQ__(2, 95)
536 #define __alignof(x)    __offsetof(struct { char __a; x __b; }, __b)
537 #endif
538
539 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L
540
541 #if !__has_extension(c_alignas)
542 #if (defined(__cplusplus) && __cplusplus >= 201103L) || \
543     __has_extension(cxx_alignas)
544 #define _Alignas(x)             alignas(x)
545 #else
546 /* XXX: Only emulates _Alignas(constant-expression); not _Alignas(type-name). */
547 #define _Alignas(x)             __aligned(x)
548 #endif
549 #endif
550
551 #if defined(__cplusplus) && __cplusplus >= 201103L
552 #define _Alignof(x)             alignof(x)
553 #else
554 #define _Alignof(x)             __alignof(x)
555 #endif
556
557 #if !defined(_Noreturn)
558 #define _Noreturn               __dead2
559 #endif
560
561 #if !__has_extension(c_static_assert)
562 #if (defined(__cplusplus) && __cplusplus >= 201103L) || \
563     __has_extension(cxx_static_assert)
564 #define _Static_assert(x, y)    static_assert(x, y)
565 #elif !__GNUC_PREREQ__(4, 6)
566 #define _Static_assert(x, y)    struct __hack
567 #ifdef _KERNEL
568 #define CTASSERT(x)             _CTASSERT(x, __LINE__)
569 #define _CTASSERT(x, y)         __CTASSERT(x, y)
570 #define __CTASSERT(x, y)        typedef char __assert ## y[(x) ? 1 : -1]
571 #endif
572 #endif
573
574 /*
575  * GCC 4.7 has -std=c++11 but does not support thread_local.
576  */
577 #if !__has_extension(c_thread_local)
578 #if (defined(__cplusplus) && __cplusplus >= 201103L && __GNUC_PREREQ__(4, 8)) || \
579     __has_extension(cxx_thread_local)
580 #define _Thread_local           thread_local
581 #else
582 #define _Thread_local           __thread
583 #endif
584 #endif
585 #endif
586
587 #endif /* __STDC_VERSION__ || __STDC_VERSION__ < 201112L */
588
589 #if defined(_KERNEL) && !defined(CTASSERT)
590 #define CTASSERT(x)             _Static_assert(x, \
591                                     "compile-time assertion failed")
592 #endif
593
594 /*
595  * Emulation of C11 _Generic().  Unlike the previously defined C11
596  * keywords, it is not possible to implement this using exactly the same
597  * syntax.  Therefore implement something similar under the name
598  * __generic().  Unlike _Generic(), this macro can only distinguish
599  * between a single type, so it requires nested invocations to
600  * distinguish multiple cases.
601  */
602
603 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
604 #define __generic(expr, t, yes, no)                                     \
605         _Generic(expr, t: yes, default: no)
606 #elif __GNUC_PREREQ__(3, 1) && !defined(__cplusplus)
607 #define __generic(expr, t, yes, no)                                     \
608         __builtin_choose_expr(                                          \
609             __builtin_types_compatible_p(__typeof(expr), t), yes, no)
610 #endif
611
612 /*-
613  * POSIX.1 requires that the macros we test be defined before any standard
614  * header file is included.
615  *
616  * Here's a quick run-down of the versions:
617  *  defined(_POSIX_SOURCE)              1003.1-1988
618  *  _POSIX_C_SOURCE == 1                1003.1-1990
619  *  _POSIX_C_SOURCE == 2                1003.2-1992 C Language Binding Option
620  *  _POSIX_C_SOURCE == 199309           1003.1b-1993
621  *  _POSIX_C_SOURCE == 199506           1003.1c-1995, 1003.1i-1995,
622  *                                      and the omnibus ISO/IEC 9945-1: 1996
623  *  _POSIX_C_SOURCE == 200112           1003.1-2001
624  *  _POSIX_C_SOURCE == 200809           1003.1-2008
625  *
626  * In addition, the X/Open Portability Guide, which is now the Single UNIX
627  * Specification, defines a feature-test macro which indicates the version of
628  * that specification, and which subsumes _POSIX_C_SOURCE.
629  *
630  * Our macros begin with two underscores to avoid namespace screwage.
631  */
632
633 /* Deal with IEEE Std. 1003.1-1990, in which _POSIX_C_SOURCE == 1. */
634 #if defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE - 0) == 1
635 #undef _POSIX_C_SOURCE          /* Probably illegal, but beyond caring now. */
636 #define _POSIX_C_SOURCE         199009
637 #endif
638
639 /* Deal with IEEE Std. 1003.2-1992, in which _POSIX_C_SOURCE == 2. */
640 #if defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE - 0) == 2
641 #undef _POSIX_C_SOURCE
642 #define _POSIX_C_SOURCE         199209
643 #endif
644
645 /* Deal with various X/Open Portability Guides and Single UNIX Spec. */
646 #ifdef _XOPEN_SOURCE
647 #if _XOPEN_SOURCE - 0 >= 700
648 #define __XSI_VISIBLE           700
649 #undef _POSIX_C_SOURCE
650 #define _POSIX_C_SOURCE         200809
651 #elif _XOPEN_SOURCE - 0 >= 600
652 #define __XSI_VISIBLE           600
653 #undef _POSIX_C_SOURCE
654 #define _POSIX_C_SOURCE         200112
655 #elif _XOPEN_SOURCE - 0 >= 500
656 #define __XSI_VISIBLE           500
657 #undef _POSIX_C_SOURCE
658 #define _POSIX_C_SOURCE         199506
659 #endif
660 #endif
661
662 /*
663  * Deal with all versions of POSIX.  The ordering relative to the tests above is
664  * important.
665  */
666 #if defined(_POSIX_SOURCE) && !defined(_POSIX_C_SOURCE)
667 #define _POSIX_C_SOURCE         198808
668 #endif
669 #ifdef _POSIX_C_SOURCE
670 #if (_POSIX_C_SOURCE - 0) >= 200809
671 #define __POSIX_VISIBLE         200809
672 #define __ISO_C_VISIBLE         1999
673 #elif (_POSIX_C_SOURCE - 0) >= 200112
674 #define __POSIX_VISIBLE         200112
675 #define __ISO_C_VISIBLE         1999
676 #elif (_POSIX_C_SOURCE - 0) >= 199506
677 #define __POSIX_VISIBLE         199506
678 #define __ISO_C_VISIBLE         1990
679 #elif (_POSIX_C_SOURCE - 0) >= 199309
680 #define __POSIX_VISIBLE         199309
681 #define __ISO_C_VISIBLE         1990
682 #elif (_POSIX_C_SOURCE - 0) >= 199209
683 #define __POSIX_VISIBLE         199209
684 #define __ISO_C_VISIBLE         1990
685 #elif (_POSIX_C_SOURCE - 0) >= 199009
686 #define __POSIX_VISIBLE         199009
687 #define __ISO_C_VISIBLE         1990
688 #else
689 #define __POSIX_VISIBLE         198808
690 #define __ISO_C_VISIBLE         0
691 #endif /* _POSIX_C_SOURCE */
692 #else
693 /*-
694  * Deal with _ANSI_SOURCE:
695  * If it is defined, and no other compilation environment is explicitly
696  * requested, then define our internal feature-test macros to zero.  This
697  * makes no difference to the preprocessor (undefined symbols in preprocessing
698  * expressions are defined to have value zero), but makes it more convenient for
699  * a test program to print out the values.
700  *
701  * If a program mistakenly defines _ANSI_SOURCE and some other macro such as
702  * _POSIX_C_SOURCE, we will assume that it wants the broader compilation
703  * environment (and in fact we will never get here).
704  */
705 #if defined(_ANSI_SOURCE)       /* Hide almost everything. */
706 #define __POSIX_VISIBLE         0
707 #define __XSI_VISIBLE           0
708 #define __BSD_VISIBLE           0
709 #define __ISO_C_VISIBLE         1990
710 #define __EXT1_VISIBLE          0
711 #elif defined(_C99_SOURCE)      /* Localism to specify strict C99 env. */
712 #define __POSIX_VISIBLE         0
713 #define __XSI_VISIBLE           0
714 #define __BSD_VISIBLE           0
715 #define __ISO_C_VISIBLE         1999
716 #define __EXT1_VISIBLE          0
717 #elif defined(_C11_SOURCE)      /* Localism to specify strict C11 env. */
718 #define __POSIX_VISIBLE         0
719 #define __XSI_VISIBLE           0
720 #define __BSD_VISIBLE           0
721 #define __ISO_C_VISIBLE         2011
722 #define __EXT1_VISIBLE          0
723 #else                           /* Default environment: show everything. */
724 #define __POSIX_VISIBLE         200809
725 #define __XSI_VISIBLE           700
726 #define __BSD_VISIBLE           1
727 #define __ISO_C_VISIBLE         2011
728 #define __EXT1_VISIBLE          1
729 #endif
730 #endif
731
732 /* User override __EXT1_VISIBLE */
733 #if defined(__STDC_WANT_LIB_EXT1__)
734 #undef  __EXT1_VISIBLE
735 #if __STDC_WANT_LIB_EXT1__
736 #define __EXT1_VISIBLE          1
737 #else
738 #define __EXT1_VISIBLE          0
739 #endif
740 #endif /* __STDC_WANT_LIB_EXT1__ */
741
742 #ifndef __BSD_VISIBLE
743 #define __BSD_VISIBLE           0
744 #endif
745 #ifndef __EXT1_VISIBLE
746 #define __EXT1_VISIBLE          0
747 #endif
748
749 /*
750  * GLOBL macro exists to preserve __start_set_* and __stop_set_* sections
751  * of kernel modules which are discarded from binutils 2.17.50+ otherwise.
752  */
753
754 #define __GLOBL1(sym)   __asm__(".globl " #sym)
755 #define __GLOBL(sym)    __GLOBL1(sym)
756
757 #endif /* !_SYS_CDEFS_H_ */