Files
ark_js_runtime/ecmascript/mem/heap_region_allocator.cpp
T
lukai d23dfb2033 Add Memory Tag for Js Heap
Issue: https://gitee.com/openharmony/ark_js_runtime/issues/I4VRCY?from=project-issue
Signed-off-by: lukai <lukai25@huawei.com>

Change-Id: I3ccf5ecfc55282092c799e2b427515f037fabca4
2022-03-01 15:09:13 +08:00

86 lines
3.1 KiB
C++

/*
* Copyright (c) 2021 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/mem/heap.h"
#include "ecmascript/mem/mark_stack.h"
#include "ecmascript/mem/region.h"
#include "libpandabase/mem/pool_manager.h"
#ifdef PANDA_TARGET_UNIX
#include <sys/prctl.h>
#ifndef PR_SET_VMA
#define PR_SET_VMA 0x53564d41
#endif
#ifndef PR_SET_VMA_ANON_NAME
#define PR_SET_VMA_ANON_NAME 0
#endif
#endif // PANDA_TARGET_UNIX
namespace panda::ecmascript {
Region *HeapRegionAllocator::AllocateAlignedRegion(Space *space, size_t capacity)
{
if (capacity == 0) {
LOG_ECMA_MEM(FATAL) << "capacity must have a size bigger than 0";
UNREACHABLE();
}
auto pool = PoolManager::GetMmapMemPool()->AllocPool(capacity, panda::SpaceType::SPACE_TYPE_OBJECT,
AllocatorType::RUNSLOTS_ALLOCATOR, nullptr);
void *mapMem = pool.GetMem();
if (mapMem == nullptr) {
LOG_ECMA_MEM(FATAL) << "pool is empty " << annoMemoryUsage_.load(std::memory_order_relaxed);
UNREACHABLE();
}
#ifdef PANDA_TARGET_UNIX
prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, mapMem, pool.GetSize(), "Arkjs Heap");
#endif // PANDA_TARGET_UNIX
#if ECMASCRIPT_ENABLE_ZAP_MEM
if (memset_s(mapMem, capacity, 0, capacity) != EOK) {
LOG_ECMA(FATAL) << "memset_s failed";
UNREACHABLE();
}
#endif
IncreaseAnnoMemoryUsage(capacity);
uintptr_t mem = ToUintPtr(mapMem);
// Check that the address is 256K byte aligned
LOG_IF(AlignUp(mem, PANDA_POOL_ALIGNMENT_IN_BYTES) != mem, FATAL, RUNTIME) << "region not align by 256KB";
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
uintptr_t begin = AlignUp(mem + sizeof(Region), static_cast<size_t>(MemAlignment::MEM_ALIGN_REGION));
uintptr_t end = mem + capacity;
return new (ToVoidPtr(mem)) Region(space, space->GetHeap(), mem, begin, end,
space->GetHeap()->GetNativeAreaAllocator());
}
void HeapRegionAllocator::FreeRegion(Region *region)
{
auto size = region->GetCapacity();
DecreaseAnnoMemoryUsage(size);
#if ECMASCRIPT_ENABLE_ZAP_MEM
if (memset_s(ToVoidPtr(region->GetAllocateBase()), size, INVALID_VALUE, size) != EOK) {
LOG_ECMA(FATAL) << "memset_s failed";
UNREACHABLE();
}
#endif
PoolManager::GetMmapMemPool()->FreePool(ToVoidPtr(region->GetAllocateBase()), size);
#ifdef PANDA_TARGET_UNIX
prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, region->GetAllocateBase(), size, nullptr);
#endif // PANDA_TARGET_UNIX
}
} // namespace panda::ecmascript