Dan Albert | 76d9cad | 2015-03-16 10:09:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Elliott Hughes | 54c72aa | 2016-03-23 15:04:52 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_BASE_MACROS_H |
| 18 | #define ANDROID_BASE_MACROS_H |
Dan Albert | 76d9cad | 2015-03-16 10:09:07 -0700 | [diff] [blame] | 19 | |
| 20 | #include <stddef.h> // for size_t |
| 21 | #include <unistd.h> // for TEMP_FAILURE_RETRY |
| 22 | |
| 23 | // bionic and glibc both have TEMP_FAILURE_RETRY, but eg Mac OS' libc doesn't. |
| 24 | #ifndef TEMP_FAILURE_RETRY |
| 25 | #define TEMP_FAILURE_RETRY(exp) \ |
| 26 | ({ \ |
| 27 | decltype(exp) _rc; \ |
| 28 | do { \ |
| 29 | _rc = (exp); \ |
| 30 | } while (_rc == -1 && errno == EINTR); \ |
| 31 | _rc; \ |
| 32 | }) |
| 33 | #endif |
| 34 | |
| 35 | // A macro to disallow the copy constructor and operator= functions |
| 36 | // This must be placed in the private: declarations for a class. |
| 37 | // |
| 38 | // For disallowing only assign or copy, delete the relevant operator or |
| 39 | // constructor, for example: |
| 40 | // void operator=(const TypeName&) = delete; |
| 41 | // Note, that most uses of DISALLOW_ASSIGN and DISALLOW_COPY are broken |
| 42 | // semantically, one should either use disallow both or neither. Try to |
| 43 | // avoid these in new code. |
| 44 | // |
| 45 | // When building with C++11 toolchains, just use the language support |
| 46 | // for explicitly deleted methods. |
| 47 | #if __cplusplus >= 201103L |
| 48 | #define DISALLOW_COPY_AND_ASSIGN(TypeName) \ |
| 49 | TypeName(const TypeName&) = delete; \ |
| 50 | void operator=(const TypeName&) = delete |
| 51 | #else |
| 52 | #define DISALLOW_COPY_AND_ASSIGN(TypeName) \ |
| 53 | TypeName(const TypeName&); \ |
| 54 | void operator=(const TypeName&) |
| 55 | #endif |
| 56 | |
| 57 | // A macro to disallow all the implicit constructors, namely the |
| 58 | // default constructor, copy constructor and operator= functions. |
| 59 | // |
| 60 | // This should be used in the private: declarations for a class |
| 61 | // that wants to prevent anyone from instantiating it. This is |
| 62 | // especially useful for classes containing only static methods. |
| 63 | #define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \ |
| 64 | TypeName(); \ |
| 65 | DISALLOW_COPY_AND_ASSIGN(TypeName) |
| 66 | |
| 67 | // The arraysize(arr) macro returns the # of elements in an array arr. |
| 68 | // The expression is a compile-time constant, and therefore can be |
| 69 | // used in defining new arrays, for example. If you use arraysize on |
| 70 | // a pointer by mistake, you will get a compile-time error. |
| 71 | // |
| 72 | // One caveat is that arraysize() doesn't accept any array of an |
| 73 | // anonymous type or a type defined inside a function. In these rare |
| 74 | // cases, you have to use the unsafe ARRAYSIZE_UNSAFE() macro below. This is |
| 75 | // due to a limitation in C++'s template system. The limitation might |
| 76 | // eventually be removed, but it hasn't happened yet. |
| 77 | |
| 78 | // This template function declaration is used in defining arraysize. |
| 79 | // Note that the function doesn't need an implementation, as we only |
| 80 | // use its type. |
| 81 | template <typename T, size_t N> |
| 82 | char(&ArraySizeHelper(T(&array)[N]))[N]; // NOLINT(readability/casting) |
| 83 | |
| 84 | #define arraysize(array) (sizeof(ArraySizeHelper(array))) |
| 85 | |
| 86 | // ARRAYSIZE_UNSAFE performs essentially the same calculation as arraysize, |
| 87 | // but can be used on anonymous types or types defined inside |
| 88 | // functions. It's less safe than arraysize as it accepts some |
| 89 | // (although not all) pointers. Therefore, you should use arraysize |
| 90 | // whenever possible. |
| 91 | // |
| 92 | // The expression ARRAYSIZE_UNSAFE(a) is a compile-time constant of type |
| 93 | // size_t. |
| 94 | // |
| 95 | // ARRAYSIZE_UNSAFE catches a few type errors. If you see a compiler error |
| 96 | // |
| 97 | // "warning: division by zero in ..." |
| 98 | // |
| 99 | // when using ARRAYSIZE_UNSAFE, you are (wrongfully) giving it a pointer. |
| 100 | // You should only use ARRAYSIZE_UNSAFE on statically allocated arrays. |
| 101 | // |
| 102 | // The following comments are on the implementation details, and can |
| 103 | // be ignored by the users. |
| 104 | // |
| 105 | // ARRAYSIZE_UNSAFE(arr) works by inspecting sizeof(arr) (the # of bytes in |
| 106 | // the array) and sizeof(*(arr)) (the # of bytes in one array |
| 107 | // element). If the former is divisible by the latter, perhaps arr is |
| 108 | // indeed an array, in which case the division result is the # of |
| 109 | // elements in the array. Otherwise, arr cannot possibly be an array, |
| 110 | // and we generate a compiler error to prevent the code from |
| 111 | // compiling. |
| 112 | // |
| 113 | // Since the size of bool is implementation-defined, we need to cast |
| 114 | // !(sizeof(a) & sizeof(*(a))) to size_t in order to ensure the final |
| 115 | // result has type size_t. |
| 116 | // |
| 117 | // This macro is not perfect as it wrongfully accepts certain |
| 118 | // pointers, namely where the pointer size is divisible by the pointee |
| 119 | // size. Since all our code has to go through a 32-bit compiler, |
| 120 | // where a pointer is 4 bytes, this means all pointers to a type whose |
| 121 | // size is 3 or greater than 4 will be (righteously) rejected. |
| 122 | #define ARRAYSIZE_UNSAFE(a) \ |
| 123 | ((sizeof(a) / sizeof(*(a))) / \ |
| 124 | static_cast<size_t>(!(sizeof(a) % sizeof(*(a))))) |
| 125 | |
Christopher Wiley | ec9ea66 | 2016-04-18 16:08:19 -0700 | [diff] [blame] | 126 | // Changing this definition will cause you a lot of pain. A majority of |
| 127 | // vendor code defines LIKELY and UNLIKELY this way, and includes |
| 128 | // this header through an indirect path. |
| 129 | #define LIKELY( exp ) (__builtin_expect( (exp) != 0, true )) |
| 130 | #define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false )) |
Dan Albert | 76d9cad | 2015-03-16 10:09:07 -0700 | [diff] [blame] | 131 | |
| 132 | #define WARN_UNUSED __attribute__((warn_unused_result)) |
| 133 | |
| 134 | // A deprecated function to call to create a false use of the parameter, for |
| 135 | // example: |
| 136 | // int foo(int x) { UNUSED(x); return 10; } |
| 137 | // to avoid compiler warnings. Going forward we prefer ATTRIBUTE_UNUSED. |
| 138 | template <typename... T> |
| 139 | void UNUSED(const T&...) { |
| 140 | } |
| 141 | |
| 142 | // An attribute to place on a parameter to a function, for example: |
| 143 | // int foo(int x ATTRIBUTE_UNUSED) { return 10; } |
| 144 | // to avoid compiler warnings. |
| 145 | #define ATTRIBUTE_UNUSED __attribute__((__unused__)) |
| 146 | |
| 147 | // The FALLTHROUGH_INTENDED macro can be used to annotate implicit fall-through |
| 148 | // between switch labels: |
| 149 | // switch (x) { |
| 150 | // case 40: |
| 151 | // case 41: |
| 152 | // if (truth_is_out_there) { |
| 153 | // ++x; |
| 154 | // FALLTHROUGH_INTENDED; // Use instead of/along with annotations in |
| 155 | // // comments. |
| 156 | // } else { |
| 157 | // return x; |
| 158 | // } |
| 159 | // case 42: |
| 160 | // ... |
| 161 | // |
| 162 | // As shown in the example above, the FALLTHROUGH_INTENDED macro should be |
| 163 | // followed by a semicolon. It is designed to mimic control-flow statements |
| 164 | // like 'break;', so it can be placed in most places where 'break;' can, but |
| 165 | // only if there are no statements on the execution path between it and the |
| 166 | // next switch label. |
| 167 | // |
| 168 | // When compiled with clang in C++11 mode, the FALLTHROUGH_INTENDED macro is |
| 169 | // expanded to [[clang::fallthrough]] attribute, which is analysed when |
| 170 | // performing switch labels fall-through diagnostic ('-Wimplicit-fallthrough'). |
| 171 | // See clang documentation on language extensions for details: |
| 172 | // http://clang.llvm.org/docs/LanguageExtensions.html#clang__fallthrough |
| 173 | // |
| 174 | // When used with unsupported compilers, the FALLTHROUGH_INTENDED macro has no |
| 175 | // effect on diagnostics. |
| 176 | // |
| 177 | // In either case this macro has no effect on runtime behavior and performance |
| 178 | // of code. |
| 179 | #if defined(__clang__) && __cplusplus >= 201103L && defined(__has_warning) |
| 180 | #if __has_feature(cxx_attributes) && __has_warning("-Wimplicit-fallthrough") |
| 181 | #define FALLTHROUGH_INTENDED [[clang::fallthrough]] // NOLINT |
| 182 | #endif |
| 183 | #endif |
| 184 | |
| 185 | #ifndef FALLTHROUGH_INTENDED |
| 186 | #define FALLTHROUGH_INTENDED \ |
| 187 | do { \ |
| 188 | } while (0) |
| 189 | #endif |
| 190 | |
Elliott Hughes | 54c72aa | 2016-03-23 15:04:52 -0700 | [diff] [blame] | 191 | #endif // ANDROID_BASE_MACROS_H |