• 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 PANDA_RUNTIME_MEM_GC_GEN_GC_GEN_GC_H_
17 #define PANDA_RUNTIME_MEM_GC_GEN_GC_GEN_GC_H_
18 
19 #include "runtime/include/mem/panda_smart_pointers.h"
20 #include "runtime/mem/gc/card_table.h"
21 #include "runtime/mem/gc/generational-gc-base.h"
22 
23 namespace panda {
24 class ManagedThread;
25 }  // namespace panda
26 namespace panda::mem {
27 
28 /**
29  * \brief Generational GC
30  */
31 template <class LanguageConfig>
32 class GenGC : public GenerationalGC<LanguageConfig> {
33 public:
34     explicit GenGC(ObjectAllocatorBase *object_allocator, const GCSettings &settings);
35     ~GenGC() override = default;
36     DEFAULT_MOVE_SEMANTIC(GenGC);
37     DEFAULT_COPY_SEMANTIC(GenGC);
38 
39     void InitGCBits(panda::ObjectHeader *obj_header) override;
40 
41     void InitGCBitsForAllocationInTLAB(panda::ObjectHeader *obj_header) override;
42 
43     void Trigger() override;
44 
45     void MarkReferences(PandaStackTL<ObjectHeader *> *references, GCPhase gc_phase) override;
46 
47     void MarkObject(ObjectHeader *object_header) override;
48 
49     bool MarkObjectIfNotMarked(ObjectHeader *object_header) override;
50 
51     void UnMarkObject(ObjectHeader *object_header) override;
52 
53     bool InGCSweepRange(uintptr_t addr) const override;
54 
55 private:
56     void InitializeImpl() override;
57 
58     void RunPhasesImpl(const GCTask &task) override;
59 
60     void PreStartupImp() override;
61 
62     /**
63      * GC for young generation. Runs with STW.
64      */
65     void RunYoungGC(const GCTask &task);
66 
67     /**
68      * GC for tenured generation.
69      */
70     void RunTenuredGC(const GCTask &task);
71 
72     /**
73      * Marks objects in young generation
74      */
75     void MarkYoung(const GCTask &task);
76 
77     void MarkYoungStack(PandaStackTL<ObjectHeader *> *objects_stack);
78 
79     /**
80      * Mark roots and add them to the stack
81      * @param objects_stack
82      * @param visit_class_roots
83      * @param visit_card_table_roots
84      */
85     void MarkRoots(PandaStackTL<ObjectHeader *> *objects_stack, CardTableVisitFlag visit_card_table_roots,
86                    VisitGCRootFlags flags = VisitGCRootFlags::ACCESS_ROOT_ALL);
87 
88     /**
89      * Initial marks roots and fill in 1st level from roots into stack.
90      * STW
91      * @param objects_stack
92      */
93     void InitialMark(PandaStackTL<ObjectHeader *> *objects_stack);
94 
95     /**
96      * Concurrently marking all objects
97      * @param objects_stack
98      */
99     NO_THREAD_SAFETY_ANALYSIS void ConcurrentMark(PandaStackTL<ObjectHeader *> *objects_stack,
100                                                   CardTableVisitFlag visit_card_table_roots);
101 
102     /**
103      * ReMarks objects after Concurrent marking
104      * @param objects_stack
105      */
106     void ReMark(PandaStackTL<ObjectHeader *> *objects_stack, const GCTask &task);
107 
108     /**
109      * Mark all objects in stack recursively for Full GC.
110      */
111     void MarkStack(PandaStackTL<ObjectHeader *> *stack);
112 
113     /**
114      * Collect dead objects in young generation and move survivors
115      * @return true if moving was success, false otherwise
116      */
117     bool CollectYoungAndMove(const GCTask &task);
118 
119     /**
120      * Sweeps string table from about to become dangled pointers to young generation
121      */
122     void SweepStringTableYoung();
123 
124     /**
125      * Remove dead strings from string table
126      */
127     void SweepStringTable();
128 
129     /**
130      * Update all refs to moved objects
131      */
132     void UpdateRefsToMovedObjects(PandaVector<ObjectHeader *> *moved_objects);
133 
134     void Sweep();
135 
136     bool IsMarked(const ObjectHeader *object) const override;
137 
138     bool ShouldRunTenuredGC(const GCTask &task) override;
139 
140     bool concurrent_marking_flag_ {false};  //! flag indicates if we currently in concurrent marking phase
141     PandaUniquePtr<CardTable> card_table_ {nullptr};
142 };
143 
144 }  // namespace panda::mem
145 
146 #endif  // PANDA_RUNTIME_MEM_GC_GEN_GC_GEN_GC_H_
147