blob: dae8e29ef0bae9a2176d0b7d0636610d8d52e4d4 [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
Andreas Gampe57943812017-12-06 21:39:13 -080022#include <android-base/logging.h>
23
Andreas Gampe542451c2016-07-26 09:02:02 -070024#include "base/enums.h"
Andreas Gampe7fbc4a52018-11-28 08:26:47 -080025#include "base/locks.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070026#include "base/macros.h"
Vladimir Marko3a21e382016-09-02 12:38:38 +010027#include "stack_reference.h"
Mathieu Chartier3e0acf62015-01-08 09:41:25 -080028#include "verify_object.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070029
30namespace art {
Mathieu Chartier0795f232016-09-27 18:43:30 -070031
Andreas Gampea1ffdba2019-01-04 16:08:51 -080032template<class T> class Handle;
Mathieu Chartiere8a3c572016-10-11 16:52:17 -070033class HandleScope;
Andreas Gampea1ffdba2019-01-04 16:08:51 -080034template<class T> class HandleWrapper;
35template<class T> class HandleWrapperObjPtr;
36template<class T> class MutableHandle;
Andreas Gampec73cb642017-02-22 10:11:30 -080037template<class MirrorType> class ObjPtr;
Mathieu Chartiere8a3c572016-10-11 16:52:17 -070038class Thread;
39class VariableSizedHandleScope;
Mathieu Chartier0795f232016-09-27 18:43:30 -070040
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070041namespace mirror {
42class Object;
Andreas Gampedeae7db2017-05-30 09:56:41 -070043} // namespace mirror
Ian Rogerse63db272014-07-15 15:36:11 -070044
Mathieu Chartiere8a3c572016-10-11 16:52:17 -070045// Basic handle scope, tracked by a list. May be variable sized.
46class PACKED(4) BaseHandleScope {
47 public:
48 bool IsVariableSized() const {
49 return number_of_references_ == kNumReferencesVariableSized;
50 }
51
52 // Number of references contained within this handle scope.
53 ALWAYS_INLINE uint32_t NumberOfReferences() const;
54
55 ALWAYS_INLINE bool Contains(StackReference<mirror::Object>* handle_scope_entry) const;
56
57 template <typename Visitor>
58 ALWAYS_INLINE void VisitRoots(Visitor& visitor) REQUIRES_SHARED(Locks::mutator_lock_);
59
60 // Link to previous BaseHandleScope or null.
61 BaseHandleScope* GetLink() const {
62 return link_;
63 }
64
65 ALWAYS_INLINE VariableSizedHandleScope* AsVariableSized();
66 ALWAYS_INLINE HandleScope* AsHandleScope();
67 ALWAYS_INLINE const VariableSizedHandleScope* AsVariableSized() const;
68 ALWAYS_INLINE const HandleScope* AsHandleScope() const;
69
70 protected:
71 BaseHandleScope(BaseHandleScope* link, uint32_t num_references)
72 : link_(link),
73 number_of_references_(num_references) {}
74
75 // Variable sized constructor.
Andreas Gampeea47ff82016-11-03 08:20:17 -070076 explicit BaseHandleScope(BaseHandleScope* link)
Mathieu Chartiere8a3c572016-10-11 16:52:17 -070077 : link_(link),
78 number_of_references_(kNumReferencesVariableSized) {}
79
80 static constexpr int32_t kNumReferencesVariableSized = -1;
81
82 // Link-list of handle scopes. The root is held by a Thread.
83 BaseHandleScope* const link_;
84
85 // Number of handlerized references. -1 for variable sized handle scopes.
86 const int32_t number_of_references_;
87
88 private:
89 DISALLOW_COPY_AND_ASSIGN(BaseHandleScope);
90};
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070091
Ian Rogers22d5e732014-07-15 22:23:51 -070092// HandleScopes are scoped objects containing a number of Handles. They are used to allocate
93// handles, for these handles (and the objects contained within them) to be visible/roots for the
94// GC. It is most common to stack allocate HandleScopes using StackHandleScope.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -070095class PACKED(4) HandleScope : public BaseHandleScope {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070096 public:
97 ~HandleScope() {}
98
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070099 // We have versions with and without explicit pointer size of the following. The first two are
100 // used at runtime, so OFFSETOF_MEMBER computes the right offsets automatically. The last one
101 // takes the pointer size explicitly so that at compile time we can cross-compile correctly.
102
103 // Returns the size of a HandleScope containing num_references handles.
Mathieu Chartier3e0acf62015-01-08 09:41:25 -0800104 static size_t SizeOf(uint32_t num_references);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700105
Andreas Gampecf4035a2014-05-28 22:43:01 -0700106 // Returns the size of a HandleScope containing num_references handles.
Andreas Gampe542451c2016-07-26 09:02:02 -0700107 static size_t SizeOf(PointerSize pointer_size, uint32_t num_references);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700108
Ian Rogers59c07062014-10-10 13:03:39 -0700109 ALWAYS_INLINE mirror::Object* GetReference(size_t i) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700110 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700111
Vladimir Markof39745e2016-01-26 12:16:55 +0000112 ALWAYS_INLINE Handle<mirror::Object> GetHandle(size_t i);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700113
Ian Rogers59c07062014-10-10 13:03:39 -0700114 ALWAYS_INLINE MutableHandle<mirror::Object> GetMutableHandle(size_t i)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700115 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700116
Ian Rogers59c07062014-10-10 13:03:39 -0700117 ALWAYS_INLINE void SetReference(size_t i, mirror::Object* object)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700118 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700119
Mathieu Chartier3e0acf62015-01-08 09:41:25 -0800120 ALWAYS_INLINE bool Contains(StackReference<mirror::Object>* handle_scope_entry) const;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700121
Ian Rogers59c07062014-10-10 13:03:39 -0700122 // Offset of link within HandleScope, used by generated code.
Andreas Gampe542451c2016-07-26 09:02:02 -0700123 static constexpr size_t LinkOffset(PointerSize pointer_size ATTRIBUTE_UNUSED) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700124 return 0;
125 }
126
Ian Rogers59c07062014-10-10 13:03:39 -0700127 // Offset of length within handle scope, used by generated code.
Andreas Gampe542451c2016-07-26 09:02:02 -0700128 static constexpr size_t NumberOfReferencesOffset(PointerSize pointer_size) {
129 return static_cast<size_t>(pointer_size);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700130 }
131
Ian Rogers59c07062014-10-10 13:03:39 -0700132 // Offset of link within handle scope, used by generated code.
Andreas Gampe542451c2016-07-26 09:02:02 -0700133 static constexpr size_t ReferencesOffset(PointerSize pointer_size) {
134 return NumberOfReferencesOffset(pointer_size) + sizeof(number_of_references_);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700135 }
136
Ian Rogers59c07062014-10-10 13:03:39 -0700137 // Placement new creation.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700138 static HandleScope* Create(void* storage, BaseHandleScope* link, uint32_t num_references)
Ian Rogers59c07062014-10-10 13:03:39 -0700139 WARN_UNUSED {
140 return new (storage) HandleScope(link, num_references);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700141 }
142
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700143 // Number of references contained within this handle scope.
144 ALWAYS_INLINE uint32_t NumberOfReferences() const {
145 DCHECK_GE(number_of_references_, 0);
146 return static_cast<uint32_t>(number_of_references_);
147 }
148
149 template <typename Visitor>
Andreas Gampea1ffdba2019-01-04 16:08:51 -0800150 ALWAYS_INLINE void VisitRoots(Visitor& visitor) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700151
Ian Rogers59c07062014-10-10 13:03:39 -0700152 protected:
153 // Return backing storage used for references.
154 ALWAYS_INLINE StackReference<mirror::Object>* GetReferences() const {
Andreas Gampe542451c2016-07-26 09:02:02 -0700155 uintptr_t address = reinterpret_cast<uintptr_t>(this) + ReferencesOffset(kRuntimePointerSize);
Ian Rogers59c07062014-10-10 13:03:39 -0700156 return reinterpret_cast<StackReference<mirror::Object>*>(address);
157 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700158
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700159 explicit HandleScope(size_t number_of_references) : HandleScope(nullptr, number_of_references) {}
Mathieu Chartierd035c2d2014-10-27 17:30:20 -0700160
Ian Rogers59c07062014-10-10 13:03:39 -0700161 // Semi-hidden constructor. Construction expected by generated code and StackHandleScope.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700162 HandleScope(BaseHandleScope* link, uint32_t num_references)
163 : BaseHandleScope(link, num_references) {}
Ian Rogers59c07062014-10-10 13:03:39 -0700164
165 // Storage for references.
166 // StackReference<mirror::Object> references_[number_of_references_]
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700167
168 private:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700169 DISALLOW_COPY_AND_ASSIGN(HandleScope);
170};
171
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700172// Fixed size handle scope that is not necessarily linked in the thread.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700173template<size_t kNumReferences>
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700174class PACKED(4) FixedSizeHandleScope : public HandleScope {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700175 public:
Ian Rogersb5cb18a2014-10-21 15:05:36 -0700176 template<class T>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700177 ALWAYS_INLINE MutableHandle<T> NewHandle(T* object) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierbc56fc32014-06-03 15:37:03 -0700178
Ian Rogersb5cb18a2014-10-21 15:05:36 -0700179 template<class T>
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700180 ALWAYS_INLINE HandleWrapper<T> NewHandleWrapper(T** object)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700181 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogersb5cb18a2014-10-21 15:05:36 -0700182
Mathieu Chartier0795f232016-09-27 18:43:30 -0700183 template<class T>
184 ALWAYS_INLINE HandleWrapperObjPtr<T> NewHandleWrapper(ObjPtr<T>* object)
185 REQUIRES_SHARED(Locks::mutator_lock_);
186
Andreas Gampec73cb642017-02-22 10:11:30 -0800187 template<class MirrorType>
188 ALWAYS_INLINE MutableHandle<MirrorType> NewHandle(ObjPtr<MirrorType> object)
Mathieu Chartier0795f232016-09-27 18:43:30 -0700189 REQUIRES_SHARED(Locks::mutator_lock_);
190
Ian Rogers59c07062014-10-10 13:03:39 -0700191 ALWAYS_INLINE void SetReference(size_t i, mirror::Object* object)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700192 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierbc56fc32014-06-03 15:37:03 -0700193
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700194 size_t RemainingSlots() const {
195 return kNumReferences - pos_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700196 }
197
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700198 private:
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700199 explicit ALWAYS_INLINE FixedSizeHandleScope(BaseHandleScope* link,
200 mirror::Object* fill_value = nullptr);
201 ALWAYS_INLINE ~FixedSizeHandleScope() {}
202
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700203 template<class T>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700204 ALWAYS_INLINE MutableHandle<T> GetHandle(size_t i) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700205 DCHECK_LT(i, kNumReferences);
206 return MutableHandle<T>(&GetReferences()[i]);
207 }
208
Ian Rogers59c07062014-10-10 13:03:39 -0700209 // Reference storage needs to be first as expected by the HandleScope layout.
210 StackReference<mirror::Object> storage_[kNumReferences];
Ian Rogers22d5e732014-07-15 22:23:51 -0700211
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700212 // Position new handles will be created.
Vladimir Marko26248c72017-02-21 17:00:28 +0000213 uint32_t pos_ = 0;
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700214
215 template<size_t kNumRefs> friend class StackHandleScope;
216 friend class VariableSizedHandleScope;
217};
218
219// Scoped handle storage of a fixed size that is stack allocated.
220template<size_t kNumReferences>
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100221class PACKED(4) StackHandleScope final : public FixedSizeHandleScope<kNumReferences> {
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700222 public:
223 explicit ALWAYS_INLINE StackHandleScope(Thread* self, mirror::Object* fill_value = nullptr);
224 ALWAYS_INLINE ~StackHandleScope();
225
226 Thread* Self() const {
227 return self_;
228 }
229
230 private:
Ian Rogers22d5e732014-07-15 22:23:51 -0700231 // The thread that the stack handle scope is a linked list upon. The stack handle scope will
232 // push and pop itself from this thread.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700233 Thread* const self_;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700234};
235
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700236// Utility class to manage a variable sized handle scope by having a list of fixed size handle
237// scopes.
238// Calls to NewHandle will create a new handle inside the current FixedSizeHandleScope.
239// When the current handle scope becomes full a new one is created and put at the front of the
240// list.
241class VariableSizedHandleScope : public BaseHandleScope {
Calin Juravleacf735c2015-02-12 15:25:22 +0000242 public:
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700243 explicit VariableSizedHandleScope(Thread* const self);
244 ~VariableSizedHandleScope();
Calin Juravleacf735c2015-02-12 15:25:22 +0000245
246 template<class T>
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700247 MutableHandle<T> NewHandle(T* object) REQUIRES_SHARED(Locks::mutator_lock_);
Calin Juravleacf735c2015-02-12 15:25:22 +0000248
Andreas Gampec73cb642017-02-22 10:11:30 -0800249 template<class MirrorType>
250 MutableHandle<MirrorType> NewHandle(ObjPtr<MirrorType> ptr)
Mathieu Chartier3398c782016-09-30 10:27:43 -0700251 REQUIRES_SHARED(Locks::mutator_lock_);
252
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700253 // Number of references contained within this handle scope.
254 ALWAYS_INLINE uint32_t NumberOfReferences() const;
255
256 ALWAYS_INLINE bool Contains(StackReference<mirror::Object>* handle_scope_entry) const;
257
258 template <typename Visitor>
259 void VisitRoots(Visitor& visitor) REQUIRES_SHARED(Locks::mutator_lock_);
260
Calin Juravleacf735c2015-02-12 15:25:22 +0000261 private:
Vladimir Marko26248c72017-02-21 17:00:28 +0000262 static constexpr size_t kLocalScopeSize = 64u;
263 static constexpr size_t kSizeOfReferencesPerScope =
264 kLocalScopeSize
265 - /* BaseHandleScope::link_ */ sizeof(BaseHandleScope*)
266 - /* BaseHandleScope::number_of_references_ */ sizeof(int32_t)
267 - /* FixedSizeHandleScope<>::pos_ */ sizeof(uint32_t);
268 static constexpr size_t kNumReferencesPerScope =
269 kSizeOfReferencesPerScope / sizeof(StackReference<mirror::Object>);
Calin Juravleacf735c2015-02-12 15:25:22 +0000270
271 Thread* const self_;
272
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700273 // Linked list of fixed size handle scopes.
274 using LocalScopeType = FixedSizeHandleScope<kNumReferencesPerScope>;
Vladimir Marko26248c72017-02-21 17:00:28 +0000275 static_assert(sizeof(LocalScopeType) == kLocalScopeSize, "Unexpected size of LocalScopeType");
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700276 LocalScopeType* current_scope_;
Calin Juravleacf735c2015-02-12 15:25:22 +0000277
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700278 DISALLOW_COPY_AND_ASSIGN(VariableSizedHandleScope);
Calin Juravleacf735c2015-02-12 15:25:22 +0000279};
280
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700281} // namespace art
282
283#endif // ART_RUNTIME_HANDLE_SCOPE_H_