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_YOUNG_GC_VISITOR_INL_H
17 #define ECMASCRIPT_MEM_YOUNG_GC_VISITOR_INL_H
18
19 #include "ecmascript/mem/young_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 {
YoungGCMarkRootVisitor(WorkNodeHolder * workNodeHolder)27 YoungGCMarkRootVisitor::YoungGCMarkRootVisitor(WorkNodeHolder *workNodeHolder) : workNodeHolder_(workNodeHolder) {}
28
VisitRoot(Root type,ObjectSlot slot)29 void YoungGCMarkRootVisitor::VisitRoot([[maybe_unused]] Root type, ObjectSlot slot)
30 {
31 HandleSlot(slot);
32 }
33
VisitRangeRoot(Root type,ObjectSlot start,ObjectSlot end)34 void YoungGCMarkRootVisitor::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 YoungGCMarkRootVisitor::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 YoungGC does not need to update slot
46 }
47
HandleSlot(ObjectSlot slot)48 void YoungGCMarkRootVisitor::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->InYoungSpace()) {
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
YoungGCMarkObjectVisitor(WorkNodeHolder * workNodeHolder)72 YoungGCMarkObjectVisitor::YoungGCMarkObjectVisitor(WorkNodeHolder *workNodeHolder) : workNodeHolder_(workNodeHolder) {}
73
VisitObjectRangeImpl(TaggedObject * root,ObjectSlot start,ObjectSlot end,VisitObjectArea area)74 void YoungGCMarkObjectVisitor::VisitObjectRangeImpl(TaggedObject *root, ObjectSlot start, ObjectSlot end,
75 VisitObjectArea area)
76 {
77 if (UNLIKELY(area == VisitObjectArea::IN_OBJECT)) {
78 JSHClass *hclass = root->SynchronizedGetClass();
79 ASSERT(!hclass->IsAllTaggedProp());
80 int index = 0;
81 LayoutInfo *layout = LayoutInfo::UncheckCast(hclass->GetLayout().GetTaggedObject());
82 ObjectSlot realEnd = start;
83 realEnd += layout->GetPropertiesCapacity();
84 end = end > realEnd ? realEnd : end;
85 for (ObjectSlot slot = start; slot < end; slot++) {
86 PropertyAttributes attr = layout->GetAttr(index++);
87 if (attr.IsTaggedRep()) {
88 HandleSlot(slot);
89 }
90 }
91 return;
92 }
93 for (ObjectSlot slot = start; slot < end; slot++) {
94 HandleSlot(slot);
95 }
96 }
97
HandleSlot(ObjectSlot slot)98 void YoungGCMarkObjectVisitor::HandleSlot(ObjectSlot slot)
99 {
100 JSTaggedValue value(slot.GetTaggedType());
101 if (!value.IsHeapObject() || value.IsWeakForHeapObject()) {
102 return;
103 }
104
105 TaggedObject *object = value.GetTaggedObject();
106 Region *objectRegion = Region::ObjectAddressToRange(object);
107 if (!objectRegion->InYoungSpace() || objectRegion->IsFreshRegion()) {
108 return;
109 }
110
111 if (objectRegion->AtomicMark(object)) {
112 workNodeHolder_->Push(object);
113 }
114 }
115
YoungGCMarkOldToNewRSetVisitor(WorkNodeHolder * workNodeHolder)116 YoungGCMarkOldToNewRSetVisitor::YoungGCMarkOldToNewRSetVisitor(WorkNodeHolder *workNodeHolder)
117 : workNodeHolder_(workNodeHolder) {}
118
operator()119 void YoungGCMarkOldToNewRSetVisitor::operator()(Region *region) const
120 {
121 region->IterateAllOldToNewBits([this](void *mem) -> bool {
122 ObjectSlot slot(ToUintPtr(mem));
123 return HandleSlot(slot);
124 });
125 }
126
HandleSlot(ObjectSlot slot)127 bool YoungGCMarkOldToNewRSetVisitor::HandleSlot(ObjectSlot slot) const
128 {
129 JSTaggedValue value(slot.GetTaggedType());
130 if (!value.IsHeapObject()) {
131 return false;
132 }
133
134 Region *region = Region::ObjectAddressToRange(value.GetRawHeapObject());
135 if (!region->InYoungSpace()) {
136 return false;
137 }
138
139 // In initial mark, all regions are non-fresh.
140 ASSERT(!region->IsFreshRegion());
141
142 if (value.IsWeakForHeapObject()) {
143 // Keep OldToNew to update weak reference.
144 return true;
145 }
146
147 TaggedObject *object = value.GetTaggedObject();
148 if (region->AtomicMark(object)) {
149 workNodeHolder_->Push(object);
150 }
151 return true;
152 }
153
154 } // namespace panda::ecmascript
155 #endif // ECMASCRIPT_MEM_YOUNG_GC_VISITOR_INL_H
156