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