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 23 #include "android-base/format.h" 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 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 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 // 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 119 #define HIDDEN __attribute__((visibility("hidden"))) 120 #define PROTECTED __attribute__((visibility("protected"))) 121 #define EXPORT __attribute__((visibility("default"))) 122 #else 123 #define HIDDEN 124 #define PROTECTED 125 #define EXPORT 126 #endif 127 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 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 142 #endif // ART_LIBARTBASE_BASE_MACROS_H_ 143