• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef ECMASCRIPT_MEM_OLD_GC_VISITOR_INL_H
17 #define ECMASCRIPT_MEM_OLD_GC_VISITOR_INL_H
18 
19 #include "ecmascript/mem/old_gc_visitor.h"
20 
21 #include "ecmascript/js_hclass-inl.h"
22 #include "ecmascript/mem/object_xray.h"
23 #include "ecmascript/mem/region-inl.h"
24 #include "ecmascript/mem/work_manager-inl.h"
25 
26 namespace panda::ecmascript {
OldGCMarkRootVisitor(WorkNodeHolder * workNodeHolder)27 OldGCMarkRootVisitor::OldGCMarkRootVisitor(WorkNodeHolder *workNodeHolder) : workNodeHolder_(workNodeHolder) {}
28 
VisitRoot(Root type,ObjectSlot slot)29 void OldGCMarkRootVisitor::VisitRoot([[maybe_unused]] Root type, ObjectSlot slot)
30 {
31     HandleSlot(slot);
32 }
33 
VisitRangeRoot(Root type,ObjectSlot start,ObjectSlot end)34 void OldGCMarkRootVisitor::VisitRangeRoot([[maybe_unused]] Root type, ObjectSlot start, ObjectSlot end)
35 {
36     for (ObjectSlot slot = start; slot < end; slot++) {
37         HandleSlot(slot);
38     }
39 }
40 
VisitBaseAndDerivedRoot(Root type,ObjectSlot base,ObjectSlot derived,uintptr_t baseOldObject)41 void OldGCMarkRootVisitor::VisitBaseAndDerivedRoot([[maybe_unused]] Root type, [[maybe_unused]] ObjectSlot base,
42                                                    [[maybe_unused]] ObjectSlot derived,
43                                                    [[maybe_unused]] uintptr_t baseOldObject)
44 {
45     // It is only used to update the derived value. The mark of OldGC does not need to update slot
46 }
47 
HandleSlot(ObjectSlot slot)48 void OldGCMarkRootVisitor::HandleSlot(ObjectSlot slot)
49 {
50     JSTaggedValue value(slot.GetTaggedType());
51     if (!value.IsHeapObject()) {
52         return;
53     }
54 
55     ASSERT(!value.IsWeak());
56     TaggedObject *object = value.GetTaggedObject();
57     Region *objectRegion = Region::ObjectAddressToRange(object);
58 
59     if (objectRegion->InSharedHeap()) {
60         return;
61     }
62 
63     if (objectRegion->IsFreshRegion()) {
64         // This should only happen in MarkRoot from js thread.
65         ASSERT(JSThread::GetCurrent() != nullptr);
66         objectRegion->NonAtomicMark(object);
67     } else if (objectRegion->AtomicMark(object)) {
68         workNodeHolder_->Push(object);
69     }
70 }
71 
OldGCMarkObjectVisitor(WorkNodeHolder * workNodeHolder)72 OldGCMarkObjectVisitor::OldGCMarkObjectVisitor(WorkNodeHolder *workNodeHolder) : workNodeHolder_(workNodeHolder) {}
73 
VisitObjectRangeImpl(TaggedObject * root,ObjectSlot start,ObjectSlot end,VisitObjectArea area)74 void OldGCMarkObjectVisitor::VisitObjectRangeImpl(TaggedObject *root, ObjectSlot start, ObjectSlot end,
75                                                   VisitObjectArea area)
76 {
77     Region *rootRegion = Region::ObjectAddressToRange(root);
78     bool rootNeedEvacuate = rootRegion->InYoungSpaceOrCSet();
79     if (UNLIKELY(area == VisitObjectArea::IN_OBJECT)) {
80         JSHClass *hclass = root->SynchronizedGetClass();
81         ASSERT(!hclass->IsAllTaggedProp());
82         int index = 0;
83         LayoutInfo *layout = LayoutInfo::UncheckCast(hclass->GetLayout().GetTaggedObject());
84         ObjectSlot realEnd = start;
85         realEnd += layout->GetPropertiesCapacity();
86         end = end > realEnd ? realEnd : end;
87         for (ObjectSlot slot = start; slot < end; slot++) {
88             PropertyAttributes attr = layout->GetAttr(index++);
89             if (attr.IsTaggedRep()) {
90                 HandleSlot(slot, rootRegion, rootNeedEvacuate);
91             }
92         }
93         return;
94     }
95     for (ObjectSlot slot = start; slot < end; slot++) {
96         HandleSlot(slot, rootRegion, rootNeedEvacuate);
97     }
98 }
99 
VisitObjectHClassImpl(TaggedObject * hclass)100 void OldGCMarkObjectVisitor::VisitObjectHClassImpl(TaggedObject *hclass)
101 {
102     ASSERT(hclass->GetClass()->IsHClass());
103     Region *hclassRegion = Region::ObjectAddressToRange(hclass);
104     if (!hclassRegion->InSharedHeap()) {
105         ASSERT(hclassRegion->InNonMovableSpace() || hclassRegion->InReadOnlySpace());
106         MarkAndPush(hclass, hclassRegion);
107     }
108 }
109 
HandleSlot(ObjectSlot slot,Region * rootRegion,bool rootNeedEvacuate)110 void OldGCMarkObjectVisitor::HandleSlot(ObjectSlot slot, Region *rootRegion, bool rootNeedEvacuate)
111 {
112     JSTaggedValue value(slot.GetTaggedType());
113     if (!value.IsHeapObject()) {
114         return;
115     }
116 
117     // Region is correct no matter value is weak or not.
118     Region *objectRegion = Region::ObjectAddressToRange(value.GetRawHeapObject());
119 
120     if (!value.IsWeakForHeapObject()) {
121         TaggedObject *object = value.GetTaggedObject();
122         HandleObject(object, objectRegion);
123     } else {
124         bool objectNeedEvacuate = objectRegion->InYoungSpaceOrCSet();
125         if (!rootNeedEvacuate && !objectNeedEvacuate) {
126             // If `rootNeedEvacuate`, every slot will be updated when/after it has been evacuated.
127             // Otherwise if `objectNeedEvacuate`, weak reference will be updated in UpdateReference by visiting RSet.
128             RecordWeakReference(reinterpret_cast<JSTaggedType*>(slot.SlotAddress()));
129         }
130     }
131 
132     bool objectInCSet = objectRegion->InCollectSet();
133     if (!rootNeedEvacuate && objectInCSet) {
134         rootRegion->AtomicInsertCrossRegionRSet(slot.SlotAddress());
135     }
136 }
137 
HandleObject(TaggedObject * object,Region * objectRegion)138 void OldGCMarkObjectVisitor::HandleObject(TaggedObject *object, Region *objectRegion)
139 {
140     if (!objectRegion->InSharedHeap() && !objectRegion->IsFreshRegion()) {
141         MarkAndPush(object, objectRegion);
142     }
143 }
144 
MarkAndPush(TaggedObject * object,Region * objectRegion)145 void OldGCMarkObjectVisitor::MarkAndPush(TaggedObject *object, Region *objectRegion)
146 {
147     if (objectRegion->AtomicMark(object)) {
148         workNodeHolder_->Push(object);
149     }
150 }
151 
RecordWeakReference(JSTaggedType * weak)152 void OldGCMarkObjectVisitor::RecordWeakReference(JSTaggedType *weak)
153 {
154     workNodeHolder_->PushWeakReference(weak);
155 }
156 
157 }  // namespace panda::ecmascript
158 #endif  // ECMASCRIPT_MEM_OLD_GC_VISITOR_INL_H
159