• 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(BaseObject * rootObject,uintptr_t start,uintptr_t end,VisitObjectArea area)74 void OldGCMarkObjectVisitor::VisitObjectRangeImpl(BaseObject *rootObject, uintptr_t start, uintptr_t end,
75                                                   VisitObjectArea area)
76 {
77     ObjectSlot startSlot(start);
78     ObjectSlot endSlot(end);
79     auto root = TaggedObject::Cast(rootObject);
80     JSThread *thread = workNodeHolder_->GetJSThread();
81     Region *rootRegion = Region::ObjectAddressToRange(root);
82     bool rootNeedEvacuate = rootRegion->InYoungSpaceOrCSet();
83     if (UNLIKELY(area == VisitObjectArea::IN_OBJECT)) {
84         JSHClass *hclass = root->SynchronizedGetClass();
85         ASSERT(!hclass->IsAllTaggedProp());
86         int index = 0;
87         LayoutInfo *layout = LayoutInfo::UncheckCast(hclass->GetLayout(thread).GetTaggedObject());
88         ObjectSlot realEnd(start);
89         realEnd += layout->GetPropertiesCapacity();
90         endSlot = endSlot > realEnd ? realEnd : endSlot;
91         for (ObjectSlot slot = startSlot; slot < endSlot; slot++) {
92             PropertyAttributes attr = layout->GetAttr(thread, index++);
93             if (attr.IsTaggedRep()) {
94                 HandleSlot(slot, rootRegion, rootNeedEvacuate);
95             }
96         }
97         return;
98     }
99     for (ObjectSlot slot = startSlot; slot < endSlot; slot++) {
100         HandleSlot(slot, rootRegion, rootNeedEvacuate);
101     }
102 }
103 
VisitJSWeakMapImpl(BaseObject * rootObject)104 void OldGCMarkObjectVisitor::VisitJSWeakMapImpl(BaseObject *rootObject)
105 {
106     TaggedObject *obj = TaggedObject::Cast(rootObject);
107     ASSERT(JSTaggedValue(obj).IsJSWeakMap());
108     workNodeHolder_->PushJSWeakMap(obj);
109 }
110 
VisitObjectHClassImpl(BaseObject * hclassObject)111 void OldGCMarkObjectVisitor::VisitObjectHClassImpl(BaseObject *hclassObject)
112 {
113     auto hclass = reinterpret_cast<TaggedObject *>(hclassObject);
114     ASSERT(hclass->GetClass()->IsHClass());
115     Region *hclassRegion = Region::ObjectAddressToRange(hclass);
116     if (!hclassRegion->InSharedHeap()) {
117         ASSERT(hclassRegion->InNonMovableSpace() || hclassRegion->InReadOnlySpace());
118         MarkAndPush(hclass, hclassRegion);
119     }
120 }
121 
HandleSlot(ObjectSlot slot,Region * rootRegion,bool rootNeedEvacuate)122 void OldGCMarkObjectVisitor::HandleSlot(ObjectSlot slot, Region *rootRegion, bool rootNeedEvacuate)
123 {
124     JSTaggedValue value(slot.GetTaggedType());
125     if (!value.IsHeapObject()) {
126         return;
127     }
128 
129     // Region is correct no matter value is weak or not.
130     Region *objectRegion = Region::ObjectAddressToRange(value.GetRawHeapObject());
131 
132     if (!value.IsWeakForHeapObject()) {
133         TaggedObject *object = value.GetTaggedObject();
134         HandleObject(object, objectRegion);
135     } else {
136         bool objectNeedEvacuate = objectRegion->InYoungSpaceOrCSet();
137         if (!rootNeedEvacuate && !objectNeedEvacuate) {
138             // If `rootNeedEvacuate`, every slot will be updated when/after it has been evacuated.
139             // Otherwise if `objectNeedEvacuate`, weak reference will be updated in UpdateReference by visiting RSet.
140             RecordWeakReference(reinterpret_cast<JSTaggedType*>(slot.SlotAddress()));
141         }
142     }
143 
144     bool objectInCSet = objectRegion->InCollectSet();
145     if (!rootNeedEvacuate && objectInCSet) {
146         rootRegion->AtomicInsertCrossRegionRSet(slot.SlotAddress());
147     }
148 }
149 
HandleObject(TaggedObject * object,Region * objectRegion)150 void OldGCMarkObjectVisitor::HandleObject(TaggedObject *object, Region *objectRegion)
151 {
152     if (!objectRegion->InSharedHeap() && !objectRegion->IsFreshRegion()) {
153         MarkAndPush(object, objectRegion);
154     }
155 }
156 
MarkAndPush(TaggedObject * object,Region * objectRegion)157 void OldGCMarkObjectVisitor::MarkAndPush(TaggedObject *object, Region *objectRegion)
158 {
159     if (objectRegion->AtomicMark(object)) {
160         workNodeHolder_->Push(object);
161     }
162 }
163 
RecordWeakReference(JSTaggedType * weak)164 void OldGCMarkObjectVisitor::RecordWeakReference(JSTaggedType *weak)
165 {
166     workNodeHolder_->PushWeakReference(weak);
167 }
168 
169 }  // namespace panda::ecmascript
170 #endif  // ECMASCRIPT_MEM_OLD_GC_VISITOR_INL_H
171