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 | |
Krzysztof Kosiński | 737b81f | 2023-04-30 02:59:54 +0000 | [diff] [blame] | 23 | #include "android-base/format.h" |
Andreas Gampe | aaadff8 | 2016-08-29 09:53:48 -0700 | [diff] [blame] | 24 | #include "android-base/macros.h" |
Andreas Gampe | d38374e | 2016-08-31 13:53:13 -0700 | [diff] [blame] | 25 | #include "android-base/thread_annotations.h" |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 26 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 27 | // Declare a friend relationship in a class with a test. Used rather that FRIEND_TEST to avoid |
| 28 | // globally importing gtest/gtest.h into the main ART header files. |
| 29 | #define ART_FRIEND_TEST(test_set_name, individual_test)\ |
| 30 | friend class test_set_name##_##individual_test##_Test |
| 31 | |
Zheng Xu | ad4450e | 2015-04-17 18:48:56 +0800 | [diff] [blame] | 32 | // Declare a friend relationship in a class with a typed test. |
| 33 | #define ART_FRIEND_TYPED_TEST(test_set_name, individual_test)\ |
| 34 | template<typename T> ART_FRIEND_TEST(test_set_name, individual_test) |
| 35 | |
Krzysztof Kosiński | 737b81f | 2023-04-30 02:59:54 +0000 | [diff] [blame] | 36 | // Shorthand for formatting with compile time checking of the format string |
| 37 | #define ART_FORMAT(str, ...) ::fmt::format(FMT_STRING(str), __VA_ARGS__) |
| 38 | |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 39 | // 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] | 40 | // NOTE: Providing placement new (and matching delete) for constructing container elements. |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 41 | #define DISALLOW_ALLOCATION() \ |
| 42 | public: \ |
Andreas Gampe | 65b798e | 2015-04-06 09:35:22 -0700 | [diff] [blame] | 43 | NO_RETURN ALWAYS_INLINE void operator delete(void*, size_t) { UNREACHABLE(); } \ |
Vladimir Marko | 76c92ac | 2015-09-17 15:39:16 +0100 | [diff] [blame] | 44 | ALWAYS_INLINE void* operator new(size_t, void* ptr) noexcept { return ptr; } \ |
| 45 | ALWAYS_INLINE void operator delete(void*, void*) noexcept { } \ |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 46 | private: \ |
Roland Levillain | 7cbd27f | 2016-08-11 23:53:33 +0100 | [diff] [blame] | 47 | void* operator new(size_t) = delete // NOLINT |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 48 | |
David Srbecky | 56de89a | 2018-10-01 15:32:20 +0100 | [diff] [blame] | 49 | // offsetof is not defined by the spec on types with non-standard layout, |
| 50 | // however it is implemented by compilers in practice. |
| 51 | // (note that reinterpret_cast is not valid constexpr) |
| 52 | // |
| 53 | // Alternative approach would be something like: |
| 54 | // #define OFFSETOF_HELPER(t, f) \ |
| 55 | // (reinterpret_cast<uintptr_t>(&reinterpret_cast<t*>(16)->f) - static_cast<uintptr_t>(16u)) |
| 56 | // #define OFFSETOF_MEMBER(t, f) \ |
| 57 | // (__builtin_constant_p(OFFSETOF_HELPER(t,f)) ? OFFSETOF_HELPER(t,f) : OFFSETOF_HELPER(t,f)) |
| 58 | #define OFFSETOF_MEMBER(t, f) offsetof(t, f) |
Carl Shapiro | 59e85cd | 2011-06-21 10:16:23 -0700 | [diff] [blame] | 59 | |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 60 | #define OFFSETOF_MEMBERPTR(t, f) \ |
Roland Levillain | 7cbd27f | 2016-08-11 23:53:33 +0100 | [diff] [blame] | 61 | (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] | 62 | |
David Srbecky | 912f36c | 2018-09-08 12:22:58 +0100 | [diff] [blame] | 63 | #define ALIGNED(x) __attribute__ ((__aligned__(x))) |
Brian Carlstrom | b1eba21 | 2013-07-17 18:07:19 -0700 | [diff] [blame] | 64 | #define PACKED(x) __attribute__ ((__aligned__(x), __packed__)) |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 65 | |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 66 | // Stringify the argument. |
| 67 | #define QUOTE(x) #x |
| 68 | #define STRINGIFY(x) QUOTE(x) |
| 69 | |
Mathieu Chartier | a7f6b81 | 2017-12-11 13:34:29 -0800 | [diff] [blame] | 70 | // Append tokens after evaluating. |
| 71 | #define APPEND_TOKENS_AFTER_EVAL_2(a, b) a ## b |
| 72 | #define APPEND_TOKENS_AFTER_EVAL(a, b) APPEND_TOKENS_AFTER_EVAL_2(a, b) |
| 73 | |
Ian Rogers | e8ae0dc | 2013-02-07 10:20:45 -0800 | [diff] [blame] | 74 | #ifndef NDEBUG |
Ian Rogers | 1ffa32f | 2013-02-05 18:29:08 -0800 | [diff] [blame] | 75 | #define ALWAYS_INLINE |
David Srbecky | 27351be | 2019-07-12 13:39:34 +0100 | [diff] [blame] | 76 | #define FLATTEN |
Ian Rogers | 1ffa32f | 2013-02-05 18:29:08 -0800 | [diff] [blame] | 77 | #else |
Christian Wailes | e8efdaa | 2021-05-26 17:33:54 +0000 | [diff] [blame] | 78 | #define ALWAYS_INLINE __attribute__ ((always_inline)) |
David Srbecky | 27351be | 2019-07-12 13:39:34 +0100 | [diff] [blame] | 79 | #define FLATTEN __attribute__ ((flatten)) |
Ian Rogers | 1ffa32f | 2013-02-05 18:29:08 -0800 | [diff] [blame] | 80 | #endif |
| 81 | |
Chris Wailes | 9f61e0a | 2021-05-12 17:16:50 -0700 | [diff] [blame] | 82 | #define NO_STACK_PROTECTOR __attribute__ ((no_stack_protector)) |
| 83 | |
Andreas Gampe | 9231f4e | 2016-08-23 17:35:19 -0700 | [diff] [blame] | 84 | // clang doesn't like attributes on lambda functions. It would be nice to say: |
| 85 | // #define ALWAYS_INLINE_LAMBDA ALWAYS_INLINE |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 86 | #define ALWAYS_INLINE_LAMBDA |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 87 | |
Andreas Gampe | 8683038 | 2014-12-12 21:41:29 -0800 | [diff] [blame] | 88 | #define NO_INLINE __attribute__ ((noinline)) |
| 89 | |
Anwar Ghuloum | 63937db | 2013-05-24 09:08:32 -0700 | [diff] [blame] | 90 | #if defined (__APPLE__) |
Anwar Ghuloum | 1d9314c | 2013-05-24 10:44:48 -0700 | [diff] [blame] | 91 | #define HOT_ATTR |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 92 | #define COLD_ATTR |
Anwar Ghuloum | 63937db | 2013-05-24 09:08:32 -0700 | [diff] [blame] | 93 | #else |
Anwar Ghuloum | 1d9314c | 2013-05-24 10:44:48 -0700 | [diff] [blame] | 94 | #define HOT_ATTR __attribute__ ((hot)) |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 95 | #define COLD_ATTR __attribute__ ((cold)) |
Anwar Ghuloum | 63937db | 2013-05-24 09:08:32 -0700 | [diff] [blame] | 96 | #endif |
| 97 | |
Ian Rogers | 96faf5b | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 98 | #define PURE __attribute__ ((__pure__)) |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 99 | |
| 100 | // Define that a position within code is unreachable, for example: |
| 101 | // int foo () { LOG(FATAL) << "Don't call me"; UNREACHABLE(); } |
| 102 | // without the UNREACHABLE a return statement would be necessary. |
Ian Rogers | 0714083 | 2014-09-30 15:43:59 -0700 | [diff] [blame] | 103 | #define UNREACHABLE __builtin_unreachable |
Elliott Hughes | c151f90 | 2012-06-21 20:33:21 -0700 | [diff] [blame] | 104 | |
Andreas Gampe | 794ad76 | 2015-02-23 08:12:24 -0800 | [diff] [blame] | 105 | // Add the C++11 noreturn attribute. |
| 106 | #define NO_RETURN [[ noreturn ]] // NOLINT[whitespace/braces] [5] |
| 107 | |
Andreas Gampe | d38374e | 2016-08-31 13:53:13 -0700 | [diff] [blame] | 108 | // Annotalysis thread-safety analysis support. Things that are not in base. |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 109 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 110 | #define LOCKABLE CAPABILITY("mutex") |
| 111 | #define SHARED_LOCKABLE SHARED_CAPABILITY("mutex") |
| 112 | |
Dmitrii Ishcheikin | 5668bf6 | 2023-11-15 00:16:51 +0000 | [diff] [blame] | 113 | // Some of the libs (e.g. libarttest(d)) require more public symbols when built |
| 114 | // in debug configuration. |
| 115 | // Using symbol visibility only for release builds allows to reduce the list of |
| 116 | // exported symbols and eliminates the need to check debug build configurations |
| 117 | // when changing the exported symbols. |
| 118 | #ifdef NDEBUG |
Vladimir Marko | 8ffaef9 | 2021-05-13 12:51:30 +0000 | [diff] [blame] | 119 | #define HIDDEN __attribute__((visibility("hidden"))) |
Dmitrii Ishcheikin | 8cd0209 | 2024-01-30 15:22:45 +0000 | [diff] [blame] | 120 | #define PROTECTED __attribute__((visibility("protected"))) |
Vladimir Marko | 8ffaef9 | 2021-05-13 12:51:30 +0000 | [diff] [blame] | 121 | #define EXPORT __attribute__((visibility("default"))) |
Dmitrii Ishcheikin | 5668bf6 | 2023-11-15 00:16:51 +0000 | [diff] [blame] | 122 | #else |
| 123 | #define HIDDEN |
Dmitrii Ishcheikin | 8cd0209 | 2024-01-30 15:22:45 +0000 | [diff] [blame] | 124 | #define PROTECTED |
Dmitrii Ishcheikin | 5668bf6 | 2023-11-15 00:16:51 +0000 | [diff] [blame] | 125 | #define EXPORT |
| 126 | #endif |
Vladimir Marko | 8ffaef9 | 2021-05-13 12:51:30 +0000 | [diff] [blame] | 127 | |
Dmitrii Ishcheikin | 8cd0209 | 2024-01-30 15:22:45 +0000 | [diff] [blame] | 128 | // Protected symbols must be declared with "protected" visibility attribute when |
| 129 | // building the library and "default" visibility when referred to from external |
| 130 | // libraries/binaries. Otherwise, the external code will expect the symbol to be |
| 131 | // defined locally and fail to link. |
| 132 | #ifdef BUILDING_LIBART |
| 133 | #define LIBART_PROTECTED PROTECTED |
| 134 | #else |
| 135 | #define LIBART_PROTECTED EXPORT |
| 136 | #endif |
| 137 | |
Ruben Ayrapetyan | b343f66 | 2023-11-20 18:09:32 +0000 | [diff] [blame] | 138 | // Some global variables shouldn't be visible outside libraries declaring them. |
| 139 | // The attribute allows hiding them, so preventing direct access. |
| 140 | #define ALWAYS_HIDDEN __attribute__((visibility("hidden"))) |
| 141 | |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 142 | #endif // ART_LIBARTBASE_BASE_MACROS_H_ |