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