cdefs.h revision 1.107
1/*	$NetBSD: cdefs.h,v 1.107 2013/05/29 19:02:30 martin 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[/*CONSTCOND*/(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__(3, 0)
236#define	__always_inline	__attribute__((__always_inline__))
237#else
238#define	__always_inline	/* nothing */
239#endif
240
241#if __GNUC_PREREQ__(4, 1)
242#define	__returns_twice	__attribute__((__returns_twice__))
243#else
244#define	__returns_twice	/* nothing */
245#endif
246
247#if __GNUC_PREREQ__(4, 5)
248#define	__noclone	__attribute__((__noclone__))
249#else
250#define	__noclone	/* nothing */
251#endif
252
253#if __GNUC_PREREQ__(2, 7)
254#define	__unused	__attribute__((__unused__))
255#else
256#define	__unused	/* delete */
257#endif
258
259#if __GNUC_PREREQ__(3, 1)
260#define	__used		__attribute__((__used__))
261#else
262#define	__used		__unused
263#endif
264
265#if __GNUC_PREREQ__(3, 1)
266#define	__noprofile	__attribute__((__no_instrument_function__))
267#else
268#define	__noprofile	/* nothing */
269#endif
270
271#if defined(__cplusplus)
272#define	__BEGIN_EXTERN_C	extern "C" {
273#define	__END_EXTERN_C		}
274#define	__static_cast(x,y)	static_cast<x>(y)
275#else
276#define	__BEGIN_EXTERN_C
277#define	__END_EXTERN_C
278#define	__static_cast(x,y)	(x)y
279#endif
280
281#if __GNUC_PREREQ__(4, 0)
282#  define __dso_public	__attribute__((__visibility__("default")))
283#  define __dso_hidden	__attribute__((__visibility__("hidden")))
284#  define __BEGIN_PUBLIC_DECLS	\
285	_Pragma("GCC visibility push(default)") __BEGIN_EXTERN_C
286#  define __END_PUBLIC_DECLS	__END_EXTERN_C _Pragma("GCC visibility pop")
287#  define __BEGIN_HIDDEN_DECLS	\
288	_Pragma("GCC visibility push(hidden)") __BEGIN_EXTERN_C
289#  define __END_HIDDEN_DECLS	__END_EXTERN_C _Pragma("GCC visibility pop")
290#else
291#  define __dso_public
292#  define __dso_hidden
293#  define __BEGIN_PUBLIC_DECLS	__BEGIN_EXTERN_C
294#  define __END_PUBLIC_DECLS	__END_EXTERN_C
295#  define __BEGIN_HIDDEN_DECLS	__BEGIN_EXTERN_C
296#  define __END_HIDDEN_DECLS	__END_EXTERN_C
297#endif
298#if __GNUC_PREREQ__(4, 2)
299#  define __dso_protected	__attribute__((__visibility__("protected")))
300#else
301#  define __dso_protected
302#endif
303
304#define	__BEGIN_DECLS		__BEGIN_PUBLIC_DECLS
305#define	__END_DECLS		__END_PUBLIC_DECLS
306
307/*
308 * Non-static C99 inline functions are optional bodies.  They don't
309 * create global symbols if not used, but can be replaced if desirable.
310 * This differs from the behavior of GCC before version 4.3.  The nearest
311 * equivalent for older GCC is `extern inline'.  For newer GCC, use the
312 * gnu_inline attribute additionally to get the old behavior.
313 *
314 * For C99 compilers other than GCC, the C99 behavior is expected.
315 */
316#if defined(__GNUC__) && defined(__GNUC_STDC_INLINE__)
317#define	__c99inline	extern __attribute__((__gnu_inline__)) __inline
318#elif defined(__GNUC__)
319#define	__c99inline	extern __inline
320#elif defined(__STDC_VERSION__)
321#define	__c99inline	__inline
322#endif
323
324#if defined(__lint__)
325#define	__packed	__packed
326#define	__aligned(x)	/* delete */
327#define	__section(x)	/* delete */
328#elif __GNUC_PREREQ__(2, 7)
329#define	__packed	__attribute__((__packed__))
330#define	__aligned(x)	__attribute__((__aligned__(x)))
331#define	__section(x)	__attribute__((__section__(x)))
332#elif defined(__PCC__)
333#define	__packed	_Pragma("packed 1")
334#define	__aligned(x)   	_Pragma("aligned " __STRING(x))
335#define	__section(x)   	_Pragma("section " ## x)
336#elif defined(_MSC_VER)
337#define	__packed	/* ignore */
338#else
339#define	__packed	error: no __packed for this compiler
340#define	__aligned(x)	error: no __aligned for this compiler
341#define	__section(x)	error: no __section for this compiler
342#endif
343
344/*
345 * C99 defines the restrict type qualifier keyword, which was made available
346 * in GCC 2.92.
347 */
348#if defined(__lint__)
349#define	__restrict	/* delete __restrict when not supported */
350#elif __STDC_VERSION__ >= 199901L
351#define	__restrict	restrict
352#elif __GNUC_PREREQ__(2, 92)
353#define	__restrict	__restrict__
354#else
355#define	__restrict	/* delete __restrict when not supported */
356#endif
357
358/*
359 * C99 defines __func__ predefined identifier, which was made available
360 * in GCC 2.95.
361 */
362#if !(__STDC_VERSION__ >= 199901L)
363#if __GNUC_PREREQ__(2, 6)
364#define	__func__	__PRETTY_FUNCTION__
365#elif __GNUC_PREREQ__(2, 4)
366#define	__func__	__FUNCTION__
367#else
368#define	__func__	""
369#endif
370#endif /* !(__STDC_VERSION__ >= 199901L) */
371
372#if defined(_KERNEL)
373#if defined(NO_KERNEL_RCSIDS)
374#undef __KERNEL_RCSID
375#define	__KERNEL_RCSID(_n, _s)		/* nothing */
376#endif /* NO_KERNEL_RCSIDS */
377#endif /* _KERNEL */
378
379#if !defined(_STANDALONE) && !defined(_KERNEL)
380#if defined(__GNUC__) || defined(__PCC__)
381#define	__RENAME(x)	___RENAME(x)
382#elif defined(__lint__)
383#define	__RENAME(x)	__symbolrename(x)
384#else
385#error "No function renaming possible"
386#endif /* __GNUC__ */
387#else /* _STANDALONE || _KERNEL */
388#define	__RENAME(x)	no renaming in kernel or standalone environment
389#endif
390
391/*
392 * A barrier to stop the optimizer from moving code or assume live
393 * register values. This is gcc specific, the version is more or less
394 * arbitrary, might work with older compilers.
395 */
396#if __GNUC_PREREQ__(2, 95)
397#define	__insn_barrier()	__asm __volatile("":::"memory")
398#else
399#define	__insn_barrier()	/* */
400#endif
401
402/*
403 * GNU C version 2.96 adds explicit branch prediction so that
404 * the CPU back-end can hint the processor and also so that
405 * code blocks can be reordered such that the predicted path
406 * sees a more linear flow, thus improving cache behavior, etc.
407 *
408 * The following two macros provide us with a way to use this
409 * compiler feature.  Use __predict_true() if you expect the expression
410 * to evaluate to true, and __predict_false() if you expect the
411 * expression to evaluate to false.
412 *
413 * A few notes about usage:
414 *
415 *	* Generally, __predict_false() error condition checks (unless
416 *	  you have some _strong_ reason to do otherwise, in which case
417 *	  document it), and/or __predict_true() `no-error' condition
418 *	  checks, assuming you want to optimize for the no-error case.
419 *
420 *	* Other than that, if you don't know the likelihood of a test
421 *	  succeeding from empirical or other `hard' evidence, don't
422 *	  make predictions.
423 *
424 *	* These are meant to be used in places that are run `a lot'.
425 *	  It is wasteful to make predictions in code that is run
426 *	  seldomly (e.g. at subsystem initialization time) as the
427 *	  basic block reordering that this affects can often generate
428 *	  larger code.
429 */
430#if __GNUC_PREREQ__(2, 96)
431#define	__predict_true(exp)	__builtin_expect((exp) != 0, 1)
432#define	__predict_false(exp)	__builtin_expect((exp) != 0, 0)
433#else
434#define	__predict_true(exp)	(exp)
435#define	__predict_false(exp)	(exp)
436#endif
437
438/*
439 * Compiler-dependent macros to declare that functions take printf-like
440 * or scanf-like arguments.  They are null except for versions of gcc
441 * that are known to support the features properly (old versions of gcc-2
442 * didn't permit keeping the keywords out of the application namespace).
443 */
444#if __GNUC_PREREQ__(2, 7)
445#define __printflike(fmtarg, firstvararg)	\
446	    __attribute__((__format__ (__printf__, fmtarg, firstvararg)))
447#define __scanflike(fmtarg, firstvararg)	\
448	    __attribute__((__format__ (__scanf__, fmtarg, firstvararg)))
449#define __format_arg(fmtarg)    __attribute__((__format_arg__ (fmtarg)))
450#else
451#define __printflike(fmtarg, firstvararg)	/* nothing */
452#define __scanflike(fmtarg, firstvararg)	/* nothing */
453#define __format_arg(fmtarg)			/* nothing */
454#endif
455
456/*
457 * Macros for manipulating "link sets".  Link sets are arrays of pointers
458 * to objects, which are gathered up by the linker.
459 *
460 * Object format-specific code has provided us with the following macros:
461 *
462 *	__link_set_add_text(set, sym)
463 *		Add a reference to the .text symbol `sym' to `set'.
464 *
465 *	__link_set_add_rodata(set, sym)
466 *		Add a reference to the .rodata symbol `sym' to `set'.
467 *
468 *	__link_set_add_data(set, sym)
469 *		Add a reference to the .data symbol `sym' to `set'.
470 *
471 *	__link_set_add_bss(set, sym)
472 *		Add a reference to the .bss symbol `sym' to `set'.
473 *
474 *	__link_set_decl(set, ptype)
475 *		Provide an extern declaration of the set `set', which
476 *		contains an array of pointers to type `ptype'.  This
477 *		macro must be used by any code which wishes to reference
478 *		the elements of a link set.
479 *
480 *	__link_set_start(set)
481 *		This points to the first slot in the link set.
482 *
483 *	__link_set_end(set)
484 *		This points to the (non-existent) slot after the last
485 *		entry in the link set.
486 *
487 *	__link_set_count(set)
488 *		Count the number of entries in link set `set'.
489 *
490 * In addition, we provide the following macros for accessing link sets:
491 *
492 *	__link_set_foreach(pvar, set)
493 *		Iterate over the link set `set'.  Because a link set is
494 *		an array of pointers, pvar must be declared as "type **pvar",
495 *		and the actual entry accessed as "*pvar".
496 *
497 *	__link_set_entry(set, idx)
498 *		Access the link set entry at index `idx' from set `set'.
499 */
500#define	__link_set_foreach(pvar, set)					\
501	for (pvar = __link_set_start(set); pvar < __link_set_end(set); pvar++)
502
503#define	__link_set_entry(set, idx)	(__link_set_start(set)[idx])
504
505/*
506 * Return the natural alignment in bytes for the given type
507 */
508#if __GNUC_PREREQ__(4, 1)
509#define	__alignof(__t)  __alignof__(__t)
510#else
511#define __alignof(__t) (sizeof(struct { char __x; __t __y; }) - sizeof(__t))
512#endif
513
514/*
515 * Return the number of elements in a statically-allocated array,
516 * __x.
517 */
518#define	__arraycount(__x)	(sizeof(__x) / sizeof(__x[0]))
519
520#ifndef __ASSEMBLER__
521/* __BIT(n): nth bit, where __BIT(0) == 0x1. */
522#define	__BIT(__n)	\
523    (((uintmax_t)(__n) >= NBBY * sizeof(uintmax_t)) ? 0 : ((uintmax_t)1 << (uintmax_t)(__n)))
524
525/* __BITS(m, n): bits m through n, m < n. */
526#define	__BITS(__m, __n)	\
527	((__BIT(MAX((__m), (__n)) + 1) - 1) ^ (__BIT(MIN((__m), (__n))) - 1))
528#endif /* !__ASSEMBLER__ */
529
530/* find least significant bit that is set */
531#define	__LOWEST_SET_BIT(__mask) ((((__mask) - 1) & (__mask)) ^ (__mask))
532
533#define	__PRIuBIT	PRIuMAX
534#define	__PRIuBITS	__PRIuBIT
535
536#define	__PRIxBIT	PRIxMAX
537#define	__PRIxBITS	__PRIxBIT
538
539#define	__SHIFTOUT(__x, __mask)	(((__x) & (__mask)) / __LOWEST_SET_BIT(__mask))
540#define	__SHIFTIN(__x, __mask) ((__x) * __LOWEST_SET_BIT(__mask))
541#define	__SHIFTOUT_MASK(__mask) __SHIFTOUT((__mask), (__mask))
542
543/*
544 * Only to be used in other headers that are included from both c or c++
545 * NOT to be used in code.
546 */
547#ifdef __cplusplus
548#define __CAST(__dt, __st)	static_cast<__dt>(__st)
549#else
550#define __CAST(__dt, __st)	((__dt)(__st))
551#endif
552
553#define __type_mask(t) (/*LINTED*/sizeof(t) < sizeof(intmax_t) ? \
554    (~((1ULL << (sizeof(t) * NBBY)) - 1)) : 0ULL)
555
556#ifndef __ASSEMBLER__
557static __inline long long __zeroll(void) { return 0; }
558static __inline int __negative_p(double x) { return x < 0; }
559#else
560#define __zeroll() (0LL)
561#define __negative_p(x) ((x) < 0)
562#endif
563
564#define __type_min_s(t) ((t)((1ULL << (sizeof(t) * NBBY - 1))))
565#define __type_max_s(t) ((t)~((1ULL << (sizeof(t) * NBBY - 1))))
566#define __type_min_u(t) ((t)0ULL)
567#define __type_max_u(t) ((t)~0ULL)
568#define __type_is_signed(t) (/*LINTED*/__type_min_s(t) + (t)1 < (t)1)
569#define __type_min(t) (__type_is_signed(t) ? __type_min_s(t) : __type_min_u(t))
570#define __type_max(t) (__type_is_signed(t) ? __type_max_s(t) : __type_max_u(t))
571
572
573#define __type_fit_u(t, a) (/*LINTED*/sizeof(t) < sizeof(intmax_t) ? \
574    (((a) & __type_mask(t)) == 0) : !__negative_p(a))
575
576#define __type_fit_s(t, a) (/*LINTED*/__negative_p(a) ? \
577    ((intmax_t)((a) + __zeroll()) >= (intmax_t)__type_min_s(t)) : \
578    ((intmax_t)((a) + __zeroll()) <= (intmax_t)__type_max_s(t)))
579
580/*
581 * return true if value 'a' fits in type 't'
582 */
583#define __type_fit(t, a) (__type_is_signed(t) ? \
584    __type_fit_s(t, a) : __type_fit_u(t, a))
585
586#endif /* !_SYS_CDEFS_H_ */
587