• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_H
17 #define ECMASCRIPT_MEM_UNIFIED_GC_UNIFIED_GC_MARKER_H
18 
19 #include "ecmascript/ecma_global_storage.h"
20 #include "ecmascript/mem/parallel_marker.h"
21 #include "ecmascript/mem/visitor.h"
22 #ifdef PANDA_JS_ETS_HYBRID_MODE
23 #include "ecmascript/cross_vm/cross_vm_operator.h"
24 #endif // PANDA_JS_ETS_HYBRID_MODE
25 
26 namespace panda::ecmascript {
27 
28 class UnifiedGCMarker : public Marker {
29 public:
UnifiedGCMarker(Heap * heap)30     UnifiedGCMarker(Heap *heap)
31         : Marker(heap) {}
32     ~UnifiedGCMarker() override = default;
33 
34     void Initialize() override;
35     void InitialMark(uint32_t threadId);
36     void MarkFromObject(TaggedObject *object);
37     void ProcessMarkStack(uint32_t threadId) override;
38     void Finish();
39 
40 private:
41     friend class UnifiedGCMarkRootVisitor;
42     friend class UnifiedGCMarkObjectVisitor;
43 
44 #ifdef PANDA_JS_ETS_HYBRID_MODE
45     inline void HandleJSXRefObject(TaggedObject *object);
46 #endif // PANDA_JS_ETS_HYBRID_MODE
47 
48     std::atomic<bool> initialized_ {false};
49     Mutex initializeMutex_;
50 };
51 
52 class UnifiedGCMarkRootVisitor final : public RootVisitor {
53 public:
54     inline explicit UnifiedGCMarkRootVisitor(WorkNodeHolder *workNodeHolder, UnifiedGCMarker *marker);
55     ~UnifiedGCMarkRootVisitor() override = default;
56 
57     inline void VisitRoot([[maybe_unused]] Root type, ObjectSlot slot) override;
58 
59     inline void VisitRangeRoot([[maybe_unused]] Root type, ObjectSlot start, ObjectSlot end) override;
60 
61     inline void VisitBaseAndDerivedRoot([[maybe_unused]] Root type, ObjectSlot base, ObjectSlot derived,
62                                         uintptr_t baseOldObject) override;
63 private:
64     inline void HandleSlot(ObjectSlot slot);
65 
66     WorkNodeHolder *workNodeHolder_ {nullptr};
67     [[maybe_unused]]UnifiedGCMarker *marker_ {nullptr};
68 };
69 
70 class UnifiedGCMarkObjectVisitor final : public BaseObjectVisitor<UnifiedGCMarkObjectVisitor> {
71 public:
72     inline explicit UnifiedGCMarkObjectVisitor(WorkNodeHolder *workNodeHolder, UnifiedGCMarker *marker);
73     ~UnifiedGCMarkObjectVisitor() override = default;
74 
75     inline void VisitObjectRangeImpl(BaseObject *root, uintptr_t start, uintptr_t end,
76                                      VisitObjectArea area) override;
77     inline void VisitObjectHClassImpl(BaseObject *hclass) override;
78 private:
79     inline void HandleSlot(ObjectSlot slot);
80 
81     WorkNodeHolder *workNodeHolder_ {nullptr};
82     [[maybe_unused]]UnifiedGCMarker *marker_ {nullptr};
83 };
84 
85 class UnifiedGCMarkRootsScope {
86 public:
UnifiedGCMarkRootsScope(JSThread * jsThread)87     explicit UnifiedGCMarkRootsScope(JSThread *jsThread): jsThread_(jsThread)
88     {
89         jsThread_->SetNodeKind(NodeKind::UNIFIED_NODE);
90     }
91 
~UnifiedGCMarkRootsScope()92     ~UnifiedGCMarkRootsScope()
93     {
94         jsThread_->SetNodeKind(NodeKind::NORMAL_NODE);
95     }
96 
97 private:
98     JSThread *jsThread_;
99     NO_COPY_SEMANTIC(UnifiedGCMarkRootsScope);
100 };
101 }  // namespace panda::ecmascript
102 
103 #endif  // ECMASCRIPT_MEM_UNIFIED_GC_UNIFIED_GC_MARKER_H