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 <libpandabase/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 isObjectFinalizebleFunc; 67 HeapManager::RegisterFinalizeReferenceFunc registerFinalizeReferenceFunc; 68 uint32_t maxGlobalRefSize; 69 bool isGlobalReferenceSizeCheckEnabled; 70 bool isSingleThread; 71 bool isUseTlabForAllocations; 72 bool isStartAsZygote; 73 }; 74 75 static MemoryManager *Create(const LanguageContext &ctx, InternalAllocatorPtr internalAllocator, GCType gcType, 76 const GCSettings &gcSettings, const GCTriggerConfig &gcTriggerConfig, 77 const HeapOptions &heapOptions); 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 PANDA_PUBLIC_API void StartGC(); 88 void StopGC(); 89 90 void Finalize(); 91 GetHeapManager()92 HeapManager *GetHeapManager() 93 { 94 ASSERT(heapManager_ != nullptr); 95 return heapManager_; 96 } 97 GetGC()98 GC *GetGC() const 99 { 100 ASSERT(gc_ != nullptr); 101 return gc_; 102 } 103 GetGCTrigger()104 GCTrigger *GetGCTrigger() 105 { 106 ASSERT(gcTrigger_ != nullptr); 107 return gcTrigger_; 108 } 109 GetGCStats()110 GCStats *GetGCStats() 111 { 112 ASSERT(gcStats_ != nullptr); 113 return gcStats_; 114 } 115 GetGlobalObjectStorage()116 GlobalObjectStorage *GetGlobalObjectStorage() const 117 { 118 ASSERT(globalObjectStorage_ != nullptr); 119 return globalObjectStorage_; 120 } 121 GetMemStats()122 MemStatsType *GetMemStats() 123 { 124 ASSERT(memStats_ != nullptr); 125 return memStats_; 126 } 127 128 private: MemoryManager(InternalAllocatorPtr internalAllocator,HeapManager * heapManager,GC * gc,GCTrigger * gcTrigger,GCStats * gcStats,MemStatsType * memStats,GlobalObjectStorage * globalObjectStorage)129 explicit MemoryManager(InternalAllocatorPtr internalAllocator, HeapManager *heapManager, GC *gc, 130 GCTrigger *gcTrigger, GCStats *gcStats, MemStatsType *memStats, 131 GlobalObjectStorage *globalObjectStorage) 132 : internalAllocator_(internalAllocator), 133 heapManager_(heapManager), 134 gc_(gc), 135 gcTrigger_(gcTrigger), 136 gcStats_(gcStats), 137 globalObjectStorage_(globalObjectStorage), 138 memStats_(memStats) 139 { 140 } 141 ~MemoryManager(); 142 143 InternalAllocatorPtr internalAllocator_; 144 HeapManager *heapManager_; 145 GC *gc_; 146 GCTrigger *gcTrigger_; 147 GCStats *gcStats_; 148 GlobalObjectStorage *globalObjectStorage_; 149 MemStatsType *memStats_; 150 151 friend class mem::Allocator; 152 }; 153 154 } // namespace panda::mem 155 156 #endif // PANDA_MEM_MEMORY_MANAGER_H 157