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