blob: f9864dc1d50211e8cfb0e55f16b846db8fb871d5 [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"
23#include "stack.h"
24
25namespace art {
26
27class Thread;
28
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070029template<class T> class Handle;
30
Ian Rogers22d5e732014-07-15 22:23:51 -070031// Handles are memory locations that contain GC roots. As the mirror::Object*s within a handle are
32// GC visible then the GC may move the references within them, something that couldn't be done with
33// a wrap pointer. Handles are generally allocated within HandleScopes. ConstHandle is a super-class
34// of Handle and doesn't support assignment operations.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070035template<class T>
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070036class ConstHandle {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070037 public:
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070038 ConstHandle() : reference_(nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070039 }
Ian Rogers22d5e732014-07-15 22:23:51 -070040
41 ALWAYS_INLINE ConstHandle(const ConstHandle<T>& handle) : reference_(handle.reference_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070042 }
Ian Rogers22d5e732014-07-15 22:23:51 -070043
44 ALWAYS_INLINE ConstHandle<T>& operator=(const ConstHandle<T>& handle) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070045 reference_ = handle.reference_;
46 return *this;
47 }
Ian Rogers22d5e732014-07-15 22:23:51 -070048
49 ALWAYS_INLINE explicit ConstHandle(StackReference<T>* reference) : reference_(reference) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070050 }
Ian Rogers22d5e732014-07-15 22:23:51 -070051
52 ALWAYS_INLINE T& operator*() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070053 return *Get();
54 }
Ian Rogers22d5e732014-07-15 22:23:51 -070055
56 ALWAYS_INLINE T* operator->() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070057 return Get();
58 }
Ian Rogers22d5e732014-07-15 22:23:51 -070059
60 ALWAYS_INLINE T* Get() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers96664ad2014-10-21 15:05:36 -070061 return down_cast<T*>(reference_->AsMirrorPtr());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070062 }
Ian Rogers22d5e732014-07-15 22:23:51 -070063
64 ALWAYS_INLINE jobject ToJObject() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -070065 if (UNLIKELY(reference_->AsMirrorPtr() == nullptr)) {
66 // Special case so that we work with NullHandles.
67 return nullptr;
68 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070069 return reinterpret_cast<jobject>(reference_);
70 }
71
Mathieu Chartier0cd81352014-05-22 16:48:55 -070072 protected:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070073 template<typename S>
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070074 explicit ConstHandle(StackReference<S>* reference)
Ian Rogers96664ad2014-10-21 15:05:36 -070075 : reference_(reference) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070076 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070077 template<typename S>
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070078 explicit ConstHandle(const ConstHandle<S>& handle)
Ian Rogers96664ad2014-10-21 15:05:36 -070079 : reference_(handle.reference_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070080 }
81
Ian Rogers96664ad2014-10-21 15:05:36 -070082 StackReference<mirror::Object>* GetReference() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE {
Mathieu Chartier0cd81352014-05-22 16:48:55 -070083 return reference_;
84 }
Ian Rogers96664ad2014-10-21 15:05:36 -070085 ALWAYS_INLINE const StackReference<mirror::Object>* GetReference() const
Ian Rogers22d5e732014-07-15 22:23:51 -070086 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070087 return reference_;
88 }
89
Ian Rogers96664ad2014-10-21 15:05:36 -070090 StackReference<mirror::Object>* reference_;
91
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070092 private:
93 friend class BuildGenericJniFrameVisitor;
94 template<class S> friend class ConstHandle;
95 friend class HandleScope;
96 template<class S> friend class HandleWrapper;
97 template<size_t kNumReferences> friend class StackHandleScope;
98};
99
Ian Rogers22d5e732014-07-15 22:23:51 -0700100// Handles that support assignment.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700101template<class T>
102class Handle : public ConstHandle<T> {
103 public:
104 Handle() {
105 }
Ian Rogers22d5e732014-07-15 22:23:51 -0700106
107 ALWAYS_INLINE Handle(const Handle<T>& handle) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700108 : ConstHandle<T>(handle.reference_) {
109 }
Ian Rogers22d5e732014-07-15 22:23:51 -0700110
111 ALWAYS_INLINE Handle<T>& operator=(const Handle<T>& handle)
112 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700113 ConstHandle<T>::operator=(handle);
114 return *this;
115 }
Ian Rogers22d5e732014-07-15 22:23:51 -0700116
117 ALWAYS_INLINE explicit Handle(StackReference<T>* reference)
118 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
119 : ConstHandle<T>(reference) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700120 }
Ian Rogers22d5e732014-07-15 22:23:51 -0700121
122 ALWAYS_INLINE T* Assign(T* reference) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers96664ad2014-10-21 15:05:36 -0700123 StackReference<mirror::Object>* ref = Handle<T>::GetReference();
124 T* old = down_cast<T*>(ref->AsMirrorPtr());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700125 ref->Assign(reference);
126 return old;
127 }
128
Ian Rogers22d5e732014-07-15 22:23:51 -0700129 template<typename S>
130 explicit Handle(const Handle<S>& handle) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
131 : ConstHandle<T>(handle) {
132 }
133
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700134 template<typename S>
135 explicit Handle(StackReference<S>* reference) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
136 : ConstHandle<T>(reference) {
137 }
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700138
139 private:
140 friend class BuildGenericJniFrameVisitor;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700141 friend class HandleScope;
142 template<class S> friend class HandleWrapper;
143 template<size_t kNumReferences> friend class StackHandleScope;
144};
145
Ian Rogers22d5e732014-07-15 22:23:51 -0700146// A special case of Handle that only holds references to null.
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700147template<class T>
148class NullHandle : public Handle<T> {
149 public:
150 NullHandle() : Handle<T>(&null_ref_) {
151 }
152
153 private:
Ian Rogers96664ad2014-10-21 15:05:36 -0700154 StackReference<mirror::Object> null_ref_;
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700155};
156
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700157} // namespace art
158
159#endif // ART_RUNTIME_HANDLE_H_