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