xref: /openbsd-src/sys/sys/cdefs.h (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: cdefs.h,v 1.28 2009/01/14 21:26:48 guenther Exp $	*/
2 /*	$NetBSD: cdefs.h,v 1.16 1996/04/03 20:46:39 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1991, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Berkeley Software Design, Inc.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)cdefs.h	8.7 (Berkeley) 1/21/94
36  */
37 
38 #ifndef	_SYS_CDEFS_H_
39 #define	_SYS_CDEFS_H_
40 
41 #include <machine/cdefs.h>
42 
43 #if defined(__cplusplus)
44 #define	__BEGIN_DECLS	extern "C" {
45 #define	__END_DECLS	}
46 #else
47 #define	__BEGIN_DECLS
48 #define	__END_DECLS
49 #endif
50 
51 /*
52  * Macro to test if we're using a specific version of gcc or later.
53  */
54 #ifdef __GNUC__
55 #define __GNUC_PREREQ__(ma, mi) \
56 	((__GNUC__ > (ma)) || (__GNUC__ == (ma) && __GNUC_MINOR__ >= (mi)))
57 #else
58 #define __GNUC_PREREQ__(ma, mi) 0
59 #endif
60 
61 /*
62  * The __CONCAT macro is used to concatenate parts of symbol names, e.g.
63  * with "#define OLD(foo) __CONCAT(old,foo)", OLD(foo) produces oldfoo.
64  * The __CONCAT macro is a bit tricky -- make sure you don't put spaces
65  * in between its arguments.  __CONCAT can also concatenate double-quoted
66  * strings produced by the __STRING macro, but this only works with ANSI C.
67  */
68 #if defined(__STDC__) || defined(__cplusplus)
69 #define	__P(protos)	protos		/* full-blown ANSI C */
70 #define	__CONCAT(x,y)	x ## y
71 #define	__STRING(x)	#x
72 
73 #define	__const		const		/* define reserved names to standard */
74 #define	__signed	signed
75 #define	__volatile	volatile
76 #if defined(__cplusplus) || defined(__PCC__)
77 #define	__inline	inline		/* convert to C++ keyword */
78 #else
79 #if !defined(__GNUC__) && !defined(lint)
80 #define	__inline			/* delete GCC keyword */
81 #endif /* !__GNUC__ && !lint */
82 #endif /* !__cplusplus */
83 
84 #else	/* !(__STDC__ || __cplusplus) */
85 #define	__P(protos)	()		/* traditional C preprocessor */
86 #define	__CONCAT(x,y)	x/**/y
87 #define	__STRING(x)	"x"
88 
89 #if !defined(__GNUC__) && !defined(lint)
90 #define	__const				/* delete pseudo-ANSI C keywords */
91 #define	__inline
92 #define	__signed
93 #define	__volatile
94 #endif	/* !__GNUC__ && !lint */
95 
96 /*
97  * In non-ANSI C environments, new programs will want ANSI-only C keywords
98  * deleted from the program and old programs will want them left alone.
99  * Programs using the ANSI C keywords const, inline etc. as normal
100  * identifiers should define -DNO_ANSI_KEYWORDS.
101  */
102 #ifndef	NO_ANSI_KEYWORDS
103 #define	const		__const		/* convert ANSI C keywords */
104 #define	inline		__inline
105 #define	signed		__signed
106 #define	volatile	__volatile
107 #endif /* !NO_ANSI_KEYWORDS */
108 #endif	/* !(__STDC__ || __cplusplus) */
109 
110 /*
111  * GCC1 and some versions of GCC2 declare dead (non-returning) and
112  * pure (no side effects) functions using "volatile" and "const";
113  * unfortunately, these then cause warnings under "-ansi -pedantic".
114  * GCC >= 2.5 uses the __attribute__((attrs)) style.  All of these
115  * work for GNU C++ (modulo a slight glitch in the C++ grammar in
116  * the distribution version of 2.5.5).
117  */
118 
119 #if !__GNUC_PREREQ__(2, 5) && !defined(__PCC__)
120 #define	__attribute__(x)	/* delete __attribute__ if non-gcc or gcc1 */
121 #if defined(__GNUC__) && !defined(__STRICT_ANSI__)
122 #define	__dead		__volatile
123 #define	__pure		__const
124 #elif defined(lint)
125 #define __dead		/* NORETURN */
126 #endif
127 #elif !defined(__STRICT_ANSI__)
128 #define __dead		__attribute__((__noreturn__))
129 #define __pure		__attribute__((__const__))
130 #endif
131 
132 /*
133  * GNU C version 2.96 adds explicit branch prediction so that
134  * the CPU back-end can hint the processor and also so that
135  * code blocks can be reordered such that the predicted path
136  * sees a more linear flow, thus improving cache behavior, etc.
137  *
138  * The following two macros provide us with a way to utilize this
139  * compiler feature.  Use __predict_true() if you expect the expression
140  * to evaluate to true, and __predict_false() if you expect the
141  * expression to evaluate to false.
142  *
143  * A few notes about usage:
144  *
145  *	* Generally, __predict_false() error condition checks (unless
146  *	  you have some _strong_ reason to do otherwise, in which case
147  *	  document it), and/or __predict_true() `no-error' condition
148  *	  checks, assuming you want to optimize for the no-error case.
149  *
150  *	* Other than that, if you don't know the likelihood of a test
151  *	  succeeding from empirical or other `hard' evidence, don't
152  *	  make predictions.
153  *
154  *	* These are meant to be used in places that are run `a lot'.
155  *	  It is wasteful to make predictions in code that is run
156  *	  seldomly (e.g. at subsystem initialization time) as the
157  *	  basic block reordering that this affects can often generate
158  *	  larger code.
159  */
160 #if __GNUC_PREREQ__(2, 96)
161 #define __predict_true(exp)	__builtin_expect(((exp) != 0), 1)
162 #define __predict_false(exp)	__builtin_expect(((exp) != 0), 0)
163 #else
164 #define __predict_true(exp)	((exp) != 0)
165 #define __predict_false(exp)	((exp) != 0)
166 #endif
167 
168 /* Delete pseudo-keywords wherever they are not available or needed. */
169 #ifndef __dead
170 #define	__dead
171 #define	__pure
172 #endif
173 
174 #if __GNUC_PREREQ__(2, 7) || defined(__PCC__)
175 #define	__packed	__attribute__((__packed__))
176 #elif defined(lint)
177 #define	__packed
178 #endif
179 
180 #if !__GNUC_PREREQ__(2, 8)
181 #define	__extension__
182 #endif
183 
184 #if __GNUC_PREREQ__(2, 8) || defined(__PCC__)
185 #define __statement(x)	__extension__(x)
186 #elif defined(lint)
187 #define __statement(x)	(0)
188 #else
189 #define __statement(x)	(x)
190 #endif
191 
192 #if __GNUC_PREREQ__(3, 0)
193 #define	__malloc	__attribute__((__malloc__))
194 #else
195 #define	__malloc
196 #endif
197 
198 /*
199  * "The nice thing about standards is that there are so many to choose from."
200  * There are a number of "feature test macros" specified by (different)
201  * standards that determine which interfaces and types the header files
202  * should expose.
203  *
204  * Because of inconsistencies in these macros, we define our own
205  * set in the private name space that end in _VISIBLE.  These are
206  * always defined and so headers can test their values easily.
207  * Things can get tricky when multiple feature macros are defined.
208  * We try to take the union of all the features requested.
209  *
210  * The following macros are guaranteed to have a value after cdefs.h
211  * has been included:
212  *	__POSIX_VISIBLE
213  *	__XPG_VISIBLE
214  *	__ISO_C_VISIBLE
215  *	__BSD_VISIBLE
216  */
217 
218 /*
219  * X/Open Portability Guides and Single Unix Specifications.
220  * _XOPEN_SOURCE				XPG3
221  * _XOPEN_SOURCE && _XOPEN_VERSION = 4		XPG4
222  * _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED = 1	XPG4v2
223  * _XOPEN_SOURCE == 500				XPG5
224  * _XOPEN_SOURCE == 520				XPG5v2
225  * _XOPEN_SOURCE == 600				POSIX 1003.1-2001 with XSI
226  * _XOPEN_SOURCE == 700				POSIX 1003.1-2008 with XSI
227  *
228  * The XPG spec implies a specific value for _POSIX_C_SOURCE.
229  */
230 #ifdef _XOPEN_SOURCE
231 # if (_XOPEN_SOURCE - 0 >= 700)
232 #  define __XPG_VISIBLE		700
233 #  undef _POSIX_C_SOURCE
234 #  define _POSIX_C_SOURCE	200809L
235 # elif (_XOPEN_SOURCE - 0 >= 600)
236 #  define __XPG_VISIBLE		600
237 #  undef _POSIX_C_SOURCE
238 #  define _POSIX_C_SOURCE	200112L
239 # elif (_XOPEN_SOURCE - 0 >= 520)
240 #  define __XPG_VISIBLE		520
241 #  undef _POSIX_C_SOURCE
242 #  define _POSIX_C_SOURCE	199506L
243 # elif (_XOPEN_SOURCE - 0 >= 500)
244 #  define __XPG_VISIBLE		500
245 #  undef _POSIX_C_SOURCE
246 #  define _POSIX_C_SOURCE	199506L
247 # elif (_XOPEN_SOURCE_EXTENDED - 0 == 1)
248 #  define __XPG_VISIBLE		420
249 # elif (_XOPEN_VERSION - 0 >= 4)
250 #  define __XPG_VISIBLE		400
251 # else
252 #  define __XPG_VISIBLE		300
253 # endif
254 #endif
255 
256 /*
257  * POSIX macros, these checks must follow the XOPEN ones above.
258  *
259  * _POSIX_SOURCE == 1		1003.1-1988 (superseded by _POSIX_C_SOURCE)
260  * _POSIX_C_SOURCE == 1		1003.1-1990
261  * _POSIX_C_SOURCE == 2		1003.2-1992
262  * _POSIX_C_SOURCE == 199309L	1003.1b-1993
263  * _POSIX_C_SOURCE == 199506L   1003.1c-1995, 1003.1i-1995,
264  *				and the omnibus ISO/IEC 9945-1:1996
265  * _POSIX_C_SOURCE == 200112L   1003.1-2001
266  * _POSIX_C_SOURCE == 200809L   1003.1-2008
267  *
268  * The POSIX spec implies a specific value for __ISO_C_VISIBLE, though
269  * this may be overridden by the _ISOC99_SOURCE macro later.
270  */
271 #ifdef _POSIX_C_SOURCE
272 # if (_POSIX_C_SOURCE - 0 >= 200809)
273 #  define __POSIX_VISIBLE	200809
274 #  define __ISO_C_VISIBLE	1999
275 # elif (_POSIX_C_SOURCE - 0 >= 200112)
276 #  define __POSIX_VISIBLE	200112
277 #  define __ISO_C_VISIBLE	1999
278 # elif (_POSIX_C_SOURCE - 0 >= 199506)
279 #  define __POSIX_VISIBLE	199506
280 #  define __ISO_C_VISIBLE	1990
281 # elif (_POSIX_C_SOURCE - 0 >= 199309)
282 #  define __POSIX_VISIBLE	199309
283 #  define __ISO_C_VISIBLE	1990
284 # elif (_POSIX_C_SOURCE - 0 >= 2)
285 #  define __POSIX_VISIBLE	199209
286 #  define __ISO_C_VISIBLE	1990
287 # else
288 #  define __POSIX_VISIBLE	199009
289 #  define __ISO_C_VISIBLE	1990
290 # endif
291 #elif defined(_POSIX_SOURCE)
292 # define __POSIX_VISIBLE	198808
293 #  define __ISO_C_VISIBLE	0
294 #endif
295 
296 /*
297  * _ANSI_SOURCE means to expose ANSI C89 interfaces only.
298  * If the user defines it in addition to one of the POSIX or XOPEN
299  * macros, assume the POSIX/XOPEN macro(s) should take precedence.
300  */
301 #if defined(_ANSI_SOURCE) && !defined(__POSIX_VISIBLE) && \
302     !defined(__XPG_VISIBLE)
303 # define __POSIX_VISIBLE	0
304 # define __XPG_VISIBLE		0
305 # define __ISO_C_VISIBLE	1990
306 #endif
307 
308 /*
309  * _ISOC99_SOURCE and __STDC_VERSION__ override any of the other macros since
310  * they are non-exclusive.
311  */
312 #if defined(_ISOC99_SOURCE) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901)
313 # undef __ISO_C_VISIBLE
314 # define __ISO_C_VISIBLE	1999
315 #endif
316 
317 /*
318  * Finally deal with BSD-specific interfaces that are not covered
319  * by any standards.  We expose these when none of the POSIX or XPG
320  * macros is defined or if the user explicitly asks for them.
321  */
322 #if !defined(_BSD_SOURCE) && \
323    (defined(_ANSI_SOURCE) || defined(__XPG_VISIBLE) || defined(__POSIX_VISIBLE))
324 # define __BSD_VISIBLE		0
325 #endif
326 
327 /*
328  * Default values.
329  */
330 #ifndef __XPG_VISIBLE
331 # define __XPG_VISIBLE		700
332 #endif
333 #ifndef __POSIX_VISIBLE
334 # define __POSIX_VISIBLE	200809
335 #endif
336 #ifndef __ISO_C_VISIBLE
337 # define __ISO_C_VISIBLE	1999
338 #endif
339 #ifndef __BSD_VISIBLE
340 # define __BSD_VISIBLE		1
341 #endif
342 
343 #endif /* !_SYS_CDEFS_H_ */
344