14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License. 54514f5e3Sopenharmony_ci * You may obtain a copy of the License at 64514f5e3Sopenharmony_ci * 74514f5e3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 84514f5e3Sopenharmony_ci * 94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and 134514f5e3Sopenharmony_ci * limitations under the License. 144514f5e3Sopenharmony_ci */ 154514f5e3Sopenharmony_ci 164514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_MEM_ALLOCATOR_INL_H 174514f5e3Sopenharmony_ci#define ECMASCRIPT_MEM_ALLOCATOR_INL_H 184514f5e3Sopenharmony_ci 194514f5e3Sopenharmony_ci#include <cstdlib> 204514f5e3Sopenharmony_ci#include <type_traits> 214514f5e3Sopenharmony_ci 224514f5e3Sopenharmony_ci#include "ecmascript/free_object.h" 234514f5e3Sopenharmony_ci#include "ecmascript/mem/allocator.h" 244514f5e3Sopenharmony_ci#include "ecmascript/mem/heap.h" 254514f5e3Sopenharmony_ci 264514f5e3Sopenharmony_cinamespace panda::ecmascript { 274514f5e3Sopenharmony_ciBumpPointerAllocator::BumpPointerAllocator(uintptr_t begin, uintptr_t end) : begin_(begin), top_(begin), end_(end) {} 284514f5e3Sopenharmony_ci 294514f5e3Sopenharmony_civoid BumpPointerAllocator::Reset() 304514f5e3Sopenharmony_ci{ 314514f5e3Sopenharmony_ci begin_ = 0; 324514f5e3Sopenharmony_ci top_ = 0; 334514f5e3Sopenharmony_ci end_ = 0; 344514f5e3Sopenharmony_ci} 354514f5e3Sopenharmony_ci 364514f5e3Sopenharmony_civoid BumpPointerAllocator::Reset(uintptr_t begin, uintptr_t end) 374514f5e3Sopenharmony_ci{ 384514f5e3Sopenharmony_ci begin_ = begin; 394514f5e3Sopenharmony_ci top_ = begin; 404514f5e3Sopenharmony_ci end_ = end; 414514f5e3Sopenharmony_ci#ifdef ARK_ASAN_ON 424514f5e3Sopenharmony_ci ASAN_POISON_MEMORY_REGION(reinterpret_cast<void *>(top_), (end_ - top_)); 434514f5e3Sopenharmony_ci#endif 444514f5e3Sopenharmony_ci} 454514f5e3Sopenharmony_ci 464514f5e3Sopenharmony_civoid BumpPointerAllocator::Reset(uintptr_t begin, uintptr_t end, uintptr_t top) 474514f5e3Sopenharmony_ci{ 484514f5e3Sopenharmony_ci begin_ = begin; 494514f5e3Sopenharmony_ci top_ = top; 504514f5e3Sopenharmony_ci end_ = end; 514514f5e3Sopenharmony_ci#ifdef ARK_ASAN_ON 524514f5e3Sopenharmony_ci ASAN_POISON_MEMORY_REGION(reinterpret_cast<void *>(top_), (end_ - top_)); 534514f5e3Sopenharmony_ci#endif 544514f5e3Sopenharmony_ci} 554514f5e3Sopenharmony_ci 564514f5e3Sopenharmony_civoid BumpPointerAllocator::ResetTopPointer(uintptr_t top) 574514f5e3Sopenharmony_ci{ 584514f5e3Sopenharmony_ci top_ = top; 594514f5e3Sopenharmony_ci} 604514f5e3Sopenharmony_ci 614514f5e3Sopenharmony_ciuintptr_t BumpPointerAllocator::Allocate(size_t size) 624514f5e3Sopenharmony_ci{ 634514f5e3Sopenharmony_ci ASSERT(size != 0); 644514f5e3Sopenharmony_ci // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) 654514f5e3Sopenharmony_ci if (UNLIKELY(top_ + size > end_)) { 664514f5e3Sopenharmony_ci return 0; 674514f5e3Sopenharmony_ci } 684514f5e3Sopenharmony_ci uintptr_t result = top_; 694514f5e3Sopenharmony_ci // It need to mark unpoison when object being allocated. 704514f5e3Sopenharmony_ci ASAN_UNPOISON_MEMORY_REGION(reinterpret_cast<void *>(result), size); 714514f5e3Sopenharmony_ci top_ += size; 724514f5e3Sopenharmony_ci return result; 734514f5e3Sopenharmony_ci} 744514f5e3Sopenharmony_ci 754514f5e3Sopenharmony_citemplate <typename T> 764514f5e3Sopenharmony_ciFreeListAllocator<T>::FreeListAllocator(BaseHeap *heap) : heap_(heap) 774514f5e3Sopenharmony_ci{ 784514f5e3Sopenharmony_ci freeList_ = std::make_unique<FreeObjectList<T>>(); 794514f5e3Sopenharmony_ci} 804514f5e3Sopenharmony_ci 814514f5e3Sopenharmony_citemplate <typename T> 824514f5e3Sopenharmony_civoid FreeListAllocator<T>::Initialize(Region *region) 834514f5e3Sopenharmony_ci{ 844514f5e3Sopenharmony_ci bpAllocator_.Reset(region->GetBegin(), region->GetEnd()); 854514f5e3Sopenharmony_ci} 864514f5e3Sopenharmony_ci 874514f5e3Sopenharmony_citemplate <typename T> 884514f5e3Sopenharmony_civoid FreeListAllocator<T>::Reset(BaseHeap *heap) 894514f5e3Sopenharmony_ci{ 904514f5e3Sopenharmony_ci heap_ = heap; 914514f5e3Sopenharmony_ci freeList_ = std::make_unique<FreeObjectList<T>>(); 924514f5e3Sopenharmony_ci FreeBumpPoint(); 934514f5e3Sopenharmony_ci} 944514f5e3Sopenharmony_ci 954514f5e3Sopenharmony_citemplate <typename T> 964514f5e3Sopenharmony_civoid FreeListAllocator<T>::AddFree(Region *region) 974514f5e3Sopenharmony_ci{ 984514f5e3Sopenharmony_ci auto begin = region->GetBegin(); 994514f5e3Sopenharmony_ci auto end = region->GetEnd(); 1004514f5e3Sopenharmony_ci FreeBumpPoint(); 1014514f5e3Sopenharmony_ci bpAllocator_.Reset(begin, end); 1024514f5e3Sopenharmony_ci} 1034514f5e3Sopenharmony_ci 1044514f5e3Sopenharmony_citemplate <typename T> 1054514f5e3Sopenharmony_ciuintptr_t FreeListAllocator<T>::Allocate(size_t size) 1064514f5e3Sopenharmony_ci{ 1074514f5e3Sopenharmony_ci auto ret = bpAllocator_.Allocate(size); 1084514f5e3Sopenharmony_ci if (LIKELY(ret != 0)) { 1094514f5e3Sopenharmony_ci allocationSizeAccumulator_ += size; 1104514f5e3Sopenharmony_ci return ret; 1114514f5e3Sopenharmony_ci } 1124514f5e3Sopenharmony_ci T *object = freeList_->Allocate(size); 1134514f5e3Sopenharmony_ci if (object != nullptr) { 1144514f5e3Sopenharmony_ci ret = Allocate(object, size); 1154514f5e3Sopenharmony_ci } 1164514f5e3Sopenharmony_ci return ret; 1174514f5e3Sopenharmony_ci} 1184514f5e3Sopenharmony_ci 1194514f5e3Sopenharmony_ci 1204514f5e3Sopenharmony_citemplate <typename T> 1214514f5e3Sopenharmony_ciuintptr_t FreeListAllocator<T>::Allocate(T *object, size_t size) 1224514f5e3Sopenharmony_ci{ 1234514f5e3Sopenharmony_ci uintptr_t begin = object->GetBegin(); 1244514f5e3Sopenharmony_ci uintptr_t end = object->GetEnd(); 1254514f5e3Sopenharmony_ci uintptr_t remainSize = end - begin - size; 1264514f5e3Sopenharmony_ci ASSERT(remainSize >= 0); 1274514f5e3Sopenharmony_ci if constexpr (std::is_same<T, MemDesc>::value) { 1284514f5e3Sopenharmony_ci memDescPool_->ReturnDescToPool(object); 1294514f5e3Sopenharmony_ci } 1304514f5e3Sopenharmony_ci // Keep a longest freeObject between bump-pointer and free object that just allocated 1314514f5e3Sopenharmony_ci allocationSizeAccumulator_ += size; 1324514f5e3Sopenharmony_ci if (remainSize <= bpAllocator_.Available()) { 1334514f5e3Sopenharmony_ci Free(begin + size, remainSize); 1344514f5e3Sopenharmony_ci return begin; 1354514f5e3Sopenharmony_ci } else { 1364514f5e3Sopenharmony_ci FreeBumpPoint(); 1374514f5e3Sopenharmony_ci bpAllocator_.Reset(begin, end); 1384514f5e3Sopenharmony_ci auto ret = bpAllocator_.Allocate(size); 1394514f5e3Sopenharmony_ci return ret; 1404514f5e3Sopenharmony_ci } 1414514f5e3Sopenharmony_ci} 1424514f5e3Sopenharmony_ci 1434514f5e3Sopenharmony_citemplate <typename T> 1444514f5e3Sopenharmony_civoid FreeListAllocator<T>::FreeBumpPoint() 1454514f5e3Sopenharmony_ci{ 1464514f5e3Sopenharmony_ci auto begin = bpAllocator_.GetTop(); 1474514f5e3Sopenharmony_ci auto size = bpAllocator_.Available(); 1484514f5e3Sopenharmony_ci bpAllocator_.Reset(); 1494514f5e3Sopenharmony_ci Free(begin, size, true); 1504514f5e3Sopenharmony_ci} 1514514f5e3Sopenharmony_ci 1524514f5e3Sopenharmony_citemplate <typename T> 1534514f5e3Sopenharmony_civoid FreeListAllocator<T>::FillBumpPointer() 1544514f5e3Sopenharmony_ci{ 1554514f5e3Sopenharmony_ci if constexpr (std::is_same<T, MemDesc>::value) { 1564514f5e3Sopenharmony_ci return; 1574514f5e3Sopenharmony_ci } 1584514f5e3Sopenharmony_ci size_t size = bpAllocator_.Available(); 1594514f5e3Sopenharmony_ci if (size != 0) { 1604514f5e3Sopenharmony_ci FreeObject::FillFreeObject(heap_, bpAllocator_.GetTop(), size); 1614514f5e3Sopenharmony_ci } 1624514f5e3Sopenharmony_ci} 1634514f5e3Sopenharmony_ci 1644514f5e3Sopenharmony_citemplate <typename T> 1654514f5e3Sopenharmony_civoid FreeListAllocator<T>::ResetBumpPointer(uintptr_t begin, uintptr_t end, uintptr_t top) 1664514f5e3Sopenharmony_ci{ 1674514f5e3Sopenharmony_ci bpAllocator_.Reset(begin, end, top); 1684514f5e3Sopenharmony_ci} 1694514f5e3Sopenharmony_ci 1704514f5e3Sopenharmony_citemplate <typename T> 1714514f5e3Sopenharmony_civoid FreeListAllocator<T>::ResetTopPointer(uintptr_t top) 1724514f5e3Sopenharmony_ci{ 1734514f5e3Sopenharmony_ci bpAllocator_.ResetTopPointer(top); 1744514f5e3Sopenharmony_ci} 1754514f5e3Sopenharmony_ci 1764514f5e3Sopenharmony_ci// The object will be marked with poison after being put into the freelist when is_asan is true. 1774514f5e3Sopenharmony_citemplate <typename T> 1784514f5e3Sopenharmony_civoid FreeListAllocator<T>::Free(uintptr_t begin, size_t size, bool isAdd) 1794514f5e3Sopenharmony_ci{ 1804514f5e3Sopenharmony_ci ASSERT(size >= 0); 1814514f5e3Sopenharmony_ci if (size != 0) { 1824514f5e3Sopenharmony_ci if constexpr (!std::is_same<T, MemDesc>::value) { 1834514f5e3Sopenharmony_ci ASSERT(heap_ != nullptr); 1844514f5e3Sopenharmony_ci T::FillFreeObject(heap_, begin, size); 1854514f5e3Sopenharmony_ci } 1864514f5e3Sopenharmony_ci 1874514f5e3Sopenharmony_ci ASAN_UNPOISON_MEMORY_REGION(reinterpret_cast<void *>(begin), size); 1884514f5e3Sopenharmony_ci freeList_->Free(begin, size, isAdd); 1894514f5e3Sopenharmony_ci#ifdef ARK_ASAN_ON 1904514f5e3Sopenharmony_ci ASAN_POISON_MEMORY_REGION(reinterpret_cast<void *>(begin), size); 1914514f5e3Sopenharmony_ci#endif 1924514f5e3Sopenharmony_ci } 1934514f5e3Sopenharmony_ci} 1944514f5e3Sopenharmony_ci 1954514f5e3Sopenharmony_citemplate <typename T> 1964514f5e3Sopenharmony_ciuintptr_t FreeListAllocator<T>::LookupSuitableFreeObject(size_t size) 1974514f5e3Sopenharmony_ci{ 1984514f5e3Sopenharmony_ci auto freeObject = freeList_->LookupSuitableFreeObject(size); 1994514f5e3Sopenharmony_ci if (freeObject != nullptr) { 2004514f5e3Sopenharmony_ci return freeObject->GetBegin(); 2014514f5e3Sopenharmony_ci } 2024514f5e3Sopenharmony_ci return 0; 2034514f5e3Sopenharmony_ci} 2044514f5e3Sopenharmony_ci 2054514f5e3Sopenharmony_citemplate <typename T> 2064514f5e3Sopenharmony_civoid FreeListAllocator<T>::RebuildFreeList() 2074514f5e3Sopenharmony_ci{ 2084514f5e3Sopenharmony_ci bpAllocator_.Reset(); 2094514f5e3Sopenharmony_ci freeList_->Rebuild(); 2104514f5e3Sopenharmony_ci} 2114514f5e3Sopenharmony_ci 2124514f5e3Sopenharmony_citemplate <typename T> 2134514f5e3Sopenharmony_ciinline void FreeListAllocator<T>::CollectFreeObjectSet(Region *region) 2144514f5e3Sopenharmony_ci{ 2154514f5e3Sopenharmony_ci region->EnumerateFreeObjectSets([&](FreeObjectSet<T> *set) { 2164514f5e3Sopenharmony_ci if (set == nullptr || set->Empty()) { 2174514f5e3Sopenharmony_ci return; 2184514f5e3Sopenharmony_ci } 2194514f5e3Sopenharmony_ci freeList_->AddSet(set); 2204514f5e3Sopenharmony_ci }); 2214514f5e3Sopenharmony_ci freeList_->IncreaseWastedSize(region->GetWastedSize()); 2224514f5e3Sopenharmony_ci} 2234514f5e3Sopenharmony_ci 2244514f5e3Sopenharmony_citemplate <typename T> 2254514f5e3Sopenharmony_ciinline bool FreeListAllocator<T>::MatchFreeObjectSet(Region *region, size_t size) 2264514f5e3Sopenharmony_ci{ 2274514f5e3Sopenharmony_ci bool ret = false; 2284514f5e3Sopenharmony_ci region->REnumerateFreeObjectSets([&](FreeObjectSet<T> *set) { 2294514f5e3Sopenharmony_ci if (set == nullptr || set->Empty()) { 2304514f5e3Sopenharmony_ci return true; 2314514f5e3Sopenharmony_ci } 2324514f5e3Sopenharmony_ci ret = freeList_->MatchFreeObjectInSet(set, size); 2334514f5e3Sopenharmony_ci return false; 2344514f5e3Sopenharmony_ci }); 2354514f5e3Sopenharmony_ci return ret; 2364514f5e3Sopenharmony_ci} 2374514f5e3Sopenharmony_ci 2384514f5e3Sopenharmony_citemplate <typename T> 2394514f5e3Sopenharmony_ciinline void FreeListAllocator<T>::DetachFreeObjectSet(Region *region) 2404514f5e3Sopenharmony_ci{ 2414514f5e3Sopenharmony_ci region->EnumerateFreeObjectSets([&](FreeObjectSet<T> *set) { 2424514f5e3Sopenharmony_ci if (set == nullptr || set->Empty()) { 2434514f5e3Sopenharmony_ci return; 2444514f5e3Sopenharmony_ci } 2454514f5e3Sopenharmony_ci freeList_->RemoveSet(set); 2464514f5e3Sopenharmony_ci }); 2474514f5e3Sopenharmony_ci freeList_->DecreaseWastedSize(region->GetWastedSize()); 2484514f5e3Sopenharmony_ci} 2494514f5e3Sopenharmony_ci 2504514f5e3Sopenharmony_citemplate <typename T> 2514514f5e3Sopenharmony_cisize_t FreeListAllocator<T>::GetAvailableSize() const 2524514f5e3Sopenharmony_ci{ 2534514f5e3Sopenharmony_ci return freeList_->GetFreeObjectSize() + bpAllocator_.Available(); 2544514f5e3Sopenharmony_ci} 2554514f5e3Sopenharmony_ci 2564514f5e3Sopenharmony_citemplate <typename T> 2574514f5e3Sopenharmony_cisize_t FreeListAllocator<T>::GetWastedSize() const 2584514f5e3Sopenharmony_ci{ 2594514f5e3Sopenharmony_ci return freeList_->GetWastedSize(); 2604514f5e3Sopenharmony_ci} 2614514f5e3Sopenharmony_ci} // namespace panda::ecmascript 2624514f5e3Sopenharmony_ci#endif // ECMASCRIPT_MEM_ALLOCATOR_INL_H 263