1/*	$NetBSD: cdefs.h,v 1.58 2004/12/11 05:59:00 christos Exp $	*/
2
3/*
4 * Copyright (c) 1991, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Berkeley Software Design, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 *	@(#)cdefs.h	8.8 (Berkeley) 1/9/95
35 */
36
37#ifndef	_SYS_CDEFS_H_
38#define	_SYS_CDEFS_H_
39
40
41/* our implementation of wchar_t is only 8-bit - die die non-portable code */
42#undef  __WCHAR_TYPE__
43#define __WCHAR_TYPE__  unsigned char
44
45
46/*
47 * Macro to test if we're using a GNU C compiler of a specific vintage
48 * or later, for e.g. features that appeared in a particular version
49 * of GNU C.  Usage:
50 *
51 *	#if __GNUC_PREREQ__(major, minor)
52 *	...cool feature...
53 *	#else
54 *	...delete feature...
55 *	#endif
56 */
57#ifdef __GNUC__
58#define	__GNUC_PREREQ__(x, y)						\
59	((__GNUC__ == (x) && __GNUC_MINOR__ >= (y)) ||			\
60	 (__GNUC__ > (x)))
61#else
62#define	__GNUC_PREREQ__(x, y)	0
63#endif
64
65#include <sys/cdefs_elf.h>
66
67#if defined(__cplusplus)
68#define	__BEGIN_DECLS		extern "C" {
69#define	__END_DECLS		}
70#define	__static_cast(x,y)	static_cast<x>(y)
71#else
72#define	__BEGIN_DECLS
73#define	__END_DECLS
74#define	__static_cast(x,y)	(x)y
75#endif
76
77/*
78 * The __CONCAT macro is used to concatenate parts of symbol names, e.g.
79 * with "#define OLD(foo) __CONCAT(old,foo)", OLD(foo) produces oldfoo.
80 * The __CONCAT macro is a bit tricky -- make sure you don't put spaces
81 * in between its arguments.  __CONCAT can also concatenate double-quoted
82 * strings produced by the __STRING macro, but this only works with ANSI C.
83 */
84
85#define	___STRING(x)	__STRING(x)
86#define	___CONCAT(x,y)	__CONCAT(x,y)
87
88#if __STDC__ || defined(__cplusplus)
89#define	__P(protos)	protos		/* full-blown ANSI C */
90#define	__CONCAT(x,y)	x ## y
91#define	__STRING(x)	#x
92
93#define	__const		const		/* define reserved names to standard */
94#define	__signed	signed
95#define	__volatile	volatile
96#if defined(__cplusplus)
97#define	__inline	inline		/* convert to C++ keyword */
98#else
99#if !defined(__GNUC__) && !defined(__lint__)
100#define	__inline			/* delete GCC keyword */
101#endif /* !__GNUC__  && !__lint__ */
102#endif /* !__cplusplus */
103
104#else	/* !(__STDC__ || __cplusplus) */
105#define	__P(protos)	()		/* traditional C preprocessor */
106#define	__CONCAT(x,y)	x/**/y
107#define	__STRING(x)	"x"
108
109#ifndef __GNUC__
110#define	__const				/* delete pseudo-ANSI C keywords */
111#define	__inline
112#define	__signed
113#define	__volatile
114#endif	/* !__GNUC__ */
115
116/*
117 * In non-ANSI C environments, new programs will want ANSI-only C keywords
118 * deleted from the program and old programs will want them left alone.
119 * Programs using the ANSI C keywords const, inline etc. as normal
120 * identifiers should define -DNO_ANSI_KEYWORDS.
121 */
122#ifndef	NO_ANSI_KEYWORDS
123#define	const		__const		/* convert ANSI C keywords */
124#define	inline		__inline
125#define	signed		__signed
126#define	volatile	__volatile
127#endif /* !NO_ANSI_KEYWORDS */
128#endif	/* !(__STDC__ || __cplusplus) */
129
130/*
131 * Used for internal auditing of the NetBSD source tree.
132 */
133#ifdef __AUDIT__
134#define	__aconst	__const
135#else
136#define	__aconst
137#endif
138
139/*
140 * The following macro is used to remove const cast-away warnings
141 * from gcc -Wcast-qual; it should be used with caution because it
142 * can hide valid errors; in particular most valid uses are in
143 * situations where the API requires it, not to cast away string
144 * constants. We don't use *intptr_t on purpose here and we are
145 * explicit about unsigned long so that we don't have additional
146 * dependencies.
147 */
148#define __UNCONST(a)	((void *)(unsigned long)(const void *)(a))
149
150/*
151 * GCC2 provides __extension__ to suppress warnings for various GNU C
152 * language extensions under "-ansi -pedantic".
153 */
154#if !__GNUC_PREREQ__(2, 0)
155#define	__extension__		/* delete __extension__ if non-gcc or gcc1 */
156#endif
157
158/*
159 * GCC1 and some versions of GCC2 declare dead (non-returning) and
160 * pure (no side effects) functions using "volatile" and "const";
161 * unfortunately, these then cause warnings under "-ansi -pedantic".
162 * GCC2 uses a new, peculiar __attribute__((attrs)) style.  All of
163 * these work for GNU C++ (modulo a slight glitch in the C++ grammar
164 * in the distribution version of 2.5.5).
165 */
166#if !__GNUC_PREREQ__(2, 5)
167#define	__attribute__(x)	/* delete __attribute__ if non-gcc or gcc1 */
168#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
169#define	__dead		__volatile
170#define	__pure		__const
171#endif
172#endif
173
174/* Delete pseudo-keywords wherever they are not available or needed. */
175#ifndef __dead
176#define	__dead
177#define	__pure
178#endif
179
180#if __GNUC_PREREQ__(2, 7)
181#define	__unused	__attribute__((__unused__))
182#else
183#define	__unused	/* delete */
184#endif
185
186#if __GNUC_PREREQ__(3, 1)
187#define	__used		__attribute__((__used__))
188#else
189#define	__used		/* delete */
190#endif
191
192#if __GNUC_PREREQ__(2, 7)
193#define	__packed	__attribute__((__packed__))
194#define	__aligned(x)	__attribute__((__aligned__(x)))
195#define	__section(x)	__attribute__((__section__(x)))
196#elif defined(__lint__)
197#define	__packed	/* delete */
198#define	__aligned(x)	/* delete */
199#define	__section(x)	/* delete */
200#else
201#define	__packed	error: no __packed for this compiler
202#define	__aligned(x)	error: no __aligned for this compiler
203#define	__section(x)	error: no __section for this compiler
204#endif
205
206#if !__GNUC_PREREQ__(2, 8)
207#define	__extension__
208#endif
209
210#if __GNUC_PREREQ__(2, 8)
211#define __statement(x)	__extension__(x)
212#elif defined(lint)
213#define __statement(x)	(0)
214#else
215#define __statement(x)	(x)
216#endif
217
218/*
219 * C99 defines the restrict type qualifier keyword, which was made available
220 * in GCC 2.92.
221 */
222#if __STDC_VERSION__ >= 199901L
223#define	__restrict	restrict
224#else
225#if !__GNUC_PREREQ__(2, 92)
226#define	__restrict	/* delete __restrict when not supported */
227#endif
228#endif
229
230/*
231 * C99 defines __func__ predefined identifier, which was made available
232 * in GCC 2.95.
233 */
234#if !(__STDC_VERSION__ >= 199901L)
235#if __GNUC_PREREQ__(2, 6)
236#define	__func__	__PRETTY_FUNCTION__
237#elif __GNUC_PREREQ__(2, 4)
238#define	__func__	__FUNCTION__
239#else
240#define	__func__	""
241#endif
242#endif /* !(__STDC_VERSION__ >= 199901L) */
243
244#if defined(_KERNEL)
245#if defined(NO_KERNEL_RCSIDS)
246#undef __KERNEL_RCSID
247#define	__KERNEL_RCSID(_n, _s)		/* nothing */
248#endif /* NO_KERNEL_RCSIDS */
249#endif /* _KERNEL */
250
251#if !defined(_STANDALONE) && !defined(_KERNEL)
252#ifdef __GNUC__
253#define	__RENAME(x)	___RENAME(x)
254#else
255#ifdef __lint__
256#define	__RENAME(x)	__symbolrename(x)
257#else
258#error "No function renaming possible"
259#endif /* __lint__ */
260#endif /* __GNUC__ */
261#else /* _STANDALONE || _KERNEL */
262#define	__RENAME(x)	no renaming in kernel or standalone environment
263#endif
264
265/*
266 * A barrier to stop the optimizer from moving code or assume live
267 * register values. This is gcc specific, the version is more or less
268 * arbitrary, might work with older compilers.
269 */
270#if __GNUC_PREREQ__(2, 95)
271#define	__insn_barrier()	__asm __volatile("":::"memory")
272#else
273#define	__insn_barrier()	/* */
274#endif
275
276/*
277 * GNU C version 2.96 adds explicit branch prediction so that
278 * the CPU back-end can hint the processor and also so that
279 * code blocks can be reordered such that the predicted path
280 * sees a more linear flow, thus improving cache behavior, etc.
281 *
282 * The following two macros provide us with a way to use this
283 * compiler feature.  Use __predict_true() if you expect the expression
284 * to evaluate to true, and __predict_false() if you expect the
285 * expression to evaluate to false.
286 *
287 * A few notes about usage:
288 *
289 *	* Generally, __predict_false() error condition checks (unless
290 *	  you have some _strong_ reason to do otherwise, in which case
291 *	  document it), and/or __predict_true() `no-error' condition
292 *	  checks, assuming you want to optimize for the no-error case.
293 *
294 *	* Other than that, if you don't know the likelihood of a test
295 *	  succeeding from empirical or other `hard' evidence, don't
296 *	  make predictions.
297 *
298 *	* These are meant to be used in places that are run `a lot'.
299 *	  It is wasteful to make predictions in code that is run
300 *	  seldomly (e.g. at subsystem initialization time) as the
301 *	  basic block reordering that this affects can often generate
302 *	  larger code.
303 */
304#if __GNUC_PREREQ__(2, 96)
305#define	__predict_true(exp)	__builtin_expect((exp) != 0, 1)
306#define	__predict_false(exp)	__builtin_expect((exp) != 0, 0)
307#else
308#define	__predict_true(exp)	(exp)
309#define	__predict_false(exp)	(exp)
310#endif
311
312#if __GNUC_PREREQ__(2, 96)
313#define __noreturn    __attribute__((__noreturn__))
314#define __mallocfunc  __attribute__((malloc))
315#else
316#define __noreturn
317#define __mallocfunc
318#endif
319
320/*
321 * Macros for manipulating "link sets".  Link sets are arrays of pointers
322 * to objects, which are gathered up by the linker.
323 *
324 * Object format-specific code has provided us with the following macros:
325 *
326 *	__link_set_add_text(set, sym)
327 *		Add a reference to the .text symbol `sym' to `set'.
328 *
329 *	__link_set_add_rodata(set, sym)
330 *		Add a reference to the .rodata symbol `sym' to `set'.
331 *
332 *	__link_set_add_data(set, sym)
333 *		Add a reference to the .data symbol `sym' to `set'.
334 *
335 *	__link_set_add_bss(set, sym)
336 *		Add a reference to the .bss symbol `sym' to `set'.
337 *
338 *	__link_set_decl(set, ptype)
339 *		Provide an extern declaration of the set `set', which
340 *		contains an array of the pointer type `ptype'.  This
341 *		macro must be used by any code which wishes to reference
342 *		the elements of a link set.
343 *
344 *	__link_set_start(set)
345 *		This points to the first slot in the link set.
346 *
347 *	__link_set_end(set)
348 *		This points to the (non-existent) slot after the last
349 *		entry in the link set.
350 *
351 *	__link_set_count(set)
352 *		Count the number of entries in link set `set'.
353 *
354 * In addition, we provide the following macros for accessing link sets:
355 *
356 *	__link_set_foreach(pvar, set)
357 *		Iterate over the link set `set'.  Because a link set is
358 *		an array of pointers, pvar must be declared as "type **pvar",
359 *		and the actual entry accessed as "*pvar".
360 *
361 *	__link_set_entry(set, idx)
362 *		Access the link set entry at index `idx' from set `set'.
363 */
364#define	__link_set_foreach(pvar, set)					\
365	for (pvar = __link_set_start(set); pvar < __link_set_end(set); pvar++)
366
367#define	__link_set_entry(set, idx)	(__link_set_begin(set)[idx])
368
369/*
370 * Some of the recend FreeBSD sources used in Bionic need this.
371 * Originally, this is used to embed the rcs versions of each source file
372 * in the generated binary. We certainly don't want this in Bionic.
373 */
374#define	__FBSDID(s)	struct __hack
375
376/*-
377 * The following definitions are an extension of the behavior originally
378 * implemented in <sys/_posix.h>, but with a different level of granularity.
379 * POSIX.1 requires that the macros we test be defined before any standard
380 * header file is included.
381 *
382 * Here's a quick run-down of the versions:
383 *  defined(_POSIX_SOURCE)		1003.1-1988
384 *  _POSIX_C_SOURCE == 1		1003.1-1990
385 *  _POSIX_C_SOURCE == 2		1003.2-1992 C Language Binding Option
386 *  _POSIX_C_SOURCE == 199309		1003.1b-1993
387 *  _POSIX_C_SOURCE == 199506		1003.1c-1995, 1003.1i-1995,
388 *					and the omnibus ISO/IEC 9945-1: 1996
389 *  _POSIX_C_SOURCE == 200112		1003.1-2001
390 *  _POSIX_C_SOURCE == 200809		1003.1-2008
391 *
392 * In addition, the X/Open Portability Guide, which is now the Single UNIX
393 * Specification, defines a feature-test macro which indicates the version of
394 * that specification, and which subsumes _POSIX_C_SOURCE.
395 *
396 * Our macros begin with two underscores to avoid namespace screwage.
397 */
398
399/* Deal with IEEE Std. 1003.1-1990, in which _POSIX_C_SOURCE == 1. */
400#if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE == 1
401#undef _POSIX_C_SOURCE		/* Probably illegal, but beyond caring now. */
402#define	_POSIX_C_SOURCE		199009
403#endif
404
405/* Deal with IEEE Std. 1003.2-1992, in which _POSIX_C_SOURCE == 2. */
406#if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE == 2
407#undef _POSIX_C_SOURCE
408#define	_POSIX_C_SOURCE		199209
409#endif
410
411/* Deal with various X/Open Portability Guides and Single UNIX Spec. */
412#ifdef _XOPEN_SOURCE
413#if _XOPEN_SOURCE - 0 >= 700
414#define	__XSI_VISIBLE		700
415#undef _POSIX_C_SOURCE
416#define	_POSIX_C_SOURCE		200809
417#elif _XOPEN_SOURCE - 0 >= 600
418#define	__XSI_VISIBLE		600
419#undef _POSIX_C_SOURCE
420#define	_POSIX_C_SOURCE		200112
421#elif _XOPEN_SOURCE - 0 >= 500
422#define	__XSI_VISIBLE		500
423#undef _POSIX_C_SOURCE
424#define	_POSIX_C_SOURCE		199506
425#endif
426#endif
427
428/*
429 * Deal with all versions of POSIX.  The ordering relative to the tests above is
430 * important.
431 */
432#if defined(_POSIX_SOURCE) && !defined(_POSIX_C_SOURCE)
433#define	_POSIX_C_SOURCE		198808
434#endif
435#ifdef _POSIX_C_SOURCE
436#if _POSIX_C_SOURCE >= 200809
437#define	__POSIX_VISIBLE		200809
438#define	__ISO_C_VISIBLE		1999
439#elif _POSIX_C_SOURCE >= 200112
440#define	__POSIX_VISIBLE		200112
441#define	__ISO_C_VISIBLE		1999
442#elif _POSIX_C_SOURCE >= 199506
443#define	__POSIX_VISIBLE		199506
444#define	__ISO_C_VISIBLE		1990
445#elif _POSIX_C_SOURCE >= 199309
446#define	__POSIX_VISIBLE		199309
447#define	__ISO_C_VISIBLE		1990
448#elif _POSIX_C_SOURCE >= 199209
449#define	__POSIX_VISIBLE		199209
450#define	__ISO_C_VISIBLE		1990
451#elif _POSIX_C_SOURCE >= 199009
452#define	__POSIX_VISIBLE		199009
453#define	__ISO_C_VISIBLE		1990
454#else
455#define	__POSIX_VISIBLE		198808
456#define	__ISO_C_VISIBLE		0
457#endif /* _POSIX_C_SOURCE */
458#else
459/*-
460 * Deal with _ANSI_SOURCE:
461 * If it is defined, and no other compilation environment is explicitly
462 * requested, then define our internal feature-test macros to zero.  This
463 * makes no difference to the preprocessor (undefined symbols in preprocessing
464 * expressions are defined to have value zero), but makes it more convenient for
465 * a test program to print out the values.
466 *
467 * If a program mistakenly defines _ANSI_SOURCE and some other macro such as
468 * _POSIX_C_SOURCE, we will assume that it wants the broader compilation
469 * environment (and in fact we will never get here).
470 */
471#if defined(_ANSI_SOURCE)	/* Hide almost everything. */
472#define	__POSIX_VISIBLE		0
473#define	__XSI_VISIBLE		0
474#define	__BSD_VISIBLE		0
475#define	__ISO_C_VISIBLE		1990
476#elif defined(_C99_SOURCE)	/* Localism to specify strict C99 env. */
477#define	__POSIX_VISIBLE		0
478#define	__XSI_VISIBLE		0
479#define	__BSD_VISIBLE		0
480#define	__ISO_C_VISIBLE		1999
481#else				/* Default environment: show everything. */
482#define	__POSIX_VISIBLE		200809
483#define	__XSI_VISIBLE		700
484#define	__BSD_VISIBLE		1
485#define	__ISO_C_VISIBLE		1999
486#endif
487#endif
488
489/*
490 * Default values.
491 */
492#ifndef __XPG_VISIBLE
493# define __XPG_VISIBLE          700
494#endif
495#ifndef __POSIX_VISIBLE
496# define __POSIX_VISIBLE        200809
497#endif
498#ifndef __ISO_C_VISIBLE
499# define __ISO_C_VISIBLE        1999
500#endif
501#ifndef __BSD_VISIBLE
502# define __BSD_VISIBLE          1
503#endif
504
505#define  __BIONIC__   1
506
507#endif /* !_SYS_CDEFS_H_ */
508