Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | */ |
Carl Shapiro | 6c21dc1 | 2011-06-20 15:20:52 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_BASE_MACROS_H_ |
| 18 | #define ART_RUNTIME_BASE_MACROS_H_ |
Carl Shapiro | 6c21dc1 | 2011-06-20 15:20:52 -0700 | [diff] [blame] | 19 | |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 20 | #include <stddef.h> // for size_t |
| 21 | |
Brian Carlstrom | 7a00a3c | 2012-01-25 18:38:03 -0800 | [diff] [blame] | 22 | #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) |
Shih-wei Liao | 24782c6 | 2012-01-08 12:46:11 -0800 | [diff] [blame] | 23 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 24 | // C++11 final and override keywords that were introduced in GCC version 4.7. |
| 25 | #if GCC_VERSION >= 40700 |
| 26 | #define OVERRIDE override |
| 27 | #define FINAL final |
| 28 | #else |
| 29 | #define OVERRIDE |
| 30 | #define FINAL |
| 31 | #endif |
| 32 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 33 | // The COMPILE_ASSERT macro can be used to verify that a compile time |
| 34 | // expression is true. For example, you could use it to verify the |
| 35 | // size of a static array: |
| 36 | // |
| 37 | // COMPILE_ASSERT(ARRAYSIZE(content_type_names) == CONTENT_NUM_TYPES, |
| 38 | // content_type_names_incorrect_size); |
| 39 | // |
| 40 | // or to make sure a struct is smaller than a certain size: |
| 41 | // |
| 42 | // COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large); |
| 43 | // |
| 44 | // The second argument to the macro is the name of the variable. If |
| 45 | // the expression is false, most compilers will issue a warning/error |
| 46 | // containing the name of the variable. |
| 47 | |
| 48 | template <bool> |
| 49 | struct CompileAssert { |
| 50 | }; |
| 51 | |
| 52 | #define COMPILE_ASSERT(expr, msg) \ |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 53 | typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1] // NOLINT |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 54 | |
Carl Shapiro | 6c21dc1 | 2011-06-20 15:20:52 -0700 | [diff] [blame] | 55 | // DISALLOW_COPY_AND_ASSIGN disallows the copy and operator= functions. |
| 56 | // It goes in the private: declarations in a class. |
| 57 | #define DISALLOW_COPY_AND_ASSIGN(TypeName) \ |
| 58 | TypeName(const TypeName&); \ |
| 59 | void operator=(const TypeName&) |
| 60 | |
| 61 | // A macro to disallow all the implicit constructors, namely the |
| 62 | // default constructor, copy constructor and operator= functions. |
| 63 | // |
| 64 | // This should be used in the private: declarations for a class |
| 65 | // that wants to prevent anyone from instantiating it. This is |
| 66 | // especially useful for classes containing only static methods. |
| 67 | #define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \ |
| 68 | TypeName(); \ |
| 69 | DISALLOW_COPY_AND_ASSIGN(TypeName) |
| 70 | |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 71 | // The arraysize(arr) macro returns the # of elements in an array arr. |
| 72 | // The expression is a compile-time constant, and therefore can be |
| 73 | // used in defining new arrays, for example. If you use arraysize on |
| 74 | // a pointer by mistake, you will get a compile-time error. |
| 75 | // |
| 76 | // One caveat is that arraysize() doesn't accept any array of an |
| 77 | // anonymous type or a type defined inside a function. In these rare |
Carl Shapiro | d2bdb57 | 2011-06-22 11:45:37 -0700 | [diff] [blame] | 78 | // cases, you have to use the unsafe ARRAYSIZE_UNSAFE() macro below. This is |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 79 | // due to a limitation in C++'s template system. The limitation might |
| 80 | // eventually be removed, but it hasn't happened yet. |
| 81 | |
| 82 | // This template function declaration is used in defining arraysize. |
| 83 | // Note that the function doesn't need an implementation, as we only |
| 84 | // use its type. |
| 85 | template <typename T, size_t N> |
| 86 | char (&ArraySizeHelper(T (&array)[N]))[N]; |
| 87 | |
| 88 | #define arraysize(array) (sizeof(ArraySizeHelper(array))) |
| 89 | |
Carl Shapiro | d2bdb57 | 2011-06-22 11:45:37 -0700 | [diff] [blame] | 90 | // ARRAYSIZE_UNSAFE performs essentially the same calculation as arraysize, |
| 91 | // but can be used on anonymous types or types defined inside |
| 92 | // functions. It's less safe than arraysize as it accepts some |
| 93 | // (although not all) pointers. Therefore, you should use arraysize |
| 94 | // whenever possible. |
| 95 | // |
| 96 | // The expression ARRAYSIZE_UNSAFE(a) is a compile-time constant of type |
| 97 | // size_t. |
| 98 | // |
| 99 | // ARRAYSIZE_UNSAFE catches a few type errors. If you see a compiler error |
| 100 | // |
| 101 | // "warning: division by zero in ..." |
| 102 | // |
| 103 | // when using ARRAYSIZE_UNSAFE, you are (wrongfully) giving it a pointer. |
| 104 | // You should only use ARRAYSIZE_UNSAFE on statically allocated arrays. |
| 105 | // |
| 106 | // The following comments are on the implementation details, and can |
| 107 | // be ignored by the users. |
| 108 | // |
| 109 | // ARRAYSIZE_UNSAFE(arr) works by inspecting sizeof(arr) (the # of bytes in |
| 110 | // the array) and sizeof(*(arr)) (the # of bytes in one array |
| 111 | // element). If the former is divisible by the latter, perhaps arr is |
| 112 | // indeed an array, in which case the division result is the # of |
| 113 | // elements in the array. Otherwise, arr cannot possibly be an array, |
| 114 | // and we generate a compiler error to prevent the code from |
| 115 | // compiling. |
| 116 | // |
| 117 | // Since the size of bool is implementation-defined, we need to cast |
| 118 | // !(sizeof(a) & sizeof(*(a))) to size_t in order to ensure the final |
| 119 | // result has type size_t. |
| 120 | // |
| 121 | // This macro is not perfect as it wrongfully accepts certain |
| 122 | // pointers, namely where the pointer size is divisible by the pointee |
| 123 | // size. Since all our code has to go through a 32-bit compiler, |
| 124 | // where a pointer is 4 bytes, this means all pointers to a type whose |
| 125 | // size is 3 or greater than 4 will be (righteously) rejected. |
| 126 | #define ARRAYSIZE_UNSAFE(a) \ |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 127 | ((sizeof(a) / sizeof(*(a))) / static_cast<size_t>(!(sizeof(a) % sizeof(*(a))))) |
Carl Shapiro | d2bdb57 | 2011-06-22 11:45:37 -0700 | [diff] [blame] | 128 | |
Elliott Hughes | ff17f1f | 2012-01-24 18:12:29 -0800 | [diff] [blame] | 129 | #define SIZEOF_MEMBER(t, f) sizeof((reinterpret_cast<t*>(4096))->f) |
Carl Shapiro | 59e85cd | 2011-06-21 10:16:23 -0700 | [diff] [blame] | 130 | |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 131 | #define OFFSETOF_MEMBER(t, f) \ |
Ian Rogers | 8a01a3a | 2013-05-06 13:25:44 -0700 | [diff] [blame] | 132 | (reinterpret_cast<const char*>(&reinterpret_cast<t*>(16)->f) - reinterpret_cast<const char*>(16)) // NOLINT |
Carl Shapiro | 59e85cd | 2011-06-21 10:16:23 -0700 | [diff] [blame] | 133 | |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 134 | #define OFFSETOF_VOLATILE_MEMBER(t, f) \ |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 135 | (reinterpret_cast<volatile char*>(&reinterpret_cast<t*>(16)->f) - reinterpret_cast<volatile char*>(16)) // NOLINT |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 136 | |
Brian Carlstrom | b1eba21 | 2013-07-17 18:07:19 -0700 | [diff] [blame] | 137 | #define PACKED(x) __attribute__ ((__aligned__(x), __packed__)) |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 138 | |
Elliott Hughes | ff17f1f | 2012-01-24 18:12:29 -0800 | [diff] [blame] | 139 | #define LIKELY(x) __builtin_expect((x), true) |
| 140 | #define UNLIKELY(x) __builtin_expect((x), false) |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 141 | |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 142 | // Stringify the argument. |
| 143 | #define QUOTE(x) #x |
| 144 | #define STRINGIFY(x) QUOTE(x) |
| 145 | |
Ian Rogers | e8ae0dc | 2013-02-07 10:20:45 -0800 | [diff] [blame] | 146 | #ifndef NDEBUG |
Ian Rogers | 1ffa32f | 2013-02-05 18:29:08 -0800 | [diff] [blame] | 147 | #define ALWAYS_INLINE |
| 148 | #else |
Ian Rogers | e8ae0dc | 2013-02-07 10:20:45 -0800 | [diff] [blame] | 149 | #define ALWAYS_INLINE __attribute__ ((always_inline)) |
Ian Rogers | 1ffa32f | 2013-02-05 18:29:08 -0800 | [diff] [blame] | 150 | #endif |
| 151 | |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 152 | #ifdef __clang__ |
| 153 | /* clang doesn't like attributes on lambda functions */ |
| 154 | #define ALWAYS_INLINE_LAMBDA |
| 155 | #else |
| 156 | #define ALWAYS_INLINE_LAMBDA ALWAYS_INLINE |
| 157 | #endif |
| 158 | |
Anwar Ghuloum | 63937db | 2013-05-24 09:08:32 -0700 | [diff] [blame] | 159 | #if defined (__APPLE__) |
Anwar Ghuloum | 1d9314c | 2013-05-24 10:44:48 -0700 | [diff] [blame] | 160 | #define HOT_ATTR |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 161 | #define COLD_ATTR |
Anwar Ghuloum | 63937db | 2013-05-24 09:08:32 -0700 | [diff] [blame] | 162 | #else |
Anwar Ghuloum | 1d9314c | 2013-05-24 10:44:48 -0700 | [diff] [blame] | 163 | #define HOT_ATTR __attribute__ ((hot)) |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 164 | #define COLD_ATTR __attribute__ ((cold)) |
Anwar Ghuloum | 63937db | 2013-05-24 09:08:32 -0700 | [diff] [blame] | 165 | #endif |
| 166 | |
Ian Rogers | 96faf5b | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 167 | #define PURE __attribute__ ((__pure__)) |
| 168 | |
Elliott Hughes | 74787a3 | 2011-12-16 15:39:48 -0800 | [diff] [blame] | 169 | // bionic and glibc both have TEMP_FAILURE_RETRY, but Mac OS' libc doesn't. |
| 170 | #ifndef TEMP_FAILURE_RETRY |
| 171 | #define TEMP_FAILURE_RETRY(exp) ({ \ |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 172 | typeof(exp) _rc; \ |
Elliott Hughes | 74787a3 | 2011-12-16 15:39:48 -0800 | [diff] [blame] | 173 | do { \ |
| 174 | _rc = (exp); \ |
| 175 | } while (_rc == -1 && errno == EINTR); \ |
| 176 | _rc; }) |
| 177 | #endif |
| 178 | |
Elliott Hughes | c151f90 | 2012-06-21 20:33:21 -0700 | [diff] [blame] | 179 | template<typename T> void UNUSED(const T&) {} |
| 180 | |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame^] | 181 | // Annotalysis thread-safety analysis support. |
| 182 | #if defined(__SUPPORT_TS_ANNOTATION__) || defined(__clang__) |
| 183 | #define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x)) |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 184 | #else |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame^] | 185 | #define THREAD_ANNOTATION_ATTRIBUTE__(x) // no-op |
| 186 | #endif |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 187 | |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame^] | 188 | #define ACQUIRED_AFTER(...) THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(__VA_ARGS__)) |
| 189 | #define ACQUIRED_BEFORE(...) THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(__VA_ARGS__)) |
| 190 | #define EXCLUSIVE_LOCKS_REQUIRED(...) THREAD_ANNOTATION_ATTRIBUTE__(exclusive_locks_required(__VA_ARGS__)) |
| 191 | #define GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x)) |
| 192 | #define GUARDED_VAR THREAD_ANNOTATION_ATTRIBUTE__(guarded) |
| 193 | #define LOCKABLE THREAD_ANNOTATION_ATTRIBUTE__(lockable) |
| 194 | #define LOCK_RETURNED(x) THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x)) |
| 195 | #define LOCKS_EXCLUDED(...) THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__)) |
| 196 | #define NO_THREAD_SAFETY_ANALYSIS THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis) |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 197 | #define PT_GUARDED_BY(x) |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame^] | 198 | // THREAD_ANNOTATION_ATTRIBUTE__(point_to_guarded_by(x)) |
| 199 | #define PT_GUARDED_VAR THREAD_ANNOTATION_ATTRIBUTE__(point_to_guarded) |
| 200 | #define SCOPED_LOCKABLE THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable) |
| 201 | #define SHARED_LOCKS_REQUIRED(...) THREAD_ANNOTATION_ATTRIBUTE__(shared_locks_required(__VA_ARGS__)) |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 202 | |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame^] | 203 | #if defined(__clang__) |
| 204 | #define EXCLUSIVE_LOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(exclusive_lock_function(__VA_ARGS__)) |
| 205 | #define EXCLUSIVE_TRYLOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(exclusive_trylock_function(__VA_ARGS__)) |
| 206 | #define SHARED_LOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(shared_lock_function(__VA_ARGS__)) |
| 207 | #define SHARED_TRYLOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(shared_trylock_function(__VA_ARGS__)) |
| 208 | #define UNLOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(unlock_function(__VA_ARGS__)) |
| 209 | #else |
| 210 | #define EXCLUSIVE_LOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(exclusive_lock(__VA_ARGS__)) |
| 211 | #define EXCLUSIVE_TRYLOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(exclusive_trylock(__VA_ARGS__)) |
| 212 | #define SHARED_LOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(shared_lock(__VA_ARGS__)) |
| 213 | #define SHARED_TRYLOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(shared_trylock(__VA_ARGS__)) |
| 214 | #define UNLOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(unlock(__VA_ARGS__)) |
| 215 | #endif |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 216 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 217 | #endif // ART_RUNTIME_BASE_MACROS_H_ |