cdefs.h revision 117837
11541Srgrimes/*
21541Srgrimes * Copyright (c) 1991, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * This code is derived from software contributed to Berkeley by
61541Srgrimes * Berkeley Software Design, Inc.
71541Srgrimes *
81541Srgrimes * Redistribution and use in source and binary forms, with or without
91541Srgrimes * modification, are permitted provided that the following conditions
101541Srgrimes * are met:
111541Srgrimes * 1. Redistributions of source code must retain the above copyright
121541Srgrimes *    notice, this list of conditions and the following disclaimer.
131541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141541Srgrimes *    notice, this list of conditions and the following disclaimer in the
151541Srgrimes *    documentation and/or other materials provided with the distribution.
161541Srgrimes * 3. All advertising materials mentioning features or use of this software
171541Srgrimes *    must display the following acknowledgement:
181541Srgrimes *	This product includes software developed by the University of
191541Srgrimes *	California, Berkeley and its contributors.
201541Srgrimes * 4. Neither the name of the University nor the names of its contributors
211541Srgrimes *    may be used to endorse or promote products derived from this software
221541Srgrimes *    without specific prior written permission.
231541Srgrimes *
241541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341541Srgrimes * SUCH DAMAGE.
351541Srgrimes *
3614495Shsu *	@(#)cdefs.h	8.8 (Berkeley) 1/9/95
3750477Speter * $FreeBSD: head/sys/sys/cdefs.h 117837 2003-07-21 18:35:51Z phk $
381541Srgrimes */
391541Srgrimes
402165Spaul#ifndef	_SYS_CDEFS_H_
412165Spaul#define	_SYS_CDEFS_H_
421541Srgrimes
431541Srgrimes#if defined(__cplusplus)
441541Srgrimes#define	__BEGIN_DECLS	extern "C" {
4536449Sdt#define	__END_DECLS	}
461541Srgrimes#else
471541Srgrimes#define	__BEGIN_DECLS
481541Srgrimes#define	__END_DECLS
491541Srgrimes#endif
501541Srgrimes
511541Srgrimes/*
521541Srgrimes * The __CONCAT macro is used to concatenate parts of symbol names, e.g.
531541Srgrimes * with "#define OLD(foo) __CONCAT(old,foo)", OLD(foo) produces oldfoo.
5436971Sbde * The __CONCAT macro is a bit tricky to use if it must work in non-ANSI
5536971Sbde * mode -- there must be no spaces between its arguments, and for nested
5636971Sbde * __CONCAT's, all the __CONCAT's must be at the left.  __CONCAT can also
5736971Sbde * concatenate double-quoted strings produced by the __STRING macro, but
5836971Sbde * this only works with ANSI C.
5925083Sjdp *
6025083Sjdp * __XSTRING is like __STRING, but it expands any macros in its argument
6125083Sjdp * first.  It is only available with ANSI C.
621541Srgrimes */
631541Srgrimes#if defined(__STDC__) || defined(__cplusplus)
641541Srgrimes#define	__P(protos)	protos		/* full-blown ANSI C */
655012Sbde#define	__CONCAT1(x,y)	x ## y
665012Sbde#define	__CONCAT(x,y)	__CONCAT1(x,y)
6725083Sjdp#define	__STRING(x)	#x		/* stringify without expanding x */
6825083Sjdp#define	__XSTRING(x)	__STRING(x)	/* expand x, then stringify */
691541Srgrimes
701541Srgrimes#define	__const		const		/* define reserved names to standard */
711541Srgrimes#define	__signed	signed
721541Srgrimes#define	__volatile	volatile
731541Srgrimes#if defined(__cplusplus)
741541Srgrimes#define	__inline	inline		/* convert to C++ keyword */
751541Srgrimes#else
761541Srgrimes#ifndef __GNUC__
771541Srgrimes#define	__inline			/* delete GCC keyword */
781541Srgrimes#endif /* !__GNUC__ */
791541Srgrimes#endif /* !__cplusplus */
801541Srgrimes
811541Srgrimes#else	/* !(__STDC__ || __cplusplus) */
821541Srgrimes#define	__P(protos)	()		/* traditional C preprocessor */
831541Srgrimes#define	__CONCAT(x,y)	x/**/y
841541Srgrimes#define	__STRING(x)	"x"
851541Srgrimes
861541Srgrimes#ifndef __GNUC__
871541Srgrimes#define	__const				/* delete pseudo-ANSI C keywords */
881541Srgrimes#define	__inline
891541Srgrimes#define	__signed
901541Srgrimes#define	__volatile
911541Srgrimes/*
921541Srgrimes * In non-ANSI C environments, new programs will want ANSI-only C keywords
931541Srgrimes * deleted from the program and old programs will want them left alone.
941541Srgrimes * When using a compiler other than gcc, programs using the ANSI C keywords
951541Srgrimes * const, inline etc. as normal identifiers should define -DNO_ANSI_KEYWORDS.
961541Srgrimes * When using "gcc -traditional", we assume that this is the intent; if
971541Srgrimes * __GNUC__ is defined but __STDC__ is not, we leave the new keywords alone.
981541Srgrimes */
991541Srgrimes#ifndef	NO_ANSI_KEYWORDS
1001541Srgrimes#define	const				/* delete ANSI C keywords */
1011541Srgrimes#define	inline
1021541Srgrimes#define	signed
1031541Srgrimes#define	volatile
1045209Snate#endif	/* !NO_ANSI_KEYWORDS */
10515243Sbde#endif	/* !__GNUC__ */
1061541Srgrimes#endif	/* !(__STDC__ || __cplusplus) */
1071541Srgrimes
1081541Srgrimes/*
10938509Sbde * Compiler-dependent macros to help declare dead (non-returning) and
11038509Sbde * pure (no side effects) functions, and unused variables.  They are
11138509Sbde * null except for versions of gcc that are known to support the features
11238509Sbde * properly (old versions of gcc-2 supported the dead and pure features
113103845Speter * in a different (wrong) way).  If we do not provide an implementation
114103845Speter * for a given compiler, let the compile fail if it is told to use
115103845Speter * a feature that we cannot live without.
1161541Srgrimes */
117103845Speter#ifdef lint
11883443Sasmodai#define	__dead2
11983443Sasmodai#define	__pure2
12083443Sasmodai#define	__unused
121103834Speter#define	__packed
122103836Speter#define	__aligned(x)
123103841Speter#define	__section(x)
124103845Speter#else
125103845Speter#if __GNUC__ < 2 || __GNUC__ == 2 && __GNUC_MINOR__ < 5
126103845Speter#define	__dead2
127103845Speter#define	__pure2
128103845Speter#define	__unused
1292059Sdg#endif
13017648Speter#if __GNUC__ == 2 && __GNUC_MINOR__ >= 5 && __GNUC_MINOR__ < 7
13183443Sasmodai#define	__dead2		__attribute__((__noreturn__))
13283443Sasmodai#define	__pure2		__attribute__((__const__))
13383443Sasmodai#define	__unused
134103845Speter/* XXX Find out what to do for __packed, __aligned and __section */
1351541Srgrimes#endif
13677631Sobrien#if __GNUC__ == 2 && __GNUC_MINOR__ >= 7 || __GNUC__ == 3
13783443Sasmodai#define	__dead2		__attribute__((__noreturn__))
13883443Sasmodai#define	__pure2		__attribute__((__const__))
13983443Sasmodai#define	__unused	__attribute__((__unused__))
140103834Speter#define	__packed	__attribute__((__packed__))
141103834Speter#define	__aligned(x)	__attribute__((__aligned__(x)))
142103836Speter#define	__section(x)	__attribute__((__section__(x)))
14317648Speter#endif
144103845Speter#endif
1451541Srgrimes
146117837Sphk#if __GNUC__ == 3 && __GNUC_MINOR__ >= 3
147117837Sphk#define __nonnull(x)	__attribute__((__nonnull__(x)))
148117837Sphk#else
149117837Sphk#define __nonnull(x)
150117837Sphk#endif
151117837Sphk
15285672Smike/* XXX: should use `#if __STDC_VERSION__ < 199901'. */
15385672Smike#if !(__GNUC__ == 2 && __GNUC_MINOR__ >= 7 || __GNUC__ >= 3)
15485672Smike#define	__func__	NULL
15585672Smike#endif
15685672Smike
157103345Sbde#if __GNUC__ >= 2 && !defined(__STRICT_ANSI__) || __STDC_VERSION__ >= 199901
15886302Smike#define	__LONG_LONG_SUPPORTED
15986302Smike#endif
16086302Smike
16138509Sbde/*
162104591Smike * GCC 2.95 provides `__restrict' as an extension to C90 to support the
163103370Swollman * C99-specific `restrict' type qualifier.  We happen to use `__restrict' as
164103370Swollman * a way to define the `restrict' type qualifier without disturbing older
165103370Swollman * software that is unaware of C99 keywords.
16686803Smike */
16786803Smike#if !(__GNUC__ == 2 && __GNUC_MINOR__ == 95)
16886803Smike#if __STDC_VERSION__ < 199901
16986803Smike#define	__restrict
17086803Smike#else
17186803Smike#define	__restrict	restrict
17286803Smike#endif
17386803Smike#endif
17486803Smike
17586803Smike/*
17699594Smike * We define this here since <stddef.h>, <sys/queue.h>, and <sys/types.h>
17799594Smike * require it.
17899594Smike */
17999594Smike#define	__offsetof(type, field)	((size_t)(&((type *)0)->field))
18099594Smike
18199594Smike/*
18238509Sbde * Compiler-dependent macros to declare that functions take printf-like
18338509Sbde * or scanf-like arguments.  They are null except for versions of gcc
18438509Sbde * that are known to support the features properly (old versions of gcc-2
18538509Sbde * didn't permit keeping the keywords out of the application namespace).
18638509Sbde */
18737614Sbde#if __GNUC__ < 2 || __GNUC__ == 2 && __GNUC_MINOR__ < 7
18837614Sbde#define	__printflike(fmtarg, firstvararg)
18937614Sbde#define	__scanflike(fmtarg, firstvararg)
19037614Sbde#else
19137614Sbde#define	__printflike(fmtarg, firstvararg) \
19237614Sbde	    __attribute__((__format__ (__printf__, fmtarg, firstvararg)))
19337614Sbde#define	__scanflike(fmtarg, firstvararg) \
19437614Sbde	    __attribute__((__format__ (__scanf__, fmtarg, firstvararg)))
19537614Sbde#endif
19637614Sbde
19740716Sjdp/* Compiler-dependent macros that rely on FreeBSD-specific extensions. */
198113679Sbde#if __FreeBSD_cc_version >= 300001
19940716Sjdp#define	__printf0like(fmtarg, firstvararg) \
20040716Sjdp	    __attribute__((__format__ (__printf0__, fmtarg, firstvararg)))
20140716Sjdp#else
20240716Sjdp#define	__printf0like(fmtarg, firstvararg)
20340716Sjdp#endif
20440716Sjdp
2055209Snate#ifdef __GNUC__
20683443Sasmodai#define	__strong_reference(sym,aliassym)	\
20758256Sjasone	extern __typeof (sym) aliassym __attribute__ ((__alias__ (#sym)));
2085209Snate#ifdef __STDC__
20937052Speter#define	__weak_reference(sym,alias)	\
21037052Speter	__asm__(".weak " #alias);	\
21137862Sdfr	__asm__(".equ "  #alias ", " #sym)
21237052Speter#define	__warn_references(sym,msg)	\
21337052Speter	__asm__(".section .gnu.warning." #sym);	\
21439502Sjdp	__asm__(".asciz \"" msg "\"");	\
21537052Speter	__asm__(".previous")
21637052Speter#else
21737052Speter#define	__weak_reference(sym,alias)	\
21837052Speter	__asm__(".weak alias");		\
21937862Sdfr	__asm__(".equ alias, sym")
22037052Speter#define	__warn_references(sym,msg)	\
22137052Speter	__asm__(".section .gnu.warning.sym"); \
22239502Sjdp	__asm__(".asciz \"msg\"");	\
22337052Speter	__asm__(".previous")
22437052Speter#endif	/* __STDC__ */
22537052Speter#endif	/* __GNUC__ */
2265209Snate
22783049Speter#ifdef __GNUC__
22850614Speter#define	__IDSTRING(name,string)	__asm__(".ident\t\"" string "\"")
22950614Speter#else
23083049Speter/*
23183049Speter * This doesn't work in header files. But it may be better than nothing.
23283056Speter * The alternative is: #define __IDSTRING(name,string)  [nothing]
23383049Speter */
23436971Sbde#define	__IDSTRING(name,string)	static const char name[] __unused = string
23550614Speter#endif
23631707Spst
23783552Sdillon/*
23883552Sdillon * Embed the rcs id of a source file in the resulting library.  Note that in
23983552Sdillon * more recent ELF binutils, we use .ident allowing the ID to be stripped.
24083552Sdillon * Usage:
24183552Sdillon *	__FBSDID("$FreeBSD: head/sys/sys/cdefs.h 117837 2003-07-21 18:35:51Z phk $");
24283552Sdillon */
24383489Sdillon#ifndef	__FBSDID
24483528Sobrien#if !defined(lint) && !defined(STRIP_FBSDID)
24583526Sobrien#define	__FBSDID(s)	__IDSTRING(__CONCAT(__rcsid_,__LINE__),s)
24683489Sdillon#else
24783552Sdillon#define	__FBSDID(s)	struct __hack
24883489Sdillon#endif
24983489Sdillon#endif
25083489Sdillon
25193996Sobrien#ifndef	__RCSID
25293996Sobrien#ifndef	NO__RCSID
25393993Sobrien#define	__RCSID(s)	__IDSTRING(__CONCAT(__rcsid_,__LINE__),s)
25493996Sobrien#else
25593996Sobrien#define	__RCSID(s)
25693993Sobrien#endif
25793996Sobrien#endif
25893993Sobrien
25993996Sobrien#ifndef	__RCSID_SOURCE
26093996Sobrien#ifndef	NO__RCSID_SOURCE
26183049Speter#define	__RCSID_SOURCE(s)	__IDSTRING(__CONCAT(__rcsid_source_,__LINE__),s)
26293996Sobrien#else
26393996Sobrien#define	__RCSID_SOURCE(s)
26431707Spst#endif
26593996Sobrien#endif
26631707Spst
26793996Sobrien#ifndef	__SCCSID
26893996Sobrien#ifndef	NO__SCCSID
26993996Sobrien#define	__SCCSID(s)	__IDSTRING(__CONCAT(__sccsid_,__LINE__),s)
27093996Sobrien#else
27193996Sobrien#define	__SCCSID(s)
27293996Sobrien#endif
27393996Sobrien#endif
27493996Sobrien
27593996Sobrien#ifndef	__COPYRIGHT
27693996Sobrien#ifndef	NO__COPYRIGHT
27783049Speter#define	__COPYRIGHT(s)	__IDSTRING(__CONCAT(__copyright_,__LINE__),s)
27893996Sobrien#else
27993996Sobrien#define	__COPYRIGHT(s)
28031707Spst#endif
28193996Sobrien#endif
28231707Spst
28374070Sbillf#ifndef	__DECONST
28474070Sbillf#define	__DECONST(type, var)	((type)(uintptr_t)(const void *)(var))
28574070Sbillf#endif
28674070Sbillf
28774070Sbillf#ifndef	__DEVOLATILE
28874070Sbillf#define	__DEVOLATILE(type, var)	((type)(uintptr_t)(volatile void *)(var))
28974070Sbillf#endif
29074070Sbillf
29174070Sbillf#ifndef	__DEQUALIFY
29274070Sbillf#define	__DEQUALIFY(type, var)	((type)(uintptr_t)(const volatile void *)(var))
29374070Sbillf#endif
29474070Sbillf
29593510Smike/*-
29693510Smike * The following definitions are an extension of the behavior originally
29799904Smikeh * implemented in <sys/_posix.h>, but with a different level of granularity.
29893510Smike * POSIX.1 requires that the macros we test be defined before any standard
29993510Smike * header file is included.
30093510Smike *
30193510Smike * Here's a quick run-down of the versions:
30293510Smike *  defined(_POSIX_SOURCE)		1003.1-1988
30393510Smike *  _POSIX_C_SOURCE == 1		1003.1-1990
304100109Swollman *  _POSIX_C_SOURCE == 2		1003.2-1992 C Language Binding Option
30593510Smike *  _POSIX_C_SOURCE == 199309		1003.1b-1993
30693510Smike *  _POSIX_C_SOURCE == 199506		1003.1c-1995, 1003.1i-1995,
30793510Smike *					and the omnibus ISO/IEC 9945-1: 1996
30893510Smike *  _POSIX_C_SOURCE == 200112		1003.1-2001
30993510Smike *
31093510Smike * In addition, the X/Open Portability Guide, which is now the Single UNIX
31193510Smike * Specification, defines a feature-test macro which indicates the version of
31293510Smike * that specification, and which subsumes _POSIX_C_SOURCE.
31393510Smike *
31493510Smike * Our macros begin with two underscores to avoid namespace screwage.
31593510Smike */
31693510Smike
31793510Smike/* Deal with IEEE Std. 1003.1-1990, in which _POSIX_C_SOURCE == 1. */
318100109Swollman#if _POSIX_C_SOURCE == 1
31993510Smike#undef _POSIX_C_SOURCE		/* Probably illegal, but beyond caring now. */
32093510Smike#define	_POSIX_C_SOURCE		199009
32193510Smike#endif
32293510Smike
323100109Swollman/* Deal with IEEE Std. 1003.2-1992, in which _POSIX_C_SOURCE == 2. */
324100109Swollman#if _POSIX_C_SOURCE == 2
325100109Swollman#undef _POSIX_C_SOURCE
326100109Swollman#define	_POSIX_C_SOURCE		199209
327100109Swollman#endif
328100109Swollman
32993510Smike/* Deal with various X/Open Portability Guides and Single UNIX Spec. */
33093510Smike#ifdef _XOPEN_SOURCE
33193715Speter#if _XOPEN_SOURCE - 0 >= 600
33293510Smike#define	__XSI_VISIBLE		600
33393510Smike#undef _POSIX_C_SOURCE
33493510Smike#define	_POSIX_C_SOURCE		200112
33593715Speter#elif _XOPEN_SOURCE - 0 >= 500
33693510Smike#define	__XSI_VISIBLE		500
33793510Smike#undef _POSIX_C_SOURCE
33893510Smike#define	_POSIX_C_SOURCE		199506
33993510Smike#endif
34093510Smike#endif
34193510Smike
34293510Smike/*
34393510Smike * Deal with all versions of POSIX.  The ordering relative to the tests above is
34493510Smike * important.
34593510Smike */
34693510Smike#if defined(_POSIX_SOURCE) && !defined(_POSIX_C_SOURCE)
34793510Smike#define	_POSIX_C_SOURCE		198808
34893510Smike#endif
34993510Smike#ifdef _POSIX_C_SOURCE
35093510Smike#if _POSIX_C_SOURCE >= 200112
35193510Smike#define	__POSIX_VISIBLE		200112
35293510Smike#define	__ISO_C_VISIBLE		1999
35393510Smike#elif _POSIX_C_SOURCE >= 199506
35493510Smike#define	__POSIX_VISIBLE		199506
35593510Smike#define	__ISO_C_VISIBLE		1990
35693510Smike#elif _POSIX_C_SOURCE >= 199309
35793510Smike#define	__POSIX_VISIBLE		199309
35893510Smike#define	__ISO_C_VISIBLE		1990
359100109Swollman#elif _POSIX_C_SOURCE >= 199209
360100109Swollman#define	__POSIX_VISIBLE		199209
361100109Swollman#define	__ISO_C_VISIBLE		1990
36293510Smike#elif _POSIX_C_SOURCE >= 199009
36393510Smike#define	__POSIX_VISIBLE		199009
36493510Smike#define	__ISO_C_VISIBLE		1990
36593510Smike#else
36693510Smike#define	__POSIX_VISIBLE		198808
36793510Smike#define	__ISO_C_VISIBLE		0
36893510Smike#endif /* _POSIX_C_SOURCE */
36993510Smike#else
37093510Smike/*-
37193510Smike * Deal with _ANSI_SOURCE:
37293510Smike * If it is defined, and no other compilation environment is explicitly
37393510Smike * requested, then define our internal feature-test macros to zero.  This
37493510Smike * makes no difference to the preprocessor (undefined symbols in preprocessing
37593510Smike * expressions are defined to have value zero), but makes it more convenient for
37693510Smike * a test program to print out the values.
37793510Smike *
37893510Smike * If a program mistakenly defines _ANSI_SOURCE and some other macro such as
37993510Smike * _POSIX_C_SOURCE, we will assume that it wants the broader compilation
38093510Smike * environment (and in fact we will never get here).
38193510Smike */
382105654Smike#if defined(_ANSI_SOURCE)	/* Hide almost everything. */
38393510Smike#define	__POSIX_VISIBLE		0
38493510Smike#define	__XSI_VISIBLE		0
38593510Smike#define	__BSD_VISIBLE		0
38693510Smike#define	__ISO_C_VISIBLE		1990
387105654Smike#elif defined(_C99_SOURCE)	/* Localism to specify strict C99 env. */
388105654Smike#define	__POSIX_VISIBLE		0
389105654Smike#define	__XSI_VISIBLE		0
390105654Smike#define	__BSD_VISIBLE		0
391105654Smike#define	__ISO_C_VISIBLE		1999
39293510Smike#else				/* Default environment: show everything. */
39393510Smike#define	__POSIX_VISIBLE		200112
39493510Smike#define	__XSI_VISIBLE		600
39593510Smike#define	__BSD_VISIBLE		1
39693510Smike#define	__ISO_C_VISIBLE		1999
39793510Smike#endif
39893510Smike#endif
39993510Smike
4002212Sbde#endif /* !_SYS_CDEFS_H_ */
401