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(BaseObject * root,uintptr_t startAddr,uintptr_t endAddr,VisitObjectArea area)74 void YoungGCMarkObjectVisitor::VisitObjectRangeImpl(BaseObject *root, uintptr_t startAddr, uintptr_t endAddr,
75 VisitObjectArea area)
76 {
77 ObjectSlot start(startAddr);
78 ObjectSlot end(endAddr);
79 if (UNLIKELY(area == VisitObjectArea::IN_OBJECT)) {
80 JSThread *thread = workNodeHolder_->GetJSThread();
81 JSHClass *hclass = TaggedObject::Cast(root)->SynchronizedGetClass();
82 ASSERT(!hclass->IsAllTaggedProp());
83 int index = 0;
84 LayoutInfo *layout = LayoutInfo::UncheckCast(hclass->GetLayout(thread).GetTaggedObject());
85 ObjectSlot realEnd = start;
86 realEnd += layout->GetPropertiesCapacity();
87 end = end > realEnd ? realEnd : end;
88 for (ObjectSlot slot = start; slot < end; slot++) {
89 PropertyAttributes attr = layout->GetAttr(thread, index++);
90 if (attr.IsTaggedRep()) {
91 HandleSlot(slot);
92 }
93 }
94 return;
95 }
96 for (ObjectSlot slot = start; slot < end; slot++) {
97 HandleSlot(slot);
98 }
99 }
100
VisitJSWeakMapImpl(BaseObject * rootObject)101 void YoungGCMarkObjectVisitor::VisitJSWeakMapImpl(BaseObject *rootObject)
102 {
103 TaggedObject *obj = TaggedObject::Cast(rootObject);
104 ASSERT(JSTaggedValue(obj).IsJSWeakMap());
105 workNodeHolder_->PushJSWeakMap(obj);
106 }
107
HandleSlot(ObjectSlot slot)108 void YoungGCMarkObjectVisitor::HandleSlot(ObjectSlot slot)
109 {
110 JSTaggedValue value(slot.GetTaggedType());
111 if (!value.IsHeapObject() || value.IsWeakForHeapObject()) {
112 return;
113 }
114
115 TaggedObject *object = value.GetTaggedObject();
116 Region *objectRegion = Region::ObjectAddressToRange(object);
117 if (!objectRegion->InYoungSpace() || objectRegion->IsFreshRegion()) {
118 return;
119 }
120
121 if (objectRegion->AtomicMark(object)) {
122 workNodeHolder_->Push(object);
123 }
124 }
125
YoungGCMarkOldToNewRSetVisitor(WorkNodeHolder * workNodeHolder)126 YoungGCMarkOldToNewRSetVisitor::YoungGCMarkOldToNewRSetVisitor(WorkNodeHolder *workNodeHolder)
127 : workNodeHolder_(workNodeHolder) {}
128
operator()129 void YoungGCMarkOldToNewRSetVisitor::operator()(Region *region) const
130 {
131 region->IterateAllOldToNewBits([this](void *mem) -> bool {
132 ObjectSlot slot(ToUintPtr(mem));
133 return HandleSlot(slot);
134 });
135 }
136
HandleSlot(ObjectSlot slot)137 bool YoungGCMarkOldToNewRSetVisitor::HandleSlot(ObjectSlot slot) const
138 {
139 JSTaggedValue value(slot.GetTaggedType());
140 if (!value.IsHeapObject()) {
141 return false;
142 }
143
144 Region *region = Region::ObjectAddressToRange(value.GetRawHeapObject());
145 if (!region->InYoungSpace()) {
146 return false;
147 }
148
149 // In initial mark, all regions are non-fresh.
150 ASSERT(!region->IsFreshRegion());
151
152 if (value.IsWeakForHeapObject()) {
153 // Keep OldToNew to update weak reference.
154 return true;
155 }
156
157 TaggedObject *object = value.GetTaggedObject();
158 if (region->AtomicMark(object)) {
159 workNodeHolder_->Push(object);
160 }
161 return true;
162 }
163
164 } // namespace panda::ecmascript
165 #endif // ECMASCRIPT_MEM_YOUNG_GC_VISITOR_INL_H
166