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