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