Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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_HANDLE_H_ |
| 18 | #define ART_RUNTIME_HANDLE_H_ |
| 19 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 20 | #include <android-base/logging.h> |
| 21 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 22 | #include "base/casts.h" |
Andreas Gampe | 7fbc4a5 | 2018-11-28 08:26:47 -0800 | [diff] [blame] | 23 | #include "base/locks.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 24 | #include "base/macros.h" |
Ian Rogers | b5cb18a | 2014-10-21 15:05:36 -0700 | [diff] [blame] | 25 | #include "base/value_object.h" |
Vladimir Marko | 3a21e38 | 2016-09-02 12:38:38 +0100 | [diff] [blame] | 26 | #include "jni.h" |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 27 | #include "obj_ptr.h" |
Vladimir Marko | 3a21e38 | 2016-09-02 12:38:38 +0100 | [diff] [blame] | 28 | #include "stack_reference.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 29 | |
| 30 | namespace art { |
| 31 | |
| 32 | class Thread; |
| 33 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 34 | template<class T> class Handle; |
Alex Light | a9bbc08 | 2019-11-14 14:51:41 -0800 | [diff] [blame] | 35 | template<typename T> class IterationRange; |
| 36 | |
| 37 | namespace mirror { |
| 38 | template<typename T> class ObjectArray; |
| 39 | template<typename T, typename C> class ArrayIter; |
| 40 | template<typename T> using HandleArrayIter = ArrayIter<T, Handle<ObjectArray<T>>>; |
| 41 | template<typename T> using ConstHandleArrayIter = ArrayIter<T, const Handle<ObjectArray<T>>>; |
| 42 | } // namespace mirror |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 43 | |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 44 | // Handles are memory locations that contain GC roots. As the mirror::Object*s within a handle are |
| 45 | // GC visible then the GC may move the references within them, something that couldn't be done with |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 46 | // a wrap pointer. Handles are generally allocated within HandleScopes. Handle is a super-class |
| 47 | // of MutableHandle and doesn't support assignment operations. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 48 | template<class T> |
Ian Rogers | b5cb18a | 2014-10-21 15:05:36 -0700 | [diff] [blame] | 49 | class Handle : public ValueObject { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 50 | public: |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 51 | Handle() : reference_(nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 52 | } |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 53 | |
Andreas Gampe | 38cea84 | 2016-11-03 13:06:25 -0700 | [diff] [blame] | 54 | ALWAYS_INLINE Handle(const Handle<T>& handle) = default; |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 55 | |
Andreas Gampe | 38cea84 | 2016-11-03 13:06:25 -0700 | [diff] [blame] | 56 | ALWAYS_INLINE Handle<T>& operator=(const Handle<T>& handle) = default; |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 57 | |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 58 | template <typename Type, |
| 59 | typename = typename std::enable_if_t<std::is_base_of_v<T, Type>>> |
| 60 | ALWAYS_INLINE Handle(const Handle<Type>& other) : reference_(other.reference_) { |
| 61 | } |
| 62 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 63 | ALWAYS_INLINE explicit Handle(StackReference<T>* reference) : reference_(reference) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 64 | } |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 65 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 66 | ALWAYS_INLINE T& operator*() const REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 67 | return *Get(); |
| 68 | } |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 69 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 70 | ALWAYS_INLINE T* operator->() const REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 71 | return Get(); |
| 72 | } |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 73 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 74 | ALWAYS_INLINE T* Get() const REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | b5cb18a | 2014-10-21 15:05:36 -0700 | [diff] [blame] | 75 | return down_cast<T*>(reference_->AsMirrorPtr()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 76 | } |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 77 | |
Alex Light | a9bbc08 | 2019-11-14 14:51:41 -0800 | [diff] [blame] | 78 | template <typename Type, |
| 79 | typename = typename std::enable_if_t<std::is_same_v<mirror::ObjectArray<Type>, T>>> |
| 80 | ALWAYS_INLINE IterationRange<mirror::ConstHandleArrayIter<Type>> ConstIterate() const |
| 81 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 82 | return T::ConstIterate(*this); |
| 83 | } |
| 84 | template <typename Type, |
| 85 | typename = typename std::enable_if_t<std::is_same_v<mirror::ObjectArray<Type>, T>>> |
| 86 | ALWAYS_INLINE IterationRange<mirror::HandleArrayIter<Type>> Iterate() |
| 87 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 88 | return T::Iterate(*this); |
| 89 | } |
| 90 | |
Vladimir Marko | c1c3452 | 2018-10-31 13:56:49 +0000 | [diff] [blame] | 91 | ALWAYS_INLINE bool IsNull() const { |
| 92 | // It's safe to null-check it without a read barrier. |
| 93 | return reference_->IsNull(); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Vladimir Marko | f39745e | 2016-01-26 12:16:55 +0000 | [diff] [blame] | 96 | ALWAYS_INLINE StackReference<mirror::Object>* GetReference() { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 97 | return reference_; |
| 98 | } |
| 99 | |
Vladimir Marko | 8697355 | 2016-01-26 15:06:15 +0000 | [diff] [blame] | 100 | ALWAYS_INLINE const StackReference<mirror::Object>* GetReference() const { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 101 | return reference_; |
| 102 | } |
| 103 | |
Vladimir Marko | ffafe8b | 2021-04-23 10:29:27 +0000 | [diff] [blame] | 104 | ALWAYS_INLINE bool operator!=(std::nullptr_t) const { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 105 | return !IsNull(); |
| 106 | } |
| 107 | |
Vladimir Marko | ffafe8b | 2021-04-23 10:29:27 +0000 | [diff] [blame] | 108 | ALWAYS_INLINE bool operator==(std::nullptr_t) const { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 109 | return IsNull(); |
| 110 | } |
| 111 | |
Alex Light | c1ad13a | 2020-03-24 11:37:45 -0700 | [diff] [blame] | 112 | mirror::Object* ObjectFromGdb() REQUIRES_SHARED(Locks::mutator_lock_); |
| 113 | T* GetFromGdb() REQUIRES_SHARED(Locks::mutator_lock_); |
| 114 | |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 115 | protected: |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 116 | template<typename S> |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 117 | explicit Handle(StackReference<S>* reference) |
Ian Rogers | b5cb18a | 2014-10-21 15:05:36 -0700 | [diff] [blame] | 118 | : reference_(reference) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 119 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 120 | template<typename S> |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 121 | explicit Handle(const Handle<S>& handle) |
Ian Rogers | b5cb18a | 2014-10-21 15:05:36 -0700 | [diff] [blame] | 122 | : reference_(handle.reference_) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Ian Rogers | b5cb18a | 2014-10-21 15:05:36 -0700 | [diff] [blame] | 125 | StackReference<mirror::Object>* reference_; |
| 126 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 127 | private: |
| 128 | friend class BuildGenericJniFrameVisitor; |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 129 | template<class S> friend class Handle; |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 130 | friend class HandleScope; |
| 131 | template<class S> friend class HandleWrapper; |
| 132 | template<size_t kNumReferences> friend class StackHandleScope; |
| 133 | }; |
| 134 | |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 135 | // Handles that support assignment. |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 136 | template<class T> |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 137 | class MutableHandle : public Handle<T> { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 138 | public: |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 139 | MutableHandle() { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 140 | } |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 141 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 142 | ALWAYS_INLINE MutableHandle(const MutableHandle<T>& handle) |
Andreas Gampe | 38cea84 | 2016-11-03 13:06:25 -0700 | [diff] [blame] | 143 | REQUIRES_SHARED(Locks::mutator_lock_) = default; |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 144 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 145 | ALWAYS_INLINE MutableHandle<T>& operator=(const MutableHandle<T>& handle) |
Andreas Gampe | 38cea84 | 2016-11-03 13:06:25 -0700 | [diff] [blame] | 146 | REQUIRES_SHARED(Locks::mutator_lock_) = default; |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 147 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 148 | ALWAYS_INLINE explicit MutableHandle(StackReference<T>* reference) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 149 | REQUIRES_SHARED(Locks::mutator_lock_) |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 150 | : Handle<T>(reference) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 151 | } |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 152 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 153 | ALWAYS_INLINE T* Assign(T* reference) REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | b5cb18a | 2014-10-21 15:05:36 -0700 | [diff] [blame] | 154 | StackReference<mirror::Object>* ref = Handle<T>::GetReference(); |
| 155 | T* old = down_cast<T*>(ref->AsMirrorPtr()); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 156 | ref->Assign(reference); |
| 157 | return old; |
| 158 | } |
| 159 | |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 160 | ALWAYS_INLINE T* Assign(ObjPtr<T> reference) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 161 | StackReference<mirror::Object>* ref = Handle<T>::GetReference(); |
| 162 | T* old = down_cast<T*>(ref->AsMirrorPtr()); |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 163 | ref->Assign(reference.Ptr()); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 164 | return old; |
| 165 | } |
| 166 | |
| 167 | |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 168 | template<typename S> |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 169 | explicit MutableHandle(const MutableHandle<S>& handle) REQUIRES_SHARED(Locks::mutator_lock_) |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 170 | : Handle<T>(handle) { |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 173 | template<typename S> |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 174 | explicit MutableHandle(StackReference<S>* reference) REQUIRES_SHARED(Locks::mutator_lock_) |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 175 | : Handle<T>(reference) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 176 | } |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 177 | |
| 178 | private: |
| 179 | friend class BuildGenericJniFrameVisitor; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 180 | friend class HandleScope; |
| 181 | template<class S> friend class HandleWrapper; |
| 182 | template<size_t kNumReferences> friend class StackHandleScope; |
| 183 | }; |
| 184 | |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 185 | // A special case of Handle that only holds references to null. Invalid when if it goes out of |
| 186 | // scope. Example: Handle<T> h = ScopedNullHandle<T> will leave h being undefined. |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 187 | template<class T> |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 188 | class ScopedNullHandle : public Handle<T> { |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 189 | public: |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 190 | ScopedNullHandle() : Handle<T>(&null_ref_) {} |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 191 | |
| 192 | private: |
Ian Rogers | b5cb18a | 2014-10-21 15:05:36 -0700 | [diff] [blame] | 193 | StackReference<mirror::Object> null_ref_; |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 194 | }; |
| 195 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 196 | } // namespace art |
| 197 | |
| 198 | #endif // ART_RUNTIME_HANDLE_H_ |