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_H 174514f5e3Sopenharmony_ci#define ECMASCRIPT_MEM_ALLOCATOR_H 184514f5e3Sopenharmony_ci 194514f5e3Sopenharmony_ci#include <memory> 204514f5e3Sopenharmony_ci 214514f5e3Sopenharmony_ci#include "ecmascript/mem/free_object_list.h" 224514f5e3Sopenharmony_ci#include "ecmascript/mem/mem.h" 234514f5e3Sopenharmony_ci 244514f5e3Sopenharmony_cinamespace panda::ecmascript { 254514f5e3Sopenharmony_ciclass Region; 264514f5e3Sopenharmony_ciclass BaseHeap; 274514f5e3Sopenharmony_ciclass JitFort; 284514f5e3Sopenharmony_ci 294514f5e3Sopenharmony_ciclass Allocator { 304514f5e3Sopenharmony_cipublic: 314514f5e3Sopenharmony_ci Allocator() = default; 324514f5e3Sopenharmony_ci virtual ~Allocator() = default; 334514f5e3Sopenharmony_ci NO_COPY_SEMANTIC(Allocator); 344514f5e3Sopenharmony_ci NO_MOVE_SEMANTIC(Allocator); 354514f5e3Sopenharmony_ci}; 364514f5e3Sopenharmony_ci 374514f5e3Sopenharmony_ciclass BumpPointerAllocator : public Allocator { 384514f5e3Sopenharmony_cipublic: 394514f5e3Sopenharmony_ci BumpPointerAllocator() = default; 404514f5e3Sopenharmony_ci ~BumpPointerAllocator() override = default; 414514f5e3Sopenharmony_ci NO_COPY_SEMANTIC(BumpPointerAllocator); 424514f5e3Sopenharmony_ci NO_MOVE_SEMANTIC(BumpPointerAllocator); 434514f5e3Sopenharmony_ci 444514f5e3Sopenharmony_ci inline BumpPointerAllocator(uintptr_t begin, uintptr_t end); 454514f5e3Sopenharmony_ci 464514f5e3Sopenharmony_ci inline void Reset(); 474514f5e3Sopenharmony_ci inline void Reset(uintptr_t begin, uintptr_t end); 484514f5e3Sopenharmony_ci inline void Reset(uintptr_t begin, uintptr_t end, uintptr_t top); 494514f5e3Sopenharmony_ci inline void ResetTopPointer(uintptr_t top); 504514f5e3Sopenharmony_ci inline uintptr_t Allocate(size_t size); 514514f5e3Sopenharmony_ci 524514f5e3Sopenharmony_ci uintptr_t GetTop() const 534514f5e3Sopenharmony_ci { 544514f5e3Sopenharmony_ci return top_; 554514f5e3Sopenharmony_ci } 564514f5e3Sopenharmony_ci 574514f5e3Sopenharmony_ci uintptr_t GetEnd() const 584514f5e3Sopenharmony_ci { 594514f5e3Sopenharmony_ci return end_; 604514f5e3Sopenharmony_ci } 614514f5e3Sopenharmony_ci 624514f5e3Sopenharmony_ci const uintptr_t *GetTopAddress() 634514f5e3Sopenharmony_ci { 644514f5e3Sopenharmony_ci return &top_; 654514f5e3Sopenharmony_ci } 664514f5e3Sopenharmony_ci 674514f5e3Sopenharmony_ci const uintptr_t *GetEndAddress() 684514f5e3Sopenharmony_ci { 694514f5e3Sopenharmony_ci return &end_; 704514f5e3Sopenharmony_ci } 714514f5e3Sopenharmony_ci 724514f5e3Sopenharmony_ci size_t Available() const 734514f5e3Sopenharmony_ci { 744514f5e3Sopenharmony_ci return (end_ - top_); 754514f5e3Sopenharmony_ci } 764514f5e3Sopenharmony_ci 774514f5e3Sopenharmony_ciprivate: 784514f5e3Sopenharmony_ci uintptr_t begin_ {0}; 794514f5e3Sopenharmony_ci uintptr_t top_ {0}; 804514f5e3Sopenharmony_ci uintptr_t end_ {0}; 814514f5e3Sopenharmony_ci}; 824514f5e3Sopenharmony_ci 834514f5e3Sopenharmony_citemplate <typename T> 844514f5e3Sopenharmony_ciclass FreeListAllocator : public Allocator { 854514f5e3Sopenharmony_cipublic: 864514f5e3Sopenharmony_ci FreeListAllocator() = delete; 874514f5e3Sopenharmony_ci ~FreeListAllocator() override = default; 884514f5e3Sopenharmony_ci 894514f5e3Sopenharmony_ci NO_COPY_SEMANTIC(FreeListAllocator); 904514f5e3Sopenharmony_ci NO_MOVE_SEMANTIC(FreeListAllocator); 914514f5e3Sopenharmony_ci 924514f5e3Sopenharmony_ci inline explicit FreeListAllocator(BaseHeap *heap); 934514f5e3Sopenharmony_ci inline explicit FreeListAllocator(BaseHeap *heap, MemDescPool *pool, JitFort *fort); 944514f5e3Sopenharmony_ci inline void Initialize(Region *region); 954514f5e3Sopenharmony_ci 964514f5e3Sopenharmony_ci inline void Reset(BaseHeap *heap); 974514f5e3Sopenharmony_ci 984514f5e3Sopenharmony_ci inline uintptr_t Allocate(size_t size); 994514f5e3Sopenharmony_ci inline void AddFree(Region *region); 1004514f5e3Sopenharmony_ci inline uintptr_t LookupSuitableFreeObject(size_t size); 1014514f5e3Sopenharmony_ci 1024514f5e3Sopenharmony_ci inline void RebuildFreeList(); 1034514f5e3Sopenharmony_ci 1044514f5e3Sopenharmony_ci inline bool MatchFreeObjectSet(Region *region, size_t size); 1054514f5e3Sopenharmony_ci inline void CollectFreeObjectSet(Region *region); 1064514f5e3Sopenharmony_ci inline void DetachFreeObjectSet(Region *region); 1074514f5e3Sopenharmony_ci 1084514f5e3Sopenharmony_ci inline void FreeBumpPoint(); 1094514f5e3Sopenharmony_ci // Only fill free object 1104514f5e3Sopenharmony_ci inline void FillBumpPointer(); 1114514f5e3Sopenharmony_ci 1124514f5e3Sopenharmony_ci inline void ResetBumpPointer(uintptr_t begin, uintptr_t end, uintptr_t top); 1134514f5e3Sopenharmony_ci inline void ResetTopPointer(uintptr_t top); 1144514f5e3Sopenharmony_ci 1154514f5e3Sopenharmony_ci inline void Free(uintptr_t begin, size_t size, bool isAdd = true); 1164514f5e3Sopenharmony_ci 1174514f5e3Sopenharmony_ci inline size_t GetAvailableSize() const; 1184514f5e3Sopenharmony_ci inline size_t GetWastedSize() const; 1194514f5e3Sopenharmony_ci 1204514f5e3Sopenharmony_ci uintptr_t GetTop() const 1214514f5e3Sopenharmony_ci { 1224514f5e3Sopenharmony_ci return bpAllocator_.GetTop(); 1234514f5e3Sopenharmony_ci } 1244514f5e3Sopenharmony_ci 1254514f5e3Sopenharmony_ci size_t GetAllocatedSize() const 1264514f5e3Sopenharmony_ci { 1274514f5e3Sopenharmony_ci return allocationSizeAccumulator_; 1284514f5e3Sopenharmony_ci } 1294514f5e3Sopenharmony_ci 1304514f5e3Sopenharmony_ci void IncreaseAllocatedSize(size_t allocatedSize) 1314514f5e3Sopenharmony_ci { 1324514f5e3Sopenharmony_ci allocationSizeAccumulator_ += allocatedSize; 1334514f5e3Sopenharmony_ci } 1344514f5e3Sopenharmony_ci 1354514f5e3Sopenharmony_ciprivate: 1364514f5e3Sopenharmony_ci inline uintptr_t Allocate(T *object, size_t size); 1374514f5e3Sopenharmony_ci std::unique_ptr<FreeObjectList<T>> freeList_ {nullptr}; 1384514f5e3Sopenharmony_ci MemDescPool *memDescPool_ {nullptr}; 1394514f5e3Sopenharmony_ci BumpPointerAllocator bpAllocator_; 1404514f5e3Sopenharmony_ci BaseHeap *heap_{nullptr}; 1414514f5e3Sopenharmony_ci size_t allocationSizeAccumulator_ {0}; 1424514f5e3Sopenharmony_ci}; 1434514f5e3Sopenharmony_ci} // namespace panda::ecmascript 1444514f5e3Sopenharmony_ci 1454514f5e3Sopenharmony_ci#endif // ECMASCRIPT_MEM_ALLOCATOR_H 146