1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 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 35 #ifndef _SYS_CDEFS_H_ 36 #define _SYS_CDEFS_H_ 37 38 #if defined(_KERNEL) && defined(_STANDALONE) 39 #error "_KERNEL and _STANDALONE are mutually exclusive" 40 #endif 41 42 /* 43 * Provide clang-compatible testing macros. All supported versions of gcc (10+) 44 * provide all of these except has_feature and has_extension which are new in 45 * gcc 14. Keep the older ifndefs, though, for non-gcc compilers that may lack 46 * them like tcc and pcc. 47 */ 48 #ifndef __has_attribute 49 #define __has_attribute(x) 0 50 #endif 51 #ifndef __has_extension 52 #define __has_extension __has_feature 53 #endif 54 #ifndef __has_feature 55 #define __has_feature(x) 0 56 #endif 57 #ifndef __has_include 58 #define __has_include(x) 0 59 #endif 60 #ifndef __has_builtin 61 #define __has_builtin(x) 0 62 #endif 63 64 #if defined(__cplusplus) 65 #define __BEGIN_DECLS extern "C" { 66 #define __END_DECLS } 67 #else 68 #define __BEGIN_DECLS 69 #define __END_DECLS 70 #endif 71 72 /* 73 * This code has been put in place to help reduce the addition of 74 * compiler specific defines in FreeBSD code. It helps to aid in 75 * having a compiler-agnostic source tree. 76 */ 77 78 /* 79 * Macro to test if we're using a specific version of gcc or later. 80 */ 81 #if defined(__GNUC__) 82 #define __GNUC_PREREQ__(ma, mi) \ 83 (__GNUC__ > (ma) || __GNUC__ == (ma) && __GNUC_MINOR__ >= (mi)) 84 #else 85 #define __GNUC_PREREQ__(ma, mi) 0 86 #endif 87 88 #if defined(__GNUC__) 89 90 /* 91 * Compiler memory barriers, specific to gcc and clang. 92 */ 93 #define __compiler_membar() __asm __volatile(" " : : : "memory") 94 95 #define __CC_SUPPORTS___INLINE 1 96 #define __CC_SUPPORTS_SYMVER 1 97 98 #endif /* __GNUC__ */ 99 100 /* 101 * TinyC pretends to be gcc 9.3. This is generally good enough to support 102 * everything FreeBSD... except for the .symver assembler directive. 103 */ 104 #ifdef __TINYC__ 105 #undef __CC_SUPPORTS_SYMVER 106 #endif 107 108 /* 109 * The __CONCAT macro is used to concatenate parts of symbol names, e.g. 110 * with "#define OLD(foo) __CONCAT(old,foo)", OLD(foo) produces oldfoo. 111 * The __CONCAT macro is a bit tricky to use if it must work in non-ANSI 112 * mode -- there must be no spaces between its arguments, and for nested 113 * __CONCAT's, all the __CONCAT's must be at the left. __CONCAT can also 114 * concatenate double-quoted strings produced by the __STRING macro, but 115 * this only works with ANSI C. 116 * 117 * __XSTRING is like __STRING, but it expands any macros in its argument 118 * first. It is only available with ANSI C. 119 */ 120 #if defined(__STDC__) || defined(__cplusplus) 121 #define __P(protos) protos /* full-blown ANSI C */ 122 #define __CONCAT1(x,y) x ## y 123 #define __CONCAT(x,y) __CONCAT1(x,y) 124 #define __STRING(x) #x /* stringify without expanding x */ 125 #define __XSTRING(x) __STRING(x) /* expand x, then stringify */ 126 127 #define __volatile volatile 128 #if defined(__cplusplus) 129 #define __inline inline /* convert to C++ keyword */ 130 #else 131 #if !(defined(__CC_SUPPORTS___INLINE)) 132 #define __inline /* delete GCC keyword */ 133 #endif /* ! __CC_SUPPORTS___INLINE */ 134 #endif /* !__cplusplus */ 135 136 #else /* !(__STDC__ || __cplusplus) */ 137 #define __P(protos) () /* traditional C preprocessor */ 138 #define __CONCAT(x,y) x/**/y 139 #define __STRING(x) "x" 140 #if !defined(__CC_SUPPORTS___INLINE) 141 /* Just delete these in a K&R environment */ 142 #define __inline 143 #define __volatile 144 #endif /* !__CC_SUPPORTS___INLINE */ 145 #endif /* !(__STDC__ || __cplusplus) */ 146 147 /* 148 * Compiler-dependent macros to help declare dead (non-returning) and pure (no 149 * side effects) functions, and unused variables. These attributes are supported 150 * by all current compilers, even pcc. 151 */ 152 #define __weak_symbol __attribute__((__weak__)) 153 #define __dead2 __attribute__((__noreturn__)) 154 #define __pure2 __attribute__((__const__)) 155 #define __unused __attribute__((__unused__)) 156 #define __used __attribute__((__used__)) 157 #define __deprecated __attribute__((__deprecated__)) 158 #define __deprecated1(msg) __attribute__((__deprecated__(msg))) 159 #define __packed __attribute__((__packed__)) 160 #define __aligned(x) __attribute__((__aligned__(x))) 161 #define __section(x) __attribute__((__section__(x))) 162 #define __writeonly __unused 163 #define __alloc_size(x) __attribute__((__alloc_size__(x))) 164 #define __alloc_size2(n, x) __attribute__((__alloc_size__(n, x))) 165 #define __alloc_align(x) __attribute__((__alloc_align__(x))) 166 167 /* 168 * Keywords added in C11. 169 */ 170 171 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L 172 173 #if !__has_extension(c_alignas) 174 #if (defined(__cplusplus) && __cplusplus >= 201103L) || \ 175 __has_extension(cxx_alignas) 176 #define _Alignas(x) alignas(x) 177 #else 178 /* XXX: Only emulates _Alignas(constant-expression); not _Alignas(type-name). */ 179 #define _Alignas(x) __aligned(x) 180 #endif 181 #endif 182 183 #if defined(__cplusplus) && __cplusplus >= 201103L 184 #define _Alignof(x) alignof(x) 185 #else 186 #define _Alignof(x) __alignof(x) 187 #endif 188 189 #if defined(__cplusplus) && __cplusplus >= 201103L 190 #define _Noreturn [[noreturn]] 191 #else 192 #define _Noreturn __dead2 193 #endif 194 195 #if !__has_extension(c_static_assert) 196 #if (defined(__cplusplus) && __cplusplus >= 201103L) || \ 197 __has_extension(cxx_static_assert) 198 #define _Static_assert(x, y) static_assert(x, y) 199 #endif 200 #endif 201 202 #if !__has_extension(c_thread_local) 203 #if (defined(__cplusplus) && __cplusplus >= 201103L) || \ 204 __has_extension(cxx_thread_local) 205 #define _Thread_local thread_local 206 #else 207 #define _Thread_local __thread 208 #endif 209 #endif 210 211 #endif /* __STDC_VERSION__ || __STDC_VERSION__ < 201112L */ 212 213 /* 214 * Emulation of C11 _Generic(). Unlike the previously defined C11 215 * keywords, it is not possible to implement this using exactly the same 216 * syntax. Therefore implement something similar under the name 217 * __generic(). Unlike _Generic(), this macro can only distinguish 218 * between a single type, so it requires nested invocations to 219 * distinguish multiple cases. 220 * 221 * Note that the comma operator is used to force expr to decay in 222 * order to match _Generic(). 223 */ 224 225 #if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) || \ 226 __has_extension(c_generic_selections) 227 #define __generic(expr, t, yes, no) \ 228 _Generic(expr, t: yes, default: no) 229 #elif !defined(__cplusplus) 230 #define __generic(expr, t, yes, no) \ 231 __builtin_choose_expr(__builtin_types_compatible_p( \ 232 __typeof(((void)0, (expr))), t), yes, no) 233 #endif 234 235 /* 236 * C99 Static array indices in function parameter declarations. Syntax such as: 237 * void bar(int myArray[static 10]); 238 * is allowed in C99 but not in C++. Define __min_size appropriately so 239 * headers using it can be compiled in either language. Use like this: 240 * void bar(int myArray[__min_size(10)]); 241 */ 242 #if !defined(__cplusplus) && \ 243 (!defined(__STDC_VERSION__) || (__STDC_VERSION__ >= 199901)) 244 #define __min_size(x) static (x) 245 #else 246 #define __min_size(x) (x) 247 #endif 248 249 #define __malloc_like __attribute__((__malloc__)) 250 #define __pure __attribute__((__pure__)) 251 252 #define __always_inline __inline __attribute__((__always_inline__)) 253 #define __noinline __attribute__ ((__noinline__)) 254 #define __fastcall __attribute__((__fastcall__)) 255 #define __result_use_check __attribute__((__warn_unused_result__)) 256 #ifdef __clang__ 257 /* 258 * clang and gcc have different semantics for __warn_unused_result__: the latter 259 * does not permit the use of a void cast to suppress the warning. Use 260 * __result_use_or_ignore_check in places where a void cast is acceptable. 261 * This can be implemented by [[nodiscard]] from C23. 262 */ 263 #define __result_use_or_ignore_check __result_use_check 264 #else 265 #define __result_use_or_ignore_check 266 #endif /* !__clang__ */ 267 268 #define __returns_twice __attribute__((__returns_twice__)) 269 270 #define __unreachable() __builtin_unreachable() 271 272 #if !defined(__STRICT_ANSI__) || __STDC_VERSION__ >= 199901 273 #define __LONG_LONG_SUPPORTED 274 #endif 275 276 /* C++11 exposes a load of C99 stuff */ 277 #if defined(__cplusplus) && __cplusplus >= 201103L 278 #define __LONG_LONG_SUPPORTED 279 #ifndef __STDC_LIMIT_MACROS 280 #define __STDC_LIMIT_MACROS 281 #endif 282 #ifndef __STDC_CONSTANT_MACROS 283 #define __STDC_CONSTANT_MACROS 284 #endif 285 #endif 286 287 /* 288 * noexcept keyword added in C++11. 289 */ 290 #if defined(__cplusplus) && __cplusplus >= 201103L 291 #define __noexcept noexcept 292 #define __noexcept_if(__c) noexcept(__c) 293 #else 294 #define __noexcept 295 #define __noexcept_if(__c) 296 #endif 297 298 /* 299 * We use `__restrict' as a way to define the `restrict' type qualifier 300 * without disturbing older software that is unaware of C99 keywords. 301 * GCC also provides `__restrict' as an extension to support C99-style 302 * restricted pointers in other language modes. 303 */ 304 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901 305 #define __restrict restrict 306 #endif 307 308 /* 309 * All modern compilers have explicit branch prediction so that the CPU back-end 310 * can hint to the processor and also so that code blocks can be reordered such 311 * that the predicted path sees a more linear flow, thus improving cache 312 * behavior, etc. Use sparingly, except in performance critical code where 313 * they make things measurably faster. 314 */ 315 #define __predict_true(exp) __builtin_expect((exp), 1) 316 #define __predict_false(exp) __builtin_expect((exp), 0) 317 318 #define __null_sentinel __attribute__((__sentinel__)) 319 #define __exported __attribute__((__visibility__("default"))) 320 #define __hidden __attribute__((__visibility__("hidden"))) 321 322 /* 323 * We define this here since <stddef.h>, <sys/queue.h>, and <sys/types.h> 324 * require it. 325 */ 326 #define __offsetof(type, field) __builtin_offsetof(type, field) 327 #define __rangeof(type, start, end) \ 328 (__offsetof(type, end) - __offsetof(type, start)) 329 330 /* 331 * Given the pointer x to the member m of the struct s, return 332 * a pointer to the containing structure. When using GCC, we first 333 * assign pointer x to a local variable, to check that its type is 334 * compatible with member m. 335 */ 336 #define __containerof(x, s, m) ({ \ 337 const volatile __typeof(((s *)0)->m) *__x = (x); \ 338 __DEQUALIFY(s *, (const volatile char *)__x - __offsetof(s, m));\ 339 }) 340 341 /* 342 * Compiler-dependent macros to declare that functions take printf-like 343 * or scanf-like arguments. 344 */ 345 #define __printflike(fmtarg, firstvararg) \ 346 __attribute__((__format__ (__printf__, fmtarg, firstvararg))) 347 #define __scanflike(fmtarg, firstvararg) \ 348 __attribute__((__format__ (__scanf__, fmtarg, firstvararg))) 349 #define __format_arg(fmtarg) __attribute__((__format_arg__ (fmtarg))) 350 #define __strfmonlike(fmtarg, firstvararg) \ 351 __attribute__((__format__ (__strfmon__, fmtarg, firstvararg))) 352 #define __strftimelike(fmtarg, firstvararg) \ 353 __attribute__((__format__ (__strftime__, fmtarg, firstvararg))) 354 355 /* 356 * Like __printflike, but allows fmtarg to be NULL. FreeBSD invented 'printf0' 357 * for this because older versions of gcc issued warnings for NULL first args. 358 * Clang has always had printf and printf0 as aliases. gcc 11.0 now follows 359 * clang. So now this is an alias for __printflike, or nothing. In the future 360 * _Nullable or _Nonnull will replace this. 361 * XXX Except that doesn't work, so for now revert to printf0 for clang and 362 * the FreeBSD gcc until I can work this out. 363 */ 364 #if defined(__clang__) || (defined(__GNUC__) && defined (__FreeBSD_cc_version)) 365 #define __printf0like(fmtarg, firstvararg) \ 366 __attribute__((__format__ (__printf0__, fmtarg, firstvararg))) 367 #else 368 #define __printf0like(fmtarg, firstvararg) 369 #endif 370 371 #define __strong_reference(sym,aliassym) \ 372 extern __typeof (sym) aliassym __attribute__ ((__alias__ (#sym))) 373 #ifdef __STDC__ 374 #define __weak_reference(sym,alias) \ 375 __asm__(".weak " #alias); \ 376 __asm__(".equ " #alias ", " #sym) 377 #define __warn_references(sym,msg) \ 378 __asm__(".section .gnu.warning." #sym); \ 379 __asm__(".asciz \"" msg "\""); \ 380 __asm__(".previous") 381 #ifdef __CC_SUPPORTS_SYMVER 382 #define __sym_compat(sym,impl,verid) \ 383 __asm__(".symver " #impl ", " #sym "@" #verid) 384 #define __sym_default(sym,impl,verid) \ 385 __asm__(".symver " #impl ", " #sym "@@@" #verid) 386 #endif 387 #else 388 #define __weak_reference(sym,alias) \ 389 __asm__(".weak alias"); \ 390 __asm__(".equ alias, sym") 391 #define __warn_references(sym,msg) \ 392 __asm__(".section .gnu.warning.sym"); \ 393 __asm__(".asciz \"msg\""); \ 394 __asm__(".previous") 395 #ifdef __CC_SUPPORTS_SYMVER 396 #define __sym_compat(sym,impl,verid) \ 397 __asm__(".symver impl, sym@verid") 398 #define __sym_default(impl,sym,verid) \ 399 __asm__(".symver impl, sym@@@verid") 400 #endif 401 #endif /* __STDC__ */ 402 403 #define __GLOBL(sym) __asm__(".globl " __XSTRING(sym)) 404 #define __WEAK(sym) __asm__(".weak " __XSTRING(sym)) 405 406 #define __IDSTRING(name,string) __asm__(".ident\t\"" string "\"") 407 408 /* 409 * Embed the rcs id of a source file in the resulting library. Note that in 410 * more recent ELF binutils, we use .ident allowing the ID to be stripped. 411 * Usage: 412 */ 413 #ifndef __FBSDID 414 #if !defined(STRIP_FBSDID) 415 #define __FBSDID(s) __IDSTRING(__CONCAT(__rcsid_,__LINE__),s) 416 #else 417 #define __FBSDID(s) struct __hack 418 #endif 419 #endif 420 421 #ifndef __RCSID 422 #ifndef NO__RCSID 423 #define __RCSID(s) __IDSTRING(__CONCAT(__rcsid_,__LINE__),s) 424 #else 425 #define __RCSID(s) struct __hack 426 #endif 427 #endif 428 429 #ifndef __RCSID_SOURCE 430 #ifndef NO__RCSID_SOURCE 431 #define __RCSID_SOURCE(s) __IDSTRING(__CONCAT(__rcsid_source_,__LINE__),s) 432 #else 433 #define __RCSID_SOURCE(s) struct __hack 434 #endif 435 #endif 436 437 #ifndef __SCCSID 438 #ifndef NO__SCCSID 439 #define __SCCSID(s) __IDSTRING(__CONCAT(__sccsid_,__LINE__),s) 440 #else 441 #define __SCCSID(s) struct __hack 442 #endif 443 #endif 444 445 #ifndef __COPYRIGHT 446 #ifndef NO__COPYRIGHT 447 #define __COPYRIGHT(s) __IDSTRING(__CONCAT(__copyright_,__LINE__),s) 448 #else 449 #define __COPYRIGHT(s) struct __hack 450 #endif 451 #endif 452 453 #ifndef __DECONST 454 #define __DECONST(type, var) ((type)(__uintptr_t)(const void *)(var)) 455 #endif 456 457 #ifndef __DEVOLATILE 458 #define __DEVOLATILE(type, var) ((type)(__uintptr_t)(volatile void *)(var)) 459 #endif 460 461 #ifndef __DEQUALIFY 462 #define __DEQUALIFY(type, var) ((type)(__uintptr_t)(const volatile void *)(var)) 463 #endif 464 465 #if !defined(_STANDALONE) && !defined(_KERNEL) 466 #define __RENAME(x) __asm(__STRING(x)) 467 #else /* _STANDALONE || _KERNEL */ 468 #define __RENAME(x) no renaming in kernel/standalone environment 469 #endif 470 471 /*- 472 * The following definitions are an extension of the behavior originally 473 * implemented in <sys/_posix.h>, but with a different level of granularity. 474 * POSIX.1 requires that the macros we test be defined before any standard 475 * header file is included. 476 * 477 * Here's a quick run-down of the versions (and some informal names) 478 * defined(_POSIX_SOURCE) 1003.1-1988 479 * encoded as 198808 below 480 * _POSIX_C_SOURCE == 1 1003.1-1990 481 * encoded as 199009 below 482 * _POSIX_C_SOURCE == 2 1003.2-1992 C Language Binding Option 483 * encoded as 199209 below 484 * _POSIX_C_SOURCE == 199309 1003.1b-1993 485 * (1003.1 Issue 4, Single Unix Spec v1, Unix 93) 486 * _POSIX_C_SOURCE == 199506 1003.1c-1995, 1003.1i-1995, 487 * and the omnibus ISO/IEC 9945-1: 1996 488 * (1003.1 Issue 5, Single Unix Spec v2, Unix 95) 489 * _POSIX_C_SOURCE == 200112 1003.1-2001 (1003.1 Issue 6, Unix 03) 490 * with _XOPEN_SOURCE=600 491 * _POSIX_C_SOURCE == 200809 1003.1-2008 (1003.1 Issue 7) 492 * IEEE Std 1003.1-2017 (Rev of 1003.1-2008) is 493 * 1003.1-2008 with two TCs applied and 494 * _XOPEN_SOURCE=700 495 * _POSIX_C_SOURCE == 202405 1003.1-2004 (1003.1 Issue 8), IEEE Std 1003.1-2024 496 * with _XOPEN_SOURCE=800 497 * 498 * In addition, the X/Open Portability Guide, which is now the Single UNIX 499 * Specification, defines a feature-test macro which indicates the version of 500 * that specification, and which subsumes _POSIX_C_SOURCE. 501 * 502 * Our macros begin with two underscores to avoid namespace screwage. 503 */ 504 505 /* Deal with IEEE Std. 1003.1-1990, in which _POSIX_C_SOURCE == 1. */ 506 #if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE == 1 507 #undef _POSIX_C_SOURCE /* Probably illegal, but beyond caring now. */ 508 #define _POSIX_C_SOURCE 199009 509 #endif 510 511 /* Deal with IEEE Std. 1003.2-1992, in which _POSIX_C_SOURCE == 2. */ 512 #if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE == 2 513 #undef _POSIX_C_SOURCE 514 #define _POSIX_C_SOURCE 199209 515 #endif 516 517 /* 518 * Deal with various X/Open Portability Guides and Single UNIX Spec. We use the 519 * '- 0' construct so software that defines _XOPEN_SOURCE to nothing doesn't 520 * cause errors. X/Open CAE Specification, August 1994, System Interfaces and 521 * Headers, Issue 4, Version 2 section 2.2 states an empty definition means the 522 * same thing as _POSIX_C_SOURCE == 2. This broadly mirrors "System V Interface 523 * Definition, Fourth Edition", but earlier editions suggest some ambiguity. 524 * However, FreeBSD has histoically implemented this as a NOP, so we just 525 * document what it should be for now to not break ports gratuitously. 526 */ 527 #ifdef _XOPEN_SOURCE 528 #if _XOPEN_SOURCE - 0 >= 800 529 #define __XSI_VISIBLE 800 530 #undef _POSIX_C_SOURCE 531 #define _POSIX_C_SOURCE 202405 532 #elif _XOPEN_SOURCE - 0 >= 700 533 #define __XSI_VISIBLE 700 534 #undef _POSIX_C_SOURCE 535 #define _POSIX_C_SOURCE 200809 536 #elif _XOPEN_SOURCE - 0 >= 600 537 #define __XSI_VISIBLE 600 538 #undef _POSIX_C_SOURCE 539 #define _POSIX_C_SOURCE 200112 540 #elif _XOPEN_SOURCE - 0 >= 500 541 #define __XSI_VISIBLE 500 542 #undef _POSIX_C_SOURCE 543 #define _POSIX_C_SOURCE 199506 544 #else 545 /* #define _POSIX_C_SOURCE 199209 */ 546 #endif 547 #endif 548 549 /* 550 * Deal with all versions of POSIX. The ordering relative to the tests above is 551 * important. 552 */ 553 #if defined(_POSIX_SOURCE) && !defined(_POSIX_C_SOURCE) 554 #define _POSIX_C_SOURCE 198808 555 #endif 556 #ifdef _POSIX_C_SOURCE 557 #if _POSIX_C_SOURCE >= 202405 558 #define __POSIX_VISIBLE 202405 559 #define __ISO_C_VISIBLE 2017 560 #elif _POSIX_C_SOURCE >= 200809 561 #define __POSIX_VISIBLE 200809 562 #define __ISO_C_VISIBLE 1999 563 #elif _POSIX_C_SOURCE >= 200112 564 #define __POSIX_VISIBLE 200112 565 #define __ISO_C_VISIBLE 1999 566 #elif _POSIX_C_SOURCE >= 199506 567 #define __POSIX_VISIBLE 199506 568 #define __ISO_C_VISIBLE 1990 569 #elif _POSIX_C_SOURCE >= 199309 570 #define __POSIX_VISIBLE 199309 571 #define __ISO_C_VISIBLE 1990 572 #elif _POSIX_C_SOURCE >= 199209 573 #define __POSIX_VISIBLE 199209 574 #define __ISO_C_VISIBLE 1990 575 #elif _POSIX_C_SOURCE >= 199009 576 #define __POSIX_VISIBLE 199009 577 #define __ISO_C_VISIBLE 1990 578 #else 579 #define __POSIX_VISIBLE 198808 580 #define __ISO_C_VISIBLE 0 581 #endif /* _POSIX_C_SOURCE */ 582 583 /* 584 * When we've explicitly asked for a newer C version, make the C variable 585 * visible by default. Also honor the glibc _ISOC{11,23}_SOURCE macros 586 * extensions. Both glibc and OpenBSD do this, even when a more strict 587 * _POSIX_C_SOURCE has been requested, and it makes good sense (especially for 588 * pre POSIX 2024, since C11 is much nicer than the old C99 base). Continue the 589 * practice with C23, though don't do older standards. Also, GLIBC doesn't have 590 * a _ISOC17_SOURCE, so it's not implemented here. glibc has earlier ISOCxx defines, 591 * but we don't implement those as they are not relevant enough. 592 */ 593 #if _ISOC23_SOURCE || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L) 594 #undef __ISO_C_VISIBLE 595 #define __ISO_C_VISIBLE 2023 596 #elif _ISOC11_SOURCE || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) 597 #undef __ISO_C_VISIBLE 598 #define __ISO_C_VISIBLE 2011 599 #endif 600 #else /* _POSIX_C_SOURCE */ 601 /*- 602 * Deal with _ANSI_SOURCE: 603 * If it is defined, and no other compilation environment is explicitly 604 * requested, then define our internal feature-test macros to zero. This 605 * makes no difference to the preprocessor (undefined symbols in preprocessing 606 * expressions are defined to have value zero), but makes it more convenient for 607 * a test program to print out the values. 608 * 609 * If a program mistakenly defines _ANSI_SOURCE and some other macro such as 610 * _POSIX_C_SOURCE, we will assume that it wants the broader compilation 611 * environment (and in fact we will never get here). 612 */ 613 #if defined(_ANSI_SOURCE) /* Hide almost everything. */ 614 #define __POSIX_VISIBLE 0 615 #define __XSI_VISIBLE 0 616 #define __BSD_VISIBLE 0 617 #define __ISO_C_VISIBLE 1990 618 #define __EXT1_VISIBLE 0 619 #elif defined(_C99_SOURCE) /* Localism to specify strict C99 env. */ 620 #define __POSIX_VISIBLE 0 621 #define __XSI_VISIBLE 0 622 #define __BSD_VISIBLE 0 623 #define __ISO_C_VISIBLE 1999 624 #define __EXT1_VISIBLE 0 625 #elif defined(_C11_SOURCE) /* Localism to specify strict C11 env. */ 626 #define __POSIX_VISIBLE 0 627 #define __XSI_VISIBLE 0 628 #define __BSD_VISIBLE 0 629 #define __ISO_C_VISIBLE 2011 630 #define __EXT1_VISIBLE 0 631 #elif defined(_C23_SOURCE) /* Localism to specify strict C23 env. */ 632 #define __POSIX_VISIBLE 0 633 #define __XSI_VISIBLE 0 634 #define __BSD_VISIBLE 0 635 #define __ISO_C_VISIBLE 2023 636 #define __EXT1_VISIBLE 0 637 #else /* Default environment: show everything. */ 638 #define __POSIX_VISIBLE 202405 639 #define __XSI_VISIBLE 800 640 #define __BSD_VISIBLE 1 641 #define __ISO_C_VISIBLE 2023 642 #define __EXT1_VISIBLE 1 643 #endif 644 #endif /* _POSIX_C_SOURCE */ 645 646 /* User override __EXT1_VISIBLE */ 647 #if defined(__STDC_WANT_LIB_EXT1__) 648 #undef __EXT1_VISIBLE 649 #if __STDC_WANT_LIB_EXT1__ 650 #define __EXT1_VISIBLE 1 651 #else 652 #define __EXT1_VISIBLE 0 653 #endif 654 #endif /* __STDC_WANT_LIB_EXT1__ */ 655 656 /* 657 * Nullability qualifiers: currently only supported by Clang. 658 */ 659 #if !(defined(__clang__) && __has_feature(nullability)) 660 #define _Nonnull 661 #define _Nullable 662 #define _Null_unspecified 663 #define __NULLABILITY_PRAGMA_PUSH 664 #define __NULLABILITY_PRAGMA_POP 665 #else 666 #define __NULLABILITY_PRAGMA_PUSH _Pragma("clang diagnostic push") \ 667 _Pragma("clang diagnostic ignored \"-Wnullability-completeness\"") 668 #define __NULLABILITY_PRAGMA_POP _Pragma("clang diagnostic pop") 669 #endif 670 671 /* 672 * Type Safety Checking 673 * 674 * Clang provides additional attributes to enable checking type safety 675 * properties that cannot be enforced by the C type system. 676 */ 677 678 #if __has_attribute(__argument_with_type_tag__) && \ 679 __has_attribute(__type_tag_for_datatype__) 680 #define __arg_type_tag(arg_kind, arg_idx, type_tag_idx) \ 681 __attribute__((__argument_with_type_tag__(arg_kind, arg_idx, type_tag_idx))) 682 #define __datatype_type_tag(kind, type) \ 683 __attribute__((__type_tag_for_datatype__(kind, type))) 684 #else 685 #define __arg_type_tag(arg_kind, arg_idx, type_tag_idx) 686 #define __datatype_type_tag(kind, type) 687 #endif 688 689 /* 690 * Lock annotations. 691 * 692 * Clang provides support for doing basic thread-safety tests at 693 * compile-time, by marking which locks will/should be held when 694 * entering/leaving a functions. 695 * 696 * Furthermore, it is also possible to annotate variables and structure 697 * members to enforce that they are only accessed when certain locks are 698 * held. 699 */ 700 701 #if __has_extension(c_thread_safety_attributes) 702 #define __lock_annotate(x) __attribute__((x)) 703 #else 704 #define __lock_annotate(x) 705 #endif 706 707 /* Structure implements a lock. */ 708 #define __lockable __lock_annotate(lockable) 709 710 /* Function acquires an exclusive or shared lock. */ 711 #define __locks_exclusive(...) \ 712 __lock_annotate(exclusive_lock_function(__VA_ARGS__)) 713 #define __locks_shared(...) \ 714 __lock_annotate(shared_lock_function(__VA_ARGS__)) 715 716 /* Function attempts to acquire an exclusive or shared lock. */ 717 #define __trylocks_exclusive(...) \ 718 __lock_annotate(exclusive_trylock_function(__VA_ARGS__)) 719 #define __trylocks_shared(...) \ 720 __lock_annotate(shared_trylock_function(__VA_ARGS__)) 721 722 /* Function releases a lock. */ 723 #define __unlocks(...) __lock_annotate(unlock_function(__VA_ARGS__)) 724 725 /* Function asserts that an exclusive or shared lock is held. */ 726 #define __asserts_exclusive(...) \ 727 __lock_annotate(assert_exclusive_lock(__VA_ARGS__)) 728 #define __asserts_shared(...) \ 729 __lock_annotate(assert_shared_lock(__VA_ARGS__)) 730 731 /* Function requires that an exclusive or shared lock is or is not held. */ 732 #define __requires_exclusive(...) \ 733 __lock_annotate(exclusive_locks_required(__VA_ARGS__)) 734 #define __requires_shared(...) \ 735 __lock_annotate(shared_locks_required(__VA_ARGS__)) 736 #define __requires_unlocked(...) \ 737 __lock_annotate(locks_excluded(__VA_ARGS__)) 738 739 /* Function should not be analyzed. */ 740 #define __no_lock_analysis __lock_annotate(no_thread_safety_analysis) 741 742 /* 743 * Function or variable should not be sanitized, e.g., by AddressSanitizer. 744 * GCC has the nosanitize attribute, but as a function attribute only, and 745 * warns on use as a variable attribute. 746 */ 747 #if __has_feature(address_sanitizer) && defined(__clang__) 748 #ifdef _KERNEL 749 #define __nosanitizeaddress __attribute__((no_sanitize("kernel-address"))) 750 #else 751 #define __nosanitizeaddress __attribute__((no_sanitize("address"))) 752 #endif 753 #else 754 #define __nosanitizeaddress 755 #endif 756 #if __has_feature(coverage_sanitizer) && defined(__clang__) 757 #define __nosanitizecoverage __attribute__((no_sanitize("coverage"))) 758 #else 759 #define __nosanitizecoverage 760 #endif 761 #if __has_feature(memory_sanitizer) && defined(__clang__) 762 #ifdef _KERNEL 763 #define __nosanitizememory __attribute__((no_sanitize("kernel-memory"))) 764 #else 765 #define __nosanitizememory __attribute__((no_sanitize("memory"))) 766 #endif 767 #else 768 #define __nosanitizememory 769 #endif 770 #if __has_feature(thread_sanitizer) && defined(__clang__) 771 #define __nosanitizethread __attribute__((no_sanitize("thread"))) 772 #else 773 #define __nosanitizethread 774 #endif 775 776 /* 777 * Make it possible to opt out of stack smashing protection. 778 */ 779 #if __has_attribute(no_stack_protector) 780 #define __nostackprotector __attribute__((no_stack_protector)) 781 #else 782 #define __nostackprotector \ 783 __attribute__((__optimize__("-fno-stack-protector"))) 784 #endif 785 786 /* Guard variables and structure members by lock. */ 787 #define __guarded_by(x) __lock_annotate(guarded_by(x)) 788 #define __pt_guarded_by(x) __lock_annotate(pt_guarded_by(x)) 789 790 /* Alignment builtins for better type checking and improved code generation. */ 791 /* Provide fallback versions for other compilers (GCC/Clang < 10): */ 792 #if !__has_builtin(__builtin_is_aligned) 793 #define __builtin_is_aligned(x, align) \ 794 (((__uintptr_t)(x) & ((align) - 1)) == 0) 795 #endif 796 #if !__has_builtin(__builtin_align_up) 797 #define __builtin_align_up(x, align) \ 798 ((__typeof__(x))(((__uintptr_t)(x)+((align)-1))&(~((align)-1)))) 799 #endif 800 #if !__has_builtin(__builtin_align_down) 801 #define __builtin_align_down(x, align) \ 802 ((__typeof__(x))((x)&(~((align)-1)))) 803 #endif 804 805 #define __align_up(x, y) __builtin_align_up(x, y) 806 #define __align_down(x, y) __builtin_align_down(x, y) 807 #define __is_aligned(x, y) __builtin_is_aligned(x, y) 808 809 #endif /* !_SYS_CDEFS_H_ */ 810