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