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