xref: /openbsd/sys/sys/cdefs.h (revision ad4b79de)
1 /*	$OpenBSD: cdefs.h,v 1.44 2024/07/30 05:57:31 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 #else
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__(3, 0)
227 #define	__malloc	__attribute__((__malloc__))
228 #else
229 #define	__malloc
230 #endif
231 
232 #if defined(__cplusplus)
233 #define	__BEGIN_EXTERN_C	extern "C" {
234 #define	__END_EXTERN_C		}
235 #else
236 #define	__BEGIN_EXTERN_C
237 #define	__END_EXTERN_C
238 #endif
239 
240 #if __GNUC_PREREQ__(4, 0)
241 #define	__dso_public	__attribute__((__visibility__("default")))
242 #define	__dso_hidden	__attribute__((__visibility__("hidden")))
243 #define	__BEGIN_PUBLIC_DECLS \
244 	_Pragma("GCC visibility push(default)") __BEGIN_EXTERN_C
245 #define	__END_PUBLIC_DECLS	__END_EXTERN_C _Pragma("GCC visibility pop")
246 #define	__BEGIN_HIDDEN_DECLS \
247 	_Pragma("GCC visibility push(hidden)") __BEGIN_EXTERN_C
248 #define	__END_HIDDEN_DECLS	__END_EXTERN_C _Pragma("GCC visibility pop")
249 #else
250 #define	__dso_public
251 #define	__dso_hidden
252 #define	__BEGIN_PUBLIC_DECLS	__BEGIN_EXTERN_C
253 #define	__END_PUBLIC_DECLS	__END_EXTERN_C
254 #define	__BEGIN_HIDDEN_DECLS	__BEGIN_EXTERN_C
255 #define	__END_HIDDEN_DECLS	__END_EXTERN_C
256 #endif
257 
258 #define	__BEGIN_DECLS	__BEGIN_EXTERN_C
259 #define	__END_DECLS	__END_EXTERN_C
260 
261 /*
262  * "The nice thing about standards is that there are so many to choose from."
263  * There are a number of "feature test macros" specified by (different)
264  * standards that determine which interfaces and types the header files
265  * should expose.
266  *
267  * Because of inconsistencies in these macros, we define our own
268  * set in the private name space that end in _VISIBLE.  These are
269  * always defined and so headers can test their values easily.
270  * Things can get tricky when multiple feature macros are defined.
271  * We try to take the union of all the features requested.
272  *
273  * The following macros are guaranteed to have a value after cdefs.h
274  * has been included:
275  *	__POSIX_VISIBLE
276  *	__XPG_VISIBLE
277  *	__ISO_C_VISIBLE
278  *	__BSD_VISIBLE
279  */
280 
281 /*
282  * X/Open Portability Guides and Single Unix Specifications.
283  * _XOPEN_SOURCE				XPG3
284  * _XOPEN_SOURCE && _XOPEN_VERSION = 4		XPG4
285  * _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED = 1	XPG4v2
286  * _XOPEN_SOURCE == 500				XPG5
287  * _XOPEN_SOURCE == 520				XPG5v2
288  * _XOPEN_SOURCE == 600				POSIX 1003.1-2001 with XSI
289  * _XOPEN_SOURCE == 700				POSIX 1003.1-2008 with XSI
290  * _XOPEN_SOURCE == 800				POSIX 1003.1-2024 with XSI
291  *
292  * The XPG spec implies a specific value for _POSIX_C_SOURCE.
293  */
294 #ifdef _XOPEN_SOURCE
295 # if (_XOPEN_SOURCE - 0 >= 800)
296 #  define __XPG_VISIBLE		800
297 #  undef _POSIX_C_SOURCE
298 #  define _POSIX_C_SOURCE	202405L
299 # elif (_XOPEN_SOURCE - 0 >= 700)
300 #  define __XPG_VISIBLE		700
301 #  undef _POSIX_C_SOURCE
302 #  define _POSIX_C_SOURCE	200809L
303 # elif (_XOPEN_SOURCE - 0 >= 600)
304 #  define __XPG_VISIBLE		600
305 #  undef _POSIX_C_SOURCE
306 #  define _POSIX_C_SOURCE	200112L
307 # elif (_XOPEN_SOURCE - 0 >= 520)
308 #  define __XPG_VISIBLE		520
309 #  undef _POSIX_C_SOURCE
310 #  define _POSIX_C_SOURCE	199506L
311 # elif (_XOPEN_SOURCE - 0 >= 500)
312 #  define __XPG_VISIBLE		500
313 #  undef _POSIX_C_SOURCE
314 #  define _POSIX_C_SOURCE	199506L
315 # elif (_XOPEN_SOURCE_EXTENDED - 0 == 1)
316 #  define __XPG_VISIBLE		420
317 # elif (_XOPEN_VERSION - 0 >= 4)
318 #  define __XPG_VISIBLE		400
319 # else
320 #  define __XPG_VISIBLE		300
321 # endif
322 #endif
323 
324 /*
325  * POSIX macros, these checks must follow the XOPEN ones above.
326  *
327  * _POSIX_SOURCE == 1		1003.1-1988 (superseded by _POSIX_C_SOURCE)
328  * _POSIX_C_SOURCE == 1		1003.1-1990
329  * _POSIX_C_SOURCE == 2		1003.2-1992
330  * _POSIX_C_SOURCE == 199309L	1003.1b-1993
331  * _POSIX_C_SOURCE == 199506L   1003.1c-1995, 1003.1i-1995,
332  *				and the omnibus ISO/IEC 9945-1:1996
333  * _POSIX_C_SOURCE == 200112L   1003.1-2001
334  * _POSIX_C_SOURCE == 200809L   1003.1-2008
335  * _POSIX_C_SOURCE == 202405L   1003.1-2024
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 >= 202405)
342 #  define __POSIX_VISIBLE	202405
343 #  define __ISO_C_VISIBLE	2017
344 # elif (_POSIX_C_SOURCE - 0 >= 200809)
345 #  define __POSIX_VISIBLE	200809
346 #  define __ISO_C_VISIBLE	1999
347 # elif (_POSIX_C_SOURCE - 0 >= 200112)
348 #  define __POSIX_VISIBLE	200112
349 #  define __ISO_C_VISIBLE	1999
350 # elif (_POSIX_C_SOURCE - 0 >= 199506)
351 #  define __POSIX_VISIBLE	199506
352 #  define __ISO_C_VISIBLE	1990
353 # elif (_POSIX_C_SOURCE - 0 >= 199309)
354 #  define __POSIX_VISIBLE	199309
355 #  define __ISO_C_VISIBLE	1990
356 # elif (_POSIX_C_SOURCE - 0 >= 2)
357 #  define __POSIX_VISIBLE	199209
358 #  define __ISO_C_VISIBLE	1990
359 # else
360 #  define __POSIX_VISIBLE	199009
361 #  define __ISO_C_VISIBLE	1990
362 # endif
363 #elif defined(_POSIX_SOURCE)
364 # define __POSIX_VISIBLE	198808
365 #  define __ISO_C_VISIBLE	0
366 #endif
367 
368 /*
369  * _ANSI_SOURCE means to expose ANSI C89 interfaces only.
370  * If the user defines it in addition to one of the POSIX or XOPEN
371  * macros, assume the POSIX/XOPEN macro(s) should take precedence.
372  */
373 #if defined(_ANSI_SOURCE) && !defined(__POSIX_VISIBLE) && \
374     !defined(__XPG_VISIBLE)
375 # define __POSIX_VISIBLE	0
376 # define __XPG_VISIBLE		0
377 # define __ISO_C_VISIBLE	1990
378 #endif
379 
380 /*
381  * _ISOC99_SOURCE, _ISOC11_SOURCE, __STDC_VERSION__, and __cplusplus
382  * override any of the other macros since they are non-exclusive.
383  */
384 #if defined(_ISOC11_SOURCE) || \
385     (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112) || \
386     (defined(__cplusplus) && __cplusplus >= 201703)
387 # undef __ISO_C_VISIBLE
388 # define __ISO_C_VISIBLE	2011
389 #elif defined(_ISOC99_SOURCE) || \
390     (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901) || \
391     (defined(__cplusplus) && __cplusplus >= 201103)
392 # undef __ISO_C_VISIBLE
393 # define __ISO_C_VISIBLE	1999
394 #endif
395 
396 /*
397  * Finally deal with BSD-specific interfaces that are not covered
398  * by any standards.  We expose these when none of the POSIX or XPG
399  * macros is defined or if the user explicitly asks for them.
400  */
401 #if !defined(_BSD_SOURCE) && \
402    (defined(_ANSI_SOURCE) || defined(__XPG_VISIBLE) || defined(__POSIX_VISIBLE))
403 # define __BSD_VISIBLE		0
404 #endif
405 
406 /*
407  * Default values.
408  */
409 #ifndef __XPG_VISIBLE
410 # define __XPG_VISIBLE		800
411 #endif
412 #ifndef __POSIX_VISIBLE
413 # define __POSIX_VISIBLE	202405
414 #endif
415 #ifndef __ISO_C_VISIBLE
416 # define __ISO_C_VISIBLE	2017
417 #endif
418 #ifndef __BSD_VISIBLE
419 # define __BSD_VISIBLE		1
420 #endif
421 
422 #endif /* !_SYS_CDEFS_H_ */
423