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_INL_H_ |
| 18 | #define ART_RUNTIME_HANDLE_SCOPE_INL_H_ |
| 19 | |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 20 | #include "handle_scope.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 21 | |
Mathieu Chartier | ed15000 | 2015-08-28 11:16:54 -0700 | [diff] [blame] | 22 | #include "base/mutex.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 23 | #include "handle.h" |
Andreas Gampe | a1ffdba | 2019-01-04 16:08:51 -0800 | [diff] [blame] | 24 | #include "handle_wrapper.h" |
Vladimir Marko | abedfca | 2019-05-23 14:07:47 +0100 | [diff] [blame] | 25 | #include "mirror/object_reference-inl.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 26 | #include "obj_ptr-inl.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 27 | #include "thread-current-inl.h" |
Andreas Gampe | 90b936d | 2017-01-31 08:58:55 -0800 | [diff] [blame] | 28 | #include "verify_object.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 29 | |
| 30 | namespace art { |
| 31 | |
| 32 | template<size_t kNumReferences> |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 33 | inline FixedSizeHandleScope<kNumReferences>::FixedSizeHandleScope(BaseHandleScope* link, |
Vladimir Marko | abedfca | 2019-05-23 14:07:47 +0100 | [diff] [blame] | 34 | ObjPtr<mirror::Object> fill_value) |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 35 | : HandleScope(link, kNumReferences) { |
Mathieu Chartier | ed15000 | 2015-08-28 11:16:54 -0700 | [diff] [blame] | 36 | if (kDebugLocking) { |
| 37 | Locks::mutator_lock_->AssertSharedHeld(Thread::Current()); |
| 38 | } |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 39 | static_assert(kNumReferences >= 1, "FixedSizeHandleScope must contain at least 1 reference"); |
| 40 | DCHECK_EQ(&storage_[0], GetReferences()); // TODO: Figure out how to use a compile assert. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 41 | for (size_t i = 0; i < kNumReferences; ++i) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 42 | SetReference(i, fill_value); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 43 | } |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | template<size_t kNumReferences> |
Vladimir Marko | abedfca | 2019-05-23 14:07:47 +0100 | [diff] [blame] | 47 | inline StackHandleScope<kNumReferences>::StackHandleScope(Thread* self, |
| 48 | ObjPtr<mirror::Object> fill_value) |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 49 | : FixedSizeHandleScope<kNumReferences>(self->GetTopHandleScope(), fill_value), |
| 50 | self_(self) { |
| 51 | DCHECK_EQ(self, Thread::Current()); |
Vladimir Marko | 1d326f9 | 2021-06-01 09:26:55 +0100 | [diff] [blame] | 52 | if (kDebugLocking) { |
| 53 | Locks::mutator_lock_->AssertSharedHeld(self_); |
| 54 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 55 | self_->PushHandleScope(this); |
| 56 | } |
| 57 | |
| 58 | template<size_t kNumReferences> |
Mathieu Chartier | 421c537 | 2014-05-14 14:11:40 -0700 | [diff] [blame] | 59 | inline StackHandleScope<kNumReferences>::~StackHandleScope() { |
Vladimir Marko | 1d326f9 | 2021-06-01 09:26:55 +0100 | [diff] [blame] | 60 | if (kDebugLocking) { |
| 61 | Locks::mutator_lock_->AssertSharedHeld(self_); |
| 62 | } |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 63 | BaseHandleScope* top_handle_scope = self_->PopHandleScope(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 64 | DCHECK_EQ(top_handle_scope, this); |
| 65 | } |
| 66 | |
Mathieu Chartier | 3e0acf6 | 2015-01-08 09:41:25 -0800 | [diff] [blame] | 67 | inline size_t HandleScope::SizeOf(uint32_t num_references) { |
| 68 | size_t header_size = sizeof(HandleScope); |
| 69 | size_t data_size = sizeof(StackReference<mirror::Object>) * num_references; |
| 70 | return header_size + data_size; |
| 71 | } |
| 72 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 73 | inline size_t HandleScope::SizeOf(PointerSize pointer_size, uint32_t num_references) { |
Mathieu Chartier | 3e0acf6 | 2015-01-08 09:41:25 -0800 | [diff] [blame] | 74 | // Assume that the layout is packed. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 75 | size_t header_size = ReferencesOffset(pointer_size); |
Mathieu Chartier | 3e0acf6 | 2015-01-08 09:41:25 -0800 | [diff] [blame] | 76 | size_t data_size = sizeof(StackReference<mirror::Object>) * num_references; |
| 77 | return header_size + data_size; |
| 78 | } |
| 79 | |
Vladimir Marko | abedfca | 2019-05-23 14:07:47 +0100 | [diff] [blame] | 80 | inline ObjPtr<mirror::Object> HandleScope::GetReference(size_t i) const { |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 81 | DCHECK_LT(i, NumberOfReferences()); |
Mathieu Chartier | ed15000 | 2015-08-28 11:16:54 -0700 | [diff] [blame] | 82 | if (kDebugLocking) { |
| 83 | Locks::mutator_lock_->AssertSharedHeld(Thread::Current()); |
| 84 | } |
Mathieu Chartier | 3e0acf6 | 2015-01-08 09:41:25 -0800 | [diff] [blame] | 85 | return GetReferences()[i].AsMirrorPtr(); |
| 86 | } |
| 87 | |
| 88 | inline Handle<mirror::Object> HandleScope::GetHandle(size_t i) { |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 89 | DCHECK_LT(i, NumberOfReferences()); |
Mathieu Chartier | 3e0acf6 | 2015-01-08 09:41:25 -0800 | [diff] [blame] | 90 | return Handle<mirror::Object>(&GetReferences()[i]); |
| 91 | } |
| 92 | |
| 93 | inline MutableHandle<mirror::Object> HandleScope::GetMutableHandle(size_t i) { |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 94 | DCHECK_LT(i, NumberOfReferences()); |
Mathieu Chartier | 3e0acf6 | 2015-01-08 09:41:25 -0800 | [diff] [blame] | 95 | return MutableHandle<mirror::Object>(&GetReferences()[i]); |
| 96 | } |
| 97 | |
Vladimir Marko | abedfca | 2019-05-23 14:07:47 +0100 | [diff] [blame] | 98 | inline void HandleScope::SetReference(size_t i, ObjPtr<mirror::Object> object) { |
Mathieu Chartier | ed15000 | 2015-08-28 11:16:54 -0700 | [diff] [blame] | 99 | if (kDebugLocking) { |
| 100 | Locks::mutator_lock_->AssertSharedHeld(Thread::Current()); |
| 101 | } |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 102 | DCHECK_LT(i, NumberOfReferences()); |
Mathieu Chartier | 3e0acf6 | 2015-01-08 09:41:25 -0800 | [diff] [blame] | 103 | GetReferences()[i].Assign(object); |
| 104 | } |
| 105 | |
| 106 | inline bool HandleScope::Contains(StackReference<mirror::Object>* handle_scope_entry) const { |
| 107 | // A HandleScope should always contain something. One created by the |
| 108 | // jni_compiler should have a jobject/jclass as a native method is |
| 109 | // passed in a this pointer or a class |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 110 | DCHECK_GT(NumberOfReferences(), 0U); |
Mathieu Chartier | 3e0acf6 | 2015-01-08 09:41:25 -0800 | [diff] [blame] | 111 | return &GetReferences()[0] <= handle_scope_entry && |
| 112 | handle_scope_entry <= &GetReferences()[number_of_references_ - 1]; |
| 113 | } |
| 114 | |
Andreas Gampe | a1ffdba | 2019-01-04 16:08:51 -0800 | [diff] [blame] | 115 | template <typename Visitor> |
| 116 | inline void HandleScope::VisitRoots(Visitor& visitor) { |
| 117 | for (size_t i = 0, count = NumberOfReferences(); i < count; ++i) { |
| 118 | // GetReference returns a pointer to the stack reference within the handle scope. If this |
| 119 | // needs to be updated, it will be done by the root visitor. |
| 120 | visitor.VisitRootIfNonNull(GetHandle(i).GetReference()); |
| 121 | } |
| 122 | } |
| 123 | |
Mathieu Chartier | 3e0acf6 | 2015-01-08 09:41:25 -0800 | [diff] [blame] | 124 | template<size_t kNumReferences> template<class T> |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 125 | inline MutableHandle<T> FixedSizeHandleScope<kNumReferences>::NewHandle(T* object) { |
Vladimir Marko | abedfca | 2019-05-23 14:07:47 +0100 | [diff] [blame] | 126 | return NewHandle(ObjPtr<T>(object)); |
Mathieu Chartier | 3e0acf6 | 2015-01-08 09:41:25 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Andreas Gampe | c73cb64 | 2017-02-22 10:11:30 -0800 | [diff] [blame] | 129 | template<size_t kNumReferences> template<class MirrorType> |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 130 | inline MutableHandle<MirrorType> FixedSizeHandleScope<kNumReferences>::NewHandle( |
Andreas Gampe | c73cb64 | 2017-02-22 10:11:30 -0800 | [diff] [blame] | 131 | ObjPtr<MirrorType> object) { |
Vladimir Marko | abedfca | 2019-05-23 14:07:47 +0100 | [diff] [blame] | 132 | SetReference(pos_, object); |
| 133 | MutableHandle<MirrorType> h(GetHandle<MirrorType>(pos_)); |
| 134 | ++pos_; |
| 135 | return h; |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Mathieu Chartier | 3e0acf6 | 2015-01-08 09:41:25 -0800 | [diff] [blame] | 138 | template<size_t kNumReferences> template<class T> |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 139 | inline HandleWrapper<T> FixedSizeHandleScope<kNumReferences>::NewHandleWrapper(T** object) { |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 140 | return HandleWrapper<T>(object, NewHandle(*object)); |
| 141 | } |
| 142 | |
| 143 | template<size_t kNumReferences> template<class T> |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 144 | inline HandleWrapperObjPtr<T> FixedSizeHandleScope<kNumReferences>::NewHandleWrapper( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 145 | ObjPtr<T>* object) { |
| 146 | return HandleWrapperObjPtr<T>(object, NewHandle(*object)); |
Mathieu Chartier | 3e0acf6 | 2015-01-08 09:41:25 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | template<size_t kNumReferences> |
Vladimir Marko | abedfca | 2019-05-23 14:07:47 +0100 | [diff] [blame] | 150 | inline void FixedSizeHandleScope<kNumReferences>::SetReference(size_t i, |
| 151 | ObjPtr<mirror::Object> object) { |
Mathieu Chartier | ed15000 | 2015-08-28 11:16:54 -0700 | [diff] [blame] | 152 | if (kDebugLocking) { |
| 153 | Locks::mutator_lock_->AssertSharedHeld(Thread::Current()); |
| 154 | } |
Mathieu Chartier | 3e0acf6 | 2015-01-08 09:41:25 -0800 | [diff] [blame] | 155 | DCHECK_LT(i, kNumReferences); |
| 156 | VerifyObject(object); |
| 157 | GetReferences()[i].Assign(object); |
| 158 | } |
| 159 | |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 160 | // Number of references contained within this handle scope. |
| 161 | inline uint32_t BaseHandleScope::NumberOfReferences() const { |
| 162 | return LIKELY(!IsVariableSized()) |
| 163 | ? AsHandleScope()->NumberOfReferences() |
| 164 | : AsVariableSized()->NumberOfReferences(); |
| 165 | } |
| 166 | |
| 167 | inline bool BaseHandleScope::Contains(StackReference<mirror::Object>* handle_scope_entry) const { |
| 168 | return LIKELY(!IsVariableSized()) |
| 169 | ? AsHandleScope()->Contains(handle_scope_entry) |
| 170 | : AsVariableSized()->Contains(handle_scope_entry); |
| 171 | } |
| 172 | |
| 173 | template <typename Visitor> |
| 174 | inline void BaseHandleScope::VisitRoots(Visitor& visitor) { |
| 175 | if (LIKELY(!IsVariableSized())) { |
| 176 | AsHandleScope()->VisitRoots(visitor); |
| 177 | } else { |
| 178 | AsVariableSized()->VisitRoots(visitor); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | inline VariableSizedHandleScope* BaseHandleScope::AsVariableSized() { |
| 183 | DCHECK(IsVariableSized()); |
| 184 | return down_cast<VariableSizedHandleScope*>(this); |
| 185 | } |
| 186 | |
| 187 | inline HandleScope* BaseHandleScope::AsHandleScope() { |
| 188 | DCHECK(!IsVariableSized()); |
| 189 | return down_cast<HandleScope*>(this); |
| 190 | } |
| 191 | |
| 192 | inline const VariableSizedHandleScope* BaseHandleScope::AsVariableSized() const { |
| 193 | DCHECK(IsVariableSized()); |
| 194 | return down_cast<const VariableSizedHandleScope*>(this); |
| 195 | } |
| 196 | |
| 197 | inline const HandleScope* BaseHandleScope::AsHandleScope() const { |
| 198 | DCHECK(!IsVariableSized()); |
| 199 | return down_cast<const HandleScope*>(this); |
| 200 | } |
| 201 | |
| 202 | template<class T> |
Vladimir Marko | abedfca | 2019-05-23 14:07:47 +0100 | [diff] [blame] | 203 | inline MutableHandle<T> VariableSizedHandleScope::NewHandle(T* object) { |
| 204 | return NewHandle(ObjPtr<T>(object)); |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Andreas Gampe | c73cb64 | 2017-02-22 10:11:30 -0800 | [diff] [blame] | 207 | template<class MirrorType> |
| 208 | inline MutableHandle<MirrorType> VariableSizedHandleScope::NewHandle(ObjPtr<MirrorType> ptr) { |
Vladimir Marko | abedfca | 2019-05-23 14:07:47 +0100 | [diff] [blame] | 209 | if (current_scope_->RemainingSlots() == 0) { |
| 210 | current_scope_ = new LocalScopeType(current_scope_); |
| 211 | } |
| 212 | return current_scope_->NewHandle(ptr); |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 215 | inline VariableSizedHandleScope::VariableSizedHandleScope(Thread* const self) |
| 216 | : BaseHandleScope(self->GetTopHandleScope()), |
Mathieu Chartier | c550327 | 2020-02-11 10:01:18 -0800 | [diff] [blame] | 217 | self_(self), |
| 218 | current_scope_(&first_scope_), |
| 219 | first_scope_(/*link=*/ nullptr) { |
Vladimir Marko | 1d326f9 | 2021-06-01 09:26:55 +0100 | [diff] [blame] | 220 | DCHECK_EQ(self, Thread::Current()); |
| 221 | if (kDebugLocking) { |
| 222 | Locks::mutator_lock_->AssertSharedHeld(self_); |
| 223 | } |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 224 | self_->PushHandleScope(this); |
| 225 | } |
| 226 | |
| 227 | inline VariableSizedHandleScope::~VariableSizedHandleScope() { |
Vladimir Marko | 1d326f9 | 2021-06-01 09:26:55 +0100 | [diff] [blame] | 228 | if (kDebugLocking) { |
| 229 | Locks::mutator_lock_->AssertSharedHeld(self_); |
| 230 | } |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 231 | BaseHandleScope* top_handle_scope = self_->PopHandleScope(); |
| 232 | DCHECK_EQ(top_handle_scope, this); |
Mathieu Chartier | c550327 | 2020-02-11 10:01:18 -0800 | [diff] [blame] | 233 | // Don't delete first_scope_ since it is not heap allocated. |
| 234 | while (current_scope_ != &first_scope_) { |
| 235 | LocalScopeType* next = down_cast<LocalScopeType*>(current_scope_->GetLink()); |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 236 | delete current_scope_; |
| 237 | current_scope_ = next; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | inline uint32_t VariableSizedHandleScope::NumberOfReferences() const { |
| 242 | uint32_t sum = 0; |
| 243 | const LocalScopeType* cur = current_scope_; |
| 244 | while (cur != nullptr) { |
| 245 | sum += cur->NumberOfReferences(); |
| 246 | cur = reinterpret_cast<const LocalScopeType*>(cur->GetLink()); |
| 247 | } |
| 248 | return sum; |
| 249 | } |
| 250 | |
| 251 | inline bool VariableSizedHandleScope::Contains(StackReference<mirror::Object>* handle_scope_entry) |
| 252 | const { |
| 253 | const LocalScopeType* cur = current_scope_; |
| 254 | while (cur != nullptr) { |
| 255 | if (cur->Contains(handle_scope_entry)) { |
| 256 | return true; |
| 257 | } |
| 258 | cur = reinterpret_cast<const LocalScopeType*>(cur->GetLink()); |
| 259 | } |
| 260 | return false; |
| 261 | } |
| 262 | |
| 263 | template <typename Visitor> |
| 264 | inline void VariableSizedHandleScope::VisitRoots(Visitor& visitor) { |
| 265 | LocalScopeType* cur = current_scope_; |
| 266 | while (cur != nullptr) { |
| 267 | cur->VisitRoots(visitor); |
| 268 | cur = reinterpret_cast<LocalScopeType*>(cur->GetLink()); |
| 269 | } |
| 270 | } |
| 271 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 272 | } // namespace art |
| 273 | |
| 274 | #endif // ART_RUNTIME_HANDLE_SCOPE_INL_H_ |