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