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 | |
| 91 | ALWAYS_INLINE bool operator==(const MirrorType* ptr) const REQUIRES_SHARED(Locks::mutator_lock_) { |
| 92 | return Decode() == ptr; |
| 93 | } |
| 94 | |
| 95 | ALWAYS_INLINE bool operator==(std::nullptr_t) const REQUIRES_SHARED(Locks::mutator_lock_) { |
| 96 | return IsNull(); |
| 97 | } |
| 98 | |
| 99 | ALWAYS_INLINE bool operator!=(const ObjPtr& ptr) const REQUIRES_SHARED(Locks::mutator_lock_) { |
| 100 | return Decode() != ptr.Decode(); |
| 101 | } |
| 102 | |
| 103 | ALWAYS_INLINE bool operator!=(const MirrorType* ptr) const REQUIRES_SHARED(Locks::mutator_lock_) { |
| 104 | return Decode() != ptr; |
| 105 | } |
| 106 | |
| 107 | ALWAYS_INLINE bool operator!=(std::nullptr_t) const REQUIRES_SHARED(Locks::mutator_lock_) { |
| 108 | return !IsNull(); |
| 109 | } |
| 110 | |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame^] | 111 | // Decode unchecked does not check that object pointer is valid. Do not use if you can avoid it. |
| 112 | ALWAYS_INLINE MirrorType* DecodeUnchecked() const REQUIRES_SHARED(Locks::mutator_lock_) { |
| 113 | if (kPoison) { |
| 114 | return reinterpret_cast<MirrorType*>( |
| 115 | static_cast<uintptr_t>(static_cast<uint32_t>(reference_ << kObjectAlignmentShift))); |
| 116 | } else { |
| 117 | return reinterpret_cast<MirrorType*>(reference_); |
| 118 | } |
| 119 | } |
| 120 | |
Mathieu Chartier | 3f7f03c | 2016-09-26 11:39:52 -0700 | [diff] [blame] | 121 | private: |
| 122 | // Trim off high bits of thread local cookie. |
| 123 | ALWAYS_INLINE static uintptr_t TrimCookie(uintptr_t cookie) { |
| 124 | return cookie & kCookieMask; |
| 125 | } |
| 126 | |
| 127 | ALWAYS_INLINE uintptr_t GetCookie() const { |
| 128 | return reference_ >> kCookieShift; |
| 129 | } |
| 130 | |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 131 | 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] | 132 | // The encoded reference and cookie. |
| 133 | uintptr_t reference_; |
| 134 | }; |
| 135 | |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 136 | template<class MirrorType, bool kPoison = kIsDebugBuild> |
| 137 | static inline ObjPtr<MirrorType, kPoison> MakeObjPtr(MirrorType* ptr) { |
| 138 | return ObjPtr<MirrorType, kPoison>(ptr); |
| 139 | } |
Mathieu Chartier | 3f7f03c | 2016-09-26 11:39:52 -0700 | [diff] [blame] | 140 | |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame^] | 141 | template<class MirrorType, bool kPoison> |
| 142 | ALWAYS_INLINE std::ostream& operator<<(std::ostream& os, ObjPtr<MirrorType, kPoison> ptr) |
| 143 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 144 | |
Mathieu Chartier | 3f7f03c | 2016-09-26 11:39:52 -0700 | [diff] [blame] | 145 | } // namespace art |
| 146 | |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 147 | #endif // ART_RUNTIME_OBJ_PTR_H_ |