• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define FLATTEN
73 #else
74 #define ALWAYS_INLINE  __attribute__ ((always_inline))
75 #define FLATTEN  __attribute__ ((flatten))
76 #endif
77 
78 #define NO_STACK_PROTECTOR __attribute__ ((no_stack_protector))
79 
80 // clang doesn't like attributes on lambda functions. It would be nice to say:
81 //   #define ALWAYS_INLINE_LAMBDA ALWAYS_INLINE
82 #define ALWAYS_INLINE_LAMBDA
83 
84 #define NO_INLINE __attribute__ ((noinline))
85 
86 #if defined (__APPLE__)
87 #define HOT_ATTR
88 #define COLD_ATTR
89 #else
90 #define HOT_ATTR __attribute__ ((hot))
91 #define COLD_ATTR __attribute__ ((cold))
92 #endif
93 
94 #define PURE __attribute__ ((__pure__))
95 
96 // Define that a position within code is unreachable, for example:
97 //   int foo () { LOG(FATAL) << "Don't call me"; UNREACHABLE(); }
98 // without the UNREACHABLE a return statement would be necessary.
99 #define UNREACHABLE  __builtin_unreachable
100 
101 // Add the C++11 noreturn attribute.
102 #define NO_RETURN [[ noreturn ]]  // NOLINT[whitespace/braces] [5]
103 
104 // Annotalysis thread-safety analysis support. Things that are not in base.
105 
106 #define LOCKABLE CAPABILITY("mutex")
107 #define SHARED_LOCKABLE SHARED_CAPABILITY("mutex")
108 
109 #define HIDDEN __attribute__((visibility("hidden")))
110 #define EXPORT __attribute__((visibility("default")))
111 
112 #endif  // ART_LIBARTBASE_BASE_MACROS_H_
113