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