1 /* libc/sys/linux/sys/cdefs.h - Helper macros for K&R vs. ANSI C compat. */
2 
3 /* Written 2000 by Werner Almesberger */
4 
5 /*-
6  * SPDX-License-Identifier: BSD-3-Clause
7  *
8  * Copyright (c) 1991, 1993
9  *	The Regents of the University of California.  All rights reserved.
10  *
11  * This code is derived from software contributed to Berkeley by
12  * Berkeley Software Design, Inc.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)cdefs.h	8.8 (Berkeley) 1/9/95
39  * $FreeBSD$
40  */
41 
42 #ifndef	_SYS_CDEFS_H_
43 #define	_SYS_CDEFS_H_
44 
45 #include <machine/_default_types.h>
46 #include <sys/features.h>
47 #include <stddef.h>
48 
49 #define __PMT(args)	args
50 #define __DOTS    	, ...
51 #define __THROW
52 
53 #ifdef __GNUC__
54 # define __ASMNAME(cname)  __XSTRING (__USER_LABEL_PREFIX__) cname
55 #endif
56 
57 #define __ptr_t void *
58 #define __long_double_t  long double
59 
60 #define __attribute_malloc__
61 #define __attribute_pure__
62 #define __attribute_format_strfmon__(a,b)
63 #define __flexarr      [0]
64 
65 #ifndef __BOUNDED_POINTERS__
66 # define __bounded      /* nothing */
67 # define __unbounded    /* nothing */
68 # define __ptrvalue     /* nothing */
69 #endif
70 
71 /*
72  * Testing against Clang-specific extensions.
73  */
74 #ifndef	__has_attribute
75 #define	__has_attribute(x)	0
76 #endif
77 #ifndef	__has_extension
78 #define	__has_extension		__has_feature
79 #endif
80 #ifndef	__has_feature
81 #define	__has_feature(x)	0
82 #endif
83 #ifndef	__has_include
84 #define	__has_include(x)	0
85 #endif
86 #ifndef	__has_builtin
87 #define	__has_builtin(x)	0
88 #endif
89 
90 #if defined(__cplusplus)
91 #define	__BEGIN_DECLS	extern "C" {
92 #define	__END_DECLS	}
93 #else
94 #define	__BEGIN_DECLS
95 #define	__END_DECLS
96 #endif
97 
98 /**
99  * Not all compilers offer __builtin_expect (e.g. CompCert does
100  * not have it). In that case, transparently replace all
101  * occurences of that builtin with just the condition:
102  */
103 #ifndef _HAVE_BUILTIN_EXPECT
104 #define __builtin_expect(cond, exp) (cond)
105 #endif
106 
107 /*
108  * This code has been put in place to help reduce the addition of
109  * compiler specific defines in FreeBSD code.  It helps to aid in
110  * having a compiler-agnostic source tree.
111  */
112 
113 #if defined(__GNUC__)
114 
115 #if __GNUC__ >= 3
116 #define	__GNUCLIKE_ASM 3
117 #define	__GNUCLIKE_MATH_BUILTIN_CONSTANTS
118 #else
119 #define	__GNUCLIKE_ASM 2
120 #endif
121 #define	__GNUCLIKE___TYPEOF 1
122 #define	__GNUCLIKE___SECTION 1
123 
124 #define	__GNUCLIKE_CTOR_SECTION_HANDLING 1
125 
126 #define	__GNUCLIKE_BUILTIN_CONSTANT_P 1
127 
128 #if (__GNUC_MINOR__ > 95 || __GNUC__ >= 3)
129 #define	__GNUCLIKE_BUILTIN_VARARGS 1
130 #define	__GNUCLIKE_BUILTIN_STDARG 1
131 #define	__GNUCLIKE_BUILTIN_VAALIST 1
132 #endif
133 
134 #define	__GNUC_VA_LIST_COMPATIBILITY 1
135 
136 /*
137  * Compiler memory barriers, specific to gcc and clang.
138  */
139 #define	__compiler_membar()	__asm __volatile(" " : : : "memory")
140 
141 #define	__GNUCLIKE_BUILTIN_NEXT_ARG 1
142 #define	__GNUCLIKE_MATH_BUILTIN_RELOPS
143 
144 #define	__GNUCLIKE_BUILTIN_MEMCPY 1
145 
146 /* XXX: if __GNUC__ >= 2: not tested everywhere originally, where replaced */
147 #define	__CC_SUPPORTS_INLINE 1
148 #define	__CC_SUPPORTS___INLINE 1
149 #define	__CC_SUPPORTS___INLINE__ 1
150 
151 #define	__CC_SUPPORTS___FUNC__ 1
152 #define	__CC_SUPPORTS_WARNING 1
153 
154 #define	__CC_SUPPORTS_VARADIC_XXX 1 /* see varargs.h */
155 
156 #define	__CC_SUPPORTS_DYNAMIC_ARRAY_INIT 1
157 
158 #endif /* __GNUC__ */
159 
160 /*
161  * The __CONCAT macro is used to concatenate parts of symbol names, e.g.
162  * with "#define OLD(foo) __CONCAT(old,foo)", OLD(foo) produces oldfoo.
163  * The __CONCAT macro is a bit tricky to use if it must work in non-ANSI
164  * mode -- there must be no spaces between its arguments, and for nested
165  * __CONCAT's, all the __CONCAT's must be at the left.  __CONCAT can also
166  * concatenate double-quoted strings produced by the __STRING macro, but
167  * this only works with ANSI C.
168  *
169  * __XSTRING is like __STRING, but it expands any macros in its argument
170  * first.  It is only available with ANSI C.
171  */
172 #if defined(__STDC__) || defined(__cplusplus)
173 #define	__P(protos)	protos		/* full-blown ANSI C */
174 #define	__CONCAT1(x,y)	x ## y
175 #define	__CONCAT(x,y)	__CONCAT1(x,y)
176 #define	__STRING(x)	#x		/* stringify without expanding x */
177 #define	__XSTRING(x)	__STRING(x)	/* expand x, then stringify */
178 
179 #define	__const		const		/* define reserved names to standard */
180 #define	__signed	signed
181 #define	__volatile	volatile
182 #if defined(__cplusplus)
183 #define	__inline	inline		/* convert to C++ keyword */
184 #else
185 #if !(defined(__CC_SUPPORTS___INLINE))
186 #define	__inline			/* delete GCC keyword */
187 #endif /* ! __CC_SUPPORTS___INLINE */
188 #endif /* !__cplusplus */
189 
190 #else	/* !(__STDC__ || __cplusplus) */
191 #define	__P(protos)	()		/* traditional C preprocessor */
192 #define	__CONCAT(x,y)	x/**/y
193 #define	__STRING(x)	"x"
194 
195 #if !defined(__CC_SUPPORTS___INLINE)
196 #define	__const				/* delete pseudo-ANSI C keywords */
197 #define	__inline
198 #define	__signed
199 #define	__volatile
200 /*
201  * In non-ANSI C environments, new programs will want ANSI-only C keywords
202  * deleted from the program and old programs will want them left alone.
203  * When using a compiler other than gcc, programs using the ANSI C keywords
204  * const, inline etc. as normal identifiers should define -DNO_ANSI_KEYWORDS.
205  * When using "gcc -traditional", we assume that this is the intent; if
206  * __GNUC__ is defined but __STDC__ is not, we leave the new keywords alone.
207  */
208 #ifndef	NO_ANSI_KEYWORDS
209 #define	const				/* delete ANSI C keywords */
210 #define	inline
211 #define	signed
212 #define	volatile
213 #endif	/* !NO_ANSI_KEYWORDS */
214 #endif	/* !__CC_SUPPORTS___INLINE */
215 #endif	/* !(__STDC__ || __cplusplus) */
216 
217 /*
218  * Compiler-dependent macros to help declare dead (non-returning) and
219  * pure (no side effects) functions, and unused variables.  They are
220  * null except for versions of gcc that are known to support the features
221  * properly (old versions of gcc-2 supported the dead and pure features
222  * in a different (wrong) way).  If we do not provide an implementation
223  * for a given compiler, let the compile fail if it is told to use
224  * a feature that we cannot live without.
225  */
226 #define	__weak_symbol	__attribute__((__weak__))
227 #if !__GNUC_PREREQ__(2, 5)
228 #define	__dead2
229 #define	__pure2
230 #define	__unused
231 #endif
232 #if __GNUC__ == 2 && __GNUC_MINOR__ >= 5 && __GNUC_MINOR__ < 7
233 #define	__dead2		__attribute__((__noreturn__))
234 #define	__pure2		__attribute__((__const__))
235 #define	__unused
236 /* XXX Find out what to do for __packed, __aligned and __section */
237 #endif
238 #if __GNUC_PREREQ__(2, 7)
239 #define	__dead2		__attribute__((__noreturn__))
240 #define	__pure2		__attribute__((__const__))
241 #define	__unused	__attribute__((__unused__))
242 #define	__used		__attribute__((__used__))
243 #define	__packed	__attribute__((__packed__))
244 #define	__aligned(x)	__attribute__((__aligned__(x)))
245 #define	__section(x)	__attribute__((__section__(x)))
246 #endif
247 
248 #ifdef _HAVE_ALLOC_SIZE
249 #define	__alloc_size(x)	__attribute__((__alloc_size__(x)))
250 #define	__alloc_size2(n, x)	__attribute__((__alloc_size__(n, x)))
251 #else
252 #define	__alloc_size(x)
253 #define	__alloc_size2(n, x)
254 #endif
255 #if __GNUC_PREREQ__(4, 9) || __has_attribute(__alloc_align__)
256 #define	__alloc_align(x)	__attribute__((__alloc_align__(x)))
257 #else
258 #define	__alloc_align(x)
259 #endif
260 
261 #if !__GNUC_PREREQ__(2, 95)
262 #define	__alignof(x)	__offsetof(struct { char __a; x __b; }, __b)
263 #endif
264 
265 #if __GNUC_PREREQ__(4,5) || defined(__clang__)
266 #define __picolibc_deprecated(m) __attribute__((__deprecated__(m)))
267 #else
268 #define __picolibc_deprecated(m) _ATTRIBUTE(__deprecated__)
269 #endif
270 
271 /*
272  * Keywords added in C11.
273  */
274 
275 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L
276 
277 #if !__has_extension(c_alignas)
278 #if (defined(__cplusplus) && __cplusplus >= 201103L) || \
279     __has_extension(cxx_alignas)
280 #define	_Alignas(x)		alignas(x)
281 #else
282 /* XXX: Only emulates _Alignas(constant-expression); not _Alignas(type-name). */
283 #define	_Alignas(x)		__aligned(x)
284 #endif
285 #endif
286 
287 #if defined(__cplusplus) && __cplusplus >= 201103L
288 #define	_Alignof(x)		alignof(x)
289 #else
290 #define	_Alignof(x)		__alignof(x)
291 #endif
292 
293 #if !defined(__cplusplus) && !__has_extension(c_atomic) && \
294 	!__has_extension(cxx_atomic) && !__GNUC_PREREQ__(4, 7)
295 /*
296  * No native support for _Atomic(). Place object in structure to prevent
297  * most forms of direct non-atomic access.
298  */
299 #define	_Atomic(T)		struct { T volatile __val; }
300 #endif
301 
302 #if defined(__cplusplus) && __cplusplus >= 201103L
303 #define	_Noreturn		[[noreturn]]
304 #else
305 #define	_Noreturn		__dead2
306 #endif
307 
308 #if !__has_extension(c_static_assert)
309 #if (defined(__cplusplus) && __cplusplus >= 201103L) || \
310     __has_extension(cxx_static_assert)
311 #define	_Static_assert(x, y)	static_assert(x, y)
312 #elif __GNUC_PREREQ__(4,6) && !defined(__cplusplus)
313 /* Nothing, gcc 4.6 and higher has _Static_assert built-in */
314 #elif defined(__COUNTER__)
315 #define	_Static_assert(x, y)	__Static_assert(x, __COUNTER__)
316 #define	__Static_assert(x, y)	___Static_assert(x, y)
317 #define	___Static_assert(x, y)	typedef char __assert_ ## y[(x) ? 1 : -1] \
318 				__unused
319 #else
320 #define	_Static_assert(x, y)	struct __hack
321 #endif
322 #endif
323 
324 #endif /* __STDC_VERSION__ || __STDC_VERSION__ < 201112L */
325 
326 /*
327  * Emulation of C11 _Generic().  Unlike the previously defined C11
328  * keywords, it is not possible to implement this using exactly the same
329  * syntax.  Therefore implement something similar under the name
330  * __generic().  Unlike _Generic(), this macro can only distinguish
331  * between a single type, so it requires nested invocations to
332  * distinguish multiple cases.
333  *
334  * Note that the comma operator is used to force expr to decay in
335  * order to match _Generic().
336  */
337 
338 #if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) || \
339     __has_extension(c_generic_selections)
340 #define	__generic(expr, t, yes, no)					\
341 	_Generic(expr, t: yes, default: no)
342 #elif __GNUC_PREREQ__(3, 1) && !defined(__cplusplus)
343 #define	__generic(expr, t, yes, no)					\
344 	__builtin_choose_expr(						\
345 	    __builtin_types_compatible_p(__typeof((0, (expr))), t), yes, no)
346 #endif
347 
348 /*
349  * C99 Static array indices in function parameter declarations.  Syntax such as:
350  * void bar(int myArray[static 10]);
351  * is allowed in C99 but not in C++.  Define __min_size appropriately so
352  * headers using it can be compiled in either language.  Use like this:
353  * void bar(int myArray[__min_size(10)]);
354  */
355 #if !defined(__cplusplus) && \
356     (defined(__clang__) || __GNUC_PREREQ__(4, 6)) && \
357     (!defined(__STDC_VERSION__) || (__STDC_VERSION__ >= 199901))
358 #define __min_size(x)	static (x)
359 #else
360 #define __min_size(x)	(x)
361 #endif
362 
363 #if __GNUC_PREREQ__(2, 96)
364 # if __GNUC_PREREQ__(11, 0)
365 #  define       __malloc_like_with_free(_f,_a) __attribute__((__malloc__, __malloc__(_f,_a)))
366 # else
367 #  define       __malloc_like_with_free(_f,_a) __attribute__((__malloc__))
368 # endif
369 # define	__pure		__attribute__((__pure__))
370 #else
371 #define __malloc_like_with_free(free,free_arg)
372 #define	__pure
373 #endif
374 
375 #define	__malloc_like __malloc_like_with_free(free, 1)
376 
377 #ifndef __always_inline
378 #if __GNUC_PREREQ__(3, 1)
379 #define	__always_inline	__inline__ __attribute__((__always_inline__))
380 #else
381 #define	__always_inline
382 #endif
383 #endif
384 
385 #ifndef __noinline
386 #if __GNUC_PREREQ__(3, 1)
387 #define	__noinline	__attribute__ ((__noinline__))
388 #else
389 #define	__noinline
390 #endif
391 #endif
392 
393 #if defined(_HAVE_ATTRIBUTE_ALWAYS_INLINE) && defined(_HAVE_ATTRIBUTE_GNU_INLINE)
394 /*
395  * When this macro is defined, use it to declare inline versions of extern functions.
396  */
397 #define __declare_extern_inline(type) extern __inline type __attribute((gnu_inline, always_inline))
398 #endif
399 
400 #if defined(__clang__) && defined(__nonnull)
401 /* Clang has a builtin macro __nonnull for the _Nonnull qualifier */
402 #undef __nonnull
403 #endif
404 #if __GNUC_PREREQ__(3, 3)
405 #define	__nonnull(x)	__attribute__((__nonnull__ x))
406 #define	__nonnull_all	__attribute__((__nonnull__))
407 #else
408 #define	__nonnull(x)
409 #define	__nonnull_all
410 #endif
411 
412 #if __GNUC_PREREQ__(3, 4)
413 #define	__fastcall	__attribute__((__fastcall__))
414 #define	__result_use_check	__attribute__((__warn_unused_result__))
415 #else
416 #define	__fastcall
417 #define	__result_use_check
418 #endif
419 
420 #if __GNUC_PREREQ__(4, 1)
421 #define	__returns_twice	__attribute__((__returns_twice__))
422 #else
423 #define	__returns_twice
424 #endif
425 
426 #if __GNUC_PREREQ__(4, 6) || __has_builtin(__builtin_unreachable)
427 #define	__unreachable()	__builtin_unreachable()
428 #else
429 #define	__unreachable()	((void)0)
430 #endif
431 
432 /* XXX: should use `#if __STDC_VERSION__ < 199901'. */
433 #if !__GNUC_PREREQ__(2, 7) && !defined(__COMPCERT__)
434 #define	__func__	NULL
435 #endif
436 
437 /*
438  * We use `__restrict' as a way to define the `restrict' type qualifier
439  * without disturbing older software that is unaware of C99 keywords.
440  * GCC also provides `__restrict' as an extension to support C99-style
441  * restricted pointers in other language modes.
442  */
443 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901
444 #define	__restrict	restrict
445 #elif !__GNUC_PREREQ__(2, 95)
446 #define	__restrict
447 #endif
448 
449 /*
450  * Additionally, we allow to use `__restrict_arr' for declaring arrays as
451  * non-overlapping per C99.  That's supported since gcc 3.1, but it's not
452  * allowed in C++.
453  */
454 #if defined(__cplusplus) || !__GNUC_PREREQ__(3, 1)
455 #define __restrict_arr
456 #elif defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
457 #define __restrict_arr       restrict
458 #else
459 #define __restrict_arr
460 #endif
461 
462 /*
463  * GNU C version 2.96 adds explicit branch prediction so that
464  * the CPU back-end can hint the processor and also so that
465  * code blocks can be reordered such that the predicted path
466  * sees a more linear flow, thus improving cache behavior, etc.
467  *
468  * The following two macros provide us with a way to utilize this
469  * compiler feature.  Use __predict_true() if you expect the expression
470  * to evaluate to true, and __predict_false() if you expect the
471  * expression to evaluate to false.
472  *
473  * A few notes about usage:
474  *
475  *	* Generally, __predict_false() error condition checks (unless
476  *	  you have some _strong_ reason to do otherwise, in which case
477  *	  document it), and/or __predict_true() `no-error' condition
478  *	  checks, assuming you want to optimize for the no-error case.
479  *
480  *	* Other than that, if you don't know the likelihood of a test
481  *	  succeeding from empirical or other `hard' evidence, don't
482  *	  make predictions.
483  *
484  *	* These are meant to be used in places that are run `a lot'.
485  *	  It is wasteful to make predictions in code that is run
486  *	  seldomly (e.g. at subsystem initialization time) as the
487  *	  basic block reordering that this affects can often generate
488  *	  larger code.
489  */
490 #if __GNUC_PREREQ__(2, 96)
491 #define	__predict_true(exp)     __builtin_expect((exp), 1)
492 #define	__predict_false(exp)    __builtin_expect((exp), 0)
493 #else
494 #define	__predict_true(exp)     (exp)
495 #define	__predict_false(exp)    (exp)
496 #endif
497 
498 #if __GNUC_PREREQ__(4, 0)
499 #define	__null_sentinel	__attribute__((__sentinel__))
500 #define	__exported	__attribute__((__visibility__("default")))
501 /* Only default visibility is supported on PE/COFF targets. */
502 #ifndef __CYGWIN__
503 #define	__hidden	__attribute__((__visibility__("hidden")))
504 #else
505 #define	__hidden
506 #endif
507 #else
508 #define	__null_sentinel
509 #define	__exported
510 #define	__hidden
511 #endif
512 
513 #define __offsetof(type, field)	offsetof(type, field)
514 #define	__rangeof(type, start, end) \
515 	(__offsetof(type, end) - __offsetof(type, start))
516 
517 /*
518  * Given the pointer x to the member m of the struct s, return
519  * a pointer to the containing structure.  When using GCC, we first
520  * assign pointer x to a local variable, to check that its type is
521  * compatible with member m.
522  */
523 #if __GNUC_PREREQ__(3, 1)
524 #define	__containerof(x, s, m) ({					\
525 	const volatile __typeof(((s *)0)->m) *__x = (x);		\
526 	__DEQUALIFY(s *, (const volatile char *)__x - __offsetof(s, m));\
527 })
528 #else
529 #define	__containerof(x, s, m)						\
530 	__DEQUALIFY(s *, (const volatile char *)(x) - __offsetof(s, m))
531 #endif
532 
533 /*
534  * Compiler-dependent macros to declare that functions take printf-like
535  * or scanf-like arguments.  They are null except for versions of gcc
536  * that are known to support the features properly (old versions of gcc-2
537  * didn't permit keeping the keywords out of the application namespace).
538  */
539 #if !__GNUC_PREREQ__(2, 7)
540 #define	__printflike(fmtarg, firstvararg)
541 #define	__scanflike(fmtarg, firstvararg)
542 #define	__format_arg(fmtarg)
543 #define	__strfmonlike(fmtarg, firstvararg)
544 #define	__strftimelike(fmtarg, firstvararg)
545 #else
546 #define	__printflike(fmtarg, firstvararg) \
547 	    __attribute__((__format__ (__printf__, fmtarg, firstvararg)))
548 #define	__scanflike(fmtarg, firstvararg) \
549 	    __attribute__((__format__ (__scanf__, fmtarg, firstvararg)))
550 #define	__format_arg(fmtarg)	__attribute__((__format_arg__ (fmtarg)))
551 #define	__strfmonlike(fmtarg, firstvararg) \
552 	    __attribute__((__format__ (__strfmon__, fmtarg, firstvararg)))
553 #define	__strftimelike(fmtarg, firstvararg) \
554 	    __attribute__((__format__ (__strftime__, fmtarg, firstvararg)))
555 #endif
556 
557 /* Compiler-dependent macros that rely on FreeBSD-specific extensions. */
558 #if defined(__FreeBSD_cc_version) && __FreeBSD_cc_version >= 300001 && \
559     defined(__GNUC__)
560 #define	__printf0like(fmtarg, firstvararg) \
561 	    __attribute__((__format__ (__printf0__, fmtarg, firstvararg)))
562 #else
563 #define	__printf0like(fmtarg, firstvararg)
564 #endif
565 
566 #if defined(_HAVE_ALIAS_ATTRIBUTE)
567 #define	__strong_reference(sym,aliassym)	\
568     extern __typeof (sym) aliassym __attribute__ ((__alias__ (__STRING(sym))))
569 #endif
570 
571 #if defined(_HAVE_WEAK_ATTRIBUTE) && defined(_HAVE_ALIAS_ATTRIBUTE)
572 #define	__weak_reference(sym,aliassym)	\
573     extern __typeof (sym) aliassym __attribute__ ((__weak__, __alias__ (__STRING(sym))))
574 #endif
575 
576 /*
577    Taken from glibc:
578    Add the compiler optimization to inhibit loop transformation to library
579    calls.  This is used to avoid recursive calls in memset and memmove
580    default implementations.
581 */
582 #if defined(_HAVE_CC_INHIBIT_LOOP_TO_LIBCALL)
583 # define __inhibit_loop_to_libcall \
584   __attribute__ ((__optimize__ ("-fno-tree-loop-distribute-patterns")))
585 #elif defined(_HAVE_NO_BUILTIN_ATTRIBUTE)
586 # define __inhibit_loop_to_libcall \
587     __attribute__((no_builtin))
588 #else
589 # define __inhibit_loop_to_libcall
590 #endif
591 
592 /*
593  * Tell optimizer to not add new builtin calls when optimizing a
594  * function body.
595  */
596 #if defined(_HAVE_NO_BUILTIN_ATTRIBUTE)
597 # define __inhibit_new_builtin_calls \
598     __attribute__((no_builtin))
599 #else
600 #define __inhibit_new_builtin_calls
601 #endif
602 
603 #if defined(__GNUC__)
604 #ifndef __weak_reference
605 #ifdef __ELF__
606 #ifdef __STDC__
607 #define	__weak_reference(sym,alias)	\
608 	__asm__(".weak " #alias);	\
609 	__asm__(".equ "  #alias ", " #sym)
610 #else
611 #define	__weak_reference(sym,alias)	\
612 	__asm__(".weak alias");		\
613 	__asm__(".equ alias, sym")
614 #endif
615 #elif __clang__
616 #ifdef __MACH__
617 /* Macos prefixes all C symbols with an underscore, this needs to be done manually in asm */
618 
619 /* So far I have not been able to create on Macos an exported symbol that itself
620  * is weak but aliases a strong symbol. A workaround is to make the original
621  * symbol weak and the alias symbol will automatically become weak too. */
622 /* Hint: use `nm -m obj.o` to check the symbols weak/strong on Mac */
623 #define __weak_reference(sym,alias) \
624 	__asm__(".weak_definition _" #sym); \
625 	__asm__(".globl _" #alias); \
626 	__asm__(".set _" #alias ", _" #sym)
627 #elif defined(__STDC__)
628 #define __weak_reference(sym,alias) \
629 	__asm__(".weak_reference " #alias); \
630 	__asm__(".globl " #alias); \
631 	__asm__(".set " #alias ", " #sym)
632 #else
633 #define __weak_reference(sym,alias) \
634 	__asm__(".weak_reference alias");\
635 	__asm__(".set alias, sym")
636 #endif
637 #else	/* !__ELF__ && !__clang__ */
638 #ifdef __STDC__
639 #define	__weak_reference(sym,alias)	\
640 	__asm__(".stabs \"_" #alias "\",11,0,0,0");	\
641 	__asm__(".stabs \"_" #sym "\",1,0,0,0")
642 #else
643 #define	__weak_reference(sym,alias)	\
644 	__asm__(".stabs \"_/**/alias\",11,0,0,0");	\
645 	__asm__(".stabs \"_/**/sym\",1,0,0,0")
646 #endif
647 #endif
648 #endif
649 
650 #endif	/* __GNUC__ */
651 
652 #ifndef	__FBSDID
653 #define	__FBSDID(s)	struct __hack
654 #endif
655 
656 #ifndef	__RCSID
657 #define	__RCSID(s)	struct __hack
658 #endif
659 
660 #ifndef	__RCSID_SOURCE
661 #define	__RCSID_SOURCE(s)	struct __hack
662 #endif
663 
664 #ifndef	__SCCSID
665 #define	__SCCSID(s)	struct __hack
666 #endif
667 
668 #ifndef	__COPYRIGHT
669 #define	__COPYRIGHT(s)	struct __hack
670 #endif
671 
672 #ifndef	__DECONST
673 #define	__DECONST(type, var)	((type)(__uintptr_t)(const void *)(var))
674 #endif
675 
676 #ifndef	__DEVOLATILE
677 #define	__DEVOLATILE(type, var)	((type)(__uintptr_t)(volatile void *)(var))
678 #endif
679 
680 #ifndef	__DEQUALIFY
681 #define	__DEQUALIFY(type, var)	((type)(__uintptr_t)(const volatile void *)(var))
682 #endif
683 
684 /*
685  * Nullability qualifiers: currently only supported by Clang.
686  */
687 #if !(defined(__clang__) && __has_feature(nullability))
688 #define	_Nonnull
689 #define	_Nullable
690 #define	_Null_unspecified
691 #define	__NULLABILITY_PRAGMA_PUSH
692 #define	__NULLABILITY_PRAGMA_POP
693 #else
694 #define	__NULLABILITY_PRAGMA_PUSH _Pragma("clang diagnostic push")	\
695 	_Pragma("clang diagnostic ignored \"-Wnullability-completeness\"")
696 #define	__NULLABILITY_PRAGMA_POP _Pragma("clang diagnostic pop")
697 #endif
698 
699 /*
700  * Type Safety Checking
701  *
702  * Clang provides additional attributes to enable checking type safety
703  * properties that cannot be enforced by the C type system.
704  */
705 
706 #if __has_attribute(__argument_with_type_tag__) && \
707     __has_attribute(__type_tag_for_datatype__)
708 #define	__arg_type_tag(arg_kind, arg_idx, type_tag_idx) \
709 	    __attribute__((__argument_with_type_tag__(arg_kind, arg_idx, type_tag_idx)))
710 #define	__datatype_type_tag(kind, type) \
711 	    __attribute__((__type_tag_for_datatype__(kind, type)))
712 #else
713 #define	__arg_type_tag(arg_kind, arg_idx, type_tag_idx)
714 #define	__datatype_type_tag(kind, type)
715 #endif
716 
717 /*
718  * Lock annotations.
719  *
720  * Clang provides support for doing basic thread-safety tests at
721  * compile-time, by marking which locks will/should be held when
722  * entering/leaving a functions.
723  *
724  * Furthermore, it is also possible to annotate variables and structure
725  * members to enforce that they are only accessed when certain locks are
726  * held.
727  */
728 
729 #if __has_extension(c_thread_safety_attributes)
730 #define	__lock_annotate(x)	__attribute__((x))
731 #else
732 #define	__lock_annotate(x)
733 #endif
734 
735 /* Structure implements a lock. */
736 /* FIXME: Use __lockable__, etc. to avoid colliding with user namespace macros,
737  * once clang is fixed: https://bugs.llvm.org/show_bug.cgi?id=34319 */
738 #define	__lockable		__lock_annotate(lockable)
739 
740 /* Function acquires an exclusive or shared lock. */
741 #define	__locks_exclusive(...) \
742 	__lock_annotate(exclusive_lock_function(__VA_ARGS__))
743 #define	__locks_shared(...) \
744 	__lock_annotate(shared_lock_function(__VA_ARGS__))
745 
746 /* Function attempts to acquire an exclusive or shared lock. */
747 #define	__trylocks_exclusive(...) \
748 	__lock_annotate(exclusive_trylock_function(__VA_ARGS__))
749 #define	__trylocks_shared(...) \
750 	__lock_annotate(shared_trylock_function(__VA_ARGS__))
751 
752 /* Function releases a lock. */
753 #define	__unlocks(...)		__lock_annotate(unlock_function(__VA_ARGS__))
754 
755 /* Function asserts that an exclusive or shared lock is held. */
756 #define	__asserts_exclusive(...) \
757 	__lock_annotate(assert_exclusive_lock(__VA_ARGS__))
758 #define	__asserts_shared(...) \
759 	__lock_annotate(assert_shared_lock(__VA_ARGS__))
760 
761 /* Function requires that an exclusive or shared lock is or is not held. */
762 #define	__requires_exclusive(...) \
763 	__lock_annotate(exclusive_locks_required(__VA_ARGS__))
764 #define	__requires_shared(...) \
765 	__lock_annotate(shared_locks_required(__VA_ARGS__))
766 #define	__requires_unlocked(...) \
767 	__lock_annotate(locks_excluded(__VA_ARGS__))
768 
769 /* Function should not be analyzed. */
770 #define	__no_lock_analysis	__lock_annotate(no_thread_safety_analysis)
771 
772 /*
773  * Function or variable should not be sanitized, e.g., by AddressSanitizer.
774  * GCC has the nosanitize attribute, but as a function attribute only, and
775  * warns on use as a variable attribute.
776  */
777 #if __has_attribute(no_sanitize) && defined(__clang__)
778 #ifdef _KERNEL
779 #define __nosanitizeaddress	__attribute__((no_sanitize("kernel-address")))
780 #define __nosanitizememory	__attribute__((no_sanitize("kernel-memory")))
781 #else
782 #define __nosanitizeaddress	__attribute__((no_sanitize("address")))
783 #define __nosanitizememory	__attribute__((no_sanitize("memory")))
784 #endif
785 #define __nosanitizethread	__attribute__((no_sanitize("thread")))
786 #else
787 #define __nosanitizeaddress
788 #define __nosanitizememory
789 #define __nosanitizethread
790 #endif
791 
792 /*
793  * fall-through case statement annotations
794  */
795 #if __cplusplus >= 201703L || __STDC_VERSION__ > 201710L
796 /* Standard C++17/C23 attribute */
797 #define __PICOLIBC_FALLTHROUGH [[fallthrough]]
798 #elif __has_attribute(fallthrough)
799 /* Non-standard but supported by at least gcc and clang */
800 #define __PICOLIBC_FALLTHROUGH __attribute__((fallthrough))
801 #else
802 #define __PICOLIBC_FALLTHROUGH do { } while(0)
803 #endif
804 
805 /* Guard variables and structure members by lock. */
806 #define	__guarded_by(x)		__lock_annotate(guarded_by(x))
807 #define	__pt_guarded_by(x)	__lock_annotate(pt_guarded_by(x))
808 
809 /* Alignment builtins for better type checking and improved code generation. */
810 /* Provide fallback versions for other compilers (GCC/Clang < 10): */
811 #if !__has_builtin(__builtin_is_aligned)
812 #define __builtin_is_aligned(x, align)	\
813 	(((__uintptr_t)x & ((align) - 1)) == 0)
814 #endif
815 #if !__has_builtin(__builtin_align_up)
816 #define __builtin_align_up(x, align)	\
817 	((__typeof__(x))(((__uintptr_t)(x)+((align)-1))&(~((align)-1))))
818 #endif
819 #if !__has_builtin(__builtin_align_down)
820 #define __builtin_align_down(x, align)	\
821 	((__typeof__(x))((x)&(~((align)-1))))
822 #endif
823 
824 #define __align_up(x, y) __builtin_align_up(x, y)
825 #define __align_down(x, y) __builtin_align_down(x, y)
826 #define __is_aligned(x, y) __builtin_is_aligned(x, y)
827 
828 #endif /* !_SYS_CDEFS_H_ */
829