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 | |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 17 | #ifndef ART_LIBARTBASE_BASE_MACROS_H_ |
| 18 | #define ART_LIBARTBASE_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 |
Mark Salyzyn | 47a4cc7 | 2014-05-22 16:27:06 -0700 | [diff] [blame] | 21 | #include <unistd.h> // for TEMP_FAILURE_RETRY |
| 22 | |
Andreas Gampe | aaadff8 | 2016-08-29 09:53:48 -0700 | [diff] [blame] | 23 | #include "android-base/macros.h" |
Andreas Gampe | d38374e | 2016-08-31 13:53:13 -0700 | [diff] [blame] | 24 | #include "android-base/thread_annotations.h" |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 25 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 26 | // Declare a friend relationship in a class with a test. Used rather that FRIEND_TEST to avoid |
| 27 | // globally importing gtest/gtest.h into the main ART header files. |
| 28 | #define ART_FRIEND_TEST(test_set_name, individual_test)\ |
| 29 | friend class test_set_name##_##individual_test##_Test |
| 30 | |
Zheng Xu | ad4450e | 2015-04-17 18:48:56 +0800 | [diff] [blame] | 31 | // Declare a friend relationship in a class with a typed test. |
| 32 | #define ART_FRIEND_TYPED_TEST(test_set_name, individual_test)\ |
| 33 | template<typename T> ART_FRIEND_TEST(test_set_name, individual_test) |
| 34 | |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 35 | // A macro to disallow new and delete operators for a class. It goes in the private: declarations. |
Vladimir Marko | 76c92ac | 2015-09-17 15:39:16 +0100 | [diff] [blame] | 36 | // NOTE: Providing placement new (and matching delete) for constructing container elements. |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 37 | #define DISALLOW_ALLOCATION() \ |
| 38 | public: \ |
Andreas Gampe | 65b798e | 2015-04-06 09:35:22 -0700 | [diff] [blame] | 39 | NO_RETURN ALWAYS_INLINE void operator delete(void*, size_t) { UNREACHABLE(); } \ |
Vladimir Marko | 76c92ac | 2015-09-17 15:39:16 +0100 | [diff] [blame] | 40 | ALWAYS_INLINE void* operator new(size_t, void* ptr) noexcept { return ptr; } \ |
| 41 | ALWAYS_INLINE void operator delete(void*, void*) noexcept { } \ |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 42 | private: \ |
Roland Levillain | 7cbd27f | 2016-08-11 23:53:33 +0100 | [diff] [blame] | 43 | void* operator new(size_t) = delete // NOLINT |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 44 | |
David Srbecky | 56de89a | 2018-10-01 15:32:20 +0100 | [diff] [blame] | 45 | // offsetof is not defined by the spec on types with non-standard layout, |
| 46 | // however it is implemented by compilers in practice. |
| 47 | // (note that reinterpret_cast is not valid constexpr) |
| 48 | // |
| 49 | // Alternative approach would be something like: |
| 50 | // #define OFFSETOF_HELPER(t, f) \ |
| 51 | // (reinterpret_cast<uintptr_t>(&reinterpret_cast<t*>(16)->f) - static_cast<uintptr_t>(16u)) |
| 52 | // #define OFFSETOF_MEMBER(t, f) \ |
| 53 | // (__builtin_constant_p(OFFSETOF_HELPER(t,f)) ? OFFSETOF_HELPER(t,f) : OFFSETOF_HELPER(t,f)) |
| 54 | #define OFFSETOF_MEMBER(t, f) offsetof(t, f) |
Carl Shapiro | 59e85cd | 2011-06-21 10:16:23 -0700 | [diff] [blame] | 55 | |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 56 | #define OFFSETOF_MEMBERPTR(t, f) \ |
Roland Levillain | 7cbd27f | 2016-08-11 23:53:33 +0100 | [diff] [blame] | 57 | (reinterpret_cast<uintptr_t>(&(reinterpret_cast<t*>(16)->*f)) - static_cast<uintptr_t>(16)) // NOLINT |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 58 | |
David Srbecky | 912f36c | 2018-09-08 12:22:58 +0100 | [diff] [blame] | 59 | #define ALIGNED(x) __attribute__ ((__aligned__(x))) |
Brian Carlstrom | b1eba21 | 2013-07-17 18:07:19 -0700 | [diff] [blame] | 60 | #define PACKED(x) __attribute__ ((__aligned__(x), __packed__)) |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 61 | |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 62 | // Stringify the argument. |
| 63 | #define QUOTE(x) #x |
| 64 | #define STRINGIFY(x) QUOTE(x) |
| 65 | |
Mathieu Chartier | a7f6b81 | 2017-12-11 13:34:29 -0800 | [diff] [blame] | 66 | // Append tokens after evaluating. |
| 67 | #define APPEND_TOKENS_AFTER_EVAL_2(a, b) a ## b |
| 68 | #define APPEND_TOKENS_AFTER_EVAL(a, b) APPEND_TOKENS_AFTER_EVAL_2(a, b) |
| 69 | |
Ian Rogers | e8ae0dc | 2013-02-07 10:20:45 -0800 | [diff] [blame] | 70 | #ifndef NDEBUG |
Ian Rogers | 1ffa32f | 2013-02-05 18:29:08 -0800 | [diff] [blame] | 71 | #define ALWAYS_INLINE |
David Srbecky | 27351be | 2019-07-12 13:39:34 +0100 | [diff] [blame] | 72 | #define FLATTEN |
Ian Rogers | 1ffa32f | 2013-02-05 18:29:08 -0800 | [diff] [blame] | 73 | #else |
Christian Wailes | e8efdaa | 2021-05-26 17:33:54 +0000 | [diff] [blame] | 74 | #define ALWAYS_INLINE __attribute__ ((always_inline)) |
David Srbecky | 27351be | 2019-07-12 13:39:34 +0100 | [diff] [blame] | 75 | #define FLATTEN __attribute__ ((flatten)) |
Ian Rogers | 1ffa32f | 2013-02-05 18:29:08 -0800 | [diff] [blame] | 76 | #endif |
| 77 | |
Andreas Gampe | 9231f4e | 2016-08-23 17:35:19 -0700 | [diff] [blame] | 78 | // clang doesn't like attributes on lambda functions. It would be nice to say: |
| 79 | // #define ALWAYS_INLINE_LAMBDA ALWAYS_INLINE |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 80 | #define ALWAYS_INLINE_LAMBDA |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 81 | |
Andreas Gampe | 8683038 | 2014-12-12 21:41:29 -0800 | [diff] [blame] | 82 | #define NO_INLINE __attribute__ ((noinline)) |
| 83 | |
Anwar Ghuloum | 63937db | 2013-05-24 09:08:32 -0700 | [diff] [blame] | 84 | #if defined (__APPLE__) |
Anwar Ghuloum | 1d9314c | 2013-05-24 10:44:48 -0700 | [diff] [blame] | 85 | #define HOT_ATTR |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 86 | #define COLD_ATTR |
Anwar Ghuloum | 63937db | 2013-05-24 09:08:32 -0700 | [diff] [blame] | 87 | #else |
Anwar Ghuloum | 1d9314c | 2013-05-24 10:44:48 -0700 | [diff] [blame] | 88 | #define HOT_ATTR __attribute__ ((hot)) |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 89 | #define COLD_ATTR __attribute__ ((cold)) |
Anwar Ghuloum | 63937db | 2013-05-24 09:08:32 -0700 | [diff] [blame] | 90 | #endif |
| 91 | |
Ian Rogers | 96faf5b | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 92 | #define PURE __attribute__ ((__pure__)) |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 93 | |
| 94 | // Define that a position within code is unreachable, for example: |
| 95 | // int foo () { LOG(FATAL) << "Don't call me"; UNREACHABLE(); } |
| 96 | // without the UNREACHABLE a return statement would be necessary. |
Ian Rogers | 0714083 | 2014-09-30 15:43:59 -0700 | [diff] [blame] | 97 | #define UNREACHABLE __builtin_unreachable |
Elliott Hughes | c151f90 | 2012-06-21 20:33:21 -0700 | [diff] [blame] | 98 | |
Andreas Gampe | 794ad76 | 2015-02-23 08:12:24 -0800 | [diff] [blame] | 99 | // Add the C++11 noreturn attribute. |
| 100 | #define NO_RETURN [[ noreturn ]] // NOLINT[whitespace/braces] [5] |
| 101 | |
Andreas Gampe | d38374e | 2016-08-31 13:53:13 -0700 | [diff] [blame] | 102 | // Annotalysis thread-safety analysis support. Things that are not in base. |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 103 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 104 | #define LOCKABLE CAPABILITY("mutex") |
| 105 | #define SHARED_LOCKABLE SHARED_CAPABILITY("mutex") |
| 106 | |
Vladimir Marko | 8ffaef9 | 2021-05-13 12:51:30 +0000 | [diff] [blame] | 107 | #define HIDDEN __attribute__((visibility("hidden"))) |
| 108 | #define EXPORT __attribute__((visibility("default"))) |
| 109 | |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 110 | #endif // ART_LIBARTBASE_BASE_MACROS_H_ |