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 20 #include <android-base/logging.h> 21 22 #include "base/casts.h" 23 #include "base/locks.h" 24 #include "base/macros.h" 25 #include "base/value_object.h" 26 #include "jni.h" 27 #include "obj_ptr.h" 28 #include "stack_reference.h" 29 30 namespace art { 31 32 class Thread; 33 34 template<class T> class Handle; 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 43 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 46 // a wrap pointer. Handles are generally allocated within HandleScopes. Handle is a super-class 47 // of MutableHandle and doesn't support assignment operations. 48 template<class T> 49 class Handle : public ValueObject { 50 public: Handle()51 Handle() : reference_(nullptr) { 52 } 53 54 ALWAYS_INLINE Handle(const Handle<T>& handle) = default; 55 56 ALWAYS_INLINE Handle<T>& operator=(const Handle<T>& handle) = default; 57 58 template <typename Type, 59 typename = typename std::enable_if_t<std::is_base_of_v<T, Type>>> Handle(const Handle<Type> & other)60 ALWAYS_INLINE Handle(const Handle<Type>& other) : reference_(other.reference_) { 61 } 62 Handle(StackReference<T> * reference)63 ALWAYS_INLINE explicit Handle(StackReference<T>* reference) : reference_(reference) { 64 } 65 REQUIRES_SHARED(Locks::mutator_lock_)66 ALWAYS_INLINE T& operator*() const REQUIRES_SHARED(Locks::mutator_lock_) { 67 return *Get(); 68 } 69 70 ALWAYS_INLINE T* operator->() const REQUIRES_SHARED(Locks::mutator_lock_) { 71 return Get(); 72 } 73 Get()74 ALWAYS_INLINE T* Get() const REQUIRES_SHARED(Locks::mutator_lock_) { 75 return down_cast<T*>(reference_->AsMirrorPtr()); 76 } 77 78 template <typename Type, 79 typename = typename std::enable_if_t<std::is_same_v<mirror::ObjectArray<Type>, T>>> ConstIterate()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>>> Iterate()86 ALWAYS_INLINE IterationRange<mirror::HandleArrayIter<Type>> Iterate() 87 REQUIRES_SHARED(Locks::mutator_lock_) { 88 return T::Iterate(*this); 89 } 90 IsNull()91 ALWAYS_INLINE bool IsNull() const { 92 // It's safe to null-check it without a read barrier. 93 return reference_->IsNull(); 94 } 95 GetReference()96 ALWAYS_INLINE StackReference<mirror::Object>* GetReference() { 97 return reference_; 98 } 99 GetReference()100 ALWAYS_INLINE const StackReference<mirror::Object>* GetReference() const { 101 return reference_; 102 } 103 104 ALWAYS_INLINE bool operator!=(std::nullptr_t) const { 105 return !IsNull(); 106 } 107 108 ALWAYS_INLINE bool operator==(std::nullptr_t) const { 109 return IsNull(); 110 } 111 112 mirror::Object* ObjectFromGdb() REQUIRES_SHARED(Locks::mutator_lock_); 113 T* GetFromGdb() REQUIRES_SHARED(Locks::mutator_lock_); 114 115 protected: 116 template<typename S> Handle(StackReference<S> * reference)117 explicit Handle(StackReference<S>* reference) 118 : reference_(reference) { 119 } 120 template<typename S> Handle(const Handle<S> & handle)121 explicit Handle(const Handle<S>& handle) 122 : reference_(handle.reference_) { 123 } 124 125 StackReference<mirror::Object>* reference_; 126 127 private: 128 friend class BuildGenericJniFrameVisitor; 129 template<class S> friend class Handle; 130 friend class HandleScope; 131 template<class S> friend class HandleWrapper; 132 template<size_t kNumReferences> friend class StackHandleScope; 133 }; 134 135 // Handles that support assignment. 136 template<class T> 137 class MutableHandle : public Handle<T> { 138 public: MutableHandle()139 MutableHandle() { 140 } 141 142 ALWAYS_INLINE MutableHandle(const MutableHandle<T>& handle) 143 REQUIRES_SHARED(Locks::mutator_lock_) = default; 144 145 ALWAYS_INLINE MutableHandle<T>& operator=(const MutableHandle<T>& handle) 146 REQUIRES_SHARED(Locks::mutator_lock_) = default; 147 MutableHandle(StackReference<T> * reference)148 ALWAYS_INLINE explicit MutableHandle(StackReference<T>* reference) 149 REQUIRES_SHARED(Locks::mutator_lock_) 150 : Handle<T>(reference) { 151 } 152 Assign(T * reference)153 ALWAYS_INLINE T* Assign(T* reference) REQUIRES_SHARED(Locks::mutator_lock_) { 154 StackReference<mirror::Object>* ref = Handle<T>::GetReference(); 155 T* old = down_cast<T*>(ref->AsMirrorPtr()); 156 ref->Assign(reference); 157 return old; 158 } 159 Assign(ObjPtr<T> reference)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()); 163 ref->Assign(reference.Ptr()); 164 return old; 165 } 166 167 168 template<typename S> MutableHandle(const MutableHandle<S> & handle)169 explicit MutableHandle(const MutableHandle<S>& handle) REQUIRES_SHARED(Locks::mutator_lock_) 170 : Handle<T>(handle) { 171 } 172 173 template<typename S> MutableHandle(StackReference<S> * reference)174 explicit MutableHandle(StackReference<S>* reference) REQUIRES_SHARED(Locks::mutator_lock_) 175 : Handle<T>(reference) { 176 } 177 178 private: 179 friend class BuildGenericJniFrameVisitor; 180 friend class HandleScope; 181 template<class S> friend class HandleWrapper; 182 template<size_t kNumReferences> friend class StackHandleScope; 183 }; 184 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. 187 template<class T> 188 class ScopedNullHandle : public Handle<T> { 189 public: ScopedNullHandle()190 ScopedNullHandle() : Handle<T>(&null_ref_) {} 191 192 private: 193 StackReference<mirror::Object> null_ref_; 194 }; 195 196 } // namespace art 197 198 #endif // ART_RUNTIME_HANDLE_H_ 199