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