blob: 75a0391086ae2755c1b615dbf0fcb4b9abf7d238 [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 Chartier2d2621a2014-10-23 16:48:06 -070031inline StackHandleScope<kNumReferences>::StackHandleScope(Thread* self, mirror::Object* fill_value)
Ian Rogers59c07062014-10-10 13:03:39 -070032 : HandleScope(self->GetTopHandleScope(), kNumReferences), self_(self), pos_(0) {
Mathieu Chartierbef89c92015-01-09 09:46:49 -080033 DCHECK_EQ(self, Thread::Current());
Mathieu Chartiered150002015-08-28 11:16:54 -070034 if (kDebugLocking) {
35 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
36 }
Andreas Gampe575e78c2014-11-03 23:41:03 -080037 static_assert(kNumReferences >= 1, "StackHandleScope must contain at least 1 reference");
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070038 // TODO: Figure out how to use a compile assert.
Ian Rogers59c07062014-10-10 13:03:39 -070039 CHECK_EQ(&storage_[0], GetReferences());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070040 for (size_t i = 0; i < kNumReferences; ++i) {
Mathieu Chartier2d2621a2014-10-23 16:48:06 -070041 SetReference(i, fill_value);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070042 }
43 self_->PushHandleScope(this);
44}
45
46template<size_t kNumReferences>
Mathieu Chartier421c5372014-05-14 14:11:40 -070047inline StackHandleScope<kNumReferences>::~StackHandleScope() {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070048 HandleScope* top_handle_scope = self_->PopHandleScope();
49 DCHECK_EQ(top_handle_scope, this);
Mathieu Chartiered150002015-08-28 11:16:54 -070050 if (kDebugLocking) {
51 Locks::mutator_lock_->AssertSharedHeld(self_);
52 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070053}
54
Mathieu Chartier3e0acf62015-01-08 09:41:25 -080055inline size_t HandleScope::SizeOf(uint32_t num_references) {
56 size_t header_size = sizeof(HandleScope);
57 size_t data_size = sizeof(StackReference<mirror::Object>) * num_references;
58 return header_size + data_size;
59}
60
Andreas Gampe542451c2016-07-26 09:02:02 -070061inline size_t HandleScope::SizeOf(PointerSize pointer_size, uint32_t num_references) {
Mathieu Chartier3e0acf62015-01-08 09:41:25 -080062 // Assume that the layout is packed.
Andreas Gampe542451c2016-07-26 09:02:02 -070063 size_t header_size = ReferencesOffset(pointer_size);
Mathieu Chartier3e0acf62015-01-08 09:41:25 -080064 size_t data_size = sizeof(StackReference<mirror::Object>) * num_references;
65 return header_size + data_size;
66}
67
68inline mirror::Object* HandleScope::GetReference(size_t i) const {
69 DCHECK_LT(i, number_of_references_);
Mathieu Chartiered150002015-08-28 11:16:54 -070070 if (kDebugLocking) {
71 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
72 }
Mathieu Chartier3e0acf62015-01-08 09:41:25 -080073 return GetReferences()[i].AsMirrorPtr();
74}
75
76inline Handle<mirror::Object> HandleScope::GetHandle(size_t i) {
77 DCHECK_LT(i, number_of_references_);
78 return Handle<mirror::Object>(&GetReferences()[i]);
79}
80
81inline MutableHandle<mirror::Object> HandleScope::GetMutableHandle(size_t i) {
82 DCHECK_LT(i, number_of_references_);
83 return MutableHandle<mirror::Object>(&GetReferences()[i]);
84}
85
86inline void HandleScope::SetReference(size_t i, mirror::Object* object) {
Mathieu Chartiered150002015-08-28 11:16:54 -070087 if (kDebugLocking) {
88 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
89 }
Mathieu Chartier3e0acf62015-01-08 09:41:25 -080090 DCHECK_LT(i, number_of_references_);
91 GetReferences()[i].Assign(object);
92}
93
94inline bool HandleScope::Contains(StackReference<mirror::Object>* handle_scope_entry) const {
95 // A HandleScope should always contain something. One created by the
96 // jni_compiler should have a jobject/jclass as a native method is
97 // passed in a this pointer or a class
98 DCHECK_GT(number_of_references_, 0U);
99 return &GetReferences()[0] <= handle_scope_entry &&
100 handle_scope_entry <= &GetReferences()[number_of_references_ - 1];
101}
102
103template<size_t kNumReferences> template<class T>
104inline MutableHandle<T> StackHandleScope<kNumReferences>::NewHandle(T* object) {
105 SetReference(pos_, object);
106 MutableHandle<T> h(GetHandle<T>(pos_));
107 pos_++;
108 return h;
109}
110
Mathieu Chartier0795f232016-09-27 18:43:30 -0700111template<size_t kNumReferences> template<class MirrorType, bool kPoison>
112inline MutableHandle<MirrorType> StackHandleScope<kNumReferences>::NewHandle(
113 ObjPtr<MirrorType, kPoison> object) {
114 return NewHandle(object.Decode());
115}
116
Mathieu Chartier3e0acf62015-01-08 09:41:25 -0800117template<size_t kNumReferences> template<class T>
118inline HandleWrapper<T> StackHandleScope<kNumReferences>::NewHandleWrapper(T** object) {
Mathieu Chartier0795f232016-09-27 18:43:30 -0700119 return HandleWrapper<T>(object, NewHandle(*object));
120}
121
122template<size_t kNumReferences> template<class T>
123inline HandleWrapperObjPtr<T> StackHandleScope<kNumReferences>::NewHandleWrapper(
124 ObjPtr<T>* object) {
125 return HandleWrapperObjPtr<T>(object, NewHandle(*object));
Mathieu Chartier3e0acf62015-01-08 09:41:25 -0800126}
127
128template<size_t kNumReferences>
129inline void StackHandleScope<kNumReferences>::SetReference(size_t i, mirror::Object* object) {
Mathieu Chartiered150002015-08-28 11:16:54 -0700130 if (kDebugLocking) {
131 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
132 }
Mathieu Chartier3e0acf62015-01-08 09:41:25 -0800133 DCHECK_LT(i, kNumReferences);
134 VerifyObject(object);
135 GetReferences()[i].Assign(object);
136}
137
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700138} // namespace art
139
140#endif // ART_RUNTIME_HANDLE_SCOPE_INL_H_