Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -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 | |
| 17 | #ifndef ART_RUNTIME_OBJ_PTR_INL_H_ |
| 18 | #define ART_RUNTIME_OBJ_PTR_INL_H_ |
| 19 | |
Andreas Gampe | 8764dc3 | 2019-01-07 15:20:12 -0800 | [diff] [blame] | 20 | #include <ostream> |
| 21 | |
Andreas Gampe | 3b7dc35 | 2017-06-06 20:02:03 -0700 | [diff] [blame] | 22 | #include "base/bit_utils.h" |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 23 | #include "obj_ptr.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 24 | #include "thread-current-inl.h" |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
Andreas Gampe | c73cb64 | 2017-02-22 10:11:30 -0800 | [diff] [blame] | 28 | template<class MirrorType> |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 29 | inline uintptr_t ObjPtr<MirrorType>::GetCurrentTrimedCookie() { |
| 30 | Thread* self = Thread::Current(); |
| 31 | if (UNLIKELY(self == nullptr)) { |
| 32 | return kCookieMask; |
| 33 | } |
| 34 | return self->GetPoisonObjectCookie() & kCookieMask; |
| 35 | } |
| 36 | |
| 37 | template<class MirrorType> |
Andreas Gampe | c73cb64 | 2017-02-22 10:11:30 -0800 | [diff] [blame] | 38 | inline bool ObjPtr<MirrorType>::IsValid() const { |
| 39 | if (!kObjPtrPoisoning || IsNull()) { |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 40 | return true; |
| 41 | } |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 42 | return GetCookie() == GetCurrentTrimedCookie(); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Andreas Gampe | c73cb64 | 2017-02-22 10:11:30 -0800 | [diff] [blame] | 45 | template<class MirrorType> |
| 46 | inline void ObjPtr<MirrorType>::AssertValid() const { |
| 47 | if (kObjPtrPoisoning) { |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 48 | CHECK(IsValid()) << "Stale object pointer " << PtrUnchecked() << " , expected cookie " |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 49 | << GetCurrentTrimedCookie() << " but got " << GetCookie(); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
Andreas Gampe | c73cb64 | 2017-02-22 10:11:30 -0800 | [diff] [blame] | 53 | template<class MirrorType> |
| 54 | inline uintptr_t ObjPtr<MirrorType>::Encode(MirrorType* ptr) { |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 55 | uintptr_t ref = reinterpret_cast<uintptr_t>(ptr); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 56 | DCHECK_ALIGNED(ref, kObjectAlignment); |
Andreas Gampe | c73cb64 | 2017-02-22 10:11:30 -0800 | [diff] [blame] | 57 | if (kObjPtrPoisoning && ref != 0) { |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 58 | DCHECK_LE(ref, 0xFFFFFFFFU); |
| 59 | ref >>= kObjectAlignmentShift; |
| 60 | // Put cookie in high bits. |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 61 | ref |= GetCurrentTrimedCookie() << kCookieShift; |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 62 | } |
| 63 | return ref; |
| 64 | } |
| 65 | |
Andreas Gampe | c73cb64 | 2017-02-22 10:11:30 -0800 | [diff] [blame] | 66 | template<class MirrorType> |
Vladimir Marko | f57f2ed | 2019-03-25 15:55:42 +0000 | [diff] [blame] | 67 | template <typename Type, |
| 68 | typename /* = typename std::enable_if_t<std::is_base_of_v<MirrorType, Type>> */> |
| 69 | inline ObjPtr<MirrorType>::ObjPtr(Type* ptr) |
| 70 | : reference_(Encode(static_cast<MirrorType*>(ptr))) { |
| 71 | } |
| 72 | |
| 73 | template<class MirrorType> |
| 74 | template <typename Type, |
| 75 | typename /* = typename std::enable_if_t<std::is_base_of_v<MirrorType, Type>> */> |
| 76 | inline ObjPtr<MirrorType>::ObjPtr(const ObjPtr<Type>& other) |
| 77 | : reference_(other.reference_) { |
| 78 | if (kObjPtrPoisoningValidateOnCopy) { |
| 79 | AssertValid(); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | template<class MirrorType> |
| 84 | template <typename Type, |
| 85 | typename /* = typename std::enable_if_t<std::is_base_of_v<MirrorType, Type>> */> |
| 86 | inline ObjPtr<MirrorType>& ObjPtr<MirrorType>::operator=(const ObjPtr<Type>& other) { |
| 87 | reference_ = other.reference_; |
| 88 | if (kObjPtrPoisoningValidateOnCopy) { |
| 89 | AssertValid(); |
| 90 | } |
| 91 | return *this; |
| 92 | } |
| 93 | |
| 94 | template<class MirrorType> |
| 95 | OBJPTR_INLINE ObjPtr<MirrorType>& ObjPtr<MirrorType>::operator=(MirrorType* ptr) { |
| 96 | Assign(ptr); |
| 97 | return *this; |
| 98 | } |
| 99 | |
| 100 | template<class MirrorType> |
| 101 | inline void ObjPtr<MirrorType>::Assign(MirrorType* ptr) { |
| 102 | reference_ = Encode(ptr); |
| 103 | } |
| 104 | |
| 105 | template<class MirrorType> |
| 106 | inline MirrorType* ObjPtr<MirrorType>::operator->() const { |
| 107 | return Ptr(); |
| 108 | } |
| 109 | |
| 110 | template<class MirrorType> |
| 111 | inline MirrorType* ObjPtr<MirrorType>::Ptr() const { |
| 112 | AssertValid(); |
| 113 | return PtrUnchecked(); |
| 114 | } |
| 115 | |
| 116 | template<class MirrorType> |
| 117 | template <typename SourceType> |
| 118 | inline ObjPtr<MirrorType> ObjPtr<MirrorType>::DownCast(ObjPtr<SourceType> ptr) { |
| 119 | static_assert(std::is_base_of_v<SourceType, MirrorType>, |
| 120 | "Target type must be a subtype of source type"); |
| 121 | return static_cast<MirrorType*>(ptr.Ptr()); |
| 122 | } |
| 123 | |
| 124 | template<class MirrorType> |
Vladimir Marko | d7e9bbf | 2019-03-28 13:18:57 +0000 | [diff] [blame] | 125 | template <typename SourceType> |
| 126 | inline ObjPtr<MirrorType> ObjPtr<MirrorType>::DownCast(SourceType* ptr) { |
| 127 | static_assert(std::is_base_of_v<SourceType, MirrorType>, |
| 128 | "Target type must be a subtype of source type"); |
| 129 | return static_cast<MirrorType*>(ptr); |
| 130 | } |
| 131 | |
| 132 | template<class MirrorType> |
Vladimir Marko | f57f2ed | 2019-03-25 15:55:42 +0000 | [diff] [blame] | 133 | size_t HashObjPtr::operator()(const ObjPtr<MirrorType>& ptr) const { |
| 134 | return std::hash<MirrorType*>()(ptr.Ptr()); |
| 135 | } |
| 136 | |
| 137 | template<class MirrorType1, class MirrorType2> |
| 138 | inline std::enable_if_t<std::is_base_of_v<MirrorType1, MirrorType2> || |
| 139 | std::is_base_of_v<MirrorType2, MirrorType1>, bool> |
Vladimir Marko | 3b45890 | 2019-03-26 17:08:41 +0000 | [diff] [blame] | 140 | operator==(ObjPtr<MirrorType1> lhs, ObjPtr<MirrorType2> rhs) { |
Vladimir Marko | f57f2ed | 2019-03-25 15:55:42 +0000 | [diff] [blame] | 141 | return lhs.Ptr() == rhs.Ptr(); |
| 142 | } |
| 143 | |
| 144 | template<class MirrorType1, class MirrorType2> |
| 145 | inline std::enable_if_t<std::is_base_of_v<MirrorType1, MirrorType2> || |
| 146 | std::is_base_of_v<MirrorType2, MirrorType1>, bool> |
Vladimir Marko | 3b45890 | 2019-03-26 17:08:41 +0000 | [diff] [blame] | 147 | operator==(const MirrorType1* lhs, ObjPtr<MirrorType2> rhs) { |
Vladimir Marko | f57f2ed | 2019-03-25 15:55:42 +0000 | [diff] [blame] | 148 | return lhs == rhs.Ptr(); |
| 149 | } |
| 150 | |
| 151 | template<class MirrorType1, class MirrorType2> |
| 152 | inline std::enable_if_t<std::is_base_of_v<MirrorType1, MirrorType2> || |
| 153 | std::is_base_of_v<MirrorType2, MirrorType1>, bool> |
Vladimir Marko | 3b45890 | 2019-03-26 17:08:41 +0000 | [diff] [blame] | 154 | operator==(ObjPtr<MirrorType1> lhs, const MirrorType2* rhs) { |
Vladimir Marko | f57f2ed | 2019-03-25 15:55:42 +0000 | [diff] [blame] | 155 | return lhs.Ptr() == rhs; |
| 156 | } |
| 157 | |
| 158 | template<class MirrorType1, class MirrorType2> |
| 159 | inline std::enable_if_t<std::is_base_of_v<MirrorType1, MirrorType2> || |
| 160 | std::is_base_of_v<MirrorType2, MirrorType1>, bool> |
Vladimir Marko | 3b45890 | 2019-03-26 17:08:41 +0000 | [diff] [blame] | 161 | operator!=(ObjPtr<MirrorType1> lhs, ObjPtr<MirrorType2> rhs) { |
Vladimir Marko | f57f2ed | 2019-03-25 15:55:42 +0000 | [diff] [blame] | 162 | return !(lhs == rhs); |
| 163 | } |
| 164 | |
| 165 | template<class MirrorType1, class MirrorType2> |
| 166 | inline std::enable_if_t<std::is_base_of_v<MirrorType1, MirrorType2> || |
| 167 | std::is_base_of_v<MirrorType2, MirrorType1>, bool> |
Vladimir Marko | 3b45890 | 2019-03-26 17:08:41 +0000 | [diff] [blame] | 168 | operator!=(const MirrorType1* lhs, ObjPtr<MirrorType2> rhs) { |
Vladimir Marko | f57f2ed | 2019-03-25 15:55:42 +0000 | [diff] [blame] | 169 | return !(lhs == rhs); |
| 170 | } |
| 171 | |
| 172 | template<class MirrorType1, class MirrorType2> |
| 173 | inline std::enable_if_t<std::is_base_of_v<MirrorType1, MirrorType2> || |
| 174 | std::is_base_of_v<MirrorType2, MirrorType1>, bool> |
Vladimir Marko | 3b45890 | 2019-03-26 17:08:41 +0000 | [diff] [blame] | 175 | operator!=(ObjPtr<MirrorType1> lhs, const MirrorType2* rhs) { |
Vladimir Marko | f57f2ed | 2019-03-25 15:55:42 +0000 | [diff] [blame] | 176 | return !(lhs == rhs); |
| 177 | } |
| 178 | |
| 179 | template<class MirrorType> |
Andreas Gampe | c73cb64 | 2017-02-22 10:11:30 -0800 | [diff] [blame] | 180 | inline std::ostream& operator<<(std::ostream& os, ObjPtr<MirrorType> ptr) { |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 181 | // May be used for dumping bad pointers, do not use the checked version. |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 182 | return os << ptr.PtrUnchecked(); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 185 | } // namespace art |
| 186 | |
| 187 | #endif // ART_RUNTIME_OBJ_PTR_INL_H_ |