blob: 0c9c0290c6105e4baa867b1540a37a7e0db9af77 [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;
35
Ian Rogers22d5e732014-07-15 22:23:51 -070036// Handles are memory locations that contain GC roots. As the mirror::Object*s within a handle are
37// 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 -070038// a wrap pointer. Handles are generally allocated within HandleScopes. Handle is a super-class
39// of MutableHandle and doesn't support assignment operations.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070040template<class T>
Ian Rogersb5cb18a2014-10-21 15:05:36 -070041class Handle : public ValueObject {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070042 public:
Andreas Gampe5a4b8a22014-09-11 08:30:08 -070043 Handle() : reference_(nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070044 }
Ian Rogers22d5e732014-07-15 22:23:51 -070045
Andreas Gampe38cea842016-11-03 13:06:25 -070046 ALWAYS_INLINE Handle(const Handle<T>& handle) = default;
Ian Rogers22d5e732014-07-15 22:23:51 -070047
Andreas Gampe38cea842016-11-03 13:06:25 -070048 ALWAYS_INLINE Handle<T>& operator=(const Handle<T>& handle) = default;
Ian Rogers22d5e732014-07-15 22:23:51 -070049
Andreas Gampe5a4b8a22014-09-11 08:30:08 -070050 ALWAYS_INLINE explicit Handle(StackReference<T>* reference) : reference_(reference) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070051 }
Ian Rogers22d5e732014-07-15 22:23:51 -070052
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070053 ALWAYS_INLINE T& operator*() const REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070054 return *Get();
55 }
Ian Rogers22d5e732014-07-15 22:23:51 -070056
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070057 ALWAYS_INLINE T* operator->() const REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070058 return Get();
59 }
Ian Rogers22d5e732014-07-15 22:23:51 -070060
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070061 ALWAYS_INLINE T* Get() const REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersb5cb18a2014-10-21 15:05:36 -070062 return down_cast<T*>(reference_->AsMirrorPtr());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070063 }
Ian Rogers22d5e732014-07-15 22:23:51 -070064
Vladimir Markoc1c34522018-10-31 13:56:49 +000065 ALWAYS_INLINE bool IsNull() const {
66 // It's safe to null-check it without a read barrier.
67 return reference_->IsNull();
Alex Lighta01de592016-11-15 10:43:06 -080068 }
69
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070070 ALWAYS_INLINE jobject ToJObject() const REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -070071 if (UNLIKELY(reference_->AsMirrorPtr() == nullptr)) {
Mathieu Chartier9865bde2015-12-21 09:58:16 -080072 // Special case so that we work with null handles.
Mathieu Chartier0cd81352014-05-22 16:48:55 -070073 return nullptr;
74 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070075 return reinterpret_cast<jobject>(reference_);
76 }
77
Vladimir Markof39745e2016-01-26 12:16:55 +000078 ALWAYS_INLINE StackReference<mirror::Object>* GetReference() {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070079 return reference_;
80 }
81
Vladimir Marko86973552016-01-26 15:06:15 +000082 ALWAYS_INLINE const StackReference<mirror::Object>* GetReference() const {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070083 return reference_;
84 }
85
Andreas Gampefa4333d2017-02-14 11:10:34 -080086 ALWAYS_INLINE bool operator!=(std::nullptr_t) const REQUIRES_SHARED(Locks::mutator_lock_) {
87 return !IsNull();
88 }
89
90 ALWAYS_INLINE bool operator==(std::nullptr_t) const REQUIRES_SHARED(Locks::mutator_lock_) {
91 return IsNull();
92 }
93
Mathieu Chartier0cd81352014-05-22 16:48:55 -070094 protected:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070095 template<typename S>
Andreas Gampe5a4b8a22014-09-11 08:30:08 -070096 explicit Handle(StackReference<S>* reference)
Ian Rogersb5cb18a2014-10-21 15:05:36 -070097 : reference_(reference) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070098 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070099 template<typename S>
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700100 explicit Handle(const Handle<S>& handle)
Ian Rogersb5cb18a2014-10-21 15:05:36 -0700101 : reference_(handle.reference_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700102 }
103
Ian Rogersb5cb18a2014-10-21 15:05:36 -0700104 StackReference<mirror::Object>* reference_;
105
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700106 private:
107 friend class BuildGenericJniFrameVisitor;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700108 template<class S> friend class Handle;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700109 friend class HandleScope;
110 template<class S> friend class HandleWrapper;
111 template<size_t kNumReferences> friend class StackHandleScope;
112};
113
Ian Rogers22d5e732014-07-15 22:23:51 -0700114// Handles that support assignment.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700115template<class T>
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700116class MutableHandle : public Handle<T> {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700117 public:
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700118 MutableHandle() {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700119 }
Ian Rogers22d5e732014-07-15 22:23:51 -0700120
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700121 ALWAYS_INLINE MutableHandle(const MutableHandle<T>& handle)
Andreas Gampe38cea842016-11-03 13:06:25 -0700122 REQUIRES_SHARED(Locks::mutator_lock_) = default;
Ian Rogers22d5e732014-07-15 22:23:51 -0700123
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700124 ALWAYS_INLINE MutableHandle<T>& operator=(const MutableHandle<T>& handle)
Andreas Gampe38cea842016-11-03 13:06:25 -0700125 REQUIRES_SHARED(Locks::mutator_lock_) = default;
Ian Rogers22d5e732014-07-15 22:23:51 -0700126
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700127 ALWAYS_INLINE explicit MutableHandle(StackReference<T>* reference)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700128 REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700129 : Handle<T>(reference) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700130 }
Ian Rogers22d5e732014-07-15 22:23:51 -0700131
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700132 ALWAYS_INLINE T* Assign(T* reference) REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersb5cb18a2014-10-21 15:05:36 -0700133 StackReference<mirror::Object>* ref = Handle<T>::GetReference();
134 T* old = down_cast<T*>(ref->AsMirrorPtr());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700135 ref->Assign(reference);
136 return old;
137 }
138
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700139 ALWAYS_INLINE T* Assign(ObjPtr<T> reference) REQUIRES_SHARED(Locks::mutator_lock_) {
140 StackReference<mirror::Object>* ref = Handle<T>::GetReference();
141 T* old = down_cast<T*>(ref->AsMirrorPtr());
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700142 ref->Assign(reference.Ptr());
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700143 return old;
144 }
145
146
Ian Rogers22d5e732014-07-15 22:23:51 -0700147 template<typename S>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700148 explicit MutableHandle(const MutableHandle<S>& handle) REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700149 : Handle<T>(handle) {
Ian Rogers22d5e732014-07-15 22:23:51 -0700150 }
151
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700152 template<typename S>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700153 explicit MutableHandle(StackReference<S>* reference) REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700154 : Handle<T>(reference) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700155 }
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700156
157 private:
158 friend class BuildGenericJniFrameVisitor;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700159 friend class HandleScope;
160 template<class S> friend class HandleWrapper;
161 template<size_t kNumReferences> friend class StackHandleScope;
162};
163
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800164// A special case of Handle that only holds references to null. Invalid when if it goes out of
165// scope. Example: Handle<T> h = ScopedNullHandle<T> will leave h being undefined.
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700166template<class T>
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800167class ScopedNullHandle : public Handle<T> {
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700168 public:
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800169 ScopedNullHandle() : Handle<T>(&null_ref_) {}
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700170
171 private:
Ian Rogersb5cb18a2014-10-21 15:05:36 -0700172 StackReference<mirror::Object> null_ref_;
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700173};
174
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700175} // namespace art
176
177#endif // ART_RUNTIME_HANDLE_H_