1*8f0ba417SAndroid Build Coastguard Worker /*
2*8f0ba417SAndroid Build Coastguard Worker * Copyright (C) 2015 The Android Open Source Project
3*8f0ba417SAndroid Build Coastguard Worker *
4*8f0ba417SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*8f0ba417SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*8f0ba417SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*8f0ba417SAndroid Build Coastguard Worker *
8*8f0ba417SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*8f0ba417SAndroid Build Coastguard Worker *
10*8f0ba417SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*8f0ba417SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*8f0ba417SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8f0ba417SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*8f0ba417SAndroid Build Coastguard Worker * limitations under the License.
15*8f0ba417SAndroid Build Coastguard Worker */
16*8f0ba417SAndroid Build Coastguard Worker
17*8f0ba417SAndroid Build Coastguard Worker #pragma once
18*8f0ba417SAndroid Build Coastguard Worker
19*8f0ba417SAndroid Build Coastguard Worker #include <stddef.h> // for size_t
20*8f0ba417SAndroid Build Coastguard Worker #include <unistd.h> // for TEMP_FAILURE_RETRY
21*8f0ba417SAndroid Build Coastguard Worker
22*8f0ba417SAndroid Build Coastguard Worker #include <utility>
23*8f0ba417SAndroid Build Coastguard Worker
24*8f0ba417SAndroid Build Coastguard Worker // bionic and glibc both have TEMP_FAILURE_RETRY, but eg Mac OS' libc doesn't.
25*8f0ba417SAndroid Build Coastguard Worker #ifndef TEMP_FAILURE_RETRY
26*8f0ba417SAndroid Build Coastguard Worker #define TEMP_FAILURE_RETRY(exp) \
27*8f0ba417SAndroid Build Coastguard Worker ({ \
28*8f0ba417SAndroid Build Coastguard Worker decltype(exp) _rc; \
29*8f0ba417SAndroid Build Coastguard Worker do { \
30*8f0ba417SAndroid Build Coastguard Worker _rc = (exp); \
31*8f0ba417SAndroid Build Coastguard Worker } while (_rc == -1 && errno == EINTR); \
32*8f0ba417SAndroid Build Coastguard Worker _rc; \
33*8f0ba417SAndroid Build Coastguard Worker })
34*8f0ba417SAndroid Build Coastguard Worker #endif
35*8f0ba417SAndroid Build Coastguard Worker
36*8f0ba417SAndroid Build Coastguard Worker // A macro to disallow the copy constructor and operator= functions
37*8f0ba417SAndroid Build Coastguard Worker // This must be placed in the private: declarations for a class.
38*8f0ba417SAndroid Build Coastguard Worker //
39*8f0ba417SAndroid Build Coastguard Worker // For disallowing only assign or copy, delete the relevant operator or
40*8f0ba417SAndroid Build Coastguard Worker // constructor, for example:
41*8f0ba417SAndroid Build Coastguard Worker // void operator=(const TypeName&) = delete;
42*8f0ba417SAndroid Build Coastguard Worker // Note, that most uses of DISALLOW_ASSIGN and DISALLOW_COPY are broken
43*8f0ba417SAndroid Build Coastguard Worker // semantically, one should either use disallow both or neither. Try to
44*8f0ba417SAndroid Build Coastguard Worker // avoid these in new code.
45*8f0ba417SAndroid Build Coastguard Worker #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
46*8f0ba417SAndroid Build Coastguard Worker TypeName(const TypeName&) = delete; \
47*8f0ba417SAndroid Build Coastguard Worker void operator=(const TypeName&) = delete
48*8f0ba417SAndroid Build Coastguard Worker
49*8f0ba417SAndroid Build Coastguard Worker // A macro to disallow all the implicit constructors, namely the
50*8f0ba417SAndroid Build Coastguard Worker // default constructor, copy constructor and operator= functions.
51*8f0ba417SAndroid Build Coastguard Worker //
52*8f0ba417SAndroid Build Coastguard Worker // This should be used in the private: declarations for a class
53*8f0ba417SAndroid Build Coastguard Worker // that wants to prevent anyone from instantiating it. This is
54*8f0ba417SAndroid Build Coastguard Worker // especially useful for classes containing only static methods.
55*8f0ba417SAndroid Build Coastguard Worker #define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
56*8f0ba417SAndroid Build Coastguard Worker TypeName() = delete; \
57*8f0ba417SAndroid Build Coastguard Worker DISALLOW_COPY_AND_ASSIGN(TypeName)
58*8f0ba417SAndroid Build Coastguard Worker
59*8f0ba417SAndroid Build Coastguard Worker // The arraysize(arr) macro returns the # of elements in an array arr.
60*8f0ba417SAndroid Build Coastguard Worker // The expression is a compile-time constant, and therefore can be
61*8f0ba417SAndroid Build Coastguard Worker // used in defining new arrays, for example. If you use arraysize on
62*8f0ba417SAndroid Build Coastguard Worker // a pointer by mistake, you will get a compile-time error.
63*8f0ba417SAndroid Build Coastguard Worker //
64*8f0ba417SAndroid Build Coastguard Worker // One caveat is that arraysize() doesn't accept any array of an
65*8f0ba417SAndroid Build Coastguard Worker // anonymous type or a type defined inside a function. In these rare
66*8f0ba417SAndroid Build Coastguard Worker // cases, you have to use the unsafe ARRAYSIZE_UNSAFE() macro below. This is
67*8f0ba417SAndroid Build Coastguard Worker // due to a limitation in C++'s template system. The limitation might
68*8f0ba417SAndroid Build Coastguard Worker // eventually be removed, but it hasn't happened yet.
69*8f0ba417SAndroid Build Coastguard Worker
70*8f0ba417SAndroid Build Coastguard Worker // This template function declaration is used in defining arraysize.
71*8f0ba417SAndroid Build Coastguard Worker // Note that the function doesn't need an implementation, as we only
72*8f0ba417SAndroid Build Coastguard Worker // use its type.
73*8f0ba417SAndroid Build Coastguard Worker template <typename T, size_t N>
74*8f0ba417SAndroid Build Coastguard Worker char(&ArraySizeHelper(T(&array)[N]))[N]; // NOLINT(readability/casting)
75*8f0ba417SAndroid Build Coastguard Worker
76*8f0ba417SAndroid Build Coastguard Worker #define arraysize(array) (sizeof(ArraySizeHelper(array)))
77*8f0ba417SAndroid Build Coastguard Worker
78*8f0ba417SAndroid Build Coastguard Worker #define SIZEOF_MEMBER(t, f) sizeof(std::declval<t>().f)
79*8f0ba417SAndroid Build Coastguard Worker
80*8f0ba417SAndroid Build Coastguard Worker // Changing this definition will cause you a lot of pain. A majority of
81*8f0ba417SAndroid Build Coastguard Worker // vendor code defines LIKELY and UNLIKELY this way, and includes
82*8f0ba417SAndroid Build Coastguard Worker // this header through an indirect path.
83*8f0ba417SAndroid Build Coastguard Worker #define LIKELY( exp ) (__builtin_expect( (exp) != 0, true ))
84*8f0ba417SAndroid Build Coastguard Worker #define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false ))
85*8f0ba417SAndroid Build Coastguard Worker
86*8f0ba417SAndroid Build Coastguard Worker #define WARN_UNUSED __attribute__((warn_unused_result))
87*8f0ba417SAndroid Build Coastguard Worker
88*8f0ba417SAndroid Build Coastguard Worker // A deprecated function to call to create a false use of the parameter, for
89*8f0ba417SAndroid Build Coastguard Worker // example:
90*8f0ba417SAndroid Build Coastguard Worker // int foo(int x) { UNUSED(x); return 10; }
91*8f0ba417SAndroid Build Coastguard Worker // to avoid compiler warnings. Going forward we prefer ATTRIBUTE_UNUSED.
92*8f0ba417SAndroid Build Coastguard Worker template <typename... T>
UNUSED(const T &...)93*8f0ba417SAndroid Build Coastguard Worker void UNUSED(const T&...) {
94*8f0ba417SAndroid Build Coastguard Worker }
95*8f0ba417SAndroid Build Coastguard Worker
96*8f0ba417SAndroid Build Coastguard Worker // An attribute to place on a parameter to a function, for example:
97*8f0ba417SAndroid Build Coastguard Worker // int foo(int x ATTRIBUTE_UNUSED) { return 10; }
98*8f0ba417SAndroid Build Coastguard Worker // to avoid compiler warnings.
99*8f0ba417SAndroid Build Coastguard Worker #define ATTRIBUTE_UNUSED __attribute__((__unused__))
100*8f0ba417SAndroid Build Coastguard Worker
101*8f0ba417SAndroid Build Coastguard Worker // The FALLTHROUGH_INTENDED macro can be used to annotate implicit fall-through
102*8f0ba417SAndroid Build Coastguard Worker // between switch labels:
103*8f0ba417SAndroid Build Coastguard Worker // switch (x) {
104*8f0ba417SAndroid Build Coastguard Worker // case 40:
105*8f0ba417SAndroid Build Coastguard Worker // case 41:
106*8f0ba417SAndroid Build Coastguard Worker // if (truth_is_out_there) {
107*8f0ba417SAndroid Build Coastguard Worker // ++x;
108*8f0ba417SAndroid Build Coastguard Worker // FALLTHROUGH_INTENDED; // Use instead of/along with annotations in
109*8f0ba417SAndroid Build Coastguard Worker // // comments.
110*8f0ba417SAndroid Build Coastguard Worker // } else {
111*8f0ba417SAndroid Build Coastguard Worker // return x;
112*8f0ba417SAndroid Build Coastguard Worker // }
113*8f0ba417SAndroid Build Coastguard Worker // case 42:
114*8f0ba417SAndroid Build Coastguard Worker // ...
115*8f0ba417SAndroid Build Coastguard Worker //
116*8f0ba417SAndroid Build Coastguard Worker // As shown in the example above, the FALLTHROUGH_INTENDED macro should be
117*8f0ba417SAndroid Build Coastguard Worker // followed by a semicolon. It is designed to mimic control-flow statements
118*8f0ba417SAndroid Build Coastguard Worker // like 'break;', so it can be placed in most places where 'break;' can, but
119*8f0ba417SAndroid Build Coastguard Worker // only if there are no statements on the execution path between it and the
120*8f0ba417SAndroid Build Coastguard Worker // next switch label.
121*8f0ba417SAndroid Build Coastguard Worker //
122*8f0ba417SAndroid Build Coastguard Worker // When compiled with clang, the FALLTHROUGH_INTENDED macro is expanded to
123*8f0ba417SAndroid Build Coastguard Worker // [[clang::fallthrough]] attribute, which is analysed when performing switch
124*8f0ba417SAndroid Build Coastguard Worker // labels fall-through diagnostic ('-Wimplicit-fallthrough'). See clang
125*8f0ba417SAndroid Build Coastguard Worker // documentation on language extensions for details:
126*8f0ba417SAndroid Build Coastguard Worker // http://clang.llvm.org/docs/LanguageExtensions.html#clang__fallthrough
127*8f0ba417SAndroid Build Coastguard Worker //
128*8f0ba417SAndroid Build Coastguard Worker // When used with unsupported compilers, the FALLTHROUGH_INTENDED macro has no
129*8f0ba417SAndroid Build Coastguard Worker // effect on diagnostics.
130*8f0ba417SAndroid Build Coastguard Worker //
131*8f0ba417SAndroid Build Coastguard Worker // In either case this macro has no effect on runtime behavior and performance
132*8f0ba417SAndroid Build Coastguard Worker // of code.
133*8f0ba417SAndroid Build Coastguard Worker #ifndef FALLTHROUGH_INTENDED
134*8f0ba417SAndroid Build Coastguard Worker #define FALLTHROUGH_INTENDED [[fallthrough]] // NOLINT
135*8f0ba417SAndroid Build Coastguard Worker #endif
136*8f0ba417SAndroid Build Coastguard Worker
137*8f0ba417SAndroid Build Coastguard Worker // Current ABI string
138*8f0ba417SAndroid Build Coastguard Worker #if defined(__arm__)
139*8f0ba417SAndroid Build Coastguard Worker #define ABI_STRING "arm"
140*8f0ba417SAndroid Build Coastguard Worker #elif defined(__aarch64__)
141*8f0ba417SAndroid Build Coastguard Worker #define ABI_STRING "arm64"
142*8f0ba417SAndroid Build Coastguard Worker #elif defined(__i386__)
143*8f0ba417SAndroid Build Coastguard Worker #define ABI_STRING "x86"
144*8f0ba417SAndroid Build Coastguard Worker #elif defined(__riscv)
145*8f0ba417SAndroid Build Coastguard Worker #define ABI_STRING "riscv64"
146*8f0ba417SAndroid Build Coastguard Worker #elif defined(__x86_64__)
147*8f0ba417SAndroid Build Coastguard Worker #define ABI_STRING "x86_64"
148*8f0ba417SAndroid Build Coastguard Worker #endif
149