diff --git a/common_components/heap/allocator/allocator.cpp b/common_components/heap/allocator/allocator.cpp index 9791d8a1049970b3ddca4c9b3dc164443d4d1c9f..add5096dfe36b441df5b7f9e550c3efbd3396617 100755 --- a/common_components/heap/allocator/allocator.cpp +++ b/common_components/heap/allocator/allocator.cpp @@ -17,6 +17,7 @@ #include #include +#include "base/common.h" #include "common_components/base/immortal_wrapper.h" #include "common_components/common/base_object.h" #include "common_components/heap/allocator/regional_heap.h" @@ -26,12 +27,23 @@ namespace common { using namespace std; Allocator::Allocator() { - allocBufferManager_ = new (std::nothrow) AllocBufferManager(); - LOGF_CHECK(allocBufferManager_ != nullptr) << "new alloc buffer manager failed"; asyncAllocationInitSwitch_ = InitAyncAllocation(); isAsyncAllocationEnable_.store(asyncAllocationInitSwitch_); } +void Allocator::Init(const RuntimeParam ¶m) +{ + allocBufferManager_ = new (std::nothrow) AllocBufferManager(); + LOGF_CHECK(allocBufferManager_ != nullptr) << "new alloc buffer manager failed"; +} + +void Allocator::Fini() +{ + DCHECK_CC(allocBufferManager_ != nullptr); + delete allocBufferManager_; + allocBufferManager_ = nullptr; +} + bool Allocator::InitAyncAllocation() { auto enableAsyncAllocation = std::getenv("arkEnableAsyncAllocation"); @@ -78,10 +90,9 @@ PagePool& PagePool::Instance() noexcept return *instance; } -Allocator* Allocator::CreateAllocator() +Allocator& Allocator::GetAllocator() { - RegionalHeap* heapSpace = new (std::nothrow) RegionalHeap(); - LOGF_CHECK(heapSpace != nullptr) << "New RegionalHeap failed"; - return heapSpace; + static ImmortalWrapper instance; + return reinterpret_cast(*instance); } } // namespace common diff --git a/common_components/heap/allocator/allocator.h b/common_components/heap/allocator/allocator.h index 841203ea95189886cbe6b57555fbbf032a2afed2..15128717f4066007711811b52e03e51d6ab75150 100755 --- a/common_components/heap/allocator/allocator.h +++ b/common_components/heap/allocator/allocator.h @@ -26,7 +26,7 @@ public: static constexpr size_t ALLOC_ALIGN = 8; static constexpr size_t HEADER_SIZE = 0; // no header for arkcommon object - static Allocator* CreateAllocator(); + static Allocator& GetAllocator(); virtual HeapAddress Allocate(size_t size, AllocType allocType) = 0; virtual HeapAddress AllocateNoGC(size_t size, AllocType allocType) = 0; @@ -59,7 +59,9 @@ public: virtual ~Allocator() {} Allocator(); - virtual void Init(const RuntimeParam& param) = 0; + virtual void Init(const RuntimeParam& param); + virtual void Fini(); + virtual size_t GetMaxCapacity() const = 0; virtual size_t GetCurrentCapacity() const = 0; virtual size_t GetUsedPageSize() const = 0; diff --git a/common_components/heap/allocator/regional_heap.cpp b/common_components/heap/allocator/regional_heap.cpp index 7ccb1bc526ffefd2804ff9d6244bee40ce198f13..1c22feffcc74d2c68189d24c46ed6cb50aa08cb8 100755 --- a/common_components/heap/allocator/regional_heap.cpp +++ b/common_components/heap/allocator/regional_heap.cpp @@ -229,6 +229,9 @@ void RegionalHeap::CopyRegion(RegionDesc* region) void RegionalHeap::Init(const RuntimeParam& param) { + Allocator::Init(param); + nonMovableSpace_.Init(); + MemoryMap::Option opt = MemoryMap::DEFAULT_OPTIONS; opt.tag = "region_heap"; size_t heapSize = param.heapParam.heapSize * KB; @@ -273,6 +276,17 @@ void RegionalHeap::Init(const RuntimeParam& param) Heap::OnHeapExtended(reservedEnd_); } +void RegionalHeap::Fini() +{ + Allocator::Fini(); + nonMovableSpace_.Fini(); + +#if defined(COMMON_SANITIZER_SUPPORT) + Sanitizer::OnHeapDeallocated(map->GetBaseAddr(), map->GetMappedSize()); +#endif + MemoryMap::DestroyMemoryMap(map_); +} + AllocationBuffer* AllocationBuffer::GetOrCreateAllocBuffer() { auto* buffer = AllocationBuffer::GetAllocBuffer(); diff --git a/common_components/heap/allocator/regional_heap.h b/common_components/heap/allocator/regional_heap.h index 2bc0cc98d5c982ac558a5541bbd8e73f045aa1d8..c709bc7ec4bd625ed40f84344e6306306efc570c 100755 --- a/common_components/heap/allocator/regional_heap.h +++ b/common_components/heap/allocator/regional_heap.h @@ -68,19 +68,8 @@ public: appSpawnSpace_(regionManager_), rawpointerSpace_(regionManager_), readonlySpace_(regionManager_) {} - NO_INLINE_CC virtual ~RegionalHeap() - { - if (allocBufferManager_ != nullptr) { - delete allocBufferManager_; - allocBufferManager_ = nullptr; - } -#if defined(COMMON_SANITIZER_SUPPORT) - Sanitizer::OnHeapDeallocated(map->GetBaseAddr(), map->GetMappedSize()); -#endif - MemoryMap::DestroyMemoryMap(map_); - } - void Init(const RuntimeParam ¶m) override; + void Fini() override; template RegionDesc* AllocateThreadLocalRegion(bool expectPhysicalMem = false); diff --git a/common_components/heap/heap.cpp b/common_components/heap/heap.cpp index 29d5aca830584c187ff0b590160da4550ed546fe..838643b0b1ff9d1fae8b789303c7879c317784ff 100644 --- a/common_components/heap/heap.cpp +++ b/common_components/heap/heap.cpp @@ -47,7 +47,7 @@ HeapAddress Heap::heapCurrentEnd_ = 0; class HeapImpl : public Heap { public: HeapImpl() - : theSpace_(Allocator::CreateAllocator()), collectorResources_(collectorProxy_), + : theSpace_(&Allocator::GetAllocator()), collectorResources_(collectorProxy_), collectorProxy_(*theSpace_, collectorResources_), stwBarrier_(collectorProxy_), idleBarrier_(collectorProxy_), enumBarrier_(collectorProxy_), markingBarrier_(collectorProxy_), remarkBarrier_(collectorProxy_), postMarkingBarrier_(collectorProxy_), preforwardBarrier_(collectorProxy_), @@ -187,10 +187,7 @@ bool HeapImpl::ForEachObject(const std::function& visitor, bo void HeapImpl::Init(const RuntimeParam& param) { - if (theSpace_ == nullptr) { - // Hack impl, since HeapImpl is Immortal, this may happen in multi UT case - new (this) HeapImpl(); - } + DCHECK_CC(theSpace_ != nullptr); theSpace_->Init(param); Heap::GetHeap().EnableGC(param.gcParam.enableGC); collectorProxy_.Init(param); @@ -202,10 +199,7 @@ void HeapImpl::Fini() { collectorResources_.Fini(); collectorProxy_.Fini(); - if (theSpace_ != nullptr) { - delete theSpace_; - theSpace_ = nullptr; - } + theSpace_->Fini(); } void HeapImpl::StartRuntimeThreads() diff --git a/common_components/heap/space/nonmovable_space.h b/common_components/heap/space/nonmovable_space.h index 6cf267776dfaf2aa1e4bcf143c36cb6008d70e40..43cb1eea251bbc82e0e94e00c8180d1ea80b0a50 100644 --- a/common_components/heap/space/nonmovable_space.h +++ b/common_components/heap/space/nonmovable_space.h @@ -30,14 +30,17 @@ public: NonMovableSpace(RegionManager& regionManager) : RegionalSpace(regionManager), polySizeRegionList_("mixed size non-movable regions"), - recentPolySizeRegionList_("recent mixed size non-movable regions") { + recentPolySizeRegionList_("recent mixed size non-movable regions") { } + + void Init() + { for (size_t i = 0; i < NONMOVABLE_OBJECT_SIZE_COUNT; i++) { recentMonoSizeRegionList_[i] = new RegionList("recent one size non-movable regions"); monoSizeRegionList_[i] = new RegionList("one size non-movable regions"); } } - ~NonMovableSpace() + void Fini() { for (size_t i = 0; i < NONMOVABLE_OBJECT_SIZE_COUNT; i++) { if (recentMonoSizeRegionList_[i] != nullptr) {