/* * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "ecmascript/mem/heap_region_allocator.h" #include "ecmascript/ecma_vm.h" #include "ecmascript/js_thread.h" #include "ecmascript/mem/heap.h" #include "ecmascript/mem/mark_stack.h" #include "ecmascript/mem/mem_map_allocator.h" #include "ecmascript/mem/region.h" #include "ecmascript/mem/space-inl.h" #include "ecmascript/platform/map.h" namespace panda::ecmascript { constexpr size_t PANDA_POOL_ALIGNMENT_IN_BYTES = 256_KB; Region *HeapRegionAllocator::AllocateAlignedRegion(Space *space, size_t capacity, JSThread* thread) { if (capacity == 0) { LOG_ECMA_MEM(FATAL) << "capacity must have a size bigger than 0"; UNREACHABLE(); } RegionSpaceFlag flags = space->GetRegionFlag(); bool isRegular = (flags != RegionSpaceFlag::IN_HUGE_OBJECT_SPACE && flags != RegionSpaceFlag::IN_HUGE_MACHINE_CODE_SPACE); bool isMachineCode = (flags == RegionSpaceFlag::IN_MACHINE_CODE_SPACE || flags == RegionSpaceFlag::IN_HUGE_MACHINE_CODE_SPACE); auto pool = MemMapAllocator::GetInstance()->Allocate(thread->GetThreadId(), capacity, DEFAULT_REGION_SIZE, ToSpaceTypeName(space->GetSpaceType()), isRegular, isMachineCode); void *mapMem = pool.GetMem(); if (mapMem == nullptr) { const_cast(thread->GetEcmaVM()->GetHeap())->ThrowOutOfMemoryErrorForDefault(DEFAULT_REGION_SIZE, "HeapRegionAllocator::AllocateAlignedRegion", false); LOG_ECMA_MEM(FATAL) << "pool is empty " << annoMemoryUsage_.load(std::memory_order_relaxed); UNREACHABLE(); } #if ECMASCRIPT_ENABLE_ZAP_MEM if (memset_s(mapMem, capacity, 0, capacity) != EOK) { LOG_FULL(FATAL) << "memset_s failed"; UNREACHABLE(); } #endif IncreaseAnnoMemoryUsage(capacity); uintptr_t mem = ToUintPtr(mapMem); // Check that the address is 256K byte aligned LOG_ECMA_IF(AlignUp(mem, PANDA_POOL_ALIGNMENT_IN_BYTES) != mem, FATAL) << "region not align by 256KB"; // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) uintptr_t begin = AlignUp(mem + sizeof(Region), static_cast(MemAlignment::MEM_ALIGN_REGION)); uintptr_t end = mem + capacity; Region *region = new (ToVoidPtr(mem)) Region(thread, mem, begin, end, flags); std::atomic_thread_fence(std::memory_order_seq_cst); return region; } void HeapRegionAllocator::FreeRegion(Region *region, size_t cachedSize) { auto size = region->GetCapacity(); bool isRegular = !region->InHugeObjectSpace() && !region->InHugeMachineCodeSpace(); auto allocateBase = region->GetAllocateBase(); DecreaseAnnoMemoryUsage(size); region->Invalidate(); region->ClearMembers(); #if ECMASCRIPT_ENABLE_ZAP_MEM if (memset_s(ToVoidPtr(allocateBase), size, INVALID_VALUE, size) != EOK) { LOG_FULL(FATAL) << "memset_s failed"; UNREACHABLE(); } #endif MemMapAllocator::GetInstance()->CacheOrFree(ToVoidPtr(allocateBase), size, isRegular, cachedSize); } } // namespace panda::ecmascript