Mathieu Chartier | 3f7f03c | 2016-09-26 11:39:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_OBJ_PTR_H_ |
| 18 | #define ART_RUNTIME_OBJ_PTR_H_ |
Mathieu Chartier | 3f7f03c | 2016-09-26 11:39:52 -0700 | [diff] [blame] | 19 | |
Andreas Gampe | 8764dc3 | 2019-01-07 15:20:12 -0800 | [diff] [blame] | 20 | #include <iosfwd> |
Mathieu Chartier | f8ac97f | 2016-10-05 15:56:52 -0700 | [diff] [blame] | 21 | #include <type_traits> |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 22 | |
Andreas Gampe | 7fbc4a5 | 2018-11-28 08:26:47 -0800 | [diff] [blame] | 23 | #include "base/locks.h" // For Locks::mutator_lock_. |
Andreas Gampe | 52edc85 | 2016-11-03 15:46:34 -0700 | [diff] [blame] | 24 | #include "base/macros.h" |
Andreas Gampe | 5a0430d | 2019-01-04 14:33:57 -0800 | [diff] [blame] | 25 | #include "runtime_globals.h" |
Mathieu Chartier | 3f7f03c | 2016-09-26 11:39:52 -0700 | [diff] [blame] | 26 | |
David Srbecky | 3f704c0 | 2018-11-16 17:05:15 +0000 | [diff] [blame] | 27 | // Always inline ObjPtr methods even in debug builds. |
| 28 | #define OBJPTR_INLINE __attribute__ ((always_inline)) |
| 29 | |
Mathieu Chartier | 3f7f03c | 2016-09-26 11:39:52 -0700 | [diff] [blame] | 30 | namespace art { |
Mathieu Chartier | 3f7f03c | 2016-09-26 11:39:52 -0700 | [diff] [blame] | 31 | |
Andreas Gampe | c73cb64 | 2017-02-22 10:11:30 -0800 | [diff] [blame] | 32 | constexpr bool kObjPtrPoisoning = kIsDebugBuild; |
| 33 | |
Andreas Gampe | 81e8938 | 2017-10-11 21:52:42 -0700 | [diff] [blame] | 34 | // It turns out that most of the performance overhead comes from copying. Don't validate for now. |
| 35 | // This defers finding stale ObjPtr objects until they are used. |
| 36 | constexpr bool kObjPtrPoisoningValidateOnCopy = false; |
| 37 | |
Mathieu Chartier | 3f7f03c | 2016-09-26 11:39:52 -0700 | [diff] [blame] | 38 | // Value type representing a pointer to a mirror::Object of type MirrorType |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 39 | // Since the cookie is thread based, it is not safe to share an ObjPtr between threads. |
Andreas Gampe | c73cb64 | 2017-02-22 10:11:30 -0800 | [diff] [blame] | 40 | template<class MirrorType> |
Mathieu Chartier | 3f7f03c | 2016-09-26 11:39:52 -0700 | [diff] [blame] | 41 | class ObjPtr { |
| 42 | static constexpr size_t kCookieShift = |
Evgenii Stepanov | 797ffe5 | 2018-01-09 16:21:46 -0800 | [diff] [blame] | 43 | kHeapReferenceSize * kBitsPerByte - kObjectAlignmentShift; |
Mathieu Chartier | 3f7f03c | 2016-09-26 11:39:52 -0700 | [diff] [blame] | 44 | static constexpr size_t kCookieBits = sizeof(uintptr_t) * kBitsPerByte - kCookieShift; |
| 45 | static constexpr uintptr_t kCookieMask = (static_cast<uintptr_t>(1u) << kCookieBits) - 1; |
| 46 | |
| 47 | static_assert(kCookieBits >= kObjectAlignmentShift, |
| 48 | "must have a least kObjectAlignmentShift bits"); |
| 49 | |
| 50 | public: |
David Srbecky | 3f704c0 | 2018-11-16 17:05:15 +0000 | [diff] [blame] | 51 | OBJPTR_INLINE ObjPtr() REQUIRES_SHARED(Locks::mutator_lock_) : reference_(0u) {} |
Mathieu Chartier | 3f7f03c | 2016-09-26 11:39:52 -0700 | [diff] [blame] | 52 | |
Andreas Gampe | 52edc85 | 2016-11-03 15:46:34 -0700 | [diff] [blame] | 53 | // Note: The following constructors allow implicit conversion. This simplifies code that uses |
| 54 | // them, e.g., for parameter passing. However, in general, implicit-conversion constructors |
Igor Murashkin | 5573c37 | 2017-11-16 13:34:30 -0800 | [diff] [blame] | 55 | // are discouraged and detected by clang-tidy. |
Andreas Gampe | 52edc85 | 2016-11-03 15:46:34 -0700 | [diff] [blame] | 56 | |
David Srbecky | 3f704c0 | 2018-11-16 17:05:15 +0000 | [diff] [blame] | 57 | OBJPTR_INLINE ObjPtr(std::nullptr_t) |
Andreas Gampe | 52edc85 | 2016-11-03 15:46:34 -0700 | [diff] [blame] | 58 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 59 | : reference_(0u) {} |
Mathieu Chartier | 3f7f03c | 2016-09-26 11:39:52 -0700 | [diff] [blame] | 60 | |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 61 | template <typename Type, |
Vladimir Marko | f57f2ed | 2019-03-25 15:55:42 +0000 | [diff] [blame] | 62 | typename = typename std::enable_if_t<std::is_base_of_v<MirrorType, Type>>> |
| 63 | OBJPTR_INLINE ObjPtr(Type* ptr) REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 64 | |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 65 | template <typename Type, |
Vladimir Marko | f57f2ed | 2019-03-25 15:55:42 +0000 | [diff] [blame] | 66 | typename = typename std::enable_if_t<std::is_base_of_v<MirrorType, Type>>> |
| 67 | OBJPTR_INLINE ObjPtr(const ObjPtr<Type>& other) REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 3f7f03c | 2016-09-26 11:39:52 -0700 | [diff] [blame] | 68 | |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 69 | template <typename Type, |
Vladimir Marko | f57f2ed | 2019-03-25 15:55:42 +0000 | [diff] [blame] | 70 | typename = typename std::enable_if_t<std::is_base_of_v<MirrorType, Type>>> |
| 71 | OBJPTR_INLINE ObjPtr& operator=(const ObjPtr<Type>& other) REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 3f7f03c | 2016-09-26 11:39:52 -0700 | [diff] [blame] | 72 | |
Vladimir Marko | f57f2ed | 2019-03-25 15:55:42 +0000 | [diff] [blame] | 73 | OBJPTR_INLINE ObjPtr& operator=(MirrorType* ptr) REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 3f7f03c | 2016-09-26 11:39:52 -0700 | [diff] [blame] | 74 | |
Vladimir Marko | f57f2ed | 2019-03-25 15:55:42 +0000 | [diff] [blame] | 75 | OBJPTR_INLINE void Assign(MirrorType* ptr) REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 3f7f03c | 2016-09-26 11:39:52 -0700 | [diff] [blame] | 76 | |
Vladimir Marko | f57f2ed | 2019-03-25 15:55:42 +0000 | [diff] [blame] | 77 | OBJPTR_INLINE MirrorType* operator->() const REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 3f7f03c | 2016-09-26 11:39:52 -0700 | [diff] [blame] | 78 | |
David Srbecky | 3f704c0 | 2018-11-16 17:05:15 +0000 | [diff] [blame] | 79 | OBJPTR_INLINE bool IsNull() const { |
Mathieu Chartier | 3f7f03c | 2016-09-26 11:39:52 -0700 | [diff] [blame] | 80 | return reference_ == 0; |
| 81 | } |
| 82 | |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 83 | // Ptr makes sure that the object pointer is valid. |
Vladimir Marko | f57f2ed | 2019-03-25 15:55:42 +0000 | [diff] [blame] | 84 | OBJPTR_INLINE MirrorType* Ptr() const REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 3f7f03c | 2016-09-26 11:39:52 -0700 | [diff] [blame] | 85 | |
David Srbecky | 3f704c0 | 2018-11-16 17:05:15 +0000 | [diff] [blame] | 86 | OBJPTR_INLINE bool IsValid() const REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 87 | |
David Srbecky | 3f704c0 | 2018-11-16 17:05:15 +0000 | [diff] [blame] | 88 | OBJPTR_INLINE void AssertValid() const REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 3f7f03c | 2016-09-26 11:39:52 -0700 | [diff] [blame] | 89 | |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 90 | // Ptr unchecked does not check that object pointer is valid. Do not use if you can avoid it. |
David Srbecky | 3f704c0 | 2018-11-16 17:05:15 +0000 | [diff] [blame] | 91 | OBJPTR_INLINE MirrorType* PtrUnchecked() const { |
Andreas Gampe | c73cb64 | 2017-02-22 10:11:30 -0800 | [diff] [blame] | 92 | if (kObjPtrPoisoning) { |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 93 | return reinterpret_cast<MirrorType*>( |
| 94 | static_cast<uintptr_t>(static_cast<uint32_t>(reference_ << kObjectAlignmentShift))); |
| 95 | } else { |
| 96 | return reinterpret_cast<MirrorType*>(reference_); |
| 97 | } |
| 98 | } |
| 99 | |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 100 | // Static function to be friendly with null pointers. |
| 101 | template <typename SourceType> |
Vladimir Marko | f57f2ed | 2019-03-25 15:55:42 +0000 | [diff] [blame] | 102 | static ObjPtr<MirrorType> DownCast(ObjPtr<SourceType> ptr) REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 103 | |
Vladimir Marko | d7e9bbf | 2019-03-28 13:18:57 +0000 | [diff] [blame^] | 104 | // Static function to be friendly with null pointers. |
| 105 | template <typename SourceType> |
| 106 | static ObjPtr<MirrorType> DownCast(SourceType* ptr) REQUIRES_SHARED(Locks::mutator_lock_); |
| 107 | |
Mathieu Chartier | 3f7f03c | 2016-09-26 11:39:52 -0700 | [diff] [blame] | 108 | private: |
| 109 | // Trim off high bits of thread local cookie. |
David Srbecky | 3f704c0 | 2018-11-16 17:05:15 +0000 | [diff] [blame] | 110 | OBJPTR_INLINE static uintptr_t GetCurrentTrimedCookie(); |
Mathieu Chartier | 3f7f03c | 2016-09-26 11:39:52 -0700 | [diff] [blame] | 111 | |
David Srbecky | 3f704c0 | 2018-11-16 17:05:15 +0000 | [diff] [blame] | 112 | OBJPTR_INLINE uintptr_t GetCookie() const { |
Mathieu Chartier | 3f7f03c | 2016-09-26 11:39:52 -0700 | [diff] [blame] | 113 | return reference_ >> kCookieShift; |
| 114 | } |
| 115 | |
David Srbecky | 3f704c0 | 2018-11-16 17:05:15 +0000 | [diff] [blame] | 116 | OBJPTR_INLINE static uintptr_t Encode(MirrorType* ptr) REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 3f7f03c | 2016-09-26 11:39:52 -0700 | [diff] [blame] | 117 | // The encoded reference and cookie. |
| 118 | uintptr_t reference_; |
Andreas Gampe | 81e8938 | 2017-10-11 21:52:42 -0700 | [diff] [blame] | 119 | |
| 120 | template <class T> friend class ObjPtr; // Required for reference_ access in copy cons/operator. |
Mathieu Chartier | 3f7f03c | 2016-09-26 11:39:52 -0700 | [diff] [blame] | 121 | }; |
| 122 | |
Andreas Gampe | 52edc85 | 2016-11-03 15:46:34 -0700 | [diff] [blame] | 123 | static_assert(std::is_trivially_copyable<ObjPtr<void>>::value, |
| 124 | "ObjPtr should be trivially copyable"); |
| 125 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 126 | // Hash function for stl data structures. |
| 127 | class HashObjPtr { |
| 128 | public: |
Andreas Gampe | c73cb64 | 2017-02-22 10:11:30 -0800 | [diff] [blame] | 129 | template<class MirrorType> |
Vladimir Marko | f57f2ed | 2019-03-25 15:55:42 +0000 | [diff] [blame] | 130 | size_t operator()(const ObjPtr<MirrorType>& ptr) const NO_THREAD_SAFETY_ANALYSIS; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 131 | }; |
| 132 | |
Vladimir Marko | f57f2ed | 2019-03-25 15:55:42 +0000 | [diff] [blame] | 133 | template<class MirrorType1, class MirrorType2> |
| 134 | OBJPTR_INLINE std::enable_if_t<std::is_base_of_v<MirrorType1, MirrorType2> || |
| 135 | std::is_base_of_v<MirrorType2, MirrorType1>, bool> |
| 136 | operator==(ObjPtr<MirrorType1> lhs, ObjPtr<MirrorType2> rhs) |
| 137 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 138 | |
| 139 | template<class MirrorType1, class MirrorType2> |
| 140 | OBJPTR_INLINE std::enable_if_t<std::is_base_of_v<MirrorType1, MirrorType2> || |
| 141 | std::is_base_of_v<MirrorType2, MirrorType1>, bool> |
| 142 | operator==(const MirrorType1* lhs, ObjPtr<MirrorType2> rhs) |
| 143 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 144 | |
| 145 | template<class MirrorType1, class MirrorType2> |
| 146 | OBJPTR_INLINE std::enable_if_t<std::is_base_of_v<MirrorType1, MirrorType2> || |
| 147 | std::is_base_of_v<MirrorType2, MirrorType1>, bool> |
| 148 | operator==(ObjPtr<MirrorType1> lhs, const MirrorType2* rhs) |
| 149 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 150 | |
| 151 | template<class MirrorType> |
| 152 | OBJPTR_INLINE bool operator==(ObjPtr<MirrorType> ptr, std::nullptr_t) { |
| 153 | return ptr.IsNull(); |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Andreas Gampe | c73cb64 | 2017-02-22 10:11:30 -0800 | [diff] [blame] | 156 | template<class MirrorType> |
Vladimir Marko | f57f2ed | 2019-03-25 15:55:42 +0000 | [diff] [blame] | 157 | OBJPTR_INLINE bool operator==(std::nullptr_t, ObjPtr<MirrorType> ptr) { |
| 158 | return ptr.IsNull(); |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Vladimir Marko | f57f2ed | 2019-03-25 15:55:42 +0000 | [diff] [blame] | 161 | template<class MirrorType1, class MirrorType2> |
| 162 | OBJPTR_INLINE std::enable_if_t<std::is_base_of_v<MirrorType1, MirrorType2> || |
| 163 | std::is_base_of_v<MirrorType2, MirrorType1>, bool> |
| 164 | operator!=(ObjPtr<MirrorType1> lhs, ObjPtr<MirrorType2> rhs) |
| 165 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 166 | |
| 167 | template<class MirrorType1, class MirrorType2> |
| 168 | OBJPTR_INLINE std::enable_if_t<std::is_base_of_v<MirrorType1, MirrorType2> || |
| 169 | std::is_base_of_v<MirrorType2, MirrorType1>, bool> |
| 170 | operator!=(const MirrorType1* lhs, ObjPtr<MirrorType2> rhs) |
| 171 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 172 | |
| 173 | template<class MirrorType1, class MirrorType2> |
| 174 | OBJPTR_INLINE std::enable_if_t<std::is_base_of_v<MirrorType1, MirrorType2> || |
| 175 | std::is_base_of_v<MirrorType2, MirrorType1>, bool> |
| 176 | operator!=(ObjPtr<MirrorType1> lhs, const MirrorType2* rhs) |
| 177 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 178 | |
| 179 | template<class MirrorType> |
| 180 | OBJPTR_INLINE bool operator!=(ObjPtr<MirrorType> ptr, std::nullptr_t) { |
| 181 | return !(ptr == nullptr); |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Andreas Gampe | c73cb64 | 2017-02-22 10:11:30 -0800 | [diff] [blame] | 184 | template<class MirrorType> |
Vladimir Marko | f57f2ed | 2019-03-25 15:55:42 +0000 | [diff] [blame] | 185 | OBJPTR_INLINE bool operator!=(std::nullptr_t, ObjPtr<MirrorType> ptr) { |
| 186 | return !(nullptr == ptr); |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Andreas Gampe | c73cb64 | 2017-02-22 10:11:30 -0800 | [diff] [blame] | 189 | template<class MirrorType> |
David Srbecky | 3f704c0 | 2018-11-16 17:05:15 +0000 | [diff] [blame] | 190 | OBJPTR_INLINE std::ostream& operator<<(std::ostream& os, ObjPtr<MirrorType> ptr); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 191 | |
Mathieu Chartier | 3f7f03c | 2016-09-26 11:39:52 -0700 | [diff] [blame] | 192 | } // namespace art |
| 193 | |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 194 | #endif // ART_RUNTIME_OBJ_PTR_H_ |