OSDN Git Service

ffc4af5712f7cad0824a012a70feff68c6939d7d
[android-x86/art.git] / runtime / mirror / class-refvisitor-inl.h
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
22 #include "class_ext-inl.h"
23
24 namespace art {
25 namespace mirror {
26
27 template <bool kVisitNativeRoots,
28           VerifyObjectFlags kVerifyFlags,
29           ReadBarrierOption kReadBarrierOption,
30           typename Visitor>
31 inline void Class::VisitReferences(ObjPtr<Class> klass, const Visitor& visitor) {
32   VisitInstanceFieldsReferences<kVerifyFlags, kReadBarrierOption>(klass.Ptr(), visitor);
33   // Right after a class is allocated, but not yet loaded
34   // (kStatusNotReady, see ClassLinker::LoadClass()), GC may find it
35   // and scan it. IsTemp() may call Class::GetAccessFlags() but may
36   // fail in the DCHECK in Class::GetAccessFlags() because the class
37   // status is kStatusNotReady. To avoid it, rely on IsResolved()
38   // only. This is fine because a temp class never goes into the
39   // kStatusResolved state.
40   if (IsResolved<kVerifyFlags>()) {
41     // Temp classes don't ever populate imt/vtable or static fields and they are not even
42     // allocated with the right size for those. Also, unresolved classes don't have fields
43     // linked yet.
44     VisitStaticFieldsReferences<kVerifyFlags, kReadBarrierOption>(this, visitor);
45   }
46   if (kVisitNativeRoots) {
47     // Since this class is reachable, we must also visit the associated roots when we scan it.
48     VisitNativeRoots<kReadBarrierOption>(
49         visitor, Runtime::Current()->GetClassLinker()->GetImagePointerSize());
50   }
51 }
52
53 template<ReadBarrierOption kReadBarrierOption, class Visitor>
54 void Class::VisitNativeRoots(Visitor& visitor, PointerSize pointer_size) {
55   for (ArtField& field : GetSFieldsUnchecked()) {
56     // Visit roots first in case the declaring class gets moved.
57     field.VisitRoots(visitor);
58     if (kIsDebugBuild && IsResolved()) {
59       CHECK_EQ(field.GetDeclaringClass<kReadBarrierOption>(), this) << GetStatus();
60     }
61   }
62   for (ArtField& field : GetIFieldsUnchecked()) {
63     // Visit roots first in case the declaring class gets moved.
64     field.VisitRoots(visitor);
65     if (kIsDebugBuild && IsResolved()) {
66       CHECK_EQ(field.GetDeclaringClass<kReadBarrierOption>(), this) << GetStatus();
67     }
68   }
69   for (ArtMethod& method : GetMethods(pointer_size)) {
70     method.VisitRoots<kReadBarrierOption>(visitor, pointer_size);
71   }
72   ObjPtr<ClassExt> ext(GetExtData<kDefaultVerifyFlags, kReadBarrierOption>());
73   if (!ext.IsNull()) {
74     ext->VisitNativeRoots<kReadBarrierOption, Visitor>(visitor, pointer_size);
75   }
76 }
77
78 }  // namespace mirror
79 }  // namespace art
80
81 #endif  // ART_RUNTIME_MIRROR_CLASS_REFVISITOR_INL_H_