xref: /freebsd/sys/sys/cdefs.h (revision d08296c7ab0d7bb259bf7b8cdf9ffb819c1929ab)
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 #include <sys/_decls.h>
65 
66 /*
67  * This code has been put in place to help reduce the addition of
68  * compiler specific defines in FreeBSD code.  It helps to aid in
69  * having a compiler-agnostic source tree.
70  */
71 
72 /*
73  * Macro to test if we're using a specific version of gcc or later.
74  */
75 #if defined(__GNUC__)
76 #define	__GNUC_PREREQ__(ma, mi)	\
77 	(__GNUC__ > (ma) || __GNUC__ == (ma) && __GNUC_MINOR__ >= (mi))
78 #else
79 #define	__GNUC_PREREQ__(ma, mi)	0
80 #endif
81 
82 #if defined(__GNUC__)
83 
84 /*
85  * Compiler memory barriers, specific to gcc and clang.
86  */
87 #define	__compiler_membar()	__asm __volatile(" " : : : "memory")
88 
89 #define	__CC_SUPPORTS___INLINE 1
90 #define	__CC_SUPPORTS_SYMVER 1
91 
92 #endif /* __GNUC__ */
93 
94 /*
95  * TinyC pretends to be gcc 9.3. This is generally good enough to support
96  * everything FreeBSD... except for the .symver assembler directive.
97  */
98 #ifdef __TINYC__
99 #undef	__CC_SUPPORTS_SYMVER
100 #endif
101 
102 /*
103  * The __CONCAT macro is used to concatenate parts of symbol names, e.g.
104  * with "#define OLD(foo) __CONCAT(old,foo)", OLD(foo) produces oldfoo.
105  * The __CONCAT macro is a bit tricky to use if it must work in non-ANSI
106  * mode -- there must be no spaces between its arguments, and for nested
107  * __CONCAT's, all the __CONCAT's must be at the left.  __CONCAT can also
108  * concatenate double-quoted strings produced by the __STRING macro, but
109  * this only works with ANSI C.
110  *
111  * __XSTRING is like __STRING, but it expands any macros in its argument
112  * first.  It is only available with ANSI C.
113  */
114 #if defined(__STDC__) || defined(__cplusplus)
115 #define	__P(protos)	protos		/* full-blown ANSI C */
116 #define	__CONCAT1(x,y)	x ## y
117 #define	__CONCAT(x,y)	__CONCAT1(x,y)
118 #define	__STRING(x)	#x		/* stringify without expanding x */
119 #define	__XSTRING(x)	__STRING(x)	/* expand x, then stringify */
120 
121 #define	__volatile	volatile
122 #if defined(__cplusplus)
123 #define	__inline	inline		/* convert to C++ keyword */
124 #else
125 #if !(defined(__CC_SUPPORTS___INLINE))
126 #define	__inline			/* delete GCC keyword */
127 #endif /* ! __CC_SUPPORTS___INLINE */
128 #endif /* !__cplusplus */
129 
130 #else	/* !(__STDC__ || __cplusplus) */
131 #define	__P(protos)	()		/* traditional C preprocessor */
132 #define	__CONCAT(x,y)	x/**/y
133 #define	__STRING(x)	"x"
134 #if !defined(__CC_SUPPORTS___INLINE)
135 /* Just delete these in a K&R environment */
136 #define	__inline
137 #define	__volatile
138 #endif	/* !__CC_SUPPORTS___INLINE */
139 #endif	/* !(__STDC__ || __cplusplus) */
140 
141 /*
142  * Compiler-dependent macros to help declare dead (non-returning) and pure (no
143  * side effects) functions, and unused variables. These attributes are supported
144  * by all current compilers, even pcc.
145  */
146 #define	__weak_symbol	__attribute__((__weak__))
147 #define	__dead2		__attribute__((__noreturn__))
148 #define	__pure2		__attribute__((__const__))
149 #define	__maybe_unused	__attribute__((__unused__))
150 #define	__unused	__attribute__((__unused__))
151 #define	__used		__attribute__((__used__))
152 #define __deprecated	__attribute__((__deprecated__))
153 #define __deprecated1(msg)	__attribute__((__deprecated__(msg)))
154 #define	__packed	__attribute__((__packed__))
155 #define	__aligned(x)	__attribute__((__aligned__(x)))
156 #define	__section(x)	__attribute__((__section__(x)))
157 #define	__writeonly	__unused
158 #define	__alloc_size(x)	__attribute__((__alloc_size__(x)))
159 #define	__alloc_size2(n, x)	__attribute__((__alloc_size__(n, x)))
160 #define	__alloc_align(x)	__attribute__((__alloc_align__(x)))
161 
162 /*
163  * Keywords added in C11.
164  */
165 
166 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L
167 
168 #if !__has_extension(c_alignas)
169 #if (defined(__cplusplus) && __cplusplus >= 201103L) || \
170     __has_extension(cxx_alignas)
171 #define	_Alignas(x)		alignas(x)
172 #else
173 /* XXX: Only emulates _Alignas(constant-expression); not _Alignas(type-name). */
174 #define	_Alignas(x)		__aligned(x)
175 #endif
176 #endif
177 
178 #if defined(__cplusplus) && __cplusplus >= 201103L
179 #define	_Alignof(x)		alignof(x)
180 #else
181 #define	_Alignof(x)		__alignof(x)
182 #endif
183 
184 #if defined(__cplusplus) && __cplusplus >= 201103L
185 #define	_Noreturn		[[noreturn]]
186 #else
187 #define	_Noreturn		__dead2
188 #endif
189 
190 #if !__has_extension(c_static_assert)
191 #if (defined(__cplusplus) && __cplusplus >= 201103L) || \
192     __has_extension(cxx_static_assert)
193 #define	_Static_assert(x, y)	static_assert(x, y)
194 #endif
195 #endif
196 
197 #if !__has_extension(c_thread_local)
198 #if (defined(__cplusplus) && __cplusplus >= 201103L) || \
199     __has_extension(cxx_thread_local)
200 #define	_Thread_local		thread_local
201 #else
202 #define	_Thread_local		__thread
203 #endif
204 #endif
205 
206 #endif /* __STDC_VERSION__ || __STDC_VERSION__ < 201112L */
207 
208 /*
209  * Emulation of C11 _Generic().  Unlike the previously defined C11
210  * keywords, it is not possible to implement this using exactly the same
211  * syntax.  Therefore implement something similar under the name
212  * __generic().  Unlike _Generic(), this macro can only distinguish
213  * between a single type, so it requires nested invocations to
214  * distinguish multiple cases.
215  *
216  * Note that the comma operator is used to force expr to decay in
217  * order to match _Generic().
218  */
219 
220 #if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) || \
221     __has_extension(c_generic_selections)
222 #define	__generic(expr, t, yes, no)					\
223 	_Generic(expr, t: yes, default: no)
224 #elif !defined(__cplusplus)
225 #define	__generic(expr, t, yes, no)					\
226 	__builtin_choose_expr(__builtin_types_compatible_p(		\
227 	    __typeof(((void)0, (expr))), t), yes, no)
228 #endif
229 
230 /*
231  * __qualsel() is the building block for C23 qualifier-preserving macros
232  * as proposed in N3020: it selects cexpr when the pointer expression p
233  * references a const-qualified object, and expr otherwise.
234  *
235  * The conditional operator's composite-type rule collapses every
236  * pointer-to-const-object type onto the single "const void *"" association,
237  * so callers passing any such pointer are matched even though _Generic()
238  * otherwise compares types exactly.
239  *
240  * The (__uintptr_t) round-trip only suppresses -Wcast-qual on the throwaway
241  * second operand.
242  */
243 #if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) || \
244     __has_extension(c_generic_selections)
245 #define	__qualsel(p, cexpr, expr)					\
246 	_Generic(1 ? (p) : (void *)(__uintptr_t)(p),			\
247 	    const void *: (cexpr),					\
248 	    default: (expr))
249 #endif
250 
251 /*
252  * C99 Static array indices in function parameter declarations.  Syntax such as:
253  * void bar(int myArray[static 10]);
254  * is allowed in C99 but not in C++.  Define __min_size appropriately so
255  * headers using it can be compiled in either language.  Use like this:
256  * void bar(int myArray[__min_size(10)]);
257  */
258 #if !defined(__cplusplus) && \
259     (!defined(__STDC_VERSION__) || (__STDC_VERSION__ >= 199901))
260 #define __min_size(x)	static (x)
261 #else
262 #define __min_size(x)	(x)
263 #endif
264 
265 #define	__malloc_like	__attribute__((__malloc__))
266 #define	__pure		__attribute__((__pure__))
267 
268 #define	__always_inline	__inline __attribute__((__always_inline__))
269 #define	__noinline	__attribute__ ((__noinline__))
270 #define	__fastcall	__attribute__((__fastcall__))
271 #define	__result_use_check	__attribute__((__warn_unused_result__))
272 #define	__returns_twice	__attribute__((__returns_twice__))
273 
274 #define	__unreachable()	__builtin_unreachable()
275 
276 #if !defined(__STRICT_ANSI__) || __STDC_VERSION__ >= 199901
277 #define	__LONG_LONG_SUPPORTED
278 #endif
279 
280 /* C++11 exposes a load of C99 stuff */
281 #if defined(__cplusplus) && __cplusplus >= 201103L
282 #define	__LONG_LONG_SUPPORTED
283 #ifndef	__STDC_LIMIT_MACROS
284 #define	__STDC_LIMIT_MACROS
285 #endif
286 #ifndef	__STDC_CONSTANT_MACROS
287 #define	__STDC_CONSTANT_MACROS
288 #endif
289 #endif
290 
291 /*
292  * noexcept keyword added in C++11.
293  */
294 #if defined(__cplusplus) && __cplusplus >= 201103L
295 #define __noexcept noexcept
296 #define __noexcept_if(__c) noexcept(__c)
297 #else
298 #define __noexcept
299 #define __noexcept_if(__c)
300 #endif
301 
302 /*
303  * nodiscard attribute added in C++17 and C23, but supported by both LLVM and
304  * GCC in earlier language versions, so we use __has_c{,pp}_attribute to test
305  * for it.
306  *
307  * __nodiscard may be used on a function:
308  * 	__nodiscard int f();
309  *
310  * or on a struct, union or enum:
311  * 	struct __nodiscard S{};
312  * 	struct S f();
313  *
314  * or in C++, on an object constructor.
315  */
316 
317 #if defined(__cplusplus) && defined(__has_cpp_attribute)
318 #if __has_cpp_attribute(nodiscard)
319 #define	__nodiscard	[[nodiscard]]
320 #endif
321 #elif defined(__STDC_VERSION__) && defined(__has_c_attribute)
322 #if __has_c_attribute(__nodiscard__)
323 #define	__nodiscard	[[__nodiscard__]]
324 #endif
325 #endif
326 
327 #ifndef __nodiscard
328 /*
329  * LLVM 16 and earlier don't support [[nodiscard]] in C, but they do support
330  * __warn_unused_result__ with the same semantics, so fall back to that.
331  * We can't do this for GCC because the semantics are different.
332  */
333 #ifdef __clang__
334 #define	__nodiscard	__attribute__((__warn_unused_result__))
335 #else
336 #define	__nodiscard
337 #endif
338 #endif
339 
340 /*
341  * We use `__restrict' as a way to define the `restrict' type qualifier
342  * without disturbing older software that is unaware of C99 keywords.
343  * GCC also provides `__restrict' as an extension to support C99-style
344  * restricted pointers in other language modes.
345  */
346 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901
347 #define	__restrict	restrict
348 #endif
349 
350 /*
351  * All modern compilers have explicit branch prediction so that the CPU back-end
352  * can hint to the processor and also so that code blocks can be reordered such
353  * that the predicted path sees a more linear flow, thus improving cache
354  * behavior, etc. Use sparingly, except in performance critical code where
355  * they make things measurably faster.
356  */
357 #define	__predict_true(exp)     __builtin_expect((exp), 1)
358 #define	__predict_false(exp)    __builtin_expect((exp), 0)
359 
360 #define	__null_sentinel	__attribute__((__sentinel__))
361 #define	__exported	__attribute__((__visibility__("default")))
362 #define	__hidden	__attribute__((__visibility__("hidden")))
363 
364 /*
365  * We define this here since <stddef.h>, <sys/queue.h>, and <sys/types.h>
366  * require it.
367  */
368 #define	__offsetof(type, field)	 __builtin_offsetof(type, field)
369 #define	__rangeof(type, start, end) \
370 	(__offsetof(type, end) - __offsetof(type, start))
371 
372 /*
373  * Given the pointer x to the member m of the struct s, return
374  * a pointer to the containing structure.  When using GCC, we first
375  * assign pointer x to a local variable, to check that its type is
376  * compatible with member m.
377  */
378 #define	__containerof(x, s, m) ({					\
379 	const volatile __typeof(((s *)0)->m) *__x = (x);		\
380 	__DEQUALIFY(s *, (const volatile char *)__x - __offsetof(s, m));\
381 })
382 
383 /*
384  * Compiler-dependent macros to declare that functions take printf-like
385  * or scanf-like arguments.
386  */
387 #define	__printflike(fmtarg, firstvararg) \
388 	    __attribute__((__format__ (__printf__, fmtarg, firstvararg)))
389 #define	__scanflike(fmtarg, firstvararg) \
390 	    __attribute__((__format__ (__scanf__, fmtarg, firstvararg)))
391 #define	__format_arg(fmtarg)	__attribute__((__format_arg__ (fmtarg)))
392 #define	__strfmonlike(fmtarg, firstvararg) \
393 	    __attribute__((__format__ (__strfmon__, fmtarg, firstvararg)))
394 #define	__strftimelike(fmtarg, firstvararg) \
395 	    __attribute__((__format__ (__strftime__, fmtarg, firstvararg)))
396 
397 /*
398  * Like __printflike, but allows fmtarg to be NULL. FreeBSD invented 'printf0'
399  * for this because older versions of gcc issued warnings for NULL first args.
400  * Clang has always had printf and printf0 as aliases. gcc 11.0 now follows
401  * clang. So now this is an alias for __printflike, or nothing. In the future
402  * _Nullable or _Nonnull will replace this.
403  * XXX Except that doesn't work, so for now revert to printf0 for clang and
404  * the FreeBSD gcc until I can work this out.
405  */
406 #if defined(__clang__) || (defined(__GNUC__) && defined (__FreeBSD_cc_version))
407 #define	__printf0like(fmtarg, firstvararg) \
408 	    __attribute__((__format__ (__printf0__, fmtarg, firstvararg)))
409 #else
410 #define	__printf0like(fmtarg, firstvararg)
411 #endif
412 
413 /* To silence warnings about null terminator not fitting into an array. */
414 #if __has_attribute(__nonstring__)
415 #define	__nonstring	__attribute__((__nonstring__))
416 #else
417 #define	__nonstring
418 #endif
419 
420 #define	__strong_reference(sym,aliassym)	\
421 	extern __typeof (sym) aliassym __attribute__ ((__alias__ (#sym)))
422 #ifdef __STDC__
423 #define	__weak_reference(sym,alias)	\
424 	__asm__(".weak " #alias);	\
425 	__asm__(".equ "  #alias ", " #sym)
426 #define	__warn_references(sym,msg)	\
427 	__asm__(".section .gnu.warning." #sym);	\
428 	__asm__(".asciz \"" msg "\"");	\
429 	__asm__(".previous")
430 #ifdef	__CC_SUPPORTS_SYMVER
431 #define	__sym_compat(sym,impl,verid)	\
432 	__asm__(".symver " #impl ", " #sym "@" #verid)
433 #define	__sym_default(sym,impl,verid)	\
434 	__asm__(".symver " #impl ", " #sym "@@@" #verid)
435 #endif
436 #else
437 #define	__weak_reference(sym,alias)	\
438 	__asm__(".weak alias");		\
439 	__asm__(".equ alias, sym")
440 #define	__warn_references(sym,msg)	\
441 	__asm__(".section .gnu.warning.sym"); \
442 	__asm__(".asciz \"msg\"");	\
443 	__asm__(".previous")
444 #ifdef	__CC_SUPPORTS_SYMVER
445 #define	__sym_compat(sym,impl,verid)	\
446 	__asm__(".symver impl, sym@verid")
447 #define	__sym_default(impl,sym,verid)	\
448 	__asm__(".symver impl, sym@@@verid")
449 #endif
450 #endif	/* __STDC__ */
451 
452 #define	__GLOBL(sym)	__asm__(".globl " __XSTRING(sym))
453 #define	__WEAK(sym)	__asm__(".weak " __XSTRING(sym))
454 
455 #define	__IDSTRING(name,string)	__asm__(".ident\t\"" string "\"")
456 
457 /*
458  * Embed the rcs id of a source file in the resulting library.  Note that in
459  * more recent ELF binutils, we use .ident allowing the ID to be stripped.
460  * Usage:
461  */
462 #ifndef	__FBSDID
463 #if !defined(STRIP_FBSDID)
464 #define	__FBSDID(s)	__IDSTRING(__CONCAT(__rcsid_,__LINE__),s)
465 #else
466 #define	__FBSDID(s)	struct __hack
467 #endif
468 #endif
469 
470 #ifndef	__RCSID
471 #ifndef	NO__RCSID
472 #define	__RCSID(s)	__IDSTRING(__CONCAT(__rcsid_,__LINE__),s)
473 #else
474 #define	__RCSID(s)	struct __hack
475 #endif
476 #endif
477 
478 #ifndef	__RCSID_SOURCE
479 #ifndef	NO__RCSID_SOURCE
480 #define	__RCSID_SOURCE(s)	__IDSTRING(__CONCAT(__rcsid_source_,__LINE__),s)
481 #else
482 #define	__RCSID_SOURCE(s)	struct __hack
483 #endif
484 #endif
485 
486 #ifndef	__SCCSID
487 #ifndef	NO__SCCSID
488 #define	__SCCSID(s)	__IDSTRING(__CONCAT(__sccsid_,__LINE__),s)
489 #else
490 #define	__SCCSID(s)	struct __hack
491 #endif
492 #endif
493 
494 #ifndef	__COPYRIGHT
495 #ifndef	NO__COPYRIGHT
496 #define	__COPYRIGHT(s)	__IDSTRING(__CONCAT(__copyright_,__LINE__),s)
497 #else
498 #define	__COPYRIGHT(s)	struct __hack
499 #endif
500 #endif
501 
502 #ifndef	__DECONST
503 #define	__DECONST(type, var)	((type)(__uintptr_t)(const void *)(var))
504 #endif
505 
506 #ifndef	__DEVOLATILE
507 #define	__DEVOLATILE(type, var)	((type)(__uintptr_t)(volatile void *)(var))
508 #endif
509 
510 #ifndef	__DEQUALIFY
511 #define	__DEQUALIFY(type, var)	((type)(__uintptr_t)(const volatile void *)(var))
512 #endif
513 
514 #if !defined(_STANDALONE) && !defined(_KERNEL)
515 #define	__RENAME(x)	__asm(__STRING(x))
516 #else /* _STANDALONE || _KERNEL */
517 #define	__RENAME(x)	no renaming in kernel/standalone environment
518 #endif
519 
520 #include <sys/_visible.h>
521 
522 /*
523  * Nullability qualifiers: currently only supported by Clang.
524  */
525 #if !(defined(__clang__) && __has_feature(nullability))
526 #define	_Nonnull
527 #define	_Nullable
528 #define	_Null_unspecified
529 #define	__NULLABILITY_PRAGMA_PUSH
530 #define	__NULLABILITY_PRAGMA_POP
531 #else
532 #define	__NULLABILITY_PRAGMA_PUSH _Pragma("clang diagnostic push")	\
533 	_Pragma("clang diagnostic ignored \"-Wnullability-completeness\"")
534 #define	__NULLABILITY_PRAGMA_POP _Pragma("clang diagnostic pop")
535 #endif
536 
537 /*
538  * Type Safety Checking
539  *
540  * Clang provides additional attributes to enable checking type safety
541  * properties that cannot be enforced by the C type system.
542  */
543 
544 #if __has_attribute(__argument_with_type_tag__) && \
545     __has_attribute(__type_tag_for_datatype__)
546 #define	__arg_type_tag(arg_kind, arg_idx, type_tag_idx) \
547 	    __attribute__((__argument_with_type_tag__(arg_kind, arg_idx, type_tag_idx)))
548 #define	__datatype_type_tag(kind, type) \
549 	    __attribute__((__type_tag_for_datatype__(kind, type)))
550 #else
551 #define	__arg_type_tag(arg_kind, arg_idx, type_tag_idx)
552 #define	__datatype_type_tag(kind, type)
553 #endif
554 
555 /*
556  * Lock annotations.
557  *
558  * Clang provides support for doing basic thread-safety tests at
559  * compile-time, by marking which locks will/should be held when
560  * entering/leaving a functions.
561  *
562  * Furthermore, it is also possible to annotate variables and structure
563  * members to enforce that they are only accessed when certain locks are
564  * held.
565  */
566 
567 #if __has_extension(c_thread_safety_attributes)
568 #define	__lock_annotate(x)	__attribute__((x))
569 #else
570 #define	__lock_annotate(x)
571 #endif
572 
573 /* Structure implements a lock. */
574 #define	__lockable		__lock_annotate(lockable)
575 
576 /* Function acquires an exclusive or shared lock. */
577 #define	__locks_exclusive(...) \
578 	__lock_annotate(exclusive_lock_function(__VA_ARGS__))
579 #define	__locks_shared(...) \
580 	__lock_annotate(shared_lock_function(__VA_ARGS__))
581 
582 /* Function attempts to acquire an exclusive or shared lock. */
583 #define	__trylocks_exclusive(...) \
584 	__lock_annotate(exclusive_trylock_function(__VA_ARGS__))
585 #define	__trylocks_shared(...) \
586 	__lock_annotate(shared_trylock_function(__VA_ARGS__))
587 
588 /* Function releases a lock. */
589 #define	__unlocks(...)		__lock_annotate(unlock_function(__VA_ARGS__))
590 
591 /* Function asserts that an exclusive or shared lock is held. */
592 #define	__asserts_exclusive(...) \
593 	__lock_annotate(assert_exclusive_lock(__VA_ARGS__))
594 #define	__asserts_shared(...) \
595 	__lock_annotate(assert_shared_lock(__VA_ARGS__))
596 
597 /* Function requires that an exclusive or shared lock is or is not held. */
598 #define	__requires_exclusive(...) \
599 	__lock_annotate(exclusive_locks_required(__VA_ARGS__))
600 #define	__requires_shared(...) \
601 	__lock_annotate(shared_locks_required(__VA_ARGS__))
602 #define	__requires_unlocked(...) \
603 	__lock_annotate(locks_excluded(__VA_ARGS__))
604 
605 /* Function should not be analyzed. */
606 #define	__no_lock_analysis	__lock_annotate(no_thread_safety_analysis)
607 
608 /*
609  * Function or variable should not be sanitized, e.g., by AddressSanitizer.
610  * GCC has the nosanitize attribute, but as a function attribute only, and
611  * warns on use as a variable attribute.
612  */
613 #if __has_feature(address_sanitizer) && defined(__clang__)
614 #ifdef _KERNEL
615 #define	__nosanitizeaddress	__attribute__((no_sanitize("kernel-address")))
616 #else
617 #define	__nosanitizeaddress	__attribute__((no_sanitize("address")))
618 #endif
619 #else
620 #define	__nosanitizeaddress
621 #endif
622 #if __has_feature(coverage_sanitizer) && defined(__clang__)
623 #define	__nosanitizecoverage	__attribute__((no_sanitize("coverage")))
624 #else
625 #define	__nosanitizecoverage
626 #endif
627 #if __has_feature(memory_sanitizer) && defined(__clang__)
628 #ifdef _KERNEL
629 #define	__nosanitizememory	__attribute__((no_sanitize("kernel-memory")))
630 #else
631 #define	__nosanitizememory	__attribute__((no_sanitize("memory")))
632 #endif
633 #else
634 #define	__nosanitizememory
635 #endif
636 #if __has_feature(thread_sanitizer) && defined(__clang__)
637 #define	__nosanitizethread	__attribute__((no_sanitize("thread")))
638 #else
639 #define	__nosanitizethread
640 #endif
641 
642 /*
643  * Make it possible to opt out of stack smashing protection.
644  */
645 #if __has_attribute(no_stack_protector)
646 #define	__nostackprotector	__attribute__((no_stack_protector))
647 #else
648 #define	__nostackprotector	\
649 	__attribute__((__optimize__("-fno-stack-protector")))
650 #endif
651 
652 /* Guard variables and structure members by lock. */
653 #define	__guarded_by(x)		__lock_annotate(guarded_by(x))
654 #define	__pt_guarded_by(x)	__lock_annotate(pt_guarded_by(x))
655 
656 /* Alignment builtins for better type checking and improved code generation. */
657 /* Provide fallback versions for other compilers (GCC/Clang < 10): */
658 #if !__has_builtin(__builtin_is_aligned)
659 #define __builtin_is_aligned(x, align)	\
660 	(((__uintptr_t)(x) & ((align) - 1)) == 0)
661 #endif
662 #if !__has_builtin(__builtin_align_up)
663 #define __builtin_align_up(x, align)	\
664 	((__typeof__(x))(((__uintptr_t)(x)+((align)-1))&(~((align)-1))))
665 #endif
666 #if !__has_builtin(__builtin_align_down)
667 #define __builtin_align_down(x, align)	\
668 	((__typeof__(x))((__uintptr_t)(x)&(~((align)-1))))
669 #endif
670 
671 #define __align_up(x, y) __builtin_align_up(x, y)
672 #define __align_down(x, y) __builtin_align_down(x, y)
673 #define __is_aligned(x, y) __builtin_is_aligned(x, y)
674 
675 #endif /* !_SYS_CDEFS_H_ */
676