blob: 271312ea9fbd37ccf03e98ef58f1438391778a10 [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
Calin Juravleacf735c2015-02-12 15:25:22 +000020#include <stack>
21
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070022#include "base/logging.h"
23#include "base/macros.h"
24#include "handle.h"
25#include "stack.h"
26#include "utils.h"
Mathieu Chartier3e0acf62015-01-08 09:41:25 -080027#include "verify_object.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070028
29namespace art {
30namespace mirror {
31class Object;
32}
Ian Rogerse63db272014-07-15 15:36:11 -070033
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070034class Thread;
35
Ian Rogers22d5e732014-07-15 22:23:51 -070036// HandleScopes are scoped objects containing a number of Handles. They are used to allocate
37// handles, for these handles (and the objects contained within them) to be visible/roots for the
38// GC. It is most common to stack allocate HandleScopes using StackHandleScope.
Mathieu Chartierbc56fc32014-06-03 15:37:03 -070039class PACKED(4) HandleScope {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070040 public:
41 ~HandleScope() {}
42
43 // Number of references contained within this handle scope.
44 uint32_t NumberOfReferences() const {
45 return number_of_references_;
46 }
47
48 // We have versions with and without explicit pointer size of the following. The first two are
49 // used at runtime, so OFFSETOF_MEMBER computes the right offsets automatically. The last one
50 // takes the pointer size explicitly so that at compile time we can cross-compile correctly.
51
52 // Returns the size of a HandleScope containing num_references handles.
Mathieu Chartier3e0acf62015-01-08 09:41:25 -080053 static size_t SizeOf(uint32_t num_references);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070054
Andreas Gampecf4035a2014-05-28 22:43:01 -070055 // Returns the size of a HandleScope containing num_references handles.
Mathieu Chartier3e0acf62015-01-08 09:41:25 -080056 static size_t SizeOf(size_t pointer_size, uint32_t num_references);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070057
58 // Link to previous HandleScope or null.
59 HandleScope* GetLink() const {
60 return link_;
61 }
62
Ian Rogers59c07062014-10-10 13:03:39 -070063 ALWAYS_INLINE mirror::Object* GetReference(size_t i) const
Mathieu Chartier3e0acf62015-01-08 09:41:25 -080064 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070065
Ian Rogers59c07062014-10-10 13:03:39 -070066 ALWAYS_INLINE Handle<mirror::Object> GetHandle(size_t i)
Mathieu Chartier3e0acf62015-01-08 09:41:25 -080067 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070068
Ian Rogers59c07062014-10-10 13:03:39 -070069 ALWAYS_INLINE MutableHandle<mirror::Object> GetMutableHandle(size_t i)
Mathieu Chartier3e0acf62015-01-08 09:41:25 -080070 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Andreas Gampe5a4b8a22014-09-11 08:30:08 -070071
Ian Rogers59c07062014-10-10 13:03:39 -070072 ALWAYS_INLINE void SetReference(size_t i, mirror::Object* object)
Mathieu Chartier3e0acf62015-01-08 09:41:25 -080073 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070074
Mathieu Chartier3e0acf62015-01-08 09:41:25 -080075 ALWAYS_INLINE bool Contains(StackReference<mirror::Object>* handle_scope_entry) const;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070076
Ian Rogers59c07062014-10-10 13:03:39 -070077 // Offset of link within HandleScope, used by generated code.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070078 static size_t LinkOffset(size_t pointer_size ATTRIBUTE_UNUSED) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070079 return 0;
80 }
81
Ian Rogers59c07062014-10-10 13:03:39 -070082 // Offset of length within handle scope, used by generated code.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070083 static size_t NumberOfReferencesOffset(size_t pointer_size) {
84 return pointer_size;
85 }
86
Ian Rogers59c07062014-10-10 13:03:39 -070087 // Offset of link within handle scope, used by generated code.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070088 static size_t ReferencesOffset(size_t pointer_size) {
89 return pointer_size + sizeof(number_of_references_);
90 }
91
Ian Rogers59c07062014-10-10 13:03:39 -070092 // Placement new creation.
93 static HandleScope* Create(void* storage, HandleScope* link, uint32_t num_references)
94 WARN_UNUSED {
95 return new (storage) HandleScope(link, num_references);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070096 }
97
Ian Rogers59c07062014-10-10 13:03:39 -070098 protected:
99 // Return backing storage used for references.
100 ALWAYS_INLINE StackReference<mirror::Object>* GetReferences() const {
101 uintptr_t address = reinterpret_cast<uintptr_t>(this) + ReferencesOffset(sizeof(void*));
102 return reinterpret_cast<StackReference<mirror::Object>*>(address);
103 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700104
Mathieu Chartierd035c2d2014-10-27 17:30:20 -0700105 explicit HandleScope(size_t number_of_references) :
106 link_(nullptr), number_of_references_(number_of_references) {
107 }
108
Ian Rogers59c07062014-10-10 13:03:39 -0700109 // Semi-hidden constructor. Construction expected by generated code and StackHandleScope.
110 explicit HandleScope(HandleScope* link, uint32_t num_references) :
111 link_(link), number_of_references_(num_references) {
112 }
113
114 // Link-list of handle scopes. The root is held by a Thread.
115 HandleScope* const link_;
116
117 // Number of handlerized references.
118 const uint32_t number_of_references_;
119
120 // Storage for references.
121 // StackReference<mirror::Object> references_[number_of_references_]
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700122
123 private:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700124 DISALLOW_COPY_AND_ASSIGN(HandleScope);
125};
126
127// A wrapper which wraps around Object** and restores the pointer in the destructor.
128// TODO: Add more functionality.
129template<class T>
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700130class HandleWrapper : public MutableHandle<T> {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700131 public:
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700132 HandleWrapper(T** obj, const MutableHandle<T>& handle)
133 : MutableHandle<T>(handle), obj_(obj) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700134 }
135
Andreas Gampe758a8012015-04-03 21:28:42 -0700136 HandleWrapper(const HandleWrapper&) = default;
137
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700138 ~HandleWrapper() {
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700139 *obj_ = MutableHandle<T>::Get();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700140 }
141
142 private:
Ian Rogersb5cb18a2014-10-21 15:05:36 -0700143 T** const obj_;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700144};
145
146// Scoped handle storage of a fixed size that is usually stack allocated.
147template<size_t kNumReferences>
Ian Rogers22d5e732014-07-15 22:23:51 -0700148class PACKED(4) StackHandleScope FINAL : public HandleScope {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700149 public:
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700150 explicit ALWAYS_INLINE StackHandleScope(Thread* self, mirror::Object* fill_value = nullptr);
151 ALWAYS_INLINE ~StackHandleScope();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700152
Ian Rogersb5cb18a2014-10-21 15:05:36 -0700153 template<class T>
Mathieu Chartier3e0acf62015-01-08 09:41:25 -0800154 ALWAYS_INLINE MutableHandle<T> NewHandle(T* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierbc56fc32014-06-03 15:37:03 -0700155
Ian Rogersb5cb18a2014-10-21 15:05:36 -0700156 template<class T>
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700157 ALWAYS_INLINE HandleWrapper<T> NewHandleWrapper(T** object)
Mathieu Chartier3e0acf62015-01-08 09:41:25 -0800158 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersb5cb18a2014-10-21 15:05:36 -0700159
Ian Rogers59c07062014-10-10 13:03:39 -0700160 ALWAYS_INLINE void SetReference(size_t i, mirror::Object* object)
Mathieu Chartier3e0acf62015-01-08 09:41:25 -0800161 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierbc56fc32014-06-03 15:37:03 -0700162
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700163 private:
164 template<class T>
165 ALWAYS_INLINE MutableHandle<T> GetHandle(size_t i) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
166 DCHECK_LT(i, kNumReferences);
167 return MutableHandle<T>(&GetReferences()[i]);
168 }
169
Ian Rogers59c07062014-10-10 13:03:39 -0700170 // Reference storage needs to be first as expected by the HandleScope layout.
171 StackReference<mirror::Object> storage_[kNumReferences];
Ian Rogers22d5e732014-07-15 22:23:51 -0700172
173 // The thread that the stack handle scope is a linked list upon. The stack handle scope will
174 // push and pop itself from this thread.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700175 Thread* const self_;
Ian Rogers22d5e732014-07-15 22:23:51 -0700176
177 // Position new handles will be created.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700178 size_t pos_;
179
180 template<size_t kNumRefs> friend class StackHandleScope;
181};
182
Calin Juravleacf735c2015-02-12 15:25:22 +0000183// Utility class to manage a collection (stack) of StackHandleScope. All the managed
184// scope handle have the same fixed sized.
185// Calls to NewHandle will create a new handle inside the top StackHandleScope.
186// When the handle scope becomes full a new one is created and push on top of the
187// previous.
188//
189// NB:
190// - it is not safe to use the *same* StackHandleScopeCollection intermix with
191// other StackHandleScopes.
192// - this is a an easy way around implementing a full ZoneHandleScope to manage an
193// arbitrary number of handles.
194class StackHandleScopeCollection {
195 public:
196 explicit StackHandleScopeCollection(Thread* const self) :
197 self_(self),
198 current_scope_num_refs_(0) {
199 }
200
201 ~StackHandleScopeCollection() {
202 while (!scopes_.empty()) {
203 delete scopes_.top();
204 scopes_.pop();
205 }
206 }
207
208 template<class T>
209 MutableHandle<T> NewHandle(T* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
210 if (scopes_.empty() || current_scope_num_refs_ >= kNumReferencesPerScope) {
211 StackHandleScope<kNumReferencesPerScope>* scope =
212 new StackHandleScope<kNumReferencesPerScope>(self_);
213 scopes_.push(scope);
214 current_scope_num_refs_ = 0;
215 }
216 current_scope_num_refs_++;
217 return scopes_.top()->NewHandle(object);
218 }
219
220 private:
221 static constexpr size_t kNumReferencesPerScope = 4;
222
223 Thread* const self_;
224
225 std::stack<StackHandleScope<kNumReferencesPerScope>*> scopes_;
226 size_t current_scope_num_refs_;
227
228 DISALLOW_COPY_AND_ASSIGN(StackHandleScopeCollection);
229};
230
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700231} // namespace art
232
233#endif // ART_RUNTIME_HANDLE_SCOPE_H_