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 * 4. 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$
34 */
35#ifndef _FBSD_COMPAT_SYS_CDEFS_H_
36#define _FBSD_COMPAT_SYS_CDEFS_H_
37
38
39#include <posix/sys/cdefs.h>
40
41
42#if 0
43#define __FBSDID(str)	static const char __fbsdid[] = str
44#else
45#define __FBSDID(str)
46#endif
47
48/*
49 * This code has been put in place to help reduce the addition of
50 * compiler specific defines in FreeBSD code.  It helps to aid in
51 * having a compiler-agnostic source tree.
52 */
53
54#if defined(__GNUC__) || defined(__INTEL_COMPILER)
55
56#if __GNUC__ >= 3 || defined(__INTEL_COMPILER)
57#define __GNUCLIKE_ASM 3
58#define __GNUCLIKE_MATH_BUILTIN_CONSTANTS
59#else
60#define __GNUCLIKE_ASM 2
61#endif
62#define __GNUCLIKE___TYPEOF 1
63#define __GNUCLIKE___OFFSETOF 1
64#define __GNUCLIKE___SECTION 1
65
66#define __GNUCLIKE_ATTRIBUTE_MODE_DI 1
67
68#ifndef __INTEL_COMPILER
69# define __GNUCLIKE_CTOR_SECTION_HANDLING 1
70#endif
71
72#define __GNUCLIKE_BUILTIN_CONSTANT_P 1
73# if defined(__INTEL_COMPILER) && defined(__cplusplus) \
74    && __INTEL_COMPILER < 800
75#  undef __GNUCLIKE_BUILTIN_CONSTANT_P
76# endif
77
78#if (__GNUC_MINOR__ > 95 || __GNUC__ >= 3) && !defined(__INTEL_COMPILER)
79# define __GNUCLIKE_BUILTIN_VARARGS 1
80# define __GNUCLIKE_BUILTIN_STDARG 1
81# define __GNUCLIKE_BUILTIN_VAALIST 1
82#endif
83
84#if defined(__GNUC__)
85# define __GNUC_VA_LIST_COMPATIBILITY 1
86#endif
87
88/*
89 * Compiler memory barriers, specific to gcc and clang.
90 */
91#if defined(__GNUC__)
92#define	__compiler_membar()	__asm __volatile(" " : : : "memory")
93#endif
94
95#ifndef __INTEL_COMPILER
96# define __GNUCLIKE_BUILTIN_NEXT_ARG 1
97# define __GNUCLIKE_MATH_BUILTIN_RELOPS
98#endif
99
100#define __GNUCLIKE_BUILTIN_MEMCPY 1
101
102/* XXX: if __GNUC__ >= 2: not tested everywhere originally, where replaced */
103#define __CC_SUPPORTS_INLINE 1
104#define __CC_SUPPORTS___INLINE 1
105#define __CC_SUPPORTS___INLINE__ 1
106
107#define __CC_SUPPORTS___FUNC__ 1
108#define __CC_SUPPORTS_WARNING 1
109
110#define __CC_SUPPORTS_VARADIC_XXX 1 /* see varargs.h */
111
112#define __CC_SUPPORTS_DYNAMIC_ARRAY_INIT 1
113
114#endif /* __GNUC__ || __INTEL_COMPILER */
115
116
117/*
118 * Macro to test if we're using a specific version of gcc or later.
119 */
120#if defined(__GNUC__) && !defined(__INTEL_COMPILER)
121#define	__GNUC_PREREQ__(ma, mi)	\
122	(__GNUC__ > (ma) || __GNUC__ == (ma) && __GNUC_MINOR__ >= (mi))
123#else
124#define	__GNUC_PREREQ__(ma, mi)	0
125#endif
126
127/*
128 * GNU C version 2.96 adds explicit branch prediction so that
129 * the CPU back-end can hint the processor and also so that
130 * code blocks can be reordered such that the predicted path
131 * sees a more linear flow, thus improving cache behavior, etc.
132 *
133 * The following two macros provide us with a way to utilize this
134 * compiler feature.  Use __predict_true() if you expect the expression
135 * to evaluate to true, and __predict_false() if you expect the
136 * expression to evaluate to false.
137 *
138 * A few notes about usage:
139 *
140 *	* Generally, __predict_false() error condition checks (unless
141 *	  you have some _strong_ reason to do otherwise, in which case
142 *	  document it), and/or __predict_true() `no-error' condition
143 *	  checks, assuming you want to optimize for the no-error case.
144 *
145 *	* Other than that, if you don't know the likelihood of a test
146 *	  succeeding from empirical or other `hard' evidence, don't
147 *	  make predictions.
148 *
149 *	* These are meant to be used in places that are run `a lot'.
150 *	  It is wasteful to make predictions in code that is run
151 *	  seldomly (e.g. at subsystem initialization time) as the
152 *	  basic block reordering that this affects can often generate
153 *	  larger code.
154 */
155#if __GNUC_PREREQ__(2, 96)
156#define __predict_true(exp)     __builtin_expect((exp), 1)
157#define __predict_false(exp)    __builtin_expect((exp), 0)
158#else
159#define __predict_true(exp)     (exp)
160#define __predict_false(exp)    (exp)
161#endif
162
163/*
164 * We define this here since <stddef.h>, <sys/queue.h>, and <sys/types.h>
165 * require it.
166 */
167#if __GNUC_PREREQ__(4, 1)
168#define __offsetof(type, field)	 __builtin_offsetof(type, field)
169#else
170#ifndef __cplusplus
171#define	__offsetof(type, field)	((size_t)(&((type *)0)->field))
172#else
173#define __offsetof(type, field)					\
174  ((reinterpret_cast <size_t>			\
175                 (&reinterpret_cast <const volatile char &>	\
176                  (static_cast<type *> (0)->field))))
177#endif
178#endif
179#define	__rangeof(type, start, end) \
180	(__offsetof(type, end) - __offsetof(type, start))
181
182/*
183 * Given the pointer x to the member m of the struct s, return
184 * a pointer to the containing structure.  When using GCC, we first
185 * assign pointer x to a local variable, to check that its type is
186 * compatible with member m.
187 */
188#if __GNUC_PREREQ__(3, 1)
189#define	__containerof(x, s, m) ({					\
190	const volatile __typeof(((s *)0)->m) *__x = (x);		\
191	__DEQUALIFY(s *, (const volatile char *)__x - __offsetof(s, m));\
192})
193#else
194#define	__containerof(x, s, m)						\
195	__DEQUALIFY(s *, (const volatile char *)(x) - __offsetof(s, m))
196#endif
197
198/*
199 * Compiler-dependent macros to help declare dead (non-returning) and
200 * pure (no side effects) functions, and unused variables.  They are
201 * null except for versions of gcc that are known to support the features
202 * properly (old versions of gcc-2 supported the dead and pure features
203 * in a different (wrong) way).  If we do not provide an implementation
204 * for a given compiler, let the compile fail if it is told to use
205 * a feature that we cannot live without.
206 */
207/*
208 * Compiler-dependent macros to help declare dead (non-returning) and
209 * pure (no side effects) functions, and unused variables.  They are
210 * null except for versions of gcc that are known to support the features
211 * properly (old versions of gcc-2 supported the dead and pure features
212 * in a different (wrong) way).  If we do not provide an implementation
213 * for a given compiler, let the compile fail if it is told to use
214 * a feature that we cannot live without.
215 */
216#define	__weak_symbol	__attribute__((__weak__))
217#if !__GNUC_PREREQ__(2, 5) && !defined(__INTEL_COMPILER)
218#define	__dead2
219#define	__pure2
220#define	__unused
221#endif
222#if __GNUC__ == 2 && __GNUC_MINOR__ >= 5 && __GNUC_MINOR__ < 7 && !defined(__INTEL_COMPILER)
223#define	__dead2		__attribute__((__noreturn__))
224#define	__pure2		__attribute__((__const__))
225#define	__unused
226/* XXX Find out what to do for __packed, __aligned and __section */
227#endif
228#if __GNUC_PREREQ__(2, 7) || defined(__INTEL_COMPILER)
229#undef __dead2
230#define	__dead2		__attribute__((__noreturn__))
231#define	__pure2		__attribute__((__const__))
232#define	__unused	__attribute__((__unused__))
233#define	__used		__attribute__((__used__))
234#define	__packed	__attribute__((__packed__))
235#define	__aligned(x)	__attribute__((__aligned__(x)))
236#define	__section(x)	__attribute__((__section__(x)))
237#endif
238#if __GNUC_PREREQ__(4, 3)
239#define	__alloc_size(x)	__attribute__((__alloc_size__(x)))
240#define	__alloc_size2(n, x)	__attribute__((__alloc_size__(n, x)))
241#else
242#define	__alloc_size(x)
243#define	__alloc_size2(n, x)
244#endif
245#if __GNUC_PREREQ__(4, 9)
246#define	__alloc_align(x)	__attribute__((__alloc_align__(x)))
247#else
248#define	__alloc_align(x)
249#endif
250
251/*
252 * Compiler-dependent macros to declare that functions take printf-like
253 * or scanf-like arguments.  They are null except for versions of gcc
254 * that are known to support the features properly (old versions of gcc-2
255 * didn't permit keeping the keywords out of the application namespace).
256 */
257#if !__GNUC_PREREQ__(2, 7) && !defined(__INTEL_COMPILER)
258#define	__printflike(fmtarg, firstvararg)
259#define	__scanflike(fmtarg, firstvararg)
260#define	__format_arg(fmtarg)
261#else
262#define	__printflike(fmtarg, firstvararg) \
263	    __attribute__((__format__ (__printf__, fmtarg, firstvararg)))
264#define	__scanflike(fmtarg, firstvararg) \
265	    __attribute__((__format__ (__scanf__, fmtarg, firstvararg)))
266#define	__format_arg(fmtarg)	__attribute__((__format_arg__ (fmtarg)))
267#endif
268
269/*
270 * C99 Static array indices in function parameter declarations.  Syntax such as:
271 * void bar(int myArray[static 10]);
272 * is allowed in C99 but not in C++.  Define __min_size appropriately so
273 * headers using it can be compiled in either language.  Use like this:
274 * void bar(int myArray[__min_size(10)]);
275 */
276#if !defined(__cplusplus) && \
277    (defined(__clang__) || __GNUC_PREREQ__(4, 6)) && \
278    (!defined(__STDC_VERSION__) || (__STDC_VERSION__ >= 199901))
279#define __min_size(x)	static (x)
280#else
281#define __min_size(x)	(x)
282#endif
283
284#if __GNUC_PREREQ__(3, 1)
285#define	__noinline	__attribute__ ((__noinline__))
286#else
287#define	__noinline
288#endif
289
290#if __GNUC_PREREQ__(3, 3)
291#define __nonnull(x)	__attribute__((__nonnull__(x)))
292#else
293#define __nonnull(x)
294#endif
295
296/*
297 * The __CONCAT macro is used to concatenate parts of symbol names, e.g.
298 * with "#define OLD(foo) __CONCAT(old,foo)", OLD(foo) produces oldfoo.
299 * The __CONCAT macro is a bit tricky to use if it must work in non-ANSI
300 * mode -- there must be no spaces between its arguments, and for nested
301 * __CONCAT's, all the __CONCAT's must be at the left.  __CONCAT can also
302 * concatenate double-quoted strings produced by the __STRING macro, but
303 * this only works with ANSI C.
304 *
305 * __XSTRING is like __STRING, but it expands any macros in its argument
306 * first.  It is only available with ANSI C.
307 */
308#if defined(__STDC__) || defined(__cplusplus)
309#define	__XSTRING(x)	__STRING(x)	/* expand x, then stringify */
310
311#define	__const		const		/* define reserved names to standard */
312#define	__signed	signed
313#define	__volatile	volatile
314#if defined(__cplusplus)
315#define	__inline	inline		/* convert to C++ keyword */
316#else
317#if !(defined(__CC_SUPPORTS___INLINE))
318#define	__inline			/* delete GCC keyword */
319#endif /* ! __CC_SUPPORTS___INLINE */
320#endif /* !__cplusplus */
321
322#else	/* !(__STDC__ || __cplusplus) */
323
324#if !defined(__CC_SUPPORTS___INLINE)
325#define	__const				/* delete pseudo-ANSI C keywords */
326#define	__inline
327#define	__signed
328#define	__volatile
329/*
330 * In non-ANSI C environments, new programs will want ANSI-only C keywords
331 * deleted from the program and old programs will want them left alone.
332 * When using a compiler other than gcc, programs using the ANSI C keywords
333 * const, inline etc. as normal identifiers should define -DNO_ANSI_KEYWORDS.
334 * When using "gcc -traditional", we assume that this is the intent; if
335 * __GNUC__ is defined but __STDC__ is not, we leave the new keywords alone.
336 */
337#ifndef	NO_ANSI_KEYWORDS
338#define	const				/* delete ANSI C keywords */
339#define	inline
340#define	signed
341#define	volatile
342#endif	/* !NO_ANSI_KEYWORDS */
343#endif	/* !__CC_SUPPORTS___INLINE */
344#endif	/* !(__STDC__ || __cplusplus) */
345
346#ifndef	__DECONST
347#define	__DECONST(type, var)	((type)(uintptr_t)(const void *)(var))
348#endif
349
350#ifndef	__UNCONST
351#define	__UNCONST(var)	((void*)(uintptr_t)(const void *)(var))
352#endif
353
354#ifndef	__DEVOLATILE
355#define	__DEVOLATILE(type, var)	((type)(uintptr_t)(volatile void *)(var))
356#endif
357
358#ifndef	__DEQUALIFY
359#define	__DEQUALIFY(type, var)	((type)(uintptr_t)(const volatile void *)(var))
360#endif
361
362#if __GNUC_PREREQ__(4,6) && !defined(__cplusplus)
363/* Nothing, gcc 4.6 and higher has _Static_assert built-in */
364#elif defined(__COUNTER__)
365#define	_Static_assert(x, y)	__Static_assert(x, __COUNTER__)
366#define	__Static_assert(x, y)	___Static_assert(x, y)
367#define	___Static_assert(x, y)	typedef char __assert_ ## y[(x) ? 1 : -1] \
368				__unused
369#else
370#define	_Static_assert(x, y)
371#endif
372
373#endif
374