xref: /aosp_15_r20/art/libartbase/base/macros.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2010 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #ifndef ART_LIBARTBASE_BASE_MACROS_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_LIBARTBASE_BASE_MACROS_H_
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include <stddef.h>  // for size_t
21*795d594fSAndroid Build Coastguard Worker #include <unistd.h>  // for TEMP_FAILURE_RETRY
22*795d594fSAndroid Build Coastguard Worker 
23*795d594fSAndroid Build Coastguard Worker #include "android-base/format.h"
24*795d594fSAndroid Build Coastguard Worker #include "android-base/macros.h"
25*795d594fSAndroid Build Coastguard Worker #include "android-base/thread_annotations.h"
26*795d594fSAndroid Build Coastguard Worker 
27*795d594fSAndroid Build Coastguard Worker // Declare a friend relationship in a class with a test. Used rather that FRIEND_TEST to avoid
28*795d594fSAndroid Build Coastguard Worker // globally importing gtest/gtest.h into the main ART header files.
29*795d594fSAndroid Build Coastguard Worker #define ART_FRIEND_TEST(test_set_name, individual_test)\
30*795d594fSAndroid Build Coastguard Worker friend class test_set_name##_##individual_test##_Test
31*795d594fSAndroid Build Coastguard Worker 
32*795d594fSAndroid Build Coastguard Worker // Declare a friend relationship in a class with a typed test.
33*795d594fSAndroid Build Coastguard Worker #define ART_FRIEND_TYPED_TEST(test_set_name, individual_test)\
34*795d594fSAndroid Build Coastguard Worker template<typename T> ART_FRIEND_TEST(test_set_name, individual_test)
35*795d594fSAndroid Build Coastguard Worker 
36*795d594fSAndroid Build Coastguard Worker // Shorthand for formatting with compile time checking of the format string
37*795d594fSAndroid Build Coastguard Worker #define ART_FORMAT(str, ...) ::fmt::format(FMT_STRING(str), __VA_ARGS__)
38*795d594fSAndroid Build Coastguard Worker 
39*795d594fSAndroid Build Coastguard Worker // A macro to disallow new and delete operators for a class. It goes in the private: declarations.
40*795d594fSAndroid Build Coastguard Worker // NOTE: Providing placement new (and matching delete) for constructing container elements.
41*795d594fSAndroid Build Coastguard Worker #define DISALLOW_ALLOCATION() \
42*795d594fSAndroid Build Coastguard Worker   public: \
43*795d594fSAndroid Build Coastguard Worker     NO_RETURN ALWAYS_INLINE void operator delete(void*, size_t) { UNREACHABLE(); } \
44*795d594fSAndroid Build Coastguard Worker     ALWAYS_INLINE void* operator new(size_t, void* ptr) noexcept { return ptr; } \
45*795d594fSAndroid Build Coastguard Worker     ALWAYS_INLINE void operator delete(void*, void*) noexcept { } \
46*795d594fSAndroid Build Coastguard Worker   private: \
47*795d594fSAndroid Build Coastguard Worker     void* operator new(size_t) = delete  // NOLINT
48*795d594fSAndroid Build Coastguard Worker 
49*795d594fSAndroid Build Coastguard Worker // offsetof is not defined by the spec on types with non-standard layout,
50*795d594fSAndroid Build Coastguard Worker // however it is implemented by compilers in practice.
51*795d594fSAndroid Build Coastguard Worker // (note that reinterpret_cast is not valid constexpr)
52*795d594fSAndroid Build Coastguard Worker //
53*795d594fSAndroid Build Coastguard Worker // Alternative approach would be something like:
54*795d594fSAndroid Build Coastguard Worker // #define OFFSETOF_HELPER(t, f) \
55*795d594fSAndroid Build Coastguard Worker //   (reinterpret_cast<uintptr_t>(&reinterpret_cast<t*>(16)->f) - static_cast<uintptr_t>(16u))
56*795d594fSAndroid Build Coastguard Worker // #define OFFSETOF_MEMBER(t, f) \
57*795d594fSAndroid Build Coastguard Worker //   (__builtin_constant_p(OFFSETOF_HELPER(t,f)) ? OFFSETOF_HELPER(t,f) : OFFSETOF_HELPER(t,f))
58*795d594fSAndroid Build Coastguard Worker #define OFFSETOF_MEMBER(t, f) offsetof(t, f)
59*795d594fSAndroid Build Coastguard Worker 
60*795d594fSAndroid Build Coastguard Worker #define OFFSETOF_MEMBERPTR(t, f) \
61*795d594fSAndroid Build Coastguard Worker   (reinterpret_cast<uintptr_t>(&(reinterpret_cast<t*>(16)->*f)) - static_cast<uintptr_t>(16))  // NOLINT
62*795d594fSAndroid Build Coastguard Worker 
63*795d594fSAndroid Build Coastguard Worker #define ALIGNED(x) __attribute__ ((__aligned__(x)))
64*795d594fSAndroid Build Coastguard Worker #define PACKED(x) __attribute__ ((__aligned__(x), __packed__))
65*795d594fSAndroid Build Coastguard Worker 
66*795d594fSAndroid Build Coastguard Worker // Stringify the argument.
67*795d594fSAndroid Build Coastguard Worker #define QUOTE(x) #x
68*795d594fSAndroid Build Coastguard Worker #define STRINGIFY(x) QUOTE(x)
69*795d594fSAndroid Build Coastguard Worker 
70*795d594fSAndroid Build Coastguard Worker // Append tokens after evaluating.
71*795d594fSAndroid Build Coastguard Worker #define APPEND_TOKENS_AFTER_EVAL_2(a, b) a ## b
72*795d594fSAndroid Build Coastguard Worker #define APPEND_TOKENS_AFTER_EVAL(a, b) APPEND_TOKENS_AFTER_EVAL_2(a, b)
73*795d594fSAndroid Build Coastguard Worker 
74*795d594fSAndroid Build Coastguard Worker #ifndef NDEBUG
75*795d594fSAndroid Build Coastguard Worker #define ALWAYS_INLINE
76*795d594fSAndroid Build Coastguard Worker #define FLATTEN
77*795d594fSAndroid Build Coastguard Worker #else
78*795d594fSAndroid Build Coastguard Worker #define ALWAYS_INLINE  __attribute__ ((always_inline))
79*795d594fSAndroid Build Coastguard Worker #define FLATTEN  __attribute__ ((flatten))
80*795d594fSAndroid Build Coastguard Worker #endif
81*795d594fSAndroid Build Coastguard Worker 
82*795d594fSAndroid Build Coastguard Worker #define NO_STACK_PROTECTOR __attribute__ ((no_stack_protector))
83*795d594fSAndroid Build Coastguard Worker 
84*795d594fSAndroid Build Coastguard Worker // clang doesn't like attributes on lambda functions. It would be nice to say:
85*795d594fSAndroid Build Coastguard Worker //   #define ALWAYS_INLINE_LAMBDA ALWAYS_INLINE
86*795d594fSAndroid Build Coastguard Worker #define ALWAYS_INLINE_LAMBDA
87*795d594fSAndroid Build Coastguard Worker 
88*795d594fSAndroid Build Coastguard Worker #define NO_INLINE __attribute__ ((noinline))
89*795d594fSAndroid Build Coastguard Worker 
90*795d594fSAndroid Build Coastguard Worker #if defined (__APPLE__)
91*795d594fSAndroid Build Coastguard Worker #define HOT_ATTR
92*795d594fSAndroid Build Coastguard Worker #define COLD_ATTR
93*795d594fSAndroid Build Coastguard Worker #else
94*795d594fSAndroid Build Coastguard Worker #define HOT_ATTR __attribute__ ((hot))
95*795d594fSAndroid Build Coastguard Worker #define COLD_ATTR __attribute__ ((cold))
96*795d594fSAndroid Build Coastguard Worker #endif
97*795d594fSAndroid Build Coastguard Worker 
98*795d594fSAndroid Build Coastguard Worker #define PURE __attribute__ ((__pure__))
99*795d594fSAndroid Build Coastguard Worker 
100*795d594fSAndroid Build Coastguard Worker // Define that a position within code is unreachable, for example:
101*795d594fSAndroid Build Coastguard Worker //   int foo () { LOG(FATAL) << "Don't call me"; UNREACHABLE(); }
102*795d594fSAndroid Build Coastguard Worker // without the UNREACHABLE a return statement would be necessary.
103*795d594fSAndroid Build Coastguard Worker #define UNREACHABLE  __builtin_unreachable
104*795d594fSAndroid Build Coastguard Worker 
105*795d594fSAndroid Build Coastguard Worker // Add the C++11 noreturn attribute.
106*795d594fSAndroid Build Coastguard Worker #define NO_RETURN [[ noreturn ]]  // NOLINT[whitespace/braces] [5]
107*795d594fSAndroid Build Coastguard Worker 
108*795d594fSAndroid Build Coastguard Worker // Annotalysis thread-safety analysis support. Things that are not in base.
109*795d594fSAndroid Build Coastguard Worker 
110*795d594fSAndroid Build Coastguard Worker #define LOCKABLE CAPABILITY("mutex")
111*795d594fSAndroid Build Coastguard Worker #define SHARED_LOCKABLE SHARED_CAPABILITY("mutex")
112*795d594fSAndroid Build Coastguard Worker 
113*795d594fSAndroid Build Coastguard Worker // Some of the libs (e.g. libarttest(d)) require more public symbols when built
114*795d594fSAndroid Build Coastguard Worker // in debug configuration.
115*795d594fSAndroid Build Coastguard Worker // Using symbol visibility only for release builds allows to reduce the list of
116*795d594fSAndroid Build Coastguard Worker // exported symbols and eliminates the need to check debug build configurations
117*795d594fSAndroid Build Coastguard Worker // when changing the exported symbols.
118*795d594fSAndroid Build Coastguard Worker #ifdef NDEBUG
119*795d594fSAndroid Build Coastguard Worker #define HIDDEN __attribute__((visibility("hidden")))
120*795d594fSAndroid Build Coastguard Worker #define PROTECTED __attribute__((visibility("protected")))
121*795d594fSAndroid Build Coastguard Worker #define EXPORT __attribute__((visibility("default")))
122*795d594fSAndroid Build Coastguard Worker #else
123*795d594fSAndroid Build Coastguard Worker #define HIDDEN
124*795d594fSAndroid Build Coastguard Worker #define PROTECTED
125*795d594fSAndroid Build Coastguard Worker #define EXPORT
126*795d594fSAndroid Build Coastguard Worker #endif
127*795d594fSAndroid Build Coastguard Worker 
128*795d594fSAndroid Build Coastguard Worker // Protected symbols must be declared with "protected" visibility attribute when
129*795d594fSAndroid Build Coastguard Worker // building the library and "default" visibility when referred to from external
130*795d594fSAndroid Build Coastguard Worker // libraries/binaries. Otherwise, the external code will expect the symbol to be
131*795d594fSAndroid Build Coastguard Worker // defined locally and fail to link.
132*795d594fSAndroid Build Coastguard Worker #ifdef BUILDING_LIBART
133*795d594fSAndroid Build Coastguard Worker #define LIBART_PROTECTED PROTECTED
134*795d594fSAndroid Build Coastguard Worker #else
135*795d594fSAndroid Build Coastguard Worker #define LIBART_PROTECTED EXPORT
136*795d594fSAndroid Build Coastguard Worker #endif
137*795d594fSAndroid Build Coastguard Worker 
138*795d594fSAndroid Build Coastguard Worker // Some global variables shouldn't be visible outside libraries declaring them.
139*795d594fSAndroid Build Coastguard Worker // The attribute allows hiding them, so preventing direct access.
140*795d594fSAndroid Build Coastguard Worker #define ALWAYS_HIDDEN __attribute__((visibility("hidden")))
141*795d594fSAndroid Build Coastguard Worker 
142*795d594fSAndroid Build Coastguard Worker #endif  // ART_LIBARTBASE_BASE_MACROS_H_
143