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