• 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_MEMORY_MANAGER_H_
17 #define PANDA_RUNTIME_MEM_MEMORY_MANAGER_H_
18 
19 #include <macros.h>
20 #include <mem/mem.h>
21 #include <runtime/mem/gc/gc_types.h>
22 #include <runtime/mem/heap_manager.h>
23 
24 namespace panda {
25 
26 class RuntimeOptions;
27 }  // namespace panda
28 
29 namespace panda::mem {
30 
31 class GC;
32 class GCStats;
33 class GCTrigger;
34 struct GCSettings;
35 class GCTriggerConfig;
36 class Allocator;
37 
38 /*
39  * Relations between VMs, MemoryManager and Runtime:
40  *
41  * +-----------------------------------------------+
42  * |                                               |
43  * |                  Runtime                      |
44  * |                                               |
45  * |  +----------+  +----------+     +----------+  |
46  * |  |          |  |          |     |          |  |
47  * |  |   VM_0   |  |   VM_1   |     |   VM_N   |  |
48  * |  |          |  |          |     |          |  |
49  * |  |          |  |          | ... |          |  |
50  * |  |  +----+  |  |  +----+  |     |  +----+  |  |
51  * |  |  |MM_0|  |  |  |MM_1|  |     |  |MM_N|  |  |
52  * |  |  +----+  |  |  +----+  |     |  +----+  |  |
53  * |  +----------+  +----------+     +----------+  |
54  * |         \           |            /            |
55  * |          \          |           /             |
56  * |           +--------------------+              |
57  * |           | Internal Allocator |              |
58  * |           +--------------------+              |
59  * +-----------------------------------------------+
60  */
61 
62 /**
63  * A class that encapsulates componentes for working with memory.
64  * Each VM is allocated its own instance.
65  */
66 class MemoryManager {
67 public:
68     struct HeapOptions {
69         HeapManager::IsObjectFinalizebleFunc is_object_finalizeble_func;
70         HeapManager::RegisterFinalizeReferenceFunc register_finalize_reference_func;
71         uint32_t max_global_ref_size;
72         bool is_global_reference_size_check_enabled;
73         bool is_single_thread;
74         bool is_use_tlab_for_allocations;
75         bool is_start_as_zygote;
76     };
77 
78     static MemoryManager *Create(LanguageContext ctx, InternalAllocatorPtr internal_allocator, GCType gc_type,
79                                  const GCSettings &gc_settings, const GCTriggerConfig &gc_trigger_config,
80                                  const HeapOptions &heap_options);
81     static void Destroy(MemoryManager *mm);
82 
83     NO_COPY_SEMANTIC(MemoryManager);
84     NO_MOVE_SEMANTIC(MemoryManager);
85 
86     void PreStartup() const;
87     void PreZygoteFork() const;
88     void PostZygoteFork() const;
89     void InitializeGC() const;
90     void StartGC() const;
91     void StopGC() const;
92 
93     void Finalize();
94 
GetHeapManager()95     HeapManager *GetHeapManager() const
96     {
97         ASSERT(heap_manager_ != nullptr);
98         return heap_manager_;
99     }
100 
GetGC()101     GC *GetGC() const
102     {
103         ASSERT(gc_ != nullptr);
104         return gc_;
105     }
106 
GetGCTrigger()107     GCTrigger *GetGCTrigger() const
108     {
109         ASSERT(gc_trigger_ != nullptr);
110         return gc_trigger_;
111     }
112 
GetGCStats()113     GCStats *GetGCStats() const
114     {
115         ASSERT(gc_stats_ != nullptr);
116         return gc_stats_;
117     }
118 
GetGlobalObjectStorage()119     GlobalObjectStorage *GetGlobalObjectStorage() const
120     {
121         ASSERT(global_object_storage_ != nullptr);
122         return global_object_storage_;
123     }
124 
GetMemStats()125     MemStatsType *GetMemStats() const
126     {
127         ASSERT(mem_stats_ != nullptr);
128         return mem_stats_;
129     }
130 
131 private:
MemoryManager(InternalAllocatorPtr internal_allocator,HeapManager * heap_manager,GC * gc,GCTrigger * gc_trigger,GCStats * gc_stats,MemStatsType * mem_stats,GlobalObjectStorage * global_object_storage)132     explicit MemoryManager(InternalAllocatorPtr internal_allocator, HeapManager *heap_manager, GC *gc,
133                            GCTrigger *gc_trigger, GCStats *gc_stats, MemStatsType *mem_stats,
134                            GlobalObjectStorage *global_object_storage)
135         : internal_allocator_(internal_allocator),
136           heap_manager_(heap_manager),
137           gc_(gc),
138           gc_trigger_(gc_trigger),
139           gc_stats_(gc_stats),
140           global_object_storage_(global_object_storage),
141           mem_stats_(mem_stats)
142     {
143     }
144     ~MemoryManager();
145 
146     InternalAllocatorPtr internal_allocator_;
147     HeapManager *heap_manager_;
148     GC *gc_;
149     GCTrigger *gc_trigger_;
150     GCStats *gc_stats_;
151     GlobalObjectStorage *global_object_storage_;
152     MemStatsType *mem_stats_;
153 
154     friend class mem::Allocator;
155 };
156 
157 }  // namespace panda::mem
158 
159 #endif  // PANDA_RUNTIME_MEM_MEMORY_MANAGER_H_
160