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_OBJECT_REFVISITOR_INL_H_
18 #define ART_RUNTIME_MIRROR_OBJECT_REFVISITOR_INL_H_
19
20 #include "object-inl.h"
21
22 #include "class_loader-inl.h"
23 #include "class-refvisitor-inl.h"
24 #include "dex_cache-inl.h"
25
26 namespace art {
27 namespace mirror {
28
29 template <bool kVisitNativeRoots,
30 VerifyObjectFlags kVerifyFlags,
31 ReadBarrierOption kReadBarrierOption,
32 typename Visitor,
33 typename JavaLangRefVisitor>
VisitReferences(const Visitor & visitor,const JavaLangRefVisitor & ref_visitor)34 inline void Object::VisitReferences(const Visitor& visitor,
35 const JavaLangRefVisitor& ref_visitor) {
36 ObjPtr<Class> klass = GetClass<kVerifyFlags, kReadBarrierOption>();
37 visitor(this, ClassOffset(), false);
38 const uint32_t class_flags = klass->GetClassFlags<kVerifyNone>();
39 if (LIKELY(class_flags == kClassFlagNormal)) {
40 DCHECK((!klass->IsVariableSize<kVerifyFlags, kReadBarrierOption>()));
41 VisitInstanceFieldsReferences<kVerifyFlags, kReadBarrierOption>(klass, visitor);
42 DCHECK((!klass->IsClassClass<kVerifyFlags, kReadBarrierOption>()));
43 DCHECK(!klass->IsStringClass());
44 DCHECK(!klass->IsClassLoaderClass());
45 DCHECK((!klass->IsArrayClass<kVerifyFlags, kReadBarrierOption>()));
46 } else {
47 if ((class_flags & kClassFlagNoReferenceFields) == 0) {
48 DCHECK(!klass->IsStringClass());
49 if (class_flags == kClassFlagClass) {
50 DCHECK((klass->IsClassClass<kVerifyFlags, kReadBarrierOption>()));
51 ObjPtr<Class> as_klass = AsClass<kVerifyNone, kReadBarrierOption>();
52 as_klass->VisitReferences<kVisitNativeRoots, kVerifyFlags, kReadBarrierOption>(klass,
53 visitor);
54 } else if (class_flags == kClassFlagObjectArray) {
55 DCHECK((klass->IsObjectArrayClass<kVerifyFlags, kReadBarrierOption>()));
56 AsObjectArray<mirror::Object, kVerifyNone, kReadBarrierOption>()->VisitReferences(visitor);
57 } else if ((class_flags & kClassFlagReference) != 0) {
58 VisitInstanceFieldsReferences<kVerifyFlags, kReadBarrierOption>(klass, visitor);
59 ref_visitor(klass, AsReference<kVerifyFlags, kReadBarrierOption>());
60 } else if (class_flags == kClassFlagDexCache) {
61 mirror::DexCache* const dex_cache = AsDexCache<kVerifyFlags, kReadBarrierOption>();
62 dex_cache->VisitReferences<kVisitNativeRoots,
63 kVerifyFlags,
64 kReadBarrierOption>(klass, visitor);
65 } else {
66 mirror::ClassLoader* const class_loader = AsClassLoader<kVerifyFlags, kReadBarrierOption>();
67 class_loader->VisitReferences<kVisitNativeRoots,
68 kVerifyFlags,
69 kReadBarrierOption>(klass, visitor);
70 }
71 } else if (kIsDebugBuild) {
72 CHECK((!klass->IsClassClass<kVerifyFlags, kReadBarrierOption>()));
73 CHECK((!klass->IsObjectArrayClass<kVerifyFlags, kReadBarrierOption>()));
74 // String still has instance fields for reflection purposes but these don't exist in
75 // actual string instances.
76 if (!klass->IsStringClass()) {
77 size_t total_reference_instance_fields = 0;
78 ObjPtr<Class> super_class = klass;
79 do {
80 total_reference_instance_fields += super_class->NumReferenceInstanceFields();
81 super_class = super_class->GetSuperClass<kVerifyFlags, kReadBarrierOption>();
82 } while (super_class != nullptr);
83 // The only reference field should be the object's class. This field is handled at the
84 // beginning of the function.
85 CHECK_EQ(total_reference_instance_fields, 1u);
86 }
87 }
88 }
89 }
90
91 } // namespace mirror
92 } // namespace art
93
94 #endif // ART_RUNTIME_MIRROR_OBJECT_REFVISITOR_INL_H_
95