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_RUNTIME_BASE_MACROS_H_
18 #define ART_RUNTIME_BASE_MACROS_H_
19 
20 #include <stddef.h>  // for size_t
21 #include <unistd.h>  // for TEMP_FAILURE_RETRY
22 
23 #include "android-base/macros.h"
24 #include "android-base/thread_annotations.h"
25 
26 #define OVERRIDE override
27 #define FINAL final
28 
29 // Declare a friend relationship in a class with a test. Used rather that FRIEND_TEST to avoid
30 // globally importing gtest/gtest.h into the main ART header files.
31 #define ART_FRIEND_TEST(test_set_name, individual_test)\
32 friend class test_set_name##_##individual_test##_Test
33 
34 // Declare a friend relationship in a class with a typed test.
35 #define ART_FRIEND_TYPED_TEST(test_set_name, individual_test)\
36 template<typename T> ART_FRIEND_TEST(test_set_name, individual_test)
37 
38 // A macro to disallow new and delete operators for a class. It goes in the private: declarations.
39 // NOTE: Providing placement new (and matching delete) for constructing container elements.
40 #define DISALLOW_ALLOCATION() \
41   public: \
42     NO_RETURN ALWAYS_INLINE void operator delete(void*, size_t) { UNREACHABLE(); } \
43     ALWAYS_INLINE void* operator new(size_t, void* ptr) noexcept { return ptr; } \
44     ALWAYS_INLINE void operator delete(void*, void*) noexcept { } \
45   private: \
46     void* operator new(size_t) = delete  // NOLINT
47 
48 #define SIZEOF_MEMBER(t, f) sizeof((reinterpret_cast<t*>(4096))->f)  // NOLINT
49 
50 #define OFFSETOF_MEMBER(t, f) \
51   (reinterpret_cast<uintptr_t>(&reinterpret_cast<t*>(16)->f) - static_cast<uintptr_t>(16u))  // NOLINT
52 
53 #define OFFSETOF_MEMBERPTR(t, f) \
54   (reinterpret_cast<uintptr_t>(&(reinterpret_cast<t*>(16)->*f)) - static_cast<uintptr_t>(16))  // NOLINT
55 
56 #define PACKED(x) __attribute__ ((__aligned__(x), __packed__))
57 
58 // Stringify the argument.
59 #define QUOTE(x) #x
60 #define STRINGIFY(x) QUOTE(x)
61 
62 #ifndef NDEBUG
63 #define ALWAYS_INLINE
64 #else
65 #define ALWAYS_INLINE  __attribute__ ((always_inline))
66 #endif
67 
68 // clang doesn't like attributes on lambda functions. It would be nice to say:
69 //   #define ALWAYS_INLINE_LAMBDA ALWAYS_INLINE
70 #define ALWAYS_INLINE_LAMBDA
71 
72 #define NO_INLINE __attribute__ ((noinline))
73 
74 #if defined (__APPLE__)
75 #define HOT_ATTR
76 #define COLD_ATTR
77 #else
78 #define HOT_ATTR __attribute__ ((hot))
79 #define COLD_ATTR __attribute__ ((cold))
80 #endif
81 
82 #define PURE __attribute__ ((__pure__))
83 
84 // Define that a position within code is unreachable, for example:
85 //   int foo () { LOG(FATAL) << "Don't call me"; UNREACHABLE(); }
86 // without the UNREACHABLE a return statement would be necessary.
87 #define UNREACHABLE  __builtin_unreachable
88 
89 // Add the C++11 noreturn attribute.
90 #define NO_RETURN [[ noreturn ]]  // NOLINT[whitespace/braces] [5]
91 
92 // Annotalysis thread-safety analysis support. Things that are not in base.
93 
94 #define LOCKABLE CAPABILITY("mutex")
95 #define SHARED_LOCKABLE SHARED_CAPABILITY("mutex")
96 
97 #endif  // ART_RUNTIME_BASE_MACROS_H_
98