blob: b34d983d6ac57bfe1fe32e7c7df8c51101b73dfe [file] [log] [blame]
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001/*
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 Gampe57943812017-12-06 21:39:13 -080020#include <android-base/logging.h>
21
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070022#include "base/casts.h"
Andreas Gampe7fbc4a52018-11-28 08:26:47 -080023#include "base/locks.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070024#include "base/macros.h"
Ian Rogersb5cb18a2014-10-21 15:05:36 -070025#include "base/value_object.h"
Vladimir Marko3a21e382016-09-02 12:38:38 +010026#include "jni.h"
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070027#include "obj_ptr.h"
Vladimir Marko3a21e382016-09-02 12:38:38 +010028#include "stack_reference.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070029
30namespace art {
31
32class Thread;
33
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070034template<class T> class Handle;
Alex Lighta9bbc082019-11-14 14:51:41 -080035template<typename T> class IterationRange;
36
37namespace mirror {
38template<typename T> class ObjectArray;
39template<typename T, typename C> class ArrayIter;
40template<typename T> using HandleArrayIter = ArrayIter<T, Handle<ObjectArray<T>>>;
41template<typename T> using ConstHandleArrayIter = ArrayIter<T, const Handle<ObjectArray<T>>>;
42} // namespace mirror
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070043
Ian Rogers22d5e732014-07-15 22:23:51 -070044// 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 Gampe5a4b8a22014-09-11 08:30:08 -070046// a wrap pointer. Handles are generally allocated within HandleScopes. Handle is a super-class
47// of MutableHandle and doesn't support assignment operations.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070048template<class T>
Ian Rogersb5cb18a2014-10-21 15:05:36 -070049class Handle : public ValueObject {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070050 public:
Andreas Gampe5a4b8a22014-09-11 08:30:08 -070051 Handle() : reference_(nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070052 }
Ian Rogers22d5e732014-07-15 22:23:51 -070053
Andreas Gampe38cea842016-11-03 13:06:25 -070054 ALWAYS_INLINE Handle(const Handle<T>& handle) = default;
Ian Rogers22d5e732014-07-15 22:23:51 -070055
Andreas Gampe38cea842016-11-03 13:06:25 -070056 ALWAYS_INLINE Handle<T>& operator=(const Handle<T>& handle) = default;
Ian Rogers22d5e732014-07-15 22:23:51 -070057
Vladimir Marko3068d582019-05-28 16:39:29 +010058 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 Gampe5a4b8a22014-09-11 08:30:08 -070063 ALWAYS_INLINE explicit Handle(StackReference<T>* reference) : reference_(reference) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070064 }
Ian Rogers22d5e732014-07-15 22:23:51 -070065
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070066 ALWAYS_INLINE T& operator*() const REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070067 return *Get();
68 }
Ian Rogers22d5e732014-07-15 22:23:51 -070069
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070070 ALWAYS_INLINE T* operator->() const REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070071 return Get();
72 }
Ian Rogers22d5e732014-07-15 22:23:51 -070073
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070074 ALWAYS_INLINE T* Get() const REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersb5cb18a2014-10-21 15:05:36 -070075 return down_cast<T*>(reference_->AsMirrorPtr());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070076 }
Ian Rogers22d5e732014-07-15 22:23:51 -070077
Alex Lighta9bbc082019-11-14 14:51:41 -080078 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 Markoc1c34522018-10-31 13:56:49 +000091 ALWAYS_INLINE bool IsNull() const {
92 // It's safe to null-check it without a read barrier.
93 return reference_->IsNull();
Alex Lighta01de592016-11-15 10:43:06 -080094 }
95
Vladimir Markof39745e2016-01-26 12:16:55 +000096 ALWAYS_INLINE StackReference<mirror::Object>* GetReference() {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070097 return reference_;
98 }
99
Vladimir Marko86973552016-01-26 15:06:15 +0000100 ALWAYS_INLINE const StackReference<mirror::Object>* GetReference() const {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700101 return reference_;
102 }
103
Vladimir Markoffafe8b2021-04-23 10:29:27 +0000104 ALWAYS_INLINE bool operator!=(std::nullptr_t) const {
Andreas Gampefa4333d2017-02-14 11:10:34 -0800105 return !IsNull();
106 }
107
Vladimir Markoffafe8b2021-04-23 10:29:27 +0000108 ALWAYS_INLINE bool operator==(std::nullptr_t) const {
Andreas Gampefa4333d2017-02-14 11:10:34 -0800109 return IsNull();
110 }
111
Alex Lightc1ad13a2020-03-24 11:37:45 -0700112 mirror::Object* ObjectFromGdb() REQUIRES_SHARED(Locks::mutator_lock_);
113 T* GetFromGdb() REQUIRES_SHARED(Locks::mutator_lock_);
114
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700115 protected:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700116 template<typename S>
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700117 explicit Handle(StackReference<S>* reference)
Ian Rogersb5cb18a2014-10-21 15:05:36 -0700118 : reference_(reference) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700119 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700120 template<typename S>
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700121 explicit Handle(const Handle<S>& handle)
Ian Rogersb5cb18a2014-10-21 15:05:36 -0700122 : reference_(handle.reference_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700123 }
124
Ian Rogersb5cb18a2014-10-21 15:05:36 -0700125 StackReference<mirror::Object>* reference_;
126
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700127 private:
128 friend class BuildGenericJniFrameVisitor;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700129 template<class S> friend class Handle;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700130 friend class HandleScope;
131 template<class S> friend class HandleWrapper;
132 template<size_t kNumReferences> friend class StackHandleScope;
133};
134
Ian Rogers22d5e732014-07-15 22:23:51 -0700135// Handles that support assignment.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700136template<class T>
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700137class MutableHandle : public Handle<T> {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700138 public:
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700139 MutableHandle() {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700140 }
Ian Rogers22d5e732014-07-15 22:23:51 -0700141
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700142 ALWAYS_INLINE MutableHandle(const MutableHandle<T>& handle)
Andreas Gampe38cea842016-11-03 13:06:25 -0700143 REQUIRES_SHARED(Locks::mutator_lock_) = default;
Ian Rogers22d5e732014-07-15 22:23:51 -0700144
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700145 ALWAYS_INLINE MutableHandle<T>& operator=(const MutableHandle<T>& handle)
Andreas Gampe38cea842016-11-03 13:06:25 -0700146 REQUIRES_SHARED(Locks::mutator_lock_) = default;
Ian Rogers22d5e732014-07-15 22:23:51 -0700147
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700148 ALWAYS_INLINE explicit MutableHandle(StackReference<T>* reference)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700149 REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700150 : Handle<T>(reference) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700151 }
Ian Rogers22d5e732014-07-15 22:23:51 -0700152
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700153 ALWAYS_INLINE T* Assign(T* reference) REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersb5cb18a2014-10-21 15:05:36 -0700154 StackReference<mirror::Object>* ref = Handle<T>::GetReference();
155 T* old = down_cast<T*>(ref->AsMirrorPtr());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700156 ref->Assign(reference);
157 return old;
158 }
159
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700160 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 Chartier1cc62e42016-10-03 18:01:28 -0700163 ref->Assign(reference.Ptr());
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700164 return old;
165 }
166
167
Ian Rogers22d5e732014-07-15 22:23:51 -0700168 template<typename S>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700169 explicit MutableHandle(const MutableHandle<S>& handle) REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700170 : Handle<T>(handle) {
Ian Rogers22d5e732014-07-15 22:23:51 -0700171 }
172
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700173 template<typename S>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700174 explicit MutableHandle(StackReference<S>* reference) REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700175 : Handle<T>(reference) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700176 }
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700177
178 private:
179 friend class BuildGenericJniFrameVisitor;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700180 friend class HandleScope;
181 template<class S> friend class HandleWrapper;
182 template<size_t kNumReferences> friend class StackHandleScope;
183};
184
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800185// 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 Chartier0cd81352014-05-22 16:48:55 -0700187template<class T>
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800188class ScopedNullHandle : public Handle<T> {
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700189 public:
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800190 ScopedNullHandle() : Handle<T>(&null_ref_) {}
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700191
192 private:
Ian Rogersb5cb18a2014-10-21 15:05:36 -0700193 StackReference<mirror::Object> null_ref_;
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700194};
195
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700196} // namespace art
197
198#endif // ART_RUNTIME_HANDLE_H_