1139825Simp/*-
21541Srgrimes * Copyright (c) 1991, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * This code is derived from software contributed to Berkeley by
61541Srgrimes * Berkeley Software Design, Inc.
71541Srgrimes *
81541Srgrimes * Redistribution and use in source and binary forms, with or without
91541Srgrimes * modification, are permitted provided that the following conditions
101541Srgrimes * are met:
111541Srgrimes * 1. Redistributions of source code must retain the above copyright
121541Srgrimes *    notice, this list of conditions and the following disclaimer.
131541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141541Srgrimes *    notice, this list of conditions and the following disclaimer in the
151541Srgrimes *    documentation and/or other materials provided with the distribution.
161541Srgrimes * 4. Neither the name of the University nor the names of its contributors
171541Srgrimes *    may be used to endorse or promote products derived from this software
181541Srgrimes *    without specific prior written permission.
191541Srgrimes *
201541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301541Srgrimes * SUCH DAMAGE.
311541Srgrimes *
3214495Shsu *	@(#)cdefs.h	8.8 (Berkeley) 1/9/95
3350477Speter * $FreeBSD$
341541Srgrimes */
351541Srgrimes
362165Spaul#ifndef	_SYS_CDEFS_H_
372165Spaul#define	_SYS_CDEFS_H_
381541Srgrimes
39250623Sed/*
40250623Sed * Testing against Clang-specific extensions.
41250623Sed */
42250623Sed
43250623Sed#ifndef	__has_extension
44250623Sed#define	__has_extension		__has_feature
45250623Sed#endif
46250623Sed#ifndef	__has_feature
47250623Sed#define	__has_feature(x)	0
48250623Sed#endif
49250623Sed#ifndef	__has_include
50250623Sed#define	__has_include(x)	0
51250623Sed#endif
52250623Sed#ifndef	__has_builtin
53250623Sed#define	__has_builtin(x)	0
54250623Sed#endif
55250623Sed
561541Srgrimes#if defined(__cplusplus)
571541Srgrimes#define	__BEGIN_DECLS	extern "C" {
5836449Sdt#define	__END_DECLS	}
591541Srgrimes#else
601541Srgrimes#define	__BEGIN_DECLS
611541Srgrimes#define	__END_DECLS
621541Srgrimes#endif
631541Srgrimes
641541Srgrimes/*
65143063Sjoerg * This code has been put in place to help reduce the addition of
66143063Sjoerg * compiler specific defines in FreeBSD code.  It helps to aid in
67143063Sjoerg * having a compiler-agnostic source tree.
68143063Sjoerg */
69143063Sjoerg
70143063Sjoerg#if defined(__GNUC__) || defined(__INTEL_COMPILER)
71143063Sjoerg
72143063Sjoerg#if __GNUC__ >= 3 || defined(__INTEL_COMPILER)
73143063Sjoerg#define __GNUCLIKE_ASM 3
74143063Sjoerg#define __GNUCLIKE_MATH_BUILTIN_CONSTANTS
75143063Sjoerg#else
76143063Sjoerg#define __GNUCLIKE_ASM 2
77143063Sjoerg#endif
78143063Sjoerg#define __GNUCLIKE___TYPEOF 1
79143063Sjoerg#define __GNUCLIKE___OFFSETOF 1
80143063Sjoerg#define __GNUCLIKE___SECTION 1
81143063Sjoerg
82143063Sjoerg#ifndef __INTEL_COMPILER
83143063Sjoerg# define __GNUCLIKE_CTOR_SECTION_HANDLING 1
84143063Sjoerg#endif
85143063Sjoerg
86143063Sjoerg#define __GNUCLIKE_BUILTIN_CONSTANT_P 1
87143063Sjoerg# if defined(__INTEL_COMPILER) && defined(__cplusplus) \
88143063Sjoerg    && __INTEL_COMPILER < 800
89143063Sjoerg#  undef __GNUCLIKE_BUILTIN_CONSTANT_P
90143063Sjoerg# endif
91143063Sjoerg
92150976Snetchild#if (__GNUC_MINOR__ > 95 || __GNUC__ >= 3) && !defined(__INTEL_COMPILER)
93143063Sjoerg# define __GNUCLIKE_BUILTIN_VARARGS 1
94150976Snetchild# define __GNUCLIKE_BUILTIN_STDARG 1
95150976Snetchild# define __GNUCLIKE_BUILTIN_VAALIST 1
96143063Sjoerg#endif
97143063Sjoerg
98143063Sjoerg#if defined(__GNUC__)
99143063Sjoerg# define __GNUC_VA_LIST_COMPATIBILITY 1
100143063Sjoerg#endif
101143063Sjoerg
102241374Sattilio/*
103241374Sattilio * Compiler memory barriers, specific to gcc and clang.
104241374Sattilio */
105241374Sattilio#if defined(__GNUC__)
106241374Sattilio#define	__compiler_membar()	__asm __volatile(" " : : : "memory")
107241374Sattilio#endif
108241374Sattilio
109143063Sjoerg#ifndef __INTEL_COMPILER
110143063Sjoerg# define __GNUCLIKE_BUILTIN_NEXT_ARG 1
111143063Sjoerg# define __GNUCLIKE_MATH_BUILTIN_RELOPS
112143063Sjoerg#endif
113143063Sjoerg
114143063Sjoerg#define __GNUCLIKE_BUILTIN_MEMCPY 1
115143063Sjoerg
116143063Sjoerg/* XXX: if __GNUC__ >= 2: not tested everywhere originally, where replaced */
117143063Sjoerg#define __CC_SUPPORTS_INLINE 1
118143063Sjoerg#define __CC_SUPPORTS___INLINE 1
119143063Sjoerg#define __CC_SUPPORTS___INLINE__ 1
120143063Sjoerg
121143063Sjoerg#define __CC_SUPPORTS___FUNC__ 1
122143063Sjoerg#define __CC_SUPPORTS_WARNING 1
123143063Sjoerg
124143063Sjoerg#define __CC_SUPPORTS_VARADIC_XXX 1 /* see varargs.h */
125143063Sjoerg
126143063Sjoerg#define __CC_SUPPORTS_DYNAMIC_ARRAY_INIT 1
127143063Sjoerg
128143063Sjoerg#endif /* __GNUC__ || __INTEL_COMPILER */
129143063Sjoerg
130143063Sjoerg/*
131120608Smux * Macro to test if we're using a specific version of gcc or later.
132120608Smux */
133126891Strhodes#if defined(__GNUC__) && !defined(__INTEL_COMPILER)
134120608Smux#define	__GNUC_PREREQ__(ma, mi)	\
135120629Smux	(__GNUC__ > (ma) || __GNUC__ == (ma) && __GNUC_MINOR__ >= (mi))
136120608Smux#else
137120608Smux#define	__GNUC_PREREQ__(ma, mi)	0
138120608Smux#endif
139120608Smux
140120608Smux/*
1411541Srgrimes * The __CONCAT macro is used to concatenate parts of symbol names, e.g.
1421541Srgrimes * with "#define OLD(foo) __CONCAT(old,foo)", OLD(foo) produces oldfoo.
14336971Sbde * The __CONCAT macro is a bit tricky to use if it must work in non-ANSI
14436971Sbde * mode -- there must be no spaces between its arguments, and for nested
14536971Sbde * __CONCAT's, all the __CONCAT's must be at the left.  __CONCAT can also
14636971Sbde * concatenate double-quoted strings produced by the __STRING macro, but
14736971Sbde * this only works with ANSI C.
14825083Sjdp *
14925083Sjdp * __XSTRING is like __STRING, but it expands any macros in its argument
15025083Sjdp * first.  It is only available with ANSI C.
1511541Srgrimes */
1521541Srgrimes#if defined(__STDC__) || defined(__cplusplus)
1531541Srgrimes#define	__P(protos)	protos		/* full-blown ANSI C */
1545012Sbde#define	__CONCAT1(x,y)	x ## y
1555012Sbde#define	__CONCAT(x,y)	__CONCAT1(x,y)
15625083Sjdp#define	__STRING(x)	#x		/* stringify without expanding x */
15725083Sjdp#define	__XSTRING(x)	__STRING(x)	/* expand x, then stringify */
1581541Srgrimes
1591541Srgrimes#define	__const		const		/* define reserved names to standard */
1601541Srgrimes#define	__signed	signed
1611541Srgrimes#define	__volatile	volatile
1621541Srgrimes#if defined(__cplusplus)
1631541Srgrimes#define	__inline	inline		/* convert to C++ keyword */
1641541Srgrimes#else
165143063Sjoerg#if !(defined(__CC_SUPPORTS___INLINE))
1661541Srgrimes#define	__inline			/* delete GCC keyword */
167143063Sjoerg#endif /* ! __CC_SUPPORTS___INLINE */
1681541Srgrimes#endif /* !__cplusplus */
1691541Srgrimes
1701541Srgrimes#else	/* !(__STDC__ || __cplusplus) */
1711541Srgrimes#define	__P(protos)	()		/* traditional C preprocessor */
1721541Srgrimes#define	__CONCAT(x,y)	x/**/y
1731541Srgrimes#define	__STRING(x)	"x"
1741541Srgrimes
175143063Sjoerg#if !defined(__CC_SUPPORTS___INLINE)
1761541Srgrimes#define	__const				/* delete pseudo-ANSI C keywords */
1771541Srgrimes#define	__inline
1781541Srgrimes#define	__signed
1791541Srgrimes#define	__volatile
1801541Srgrimes/*
1811541Srgrimes * In non-ANSI C environments, new programs will want ANSI-only C keywords
1821541Srgrimes * deleted from the program and old programs will want them left alone.
1831541Srgrimes * When using a compiler other than gcc, programs using the ANSI C keywords
1841541Srgrimes * const, inline etc. as normal identifiers should define -DNO_ANSI_KEYWORDS.
1851541Srgrimes * When using "gcc -traditional", we assume that this is the intent; if
1861541Srgrimes * __GNUC__ is defined but __STDC__ is not, we leave the new keywords alone.
1871541Srgrimes */
1881541Srgrimes#ifndef	NO_ANSI_KEYWORDS
1891541Srgrimes#define	const				/* delete ANSI C keywords */
1901541Srgrimes#define	inline
1911541Srgrimes#define	signed
1921541Srgrimes#define	volatile
1935209Snate#endif	/* !NO_ANSI_KEYWORDS */
194143063Sjoerg#endif	/* !__CC_SUPPORTS___INLINE */
1951541Srgrimes#endif	/* !(__STDC__ || __cplusplus) */
1961541Srgrimes
1971541Srgrimes/*
19838509Sbde * Compiler-dependent macros to help declare dead (non-returning) and
19938509Sbde * pure (no side effects) functions, and unused variables.  They are
20038509Sbde * null except for versions of gcc that are known to support the features
20138509Sbde * properly (old versions of gcc-2 supported the dead and pure features
202103845Speter * in a different (wrong) way).  If we do not provide an implementation
203103845Speter * for a given compiler, let the compile fail if it is told to use
204103845Speter * a feature that we cannot live without.
2051541Srgrimes */
206103845Speter#ifdef lint
20783443Sasmodai#define	__dead2
20883443Sasmodai#define	__pure2
20983443Sasmodai#define	__unused
210103834Speter#define	__packed
211103836Speter#define	__aligned(x)
212103841Speter#define	__section(x)
213103845Speter#else
214126891Strhodes#if !__GNUC_PREREQ__(2, 5) && !defined(__INTEL_COMPILER)
215103845Speter#define	__dead2
216103845Speter#define	__pure2
217103845Speter#define	__unused
2182059Sdg#endif
219126891Strhodes#if __GNUC__ == 2 && __GNUC_MINOR__ >= 5 && __GNUC_MINOR__ < 7 && !defined(__INTEL_COMPILER)
22083443Sasmodai#define	__dead2		__attribute__((__noreturn__))
22183443Sasmodai#define	__pure2		__attribute__((__const__))
22283443Sasmodai#define	__unused
223103845Speter/* XXX Find out what to do for __packed, __aligned and __section */
2241541Srgrimes#endif
225120608Smux#if __GNUC_PREREQ__(2, 7)
22683443Sasmodai#define	__dead2		__attribute__((__noreturn__))
22783443Sasmodai#define	__pure2		__attribute__((__const__))
22883443Sasmodai#define	__unused	__attribute__((__unused__))
229132782Skan#define	__used		__attribute__((__used__))
230103834Speter#define	__packed	__attribute__((__packed__))
231103834Speter#define	__aligned(x)	__attribute__((__aligned__(x)))
232103836Speter#define	__section(x)	__attribute__((__section__(x)))
23317648Speter#endif
234126891Strhodes#if defined(__INTEL_COMPILER)
235126891Strhodes#define __dead2		__attribute__((__noreturn__))
236126891Strhodes#define __pure2		__attribute__((__const__))
237126891Strhodes#define __unused	__attribute__((__unused__))
238132782Skan#define __used		__attribute__((__used__))
239126891Strhodes#define __packed	__attribute__((__packed__))
240126891Strhodes#define __aligned(x)	__attribute__((__aligned__(x)))
241126891Strhodes#define __section(x)	__attribute__((__section__(x)))
242103845Speter#endif
243126891Strhodes#endif
2441541Srgrimes
245228562Sed#if !__GNUC_PREREQ__(2, 95)
246228562Sed#define	__alignof(x)	__offsetof(struct { char __a; x __b; }, __b)
247228562Sed#endif
248228562Sed
249228477Sed/*
250228897Sed * Keywords added in C11.
251228477Sed */
252250623Sed
253250623Sed#if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L
254250623Sed
255250623Sed#if !__has_extension(c_alignas)
256250623Sed#if (defined(__cplusplus) && __cplusplus >= 201103L) || \
257250623Sed    __has_extension(cxx_alignas)
258250623Sed#define	_Alignas(x)		alignas(x)
259250623Sed#else
260250623Sed/* XXX: Only emulates _Alignas(constant-expression); not _Alignas(type-name). */
261250623Sed#define	_Alignas(x)		__aligned(x)
262250623Sed#endif
263250623Sed#endif
264250623Sed
265228322Stheraven#if defined(__cplusplus) && __cplusplus >= 201103L
266250623Sed#define	_Alignof(x)		alignof(x)
267250623Sed#else
268250623Sed#define	_Alignof(x)		__alignof(x)
269250623Sed#endif
270250623Sed
271251804Sed#if !__has_extension(c_atomic) && !__has_extension(cxx_atomic)
272251804Sed/*
273251804Sed * No native support for _Atomic(). Place object in structure to prevent
274251804Sed * most forms of direct non-atomic access.
275251804Sed */
276251804Sed#define	_Atomic(T)		struct { T volatile __val; }
277251804Sed#endif
278251804Sed
279250623Sed#if defined(__cplusplus) && __cplusplus >= 201103L
280228330Stheraven#define	_Noreturn		[[noreturn]]
281228477Sed#else
282228562Sed#define	_Noreturn		__dead2
283250623Sed#endif
284250623Sed
285250623Sed#if !__has_extension(c_static_assert)
286250623Sed#if (defined(__cplusplus) && __cplusplus >= 201103L) || \
287250623Sed    __has_extension(cxx_static_assert)
288250623Sed#define	_Static_assert(x, y)	static_assert(x, y)
289250623Sed#elif defined(__COUNTER__)
290228562Sed#define	_Static_assert(x, y)	__Static_assert(x, __COUNTER__)
291228562Sed#define	__Static_assert(x, y)	___Static_assert(x, y)
292228562Sed#define	___Static_assert(x, y)	typedef char __assert_ ## y[(x) ? 1 : -1]
293228495Sed#else
294228562Sed#define	_Static_assert(x, y)	struct __hack
295228477Sed#endif
296228495Sed#endif
297228322Stheraven
298250623Sed#if !__has_extension(c_thread_local)
299250623Sed/* XXX: Change this to test against C++11 when clang in base supports it. */
300250623Sed#if /* (defined(__cplusplus) && __cplusplus >= 201103L) || */ \
301250623Sed    __has_extension(cxx_thread_local)
302250623Sed#define	_Thread_local		thread_local
303250623Sed#else
304250623Sed#define	_Thread_local		__thread
305250623Sed#endif
306250623Sed#endif
307250623Sed
308250623Sed#endif /* __STDC_VERSION__ || __STDC_VERSION__ < 201112L */
309250623Sed
310229574Sed/*
311229574Sed * Emulation of C11 _Generic().  Unlike the previously defined C11
312229574Sed * keywords, it is not possible to implement this using exactly the same
313229574Sed * syntax.  Therefore implement something similar under the name
314229574Sed * __generic().  Unlike _Generic(), this macro can only distinguish
315229574Sed * between a single type, so it requires nested invocations to
316229574Sed * distinguish multiple cases.
317229574Sed */
318229574Sed
319229574Sed#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
320229574Sed#define	__generic(expr, t, yes, no)					\
321229574Sed	_Generic(expr, t: yes, default: no)
322230277Sed#elif __GNUC_PREREQ__(3, 1) && !defined(__cplusplus)
323229574Sed#define	__generic(expr, t, yes, no)					\
324229574Sed	__builtin_choose_expr(						\
325229574Sed	    __builtin_types_compatible_p(__typeof(expr), t), yes, no)
326229574Sed#endif
327229574Sed
328132538Stjr#if __GNUC_PREREQ__(2, 96)
329187961Sdas#define	__malloc_like	__attribute__((__malloc__))
330132538Stjr#define	__pure		__attribute__((__pure__))
331132538Stjr#else
332187961Sdas#define	__malloc_like
333132538Stjr#define	__pure
334132538Stjr#endif
335132538Stjr
336126891Strhodes#if __GNUC_PREREQ__(3, 1) || (defined(__INTEL_COMPILER) && __INTEL_COMPILER >= 800)
337117906Speter#define	__always_inline	__attribute__((__always_inline__))
338117906Speter#else
339117906Speter#define	__always_inline
340117906Speter#endif
341117906Speter
342177618Ssam#if __GNUC_PREREQ__(3, 1)
343177618Ssam#define	__noinline	__attribute__ ((__noinline__))
344177618Ssam#else
345177618Ssam#define	__noinline
346177618Ssam#endif
347177618Ssam
348120608Smux#if __GNUC_PREREQ__(3, 3)
349117837Sphk#define __nonnull(x)	__attribute__((__nonnull__(x)))
350117837Sphk#else
351117837Sphk#define __nonnull(x)
352117837Sphk#endif
353117837Sphk
354237025Sjkim#if __GNUC_PREREQ__(3, 4)
355237025Sjkim#define	__fastcall	__attribute__((__fastcall__))
356237025Sjkim#else
357237025Sjkim#define	__fastcall
358237025Sjkim#endif
359237025Sjkim
360234785Sdim#if __GNUC_PREREQ__(4, 1)
361234785Sdim#define	__returns_twice	__attribute__((__returns_twice__))
362234785Sdim#else
363234785Sdim#define	__returns_twice
364234785Sdim#endif
365234785Sdim
36685672Smike/* XXX: should use `#if __STDC_VERSION__ < 199901'. */
367126891Strhodes#if !__GNUC_PREREQ__(2, 7) && !defined(__INTEL_COMPILER)
36885672Smike#define	__func__	NULL
36985672Smike#endif
37085672Smike
371126891Strhodes#if (defined(__INTEL_COMPILER) || (defined(__GNUC__) && __GNUC__ >= 2)) && !defined(__STRICT_ANSI__) || __STDC_VERSION__ >= 199901
37286302Smike#define	__LONG_LONG_SUPPORTED
37386302Smike#endif
37486302Smike
375227475Stheraven/* C++11 exposes a load of C99 stuff */
376227478Sdim#if defined(__cplusplus) && __cplusplus >= 201103L
377227478Sdim#define	__LONG_LONG_SUPPORTED
378227478Sdim#ifndef	__STDC_LIMIT_MACROS
379227478Sdim#define	__STDC_LIMIT_MACROS
380227475Stheraven#endif
381227478Sdim#ifndef	__STDC_CONSTANT_MACROS
382227478Sdim#define	__STDC_CONSTANT_MACROS
383227478Sdim#endif
384227478Sdim#endif
385227475Stheraven
38638509Sbde/*
387104591Smike * GCC 2.95 provides `__restrict' as an extension to C90 to support the
388103370Swollman * C99-specific `restrict' type qualifier.  We happen to use `__restrict' as
389103370Swollman * a way to define the `restrict' type qualifier without disturbing older
390103370Swollman * software that is unaware of C99 keywords.
39186803Smike */
39286803Smike#if !(__GNUC__ == 2 && __GNUC_MINOR__ == 95)
393189247Sed#if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901 || defined(lint)
39486803Smike#define	__restrict
39586803Smike#else
39686803Smike#define	__restrict	restrict
39786803Smike#endif
39886803Smike#endif
39986803Smike
40086803Smike/*
401120609Smux * GNU C version 2.96 adds explicit branch prediction so that
402120609Smux * the CPU back-end can hint the processor and also so that
403120609Smux * code blocks can be reordered such that the predicted path
404120609Smux * sees a more linear flow, thus improving cache behavior, etc.
405120609Smux *
406120609Smux * The following two macros provide us with a way to utilize this
407120609Smux * compiler feature.  Use __predict_true() if you expect the expression
408120609Smux * to evaluate to true, and __predict_false() if you expect the
409120609Smux * expression to evaluate to false.
410120609Smux *
411120609Smux * A few notes about usage:
412120609Smux *
413120609Smux *	* Generally, __predict_false() error condition checks (unless
414120609Smux *	  you have some _strong_ reason to do otherwise, in which case
415120609Smux *	  document it), and/or __predict_true() `no-error' condition
416120609Smux *	  checks, assuming you want to optimize for the no-error case.
417120609Smux *
418120609Smux *	* Other than that, if you don't know the likelihood of a test
419120609Smux *	  succeeding from empirical or other `hard' evidence, don't
420120609Smux *	  make predictions.
421120609Smux *
422120609Smux *	* These are meant to be used in places that are run `a lot'.
423120609Smux *	  It is wasteful to make predictions in code that is run
424120609Smux *	  seldomly (e.g. at subsystem initialization time) as the
425120609Smux *	  basic block reordering that this affects can often generate
426120609Smux *	  larger code.
427120609Smux */
428120609Smux#if __GNUC_PREREQ__(2, 96)
429120609Smux#define __predict_true(exp)     __builtin_expect((exp), 1)
430120609Smux#define __predict_false(exp)    __builtin_expect((exp), 0)
431120609Smux#else
432120609Smux#define __predict_true(exp)     (exp)
433120609Smux#define __predict_false(exp)    (exp)
434120609Smux#endif
435120609Smux
436211335Skib#if __GNUC_PREREQ__(4, 2)
437211868Skib#define	__hidden	__attribute__((__visibility__("hidden")))
438211868Skib#define	__exported	__attribute__((__visibility__("default")))
439211335Skib#else
440211335Skib#define	__hidden
441211335Skib#define	__exported
442211335Skib#endif
443211335Skib
444120609Smux/*
44599594Smike * We define this here since <stddef.h>, <sys/queue.h>, and <sys/types.h>
44699594Smike * require it.
44799594Smike */
448162488Skan#if __GNUC_PREREQ__(4, 1)
449162488Skan#define __offsetof(type, field)	 __builtin_offsetof(type, field)
450162488Skan#else
451146941Skan#ifndef __cplusplus
452228562Sed#define	__offsetof(type, field) \
453228564Sed	((__size_t)(__uintptr_t)((const volatile void *)&((type *)0)->field))
454146941Skan#else
455146941Skan#define __offsetof(type, field)					\
456228562Sed  (__offsetof__ (reinterpret_cast <__size_t>			\
457146941Skan                 (&reinterpret_cast <const volatile char &>	\
458146941Skan                  (static_cast<type *> (0)->field))))
459146941Skan#endif
460162488Skan#endif
461133604Sjulian#define	__rangeof(type, start, end) \
462133604Sjulian	(__offsetof(type, end) - __offsetof(type, start))
46399594Smike
46499594Smike/*
465240450Sed * Given the pointer x to the member m of the struct s, return
466240450Sed * a pointer to the containing structure.  When using GCC, we first
467240450Sed * assign pointer x to a local variable, to check that its type is
468240450Sed * compatible with member m.
469240450Sed */
470240450Sed#if __GNUC_PREREQ__(3, 1)
471240450Sed#define	__containerof(x, s, m) ({					\
472240450Sed	const volatile __typeof(((s *)0)->m) *__x = (x);		\
473240450Sed	__DEQUALIFY(s *, (const volatile char *)__x - __offsetof(s, m));\
474240450Sed})
475240450Sed#else
476240450Sed#define	__containerof(x, s, m)						\
477240450Sed	__DEQUALIFY(s *, (const volatile char *)(x) - __offsetof(s, m))
478240450Sed#endif
479240450Sed
480240450Sed/*
48138509Sbde * Compiler-dependent macros to declare that functions take printf-like
48238509Sbde * or scanf-like arguments.  They are null except for versions of gcc
48338509Sbde * that are known to support the features properly (old versions of gcc-2
48438509Sbde * didn't permit keeping the keywords out of the application namespace).
48538509Sbde */
486126891Strhodes#if !__GNUC_PREREQ__(2, 7) && !defined(__INTEL_COMPILER)
48737614Sbde#define	__printflike(fmtarg, firstvararg)
48837614Sbde#define	__scanflike(fmtarg, firstvararg)
489154867Sstefanf#define	__format_arg(fmtarg)
490232498Stheraven#define	__strfmonlike(fmtarg, firstvararg)
491232498Stheraven#define	__strftimelike(fmtarg, firstvararg)
49237614Sbde#else
49337614Sbde#define	__printflike(fmtarg, firstvararg) \
49437614Sbde	    __attribute__((__format__ (__printf__, fmtarg, firstvararg)))
49537614Sbde#define	__scanflike(fmtarg, firstvararg) \
49637614Sbde	    __attribute__((__format__ (__scanf__, fmtarg, firstvararg)))
497154867Sstefanf#define	__format_arg(fmtarg)	__attribute__((__format_arg__ (fmtarg)))
498232498Stheraven#define	__strfmonlike(fmtarg, firstvararg) \
499232498Stheraven	    __attribute__((__format__ (__strfmon__, fmtarg, firstvararg)))
500232498Stheraven#define	__strftimelike(fmtarg, firstvararg) \
501232498Stheraven	    __attribute__((__format__ (__strftime__, fmtarg, firstvararg)))
50237614Sbde#endif
50337614Sbde
50440716Sjdp/* Compiler-dependent macros that rely on FreeBSD-specific extensions. */
505227082Srmh#if defined(__FreeBSD_cc_version) && __FreeBSD_cc_version >= 300001 && \
506227082Srmh    defined(__GNUC__) && !defined(__INTEL_COMPILER)
50740716Sjdp#define	__printf0like(fmtarg, firstvararg) \
50840716Sjdp	    __attribute__((__format__ (__printf0__, fmtarg, firstvararg)))
50940716Sjdp#else
51040716Sjdp#define	__printf0like(fmtarg, firstvararg)
51140716Sjdp#endif
51240716Sjdp
513126891Strhodes#if defined(__GNUC__) || defined(__INTEL_COMPILER)
514126891Strhodes#ifndef __INTEL_COMPILER
51583443Sasmodai#define	__strong_reference(sym,aliassym)	\
516148005Skbyanc	extern __typeof (sym) aliassym __attribute__ ((__alias__ (#sym)))
517126891Strhodes#endif
5185209Snate#ifdef __STDC__
51937052Speter#define	__weak_reference(sym,alias)	\
52037052Speter	__asm__(".weak " #alias);	\
52137862Sdfr	__asm__(".equ "  #alias ", " #sym)
52237052Speter#define	__warn_references(sym,msg)	\
52337052Speter	__asm__(".section .gnu.warning." #sym);	\
52439502Sjdp	__asm__(".asciz \"" msg "\"");	\
52537052Speter	__asm__(".previous")
526156607Sdeischen#define	__sym_compat(sym,impl,verid)	\
527156607Sdeischen	__asm__(".symver " #impl ", " #sym "@" #verid)
528156607Sdeischen#define	__sym_default(sym,impl,verid)	\
529156607Sdeischen	__asm__(".symver " #impl ", " #sym "@@" #verid)
53037052Speter#else
53137052Speter#define	__weak_reference(sym,alias)	\
53237052Speter	__asm__(".weak alias");		\
53337862Sdfr	__asm__(".equ alias, sym")
53437052Speter#define	__warn_references(sym,msg)	\
53537052Speter	__asm__(".section .gnu.warning.sym"); \
53639502Sjdp	__asm__(".asciz \"msg\"");	\
53737052Speter	__asm__(".previous")
538156607Sdeischen#define	__sym_compat(sym,impl,verid)	\
539156607Sdeischen	__asm__(".symver impl, sym@verid")
540156607Sdeischen#define	__sym_default(impl,sym,verid)	\
541156607Sdeischen	__asm__(".symver impl, sym@@verid")
54237052Speter#endif	/* __STDC__ */
543126891Strhodes#endif	/* __GNUC__ || __INTEL_COMPILER */
5445209Snate
545215701Sdim#define	__GLOBL1(sym)	__asm__(".globl " #sym)
546215701Sdim#define	__GLOBL(sym)	__GLOBL1(sym)
547215137Sdim
548126891Strhodes#if defined(__GNUC__) || defined(__INTEL_COMPILER)
54950614Speter#define	__IDSTRING(name,string)	__asm__(".ident\t\"" string "\"")
55050614Speter#else
55183049Speter/*
552118023Sgad * The following definition might not work well if used in header files,
553118023Sgad * but it should be better than nothing.  If you want a "do nothing"
554118023Sgad * version, then it should generate some harmless declaration, such as:
555118023Sgad *    #define __IDSTRING(name,string)	struct __hack
55683049Speter */
55736971Sbde#define	__IDSTRING(name,string)	static const char name[] __unused = string
55850614Speter#endif
55931707Spst
56083552Sdillon/*
56183552Sdillon * Embed the rcs id of a source file in the resulting library.  Note that in
56283552Sdillon * more recent ELF binutils, we use .ident allowing the ID to be stripped.
56383552Sdillon * Usage:
56483552Sdillon *	__FBSDID("$FreeBSD$");
56583552Sdillon */
56683489Sdillon#ifndef	__FBSDID
56783528Sobrien#if !defined(lint) && !defined(STRIP_FBSDID)
56883526Sobrien#define	__FBSDID(s)	__IDSTRING(__CONCAT(__rcsid_,__LINE__),s)
56983489Sdillon#else
57083552Sdillon#define	__FBSDID(s)	struct __hack
57183489Sdillon#endif
57283489Sdillon#endif
57383489Sdillon
57493996Sobrien#ifndef	__RCSID
57593996Sobrien#ifndef	NO__RCSID
57693993Sobrien#define	__RCSID(s)	__IDSTRING(__CONCAT(__rcsid_,__LINE__),s)
57793996Sobrien#else
578118023Sgad#define	__RCSID(s)	struct __hack
57993993Sobrien#endif
58093996Sobrien#endif
58193993Sobrien
58293996Sobrien#ifndef	__RCSID_SOURCE
58393996Sobrien#ifndef	NO__RCSID_SOURCE
58483049Speter#define	__RCSID_SOURCE(s)	__IDSTRING(__CONCAT(__rcsid_source_,__LINE__),s)
58593996Sobrien#else
586118023Sgad#define	__RCSID_SOURCE(s)	struct __hack
58731707Spst#endif
58893996Sobrien#endif
58931707Spst
59093996Sobrien#ifndef	__SCCSID
59193996Sobrien#ifndef	NO__SCCSID
59293996Sobrien#define	__SCCSID(s)	__IDSTRING(__CONCAT(__sccsid_,__LINE__),s)
59393996Sobrien#else
594118023Sgad#define	__SCCSID(s)	struct __hack
59593996Sobrien#endif
59693996Sobrien#endif
59793996Sobrien
59893996Sobrien#ifndef	__COPYRIGHT
59993996Sobrien#ifndef	NO__COPYRIGHT
60083049Speter#define	__COPYRIGHT(s)	__IDSTRING(__CONCAT(__copyright_,__LINE__),s)
60193996Sobrien#else
602118023Sgad#define	__COPYRIGHT(s)	struct __hack
60331707Spst#endif
60493996Sobrien#endif
60531707Spst
60674070Sbillf#ifndef	__DECONST
607228562Sed#define	__DECONST(type, var)	((type)(__uintptr_t)(const void *)(var))
60874070Sbillf#endif
60974070Sbillf
61074070Sbillf#ifndef	__DEVOLATILE
611228562Sed#define	__DEVOLATILE(type, var)	((type)(__uintptr_t)(volatile void *)(var))
61274070Sbillf#endif
61374070Sbillf
61474070Sbillf#ifndef	__DEQUALIFY
615228562Sed#define	__DEQUALIFY(type, var)	((type)(__uintptr_t)(const volatile void *)(var))
61674070Sbillf#endif
61774070Sbillf
618210226Strasz/*-
61993510Smike * The following definitions are an extension of the behavior originally
62099904Smikeh * implemented in <sys/_posix.h>, but with a different level of granularity.
62193510Smike * POSIX.1 requires that the macros we test be defined before any standard
62293510Smike * header file is included.
62393510Smike *
62493510Smike * Here's a quick run-down of the versions:
62593510Smike *  defined(_POSIX_SOURCE)		1003.1-1988
62693510Smike *  _POSIX_C_SOURCE == 1		1003.1-1990
627100109Swollman *  _POSIX_C_SOURCE == 2		1003.2-1992 C Language Binding Option
62893510Smike *  _POSIX_C_SOURCE == 199309		1003.1b-1993
62993510Smike *  _POSIX_C_SOURCE == 199506		1003.1c-1995, 1003.1i-1995,
63093510Smike *					and the omnibus ISO/IEC 9945-1: 1996
63193510Smike *  _POSIX_C_SOURCE == 200112		1003.1-2001
632189134Sdas *  _POSIX_C_SOURCE == 200809		1003.1-2008
63393510Smike *
63493510Smike * In addition, the X/Open Portability Guide, which is now the Single UNIX
63593510Smike * Specification, defines a feature-test macro which indicates the version of
63693510Smike * that specification, and which subsumes _POSIX_C_SOURCE.
63793510Smike *
63893510Smike * Our macros begin with two underscores to avoid namespace screwage.
63993510Smike */
64093510Smike
64193510Smike/* Deal with IEEE Std. 1003.1-1990, in which _POSIX_C_SOURCE == 1. */
642121783Speter#if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE == 1
64393510Smike#undef _POSIX_C_SOURCE		/* Probably illegal, but beyond caring now. */
64493510Smike#define	_POSIX_C_SOURCE		199009
64593510Smike#endif
64693510Smike
647100109Swollman/* Deal with IEEE Std. 1003.2-1992, in which _POSIX_C_SOURCE == 2. */
648121783Speter#if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE == 2
649100109Swollman#undef _POSIX_C_SOURCE
650100109Swollman#define	_POSIX_C_SOURCE		199209
651100109Swollman#endif
652100109Swollman
65393510Smike/* Deal with various X/Open Portability Guides and Single UNIX Spec. */
65493510Smike#ifdef _XOPEN_SOURCE
655189134Sdas#if _XOPEN_SOURCE - 0 >= 700
656189134Sdas#define	__XSI_VISIBLE		700
657189134Sdas#undef _POSIX_C_SOURCE
658189134Sdas#define	_POSIX_C_SOURCE		200809
659189134Sdas#elif _XOPEN_SOURCE - 0 >= 600
66093510Smike#define	__XSI_VISIBLE		600
66193510Smike#undef _POSIX_C_SOURCE
66293510Smike#define	_POSIX_C_SOURCE		200112
66393715Speter#elif _XOPEN_SOURCE - 0 >= 500
66493510Smike#define	__XSI_VISIBLE		500
66593510Smike#undef _POSIX_C_SOURCE
66693510Smike#define	_POSIX_C_SOURCE		199506
66793510Smike#endif
66893510Smike#endif
66993510Smike
67093510Smike/*
67193510Smike * Deal with all versions of POSIX.  The ordering relative to the tests above is
67293510Smike * important.
67393510Smike */
67493510Smike#if defined(_POSIX_SOURCE) && !defined(_POSIX_C_SOURCE)
67593510Smike#define	_POSIX_C_SOURCE		198808
67693510Smike#endif
67793510Smike#ifdef _POSIX_C_SOURCE
678189134Sdas#if _POSIX_C_SOURCE >= 200809
679189134Sdas#define	__POSIX_VISIBLE		200809
680189134Sdas#define	__ISO_C_VISIBLE		1999
681189209Sdas#elif _POSIX_C_SOURCE >= 200112
68293510Smike#define	__POSIX_VISIBLE		200112
68393510Smike#define	__ISO_C_VISIBLE		1999
68493510Smike#elif _POSIX_C_SOURCE >= 199506
68593510Smike#define	__POSIX_VISIBLE		199506
68693510Smike#define	__ISO_C_VISIBLE		1990
68793510Smike#elif _POSIX_C_SOURCE >= 199309
68893510Smike#define	__POSIX_VISIBLE		199309
68993510Smike#define	__ISO_C_VISIBLE		1990
690100109Swollman#elif _POSIX_C_SOURCE >= 199209
691100109Swollman#define	__POSIX_VISIBLE		199209
692100109Swollman#define	__ISO_C_VISIBLE		1990
69393510Smike#elif _POSIX_C_SOURCE >= 199009
69493510Smike#define	__POSIX_VISIBLE		199009
69593510Smike#define	__ISO_C_VISIBLE		1990
69693510Smike#else
69793510Smike#define	__POSIX_VISIBLE		198808
69893510Smike#define	__ISO_C_VISIBLE		0
69993510Smike#endif /* _POSIX_C_SOURCE */
70093510Smike#else
701210226Strasz/*-
70293510Smike * Deal with _ANSI_SOURCE:
70393510Smike * If it is defined, and no other compilation environment is explicitly
70493510Smike * requested, then define our internal feature-test macros to zero.  This
70593510Smike * makes no difference to the preprocessor (undefined symbols in preprocessing
70693510Smike * expressions are defined to have value zero), but makes it more convenient for
70793510Smike * a test program to print out the values.
70893510Smike *
70993510Smike * If a program mistakenly defines _ANSI_SOURCE and some other macro such as
71093510Smike * _POSIX_C_SOURCE, we will assume that it wants the broader compilation
71193510Smike * environment (and in fact we will never get here).
71293510Smike */
713105654Smike#if defined(_ANSI_SOURCE)	/* Hide almost everything. */
71493510Smike#define	__POSIX_VISIBLE		0
71593510Smike#define	__XSI_VISIBLE		0
71693510Smike#define	__BSD_VISIBLE		0
71793510Smike#define	__ISO_C_VISIBLE		1990
718105654Smike#elif defined(_C99_SOURCE)	/* Localism to specify strict C99 env. */
719105654Smike#define	__POSIX_VISIBLE		0
720105654Smike#define	__XSI_VISIBLE		0
721105654Smike#define	__BSD_VISIBLE		0
722105654Smike#define	__ISO_C_VISIBLE		1999
723228900Sed#elif defined(_C11_SOURCE)	/* Localism to specify strict C11 env. */
724228900Sed#define	__POSIX_VISIBLE		0
725228900Sed#define	__XSI_VISIBLE		0
726228900Sed#define	__BSD_VISIBLE		0
727228900Sed#define	__ISO_C_VISIBLE		2011
72893510Smike#else				/* Default environment: show everything. */
729189134Sdas#define	__POSIX_VISIBLE		200809
730189134Sdas#define	__XSI_VISIBLE		700
73193510Smike#define	__BSD_VISIBLE		1
732228900Sed#define	__ISO_C_VISIBLE		2011
73393510Smike#endif
73493510Smike#endif
73593510Smike
736232498Stheraven#if defined(__mips) || defined(__powerpc64__)
737232498Stheraven#define __NO_TLS 1
738232498Stheraven#endif
739232498Stheraven
7402212Sbde#endif /* !_SYS_CDEFS_H_ */
741