Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1 | /* |
| 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 Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 20 | #include <stack> |
| 21 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 22 | #include "base/enums.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 23 | #include "base/logging.h" |
| 24 | #include "base/macros.h" |
Vladimir Marko | 3a21e38 | 2016-09-02 12:38:38 +0100 | [diff] [blame] | 25 | #include "base/mutex.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 26 | #include "handle.h" |
Vladimir Marko | 3a21e38 | 2016-09-02 12:38:38 +0100 | [diff] [blame] | 27 | #include "stack_reference.h" |
Mathieu Chartier | 3e0acf6 | 2015-01-08 09:41:25 -0800 | [diff] [blame] | 28 | #include "verify_object.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 29 | |
| 30 | namespace art { |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 31 | |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 32 | class HandleScope; |
Andreas Gampe | c73cb64 | 2017-02-22 10:11:30 -0800 | [diff] [blame] | 33 | template<class MirrorType> class ObjPtr; |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 34 | class Thread; |
| 35 | class VariableSizedHandleScope; |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 36 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 37 | namespace mirror { |
| 38 | class Object; |
Andreas Gampe | deae7db | 2017-05-30 09:56:41 -0700 | [diff] [blame] | 39 | } // namespace mirror |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 40 | |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 41 | // Basic handle scope, tracked by a list. May be variable sized. |
| 42 | class PACKED(4) BaseHandleScope { |
| 43 | public: |
| 44 | bool IsVariableSized() const { |
| 45 | return number_of_references_ == kNumReferencesVariableSized; |
| 46 | } |
| 47 | |
| 48 | // Number of references contained within this handle scope. |
| 49 | ALWAYS_INLINE uint32_t NumberOfReferences() const; |
| 50 | |
| 51 | ALWAYS_INLINE bool Contains(StackReference<mirror::Object>* handle_scope_entry) const; |
| 52 | |
| 53 | template <typename Visitor> |
| 54 | ALWAYS_INLINE void VisitRoots(Visitor& visitor) REQUIRES_SHARED(Locks::mutator_lock_); |
| 55 | |
| 56 | // Link to previous BaseHandleScope or null. |
| 57 | BaseHandleScope* GetLink() const { |
| 58 | return link_; |
| 59 | } |
| 60 | |
| 61 | ALWAYS_INLINE VariableSizedHandleScope* AsVariableSized(); |
| 62 | ALWAYS_INLINE HandleScope* AsHandleScope(); |
| 63 | ALWAYS_INLINE const VariableSizedHandleScope* AsVariableSized() const; |
| 64 | ALWAYS_INLINE const HandleScope* AsHandleScope() const; |
| 65 | |
| 66 | protected: |
| 67 | BaseHandleScope(BaseHandleScope* link, uint32_t num_references) |
| 68 | : link_(link), |
| 69 | number_of_references_(num_references) {} |
| 70 | |
| 71 | // Variable sized constructor. |
Andreas Gampe | ea47ff8 | 2016-11-03 08:20:17 -0700 | [diff] [blame] | 72 | explicit BaseHandleScope(BaseHandleScope* link) |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 73 | : link_(link), |
| 74 | number_of_references_(kNumReferencesVariableSized) {} |
| 75 | |
| 76 | static constexpr int32_t kNumReferencesVariableSized = -1; |
| 77 | |
| 78 | // Link-list of handle scopes. The root is held by a Thread. |
| 79 | BaseHandleScope* const link_; |
| 80 | |
| 81 | // Number of handlerized references. -1 for variable sized handle scopes. |
| 82 | const int32_t number_of_references_; |
| 83 | |
| 84 | private: |
| 85 | DISALLOW_COPY_AND_ASSIGN(BaseHandleScope); |
| 86 | }; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 87 | |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 88 | // HandleScopes are scoped objects containing a number of Handles. They are used to allocate |
| 89 | // handles, for these handles (and the objects contained within them) to be visible/roots for the |
| 90 | // GC. It is most common to stack allocate HandleScopes using StackHandleScope. |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 91 | class PACKED(4) HandleScope : public BaseHandleScope { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 92 | public: |
| 93 | ~HandleScope() {} |
| 94 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 95 | // We have versions with and without explicit pointer size of the following. The first two are |
| 96 | // used at runtime, so OFFSETOF_MEMBER computes the right offsets automatically. The last one |
| 97 | // takes the pointer size explicitly so that at compile time we can cross-compile correctly. |
| 98 | |
| 99 | // Returns the size of a HandleScope containing num_references handles. |
Mathieu Chartier | 3e0acf6 | 2015-01-08 09:41:25 -0800 | [diff] [blame] | 100 | static size_t SizeOf(uint32_t num_references); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 101 | |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 102 | // Returns the size of a HandleScope containing num_references handles. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 103 | static size_t SizeOf(PointerSize pointer_size, uint32_t num_references); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 104 | |
Ian Rogers | 59c0706 | 2014-10-10 13:03:39 -0700 | [diff] [blame] | 105 | ALWAYS_INLINE mirror::Object* GetReference(size_t i) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 106 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 107 | |
Vladimir Marko | f39745e | 2016-01-26 12:16:55 +0000 | [diff] [blame] | 108 | ALWAYS_INLINE Handle<mirror::Object> GetHandle(size_t i); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 109 | |
Ian Rogers | 59c0706 | 2014-10-10 13:03:39 -0700 | [diff] [blame] | 110 | ALWAYS_INLINE MutableHandle<mirror::Object> GetMutableHandle(size_t i) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 111 | REQUIRES_SHARED(Locks::mutator_lock_); |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 112 | |
Ian Rogers | 59c0706 | 2014-10-10 13:03:39 -0700 | [diff] [blame] | 113 | ALWAYS_INLINE void SetReference(size_t i, mirror::Object* object) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 114 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 115 | |
Mathieu Chartier | 3e0acf6 | 2015-01-08 09:41:25 -0800 | [diff] [blame] | 116 | ALWAYS_INLINE bool Contains(StackReference<mirror::Object>* handle_scope_entry) const; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 117 | |
Ian Rogers | 59c0706 | 2014-10-10 13:03:39 -0700 | [diff] [blame] | 118 | // Offset of link within HandleScope, used by generated code. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 119 | static constexpr size_t LinkOffset(PointerSize pointer_size ATTRIBUTE_UNUSED) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 120 | return 0; |
| 121 | } |
| 122 | |
Ian Rogers | 59c0706 | 2014-10-10 13:03:39 -0700 | [diff] [blame] | 123 | // Offset of length within handle scope, used by generated code. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 124 | static constexpr size_t NumberOfReferencesOffset(PointerSize pointer_size) { |
| 125 | return static_cast<size_t>(pointer_size); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Ian Rogers | 59c0706 | 2014-10-10 13:03:39 -0700 | [diff] [blame] | 128 | // Offset of link within handle scope, used by generated code. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 129 | static constexpr size_t ReferencesOffset(PointerSize pointer_size) { |
| 130 | return NumberOfReferencesOffset(pointer_size) + sizeof(number_of_references_); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Ian Rogers | 59c0706 | 2014-10-10 13:03:39 -0700 | [diff] [blame] | 133 | // Placement new creation. |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 134 | static HandleScope* Create(void* storage, BaseHandleScope* link, uint32_t num_references) |
Ian Rogers | 59c0706 | 2014-10-10 13:03:39 -0700 | [diff] [blame] | 135 | WARN_UNUSED { |
| 136 | return new (storage) HandleScope(link, num_references); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 139 | // Number of references contained within this handle scope. |
| 140 | ALWAYS_INLINE uint32_t NumberOfReferences() const { |
| 141 | DCHECK_GE(number_of_references_, 0); |
| 142 | return static_cast<uint32_t>(number_of_references_); |
| 143 | } |
| 144 | |
| 145 | template <typename Visitor> |
| 146 | void VisitRoots(Visitor& visitor) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 147 | for (size_t i = 0, count = NumberOfReferences(); i < count; ++i) { |
| 148 | // GetReference returns a pointer to the stack reference within the handle scope. If this |
| 149 | // needs to be updated, it will be done by the root visitor. |
| 150 | visitor.VisitRootIfNonNull(GetHandle(i).GetReference()); |
| 151 | } |
| 152 | } |
| 153 | |
Ian Rogers | 59c0706 | 2014-10-10 13:03:39 -0700 | [diff] [blame] | 154 | protected: |
| 155 | // Return backing storage used for references. |
| 156 | ALWAYS_INLINE StackReference<mirror::Object>* GetReferences() const { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 157 | uintptr_t address = reinterpret_cast<uintptr_t>(this) + ReferencesOffset(kRuntimePointerSize); |
Ian Rogers | 59c0706 | 2014-10-10 13:03:39 -0700 | [diff] [blame] | 158 | return reinterpret_cast<StackReference<mirror::Object>*>(address); |
| 159 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 160 | |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 161 | explicit HandleScope(size_t number_of_references) : HandleScope(nullptr, number_of_references) {} |
Mathieu Chartier | d035c2d | 2014-10-27 17:30:20 -0700 | [diff] [blame] | 162 | |
Ian Rogers | 59c0706 | 2014-10-10 13:03:39 -0700 | [diff] [blame] | 163 | // Semi-hidden constructor. Construction expected by generated code and StackHandleScope. |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 164 | HandleScope(BaseHandleScope* link, uint32_t num_references) |
| 165 | : BaseHandleScope(link, num_references) {} |
Ian Rogers | 59c0706 | 2014-10-10 13:03:39 -0700 | [diff] [blame] | 166 | |
| 167 | // Storage for references. |
| 168 | // StackReference<mirror::Object> references_[number_of_references_] |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 169 | |
| 170 | private: |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 171 | DISALLOW_COPY_AND_ASSIGN(HandleScope); |
| 172 | }; |
| 173 | |
| 174 | // A wrapper which wraps around Object** and restores the pointer in the destructor. |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 175 | // TODO: Delete |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 176 | template<class T> |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 177 | class HandleWrapper : public MutableHandle<T> { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 178 | public: |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 179 | HandleWrapper(T** obj, const MutableHandle<T>& handle) |
| 180 | : MutableHandle<T>(handle), obj_(obj) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Andreas Gampe | 758a801 | 2015-04-03 21:28:42 -0700 | [diff] [blame] | 183 | HandleWrapper(const HandleWrapper&) = default; |
| 184 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 185 | ~HandleWrapper() { |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 186 | *obj_ = MutableHandle<T>::Get(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | private: |
Ian Rogers | b5cb18a | 2014-10-21 15:05:36 -0700 | [diff] [blame] | 190 | T** const obj_; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 191 | }; |
| 192 | |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 193 | |
| 194 | // A wrapper which wraps around ObjPtr<Object>* and restores the pointer in the destructor. |
| 195 | // TODO: Add more functionality. |
| 196 | template<class T> |
| 197 | class HandleWrapperObjPtr : public MutableHandle<T> { |
| 198 | public: |
| 199 | HandleWrapperObjPtr(ObjPtr<T>* obj, const MutableHandle<T>& handle) |
| 200 | : MutableHandle<T>(handle), obj_(obj) {} |
| 201 | |
| 202 | HandleWrapperObjPtr(const HandleWrapperObjPtr&) = default; |
| 203 | |
| 204 | ~HandleWrapperObjPtr() { |
| 205 | *obj_ = ObjPtr<T>(MutableHandle<T>::Get()); |
| 206 | } |
| 207 | |
| 208 | private: |
| 209 | ObjPtr<T>* const obj_; |
| 210 | }; |
| 211 | |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 212 | // Fixed size handle scope that is not necessarily linked in the thread. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 213 | template<size_t kNumReferences> |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 214 | class PACKED(4) FixedSizeHandleScope : public HandleScope { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 215 | public: |
Ian Rogers | b5cb18a | 2014-10-21 15:05:36 -0700 | [diff] [blame] | 216 | template<class T> |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 217 | ALWAYS_INLINE MutableHandle<T> NewHandle(T* object) REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | bc56fc3 | 2014-06-03 15:37:03 -0700 | [diff] [blame] | 218 | |
Ian Rogers | b5cb18a | 2014-10-21 15:05:36 -0700 | [diff] [blame] | 219 | template<class T> |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 220 | ALWAYS_INLINE HandleWrapper<T> NewHandleWrapper(T** object) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 221 | REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | b5cb18a | 2014-10-21 15:05:36 -0700 | [diff] [blame] | 222 | |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 223 | template<class T> |
| 224 | ALWAYS_INLINE HandleWrapperObjPtr<T> NewHandleWrapper(ObjPtr<T>* object) |
| 225 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 226 | |
Andreas Gampe | c73cb64 | 2017-02-22 10:11:30 -0800 | [diff] [blame] | 227 | template<class MirrorType> |
| 228 | ALWAYS_INLINE MutableHandle<MirrorType> NewHandle(ObjPtr<MirrorType> object) |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 229 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 230 | |
Ian Rogers | 59c0706 | 2014-10-10 13:03:39 -0700 | [diff] [blame] | 231 | ALWAYS_INLINE void SetReference(size_t i, mirror::Object* object) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 232 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | bc56fc3 | 2014-06-03 15:37:03 -0700 | [diff] [blame] | 233 | |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 234 | size_t RemainingSlots() const { |
| 235 | return kNumReferences - pos_; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 238 | private: |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 239 | explicit ALWAYS_INLINE FixedSizeHandleScope(BaseHandleScope* link, |
| 240 | mirror::Object* fill_value = nullptr); |
| 241 | ALWAYS_INLINE ~FixedSizeHandleScope() {} |
| 242 | |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 243 | template<class T> |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 244 | ALWAYS_INLINE MutableHandle<T> GetHandle(size_t i) REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 245 | DCHECK_LT(i, kNumReferences); |
| 246 | return MutableHandle<T>(&GetReferences()[i]); |
| 247 | } |
| 248 | |
Ian Rogers | 59c0706 | 2014-10-10 13:03:39 -0700 | [diff] [blame] | 249 | // Reference storage needs to be first as expected by the HandleScope layout. |
| 250 | StackReference<mirror::Object> storage_[kNumReferences]; |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 251 | |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 252 | // Position new handles will be created. |
Vladimir Marko | 26248c7 | 2017-02-21 17:00:28 +0000 | [diff] [blame] | 253 | uint32_t pos_ = 0; |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 254 | |
| 255 | template<size_t kNumRefs> friend class StackHandleScope; |
| 256 | friend class VariableSizedHandleScope; |
| 257 | }; |
| 258 | |
| 259 | // Scoped handle storage of a fixed size that is stack allocated. |
| 260 | template<size_t kNumReferences> |
| 261 | class PACKED(4) StackHandleScope FINAL : public FixedSizeHandleScope<kNumReferences> { |
| 262 | public: |
| 263 | explicit ALWAYS_INLINE StackHandleScope(Thread* self, mirror::Object* fill_value = nullptr); |
| 264 | ALWAYS_INLINE ~StackHandleScope(); |
| 265 | |
| 266 | Thread* Self() const { |
| 267 | return self_; |
| 268 | } |
| 269 | |
| 270 | private: |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 271 | // The thread that the stack handle scope is a linked list upon. The stack handle scope will |
| 272 | // push and pop itself from this thread. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 273 | Thread* const self_; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 274 | }; |
| 275 | |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 276 | // Utility class to manage a variable sized handle scope by having a list of fixed size handle |
| 277 | // scopes. |
| 278 | // Calls to NewHandle will create a new handle inside the current FixedSizeHandleScope. |
| 279 | // When the current handle scope becomes full a new one is created and put at the front of the |
| 280 | // list. |
| 281 | class VariableSizedHandleScope : public BaseHandleScope { |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 282 | public: |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 283 | explicit VariableSizedHandleScope(Thread* const self); |
| 284 | ~VariableSizedHandleScope(); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 285 | |
| 286 | template<class T> |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 287 | MutableHandle<T> NewHandle(T* object) REQUIRES_SHARED(Locks::mutator_lock_); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 288 | |
Andreas Gampe | c73cb64 | 2017-02-22 10:11:30 -0800 | [diff] [blame] | 289 | template<class MirrorType> |
| 290 | MutableHandle<MirrorType> NewHandle(ObjPtr<MirrorType> ptr) |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 291 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 292 | |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 293 | // Number of references contained within this handle scope. |
| 294 | ALWAYS_INLINE uint32_t NumberOfReferences() const; |
| 295 | |
| 296 | ALWAYS_INLINE bool Contains(StackReference<mirror::Object>* handle_scope_entry) const; |
| 297 | |
| 298 | template <typename Visitor> |
| 299 | void VisitRoots(Visitor& visitor) REQUIRES_SHARED(Locks::mutator_lock_); |
| 300 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 301 | private: |
Vladimir Marko | 26248c7 | 2017-02-21 17:00:28 +0000 | [diff] [blame] | 302 | static constexpr size_t kLocalScopeSize = 64u; |
| 303 | static constexpr size_t kSizeOfReferencesPerScope = |
| 304 | kLocalScopeSize |
| 305 | - /* BaseHandleScope::link_ */ sizeof(BaseHandleScope*) |
| 306 | - /* BaseHandleScope::number_of_references_ */ sizeof(int32_t) |
| 307 | - /* FixedSizeHandleScope<>::pos_ */ sizeof(uint32_t); |
| 308 | static constexpr size_t kNumReferencesPerScope = |
| 309 | kSizeOfReferencesPerScope / sizeof(StackReference<mirror::Object>); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 310 | |
| 311 | Thread* const self_; |
| 312 | |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 313 | // Linked list of fixed size handle scopes. |
| 314 | using LocalScopeType = FixedSizeHandleScope<kNumReferencesPerScope>; |
Vladimir Marko | 26248c7 | 2017-02-21 17:00:28 +0000 | [diff] [blame] | 315 | static_assert(sizeof(LocalScopeType) == kLocalScopeSize, "Unexpected size of LocalScopeType"); |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 316 | LocalScopeType* current_scope_; |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 317 | |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 318 | DISALLOW_COPY_AND_ASSIGN(VariableSizedHandleScope); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 319 | }; |
| 320 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 321 | } // namespace art |
| 322 | |
| 323 | #endif // ART_RUNTIME_HANDLE_SCOPE_H_ |