Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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_MIRROR_CLASS_REFVISITOR_INL_H_ |
| 18 | #define ART_RUNTIME_MIRROR_CLASS_REFVISITOR_INL_H_ |
| 19 | |
| 20 | #include "class-inl.h" |
| 21 | |
Andreas Gampe | a1d2f95 | 2017-04-20 22:53:58 -0700 | [diff] [blame] | 22 | #include "art_field-inl.h" |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 23 | #include "class_ext-inl.h" |
| 24 | |
| 25 | namespace art { |
| 26 | namespace mirror { |
| 27 | |
| 28 | template <bool kVisitNativeRoots, |
| 29 | VerifyObjectFlags kVerifyFlags, |
| 30 | ReadBarrierOption kReadBarrierOption, |
| 31 | typename Visitor> |
| 32 | inline void Class::VisitReferences(ObjPtr<Class> klass, const Visitor& visitor) { |
| 33 | VisitInstanceFieldsReferences<kVerifyFlags, kReadBarrierOption>(klass.Ptr(), visitor); |
| 34 | // Right after a class is allocated, but not yet loaded |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 35 | // (ClassStatus::kNotReady, see ClassLinker::LoadClass()), GC may find it |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 36 | // and scan it. IsTemp() may call Class::GetAccessFlags() but may |
| 37 | // fail in the DCHECK in Class::GetAccessFlags() because the class |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 38 | // status is ClassStatus::kNotReady. To avoid it, rely on IsResolved() |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 39 | // only. This is fine because a temp class never goes into the |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 40 | // ClassStatus::kResolved state. |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 41 | if (IsResolved<kVerifyFlags>()) { |
| 42 | // Temp classes don't ever populate imt/vtable or static fields and they are not even |
| 43 | // allocated with the right size for those. Also, unresolved classes don't have fields |
| 44 | // linked yet. |
| 45 | VisitStaticFieldsReferences<kVerifyFlags, kReadBarrierOption>(this, visitor); |
| 46 | } |
| 47 | if (kVisitNativeRoots) { |
| 48 | // Since this class is reachable, we must also visit the associated roots when we scan it. |
| 49 | VisitNativeRoots<kReadBarrierOption>( |
| 50 | visitor, Runtime::Current()->GetClassLinker()->GetImagePointerSize()); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | template<ReadBarrierOption kReadBarrierOption, class Visitor> |
| 55 | void Class::VisitNativeRoots(Visitor& visitor, PointerSize pointer_size) { |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 56 | VisitFields<kReadBarrierOption>([&](ArtField* field) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 57 | field->VisitRoots(visitor); |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 58 | if (kIsDebugBuild && IsResolved()) { |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 59 | CHECK_EQ(field->GetDeclaringClass<kReadBarrierOption>(), this) |
| 60 | << GetStatus() << field->GetDeclaringClass()->PrettyClass() << " != " << PrettyClass(); |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 61 | } |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 62 | }); |
| 63 | // Don't use VisitMethods because we don't want to hit the class-ext methods twice. |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 64 | for (ArtMethod& method : GetMethods(pointer_size)) { |
| 65 | method.VisitRoots<kReadBarrierOption>(visitor, pointer_size); |
| 66 | } |
| 67 | ObjPtr<ClassExt> ext(GetExtData<kDefaultVerifyFlags, kReadBarrierOption>()); |
| 68 | if (!ext.IsNull()) { |
| 69 | ext->VisitNativeRoots<kReadBarrierOption, Visitor>(visitor, pointer_size); |
| 70 | } |
| 71 | } |
| 72 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 73 | template<ReadBarrierOption kReadBarrierOption, class Visitor> |
| 74 | void Class::VisitMethods(Visitor visitor, PointerSize pointer_size) { |
| 75 | for (ArtMethod& method : GetMethods(pointer_size)) { |
| 76 | visitor(&method); |
| 77 | } |
| 78 | ObjPtr<ClassExt> ext(GetExtData<kDefaultVerifyFlags, kReadBarrierOption>()); |
| 79 | if (!ext.IsNull()) { |
| 80 | ext->VisitMethods<kReadBarrierOption, Visitor>(visitor, pointer_size); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | template<ReadBarrierOption kReadBarrierOption, class Visitor> |
| 85 | void Class::VisitFields(Visitor visitor) { |
| 86 | for (ArtField& sfield : GetSFieldsUnchecked()) { |
| 87 | visitor(&sfield); |
| 88 | } |
| 89 | for (ArtField& ifield : GetIFieldsUnchecked()) { |
| 90 | visitor(&ifield); |
| 91 | } |
| 92 | } |
| 93 | |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 94 | } // namespace mirror |
| 95 | } // namespace art |
| 96 | |
| 97 | #endif // ART_RUNTIME_MIRROR_CLASS_REFVISITOR_INL_H_ |