blob: 5df010bb1d2e8108721a2f88903961566ede6b4f [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
20#include "base/casts.h"
21#include "base/logging.h"
22#include "base/macros.h"
Ian Rogersb5cb18a2014-10-21 15:05:36 -070023#include "base/value_object.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070024#include "stack.h"
25
26namespace art {
27
28class Thread;
29
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070030template<class T> class Handle;
31
Ian Rogers22d5e732014-07-15 22:23:51 -070032// Handles are memory locations that contain GC roots. As the mirror::Object*s within a handle are
33// 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 -070034// a wrap pointer. Handles are generally allocated within HandleScopes. Handle is a super-class
35// of MutableHandle and doesn't support assignment operations.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070036template<class T>
Ian Rogersb5cb18a2014-10-21 15:05:36 -070037class Handle : public ValueObject {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070038 public:
Andreas Gampe5a4b8a22014-09-11 08:30:08 -070039 Handle() : reference_(nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070040 }
Ian Rogers22d5e732014-07-15 22:23:51 -070041
Andreas Gampe5a4b8a22014-09-11 08:30:08 -070042 ALWAYS_INLINE Handle(const Handle<T>& handle) : reference_(handle.reference_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070043 }
Ian Rogers22d5e732014-07-15 22:23:51 -070044
Andreas Gampe5a4b8a22014-09-11 08:30:08 -070045 ALWAYS_INLINE Handle<T>& operator=(const Handle<T>& handle) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070046 reference_ = handle.reference_;
47 return *this;
48 }
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
Mathieu Chartier90443472015-07-16 20:32:27 -070053 ALWAYS_INLINE T& operator*() const SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070054 return *Get();
55 }
Ian Rogers22d5e732014-07-15 22:23:51 -070056
Mathieu Chartier90443472015-07-16 20:32:27 -070057 ALWAYS_INLINE T* operator->() const SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070058 return Get();
59 }
Ian Rogers22d5e732014-07-15 22:23:51 -070060
Mathieu Chartier90443472015-07-16 20:32:27 -070061 ALWAYS_INLINE T* Get() const SHARED_REQUIRES(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
Mathieu Chartier90443472015-07-16 20:32:27 -070065 ALWAYS_INLINE jobject ToJObject() const SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -070066 if (UNLIKELY(reference_->AsMirrorPtr() == nullptr)) {
Mathieu Chartier9865bde2015-12-21 09:58:16 -080067 // Special case so that we work with null handles.
Mathieu Chartier0cd81352014-05-22 16:48:55 -070068 return nullptr;
69 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070070 return reinterpret_cast<jobject>(reference_);
71 }
72
Vladimir Markof39745e2016-01-26 12:16:55 +000073 ALWAYS_INLINE StackReference<mirror::Object>* GetReference() {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070074 return reference_;
75 }
76
77 ALWAYS_INLINE const StackReference<mirror::Object>* GetReference() const
Mathieu Chartier90443472015-07-16 20:32:27 -070078 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070079 return reference_;
80 }
81
Mathieu Chartier0cd81352014-05-22 16:48:55 -070082 protected:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070083 template<typename S>
Andreas Gampe5a4b8a22014-09-11 08:30:08 -070084 explicit Handle(StackReference<S>* reference)
Ian Rogersb5cb18a2014-10-21 15:05:36 -070085 : reference_(reference) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070086 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070087 template<typename S>
Andreas Gampe5a4b8a22014-09-11 08:30:08 -070088 explicit Handle(const Handle<S>& handle)
Ian Rogersb5cb18a2014-10-21 15:05:36 -070089 : reference_(handle.reference_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070090 }
91
Ian Rogersb5cb18a2014-10-21 15:05:36 -070092 StackReference<mirror::Object>* reference_;
93
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070094 private:
95 friend class BuildGenericJniFrameVisitor;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -070096 template<class S> friend class Handle;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070097 friend class HandleScope;
98 template<class S> friend class HandleWrapper;
99 template<size_t kNumReferences> friend class StackHandleScope;
100};
101
Ian Rogers22d5e732014-07-15 22:23:51 -0700102// Handles that support assignment.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700103template<class T>
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700104class MutableHandle : public Handle<T> {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700105 public:
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700106 MutableHandle() {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700107 }
Ian Rogers22d5e732014-07-15 22:23:51 -0700108
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700109 ALWAYS_INLINE MutableHandle(const MutableHandle<T>& handle)
Mathieu Chartier90443472015-07-16 20:32:27 -0700110 SHARED_REQUIRES(Locks::mutator_lock_)
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700111 : Handle<T>(handle.reference_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700112 }
Ian Rogers22d5e732014-07-15 22:23:51 -0700113
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700114 ALWAYS_INLINE MutableHandle<T>& operator=(const MutableHandle<T>& handle)
Mathieu Chartier90443472015-07-16 20:32:27 -0700115 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700116 Handle<T>::operator=(handle);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700117 return *this;
118 }
Ian Rogers22d5e732014-07-15 22:23:51 -0700119
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700120 ALWAYS_INLINE explicit MutableHandle(StackReference<T>* reference)
Mathieu Chartier90443472015-07-16 20:32:27 -0700121 SHARED_REQUIRES(Locks::mutator_lock_)
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700122 : Handle<T>(reference) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700123 }
Ian Rogers22d5e732014-07-15 22:23:51 -0700124
Mathieu Chartier90443472015-07-16 20:32:27 -0700125 ALWAYS_INLINE T* Assign(T* reference) SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersb5cb18a2014-10-21 15:05:36 -0700126 StackReference<mirror::Object>* ref = Handle<T>::GetReference();
127 T* old = down_cast<T*>(ref->AsMirrorPtr());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700128 ref->Assign(reference);
129 return old;
130 }
131
Ian Rogers22d5e732014-07-15 22:23:51 -0700132 template<typename S>
Mathieu Chartier90443472015-07-16 20:32:27 -0700133 explicit MutableHandle(const MutableHandle<S>& handle) SHARED_REQUIRES(Locks::mutator_lock_)
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700134 : Handle<T>(handle) {
Ian Rogers22d5e732014-07-15 22:23:51 -0700135 }
136
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700137 template<typename S>
Mathieu Chartier90443472015-07-16 20:32:27 -0700138 explicit MutableHandle(StackReference<S>* reference) SHARED_REQUIRES(Locks::mutator_lock_)
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700139 : Handle<T>(reference) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700140 }
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700141
142 private:
143 friend class BuildGenericJniFrameVisitor;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700144 friend class HandleScope;
145 template<class S> friend class HandleWrapper;
146 template<size_t kNumReferences> friend class StackHandleScope;
147};
148
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800149// A special case of Handle that only holds references to null. Invalid when if it goes out of
150// scope. Example: Handle<T> h = ScopedNullHandle<T> will leave h being undefined.
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700151template<class T>
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800152class ScopedNullHandle : public Handle<T> {
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700153 public:
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800154 ScopedNullHandle() : Handle<T>(&null_ref_) {}
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700155
156 private:
Ian Rogersb5cb18a2014-10-21 15:05:36 -0700157 StackReference<mirror::Object> null_ref_;
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700158};
159
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700160} // namespace art
161
162#endif // ART_RUNTIME_HANDLE_H_