1 /*
2 * Copyright (c) 2025 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_UNIFIED_GC_UNIFIED_GC_MARKER_INL_H
17 #define ECMASCRIPT_MEM_UNIFIED_GC_UNIFIED_GC_MARKER_INL_H
18
19 #include "ecmascript/cross_vm/unified_gc/unified_gc_marker.h"
20
21 #include "ecmascript/cross_vm/daemon_task_hybrid-inl.h"
22 #include "ecmascript/cross_vm/js_tagged_value_hybrid-inl.h"
23 #include "ecmascript/cross_vm/work_manager_hybrid-inl.h"
24 #include "ecmascript/js_thread.h"
25 #include "ecmascript/layout_info.h"
26 #include "ecmascript/mem/work_manager-inl.h"
27
28 namespace panda::ecmascript {
29
UnifiedGCMarkRootVisitor(WorkNodeHolder * workNodeHolder,UnifiedGCMarker * marker)30 UnifiedGCMarkRootVisitor::UnifiedGCMarkRootVisitor(WorkNodeHolder *workNodeHolder, UnifiedGCMarker *marker)
31 : workNodeHolder_(workNodeHolder), marker_(marker) {}
32
VisitRoot(Root type,ObjectSlot slot)33 void UnifiedGCMarkRootVisitor::VisitRoot([[maybe_unused]] Root type, ObjectSlot slot)
34 {
35 HandleSlot(slot);
36 }
37
VisitRangeRoot(Root type,ObjectSlot start,ObjectSlot end)38 void UnifiedGCMarkRootVisitor::VisitRangeRoot([[maybe_unused]] Root type, ObjectSlot start, ObjectSlot end)
39 {
40 for (ObjectSlot slot = start; slot < end; slot++) {
41 HandleSlot(slot);
42 }
43 }
44
VisitBaseAndDerivedRoot(Root type,ObjectSlot base,ObjectSlot derived,uintptr_t baseOldObject)45 void UnifiedGCMarkRootVisitor::VisitBaseAndDerivedRoot([[maybe_unused]] Root type, [[maybe_unused]] ObjectSlot base,
46 [[maybe_unused]] ObjectSlot derived,
47 [[maybe_unused]] uintptr_t baseOldObject)
48 {
49 // It is only used to update the derived value. The mark of OldGC does not need to update slot
50 }
51
HandleSlot(ObjectSlot slot)52 void UnifiedGCMarkRootVisitor::HandleSlot(ObjectSlot slot)
53 {
54 JSTaggedValue value(slot.GetTaggedType());
55 if (!value.IsHeapObject()) {
56 return;
57 }
58 TaggedObject *object = value.GetTaggedObject();
59 Region *objectRegion = Region::ObjectAddressToRange(object);
60
61 if (objectRegion->InSharedHeap()) {
62 return;
63 }
64
65 #ifdef PANDA_JS_ETS_HYBRID_MODE
66 marker_->HandleJSXRefObject(object);
67 #endif // PANDA_JS_ETS_HYBRID_MODE
68
69 if (objectRegion->AtomicMark(object)) {
70 workNodeHolder_->Push(object);
71 }
72 }
73
UnifiedGCMarkObjectVisitor(WorkNodeHolder * workNodeHolder,UnifiedGCMarker * marker)74 UnifiedGCMarkObjectVisitor::UnifiedGCMarkObjectVisitor(WorkNodeHolder *workNodeHolder, UnifiedGCMarker *marker)
75 : workNodeHolder_(workNodeHolder), marker_(marker) {}
76
VisitObjectRangeImpl(BaseObject * root,uintptr_t startAddr,uintptr_t endAddr,VisitObjectArea area)77 void UnifiedGCMarkObjectVisitor::VisitObjectRangeImpl(BaseObject *root, uintptr_t startAddr, uintptr_t endAddr,
78 VisitObjectArea area)
79 {
80 ObjectSlot start(startAddr);
81 ObjectSlot end(endAddr);
82 if (UNLIKELY(area == VisitObjectArea::IN_OBJECT)) {
83 JSHClass *hclass = TaggedObject::Cast(root)->SynchronizedGetClass();
84 ASSERT(!hclass->IsAllTaggedProp());
85 int index = 0;
86 JSThread *thread = workNodeHolder_->GetJSThread();
87 LayoutInfo *layout = LayoutInfo::UncheckCast(hclass->GetLayout(thread).GetTaggedObject());
88 ObjectSlot realEnd = start;
89 realEnd += layout->GetPropertiesCapacity();
90 end = end > realEnd ? realEnd : end;
91 for (ObjectSlot slot = start; slot < end; slot++) {
92 PropertyAttributes attr = layout->GetAttr(thread, index++);
93 if (attr.IsTaggedRep()) {
94 HandleSlot(slot);
95 }
96 }
97 return;
98 }
99 for (ObjectSlot slot = start; slot < end; slot++) {
100 HandleSlot(slot);
101 }
102 }
103
HandleSlot(ObjectSlot slot)104 void UnifiedGCMarkObjectVisitor::HandleSlot(ObjectSlot slot)
105 {
106 JSTaggedValue value(slot.GetTaggedType());
107 if (!value.IsHeapObject() || value.IsWeakForHeapObject()) {
108 return;
109 }
110 TaggedObject *object = value.GetTaggedObject();
111 Region *objectRegion = Region::ObjectAddressToRange(object);
112 if (objectRegion->InSharedHeap()) {
113 return;
114 }
115 #ifdef PANDA_JS_ETS_HYBRID_MODE
116 marker_->HandleJSXRefObject(object);
117 #endif // PANDA_JS_ETS_HYBRID_MODE
118 if (objectRegion->AtomicMark(object)) {
119 workNodeHolder_->Push(object);
120 }
121 }
122
VisitObjectHClassImpl(BaseObject * hclass)123 void UnifiedGCMarkObjectVisitor::VisitObjectHClassImpl(BaseObject *hclass)
124 {
125 ASSERT(TaggedObject::Cast(hclass)->GetClass()->IsHClass());
126 Region *hclassRegion = Region::ObjectAddressToRange(hclass);
127 if (!hclassRegion->InSharedHeap()) {
128 ASSERT(hclassRegion->InNonMovableSpace() || hclassRegion->InReadOnlySpace());
129 if (hclassRegion->AtomicMark(hclass)) {
130 workNodeHolder_->Push(TaggedObject::Cast(hclass));
131 }
132 }
133 }
134
135 #ifdef PANDA_JS_ETS_HYBRID_MODE
HandleJSXRefObject(TaggedObject * object)136 inline void UnifiedGCMarker::HandleJSXRefObject(TaggedObject *object)
137 {
138 JSTaggedValue value(object);
139 if (value.IsJSXRefObject()) {
140 auto stsVMInterface = heap_->GetEcmaVM()->GetCrossVMOperator()->GetSTSVMInterface();
141 stsVMInterface->MarkFromObject(JSObject::Cast(object)->GetNativePointerField(heap_->GetJSThread(), 0));
142 }
143 }
144 #endif // PANDA_JS_ETS_HYBRID_MODE
145 } // namespace panda::ecmascript
146 #endif // ECMASCRIPT_MEM_UNIFIED_GC_UNIFIED_GC_MARKER_INL_H
147