blob: 263b7746dbab733c6e40f310222bc3975011ee02 [file] [log] [blame]
Andreas Gampec6ea7d02017-02-01 16:46:28 -08001/*
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 Gampea1d2f952017-04-20 22:53:58 -070022#include "art_field-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080023#include "class_ext-inl.h"
24
25namespace art {
26namespace mirror {
27
28template <bool kVisitNativeRoots,
29 VerifyObjectFlags kVerifyFlags,
30 ReadBarrierOption kReadBarrierOption,
31 typename Visitor>
32inline 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 Marko2c64a832018-01-04 11:31:56 +000035 // (ClassStatus::kNotReady, see ClassLinker::LoadClass()), GC may find it
Andreas Gampec6ea7d02017-02-01 16:46:28 -080036 // and scan it. IsTemp() may call Class::GetAccessFlags() but may
37 // fail in the DCHECK in Class::GetAccessFlags() because the class
Vladimir Marko2c64a832018-01-04 11:31:56 +000038 // status is ClassStatus::kNotReady. To avoid it, rely on IsResolved()
Andreas Gampec6ea7d02017-02-01 16:46:28 -080039 // only. This is fine because a temp class never goes into the
Vladimir Marko2c64a832018-01-04 11:31:56 +000040 // ClassStatus::kResolved state.
Andreas Gampec6ea7d02017-02-01 16:46:28 -080041 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
54template<ReadBarrierOption kReadBarrierOption, class Visitor>
55void Class::VisitNativeRoots(Visitor& visitor, PointerSize pointer_size) {
56 for (ArtField& field : GetSFieldsUnchecked()) {
57 // Visit roots first in case the declaring class gets moved.
58 field.VisitRoots(visitor);
59 if (kIsDebugBuild && IsResolved()) {
60 CHECK_EQ(field.GetDeclaringClass<kReadBarrierOption>(), this) << GetStatus();
61 }
62 }
63 for (ArtField& field : GetIFieldsUnchecked()) {
64 // Visit roots first in case the declaring class gets moved.
65 field.VisitRoots(visitor);
66 if (kIsDebugBuild && IsResolved()) {
67 CHECK_EQ(field.GetDeclaringClass<kReadBarrierOption>(), this) << GetStatus();
68 }
69 }
70 for (ArtMethod& method : GetMethods(pointer_size)) {
71 method.VisitRoots<kReadBarrierOption>(visitor, pointer_size);
72 }
73 ObjPtr<ClassExt> ext(GetExtData<kDefaultVerifyFlags, kReadBarrierOption>());
74 if (!ext.IsNull()) {
75 ext->VisitNativeRoots<kReadBarrierOption, Visitor>(visitor, pointer_size);
76 }
77}
78
79} // namespace mirror
80} // namespace art
81
82#endif // ART_RUNTIME_MIRROR_CLASS_REFVISITOR_INL_H_