xref: /openbsd-src/sys/sys/cdefs.h (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: cdefs.h,v 1.39 2014/04/18 11:51:17 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 /*
44  * Macro to test if we're using a specific version of gcc or later.
45  */
46 #ifdef __GNUC__
47 #define __GNUC_PREREQ__(ma, mi) \
48 	((__GNUC__ > (ma)) || (__GNUC__ == (ma) && __GNUC_MINOR__ >= (mi)))
49 #else
50 #define __GNUC_PREREQ__(ma, mi) 0
51 #endif
52 
53 /*
54  * The __CONCAT macro is used to concatenate parts of symbol names, e.g.
55  * with "#define OLD(foo) __CONCAT(old,foo)", OLD(foo) produces oldfoo.
56  * The __CONCAT macro is a bit tricky -- make sure you don't put spaces
57  * in between its arguments.  Do not use __CONCAT on double-quoted strings,
58  * such as those from the __STRING macro: to concatenate strings just put
59  * them next to each other.
60  */
61 #if defined(__STDC__) || defined(__cplusplus)
62 #define	__P(protos)	protos		/* full-blown ANSI C */
63 #define	__CONCAT(x,y)	x ## y
64 #define	__STRING(x)	#x
65 
66 #define	__const		const		/* define reserved names to standard */
67 #define	__signed	signed
68 #define	__volatile	volatile
69 #if defined(__cplusplus) || defined(__PCC__)
70 #define	__inline	inline		/* convert to C++ keyword */
71 #else
72 #if !defined(__GNUC__)
73 #define	__inline			/* delete GCC keyword */
74 #endif /* !__GNUC__ */
75 #endif /* !__cplusplus */
76 
77 #else	/* !(__STDC__ || __cplusplus) */
78 #define	__P(protos)	()		/* traditional C preprocessor */
79 #define	__CONCAT(x,y)	x/**/y
80 #define	__STRING(x)	"x"
81 
82 #if !defined(__GNUC__)
83 #define	__const				/* delete pseudo-ANSI C keywords */
84 #define	__inline
85 #define	__signed
86 #define	__volatile
87 #endif	/* !__GNUC__ */
88 #endif	/* !(__STDC__ || __cplusplus) */
89 
90 /*
91  * GCC1 and some versions of GCC2 declare dead (non-returning) and
92  * pure (no side effects) functions using "volatile" and "const";
93  * unfortunately, these then cause warnings under "-ansi -pedantic".
94  * GCC >= 2.5 uses the __attribute__((attrs)) style.  All of these
95  * work for GNU C++ (modulo a slight glitch in the C++ grammar in
96  * the distribution version of 2.5.5).
97  */
98 
99 #if !__GNUC_PREREQ__(2, 5) && !defined(__PCC__)
100 #define	__attribute__(x)	/* delete __attribute__ if non-gcc or gcc1 */
101 #if defined(__GNUC__) && !defined(__STRICT_ANSI__)
102 #define	__dead		__volatile
103 #define	__pure		__const
104 #endif
105 #elif !defined(__STRICT_ANSI__)
106 #define __dead		__attribute__((__noreturn__))
107 #define __pure		__attribute__((__const__))
108 #endif
109 
110 #if __GNUC_PREREQ__(2, 7)
111 #define	__unused	__attribute__((__unused__))
112 #else
113 #define	__unused	/* delete */
114 #endif
115 
116 #if __GNUC_PREREQ__(3, 1)
117 #define	__used		__attribute__((__used__))
118 #else
119 #define	__used		__unused	/* suppress -Wunused warnings */
120 #endif
121 
122 #if __GNUC_PREREQ__(3,4)
123 # define __warn_unused_result	__attribute__((__warn_unused_result__))
124 #else
125 # define __warn_unused_result	/* delete */
126 #endif
127 
128 #if __GNUC_PREREQ__(3,3) && !defined(__clang__)
129 # define __bounded(args)	__attribute__ ((__bounded__ args ))
130 #else
131 # define __bounded(args)	/* delete */
132 #endif
133 
134 /*
135  * __returns_twice makes the compiler not assume the function
136  * only returns once.  This affects registerisation of variables:
137  * even local variables need to be in memory across such a call.
138  * Example: setjmp()
139  */
140 #if __GNUC_PREREQ__(4, 1)
141 #define __returns_twice	__attribute__((returns_twice))
142 #else
143 #define __returns_twice
144 #endif
145 
146 /*
147  * __only_inline makes the compiler only use this function definition
148  * for inlining; references that can't be inlined will be left as
149  * external references instead of generating a local copy.  The
150  * matching library should include a simple extern definition for
151  * the function to handle those references.  c.f. ctype.h
152  */
153 #ifdef __GNUC__
154 #  if __GNUC_PREREQ__(4, 2)
155 #define __only_inline	extern __inline __attribute__((__gnu_inline__))
156 #  else
157 #define __only_inline	extern __inline
158 #  endif
159 #else
160 #define __only_inline	static __inline
161 #endif
162 
163 /*
164  * GNU C version 2.96 adds explicit branch prediction so that
165  * the CPU back-end can hint the processor and also so that
166  * code blocks can be reordered such that the predicted path
167  * sees a more linear flow, thus improving cache behavior, etc.
168  *
169  * The following two macros provide us with a way to utilize this
170  * compiler feature.  Use __predict_true() if you expect the expression
171  * to evaluate to true, and __predict_false() if you expect the
172  * expression to evaluate to false.
173  *
174  * A few notes about usage:
175  *
176  *	* Generally, __predict_false() error condition checks (unless
177  *	  you have some _strong_ reason to do otherwise, in which case
178  *	  document it), and/or __predict_true() `no-error' condition
179  *	  checks, assuming you want to optimize for the no-error case.
180  *
181  *	* Other than that, if you don't know the likelihood of a test
182  *	  succeeding from empirical or other `hard' evidence, don't
183  *	  make predictions.
184  *
185  *	* These are meant to be used in places that are run `a lot'.
186  *	  It is wasteful to make predictions in code that is run
187  *	  seldomly (e.g. at subsystem initialization time) as the
188  *	  basic block reordering that this affects can often generate
189  *	  larger code.
190  */
191 #if __GNUC_PREREQ__(2, 96)
192 #define __predict_true(exp)	__builtin_expect(((exp) != 0), 1)
193 #define __predict_false(exp)	__builtin_expect(((exp) != 0), 0)
194 #else
195 #define __predict_true(exp)	((exp) != 0)
196 #define __predict_false(exp)	((exp) != 0)
197 #endif
198 
199 /* Delete pseudo-keywords wherever they are not available or needed. */
200 #ifndef __dead
201 #define	__dead
202 #define	__pure
203 #endif
204 
205 /*
206  * The __packed macro indicates that a variable or structure members
207  * should have the smallest possible alignment, despite any host CPU
208  * alignment requirements.
209  *
210  * The __aligned(x) macro specifies the minimum alignment of a
211  * variable or structure.
212  *
213  * These macros together are useful for describing the layout and
214  * alignment of messages exchanged with hardware or other systems.
215  */
216 
217 #if __GNUC_PREREQ__(2, 7) || defined(__PCC__)
218 #define	__packed	__attribute__((__packed__))
219 #define	__aligned(x)	__attribute__((__aligned__(x)))
220 #endif
221 
222 #if !__GNUC_PREREQ__(2, 8)
223 #define	__extension__
224 #endif
225 
226 #if __GNUC_PREREQ__(2, 8) || defined(__PCC__)
227 #define __statement(x)	__extension__(x)
228 #else
229 #define __statement(x)	(x)
230 #endif
231 
232 #if __GNUC_PREREQ__(3, 0)
233 #define	__malloc	__attribute__((__malloc__))
234 #else
235 #define	__malloc
236 #endif
237 
238 #if defined(__cplusplus)
239 #define	__BEGIN_EXTERN_C	extern "C" {
240 #define	__END_EXTERN_C		}
241 #else
242 #define	__BEGIN_EXTERN_C
243 #define	__END_EXTERN_C
244 #endif
245 
246 #if __GNUC_PREREQ__(4, 0)
247 #define	__dso_public	__attribute__((__visibility__("default")))
248 #define	__dso_hidden	__attribute__((__visibility__("hidden")))
249 #define	__BEGIN_PUBLIC_DECLS \
250 	_Pragma("GCC visibility push(default)") __BEGIN_EXTERN_C
251 #define	__END_PUBLIC_DECLS	__END_EXTERN_C _Pragma("GCC visibility pop")
252 #define	__BEGIN_HIDDEN_DECLS \
253 	_Pragma("GCC visibility push(hidden)") __BEGIN_EXTERN_C
254 #define	__END_HIDDEN_DECLS	__END_EXTERN_C _Pragma("GCC visibility pop")
255 #else
256 #define	__dso_public
257 #define	__dso_hidden
258 #define	__BEGIN_PUBLIC_DECLS	__BEGIN_EXTERN_C
259 #define	__END_PUBLIC_DECLS	__END_EXTERN_C
260 #define	__BEGIN_HIDDEN_DECLS	__BEGIN_EXTERN_C
261 #define	__END_HIDDEN_DECLS	__END_EXTERN_C
262 #endif
263 
264 #define	__BEGIN_DECLS	__BEGIN_EXTERN_C
265 #define	__END_DECLS	__END_EXTERN_C
266 
267 /*
268  * "The nice thing about standards is that there are so many to choose from."
269  * There are a number of "feature test macros" specified by (different)
270  * standards that determine which interfaces and types the header files
271  * should expose.
272  *
273  * Because of inconsistencies in these macros, we define our own
274  * set in the private name space that end in _VISIBLE.  These are
275  * always defined and so headers can test their values easily.
276  * Things can get tricky when multiple feature macros are defined.
277  * We try to take the union of all the features requested.
278  *
279  * The following macros are guaranteed to have a value after cdefs.h
280  * has been included:
281  *	__POSIX_VISIBLE
282  *	__XPG_VISIBLE
283  *	__ISO_C_VISIBLE
284  *	__BSD_VISIBLE
285  */
286 
287 /*
288  * X/Open Portability Guides and Single Unix Specifications.
289  * _XOPEN_SOURCE				XPG3
290  * _XOPEN_SOURCE && _XOPEN_VERSION = 4		XPG4
291  * _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED = 1	XPG4v2
292  * _XOPEN_SOURCE == 500				XPG5
293  * _XOPEN_SOURCE == 520				XPG5v2
294  * _XOPEN_SOURCE == 600				POSIX 1003.1-2001 with XSI
295  * _XOPEN_SOURCE == 700				POSIX 1003.1-2008 with XSI
296  *
297  * The XPG spec implies a specific value for _POSIX_C_SOURCE.
298  */
299 #ifdef _XOPEN_SOURCE
300 # if (_XOPEN_SOURCE - 0 >= 700)
301 #  define __XPG_VISIBLE		700
302 #  undef _POSIX_C_SOURCE
303 #  define _POSIX_C_SOURCE	200809L
304 # elif (_XOPEN_SOURCE - 0 >= 600)
305 #  define __XPG_VISIBLE		600
306 #  undef _POSIX_C_SOURCE
307 #  define _POSIX_C_SOURCE	200112L
308 # elif (_XOPEN_SOURCE - 0 >= 520)
309 #  define __XPG_VISIBLE		520
310 #  undef _POSIX_C_SOURCE
311 #  define _POSIX_C_SOURCE	199506L
312 # elif (_XOPEN_SOURCE - 0 >= 500)
313 #  define __XPG_VISIBLE		500
314 #  undef _POSIX_C_SOURCE
315 #  define _POSIX_C_SOURCE	199506L
316 # elif (_XOPEN_SOURCE_EXTENDED - 0 == 1)
317 #  define __XPG_VISIBLE		420
318 # elif (_XOPEN_VERSION - 0 >= 4)
319 #  define __XPG_VISIBLE		400
320 # else
321 #  define __XPG_VISIBLE		300
322 # endif
323 #endif
324 
325 /*
326  * POSIX macros, these checks must follow the XOPEN ones above.
327  *
328  * _POSIX_SOURCE == 1		1003.1-1988 (superseded by _POSIX_C_SOURCE)
329  * _POSIX_C_SOURCE == 1		1003.1-1990
330  * _POSIX_C_SOURCE == 2		1003.2-1992
331  * _POSIX_C_SOURCE == 199309L	1003.1b-1993
332  * _POSIX_C_SOURCE == 199506L   1003.1c-1995, 1003.1i-1995,
333  *				and the omnibus ISO/IEC 9945-1:1996
334  * _POSIX_C_SOURCE == 200112L   1003.1-2001
335  * _POSIX_C_SOURCE == 200809L   1003.1-2008
336  *
337  * The POSIX spec implies a specific value for __ISO_C_VISIBLE, though
338  * this may be overridden by the _ISOC99_SOURCE macro later.
339  */
340 #ifdef _POSIX_C_SOURCE
341 # if (_POSIX_C_SOURCE - 0 >= 200809)
342 #  define __POSIX_VISIBLE	200809
343 #  define __ISO_C_VISIBLE	1999
344 # elif (_POSIX_C_SOURCE - 0 >= 200112)
345 #  define __POSIX_VISIBLE	200112
346 #  define __ISO_C_VISIBLE	1999
347 # elif (_POSIX_C_SOURCE - 0 >= 199506)
348 #  define __POSIX_VISIBLE	199506
349 #  define __ISO_C_VISIBLE	1990
350 # elif (_POSIX_C_SOURCE - 0 >= 199309)
351 #  define __POSIX_VISIBLE	199309
352 #  define __ISO_C_VISIBLE	1990
353 # elif (_POSIX_C_SOURCE - 0 >= 2)
354 #  define __POSIX_VISIBLE	199209
355 #  define __ISO_C_VISIBLE	1990
356 # else
357 #  define __POSIX_VISIBLE	199009
358 #  define __ISO_C_VISIBLE	1990
359 # endif
360 #elif defined(_POSIX_SOURCE)
361 # define __POSIX_VISIBLE	198808
362 #  define __ISO_C_VISIBLE	0
363 #endif
364 
365 /*
366  * _ANSI_SOURCE means to expose ANSI C89 interfaces only.
367  * If the user defines it in addition to one of the POSIX or XOPEN
368  * macros, assume the POSIX/XOPEN macro(s) should take precedence.
369  */
370 #if defined(_ANSI_SOURCE) && !defined(__POSIX_VISIBLE) && \
371     !defined(__XPG_VISIBLE)
372 # define __POSIX_VISIBLE	0
373 # define __XPG_VISIBLE		0
374 # define __ISO_C_VISIBLE	1990
375 #endif
376 
377 /*
378  * _ISOC99_SOURCE and __STDC_VERSION__ override any of the other macros since
379  * they are non-exclusive.
380  */
381 #if defined(_ISOC99_SOURCE) || \
382     (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901) || \
383     (defined(__cplusplus) && __cplusplus >= 201103)
384 # undef __ISO_C_VISIBLE
385 # define __ISO_C_VISIBLE	1999
386 #endif
387 
388 /*
389  * Finally deal with BSD-specific interfaces that are not covered
390  * by any standards.  We expose these when none of the POSIX or XPG
391  * macros is defined or if the user explicitly asks for them.
392  */
393 #if !defined(_BSD_SOURCE) && \
394    (defined(_ANSI_SOURCE) || defined(__XPG_VISIBLE) || defined(__POSIX_VISIBLE))
395 # define __BSD_VISIBLE		0
396 #endif
397 
398 /*
399  * Default values.
400  */
401 #ifndef __XPG_VISIBLE
402 # define __XPG_VISIBLE		700
403 #endif
404 #ifndef __POSIX_VISIBLE
405 # define __POSIX_VISIBLE	200809
406 #endif
407 #ifndef __ISO_C_VISIBLE
408 # define __ISO_C_VISIBLE	1999
409 #endif
410 #ifndef __BSD_VISIBLE
411 # define __BSD_VISIBLE		1
412 #endif
413 
414 #endif /* !_SYS_CDEFS_H_ */
415