blob: b212d095cbd1cd82dc50eeb67bef16d77732e1fc [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_INL_H_
18#define ART_RUNTIME_HANDLE_SCOPE_INL_H_
19
Ian Rogers22d5e732014-07-15 22:23:51 -070020#include "handle_scope.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070021
Mathieu Chartiered150002015-08-28 11:16:54 -070022#include "base/mutex.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070023#include "handle.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070024#include "obj_ptr-inl.h"
Mathieu Chartiered150002015-08-28 11:16:54 -070025#include "thread-inl.h"
Mathieu Chartier3e0acf62015-01-08 09:41:25 -080026#include "verify_object-inl.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070027
28namespace art {
29
30template<size_t kNumReferences>
Mathieu Chartiere8a3c572016-10-11 16:52:17 -070031inline FixedSizeHandleScope<kNumReferences>::FixedSizeHandleScope(BaseHandleScope* link,
32 mirror::Object* fill_value)
33 : HandleScope(link, kNumReferences) {
Mathieu Chartiered150002015-08-28 11:16:54 -070034 if (kDebugLocking) {
35 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
36 }
Mathieu Chartiere8a3c572016-10-11 16:52:17 -070037 static_assert(kNumReferences >= 1, "FixedSizeHandleScope must contain at least 1 reference");
38 DCHECK_EQ(&storage_[0], GetReferences()); // TODO: Figure out how to use a compile assert.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070039 for (size_t i = 0; i < kNumReferences; ++i) {
Mathieu Chartier2d2621a2014-10-23 16:48:06 -070040 SetReference(i, fill_value);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070041 }
Mathieu Chartiere8a3c572016-10-11 16:52:17 -070042}
43
44template<size_t kNumReferences>
45inline StackHandleScope<kNumReferences>::StackHandleScope(Thread* self, mirror::Object* fill_value)
46 : FixedSizeHandleScope<kNumReferences>(self->GetTopHandleScope(), fill_value),
47 self_(self) {
48 DCHECK_EQ(self, Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070049 self_->PushHandleScope(this);
50}
51
52template<size_t kNumReferences>
Mathieu Chartier421c5372014-05-14 14:11:40 -070053inline StackHandleScope<kNumReferences>::~StackHandleScope() {
Mathieu Chartiere8a3c572016-10-11 16:52:17 -070054 BaseHandleScope* top_handle_scope = self_->PopHandleScope();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070055 DCHECK_EQ(top_handle_scope, this);
Mathieu Chartiered150002015-08-28 11:16:54 -070056 if (kDebugLocking) {
57 Locks::mutator_lock_->AssertSharedHeld(self_);
58 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070059}
60
Mathieu Chartier3e0acf62015-01-08 09:41:25 -080061inline size_t HandleScope::SizeOf(uint32_t num_references) {
62 size_t header_size = sizeof(HandleScope);
63 size_t data_size = sizeof(StackReference<mirror::Object>) * num_references;
64 return header_size + data_size;
65}
66
Andreas Gampe542451c2016-07-26 09:02:02 -070067inline size_t HandleScope::SizeOf(PointerSize pointer_size, uint32_t num_references) {
Mathieu Chartier3e0acf62015-01-08 09:41:25 -080068 // Assume that the layout is packed.
Andreas Gampe542451c2016-07-26 09:02:02 -070069 size_t header_size = ReferencesOffset(pointer_size);
Mathieu Chartier3e0acf62015-01-08 09:41:25 -080070 size_t data_size = sizeof(StackReference<mirror::Object>) * num_references;
71 return header_size + data_size;
72}
73
74inline mirror::Object* HandleScope::GetReference(size_t i) const {
Mathieu Chartiere8a3c572016-10-11 16:52:17 -070075 DCHECK_LT(i, NumberOfReferences());
Mathieu Chartiered150002015-08-28 11:16:54 -070076 if (kDebugLocking) {
77 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
78 }
Mathieu Chartier3e0acf62015-01-08 09:41:25 -080079 return GetReferences()[i].AsMirrorPtr();
80}
81
82inline Handle<mirror::Object> HandleScope::GetHandle(size_t i) {
Mathieu Chartiere8a3c572016-10-11 16:52:17 -070083 DCHECK_LT(i, NumberOfReferences());
Mathieu Chartier3e0acf62015-01-08 09:41:25 -080084 return Handle<mirror::Object>(&GetReferences()[i]);
85}
86
87inline MutableHandle<mirror::Object> HandleScope::GetMutableHandle(size_t i) {
Mathieu Chartiere8a3c572016-10-11 16:52:17 -070088 DCHECK_LT(i, NumberOfReferences());
Mathieu Chartier3e0acf62015-01-08 09:41:25 -080089 return MutableHandle<mirror::Object>(&GetReferences()[i]);
90}
91
92inline void HandleScope::SetReference(size_t i, mirror::Object* object) {
Mathieu Chartiered150002015-08-28 11:16:54 -070093 if (kDebugLocking) {
94 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
95 }
Mathieu Chartiere8a3c572016-10-11 16:52:17 -070096 DCHECK_LT(i, NumberOfReferences());
Mathieu Chartier3e0acf62015-01-08 09:41:25 -080097 GetReferences()[i].Assign(object);
98}
99
100inline bool HandleScope::Contains(StackReference<mirror::Object>* handle_scope_entry) const {
101 // A HandleScope should always contain something. One created by the
102 // jni_compiler should have a jobject/jclass as a native method is
103 // passed in a this pointer or a class
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700104 DCHECK_GT(NumberOfReferences(), 0U);
Mathieu Chartier3e0acf62015-01-08 09:41:25 -0800105 return &GetReferences()[0] <= handle_scope_entry &&
106 handle_scope_entry <= &GetReferences()[number_of_references_ - 1];
107}
108
109template<size_t kNumReferences> template<class T>
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700110inline MutableHandle<T> FixedSizeHandleScope<kNumReferences>::NewHandle(T* object) {
Mathieu Chartier3e0acf62015-01-08 09:41:25 -0800111 SetReference(pos_, object);
112 MutableHandle<T> h(GetHandle<T>(pos_));
113 pos_++;
114 return h;
115}
116
Mathieu Chartier0795f232016-09-27 18:43:30 -0700117template<size_t kNumReferences> template<class MirrorType, bool kPoison>
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700118inline MutableHandle<MirrorType> FixedSizeHandleScope<kNumReferences>::NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700119 ObjPtr<MirrorType, kPoison> object) {
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700120 return NewHandle(object.Ptr());
Mathieu Chartier0795f232016-09-27 18:43:30 -0700121}
122
Mathieu Chartier3e0acf62015-01-08 09:41:25 -0800123template<size_t kNumReferences> template<class T>
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700124inline HandleWrapper<T> FixedSizeHandleScope<kNumReferences>::NewHandleWrapper(T** object) {
Mathieu Chartier0795f232016-09-27 18:43:30 -0700125 return HandleWrapper<T>(object, NewHandle(*object));
126}
127
128template<size_t kNumReferences> template<class T>
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700129inline HandleWrapperObjPtr<T> FixedSizeHandleScope<kNumReferences>::NewHandleWrapper(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700130 ObjPtr<T>* object) {
131 return HandleWrapperObjPtr<T>(object, NewHandle(*object));
Mathieu Chartier3e0acf62015-01-08 09:41:25 -0800132}
133
134template<size_t kNumReferences>
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700135inline void FixedSizeHandleScope<kNumReferences>::SetReference(size_t i, mirror::Object* object) {
Mathieu Chartiered150002015-08-28 11:16:54 -0700136 if (kDebugLocking) {
137 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
138 }
Mathieu Chartier3e0acf62015-01-08 09:41:25 -0800139 DCHECK_LT(i, kNumReferences);
140 VerifyObject(object);
141 GetReferences()[i].Assign(object);
142}
143
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700144// Number of references contained within this handle scope.
145inline uint32_t BaseHandleScope::NumberOfReferences() const {
146 return LIKELY(!IsVariableSized())
147 ? AsHandleScope()->NumberOfReferences()
148 : AsVariableSized()->NumberOfReferences();
149}
150
151inline bool BaseHandleScope::Contains(StackReference<mirror::Object>* handle_scope_entry) const {
152 return LIKELY(!IsVariableSized())
153 ? AsHandleScope()->Contains(handle_scope_entry)
154 : AsVariableSized()->Contains(handle_scope_entry);
155}
156
157template <typename Visitor>
158inline void BaseHandleScope::VisitRoots(Visitor& visitor) {
159 if (LIKELY(!IsVariableSized())) {
160 AsHandleScope()->VisitRoots(visitor);
161 } else {
162 AsVariableSized()->VisitRoots(visitor);
163 }
164}
165
166inline VariableSizedHandleScope* BaseHandleScope::AsVariableSized() {
167 DCHECK(IsVariableSized());
168 return down_cast<VariableSizedHandleScope*>(this);
169}
170
171inline HandleScope* BaseHandleScope::AsHandleScope() {
172 DCHECK(!IsVariableSized());
173 return down_cast<HandleScope*>(this);
174}
175
176inline const VariableSizedHandleScope* BaseHandleScope::AsVariableSized() const {
177 DCHECK(IsVariableSized());
178 return down_cast<const VariableSizedHandleScope*>(this);
179}
180
181inline const HandleScope* BaseHandleScope::AsHandleScope() const {
182 DCHECK(!IsVariableSized());
183 return down_cast<const HandleScope*>(this);
184}
185
186template<class T>
187MutableHandle<T> VariableSizedHandleScope::NewHandle(T* object) {
188 if (current_scope_->RemainingSlots() == 0) {
189 current_scope_ = new LocalScopeType(current_scope_);
190 }
191 return current_scope_->NewHandle(object);
192}
193
Mathieu Chartier3398c782016-09-30 10:27:43 -0700194template<class MirrorType, bool kPoison>
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700195inline MutableHandle<MirrorType> VariableSizedHandleScope::NewHandle(
Mathieu Chartier3398c782016-09-30 10:27:43 -0700196 ObjPtr<MirrorType, kPoison> ptr) {
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700197 return NewHandle(ptr.Ptr());
Mathieu Chartier3398c782016-09-30 10:27:43 -0700198}
199
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700200inline VariableSizedHandleScope::VariableSizedHandleScope(Thread* const self)
201 : BaseHandleScope(self->GetTopHandleScope()),
202 self_(self) {
203 current_scope_ = new LocalScopeType(/*link*/ nullptr);
204 self_->PushHandleScope(this);
205}
206
207inline VariableSizedHandleScope::~VariableSizedHandleScope() {
208 BaseHandleScope* top_handle_scope = self_->PopHandleScope();
209 DCHECK_EQ(top_handle_scope, this);
210 while (current_scope_ != nullptr) {
211 LocalScopeType* next = reinterpret_cast<LocalScopeType*>(current_scope_->GetLink());
212 delete current_scope_;
213 current_scope_ = next;
214 }
215}
216
217inline uint32_t VariableSizedHandleScope::NumberOfReferences() const {
218 uint32_t sum = 0;
219 const LocalScopeType* cur = current_scope_;
220 while (cur != nullptr) {
221 sum += cur->NumberOfReferences();
222 cur = reinterpret_cast<const LocalScopeType*>(cur->GetLink());
223 }
224 return sum;
225}
226
227inline bool VariableSizedHandleScope::Contains(StackReference<mirror::Object>* handle_scope_entry)
228 const {
229 const LocalScopeType* cur = current_scope_;
230 while (cur != nullptr) {
231 if (cur->Contains(handle_scope_entry)) {
232 return true;
233 }
234 cur = reinterpret_cast<const LocalScopeType*>(cur->GetLink());
235 }
236 return false;
237}
238
239template <typename Visitor>
240inline void VariableSizedHandleScope::VisitRoots(Visitor& visitor) {
241 LocalScopeType* cur = current_scope_;
242 while (cur != nullptr) {
243 cur->VisitRoots(visitor);
244 cur = reinterpret_cast<LocalScopeType*>(cur->GetLink());
245 }
246}
247
248
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700249} // namespace art
250
251#endif // ART_RUNTIME_HANDLE_SCOPE_INL_H_