1 /* $NetBSD: cdefs.h,v 1.166 2025/04/06 22:43:08 rillig 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 (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) _Alignas((x))
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 * We use an explicit ternary operator to map any value to exactly 0 or
551 * 1 so that (a) we can specify one value to expect, and (b) the given
552 * expression occurs only in the position of a conditional so that C++
553 * classes with conversion to bool work as if this were a conditional.
554 * In contrast, say, `(exp) != 0' would require the type of exp to
555 * support conversion to integer as well.
556 */
557 #if __GNUC_PREREQ__(2, 96) || defined(__lint__)
558 #define __predict_true(exp) __builtin_expect((exp) ? 1 : 0, 1)
559 #define __predict_false(exp) __builtin_expect((exp) ? 1 : 0, 0)
560 #else
561 #define __predict_true(exp) (exp)
562 #define __predict_false(exp) (exp)
563 #endif
564
565 /*
566 * Compiler-dependent macros to declare that functions take printf-like
567 * or scanf-like arguments. They are null except for versions of gcc
568 * that are known to support the features properly (old versions of gcc-2
569 * didn't permit keeping the keywords out of the application namespace).
570 */
571 #if __GNUC_PREREQ__(2, 7) || defined(__lint__)
572 #define __printflike(fmtarg, firstvararg) \
573 __attribute__((__format__ (__printf__, fmtarg, firstvararg)))
574 #ifndef __syslog_attribute__
575 #define __syslog__ __printf__
576 #endif
577 #define __sysloglike(fmtarg, firstvararg) \
578 __attribute__((__format__ (__syslog__, fmtarg, firstvararg)))
579 #define __scanflike(fmtarg, firstvararg) \
580 __attribute__((__format__ (__scanf__, fmtarg, firstvararg)))
581 #define __format_arg(fmtarg) __attribute__((__format_arg__ (fmtarg)))
582 #else
583 #define __printflike(fmtarg, firstvararg) /* nothing */
584 #define __scanflike(fmtarg, firstvararg) /* nothing */
585 #define __sysloglike(fmtarg, firstvararg) /* nothing */
586 #define __format_arg(fmtarg) /* nothing */
587 #endif
588
589 /*
590 * Macros for manipulating "link sets". Link sets are arrays of pointers
591 * to objects, which are gathered up by the linker.
592 *
593 * Object format-specific code has provided us with the following macros:
594 *
595 * __link_set_add_text(set, sym)
596 * Add a reference to the .text symbol `sym' to `set'.
597 *
598 * __link_set_add_rodata(set, sym)
599 * Add a reference to the .rodata symbol `sym' to `set'.
600 *
601 * __link_set_add_data(set, sym)
602 * Add a reference to the .data symbol `sym' to `set'.
603 *
604 * __link_set_add_bss(set, sym)
605 * Add a reference to the .bss symbol `sym' to `set'.
606 *
607 * __link_set_decl(set, ptype)
608 * Provide an extern declaration of the set `set', which
609 * contains an array of pointers to type `ptype'. This
610 * macro must be used by any code which wishes to reference
611 * the elements of a link set.
612 *
613 * __link_set_start(set)
614 * This points to the first slot in the link set.
615 *
616 * __link_set_end(set)
617 * This points to the (non-existent) slot after the last
618 * entry in the link set.
619 *
620 * __link_set_count(set)
621 * Count the number of entries in link set `set'.
622 *
623 * In addition, we provide the following macros for accessing link sets:
624 *
625 * __link_set_foreach(pvar, set)
626 * Iterate over the link set `set'. Because a link set is
627 * an array of pointers, pvar must be declared as "type **pvar",
628 * and the actual entry accessed as "*pvar".
629 *
630 * __link_set_entry(set, idx)
631 * Access the link set entry at index `idx' from set `set'.
632 */
633 #define __link_set_foreach(pvar, set) \
634 for (pvar = __link_set_start(set); pvar < __link_set_end(set); pvar++)
635
636 #define __link_set_entry(set, idx) (__link_set_start(set)[idx])
637
638 /*
639 * Return the natural alignment in bytes for the given type
640 */
641 #if __GNUC_PREREQ__(4, 1) || defined(__lint__)
642 #define __alignof(__t) __alignof__(__t)
643 #else
644 #define __alignof(__t) (sizeof(struct { char __x; __t __y; }) - sizeof(__t))
645 #endif
646
647 /*
648 * Return the number of elements in a statically-allocated array,
649 * __x.
650 */
651 #define __arraycount(__x) (sizeof(__x) / sizeof(__x[0]))
652
653 #ifndef __ASSEMBLER__
654 /* __BIT(n): nth bit, where __BIT(0) == 0x1. */
655 #define __BIT(__n) \
656 (((__UINTMAX_TYPE__)(__n) >= __CHAR_BIT__ * sizeof(__UINTMAX_TYPE__)) \
657 ? 0 \
658 : ((__UINTMAX_TYPE__)1 << \
659 (__UINTMAX_TYPE__)((__n) & \
660 (__CHAR_BIT__ * sizeof(__UINTMAX_TYPE__) - 1))))
661
662 /* __MASK(n): first n bits all set, where __MASK(4) == 0b1111. */
663 #define __MASK(__n) (__BIT(__n) - 1)
664
665 /* Macros for min/max. */
666 #define __MIN(a,b) ((/*CONSTCOND*/(a)<=(b))?(a):(b))
667 #define __MAX(a,b) ((/*CONSTCOND*/(a)>(b))?(a):(b))
668
669 /* __BITS(m, n): bits m through n, m < n. */
670 #define __BITS(__m, __n) \
671 ((__BIT(__MAX((__m), (__n)) + 1) - 1) ^ (__BIT(__MIN((__m), (__n))) - 1))
672 #endif /* !__ASSEMBLER__ */
673
674 /* find least significant bit that is set */
675 #define __LOWEST_SET_BIT(__mask) ((((__mask) - 1) & (__mask)) ^ (__mask))
676
677 #define __PRIuBIT PRIuMAX
678 #define __PRIuBITS __PRIuBIT
679
680 #define __PRIxBIT PRIxMAX
681 #define __PRIxBITS __PRIxBIT
682
683 #define __SHIFTOUT(__x, __mask) (((__x) & (__mask)) / __LOWEST_SET_BIT(__mask))
684 #define __SHIFTIN(__x, __mask) ((__x) * __LOWEST_SET_BIT(__mask))
685 #define __SHIFTOUT_MASK(__mask) __SHIFTOUT((__mask), (__mask))
686
687 /*
688 * Only to be used in other headers that are included from both c or c++
689 * NOT to be used in code.
690 */
691 #ifdef __cplusplus
692 #define __CAST(__dt, __st) static_cast<__dt>(__st)
693 #else
694 #define __CAST(__dt, __st) ((__dt)(__st))
695 #endif
696
697 #define __CASTV(__dt, __st) __CAST(__dt, __CAST(void *, __st))
698 #define __CASTCV(__dt, __st) __CAST(__dt, __CAST(const void *, __st))
699
700 /*
701 * Suppresses `variable set but not used' warnings.
702 *
703 * Typically for #ifdefs, where one branch of the #ifdef uses a
704 * variable but the other does not. Useful in patching external code
705 * to keep the patches narrowly scoped.
706 *
707 * Limitation: Only for variables, and only non-volatile variables.
708 *
709 * (Abusing this for anything else may lead to side effects. Pointers
710 * to volatile objects are OK, as in `volatile int *a', as long as the
711 * pointer itself is not volatile, as in `int *volatile a'.)
712 */
713 #define __USE(a) (/*LINTED*/(void)(a))
714
715 /*
716 * Verifies the expression e compiles, but does not evaluate it. Safe
717 * when e has side effects.
718 *
719 * Typically used for the arguments to macros with conditional
720 * definitions like DIAGNOSTIC or KDTRACE_HOOKS: when enabled, the
721 * macro uses the argument; when disabled, the macro passes the
722 * argument to __MACROUSE but doesn't otherwise use it. Cast to long
723 * in case the argument is a bit field, which is forbidden in sizeof.
724 *
725 * Limitation: Doesn't work for expressions of aggregate (struct/union)
726 * types.
727 *
728 * (If you find a way to handle both bit fields and aggregate types,
729 * you could unify __USE and __MACROUSE.)
730 */
731 #define __MACROUSE(e) (/*LINTED*/(void)sizeof((long)(e)))
732
733 #define __type_mask(t) (/*LINTED*/sizeof(t) < sizeof(__INTMAX_TYPE__) ? \
734 (~((1ULL << (sizeof(t) * __CHAR_BIT__)) - 1)) : 0ULL)
735
736 #ifndef __ASSEMBLER__
__zeroll(void)737 static __inline long long __zeroll(void) { return 0; }
__zeroull(void)738 static __inline unsigned long long __zeroull(void) { return 0; }
739 #else
740 #define __zeroll() (0LL)
741 #define __zeroull() (0ULL)
742 #endif
743
744 #define __negative_p(x) (!((x) > 0) && ((x) != 0))
745
746 #define __type_min_s(t) ((t)((1ULL << (sizeof(t) * __CHAR_BIT__ - 1))))
747 #define __type_max_s(t) ((t)~((1ULL << (sizeof(t) * __CHAR_BIT__ - 1))))
748 #define __type_min_u(t) ((t)0ULL)
749 #define __type_max_u(t) ((t)~0ULL)
750 #define __type_is_signed(t) (/*LINTED*/__type_min_s(t) + (t)1 < (t)1)
751 #define __type_min(t) (__type_is_signed(t) ? __type_min_s(t) : __type_min_u(t))
752 #define __type_max(t) (__type_is_signed(t) ? __type_max_s(t) : __type_max_u(t))
753
754
755 #define __type_fit_u(t, a) \
756 (/*LINTED*/!__negative_p(a) && \
757 ((__UINTMAX_TYPE__)((a) + __zeroull()) <= \
758 (__UINTMAX_TYPE__)__type_max_u(t)))
759
760 #define __type_fit_s(t, a) \
761 (/*LINTED*/__negative_p(a) \
762 ? ((__INTMAX_TYPE__)((a) + __zeroll()) >= \
763 (__INTMAX_TYPE__)__type_min_s(t)) \
764 : ((__INTMAX_TYPE__)((a) + __zeroll()) >= (__INTMAX_TYPE__)0 && \
765 ((__INTMAX_TYPE__)((a) + __zeroll()) <= \
766 (__INTMAX_TYPE__)__type_max_s(t))))
767
768 /*
769 * return true if value 'a' fits in type 't'
770 */
771 #define __type_fit(t, a) (__type_is_signed(t) ? \
772 __type_fit_s(t, a) : __type_fit_u(t, a))
773
774 #endif /* !_SYS_CDEFS_H_ */
775