• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_FULL_GC_H
17 #define ECMASCRIPT_MEM_FULL_GC_H
18 
19 #include "ecmascript/mem/garbage_collector.h"
20 #include "ecmascript/mem/heap.h"
21 #include "ecmascript/mem/work_manager.h"
22 
23 namespace panda {
24 namespace ecmascript {
25 class FullGCRunner;
26 
27 class FullGC final : public GarbageCollector {
28 public:
29     explicit FullGC(Heap *heap);
30     ~FullGC() override = default;
31 
32     NO_COPY_SEMANTIC(FullGC);
33     NO_MOVE_SEMANTIC(FullGC);
34 
35     void RunPhases() override;
36     void RunPhasesForAppSpawn();
37     void SetForAppSpawn(bool flag);
38 protected:
39     void Initialize() override;
40     void Mark() override;
41     void Sweep() override;
42     void Finish() override;
43 
44 private:
45     void MarkRoots();
46     void ProcessSharedGCRSetWorkList();
47     bool HasEvacuated(Region *region);
48 
49     Heap *heap_;
50     size_t youngAndOldAliveSize_ = 0;
51     size_t nonMoveSpaceFreeSize_ = 0;
52     size_t youngSpaceCommitSize_ = 0;
53     size_t oldSpaceCommitSize_ = 0;
54     size_t nonMoveSpaceCommitSize_ = 0;
55     bool forAppSpawn_ {false};
56     // obtain from heap
57     WorkManager *workManager_ {nullptr};
58 
59     friend class WorkManager;
60     friend class Heap;
61 };
62 
63 class FullGCMarkRootVisitor final : public RootVisitor {
64 public:
65     inline explicit FullGCMarkRootVisitor(FullGCRunner *runner);
66     ~FullGCMarkRootVisitor() override = default;
67 
68     inline void VisitRoot([[maybe_unused]] Root type, ObjectSlot slot) override;
69 
70     inline void VisitRangeRoot([[maybe_unused]] Root type, ObjectSlot start, ObjectSlot end) override;
71 
72     inline void VisitBaseAndDerivedRoot([[maybe_unused]] Root type, ObjectSlot base, ObjectSlot derived,
73                                         uintptr_t baseOldObject) override;
74 private:
75     FullGCRunner *runner_ {nullptr};
76 };
77 
78 class FullGCMarkObjectVisitor final : public EcmaObjectRangeVisitor<FullGCMarkObjectVisitor> {
79 public:
80     inline explicit FullGCMarkObjectVisitor(FullGCRunner *runner);
81     ~FullGCMarkObjectVisitor() override = default;
82 
83     inline void VisitObjectRangeImpl(TaggedObject *root, ObjectSlot start, ObjectSlot end,
84                                      VisitObjectArea area) override;
85 
86     inline void VisitHClassSlot(ObjectSlot slot, TaggedObject *hclass);
87 private:
88     FullGCRunner *runner_ {nullptr};
89 };
90 
91 class FullGCUpdateLocalToShareRSetVisitor final : public EcmaObjectRangeVisitor<FullGCUpdateLocalToShareRSetVisitor> {
92 public:
93     inline explicit FullGCUpdateLocalToShareRSetVisitor(FullGCRunner *runner);
94     ~FullGCUpdateLocalToShareRSetVisitor() override = default;
95 
96     inline void VisitObjectRangeImpl(TaggedObject *root, ObjectSlot start, ObjectSlot end,
97                                      VisitObjectArea area) override;
98 private:
99     inline void SetLocalToShareRSet(ObjectSlot slot, Region *rootRegion);
100 
101     FullGCRunner *runner_ {nullptr};
102 };
103 
104 class FullGCRunner {
105 public:
106     inline explicit FullGCRunner(Heap *heap, WorkNodeHolder *workNodeHolder, bool isAppSpawn);
107     ~FullGCRunner() = default;
108 
109     inline FullGCMarkRootVisitor &GetMarkRootVisitor();
110     inline FullGCMarkObjectVisitor &GetMarkObjectVisitor();
111 
112 protected:
113     inline void HandleMarkingSlot(ObjectSlot slot);
114 
115     inline void HandleMarkingSlotObject(ObjectSlot slot, TaggedObject *object);
116 
117     template <class Callback>
118     void VisitBodyInObj(TaggedObject *root, ObjectSlot start, ObjectSlot end, Callback &&cb);
119 
120 private:
121     inline bool NeedEvacuate(Region *region);
122 
123     inline void EvacuateObject(ObjectSlot slot, TaggedObject *object, const MarkWord &markWord);
124 
125     inline uintptr_t AllocateForwardAddress(size_t size);
126 
127     inline uintptr_t AllocateDstSpace(size_t size);
128 
129     inline uintptr_t AllocateAppSpawnSpace(size_t size);
130 
131     inline void RawCopyObject(uintptr_t fromAddress, uintptr_t toAddress, size_t size, const MarkWord &markWord);
132 
133     inline void UpdateForwardAddressIfSuccess(ObjectSlot slot, TaggedObject *object, JSHClass *klass, size_t size,
134         TaggedObject *toAddress);
135 
136     inline void UpdateForwardAddressIfFailed(ObjectSlot slot, size_t size, uintptr_t toAddress, TaggedObject *dst);
137 
138     inline void PushObject(TaggedObject *object);
139 
140     inline void RecordWeakReference(JSTaggedType *weak);
141 
142     Heap *heap_ {nullptr};
143     WorkNodeHolder *workNodeHolder_ {nullptr};
144     bool isAppSpawn_ {false};
145     FullGCMarkRootVisitor markRootVisitor_;
146     FullGCMarkObjectVisitor markObjectVisitor_;
147     FullGCUpdateLocalToShareRSetVisitor updateLocalToShareRSetVisitor_;
148 
149     friend class FullGCMarkRootVisitor;
150     friend class FullGCMarkObjectVisitor;
151     friend class FullGCUpdateLocalToShareRSetVisitor;
152     friend class CompressGCMarker;
153 };
154 }  // namespace ecmascript
155 }  // namespace panda
156 
157 #endif  // ECMASCRIPT_MEM_FULL_GC_H
158