blob: 421a413fb16dfbf91299ba14c1c80971816e3c36 [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
22#include "handle.h"
23#include "thread.h"
Mathieu Chartier3e0acf62015-01-08 09:41:25 -080024#include "verify_object-inl.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070025
26namespace art {
27
28template<size_t kNumReferences>
Mathieu Chartier2d2621a2014-10-23 16:48:06 -070029inline StackHandleScope<kNumReferences>::StackHandleScope(Thread* self, mirror::Object* fill_value)
Ian Rogers59c07062014-10-10 13:03:39 -070030 : HandleScope(self->GetTopHandleScope(), kNumReferences), self_(self), pos_(0) {
Andreas Gampe575e78c2014-11-03 23:41:03 -080031 static_assert(kNumReferences >= 1, "StackHandleScope must contain at least 1 reference");
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070032 // TODO: Figure out how to use a compile assert.
Ian Rogers59c07062014-10-10 13:03:39 -070033 CHECK_EQ(&storage_[0], GetReferences());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070034 for (size_t i = 0; i < kNumReferences; ++i) {
Mathieu Chartier2d2621a2014-10-23 16:48:06 -070035 SetReference(i, fill_value);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070036 }
37 self_->PushHandleScope(this);
38}
39
40template<size_t kNumReferences>
Mathieu Chartier421c5372014-05-14 14:11:40 -070041inline StackHandleScope<kNumReferences>::~StackHandleScope() {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070042 HandleScope* top_handle_scope = self_->PopHandleScope();
43 DCHECK_EQ(top_handle_scope, this);
44}
45
Mathieu Chartier3e0acf62015-01-08 09:41:25 -080046inline size_t HandleScope::SizeOf(uint32_t num_references) {
47 size_t header_size = sizeof(HandleScope);
48 size_t data_size = sizeof(StackReference<mirror::Object>) * num_references;
49 return header_size + data_size;
50}
51
52inline size_t HandleScope::SizeOf(size_t pointer_size, uint32_t num_references) {
53 // Assume that the layout is packed.
54 size_t header_size = pointer_size + sizeof(number_of_references_);
55 size_t data_size = sizeof(StackReference<mirror::Object>) * num_references;
56 return header_size + data_size;
57}
58
59inline mirror::Object* HandleScope::GetReference(size_t i) const {
60 DCHECK_LT(i, number_of_references_);
61 return GetReferences()[i].AsMirrorPtr();
62}
63
64inline Handle<mirror::Object> HandleScope::GetHandle(size_t i) {
65 DCHECK_LT(i, number_of_references_);
66 return Handle<mirror::Object>(&GetReferences()[i]);
67}
68
69inline MutableHandle<mirror::Object> HandleScope::GetMutableHandle(size_t i) {
70 DCHECK_LT(i, number_of_references_);
71 return MutableHandle<mirror::Object>(&GetReferences()[i]);
72}
73
74inline void HandleScope::SetReference(size_t i, mirror::Object* object) {
75 DCHECK_LT(i, number_of_references_);
76 GetReferences()[i].Assign(object);
77}
78
79inline bool HandleScope::Contains(StackReference<mirror::Object>* handle_scope_entry) const {
80 // A HandleScope should always contain something. One created by the
81 // jni_compiler should have a jobject/jclass as a native method is
82 // passed in a this pointer or a class
83 DCHECK_GT(number_of_references_, 0U);
84 return &GetReferences()[0] <= handle_scope_entry &&
85 handle_scope_entry <= &GetReferences()[number_of_references_ - 1];
86}
87
88template<size_t kNumReferences> template<class T>
89inline MutableHandle<T> StackHandleScope<kNumReferences>::NewHandle(T* object) {
90 SetReference(pos_, object);
91 MutableHandle<T> h(GetHandle<T>(pos_));
92 pos_++;
93 return h;
94}
95
96template<size_t kNumReferences> template<class T>
97inline HandleWrapper<T> StackHandleScope<kNumReferences>::NewHandleWrapper(T** object) {
98 SetReference(pos_, *object);
99 MutableHandle<T> h(GetHandle<T>(pos_));
100 pos_++;
101 return HandleWrapper<T>(object, h);
102}
103
104template<size_t kNumReferences>
105inline void StackHandleScope<kNumReferences>::SetReference(size_t i, mirror::Object* object) {
106 DCHECK_LT(i, kNumReferences);
107 VerifyObject(object);
108 GetReferences()[i].Assign(object);
109}
110
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700111} // namespace art
112
113#endif // ART_RUNTIME_HANDLE_SCOPE_INL_H_