xref: /netbsd/sys/sys/cdefs.h (revision 63daa7c7)
1 /*	$NetBSD: cdefs.h,v 1.160 2023/04/30 08:45:48 riastradh Exp $	*/
2 
3 /* * Copyright (c) 1991, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * Berkeley Software Design, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)cdefs.h	8.8 (Berkeley) 1/9/95
34  */
35 
36 #ifndef	_SYS_CDEFS_H_
37 #define	_SYS_CDEFS_H_
38 
39 /*
40  * Macro to test if we're using a GNU C compiler of a specific vintage
41  * or later, for e.g. features that appeared in a particular version
42  * of GNU C.  Usage:
43  *
44  *	#if __GNUC_PREREQ__(major, minor)
45  *	...cool feature...
46  *	#else
47  *	...delete feature...
48  *	#endif
49  */
50 #ifdef __GNUC__
51 #define	__GNUC_PREREQ__(x, y)						\
52 	((__GNUC__ == (x) && __GNUC_MINOR__ >= (y)) ||			\
53 	 (__GNUC__ > (x)))
54 #else
55 #define	__GNUC_PREREQ__(x, y)	0
56 #endif
57 
58 /*
59  * Macros to test Clang/LLVM features.
60  * Usage:
61  *
62  *	#if __has_feature(safe_stack)
63  *	...SafeStack specific code...
64  *	#else
65  *	..regular code...
66  *	#endif
67  */
68 #ifndef __has_feature
69 #define __has_feature(x)	0
70 #endif
71 
72 #ifndef __has_extension
73 #define __has_extension		__has_feature /* Compat with pre-3.0 Clang */
74 #endif
75 
76 #include <machine/cdefs.h>
77 #ifdef __ELF__
78 #include <sys/cdefs_elf.h>
79 #else
80 #include <sys/cdefs_aout.h>
81 #endif
82 
83 #ifdef __GNUC__
84 #define	__strict_weak_alias(alias,sym)					\
85 	__unused static __typeof__(alias) *__weak_alias_##alias = &sym;	\
86 	__weak_alias(alias,sym)
87 #else
88 #define	__strict_weak_alias(alias,sym) __weak_alias(alias,sym)
89 #endif
90 
91 /*
92  * Optional marker for size-optimised MD calling convention.
93  */
94 #ifndef __compactcall
95 #define	__compactcall
96 #endif
97 
98 /*
99  * The __CONCAT macro is used to concatenate parts of symbol names, e.g.
100  * with "#define OLD(foo) __CONCAT(old,foo)", OLD(foo) produces oldfoo.
101  * The __CONCAT macro is a bit tricky -- make sure you don't put spaces
102  * in between its arguments.  __CONCAT can also concatenate double-quoted
103  * strings produced by the __STRING macro, but this only works with ANSI C.
104  */
105 
106 #define	___STRING(x)	__STRING(x)
107 #define	___CONCAT(x,y)	__CONCAT(x,y)
108 
109 #if __STDC__ || defined(__cplusplus)
110 #define	__P(protos)	protos		/* full-blown ANSI C */
111 #define	__CONCAT(x,y)	x ## y
112 #define	__STRING(x)	#x
113 
114 #define	__const		const		/* define reserved names to standard */
115 #define	__signed	signed
116 #define	__volatile	volatile
117 
118 #define	__CONCAT3(a,b,c)		a ## b ## c
119 #define	__CONCAT4(a,b,c,d)		a ## b ## c ## d
120 #define	__CONCAT5(a,b,c,d,e)		a ## b ## c ## d ## e
121 #define	__CONCAT6(a,b,c,d,e,f)		a ## b ## c ## d ## e ## f
122 #define	__CONCAT7(a,b,c,d,e,f,g)	a ## b ## c ## d ## e ## f ## g
123 #define	__CONCAT8(a,b,c,d,e,f,g,h)	a ## b ## c ## d ## e ## f ## g ## h
124 
125 #if defined(__cplusplus) || defined(__PCC__)
126 #define	__inline	inline		/* convert to C++/C99 keyword */
127 #else
128 #if !defined(__GNUC__) && !defined(__lint__)
129 #define	__inline			/* delete GCC keyword */
130 #endif /* !__GNUC__  && !__lint__ */
131 #endif /* !__cplusplus */
132 
133 #else	/* !(__STDC__ || __cplusplus) */
134 #define	__P(protos)	()		/* traditional C preprocessor */
135 #define	__CONCAT(x,y)	x/**/y
136 #define	__STRING(x)	"x"
137 
138 #ifndef __GNUC__
139 #define	__const				/* delete pseudo-ANSI C keywords */
140 #define	__inline
141 #define	__signed
142 #define	__volatile
143 #endif	/* !__GNUC__ */
144 
145 /*
146  * In non-ANSI C environments, new programs will want ANSI-only C keywords
147  * deleted from the program and old programs will want them left alone.
148  * Programs using the ANSI C keywords const, inline etc. as normal
149  * identifiers should define -DNO_ANSI_KEYWORDS.
150  */
151 #ifndef	NO_ANSI_KEYWORDS
152 #define	const		__const		/* convert ANSI C keywords */
153 #define	inline		__inline
154 #define	signed		__signed
155 #define	volatile	__volatile
156 #endif /* !NO_ANSI_KEYWORDS */
157 #endif	/* !(__STDC__ || __cplusplus) */
158 
159 /*
160  * Used for internal auditing of the NetBSD source tree.
161  */
162 #ifdef __AUDIT__
163 #define	__aconst	__const
164 #else
165 #define	__aconst
166 #endif
167 
168 /*
169  * Compile Time Assertion.
170  */
171 #ifdef __COUNTER__
172 #define	__CTASSERT(x)		__CTASSERT0(x, __ctassert, __COUNTER__)
173 #else
174 #define	__CTASSERT(x)		__CTASSERT99(x, __INCLUDE_LEVEL__, __LINE__)
175 #define	__CTASSERT99(x, a, b)	__CTASSERT0(x, __CONCAT(__ctassert,a), \
176 					       __CONCAT(_,b))
177 #endif
178 #define	__CTASSERT0(x, y, z)	__CTASSERT1(x, y, z)
179 #define	__CTASSERT1(x, y, z)	\
180 	struct y ## z ## _struct { \
181 		unsigned int y ## z : /*CONSTCOND*/(x) ? 1 : -1; \
182 	}
183 
184 /*
185  * The following macro is used to remove const cast-away warnings
186  * from gcc -Wcast-qual; it should be used with caution because it
187  * can hide valid errors; in particular most valid uses are in
188  * situations where the API requires it, not to cast away string
189  * constants. We don't use *intptr_t on purpose here and we are
190  * explicit about unsigned long so that we don't have additional
191  * dependencies.
192  */
193 #define __UNCONST(a)	((void *)(unsigned long)(const void *)(a))
194 
195 /*
196  * The following macro is used to remove the volatile cast-away warnings
197  * from gcc -Wcast-qual; as above it should be used with caution
198  * because it can hide valid errors or warnings.  Valid uses include
199  * making it possible to pass a volatile pointer to memset().
200  * For the same reasons as above, we use unsigned long and not intptr_t.
201  */
202 #define __UNVOLATILE(a)	((void *)(unsigned long)(volatile void *)(a))
203 
204 /*
205  * The following macro is used to remove the the function type cast warnings
206  * from gcc -Wcast-function-type and as above should be used with caution.
207  */
208 #define __FPTRCAST(t, f)	((t)(void *)(f))
209 
210 /*
211  * GCC2 provides __extension__ to suppress warnings for various GNU C
212  * language extensions under "-ansi -pedantic".
213  */
214 #if !__GNUC_PREREQ__(2, 0)
215 #define	__extension__		/* delete __extension__ if non-gcc or gcc1 */
216 #endif
217 
218 /*
219  * GCC1 and some versions of GCC2 declare dead (non-returning) and
220  * pure (no side effects) functions using "volatile" and "const";
221  * unfortunately, these then cause warnings under "-ansi -pedantic".
222  * GCC2 uses a new, peculiar __attribute__((attrs)) style.  All of
223  * these work for GNU C++ (modulo a slight glitch in the C++ grammar
224  * in the distribution version of 2.5.5).
225  *
226  * GCC defines a pure function as depending only on its arguments and
227  * global variables.  Typical examples are strlen and sqrt.
228  *
229  * GCC defines a const function as depending only on its arguments.
230  * Therefore calling a const function again with identical arguments
231  * will always produce the same result.
232  *
233  * Rounding modes for floating point operations are considered global
234  * variables and prevent sqrt from being a const function.
235  *
236  * Calls to const functions can be optimised away and moved around
237  * without limitations.
238  */
239 #if !__GNUC_PREREQ__(2, 0) && !defined(__lint__)
240 #define __attribute__(x)
241 #endif
242 
243 #if __GNUC_PREREQ__(2, 5) || defined(__lint__)
244 #define	__dead		__attribute__((__noreturn__))
245 #elif defined(__GNUC__)
246 #define	__dead		__volatile
247 #else
248 #define	__dead
249 #endif
250 
251 #if __GNUC_PREREQ__(2, 96) || defined(__lint__)
252 #define	__pure		__attribute__((__pure__))
253 #elif defined(__GNUC__)
254 #define	__pure		__const
255 #else
256 #define	__pure
257 #endif
258 
259 #if __GNUC_PREREQ__(2, 5) || defined(__lint__)
260 #define	__constfunc	__attribute__((__const__))
261 #else
262 #define	__constfunc
263 #endif
264 
265 #if __GNUC_PREREQ__(3, 0) || defined(__lint__)
266 #define	__noinline	__attribute__((__noinline__))
267 #else
268 #define	__noinline	/* nothing */
269 #endif
270 
271 #if __GNUC_PREREQ__(3, 0) || defined(__lint__)
272 #define	__always_inline	__attribute__((__always_inline__))
273 #else
274 #define	__always_inline	/* nothing */
275 #endif
276 
277 #if __GNUC_PREREQ__(4, 0) || defined(__lint__)
278 #define	__null_sentinel	__attribute__((__sentinel__))
279 #else
280 #define	__null_sentinel	/* nothing */
281 #endif
282 
283 #if __GNUC_PREREQ__(4, 1) || defined(__lint__)
284 #define	__returns_twice	__attribute__((__returns_twice__))
285 #else
286 #define	__returns_twice	/* nothing */
287 #endif
288 
289 #if __GNUC_PREREQ__(4, 5) || defined(__lint__)
290 #define	__noclone	__attribute__((__noclone__))
291 #else
292 #define	__noclone	/* nothing */
293 #endif
294 
295 /*
296  * __unused: Note that item or function might be unused.
297  */
298 #if __GNUC_PREREQ__(2, 7) || defined(__lint__)
299 #define	__unused	__attribute__((__unused__))
300 #else
301 #define	__unused	/* delete */
302 #endif
303 
304 /*
305  * __used: Note that item is needed, even if it appears to be unused.
306  */
307 #if __GNUC_PREREQ__(3, 1) || defined(__lint__)
308 #define	__used		__attribute__((__used__))
309 #else
310 #define	__used		__unused
311 #endif
312 
313 /*
314  * __diagused: Note that item is used in diagnostic code, but may be
315  * unused in non-diagnostic code.
316  */
317 #if (defined(_KERNEL) && defined(DIAGNOSTIC)) \
318  || (!defined(_KERNEL) && !defined(NDEBUG))
319 #define	__diagused	/* empty */
320 #else
321 #define	__diagused	__unused
322 #endif
323 
324 /*
325  * __debugused: Note that item is used in debug code, but may be
326  * unused in non-debug code.
327  */
328 #if defined(DEBUG)
329 #define	__debugused	/* empty */
330 #else
331 #define	__debugused	__unused
332 #endif
333 
334 #if __GNUC_PREREQ__(3, 1) || defined(__lint__)
335 #define	__noprofile	__attribute__((__no_instrument_function__))
336 #else
337 #define	__noprofile	/* nothing */
338 #endif
339 
340 #if __GNUC_PREREQ__(4, 6) || defined(__clang__) || defined(__lint__)
341 #define	__unreachable()	__builtin_unreachable()
342 #else
343 #define	__unreachable()	do {} while (/*CONSTCOND*/0)
344 #endif
345 
346 #if defined(_KERNEL) || defined(_RUMPKERNEL)
347 #if defined(__clang__) && __has_feature(address_sanitizer)
348 #define	__noasan	__attribute__((no_sanitize("kernel-address", "address")))
349 #elif __GNUC_PREREQ__(4, 9) && defined(__SANITIZE_ADDRESS__)
350 #define	__noasan	__attribute__((no_sanitize_address))
351 #else
352 #define	__noasan	/* nothing */
353 #endif
354 
355 #if defined(__clang__) && __has_feature(thread_sanitizer)
356 #define	__nocsan	__attribute__((no_sanitize("thread")))
357 #elif __GNUC_PREREQ__(4, 9) && defined(__SANITIZE_THREAD__)
358 #define	__nocsan	__attribute__((no_sanitize_thread))
359 #else
360 #define	__nocsan	/* nothing */
361 #endif
362 
363 #if defined(__clang__) && __has_feature(memory_sanitizer)
364 #define	__nomsan	__attribute__((no_sanitize("kernel-memory", "memory")))
365 #else
366 #define	__nomsan	/* nothing */
367 #endif
368 
369 #if defined(__clang__) && __has_feature(undefined_behavior_sanitizer)
370 #define __noubsan	__attribute__((no_sanitize("undefined")))
371 #elif __GNUC_PREREQ__(4, 9) && defined(__SANITIZE_UNDEFINED__)
372 #define __noubsan	__attribute__((no_sanitize_undefined))
373 #else
374 #define __noubsan	/* nothing */
375 #endif
376 #endif
377 
378 #if defined(__COVERITY__) ||						\
379     __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__) ||\
380     __has_feature(leak_sanitizer) || defined(__SANITIZE_LEAK__)
381 #define	__NO_LEAKS
382 #endif
383 
384 /*
385  * To be used when an empty body is required like:
386  *
387  * #ifdef DEBUG
388  * # define dprintf(a) printf(a)
389  * #else
390  * # define dprintf(a) __nothing
391  * #endif
392  *
393  * We use ((void)0) instead of do {} while (0) so that it
394  * works on , expressions.
395  */
396 #define __nothing	(/*LINTED*/(void)0)
397 
398 #if defined(__cplusplus)
399 #define	__BEGIN_EXTERN_C	extern "C" {
400 #define	__END_EXTERN_C		}
401 #define	__static_cast(x,y)	static_cast<x>(y)
402 #else
403 #define	__BEGIN_EXTERN_C
404 #define	__END_EXTERN_C
405 #define	__static_cast(x,y)	(x)y
406 #endif
407 
408 #if __GNUC_PREREQ__(4, 0) || defined(__lint__)
409 #  define __dso_public	__attribute__((__visibility__("default")))
410 #  define __dso_hidden	__attribute__((__visibility__("hidden")))
411 #  define __BEGIN_PUBLIC_DECLS	\
412 	_Pragma("GCC visibility push(default)") __BEGIN_EXTERN_C
413 #  define __END_PUBLIC_DECLS	__END_EXTERN_C _Pragma("GCC visibility pop")
414 #  define __BEGIN_HIDDEN_DECLS	\
415 	_Pragma("GCC visibility push(hidden)") __BEGIN_EXTERN_C
416 #  define __END_HIDDEN_DECLS	__END_EXTERN_C _Pragma("GCC visibility pop")
417 #else
418 #  define __dso_public
419 #  define __dso_hidden
420 #  define __BEGIN_PUBLIC_DECLS	__BEGIN_EXTERN_C
421 #  define __END_PUBLIC_DECLS	__END_EXTERN_C
422 #  define __BEGIN_HIDDEN_DECLS	__BEGIN_EXTERN_C
423 #  define __END_HIDDEN_DECLS	__END_EXTERN_C
424 #endif
425 #if __GNUC_PREREQ__(4, 2) || defined(__lint__)
426 #  define __dso_protected	__attribute__((__visibility__("protected")))
427 #else
428 #  define __dso_protected
429 #endif
430 
431 #define	__BEGIN_DECLS		__BEGIN_PUBLIC_DECLS
432 #define	__END_DECLS		__END_PUBLIC_DECLS
433 
434 /*
435  * Non-static C99 inline functions are optional bodies.  They don't
436  * create global symbols if not used, but can be replaced if desirable.
437  * This differs from the behavior of GCC before version 4.3.  The nearest
438  * equivalent for older GCC is `extern inline'.  For newer GCC, use the
439  * gnu_inline attribute additionally to get the old behavior.
440  *
441  * For C99 compilers other than GCC, the C99 behavior is expected.
442  */
443 #if defined(__GNUC__) && defined(__GNUC_STDC_INLINE__)
444 #define	__c99inline	extern __attribute__((__gnu_inline__)) __inline
445 #elif defined(__GNUC__)
446 #define	__c99inline	extern __inline
447 #elif defined(__STDC_VERSION__) || defined(__lint__)
448 #define	__c99inline	__inline
449 #endif
450 
451 #if defined(__lint__)
452 #define __thread	/* delete */
453 #define	__packed	__packed
454 #define	__aligned(x)	/* delete */
455 #define	__section(x)	/* delete */
456 #elif __GNUC_PREREQ__(2, 7) || defined(__PCC__) || defined(__lint__)
457 #define	__packed	__attribute__((__packed__))
458 #define	__aligned(x)	__attribute__((__aligned__(x)))
459 #define	__section(x)	__attribute__((__section__(x)))
460 #elif defined(_MSC_VER)
461 #define	__packed	/* ignore */
462 #else
463 #define	__packed	error: no __packed for this compiler
464 #define	__aligned(x)	error: no __aligned for this compiler
465 #define	__section(x)	error: no __section for this compiler
466 #endif
467 
468 /*
469  * C99 defines the restrict type qualifier keyword, which was made available
470  * in GCC 2.92.
471  */
472 #if __STDC_VERSION__ >= 199901L
473 #define	__restrict	restrict
474 #elif __GNUC_PREREQ__(2, 92)
475 #define	__restrict	__restrict__
476 #else
477 #define	__restrict	/* delete __restrict when not supported */
478 #endif
479 
480 /*
481  * C99 and C++11 define __func__ predefined identifier, which was made
482  * available in GCC 2.95.
483  */
484 #if !(__STDC_VERSION__ >= 199901L) && !(__cplusplus - 0 >= 201103L)
485 #if __GNUC_PREREQ__(2, 4) || defined(__lint__)
486 #define	__func__	__FUNCTION__
487 #else
488 #define	__func__	""
489 #endif
490 #endif /* !(__STDC_VERSION__ >= 199901L) && !(__cplusplus - 0 >= 201103L) */
491 
492 #if defined(_KERNEL) && defined(NO_KERNEL_RCSIDS)
493 #undef	__KERNEL_RCSID
494 #define	__KERNEL_RCSID(_n, _s)	/* nothing */
495 #undef	__RCSID
496 #define	__RCSID(_s)		/* nothing */
497 #endif
498 
499 #if !defined(_STANDALONE) && !defined(_KERNEL)
500 #if defined(__GNUC__) || defined(__PCC__)
501 #define	__RENAME(x)	___RENAME(x)
502 #elif defined(__lint__)
503 #define	__RENAME(x)	__symbolrename(x)
504 #else
505 #error "No function renaming possible"
506 #endif /* __GNUC__ */
507 #else /* _STANDALONE || _KERNEL */
508 #define	__RENAME(x)	no renaming in kernel/standalone environment
509 #endif
510 
511 /*
512  * A barrier to stop the optimizer from moving code or assume live
513  * register values. This is gcc specific, the version is more or less
514  * arbitrary, might work with older compilers.
515  */
516 #if __GNUC_PREREQ__(2, 95) || defined(__lint__)
517 #define	__insn_barrier()	__asm __volatile("":::"memory")
518 #else
519 #define	__insn_barrier()	/* */
520 #endif
521 
522 /*
523  * GNU C version 2.96 adds explicit branch prediction so that
524  * the CPU back-end can hint the processor and also so that
525  * code blocks can be reordered such that the predicted path
526  * sees a more linear flow, thus improving cache behavior, etc.
527  *
528  * The following two macros provide us with a way to use this
529  * compiler feature.  Use __predict_true() if you expect the expression
530  * to evaluate to true, and __predict_false() if you expect the
531  * expression to evaluate to false.
532  *
533  * A few notes about usage:
534  *
535  *	* Generally, __predict_false() error condition checks (unless
536  *	  you have some _strong_ reason to do otherwise, in which case
537  *	  document it), and/or __predict_true() `no-error' condition
538  *	  checks, assuming you want to optimize for the no-error case.
539  *
540  *	* Other than that, if you don't know the likelihood of a test
541  *	  succeeding from empirical or other `hard' evidence, don't
542  *	  make predictions.
543  *
544  *	* These are meant to be used in places that are run `a lot'.
545  *	  It is wasteful to make predictions in code that is run
546  *	  seldomly (e.g. at subsystem initialization time) as the
547  *	  basic block reordering that this affects can often generate
548  *	  larger code.
549  */
550 #if __GNUC_PREREQ__(2, 96) || defined(__lint__)
551 #define	__predict_true(exp)	__builtin_expect((exp) != 0, 1)
552 #define	__predict_false(exp)	__builtin_expect((exp) != 0, 0)
553 #else
554 #define	__predict_true(exp)	(exp)
555 #define	__predict_false(exp)	(exp)
556 #endif
557 
558 /*
559  * Compiler-dependent macros to declare that functions take printf-like
560  * or scanf-like arguments.  They are null except for versions of gcc
561  * that are known to support the features properly (old versions of gcc-2
562  * didn't permit keeping the keywords out of the application namespace).
563  */
564 #if __GNUC_PREREQ__(2, 7) || defined(__lint__)
565 #define __printflike(fmtarg, firstvararg)	\
566 	    __attribute__((__format__ (__printf__, fmtarg, firstvararg)))
567 #ifndef __syslog_attribute__
568 #define __syslog__ __printf__
569 #endif
570 #define __sysloglike(fmtarg, firstvararg)	\
571 	    __attribute__((__format__ (__syslog__, fmtarg, firstvararg)))
572 #define __scanflike(fmtarg, firstvararg)	\
573 	    __attribute__((__format__ (__scanf__, fmtarg, firstvararg)))
574 #define __format_arg(fmtarg)    __attribute__((__format_arg__ (fmtarg)))
575 #else
576 #define __printflike(fmtarg, firstvararg)	/* nothing */
577 #define __scanflike(fmtarg, firstvararg)	/* nothing */
578 #define __sysloglike(fmtarg, firstvararg)	/* nothing */
579 #define __format_arg(fmtarg)			/* nothing */
580 #endif
581 
582 /*
583  * Macros for manipulating "link sets".  Link sets are arrays of pointers
584  * to objects, which are gathered up by the linker.
585  *
586  * Object format-specific code has provided us with the following macros:
587  *
588  *	__link_set_add_text(set, sym)
589  *		Add a reference to the .text symbol `sym' to `set'.
590  *
591  *	__link_set_add_rodata(set, sym)
592  *		Add a reference to the .rodata symbol `sym' to `set'.
593  *
594  *	__link_set_add_data(set, sym)
595  *		Add a reference to the .data symbol `sym' to `set'.
596  *
597  *	__link_set_add_bss(set, sym)
598  *		Add a reference to the .bss symbol `sym' to `set'.
599  *
600  *	__link_set_decl(set, ptype)
601  *		Provide an extern declaration of the set `set', which
602  *		contains an array of pointers to type `ptype'.  This
603  *		macro must be used by any code which wishes to reference
604  *		the elements of a link set.
605  *
606  *	__link_set_start(set)
607  *		This points to the first slot in the link set.
608  *
609  *	__link_set_end(set)
610  *		This points to the (non-existent) slot after the last
611  *		entry in the link set.
612  *
613  *	__link_set_count(set)
614  *		Count the number of entries in link set `set'.
615  *
616  * In addition, we provide the following macros for accessing link sets:
617  *
618  *	__link_set_foreach(pvar, set)
619  *		Iterate over the link set `set'.  Because a link set is
620  *		an array of pointers, pvar must be declared as "type **pvar",
621  *		and the actual entry accessed as "*pvar".
622  *
623  *	__link_set_entry(set, idx)
624  *		Access the link set entry at index `idx' from set `set'.
625  */
626 #define	__link_set_foreach(pvar, set)					\
627 	for (pvar = __link_set_start(set); pvar < __link_set_end(set); pvar++)
628 
629 #define	__link_set_entry(set, idx)	(__link_set_start(set)[idx])
630 
631 /*
632  * Return the natural alignment in bytes for the given type
633  */
634 #if __GNUC_PREREQ__(4, 1) || defined(__lint__)
635 #define	__alignof(__t)  __alignof__(__t)
636 #else
637 #define __alignof(__t) (sizeof(struct { char __x; __t __y; }) - sizeof(__t))
638 #endif
639 
640 /*
641  * Return the number of elements in a statically-allocated array,
642  * __x.
643  */
644 #define	__arraycount(__x)	(sizeof(__x) / sizeof(__x[0]))
645 
646 #ifndef __ASSEMBLER__
647 /* __BIT(n): nth bit, where __BIT(0) == 0x1. */
648 #define	__BIT(__n)	\
649     (((uintmax_t)(__n) >= NBBY * sizeof(uintmax_t)) ? 0 : \
650     ((uintmax_t)1 << (uintmax_t)((__n) & (NBBY * sizeof(uintmax_t) - 1))))
651 
652 /* __MASK(n): first n bits all set, where __MASK(4) == 0b1111. */
653 #define	__MASK(__n)	(__BIT(__n) - 1)
654 
655 /* Macros for min/max. */
656 #define	__MIN(a,b)	((/*CONSTCOND*/(a)<=(b))?(a):(b))
657 #define	__MAX(a,b)	((/*CONSTCOND*/(a)>(b))?(a):(b))
658 
659 /* __BITS(m, n): bits m through n, m < n. */
660 #define	__BITS(__m, __n)	\
661 	((__BIT(__MAX((__m), (__n)) + 1) - 1) ^ (__BIT(__MIN((__m), (__n))) - 1))
662 #endif /* !__ASSEMBLER__ */
663 
664 /* find least significant bit that is set */
665 #define	__LOWEST_SET_BIT(__mask) ((((__mask) - 1) & (__mask)) ^ (__mask))
666 
667 #define	__PRIuBIT	PRIuMAX
668 #define	__PRIuBITS	__PRIuBIT
669 
670 #define	__PRIxBIT	PRIxMAX
671 #define	__PRIxBITS	__PRIxBIT
672 
673 #define	__SHIFTOUT(__x, __mask)	(((__x) & (__mask)) / __LOWEST_SET_BIT(__mask))
674 #define	__SHIFTIN(__x, __mask) ((__x) * __LOWEST_SET_BIT(__mask))
675 #define	__SHIFTOUT_MASK(__mask) __SHIFTOUT((__mask), (__mask))
676 
677 /*
678  * Only to be used in other headers that are included from both c or c++
679  * NOT to be used in code.
680  */
681 #ifdef __cplusplus
682 #define __CAST(__dt, __st)	static_cast<__dt>(__st)
683 #else
684 #define __CAST(__dt, __st)	((__dt)(__st))
685 #endif
686 
687 #define __CASTV(__dt, __st)	__CAST(__dt, __CAST(void *, __st))
688 #define __CASTCV(__dt, __st)	__CAST(__dt, __CAST(const void *, __st))
689 
690 /*
691  * Suppresses `variable set but not used' warnings.
692  *
693  * Typically for #ifdefs, where one branch of the #ifdef uses a
694  * variable but the other does not.  Useful in patching external code
695  * to keep the patches narrowly scoped.
696  *
697  * Limitation: Only for variables, and only non-volatile variables.
698  *
699  * (Abusing this for anything else may lead to side effects.  Pointers
700  * to volatile objects are OK, as in `volatile int *a', as long as the
701  * pointer itself is not volatile, as in `int *volatile a'.)
702  */
703 #define	__USE(a) (/*LINTED*/(void)(a))
704 
705 /*
706  * Verifies the expression e compiles, but does not evaluate it.  Safe
707  * when e has side effects.
708  *
709  * Typically used for the arguments to macros with conditional
710  * definitions like DIAGNOSTIC or KDTRACE_HOOKS: when enabled, the
711  * macro uses the argument; when disabled, the macro passes the
712  * argument to __MACROUSE but doesn't otherwise use it.  Cast to long
713  * in case the argument is a bit field, which is forbidden in sizeof.
714  *
715  * Limitation: Doesn't work for expressions of aggregate (struct/union)
716  * types.
717  *
718  * (If you find a way to handle both bit fields and aggregate types,
719  * you could unify __USE and __MACROUSE.)
720  */
721 #define	__MACROUSE(e)	(/*LINTED*/(void)sizeof((long)(e)))
722 
723 #define __type_mask(t) (/*LINTED*/sizeof(t) < sizeof(intmax_t) ? \
724     (~((1ULL << (sizeof(t) * NBBY)) - 1)) : 0ULL)
725 
726 #ifndef __ASSEMBLER__
__zeroll(void)727 static __inline long long __zeroll(void) { return 0; }
__zeroull(void)728 static __inline unsigned long long __zeroull(void) { return 0; }
729 #else
730 #define __zeroll() (0LL)
731 #define __zeroull() (0ULL)
732 #endif
733 
734 #define __negative_p(x) (!((x) > 0) && ((x) != 0))
735 
736 #define __type_min_s(t) ((t)((1ULL << (sizeof(t) * NBBY - 1))))
737 #define __type_max_s(t) ((t)~((1ULL << (sizeof(t) * NBBY - 1))))
738 #define __type_min_u(t) ((t)0ULL)
739 #define __type_max_u(t) ((t)~0ULL)
740 #define __type_is_signed(t) (/*LINTED*/__type_min_s(t) + (t)1 < (t)1)
741 #define __type_min(t) (__type_is_signed(t) ? __type_min_s(t) : __type_min_u(t))
742 #define __type_max(t) (__type_is_signed(t) ? __type_max_s(t) : __type_max_u(t))
743 
744 
745 #define __type_fit_u(t, a) (/*LINTED*/!__negative_p(a) && \
746     (uintmax_t)((a) + __zeroull()) <= (uintmax_t)__type_max_u(t))
747 
748 #define __type_fit_s(t, a) (/*LINTED*/__negative_p(a) ? \
749     ((intmax_t)((a) + __zeroll()) >= (intmax_t)__type_min_s(t)) : \
750     ((intmax_t)((a) + __zeroll()) >= (intmax_t)0 && \
751      (intmax_t)((a) + __zeroll()) <= (intmax_t)__type_max_s(t)))
752 
753 /*
754  * return true if value 'a' fits in type 't'
755  */
756 #define __type_fit(t, a) (__type_is_signed(t) ? \
757     __type_fit_s(t, a) : __type_fit_u(t, a))
758 
759 #endif /* !_SYS_CDEFS_H_ */
760