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