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> |
| 21 | |
Mathieu Chartier | 3f7f03c | 2016-09-26 11:39:52 -0700 | [diff] [blame] | 22 | #include "base/mutex.h" // For Locks::mutator_lock_. |
| 23 | #include "globals.h" |
| 24 | #include "mirror/object_reference.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 = |
| 34 | sizeof(mirror::HeapReference<mirror::Object>) * kBitsPerByte - kObjectAlignmentShift; |
| 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_) |
| 48 | : reference_(Encode(static_cast<MirrorType*>(ptr))) {} |
| 49 | |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 50 | template <typename Type> |
| 51 | ALWAYS_INLINE ObjPtr(const ObjPtr<Type>& other) REQUIRES_SHARED(Locks::mutator_lock_) |
| 52 | : reference_(Encode(static_cast<MirrorType*>(other.Decode()))) {} |
Mathieu Chartier | 3f7f03c | 2016-09-26 11:39:52 -0700 | [diff] [blame] | 53 | |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 54 | template <typename Type> |
Mathieu Chartier | 3f7f03c | 2016-09-26 11:39:52 -0700 | [diff] [blame] | 55 | ALWAYS_INLINE ObjPtr& operator=(const ObjPtr& other) { |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 56 | reference_ = Encode(static_cast<MirrorType*>(other.Decode())); |
Mathieu Chartier | 3f7f03c | 2016-09-26 11:39:52 -0700 | [diff] [blame] | 57 | return *this; |
| 58 | } |
| 59 | |
| 60 | ALWAYS_INLINE ObjPtr& operator=(MirrorType* ptr) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 61 | Assign(ptr); |
| 62 | return *this; |
| 63 | } |
| 64 | |
| 65 | ALWAYS_INLINE void Assign(MirrorType* ptr) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 66 | reference_ = Encode(ptr); |
| 67 | } |
| 68 | |
| 69 | ALWAYS_INLINE MirrorType* operator->() const REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 3f7f03c | 2016-09-26 11:39:52 -0700 | [diff] [blame] | 70 | return Decode(); |
| 71 | } |
| 72 | |
| 73 | ALWAYS_INLINE bool IsNull() const { |
| 74 | return reference_ == 0; |
| 75 | } |
| 76 | |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 77 | // Decode makes sure that the object pointer is valid. |
| 78 | ALWAYS_INLINE MirrorType* Decode() const REQUIRES_SHARED(Locks::mutator_lock_) { |
| 79 | AssertValid(); |
| 80 | return DecodeUnchecked(); |
Mathieu Chartier | 3f7f03c | 2016-09-26 11:39:52 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 83 | ALWAYS_INLINE bool IsValid() const REQUIRES_SHARED(Locks::mutator_lock_); |
| 84 | |
| 85 | ALWAYS_INLINE void AssertValid() const REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 3f7f03c | 2016-09-26 11:39:52 -0700 | [diff] [blame] | 86 | |
| 87 | ALWAYS_INLINE bool operator==(const ObjPtr& ptr) const REQUIRES_SHARED(Locks::mutator_lock_) { |
| 88 | return Decode() == ptr.Decode(); |
| 89 | } |
| 90 | |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame^] | 91 | template <typename PointerType> |
| 92 | ALWAYS_INLINE bool operator==(const PointerType* ptr) const |
| 93 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 3f7f03c | 2016-09-26 11:39:52 -0700 | [diff] [blame] | 94 | return Decode() == ptr; |
| 95 | } |
| 96 | |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame^] | 97 | ALWAYS_INLINE bool operator==(std::nullptr_t) const { |
Mathieu Chartier | 3f7f03c | 2016-09-26 11:39:52 -0700 | [diff] [blame] | 98 | return IsNull(); |
| 99 | } |
| 100 | |
| 101 | ALWAYS_INLINE bool operator!=(const ObjPtr& ptr) const REQUIRES_SHARED(Locks::mutator_lock_) { |
| 102 | return Decode() != ptr.Decode(); |
| 103 | } |
| 104 | |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame^] | 105 | template <typename PointerType> |
| 106 | ALWAYS_INLINE bool operator!=(const PointerType* ptr) const |
| 107 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 3f7f03c | 2016-09-26 11:39:52 -0700 | [diff] [blame] | 108 | return Decode() != ptr; |
| 109 | } |
| 110 | |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame^] | 111 | ALWAYS_INLINE bool operator!=(std::nullptr_t) const { |
Mathieu Chartier | 3f7f03c | 2016-09-26 11:39:52 -0700 | [diff] [blame] | 112 | return !IsNull(); |
| 113 | } |
| 114 | |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 115 | // Decode unchecked does not check that object pointer is valid. Do not use if you can avoid it. |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame^] | 116 | ALWAYS_INLINE MirrorType* DecodeUnchecked() const { |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 117 | if (kPoison) { |
| 118 | return reinterpret_cast<MirrorType*>( |
| 119 | static_cast<uintptr_t>(static_cast<uint32_t>(reference_ << kObjectAlignmentShift))); |
| 120 | } else { |
| 121 | return reinterpret_cast<MirrorType*>(reference_); |
| 122 | } |
| 123 | } |
| 124 | |
Mathieu Chartier | 3f7f03c | 2016-09-26 11:39:52 -0700 | [diff] [blame] | 125 | private: |
| 126 | // Trim off high bits of thread local cookie. |
| 127 | ALWAYS_INLINE static uintptr_t TrimCookie(uintptr_t cookie) { |
| 128 | return cookie & kCookieMask; |
| 129 | } |
| 130 | |
| 131 | ALWAYS_INLINE uintptr_t GetCookie() const { |
| 132 | return reference_ >> kCookieShift; |
| 133 | } |
| 134 | |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 135 | 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] | 136 | // The encoded reference and cookie. |
| 137 | uintptr_t reference_; |
| 138 | }; |
| 139 | |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame^] | 140 | template<class MirrorType, bool kPoison, typename PointerType> |
| 141 | ALWAYS_INLINE bool operator==(const PointerType* a, const ObjPtr<MirrorType, kPoison>& b) |
| 142 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 143 | return b == a; |
| 144 | } |
| 145 | |
| 146 | template<class MirrorType, bool kPoison> |
| 147 | ALWAYS_INLINE bool operator==(std::nullptr_t, const ObjPtr<MirrorType, kPoison>& b) { |
| 148 | return b == nullptr; |
| 149 | } |
| 150 | |
| 151 | template<typename MirrorType, bool kPoison, typename PointerType> |
| 152 | ALWAYS_INLINE bool operator!=(const PointerType* a, const ObjPtr<MirrorType, kPoison>& b) |
| 153 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 154 | return b != a; |
| 155 | } |
| 156 | |
| 157 | template<class MirrorType, bool kPoison> |
| 158 | ALWAYS_INLINE bool operator!=(std::nullptr_t, const ObjPtr<MirrorType, kPoison>& b) { |
| 159 | return b != nullptr; |
| 160 | } |
| 161 | |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 162 | template<class MirrorType, bool kPoison = kIsDebugBuild> |
| 163 | static inline ObjPtr<MirrorType, kPoison> MakeObjPtr(MirrorType* ptr) { |
| 164 | return ObjPtr<MirrorType, kPoison>(ptr); |
| 165 | } |
Mathieu Chartier | 3f7f03c | 2016-09-26 11:39:52 -0700 | [diff] [blame] | 166 | |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame^] | 167 | template<class MirrorType, bool kPoison = kIsDebugBuild> |
| 168 | static inline ObjPtr<MirrorType, kPoison> MakeObjPtr(ObjPtr<MirrorType, kPoison> ptr) { |
| 169 | return ObjPtr<MirrorType, kPoison>(ptr); |
| 170 | } |
| 171 | |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 172 | template<class MirrorType, bool kPoison> |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame^] | 173 | ALWAYS_INLINE std::ostream& operator<<(std::ostream& os, ObjPtr<MirrorType, kPoison> ptr); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 174 | |
Mathieu Chartier | 3f7f03c | 2016-09-26 11:39:52 -0700 | [diff] [blame] | 175 | } // namespace art |
| 176 | |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 177 | #endif // ART_RUNTIME_OBJ_PTR_H_ |