• 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     static MemoryManager *Create(const LanguageContext &ctx, InternalAllocatorPtr internalAllocator, GCType gcType,
77                                  const GCSettings &gcSettings, const GCTriggerConfig &gcTriggerConfig,
78                                  const HeapOptions &heapOptions);
79     static void Destroy(MemoryManager *mm);
80 
81     NO_COPY_SEMANTIC(MemoryManager);
82     NO_MOVE_SEMANTIC(MemoryManager);
83 
84     void PreStartup();
85     void PreZygoteFork();
86     void PostZygoteFork();
87     void InitializeGC(PandaVM *vm);
88     PANDA_PUBLIC_API void StartGC();
89     void StopGC();
90 
91     void Finalize();
92 
GetHeapManager()93     HeapManager *GetHeapManager()
94     {
95         ASSERT(heapManager_ != nullptr);
96         return heapManager_;
97     }
98 
GetGC()99     GC *GetGC() const
100     {
101         ASSERT(gc_ != nullptr);
102         return gc_;
103     }
104 
GetGCTrigger()105     GCTrigger *GetGCTrigger()
106     {
107         ASSERT(gcTrigger_ != nullptr);
108         return gcTrigger_;
109     }
110 
GetGCStats()111     GCStats *GetGCStats()
112     {
113         ASSERT(gcStats_ != nullptr);
114         return gcStats_;
115     }
116 
GetGlobalObjectStorage()117     GlobalObjectStorage *GetGlobalObjectStorage() const
118     {
119         ASSERT(globalObjectStorage_ != nullptr);
120         return globalObjectStorage_;
121     }
122 
GetMemStats()123     MemStatsType *GetMemStats()
124     {
125         ASSERT(memStats_ != nullptr);
126         return memStats_;
127     }
128 
129 private:
MemoryManager(InternalAllocatorPtr internalAllocator,HeapManager * heapManager,GC * gc,GCTrigger * gcTrigger,GCStats * gcStats,MemStatsType * memStats,GlobalObjectStorage * globalObjectStorage)130     explicit MemoryManager(InternalAllocatorPtr internalAllocator, HeapManager *heapManager, GC *gc,
131                            GCTrigger *gcTrigger, GCStats *gcStats, MemStatsType *memStats,
132                            GlobalObjectStorage *globalObjectStorage)
133         : internalAllocator_(internalAllocator),
134           heapManager_(heapManager),
135           gc_(gc),
136           gcTrigger_(gcTrigger),
137           gcStats_(gcStats),
138           globalObjectStorage_(globalObjectStorage),
139           memStats_(memStats)
140     {
141     }
142     ~MemoryManager();
143 
144     InternalAllocatorPtr internalAllocator_;
145     HeapManager *heapManager_;
146     GC *gc_;
147     GCTrigger *gcTrigger_;
148     GCStats *gcStats_;
149     GlobalObjectStorage *globalObjectStorage_;
150     MemStatsType *memStats_;
151 
152     friend class mem::Allocator;
153 };
154 
155 }  // namespace ark::mem
156 
157 #endif  // PANDA_MEM_MEMORY_MANAGER_H
158