1 /* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef COMMON_COMPONENTS_HEAP_ALLOCATOR_ALLOCATOR_H 17 #define COMMON_COMPONENTS_HEAP_ALLOCATOR_ALLOCATOR_H 18 19 #include "common_components/heap/allocator/alloc_buffer_manager.h" 20 #include "common_components/heap/heap.h" 21 22 namespace common { 23 // Allocator abstract class 24 class Allocator { 25 public: 26 static constexpr size_t ALLOC_ALIGN = 8; 27 static constexpr size_t HEADER_SIZE = 0; // no header for arkcommon object 28 29 static Allocator* CreateAllocator(); 30 31 virtual HeapAddress Allocate(size_t size, AllocType allocType) = 0; 32 virtual HeapAddress AllocateNoGC(size_t size, AllocType allocType) = 0; 33 virtual bool ForEachObject(const std::function<void(BaseObject*)>&, bool safe) const = 0; 34 35 // release physical pages of garbage memory. 36 virtual size_t ReclaimGarbageMemory(bool releaseAll) = 0; 37 virtual void FeedHungryBuffers() = 0; 38 39 // returns the total size of live large objects, excluding alignment/roundup/header, ... 40 // LargeObjects() is missing. 41 virtual size_t LargeObjectSize() const = 0; 42 43 // returns the total bytes that has been allocated, including alignment/roundup/header, ... 44 // allocated bytes for large objects are included. 45 virtual size_t GetAllocatedBytes() const = 0; 46 47 virtual size_t GetSurvivedSize() const = 0; 48 RegisterAllocBuffer(AllocationBuffer & buffer)49 inline void RegisterAllocBuffer(AllocationBuffer& buffer) const 50 { 51 allocBufferManager_->RegisterAllocBuffer(buffer); 52 } 53 UnregisterAllocBuffer(AllocationBuffer & buffer)54 inline void UnregisterAllocBuffer(AllocationBuffer& buffer) const 55 { 56 allocBufferManager_->UnregisterAllocBuffer(buffer); 57 } 58 ~Allocator()59 virtual ~Allocator() {} 60 Allocator(); 61 62 virtual void Init(const RuntimeParam& param) = 0; 63 virtual size_t GetMaxCapacity() const = 0; 64 virtual size_t GetCurrentCapacity() const = 0; 65 virtual size_t GetUsedPageSize() const = 0; 66 virtual HeapAddress GetSpaceStartAddress() const = 0; 67 virtual HeapAddress GetSpaceEndAddress() const = 0; 68 69 // IsHeapAddress is a range-based check, used to quickly identify heap address, 70 // non-heap address never falls into this address range. 71 // for more accurate check, use IsHeapObject(). IsHeapAddress(HeapAddress addr)72 ALWAYS_INLINE_CC bool IsHeapAddress(HeapAddress addr) const { return Heap::IsHeapAddress(addr); } 73 74 #ifndef NDEBUG 75 virtual bool IsHeapObject(HeapAddress) const = 0; 76 #endif 77 78 template <typename AllocBufferVisitor> VisitAllocBuffers(const AllocBufferVisitor & visitor)79 void VisitAllocBuffers(const AllocBufferVisitor& visitor) { allocBufferManager_->VisitAllocBuffers(visitor); } AddHungryBuffer(AllocationBuffer & buffer)80 void AddHungryBuffer(AllocationBuffer& buffer) { allocBufferManager_->AddHungryBuffer(buffer); } SwapHungryBuffers(AllocBufferManager::HungryBuffers & getBufferList)81 void SwapHungryBuffers(AllocBufferManager::HungryBuffers &getBufferList) 82 { 83 allocBufferManager_->SwapHungryBuffers(getBufferList); 84 } 85 86 protected: 87 AllocBufferManager* allocBufferManager_; 88 std::atomic<bool> isAsyncAllocationEnable_ = { true }; 89 90 private: 91 bool InitAyncAllocation(); 92 bool asyncAllocationInitSwitch_ = true; 93 }; 94 } // namespace common 95 96 #endif 97