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