cdefs.h revision 1.90
1/*	$NetBSD: cdefs.h,v 1.90 2012/02/17 18:58:51 joerg Exp $	*/
2
3/*
4 * Copyright (c) 1991, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Berkeley Software Design, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 *	@(#)cdefs.h	8.8 (Berkeley) 1/9/95
35 */
36
37#ifndef	_SYS_CDEFS_H_
38#define	_SYS_CDEFS_H_
39
40/*
41 * Macro to test if we're using a GNU C compiler of a specific vintage
42 * or later, for e.g. features that appeared in a particular version
43 * of GNU C.  Usage:
44 *
45 *	#if __GNUC_PREREQ__(major, minor)
46 *	...cool feature...
47 *	#else
48 *	...delete feature...
49 *	#endif
50 */
51#ifdef __GNUC__
52#define	__GNUC_PREREQ__(x, y)						\
53	((__GNUC__ == (x) && __GNUC_MINOR__ >= (y)) ||			\
54	 (__GNUC__ > (x)))
55#else
56#define	__GNUC_PREREQ__(x, y)	0
57#endif
58
59#include <machine/cdefs.h>
60#ifdef __ELF__
61#include <sys/cdefs_elf.h>
62#else
63#include <sys/cdefs_aout.h>
64#endif
65
66#ifdef __GNUC__
67#define	__strict_weak_alias(alias,sym)					\
68	__unused static __typeof__(alias) *__weak_alias_##alias = &sym;	\
69	__weak_alias(alias,sym)
70#else
71#define	__strict_weak_alias(alias,sym) __weak_alias(alias,sym)
72#endif
73
74/*
75 * Optional marker for size-optimised MD calling convention.
76 */
77#ifndef __compactcall
78#define	__compactcall
79#endif
80
81/*
82 * The __CONCAT macro is used to concatenate parts of symbol names, e.g.
83 * with "#define OLD(foo) __CONCAT(old,foo)", OLD(foo) produces oldfoo.
84 * The __CONCAT macro is a bit tricky -- make sure you don't put spaces
85 * in between its arguments.  __CONCAT can also concatenate double-quoted
86 * strings produced by the __STRING macro, but this only works with ANSI C.
87 */
88
89#define	___STRING(x)	__STRING(x)
90#define	___CONCAT(x,y)	__CONCAT(x,y)
91
92#if __STDC__ || defined(__cplusplus)
93#define	__P(protos)	protos		/* full-blown ANSI C */
94#define	__CONCAT(x,y)	x ## y
95#define	__STRING(x)	#x
96
97#define	__const		const		/* define reserved names to standard */
98#define	__signed	signed
99#define	__volatile	volatile
100#if defined(__cplusplus) || defined(__PCC__)
101#define	__inline	inline		/* convert to C++/C99 keyword */
102#else
103#if !defined(__GNUC__) && !defined(__lint__)
104#define	__inline			/* delete GCC keyword */
105#endif /* !__GNUC__  && !__lint__ */
106#endif /* !__cplusplus */
107
108#else	/* !(__STDC__ || __cplusplus) */
109#define	__P(protos)	()		/* traditional C preprocessor */
110#define	__CONCAT(x,y)	x/**/y
111#define	__STRING(x)	"x"
112
113#ifndef __GNUC__
114#define	__const				/* delete pseudo-ANSI C keywords */
115#define	__inline
116#define	__signed
117#define	__volatile
118#endif	/* !__GNUC__ */
119
120/*
121 * In non-ANSI C environments, new programs will want ANSI-only C keywords
122 * deleted from the program and old programs will want them left alone.
123 * Programs using the ANSI C keywords const, inline etc. as normal
124 * identifiers should define -DNO_ANSI_KEYWORDS.
125 */
126#ifndef	NO_ANSI_KEYWORDS
127#define	const		__const		/* convert ANSI C keywords */
128#define	inline		__inline
129#define	signed		__signed
130#define	volatile	__volatile
131#endif /* !NO_ANSI_KEYWORDS */
132#endif	/* !(__STDC__ || __cplusplus) */
133
134/*
135 * Used for internal auditing of the NetBSD source tree.
136 */
137#ifdef __AUDIT__
138#define	__aconst	__const
139#else
140#define	__aconst
141#endif
142
143/*
144 * Compile Time Assertion.
145 */
146#ifdef __COUNTER__
147#define	__CTASSERT(x)		__CTASSERT0(x, __ctassert, __COUNTER__)
148#else
149#define	__CTASSERT(x)		__CTASSERT0(x, __ctassert, __LINE__)
150#endif
151#define	__CTASSERT0(x, y, z)	__CTASSERT1(x, y, z)
152#define	__CTASSERT1(x, y, z)	typedef char y ## z[(x) ? 1 : -1];
153
154/*
155 * The following macro is used to remove const cast-away warnings
156 * from gcc -Wcast-qual; it should be used with caution because it
157 * can hide valid errors; in particular most valid uses are in
158 * situations where the API requires it, not to cast away string
159 * constants. We don't use *intptr_t on purpose here and we are
160 * explicit about unsigned long so that we don't have additional
161 * dependencies.
162 */
163#define __UNCONST(a)	((void *)(unsigned long)(const void *)(a))
164
165/*
166 * The following macro is used to remove the volatile cast-away warnings
167 * from gcc -Wcast-qual; as above it should be used with caution
168 * because it can hide valid errors or warnings.  Valid uses include
169 * making it possible to pass a volatile pointer to memset().
170 * For the same reasons as above, we use unsigned long and not intptr_t.
171 */
172#define __UNVOLATILE(a)	((void *)(unsigned long)(volatile void *)(a))
173
174/*
175 * GCC2 provides __extension__ to suppress warnings for various GNU C
176 * language extensions under "-ansi -pedantic".
177 */
178#if !__GNUC_PREREQ__(2, 0)
179#define	__extension__		/* delete __extension__ if non-gcc or gcc1 */
180#endif
181
182/*
183 * GCC1 and some versions of GCC2 declare dead (non-returning) and
184 * pure (no side effects) functions using "volatile" and "const";
185 * unfortunately, these then cause warnings under "-ansi -pedantic".
186 * GCC2 uses a new, peculiar __attribute__((attrs)) style.  All of
187 * these work for GNU C++ (modulo a slight glitch in the C++ grammar
188 * in the distribution version of 2.5.5).
189 *
190 * GCC defines a pure function as depending only on its arguments and
191 * global variables.  Typical examples are strlen and sqrt.
192 *
193 * GCC defines a const function as depending only on its arguments.
194 * Therefore calling a const function again with identical arguments
195 * will always produce the same result.
196 *
197 * Rounding modes for floating point operations are considered global
198 * variables and prevent sqrt from being a const function.
199 *
200 * Calls to const functions can be optimised away and moved around
201 * without limitations.
202 */
203#if !__GNUC_PREREQ__(2, 0)
204#define __attribute__(x)
205#endif
206
207#if __GNUC_PREREQ__(2, 5)
208#define	__dead		__attribute__((__noreturn__))
209#elif defined(__GNUC__)
210#define	__dead		__volatile
211#else
212#define	__dead
213#endif
214
215#if __GNUC_PREREQ__(2, 96)
216#define	__pure		__attribute__((__pure__))
217#elif defined(__GNUC__)
218#define	__pure		__const
219#else
220#define	__pure
221#endif
222
223#if __GNUC_PREREQ__(2, 5)
224#define	__constfunc	__attribute__((__const__))
225#else
226#define	__constfunc
227#endif
228
229#if __GNUC_PREREQ__(3, 0)
230#define	__noinline	__attribute__((__noinline__))
231#else
232#define	__noinline	/* nothing */
233#endif
234
235#if __GNUC_PREREQ__(4, 1)
236#define	__returns_twice	__attribute__((__returns_twice__))
237#else
238#define	__returns_twice	/* nothing */
239#endif
240
241#if __GNUC_PREREQ__(4, 5)
242#define	__noclone	__attribute__((__noclone__))
243#else
244#define	__noclone	/* nothing */
245#endif
246
247#if __GNUC_PREREQ__(2, 7)
248#define	__unused	__attribute__((__unused__))
249#else
250#define	__unused	/* delete */
251#endif
252
253#if __GNUC_PREREQ__(3, 1)
254#define	__used		__attribute__((__used__))
255#else
256#define	__used		__unused
257#endif
258
259#if __GNUC_PREREQ__(3, 1)
260#define	__noprofile	__attribute__((__no_instrument_function__))
261#else
262#define	__noprofile	/* nothing */
263#endif
264
265#if defined(__cplusplus)
266#define	__BEGIN_EXTERN_C	extern "C" {
267#define	__END_EXTERN_C		}
268#define	__static_cast(x,y)	static_cast<x>(y)
269#else
270#define	__BEGIN_EXTERN_C
271#define	__END_EXTERN_C
272#define	__static_cast(x,y)	(x)y
273#endif
274
275#if __GNUC_PREREQ__(4, 0)
276#  define __dso_public	__attribute__((__visibility__("default")))
277#  define __dso_hidden	__attribute__((__visibility__("hidden")))
278#  define __BEGIN_PUBLIC_DECLS	\
279	_Pragma("GCC visibility push(default)") __BEGIN_EXTERN_C
280#  define __END_PUBLIC_DECLS	__END_EXTERN_C _Pragma("GCC visibility pop")
281#  define __BEGIN_HIDDEN_DECLS	\
282	_Pragma("GCC visibility push(hidden)") __BEGIN_EXTERN_C
283#  define __END_HIDDEN_DECLS	__END_EXTERN_C _Pragma("GCC visibility pop")
284#else
285#  define __dso_public
286#  define __dso_hidden
287#  define __BEGIN_PUBLIC_DECLS	__BEGIN_EXTERN_C
288#  define __END_PUBLIC_DECLS	__END_EXTERN_C
289#  define __BEGIN_HIDDEN_DECLS	__BEGIN_EXTERN_C
290#  define __END_HIDDEN_DECLS	__END_EXTERN_C
291#endif
292
293#define	__BEGIN_DECLS		__BEGIN_PUBLIC_DECLS
294#define	__END_DECLS		__END_PUBLIC_DECLS
295
296/*
297 * Non-static C99 inline functions are optional bodies.  They don't
298 * create global symbols if not used, but can be replaced if desirable.
299 * This differs from the behavior of GCC before version 4.3.  The nearest
300 * equivalent for older GCC is `extern inline'.  For newer GCC, use the
301 * gnu_inline attribute additionally to get the old behavior.
302 *
303 * For C99 compilers other than GCC, the C99 behavior is expected.
304 */
305#if defined(__GNUC__) && defined(__GNUC_STDC_INLINE__)
306#define	__c99inline	extern __attribute__((__gnu_inline__)) __inline
307#elif defined(__GNUC__)
308#define	__c99inline	extern __inline
309#elif defined(__STDC_VERSION__)
310#define	__c99inline	__inline
311#endif
312
313#if defined(__lint__)
314#define	__packed	__packed
315#define	__aligned(x)	/* delete */
316#define	__section(x)	/* delete */
317#elif __GNUC_PREREQ__(2, 7)
318#define	__packed	__attribute__((__packed__))
319#define	__aligned(x)	__attribute__((__aligned__(x)))
320#define	__section(x)	__attribute__((__section__(x)))
321#elif defined(__PCC__)
322#define	__packed	_Pragma("packed 1")
323#define	__aligned(x)   	_Pragma("aligned " __STRING(x))
324#define	__section(x)   	_Pragma("section " ## x)
325#elif defined(_MSC_VER)
326#define	__packed	/* ignore */
327#else
328#define	__packed	error: no __packed for this compiler
329#define	__aligned(x)	error: no __aligned for this compiler
330#define	__section(x)	error: no __section for this compiler
331#endif
332
333/*
334 * C99 defines the restrict type qualifier keyword, which was made available
335 * in GCC 2.92.
336 */
337#if defined(__lint__)
338#define	__restrict	/* delete __restrict when not supported */
339#elif __STDC_VERSION__ >= 199901L
340#define	__restrict	restrict
341#elif __GNUC_PREREQ__(2, 92)
342#define	__restrict	__restrict__
343#else
344#define	__restrict	/* delete __restrict when not supported */
345#endif
346
347/*
348 * C99 defines __func__ predefined identifier, which was made available
349 * in GCC 2.95.
350 */
351#if !(__STDC_VERSION__ >= 199901L)
352#if __GNUC_PREREQ__(2, 6)
353#define	__func__	__PRETTY_FUNCTION__
354#elif __GNUC_PREREQ__(2, 4)
355#define	__func__	__FUNCTION__
356#else
357#define	__func__	""
358#endif
359#endif /* !(__STDC_VERSION__ >= 199901L) */
360
361#if defined(_KERNEL)
362#if defined(NO_KERNEL_RCSIDS)
363#undef __KERNEL_RCSID
364#define	__KERNEL_RCSID(_n, _s)		/* nothing */
365#endif /* NO_KERNEL_RCSIDS */
366#endif /* _KERNEL */
367
368#if !defined(_STANDALONE) && !defined(_KERNEL)
369#if defined(__GNUC__) || defined(__PCC__)
370#define	__RENAME(x)	___RENAME(x)
371#else
372#ifdef __lint__
373#define	__RENAME(x)	__symbolrename(x)
374#else
375#error "No function renaming possible"
376#endif /* __lint__ */
377#endif /* __GNUC__ */
378#else /* _STANDALONE || _KERNEL */
379#define	__RENAME(x)	no renaming in kernel or standalone environment
380#endif
381
382/*
383 * A barrier to stop the optimizer from moving code or assume live
384 * register values. This is gcc specific, the version is more or less
385 * arbitrary, might work with older compilers.
386 */
387#if __GNUC_PREREQ__(2, 95)
388#define	__insn_barrier()	__asm __volatile("":::"memory")
389#else
390#define	__insn_barrier()	/* */
391#endif
392
393/*
394 * GNU C version 2.96 adds explicit branch prediction so that
395 * the CPU back-end can hint the processor and also so that
396 * code blocks can be reordered such that the predicted path
397 * sees a more linear flow, thus improving cache behavior, etc.
398 *
399 * The following two macros provide us with a way to use this
400 * compiler feature.  Use __predict_true() if you expect the expression
401 * to evaluate to true, and __predict_false() if you expect the
402 * expression to evaluate to false.
403 *
404 * A few notes about usage:
405 *
406 *	* Generally, __predict_false() error condition checks (unless
407 *	  you have some _strong_ reason to do otherwise, in which case
408 *	  document it), and/or __predict_true() `no-error' condition
409 *	  checks, assuming you want to optimize for the no-error case.
410 *
411 *	* Other than that, if you don't know the likelihood of a test
412 *	  succeeding from empirical or other `hard' evidence, don't
413 *	  make predictions.
414 *
415 *	* These are meant to be used in places that are run `a lot'.
416 *	  It is wasteful to make predictions in code that is run
417 *	  seldomly (e.g. at subsystem initialization time) as the
418 *	  basic block reordering that this affects can often generate
419 *	  larger code.
420 */
421#if __GNUC_PREREQ__(2, 96)
422#define	__predict_true(exp)	__builtin_expect((exp) != 0, 1)
423#define	__predict_false(exp)	__builtin_expect((exp) != 0, 0)
424#else
425#define	__predict_true(exp)	(exp)
426#define	__predict_false(exp)	(exp)
427#endif
428
429/*
430 * Compiler-dependent macros to declare that functions take printf-like
431 * or scanf-like arguments.  They are null except for versions of gcc
432 * that are known to support the features properly (old versions of gcc-2
433 * didn't permit keeping the keywords out of the application namespace).
434 */
435#if __GNUC_PREREQ__(2, 7)
436#define __printflike(fmtarg, firstvararg)	\
437	    __attribute__((__format__ (__printf__, fmtarg, firstvararg)))
438#define __scanflike(fmtarg, firstvararg)	\
439	    __attribute__((__format__ (__scanf__, fmtarg, firstvararg)))
440#define __format_arg(fmtarg)    __attribute__((__format_arg__ (fmtarg)))
441#else
442#define __printflike(fmtarg, firstvararg)	/* nothing */
443#define __scanflike(fmtarg, firstvararg)	/* nothing */
444#define __format_arg(fmtarg)			/* nothing */
445#endif
446
447/*
448 * Macros for manipulating "link sets".  Link sets are arrays of pointers
449 * to objects, which are gathered up by the linker.
450 *
451 * Object format-specific code has provided us with the following macros:
452 *
453 *	__link_set_add_text(set, sym)
454 *		Add a reference to the .text symbol `sym' to `set'.
455 *
456 *	__link_set_add_rodata(set, sym)
457 *		Add a reference to the .rodata symbol `sym' to `set'.
458 *
459 *	__link_set_add_data(set, sym)
460 *		Add a reference to the .data symbol `sym' to `set'.
461 *
462 *	__link_set_add_bss(set, sym)
463 *		Add a reference to the .bss symbol `sym' to `set'.
464 *
465 *	__link_set_decl(set, ptype)
466 *		Provide an extern declaration of the set `set', which
467 *		contains an array of the pointer type `ptype'.  This
468 *		macro must be used by any code which wishes to reference
469 *		the elements of a link set.
470 *
471 *	__link_set_start(set)
472 *		This points to the first slot in the link set.
473 *
474 *	__link_set_end(set)
475 *		This points to the (non-existent) slot after the last
476 *		entry in the link set.
477 *
478 *	__link_set_count(set)
479 *		Count the number of entries in link set `set'.
480 *
481 * In addition, we provide the following macros for accessing link sets:
482 *
483 *	__link_set_foreach(pvar, set)
484 *		Iterate over the link set `set'.  Because a link set is
485 *		an array of pointers, pvar must be declared as "type **pvar",
486 *		and the actual entry accessed as "*pvar".
487 *
488 *	__link_set_entry(set, idx)
489 *		Access the link set entry at index `idx' from set `set'.
490 */
491#define	__link_set_foreach(pvar, set)					\
492	for (pvar = __link_set_start(set); pvar < __link_set_end(set); pvar++)
493
494#define	__link_set_entry(set, idx)	(__link_set_begin(set)[idx])
495
496/*
497 * Return the number of elements in a statically-allocated array,
498 * __x.
499 */
500#define	__arraycount(__x)	(sizeof(__x) / sizeof(__x[0]))
501
502/* __BIT(n): nth bit, where __BIT(0) == 0x1. */
503#define	__BIT(__n)	\
504    (((uintmax_t)(__n) >= NBBY * sizeof(uintmax_t)) ? 0 : ((uintmax_t)1 << (uintmax_t)(__n)))
505
506/* __BITS(m, n): bits m through n, m < n. */
507#define	__BITS(__m, __n)	\
508	((__BIT(MAX((__m), (__n)) + 1) - 1) ^ (__BIT(MIN((__m), (__n))) - 1))
509
510/* find least significant bit that is set */
511#define	__LOWEST_SET_BIT(__mask) ((((__mask) - 1) & (__mask)) ^ (__mask))
512
513#define	__PRIuBIT	PRIuMAX
514#define	__PRIuBITS	__PRIuBIT
515
516#define	__PRIxBIT	PRIxMAX
517#define	__PRIxBITS	__PRIxBIT
518
519#define	__SHIFTOUT(__x, __mask)	(((__x) & (__mask)) / __LOWEST_SET_BIT(__mask))
520#define	__SHIFTIN(__x, __mask) ((__x) * __LOWEST_SET_BIT(__mask))
521#define	__SHIFTOUT_MASK(__mask) __SHIFTOUT((__mask), (__mask))
522
523/*
524 * Only to be used in other headers that are included from both c or c++
525 * NOT to be used in code.
526 */
527#ifdef __cplusplus
528#define __CAST(__dt, __st)	static_cast<__dt>(__st)
529#else
530#define __CAST(__dt, __st)	((__dt)(__st))
531#endif
532
533#endif /* !_SYS_CDEFS_H_ */
534