blob: 2688626cd3960eee38b390878e6351c924360bdb [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/* $NetBSD: cdefs.h,v 1.58 2004/12/11 05:59:00 christos Exp $ */
2
3/*
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Berkeley Software Design, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)cdefs.h 8.8 (Berkeley) 1/9/95
35 */
36
37#ifndef _SYS_CDEFS_H_
38#define _SYS_CDEFS_H_
39
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080040/*
Elliott Hughese6c57fc2014-05-23 20:06:03 -070041 * Testing against Clang-specific extensions.
42 */
43
44#ifndef __has_extension
45#define __has_extension __has_feature
46#endif
47#ifndef __has_feature
48#define __has_feature(x) 0
49#endif
50#ifndef __has_include
51#define __has_include(x) 0
52#endif
53#ifndef __has_builtin
54#define __has_builtin(x) 0
55#endif
Nick Kralevich1aaa1782014-10-06 14:49:00 -070056#ifndef __has_attribute
57#define __has_attribute(x) 0
58#endif
Elliott Hughese6c57fc2014-05-23 20:06:03 -070059
60
Elliott Hughes998f2d52014-12-19 19:30:11 -080061#define __strong_alias(alias, sym) \
62 __asm__(".global " #alias "\n" \
63 #alias " = " #sym);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080064
65#if defined(__cplusplus)
66#define __BEGIN_DECLS extern "C" {
67#define __END_DECLS }
68#define __static_cast(x,y) static_cast<x>(y)
69#else
70#define __BEGIN_DECLS
71#define __END_DECLS
72#define __static_cast(x,y) (x)y
73#endif
74
Elliott Hughes5470c182016-07-22 11:36:17 -070075#if defined(__cplusplus)
76#define __BIONIC_CAST(_k,_t,_v) (_k<_t>(_v))
77#else
78#define __BIONIC_CAST(_k,_t,_v) ((_t) (_v))
79#endif
80
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080081/*
82 * The __CONCAT macro is used to concatenate parts of symbol names, e.g.
83 * with "#define OLD(foo) __CONCAT(old,foo)", OLD(foo) produces oldfoo.
84 * The __CONCAT macro is a bit tricky -- make sure you don't put spaces
85 * in between its arguments. __CONCAT can also concatenate double-quoted
86 * strings produced by the __STRING macro, but this only works with ANSI C.
87 */
88
89#define ___STRING(x) __STRING(x)
90#define ___CONCAT(x,y) __CONCAT(x,y)
91
David 'Digit' Turner8eda21b2011-03-17 10:56:25 +010092#if defined(__STDC__) || defined(__cplusplus)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080093#define __P(protos) protos /* full-blown ANSI C */
94#define __CONCAT(x,y) x ## y
95#define __STRING(x) #x
96
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080097#if defined(__cplusplus)
98#define __inline inline /* convert to C++ keyword */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080099#endif /* !__cplusplus */
100
101#else /* !(__STDC__ || __cplusplus) */
102#define __P(protos) () /* traditional C preprocessor */
103#define __CONCAT(x,y) x/**/y
104#define __STRING(x) "x"
105
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800106#endif /* !(__STDC__ || __cplusplus) */
107
Elliott Hughesf4840502016-06-06 17:35:53 -0700108#define __always_inline __attribute__((__always_inline__))
Elliott Hughes44ddb312015-09-22 13:00:21 -0700109#define __dead __attribute__((__noreturn__))
Elliott Hughesf4840502016-06-06 17:35:53 -0700110#define __noreturn __attribute__((__noreturn__))
111#define __mallocfunc __attribute__((malloc))
112#define __packed __attribute__((__packed__))
Elliott Hughes44ddb312015-09-22 13:00:21 -0700113#define __pure __attribute__((__const__))
Elliott Hughesa0ee0782013-01-30 19:06:37 -0800114#define __pure2 __attribute__((__const__)) /* Android-added: used by FreeBSD libm */
Elliott Hughesf4840502016-06-06 17:35:53 -0700115#define __purefunc __attribute__((pure))
116#define __unused __attribute__((__unused__))
117#define __used __attribute__((__used__))
Elliott Hughes06040fd2013-07-09 13:25:03 -0700118
Dan Albertbaa2a972015-08-13 16:58:50 -0700119/*
120 * _Nonnull is similar to the nonnull attribute in that it will instruct
121 * compilers to warn the user if it can prove that a null argument is being
122 * passed. Unlike the nonnull attribute, this annotation indicated that a value
123 * *should not* be null, not that it *cannot* be null, or even that the behavior
124 * is undefined. The important distinction is that the optimizer will perform
125 * surprising optimizations like the following:
126 *
127 * void foo(void*) __attribute__(nonnull, 1);
128 *
129 * int bar(int* p) {
130 * foo(p);
131 *
132 * // The following null check will be elided because nonnull attribute
133 * // means that, since we call foo with p, p can be assumed to not be
134 * // null. Thus this will crash if we are called with a null pointer.
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700135 * if (p != NULL) {
Dan Albertbaa2a972015-08-13 16:58:50 -0700136 * return *p;
137 * }
138 * return 0;
139 * }
140 *
141 * int main() {
142 * return bar(NULL);
143 * }
144 *
145 * http://clang.llvm.org/docs/AttributeReference.html#nonnull
146 */
147#if !(defined(__clang__) && __has_feature(nullability))
148#define _Nonnull
Elliott Hughes5470c182016-07-22 11:36:17 -0700149#define _Nullable
Dan Albertbaa2a972015-08-13 16:58:50 -0700150#endif
151
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700152#define __printflike(x, y) __attribute__((__format__(printf, x, y)))
153#define __scanflike(x, y) __attribute__((__format__(scanf, x, y)))
Elliott Hughesd04c1832013-05-14 16:08:43 -0700154
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800155/*
Elliott Hughesf4840502016-06-06 17:35:53 -0700156 * GNU C version 2.96 added explicit branch prediction so that
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800157 * the CPU back-end can hint the processor and also so that
158 * code blocks can be reordered such that the predicted path
159 * sees a more linear flow, thus improving cache behavior, etc.
160 *
161 * The following two macros provide us with a way to use this
162 * compiler feature. Use __predict_true() if you expect the expression
163 * to evaluate to true, and __predict_false() if you expect the
164 * expression to evaluate to false.
165 *
166 * A few notes about usage:
167 *
168 * * Generally, __predict_false() error condition checks (unless
169 * you have some _strong_ reason to do otherwise, in which case
170 * document it), and/or __predict_true() `no-error' condition
171 * checks, assuming you want to optimize for the no-error case.
172 *
173 * * Other than that, if you don't know the likelihood of a test
174 * succeeding from empirical or other `hard' evidence, don't
175 * make predictions.
176 *
177 * * These are meant to be used in places that are run `a lot'.
178 * It is wasteful to make predictions in code that is run
179 * seldomly (e.g. at subsystem initialization time) as the
180 * basic block reordering that this affects can often generate
181 * larger code.
182 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800183#define __predict_true(exp) __builtin_expect((exp) != 0, 1)
184#define __predict_false(exp) __builtin_expect((exp) != 0, 0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800185
Elliott Hughes24fad012013-02-04 13:44:14 -0800186#define __wur __attribute__((__warn_unused_result__))
Elliott Hughes24fad012013-02-04 13:44:14 -0800187
Josh Gao8ba44242016-04-25 14:53:13 -0700188#ifdef __clang__
189#define __errorattr(msg) __attribute__((unavailable(msg)))
190#else
Yabin Cuic9d09432015-02-13 10:52:35 -0800191#define __errorattr(msg) __attribute__((__error__(msg)))
Josh Gao8ba44242016-04-25 14:53:13 -0700192#endif
193
Yabin Cuic9d09432015-02-13 10:52:35 -0800194#define __errordecl(name, msg) extern void name(void) __errorattr(msg)
195
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800196/*
Elliott Hughes2cfb4e82014-08-18 14:45:42 -0700197 * Some BSD source needs these macros.
198 * Originally they embedded the rcs versions of each source file
199 * in the generated binary. We strip strings during build anyway,.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800200 */
Elliott Hughes2cfb4e82014-08-18 14:45:42 -0700201#define __IDSTRING(_prefix,_s) /* nothing */
202#define __COPYRIGHT(_s) /* nothing */
203#define __FBSDID(_s) /* nothing */
204#define __RCSID(_s) /* nothing */
205#define __SCCSID(_s) /* nothing */
David 'Digit' Turner3527fd62010-06-14 17:18:35 -0700206
Elliott Hughesf4c948a2014-08-19 11:16:41 -0700207/*
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700208 * With bionic, you always get all C and POSIX API.
Elliott Hughesfc8974b2014-09-04 08:52:38 -0700209 *
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700210 * If you want BSD and/or GNU extensions, _BSD_SOURCE and/or _GNU_SOURCE are
211 * expected to be defined by callers before *any* standard header file is
212 * included.
213 *
214 * In our header files we test against __USE_BSD and __USE_GNU.
Elliott Hughesf4c948a2014-08-19 11:16:41 -0700215 */
Elliott Hughesf4c948a2014-08-19 11:16:41 -0700216#if defined(_GNU_SOURCE)
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700217# define __USE_BSD 1
Elliott Hughesf4c948a2014-08-19 11:16:41 -0700218# define __USE_GNU 1
Elliott Hughesfc8974b2014-09-04 08:52:38 -0700219#endif
220
221#if defined(_BSD_SOURCE)
222# define __USE_BSD 1
Elliott Hughesf4c948a2014-08-19 11:16:41 -0700223#endif
224
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700225/* _FILE_OFFSET_BITS 64 support. */
Elliott Hughes68dc20d2015-02-06 22:28:49 -0800226#if !defined(__LP64__) && defined(_FILE_OFFSET_BITS)
227#if _FILE_OFFSET_BITS == 64
228#define __USE_FILE_OFFSET64 1
229#endif
230#endif
231
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700232/* C99 added the `restrict` type qualifier keyword. Before then, `__restrict` is a GNU extension. */
233#if __STDC_VERSION__ >= 199901L
234#define __restrict restrict
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700235#endif
236
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700237/* C99 added the `__func__` predefined identifier. */
238#if __STDC_VERSION__ < 199901L
239#define __func__ __PRETTY_FUNCTION__
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700240#endif
David 'Digit' Turner3527fd62010-06-14 17:18:35 -0700241
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800242#define __BIONIC__ 1
David 'Digit' Turner23d24392010-12-06 12:05:11 +0100243#include <android/api-level.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800244
Elliott Hughescaa15bf2014-04-03 13:59:14 -0700245/* glibc compatibility. */
246#if __LP64__
247#define __WORDSIZE 64
248#else
249#define __WORDSIZE 32
250#endif
251
Nick Kralevichbd8e6742013-08-28 13:22:52 -0700252/*
253 * When _FORTIFY_SOURCE is defined, automatic bounds checking is
254 * added to commonly used libc functions. If a buffer overrun is
255 * detected, the program is safely aborted.
256 *
257 * See
258 * http://gcc.gnu.org/onlinedocs/gcc/Object-Size-Checking.html for details.
259 */
Nick Kralevich16d1af12013-06-17 14:49:19 -0700260#if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0
Elliott Hughes44ddb312015-09-22 13:00:21 -0700261# define __BIONIC_FORTIFY 1
262# if _FORTIFY_SOURCE == 2
263# define __bos(s) __builtin_object_size((s), 1)
264# else
265# define __bos(s) __builtin_object_size((s), 0)
266# endif
267# define __bos0(s) __builtin_object_size((s), 0)
Elliott Hughes5470c182016-07-22 11:36:17 -0700268# if defined(__clang__)
269# define __BIONIC_FORTIFY_INLINE extern __inline__ __always_inline __attribute__((gnu_inline))
270# else
271# define __BIONIC_FORTIFY_INLINE extern __inline__ __always_inline __attribute__((gnu_inline)) __attribute__((__artificial__))
272# endif
Nick Kralevich1aaa1782014-10-06 14:49:00 -0700273#endif
Elliott Hughes5470c182016-07-22 11:36:17 -0700274#define __BIONIC_FORTIFY_UNKNOWN_SIZE __BIONIC_CAST(static_cast, size_t, -1)
Nick Kralevich71a18dd2012-06-07 14:01:26 -0700275
Elliott Hughes2cfb4e82014-08-18 14:45:42 -0700276/* Used to tag non-static symbols that are private and never exposed by the shared library. */
277#define __LIBC_HIDDEN__ __attribute__((visibility("hidden")))
278
Elliott Hughesf47514d2016-07-19 13:56:46 -0700279/*
280 * Used to tag symbols that should be hidden for 64-bit,
281 * but visible to preserve binary compatibility for LP32.
282 */
Elliott Hughes2cfb4e82014-08-18 14:45:42 -0700283#ifdef __LP64__
Elliott Hughesf47514d2016-07-19 13:56:46 -0700284#define __LIBC32_LEGACY_PUBLIC__ __attribute__((visibility("hidden")))
Elliott Hughes2cfb4e82014-08-18 14:45:42 -0700285#else
Elliott Hughesf47514d2016-07-19 13:56:46 -0700286#define __LIBC32_LEGACY_PUBLIC__ __attribute__((visibility("default")))
Elliott Hughes2cfb4e82014-08-18 14:45:42 -0700287#endif
288
Elliott Hughes2cfb4e82014-08-18 14:45:42 -0700289/* Used to rename functions so that the compiler emits a call to 'x' rather than the function this was applied to. */
290#define __RENAME(x) __asm__(#x)
291
Josh Gaob8e1b7052016-04-13 17:18:20 -0700292#if __ANDROID_API__ < 21
293#define __BIONIC_LEGACY_INLINE static __inline
294#else
295#define __BIONIC_LEGACY_INLINE extern
296#endif
297
Dan Alberta6435a12015-08-06 14:32:25 -0700298#ifdef __clang__
299#define __AVAILABILITY(...) __attribute__((availability(android,__VA_ARGS__)))
Josh Gao5a3d5ca2016-04-29 12:15:18 -0700300#else
301#define __AVAILABILITY(...)
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700302#endif
Josh Gao5a3d5ca2016-04-29 12:15:18 -0700303
Dan Alberta6435a12015-08-06 14:32:25 -0700304#define __INTRODUCED_IN(api_level) __AVAILABILITY(introduced=api_level)
305#define __DEPRECATED_IN(api_level) __AVAILABILITY(deprecated=api_level)
306#define __REMOVED_IN(api_level) __AVAILABILITY(obsoleted=api_level)
Josh Gao5a3d5ca2016-04-29 12:15:18 -0700307
Josh Gao34c599a2016-04-29 13:45:25 -0700308#define __INTRODUCED_IN_FUTURE __INTRODUCED_IN(10000)
309
Josh Gao5a3d5ca2016-04-29 12:15:18 -0700310#if __LP64__
311#define __INTRODUCED_IN_32(api_level)
312#define __INTRODUCED_IN_64 __INTRODUCED_IN
Dan Albert466dbe42015-01-28 18:16:08 -0800313#else
Josh Gao5a3d5ca2016-04-29 12:15:18 -0700314#define __INTRODUCED_IN_32 __INTRODUCED_IN
315#define __INTRODUCED_IN_64(api_level)
316#endif
Dan Albert466dbe42015-01-28 18:16:08 -0800317
Josh Gao46b44162016-05-27 11:14:16 -0700318#if defined(__arm__)
319#define __INTRODUCED_IN_ARM __INTRODUCED_IN
320#else
321#define __INTRODUCED_IN_ARM(x)
322#endif
323
324#if defined(__i386__)
325#define __INTRODUCED_IN_X86 __INTRODUCED_IN
326#else
327#define __INTRODUCED_IN_X86(x)
328#endif
329
330#if defined(__mips__)
331#define __INTRODUCED_IN_MIPS __INTRODUCED_IN
332#else
333#define __INTRODUCED_IN_MIPS(x)
334#endif
335
Daniel Micayfed26592015-07-18 13:55:51 -0400336#if __has_builtin(__builtin_umul_overflow) || __GNUC__ >= 5
337#if __LP64__
338#define __size_mul_overflow(a, b, result) __builtin_umull_overflow(a, b, result)
339#else
340#define __size_mul_overflow(a, b, result) __builtin_umul_overflow(a, b, result)
341#endif
342#else
Xia Yang6b45d572016-01-20 18:34:59 -0800343extern __inline__ __always_inline __attribute__((gnu_inline))
344int __size_mul_overflow(__SIZE_TYPE__ a, __SIZE_TYPE__ b, __SIZE_TYPE__ *result) {
Daniel Micayfed26592015-07-18 13:55:51 -0400345 *result = a * b;
346 static const __SIZE_TYPE__ mul_no_overflow = 1UL << (sizeof(__SIZE_TYPE__) * 4);
347 return (a >= mul_no_overflow || b >= mul_no_overflow) && a > 0 && (__SIZE_TYPE__)-1 / a < b;
348}
349#endif
350
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800351#endif /* !_SYS_CDEFS_H_ */