blob: 2fd42d2350c123fce682edb289d79af5330e33ed [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_SCOPE_H_
18#define ART_RUNTIME_HANDLE_SCOPE_H_
19
20#include "base/logging.h"
21#include "base/macros.h"
22#include "handle.h"
23#include "stack.h"
24#include "utils.h"
25
26namespace art {
27namespace mirror {
28class Object;
29}
Ian Rogerse63db272014-07-15 15:36:11 -070030
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070031class Thread;
32
33// HandleScopes can be allocated within the bridge frame between managed and native code backed by
34// stack storage or manually allocated in native.
Mathieu Chartierbc56fc32014-06-03 15:37:03 -070035class PACKED(4) HandleScope {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070036 public:
37 ~HandleScope() {}
38
39 // Number of references contained within this handle scope.
40 uint32_t NumberOfReferences() const {
41 return number_of_references_;
42 }
43
44 // We have versions with and without explicit pointer size of the following. The first two are
45 // used at runtime, so OFFSETOF_MEMBER computes the right offsets automatically. The last one
46 // takes the pointer size explicitly so that at compile time we can cross-compile correctly.
47
48 // Returns the size of a HandleScope containing num_references handles.
49 static size_t SizeOf(uint32_t num_references) {
Mathieu Chartierbc56fc32014-06-03 15:37:03 -070050 size_t header_size = sizeof(HandleScope);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070051 size_t data_size = sizeof(StackReference<mirror::Object>) * num_references;
52 return header_size + data_size;
53 }
54
Andreas Gampecf4035a2014-05-28 22:43:01 -070055 // Returns the size of a HandleScope containing num_references handles.
56 static size_t SizeOf(size_t pointer_size, uint32_t num_references) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070057 // Assume that the layout is packed.
58 size_t header_size = pointer_size + sizeof(number_of_references_);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070059 size_t data_size = sizeof(StackReference<mirror::Object>) * num_references;
Andreas Gampecf4035a2014-05-28 22:43:01 -070060 return header_size + data_size;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070061 }
62
63 // Link to previous HandleScope or null.
64 HandleScope* GetLink() const {
65 return link_;
66 }
67
68 void SetLink(HandleScope* link) {
69 DCHECK_NE(this, link);
70 link_ = link;
71 }
72
73 // Sets the number_of_references_ field for constructing tables out of raw memory. Warning: will
74 // not resize anything.
75 void SetNumberOfReferences(uint32_t num_references) {
76 number_of_references_ = num_references;
77 }
78
79 mirror::Object* GetReference(size_t i) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
80 ALWAYS_INLINE {
81 DCHECK_LT(i, number_of_references_);
82 return references_[i].AsMirrorPtr();
83 }
84
85 Handle<mirror::Object> GetHandle(size_t i) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
86 ALWAYS_INLINE {
87 DCHECK_LT(i, number_of_references_);
88 return Handle<mirror::Object>(&references_[i]);
89 }
90
91 void SetReference(size_t i, mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
92 ALWAYS_INLINE {
93 DCHECK_LT(i, number_of_references_);
94 references_[i].Assign(object);
95 }
96
97 bool Contains(StackReference<mirror::Object>* handle_scope_entry) const {
98 // A HandleScope should always contain something. One created by the
99 // jni_compiler should have a jobject/jclass as a native method is
100 // passed in a this pointer or a class
101 DCHECK_GT(number_of_references_, 0U);
Mathieu Chartierbc56fc32014-06-03 15:37:03 -0700102 return &references_[0] <= handle_scope_entry &&
103 handle_scope_entry <= &references_[number_of_references_ - 1];
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700104 }
105
106 // Offset of link within HandleScope, used by generated code
107 static size_t LinkOffset(size_t pointer_size) {
108 return 0;
109 }
110
111 // Offset of length within handle scope, used by generated code
112 static size_t NumberOfReferencesOffset(size_t pointer_size) {
113 return pointer_size;
114 }
115
116 // Offset of link within handle scope, used by generated code
117 static size_t ReferencesOffset(size_t pointer_size) {
118 return pointer_size + sizeof(number_of_references_);
119 }
120
121 protected:
122 explicit HandleScope(size_t number_of_references) :
123 link_(nullptr), number_of_references_(number_of_references) {
124 }
125
126 HandleScope* link_;
127 uint32_t number_of_references_;
128
129 // number_of_references_ are available if this is allocated and filled in by jni_compiler.
130 StackReference<mirror::Object> references_[0];
131
132 private:
133 template<size_t kNumReferences> friend class StackHandleScope;
134 DISALLOW_COPY_AND_ASSIGN(HandleScope);
135};
136
137// A wrapper which wraps around Object** and restores the pointer in the destructor.
138// TODO: Add more functionality.
139template<class T>
Mathieu Chartierdb2633c2014-05-16 09:59:29 -0700140class HandleWrapper : public Handle<T> {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700141 public:
142 HandleWrapper(T** obj, const Handle<T>& handle)
Mathieu Chartierdb2633c2014-05-16 09:59:29 -0700143 : Handle<T>(handle), obj_(obj) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700144 }
145
146 ~HandleWrapper() {
Mathieu Chartierdb2633c2014-05-16 09:59:29 -0700147 *obj_ = Handle<T>::Get();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700148 }
149
150 private:
151 T** obj_;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700152};
153
154// Scoped handle storage of a fixed size that is usually stack allocated.
155template<size_t kNumReferences>
Mathieu Chartierbc56fc32014-06-03 15:37:03 -0700156class PACKED(4) StackHandleScope : public HandleScope {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700157 public:
158 explicit StackHandleScope(Thread* self);
159 ~StackHandleScope();
160
Mathieu Chartierbc56fc32014-06-03 15:37:03 -0700161 // Currently unused, using this GetReference instead of the one in HandleScope is preferred to
162 // avoid compiler optimizations incorrectly optimizing out of bound array accesses.
163 // TODO: Remove this when it is un-necessary.
164 mirror::Object* GetReference(size_t i) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
165 ALWAYS_INLINE {
166 DCHECK_LT(i, number_of_references_);
167 return references_storage_[i].AsMirrorPtr();
168 }
169
170 Handle<mirror::Object> GetHandle(size_t i) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
171 ALWAYS_INLINE {
172 DCHECK_LT(i, number_of_references_);
173 return Handle<mirror::Object>(&references_storage_[i]);
174 }
175
176 void SetReference(size_t i, mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
177 ALWAYS_INLINE {
178 DCHECK_LT(i, number_of_references_);
179 references_storage_[i].Assign(object);
180 }
181
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700182 template<class T>
183 Handle<T> NewHandle(T* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
184 SetReference(pos_, object);
185 return Handle<T>(GetHandle(pos_++));
186 }
187
188 template<class T>
189 HandleWrapper<T> NewHandleWrapper(T** object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
190 SetReference(pos_, *object);
191 Handle<T> h(GetHandle(pos_++));
192 return HandleWrapper<T>(object, h);
193 }
194
195 private:
196 // references_storage_ needs to be first so that it matches the address of references_.
197 StackReference<mirror::Object> references_storage_[kNumReferences];
198 Thread* const self_;
199 size_t pos_;
200
201 template<size_t kNumRefs> friend class StackHandleScope;
202};
203
204} // namespace art
205
206#endif // ART_RUNTIME_HANDLE_SCOPE_H_