• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-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 PANDA_MEM_MEMORY_MANAGER_H
17 #define PANDA_MEM_MEMORY_MANAGER_H
18 
19 #include <libpandabase/macros.h>
20 #include <mem/mem.h>
21 #include <runtime/mem/gc/gc_types.h>
22 #include <runtime/mem/heap_manager.h>
23 #include "runtime/mem/lock_config_helper.h"
24 
25 namespace ark {
26 class RuntimeOptions;
27 }  // namespace ark
28 namespace ark::mem {
29 
30 class GCStats;
31 class GCTrigger;
32 class GCSettings;
33 class GCTriggerConfig;
34 class Allocator;
35 
36 /*
37  * Relations between VMs, MemoryManager and Runtime:
38  *
39  * +-----------------------------------------------+
40  * |                                               |
41  * |                  Runtime                      |
42  * |                                               |
43  * |  +----------+  +----------+     +----------+  |
44  * |  |          |  |          |     |          |  |
45  * |  |   VM_0   |  |   VM_1   |     |   VM_N   |  |
46  * |  |          |  |          |     |          |  |
47  * |  |          |  |          | ... |          |  |
48  * |  |  +----+  |  |  +----+  |     |  +----+  |  |
49  * |  |  |MM_0|  |  |  |MM_1|  |     |  |MM_N|  |  |
50  * |  |  +----+  |  |  +----+  |     |  +----+  |  |
51  * |  +----------+  +----------+     +----------+  |
52  * |         \           |            /            |
53  * |          \          |           /             |
54  * |           +--------------------+              |
55  * |           | Internal Allocator |              |
56  * |           +--------------------+              |
57  * +-----------------------------------------------+
58  */
59 
60 /**
61  * A class that encapsulates components for working with memory.
62  * Each VM is allocated its own instance.
63  */
64 class MemoryManager {
65 public:
66     struct HeapOptions {
67         HeapManager::IsObjectFinalizebleFunc isObjectFinalizebleFunc;
68         HeapManager::RegisterFinalizeReferenceFunc registerFinalizeReferenceFunc;
69         uint32_t maxGlobalRefSize;
70         bool isGlobalReferenceSizeCheckEnabled;
71         MTModeT multithreadingMode;
72         bool isUseTlabForAllocations;
73         bool isStartAsZygote;
74     };
75 
76     // CC-OFFNXT(G.FUN.01-CPP) solid logic, the fix will degrade the readability and maintenance of the code
77     static MemoryManager *Create(const LanguageContext &ctx, InternalAllocatorPtr internalAllocator, GCType gcType,
78                                  const GCSettings &gcSettings, const GCTriggerConfig &gcTriggerConfig,
79                                  const HeapOptions &heapOptions);
80     static void Destroy(MemoryManager *mm);
81 
82     NO_COPY_SEMANTIC(MemoryManager);
83     NO_MOVE_SEMANTIC(MemoryManager);
84 
85     void PreStartup();
86     void PreZygoteFork();
87     void PostZygoteFork();
88     void InitializeGC(PandaVM *vm);
89     PANDA_PUBLIC_API void StartGC();
90     void StopGC();
91 
92     void Finalize();
93 
GetHeapManager()94     HeapManager *GetHeapManager()
95     {
96         ASSERT(heapManager_ != nullptr);
97         return heapManager_;
98     }
99 
GetGC()100     GC *GetGC() const
101     {
102         ASSERT(gc_ != nullptr);
103         return gc_;
104     }
105 
GetGCTrigger()106     GCTrigger *GetGCTrigger()
107     {
108         ASSERT(gcTrigger_ != nullptr);
109         return gcTrigger_;
110     }
111 
GetGCStats()112     GCStats *GetGCStats()
113     {
114         ASSERT(gcStats_ != nullptr);
115         return gcStats_;
116     }
117 
GetGlobalObjectStorage()118     GlobalObjectStorage *GetGlobalObjectStorage() const
119     {
120         ASSERT(globalObjectStorage_ != nullptr);
121         return globalObjectStorage_;
122     }
123 
GetMemStats()124     MemStatsType *GetMemStats()
125     {
126         ASSERT(memStats_ != nullptr);
127         return memStats_;
128     }
129 
130 private:
131     // CC-OFFNXT(G.FUN.01-CPP) solid logic, the fix will degrade the readability and maintenance of the code
MemoryManager(InternalAllocatorPtr internalAllocator,HeapManager * heapManager,GC * gc,GCTrigger * gcTrigger,GCStats * gcStats,MemStatsType * memStats,GlobalObjectStorage * globalObjectStorage)132     explicit MemoryManager(InternalAllocatorPtr internalAllocator, HeapManager *heapManager, GC *gc,
133                            GCTrigger *gcTrigger, GCStats *gcStats, MemStatsType *memStats,
134                            GlobalObjectStorage *globalObjectStorage)
135         : internalAllocator_(internalAllocator),
136           heapManager_(heapManager),
137           gc_(gc),
138           gcTrigger_(gcTrigger),
139           gcStats_(gcStats),
140           globalObjectStorage_(globalObjectStorage),
141           memStats_(memStats)
142     {
143     }
144     ~MemoryManager();
145 
146     InternalAllocatorPtr internalAllocator_;
147     HeapManager *heapManager_;
148     GC *gc_;
149     GCTrigger *gcTrigger_;
150     GCStats *gcStats_;
151     GlobalObjectStorage *globalObjectStorage_;
152     MemStatsType *memStats_;
153 
154     friend class mem::Allocator;
155 };
156 
157 }  // namespace ark::mem
158 
159 #endif  // PANDA_MEM_MEMORY_MANAGER_H
160