Fairphone ODM | 25c12f5 | 2023-12-15 17:24:06 +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 | */ |
| 16 | |
| 17 | #ifndef ART_LIBARTBASE_BASE_MACROS_H_ |
| 18 | #define ART_LIBARTBASE_BASE_MACROS_H_ |
| 19 | |
| 20 | #include <stddef.h> // for size_t |
| 21 | #include <unistd.h> // for TEMP_FAILURE_RETRY |
| 22 | |
android-t2 | 163f240 | 2023-12-18 10:22:00 +0800 | [diff] [blame] | 23 | #include "android-base/format.h" |
Fairphone ODM | 25c12f5 | 2023-12-15 17:24:06 +0800 | [diff] [blame] | 24 | #include "android-base/macros.h" |
| 25 | #include "android-base/thread_annotations.h" |
| 26 | |
| 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 | |
| 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 | |
android-t2 | 163f240 | 2023-12-18 10:22:00 +0800 | [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 | |
Fairphone ODM | 25c12f5 | 2023-12-15 17:24:06 +0800 | [diff] [blame] | 39 | // A macro to disallow new and delete operators for a class. It goes in the private: declarations. |
| 40 | // NOTE: Providing placement new (and matching delete) for constructing container elements. |
| 41 | #define DISALLOW_ALLOCATION() \ |
| 42 | public: \ |
| 43 | NO_RETURN ALWAYS_INLINE void operator delete(void*, size_t) { UNREACHABLE(); } \ |
| 44 | ALWAYS_INLINE void* operator new(size_t, void* ptr) noexcept { return ptr; } \ |
| 45 | ALWAYS_INLINE void operator delete(void*, void*) noexcept { } \ |
| 46 | private: \ |
| 47 | void* operator new(size_t) = delete // NOLINT |
| 48 | |
| 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) |
| 59 | |
| 60 | #define OFFSETOF_MEMBERPTR(t, f) \ |
| 61 | (reinterpret_cast<uintptr_t>(&(reinterpret_cast<t*>(16)->*f)) - static_cast<uintptr_t>(16)) // NOLINT |
| 62 | |
| 63 | #define ALIGNED(x) __attribute__ ((__aligned__(x))) |
| 64 | #define PACKED(x) __attribute__ ((__aligned__(x), __packed__)) |
| 65 | |
| 66 | // Stringify the argument. |
| 67 | #define QUOTE(x) #x |
| 68 | #define STRINGIFY(x) QUOTE(x) |
| 69 | |
| 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 | |
| 74 | #ifndef NDEBUG |
| 75 | #define ALWAYS_INLINE |
| 76 | #define FLATTEN |
| 77 | #else |
| 78 | #define ALWAYS_INLINE __attribute__ ((always_inline)) |
| 79 | #define FLATTEN __attribute__ ((flatten)) |
| 80 | #endif |
| 81 | |
| 82 | #define NO_STACK_PROTECTOR __attribute__ ((no_stack_protector)) |
| 83 | |
| 84 | // clang doesn't like attributes on lambda functions. It would be nice to say: |
| 85 | // #define ALWAYS_INLINE_LAMBDA ALWAYS_INLINE |
| 86 | #define ALWAYS_INLINE_LAMBDA |
| 87 | |
| 88 | #define NO_INLINE __attribute__ ((noinline)) |
| 89 | |
| 90 | #if defined (__APPLE__) |
| 91 | #define HOT_ATTR |
| 92 | #define COLD_ATTR |
| 93 | #else |
| 94 | #define HOT_ATTR __attribute__ ((hot)) |
| 95 | #define COLD_ATTR __attribute__ ((cold)) |
| 96 | #endif |
| 97 | |
| 98 | #define PURE __attribute__ ((__pure__)) |
| 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. |
| 103 | #define UNREACHABLE __builtin_unreachable |
| 104 | |
| 105 | // Add the C++11 noreturn attribute. |
| 106 | #define NO_RETURN [[ noreturn ]] // NOLINT[whitespace/braces] [5] |
| 107 | |
| 108 | // Annotalysis thread-safety analysis support. Things that are not in base. |
| 109 | |
| 110 | #define LOCKABLE CAPABILITY("mutex") |
| 111 | #define SHARED_LOCKABLE SHARED_CAPABILITY("mutex") |
| 112 | |
| 113 | #define HIDDEN __attribute__((visibility("hidden"))) |
| 114 | #define EXPORT __attribute__((visibility("default"))) |
| 115 | |
| 116 | #endif // ART_LIBARTBASE_BASE_MACROS_H_ |