xref: /freebsd/sys/sys/cdefs.h (revision 5a2f6016dec69cbcfe9595c7f40a7e44d85a9288)
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 #define	__returns_twice	__attribute__((__returns_twice__))
257 
258 #define	__unreachable()	__builtin_unreachable()
259 
260 #if !defined(__STRICT_ANSI__) || __STDC_VERSION__ >= 199901
261 #define	__LONG_LONG_SUPPORTED
262 #endif
263 
264 /* C++11 exposes a load of C99 stuff */
265 #if defined(__cplusplus) && __cplusplus >= 201103L
266 #define	__LONG_LONG_SUPPORTED
267 #ifndef	__STDC_LIMIT_MACROS
268 #define	__STDC_LIMIT_MACROS
269 #endif
270 #ifndef	__STDC_CONSTANT_MACROS
271 #define	__STDC_CONSTANT_MACROS
272 #endif
273 #endif
274 
275 /*
276  * noexcept keyword added in C++11.
277  */
278 #if defined(__cplusplus) && __cplusplus >= 201103L
279 #define __noexcept noexcept
280 #define __noexcept_if(__c) noexcept(__c)
281 #else
282 #define __noexcept
283 #define __noexcept_if(__c)
284 #endif
285 
286 /*
287  * nodiscard attribute added in C++17 and C23, but supported by both LLVM and
288  * GCC in earlier language versions, so we use __has_c{,pp}_attribute to test
289  * for it.
290  *
291  * __nodiscard may be used on a function:
292  * 	__nodiscard int f();
293  *
294  * or on a struct, union or enum:
295  * 	struct __nodiscard S{};
296  * 	struct S f();
297  *
298  * or in C++, on an object constructor.
299  */
300 
301 #if defined(__cplusplus) && defined(__has_cpp_attribute)
302 #if __has_cpp_attribute(nodiscard)
303 #define	__nodiscard	[[nodiscard]]
304 #endif
305 #elif defined(__STDC_VERSION__) && defined(__has_c_attribute)
306 #if __has_c_attribute(__nodiscard__)
307 #define	__nodiscard	[[__nodiscard__]]
308 #endif
309 #endif
310 
311 #ifndef __nodiscard
312 /*
313  * LLVM 16 and earlier don't support [[nodiscard]] in C, but they do support
314  * __warn_unused_result__ with the same semantics, so fall back to that.
315  * We can't do this for GCC because the semantics are different.
316  */
317 #ifdef __clang__
318 #define	__nodiscard	__attribute__((__warn_unused_result__))
319 #else
320 #define	__nodiscard
321 #endif
322 #endif
323 
324 /*
325  * We use `__restrict' as a way to define the `restrict' type qualifier
326  * without disturbing older software that is unaware of C99 keywords.
327  * GCC also provides `__restrict' as an extension to support C99-style
328  * restricted pointers in other language modes.
329  */
330 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901
331 #define	__restrict	restrict
332 #endif
333 
334 /*
335  * All modern compilers have explicit branch prediction so that the CPU back-end
336  * can hint to the processor and also so that code blocks can be reordered such
337  * that the predicted path sees a more linear flow, thus improving cache
338  * behavior, etc. Use sparingly, except in performance critical code where
339  * they make things measurably faster.
340  */
341 #define	__predict_true(exp)     __builtin_expect((exp), 1)
342 #define	__predict_false(exp)    __builtin_expect((exp), 0)
343 
344 #define	__null_sentinel	__attribute__((__sentinel__))
345 #define	__exported	__attribute__((__visibility__("default")))
346 #define	__hidden	__attribute__((__visibility__("hidden")))
347 
348 /*
349  * We define this here since <stddef.h>, <sys/queue.h>, and <sys/types.h>
350  * require it.
351  */
352 #define	__offsetof(type, field)	 __builtin_offsetof(type, field)
353 #define	__rangeof(type, start, end) \
354 	(__offsetof(type, end) - __offsetof(type, start))
355 
356 /*
357  * Given the pointer x to the member m of the struct s, return
358  * a pointer to the containing structure.  When using GCC, we first
359  * assign pointer x to a local variable, to check that its type is
360  * compatible with member m.
361  */
362 #define	__containerof(x, s, m) ({					\
363 	const volatile __typeof(((s *)0)->m) *__x = (x);		\
364 	__DEQUALIFY(s *, (const volatile char *)__x - __offsetof(s, m));\
365 })
366 
367 /*
368  * Compiler-dependent macros to declare that functions take printf-like
369  * or scanf-like arguments.
370  */
371 #define	__printflike(fmtarg, firstvararg) \
372 	    __attribute__((__format__ (__printf__, fmtarg, firstvararg)))
373 #define	__scanflike(fmtarg, firstvararg) \
374 	    __attribute__((__format__ (__scanf__, fmtarg, firstvararg)))
375 #define	__format_arg(fmtarg)	__attribute__((__format_arg__ (fmtarg)))
376 #define	__strfmonlike(fmtarg, firstvararg) \
377 	    __attribute__((__format__ (__strfmon__, fmtarg, firstvararg)))
378 #define	__strftimelike(fmtarg, firstvararg) \
379 	    __attribute__((__format__ (__strftime__, fmtarg, firstvararg)))
380 
381 /*
382  * Like __printflike, but allows fmtarg to be NULL. FreeBSD invented 'printf0'
383  * for this because older versions of gcc issued warnings for NULL first args.
384  * Clang has always had printf and printf0 as aliases. gcc 11.0 now follows
385  * clang. So now this is an alias for __printflike, or nothing. In the future
386  * _Nullable or _Nonnull will replace this.
387  * XXX Except that doesn't work, so for now revert to printf0 for clang and
388  * the FreeBSD gcc until I can work this out.
389  */
390 #if defined(__clang__) || (defined(__GNUC__) && defined (__FreeBSD_cc_version))
391 #define	__printf0like(fmtarg, firstvararg) \
392 	    __attribute__((__format__ (__printf0__, fmtarg, firstvararg)))
393 #else
394 #define	__printf0like(fmtarg, firstvararg)
395 #endif
396 
397 #define	__strong_reference(sym,aliassym)	\
398 	extern __typeof (sym) aliassym __attribute__ ((__alias__ (#sym)))
399 #ifdef __STDC__
400 #define	__weak_reference(sym,alias)	\
401 	__asm__(".weak " #alias);	\
402 	__asm__(".equ "  #alias ", " #sym)
403 #define	__warn_references(sym,msg)	\
404 	__asm__(".section .gnu.warning." #sym);	\
405 	__asm__(".asciz \"" msg "\"");	\
406 	__asm__(".previous")
407 #ifdef	__CC_SUPPORTS_SYMVER
408 #define	__sym_compat(sym,impl,verid)	\
409 	__asm__(".symver " #impl ", " #sym "@" #verid)
410 #define	__sym_default(sym,impl,verid)	\
411 	__asm__(".symver " #impl ", " #sym "@@@" #verid)
412 #endif
413 #else
414 #define	__weak_reference(sym,alias)	\
415 	__asm__(".weak alias");		\
416 	__asm__(".equ alias, sym")
417 #define	__warn_references(sym,msg)	\
418 	__asm__(".section .gnu.warning.sym"); \
419 	__asm__(".asciz \"msg\"");	\
420 	__asm__(".previous")
421 #ifdef	__CC_SUPPORTS_SYMVER
422 #define	__sym_compat(sym,impl,verid)	\
423 	__asm__(".symver impl, sym@verid")
424 #define	__sym_default(impl,sym,verid)	\
425 	__asm__(".symver impl, sym@@@verid")
426 #endif
427 #endif	/* __STDC__ */
428 
429 #define	__GLOBL(sym)	__asm__(".globl " __XSTRING(sym))
430 #define	__WEAK(sym)	__asm__(".weak " __XSTRING(sym))
431 
432 #define	__IDSTRING(name,string)	__asm__(".ident\t\"" string "\"")
433 
434 /*
435  * Embed the rcs id of a source file in the resulting library.  Note that in
436  * more recent ELF binutils, we use .ident allowing the ID to be stripped.
437  * Usage:
438  */
439 #ifndef	__FBSDID
440 #if !defined(STRIP_FBSDID)
441 #define	__FBSDID(s)	__IDSTRING(__CONCAT(__rcsid_,__LINE__),s)
442 #else
443 #define	__FBSDID(s)	struct __hack
444 #endif
445 #endif
446 
447 #ifndef	__RCSID
448 #ifndef	NO__RCSID
449 #define	__RCSID(s)	__IDSTRING(__CONCAT(__rcsid_,__LINE__),s)
450 #else
451 #define	__RCSID(s)	struct __hack
452 #endif
453 #endif
454 
455 #ifndef	__RCSID_SOURCE
456 #ifndef	NO__RCSID_SOURCE
457 #define	__RCSID_SOURCE(s)	__IDSTRING(__CONCAT(__rcsid_source_,__LINE__),s)
458 #else
459 #define	__RCSID_SOURCE(s)	struct __hack
460 #endif
461 #endif
462 
463 #ifndef	__SCCSID
464 #ifndef	NO__SCCSID
465 #define	__SCCSID(s)	__IDSTRING(__CONCAT(__sccsid_,__LINE__),s)
466 #else
467 #define	__SCCSID(s)	struct __hack
468 #endif
469 #endif
470 
471 #ifndef	__COPYRIGHT
472 #ifndef	NO__COPYRIGHT
473 #define	__COPYRIGHT(s)	__IDSTRING(__CONCAT(__copyright_,__LINE__),s)
474 #else
475 #define	__COPYRIGHT(s)	struct __hack
476 #endif
477 #endif
478 
479 #ifndef	__DECONST
480 #define	__DECONST(type, var)	((type)(__uintptr_t)(const void *)(var))
481 #endif
482 
483 #ifndef	__DEVOLATILE
484 #define	__DEVOLATILE(type, var)	((type)(__uintptr_t)(volatile void *)(var))
485 #endif
486 
487 #ifndef	__DEQUALIFY
488 #define	__DEQUALIFY(type, var)	((type)(__uintptr_t)(const volatile void *)(var))
489 #endif
490 
491 #if !defined(_STANDALONE) && !defined(_KERNEL)
492 #define	__RENAME(x)	__asm(__STRING(x))
493 #else /* _STANDALONE || _KERNEL */
494 #define	__RENAME(x)	no renaming in kernel/standalone environment
495 #endif
496 
497 #include <sys/_visible.h>
498 
499 /*
500  * Nullability qualifiers: currently only supported by Clang.
501  */
502 #if !(defined(__clang__) && __has_feature(nullability))
503 #define	_Nonnull
504 #define	_Nullable
505 #define	_Null_unspecified
506 #define	__NULLABILITY_PRAGMA_PUSH
507 #define	__NULLABILITY_PRAGMA_POP
508 #else
509 #define	__NULLABILITY_PRAGMA_PUSH _Pragma("clang diagnostic push")	\
510 	_Pragma("clang diagnostic ignored \"-Wnullability-completeness\"")
511 #define	__NULLABILITY_PRAGMA_POP _Pragma("clang diagnostic pop")
512 #endif
513 
514 /*
515  * Type Safety Checking
516  *
517  * Clang provides additional attributes to enable checking type safety
518  * properties that cannot be enforced by the C type system.
519  */
520 
521 #if __has_attribute(__argument_with_type_tag__) && \
522     __has_attribute(__type_tag_for_datatype__)
523 #define	__arg_type_tag(arg_kind, arg_idx, type_tag_idx) \
524 	    __attribute__((__argument_with_type_tag__(arg_kind, arg_idx, type_tag_idx)))
525 #define	__datatype_type_tag(kind, type) \
526 	    __attribute__((__type_tag_for_datatype__(kind, type)))
527 #else
528 #define	__arg_type_tag(arg_kind, arg_idx, type_tag_idx)
529 #define	__datatype_type_tag(kind, type)
530 #endif
531 
532 /*
533  * Lock annotations.
534  *
535  * Clang provides support for doing basic thread-safety tests at
536  * compile-time, by marking which locks will/should be held when
537  * entering/leaving a functions.
538  *
539  * Furthermore, it is also possible to annotate variables and structure
540  * members to enforce that they are only accessed when certain locks are
541  * held.
542  */
543 
544 #if __has_extension(c_thread_safety_attributes)
545 #define	__lock_annotate(x)	__attribute__((x))
546 #else
547 #define	__lock_annotate(x)
548 #endif
549 
550 /* Structure implements a lock. */
551 #define	__lockable		__lock_annotate(lockable)
552 
553 /* Function acquires an exclusive or shared lock. */
554 #define	__locks_exclusive(...) \
555 	__lock_annotate(exclusive_lock_function(__VA_ARGS__))
556 #define	__locks_shared(...) \
557 	__lock_annotate(shared_lock_function(__VA_ARGS__))
558 
559 /* Function attempts to acquire an exclusive or shared lock. */
560 #define	__trylocks_exclusive(...) \
561 	__lock_annotate(exclusive_trylock_function(__VA_ARGS__))
562 #define	__trylocks_shared(...) \
563 	__lock_annotate(shared_trylock_function(__VA_ARGS__))
564 
565 /* Function releases a lock. */
566 #define	__unlocks(...)		__lock_annotate(unlock_function(__VA_ARGS__))
567 
568 /* Function asserts that an exclusive or shared lock is held. */
569 #define	__asserts_exclusive(...) \
570 	__lock_annotate(assert_exclusive_lock(__VA_ARGS__))
571 #define	__asserts_shared(...) \
572 	__lock_annotate(assert_shared_lock(__VA_ARGS__))
573 
574 /* Function requires that an exclusive or shared lock is or is not held. */
575 #define	__requires_exclusive(...) \
576 	__lock_annotate(exclusive_locks_required(__VA_ARGS__))
577 #define	__requires_shared(...) \
578 	__lock_annotate(shared_locks_required(__VA_ARGS__))
579 #define	__requires_unlocked(...) \
580 	__lock_annotate(locks_excluded(__VA_ARGS__))
581 
582 /* Function should not be analyzed. */
583 #define	__no_lock_analysis	__lock_annotate(no_thread_safety_analysis)
584 
585 /*
586  * Function or variable should not be sanitized, e.g., by AddressSanitizer.
587  * GCC has the nosanitize attribute, but as a function attribute only, and
588  * warns on use as a variable attribute.
589  */
590 #if __has_feature(address_sanitizer) && defined(__clang__)
591 #ifdef _KERNEL
592 #define	__nosanitizeaddress	__attribute__((no_sanitize("kernel-address")))
593 #else
594 #define	__nosanitizeaddress	__attribute__((no_sanitize("address")))
595 #endif
596 #else
597 #define	__nosanitizeaddress
598 #endif
599 #if __has_feature(coverage_sanitizer) && defined(__clang__)
600 #define	__nosanitizecoverage	__attribute__((no_sanitize("coverage")))
601 #else
602 #define	__nosanitizecoverage
603 #endif
604 #if __has_feature(memory_sanitizer) && defined(__clang__)
605 #ifdef _KERNEL
606 #define	__nosanitizememory	__attribute__((no_sanitize("kernel-memory")))
607 #else
608 #define	__nosanitizememory	__attribute__((no_sanitize("memory")))
609 #endif
610 #else
611 #define	__nosanitizememory
612 #endif
613 #if __has_feature(thread_sanitizer) && defined(__clang__)
614 #define	__nosanitizethread	__attribute__((no_sanitize("thread")))
615 #else
616 #define	__nosanitizethread
617 #endif
618 
619 /*
620  * Make it possible to opt out of stack smashing protection.
621  */
622 #if __has_attribute(no_stack_protector)
623 #define	__nostackprotector	__attribute__((no_stack_protector))
624 #else
625 #define	__nostackprotector	\
626 	__attribute__((__optimize__("-fno-stack-protector")))
627 #endif
628 
629 /* Guard variables and structure members by lock. */
630 #define	__guarded_by(x)		__lock_annotate(guarded_by(x))
631 #define	__pt_guarded_by(x)	__lock_annotate(pt_guarded_by(x))
632 
633 /* Alignment builtins for better type checking and improved code generation. */
634 /* Provide fallback versions for other compilers (GCC/Clang < 10): */
635 #if !__has_builtin(__builtin_is_aligned)
636 #define __builtin_is_aligned(x, align)	\
637 	(((__uintptr_t)(x) & ((align) - 1)) == 0)
638 #endif
639 #if !__has_builtin(__builtin_align_up)
640 #define __builtin_align_up(x, align)	\
641 	((__typeof__(x))(((__uintptr_t)(x)+((align)-1))&(~((align)-1))))
642 #endif
643 #if !__has_builtin(__builtin_align_down)
644 #define __builtin_align_down(x, align)	\
645 	((__typeof__(x))((x)&(~((align)-1))))
646 #endif
647 
648 #define __align_up(x, y) __builtin_align_up(x, y)
649 #define __align_down(x, y) __builtin_align_down(x, y)
650 #define __is_aligned(x, y) __builtin_is_aligned(x, y)
651 
652 #endif /* !_SYS_CDEFS_H_ */
653