blob: d4c13d4676792b074a0e4a695ec8adc271f4d472 [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"
Vladimir Marko3a21e382016-09-02 12:38:38 +010023#include "base/mutex.h"
Ian Rogersb5cb18a2014-10-21 15:05:36 -070024#include "base/value_object.h"
Vladimir Marko3a21e382016-09-02 12:38:38 +010025#include "jni.h"
26#include "stack_reference.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070027
28namespace art {
29
30class Thread;
31
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070032template<class T> class Handle;
33
Ian Rogers22d5e732014-07-15 22:23:51 -070034// Handles are memory locations that contain GC roots. As the mirror::Object*s within a handle are
35// 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 -070036// a wrap pointer. Handles are generally allocated within HandleScopes. Handle is a super-class
37// of MutableHandle and doesn't support assignment operations.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070038template<class T>
Ian Rogersb5cb18a2014-10-21 15:05:36 -070039class Handle : public ValueObject {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070040 public:
Andreas Gampe5a4b8a22014-09-11 08:30:08 -070041 Handle() : reference_(nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070042 }
Ian Rogers22d5e732014-07-15 22:23:51 -070043
Andreas Gampe5a4b8a22014-09-11 08:30:08 -070044 ALWAYS_INLINE Handle(const Handle<T>& handle) : reference_(handle.reference_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070045 }
Ian Rogers22d5e732014-07-15 22:23:51 -070046
Andreas Gampe5a4b8a22014-09-11 08:30:08 -070047 ALWAYS_INLINE Handle<T>& operator=(const Handle<T>& handle) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070048 reference_ = handle.reference_;
49 return *this;
50 }
Ian Rogers22d5e732014-07-15 22:23:51 -070051
Andreas Gampe5a4b8a22014-09-11 08:30:08 -070052 ALWAYS_INLINE explicit Handle(StackReference<T>* reference) : reference_(reference) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070053 }
Ian Rogers22d5e732014-07-15 22:23:51 -070054
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070055 ALWAYS_INLINE T& operator*() const REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070056 return *Get();
57 }
Ian Rogers22d5e732014-07-15 22:23:51 -070058
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070059 ALWAYS_INLINE T* operator->() const REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070060 return Get();
61 }
Ian Rogers22d5e732014-07-15 22:23:51 -070062
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070063 ALWAYS_INLINE T* Get() const REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersb5cb18a2014-10-21 15:05:36 -070064 return down_cast<T*>(reference_->AsMirrorPtr());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070065 }
Ian Rogers22d5e732014-07-15 22:23:51 -070066
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070067 ALWAYS_INLINE jobject ToJObject() const REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -070068 if (UNLIKELY(reference_->AsMirrorPtr() == nullptr)) {
Mathieu Chartier9865bde2015-12-21 09:58:16 -080069 // Special case so that we work with null handles.
Mathieu Chartier0cd81352014-05-22 16:48:55 -070070 return nullptr;
71 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070072 return reinterpret_cast<jobject>(reference_);
73 }
74
Vladimir Markof39745e2016-01-26 12:16:55 +000075 ALWAYS_INLINE StackReference<mirror::Object>* GetReference() {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070076 return reference_;
77 }
78
Vladimir Marko86973552016-01-26 15:06:15 +000079 ALWAYS_INLINE const StackReference<mirror::Object>* GetReference() const {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070080 return reference_;
81 }
82
Mathieu Chartier0cd81352014-05-22 16:48:55 -070083 protected:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070084 template<typename S>
Andreas Gampe5a4b8a22014-09-11 08:30:08 -070085 explicit Handle(StackReference<S>* reference)
Ian Rogersb5cb18a2014-10-21 15:05:36 -070086 : reference_(reference) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070087 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070088 template<typename S>
Andreas Gampe5a4b8a22014-09-11 08:30:08 -070089 explicit Handle(const Handle<S>& handle)
Ian Rogersb5cb18a2014-10-21 15:05:36 -070090 : reference_(handle.reference_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070091 }
92
Ian Rogersb5cb18a2014-10-21 15:05:36 -070093 StackReference<mirror::Object>* reference_;
94
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070095 private:
96 friend class BuildGenericJniFrameVisitor;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -070097 template<class S> friend class Handle;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070098 friend class HandleScope;
99 template<class S> friend class HandleWrapper;
100 template<size_t kNumReferences> friend class StackHandleScope;
101};
102
Ian Rogers22d5e732014-07-15 22:23:51 -0700103// Handles that support assignment.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700104template<class T>
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700105class MutableHandle : public Handle<T> {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700106 public:
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700107 MutableHandle() {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700108 }
Ian Rogers22d5e732014-07-15 22:23:51 -0700109
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700110 ALWAYS_INLINE MutableHandle(const MutableHandle<T>& handle)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700111 REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700112 : Handle<T>(handle.reference_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700113 }
Ian Rogers22d5e732014-07-15 22:23:51 -0700114
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700115 ALWAYS_INLINE MutableHandle<T>& operator=(const MutableHandle<T>& handle)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700116 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700117 Handle<T>::operator=(handle);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700118 return *this;
119 }
Ian Rogers22d5e732014-07-15 22:23:51 -0700120
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700121 ALWAYS_INLINE explicit MutableHandle(StackReference<T>* reference)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700122 REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700123 : Handle<T>(reference) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700124 }
Ian Rogers22d5e732014-07-15 22:23:51 -0700125
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700126 ALWAYS_INLINE T* Assign(T* reference) REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersb5cb18a2014-10-21 15:05:36 -0700127 StackReference<mirror::Object>* ref = Handle<T>::GetReference();
128 T* old = down_cast<T*>(ref->AsMirrorPtr());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700129 ref->Assign(reference);
130 return old;
131 }
132
Ian Rogers22d5e732014-07-15 22:23:51 -0700133 template<typename S>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700134 explicit MutableHandle(const MutableHandle<S>& handle) REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700135 : Handle<T>(handle) {
Ian Rogers22d5e732014-07-15 22:23:51 -0700136 }
137
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700138 template<typename S>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700139 explicit MutableHandle(StackReference<S>* reference) REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700140 : Handle<T>(reference) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700141 }
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700142
143 private:
144 friend class BuildGenericJniFrameVisitor;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700145 friend class HandleScope;
146 template<class S> friend class HandleWrapper;
147 template<size_t kNumReferences> friend class StackHandleScope;
148};
149
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800150// A special case of Handle that only holds references to null. Invalid when if it goes out of
151// scope. Example: Handle<T> h = ScopedNullHandle<T> will leave h being undefined.
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700152template<class T>
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800153class ScopedNullHandle : public Handle<T> {
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700154 public:
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800155 ScopedNullHandle() : Handle<T>(&null_ref_) {}
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700156
157 private:
Ian Rogersb5cb18a2014-10-21 15:05:36 -0700158 StackReference<mirror::Object> null_ref_;
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700159};
160
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700161} // namespace art
162
163#endif // ART_RUNTIME_HANDLE_H_