blob: c55835dd007b08b9579de9132a9369ebb86e8f42 [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
Ian Rogers22d5e732014-07-15 22:23:51 -070033// HandleScopes are scoped objects containing a number of Handles. They are used to allocate
34// handles, for these handles (and the objects contained within them) to be visible/roots for the
35// GC. It is most common to stack allocate HandleScopes using StackHandleScope.
Mathieu Chartierbc56fc32014-06-03 15:37:03 -070036class PACKED(4) HandleScope {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070037 public:
38 ~HandleScope() {}
39
40 // Number of references contained within this handle scope.
41 uint32_t NumberOfReferences() const {
42 return number_of_references_;
43 }
44
45 // We have versions with and without explicit pointer size of the following. The first two are
46 // used at runtime, so OFFSETOF_MEMBER computes the right offsets automatically. The last one
47 // takes the pointer size explicitly so that at compile time we can cross-compile correctly.
48
49 // Returns the size of a HandleScope containing num_references handles.
50 static size_t SizeOf(uint32_t num_references) {
Mathieu Chartierbc56fc32014-06-03 15:37:03 -070051 size_t header_size = sizeof(HandleScope);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070052 size_t data_size = sizeof(StackReference<mirror::Object>) * num_references;
53 return header_size + data_size;
54 }
55
Andreas Gampecf4035a2014-05-28 22:43:01 -070056 // Returns the size of a HandleScope containing num_references handles.
57 static size_t SizeOf(size_t pointer_size, uint32_t num_references) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070058 // Assume that the layout is packed.
59 size_t header_size = pointer_size + sizeof(number_of_references_);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070060 size_t data_size = sizeof(StackReference<mirror::Object>) * num_references;
Andreas Gampecf4035a2014-05-28 22:43:01 -070061 return header_size + data_size;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070062 }
63
64 // Link to previous HandleScope or null.
65 HandleScope* GetLink() const {
66 return link_;
67 }
68
Ian Rogers59c07062014-10-10 13:03:39 -070069 ALWAYS_INLINE mirror::Object* GetReference(size_t i) const
70 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070071 DCHECK_LT(i, number_of_references_);
Ian Rogers59c07062014-10-10 13:03:39 -070072 return GetReferences()[i].AsMirrorPtr();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070073 }
74
Ian Rogers59c07062014-10-10 13:03:39 -070075 ALWAYS_INLINE Handle<mirror::Object> GetHandle(size_t i)
76 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070077 DCHECK_LT(i, number_of_references_);
Ian Rogers59c07062014-10-10 13:03:39 -070078 return Handle<mirror::Object>(&GetReferences()[i]);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070079 }
80
Ian Rogers59c07062014-10-10 13:03:39 -070081 ALWAYS_INLINE MutableHandle<mirror::Object> GetMutableHandle(size_t i)
82 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe5a4b8a22014-09-11 08:30:08 -070083 DCHECK_LT(i, number_of_references_);
Ian Rogers59c07062014-10-10 13:03:39 -070084 return MutableHandle<mirror::Object>(&GetReferences()[i]);
Andreas Gampe5a4b8a22014-09-11 08:30:08 -070085 }
86
Ian Rogers59c07062014-10-10 13:03:39 -070087 ALWAYS_INLINE void SetReference(size_t i, mirror::Object* object)
88 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070089 DCHECK_LT(i, number_of_references_);
Ian Rogers59c07062014-10-10 13:03:39 -070090 GetReferences()[i].Assign(object);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070091 }
92
93 bool Contains(StackReference<mirror::Object>* handle_scope_entry) const {
94 // A HandleScope should always contain something. One created by the
95 // jni_compiler should have a jobject/jclass as a native method is
96 // passed in a this pointer or a class
97 DCHECK_GT(number_of_references_, 0U);
Ian Rogers59c07062014-10-10 13:03:39 -070098 return &GetReferences()[0] <= handle_scope_entry &&
99 handle_scope_entry <= &GetReferences()[number_of_references_ - 1];
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700100 }
101
Ian Rogers59c07062014-10-10 13:03:39 -0700102 // Offset of link within HandleScope, used by generated code.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700103 static size_t LinkOffset(size_t pointer_size) {
104 return 0;
105 }
106
Ian Rogers59c07062014-10-10 13:03:39 -0700107 // Offset of length within handle scope, used by generated code.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700108 static size_t NumberOfReferencesOffset(size_t pointer_size) {
109 return pointer_size;
110 }
111
Ian Rogers59c07062014-10-10 13:03:39 -0700112 // Offset of link within handle scope, used by generated code.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700113 static size_t ReferencesOffset(size_t pointer_size) {
114 return pointer_size + sizeof(number_of_references_);
115 }
116
Ian Rogers59c07062014-10-10 13:03:39 -0700117 // Placement new creation.
118 static HandleScope* Create(void* storage, HandleScope* link, uint32_t num_references)
119 WARN_UNUSED {
120 return new (storage) HandleScope(link, num_references);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700121 }
122
Ian Rogers59c07062014-10-10 13:03:39 -0700123 protected:
124 // Return backing storage used for references.
125 ALWAYS_INLINE StackReference<mirror::Object>* GetReferences() const {
126 uintptr_t address = reinterpret_cast<uintptr_t>(this) + ReferencesOffset(sizeof(void*));
127 return reinterpret_cast<StackReference<mirror::Object>*>(address);
128 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700129
Mathieu Chartierd035c2d2014-10-27 17:30:20 -0700130 explicit HandleScope(size_t number_of_references) :
131 link_(nullptr), number_of_references_(number_of_references) {
132 }
133
Ian Rogers59c07062014-10-10 13:03:39 -0700134 // Semi-hidden constructor. Construction expected by generated code and StackHandleScope.
135 explicit HandleScope(HandleScope* link, uint32_t num_references) :
136 link_(link), number_of_references_(num_references) {
137 }
138
139 // Link-list of handle scopes. The root is held by a Thread.
140 HandleScope* const link_;
141
142 // Number of handlerized references.
143 const uint32_t number_of_references_;
144
145 // Storage for references.
146 // StackReference<mirror::Object> references_[number_of_references_]
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700147
148 private:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700149 DISALLOW_COPY_AND_ASSIGN(HandleScope);
150};
151
152// A wrapper which wraps around Object** and restores the pointer in the destructor.
153// TODO: Add more functionality.
154template<class T>
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700155class HandleWrapper : public MutableHandle<T> {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700156 public:
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700157 HandleWrapper(T** obj, const MutableHandle<T>& handle)
158 : MutableHandle<T>(handle), obj_(obj) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700159 }
160
161 ~HandleWrapper() {
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700162 *obj_ = MutableHandle<T>::Get();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700163 }
164
165 private:
Ian Rogersb5cb18a2014-10-21 15:05:36 -0700166 T** const obj_;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700167};
168
169// Scoped handle storage of a fixed size that is usually stack allocated.
170template<size_t kNumReferences>
Ian Rogers22d5e732014-07-15 22:23:51 -0700171class PACKED(4) StackHandleScope FINAL : public HandleScope {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700172 public:
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700173 explicit ALWAYS_INLINE StackHandleScope(Thread* self, mirror::Object* fill_value = nullptr);
174 ALWAYS_INLINE ~StackHandleScope();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700175
Ian Rogersb5cb18a2014-10-21 15:05:36 -0700176 template<class T>
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700177 ALWAYS_INLINE MutableHandle<T> NewHandle(T* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersb5cb18a2014-10-21 15:05:36 -0700178 SetReference(pos_, object);
179 MutableHandle<T> h(GetHandle<T>(pos_));
180 pos_++;
181 return h;
Mathieu Chartierbc56fc32014-06-03 15:37:03 -0700182 }
183
Ian Rogersb5cb18a2014-10-21 15:05:36 -0700184 template<class T>
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700185 ALWAYS_INLINE HandleWrapper<T> NewHandleWrapper(T** object)
186 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersb5cb18a2014-10-21 15:05:36 -0700187 SetReference(pos_, *object);
188 MutableHandle<T> h(GetHandle<T>(pos_));
189 pos_++;
190 return HandleWrapper<T>(object, h);
191 }
192
Ian Rogers59c07062014-10-10 13:03:39 -0700193 ALWAYS_INLINE void SetReference(size_t i, mirror::Object* object)
194 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
195 DCHECK_LT(i, kNumReferences);
196 GetReferences()[i].Assign(object);
Mathieu Chartierbc56fc32014-06-03 15:37:03 -0700197 }
198
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700199 private:
200 template<class T>
201 ALWAYS_INLINE MutableHandle<T> GetHandle(size_t i) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
202 DCHECK_LT(i, kNumReferences);
203 return MutableHandle<T>(&GetReferences()[i]);
204 }
205
Ian Rogers59c07062014-10-10 13:03:39 -0700206 // Reference storage needs to be first as expected by the HandleScope layout.
207 StackReference<mirror::Object> storage_[kNumReferences];
Ian Rogers22d5e732014-07-15 22:23:51 -0700208
209 // The thread that the stack handle scope is a linked list upon. The stack handle scope will
210 // push and pop itself from this thread.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700211 Thread* const self_;
Ian Rogers22d5e732014-07-15 22:23:51 -0700212
213 // Position new handles will be created.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700214 size_t pos_;
215
216 template<size_t kNumRefs> friend class StackHandleScope;
217};
218
219} // namespace art
220
221#endif // ART_RUNTIME_HANDLE_SCOPE_H_