/* * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef ART_RUNTIME_MIRROR_OBJECT_REFVISITOR_INL_H_ #define ART_RUNTIME_MIRROR_OBJECT_REFVISITOR_INL_H_ #include "object-inl.h" #include "class-refvisitor-inl.h" #include "class_loader-inl.h" #include "dex_cache-inl.h" namespace art HIDDEN { namespace mirror { template static void CheckNoReferenceField(ObjPtr klass) REQUIRES_SHARED(art::Locks::mutator_lock_) { if (!kIsDebugBuild) { return; } CHECK(!klass->IsClassClass()); CHECK((!klass->IsObjectArrayClass())); // String still has instance fields for reflection purposes but these don't exist in // actual string instances. if (!klass->IsStringClass()) { size_t total_reference_instance_fields = 0; ObjPtr super_class = klass; do { total_reference_instance_fields += super_class->NumReferenceInstanceFields(); super_class = super_class->GetSuperClass(); } while (super_class != nullptr); // The only reference field should be the object's class. CHECK_EQ(total_reference_instance_fields, 1u); } } template static void CheckNormalClass(ObjPtr klass) REQUIRES_SHARED(art::Locks::mutator_lock_) { DCHECK(!klass->IsVariableSize()); DCHECK(!klass->IsClassClass()); DCHECK(!klass->IsStringClass()); DCHECK(!klass->IsClassLoaderClass()); DCHECK(!klass->IsArrayClass()); } template inline void Object::VisitReferences(const Visitor& visitor, const JavaLangRefVisitor& ref_visitor) { visitor(this, ClassOffset(), /* is_static= */ false); ObjPtr klass = GetClass(); const uint32_t class_flags = klass->GetClassFlags(); if (LIKELY(class_flags == kClassFlagNormal) || class_flags == kClassFlagRecord) { CheckNormalClass(klass); VisitInstanceFieldsReferences(klass, visitor); return; } if ((class_flags & kClassFlagNoReferenceFields) != 0) { CheckNoReferenceField(klass); return; } DCHECK(!klass->IsStringClass()); if (class_flags == kClassFlagClass) { DCHECK(klass->IsClassClass()); ObjPtr as_klass = AsClass(); as_klass->VisitReferences(klass, visitor); return; } if (class_flags == kClassFlagObjectArray) { DCHECK((klass->IsObjectArrayClass())); AsObjectArray()->VisitReferences(visitor); return; } if ((class_flags & kClassFlagReference) != 0) { VisitInstanceFieldsReferences(klass, visitor); ref_visitor(klass, AsReference()); return; } if (class_flags == kClassFlagDexCache) { DCHECK(klass->IsDexCacheClass()); ObjPtr const dex_cache = AsDexCache(); dex_cache->VisitReferences(klass, visitor); return; } if (class_flags == kClassFlagClassLoader) { DCHECK(klass->IsClassLoaderClass()); ObjPtr const class_loader = AsClassLoader(); class_loader->VisitReferences(klass, visitor); return; } LOG(FATAL) << "Unexpected class flags: " << std::hex << class_flags << " for " << klass->PrettyClass(); } // Could be called with from-space address of the object as we access klass and // length (in case of arrays/strings) and we don't want to cause cascading faults. template inline size_t Object::VisitRefsForCompaction(const Visitor& visitor, MemberOffset begin, MemberOffset end) { constexpr VerifyObjectFlags kSizeOfFlags = RemoveThisFlags(kVerifyFlags); size_t size; // We want to continue using pre-compact klass to avoid cascading faults. ObjPtr klass = GetClass(); DCHECK(klass != nullptr) << "obj=" << this; const uint32_t class_flags = klass->GetClassFlags(); if (LIKELY(class_flags == kClassFlagNormal) || class_flags == kClassFlagRecord) { CheckNormalClass(klass); VisitInstanceFieldsReferences(klass, visitor); size = kFetchObjSize ? klass->GetObjectSize() : 0; } else if ((class_flags & kClassFlagNoReferenceFields) != 0) { if ((class_flags & kClassFlagString) != 0) { size = kFetchObjSize ? static_cast(this)->SizeOf() : 0; } else if (klass->IsArrayClass()) { // TODO: We can optimize this by implementing a SizeOf() version which takes // component-size-shift as an argument, thereby avoiding multiple loads of // component_type. size = kFetchObjSize ? static_cast(this)->SizeOf() : 0; } else { // Only possibility left is of a normal klass instance with no references. size = kFetchObjSize ? klass->GetObjectSize() : 0; } } else if (class_flags == kClassFlagClass) { DCHECK(klass->IsClassClass()); ObjPtr as_klass = ObjPtr::DownCast(this); as_klass->VisitReferences(klass, visitor); size = kFetchObjSize ? as_klass->SizeOf() : 0; } else if (class_flags == kClassFlagObjectArray) { ObjPtr> obj_arr = ObjPtr>::DownCast(this); obj_arr->VisitReferences(visitor, begin, end); size = kFetchObjSize ? obj_arr->SizeOf() : 0; } else if ((class_flags & kClassFlagReference) != 0) { VisitInstanceFieldsReferences(klass, visitor); // Visit referent also as this is about updating the reference only. // There is no reference processing happening here. visitor(this, mirror::Reference::ReferentOffset(), /* is_static= */ false); size = kFetchObjSize ? klass->GetObjectSize() : 0; } else if (class_flags == kClassFlagDexCache) { DCHECK(klass->IsDexCacheClass()); ObjPtr const dex_cache = ObjPtr::DownCast(this); dex_cache->VisitReferences(klass, visitor); size = kFetchObjSize ? klass->GetObjectSize() : 0; } else if (class_flags == kClassFlagClassLoader) { DCHECK(klass->IsClassLoaderClass()); ObjPtr const class_loader = ObjPtr::DownCast(this); class_loader->VisitReferences(klass, visitor); size = kFetchObjSize ? klass->GetObjectSize() : 0; } else { LOG(FATAL) << "Unexpected class flags: " << std::hex << class_flags << " for " << klass->PrettyClass(); size = -1; } visitor(this, ClassOffset(), /* is_static= */ false); return size; } } // namespace mirror } // namespace art #endif // ART_RUNTIME_MIRROR_OBJECT_REFVISITOR_INL_H_