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 #include "common_components/heap/allocator/allocator.h" 16 17 #include <cinttypes> 18 #include <cstdint> 19 20 #include "common_components/base/immortal_wrapper.h" 21 #include "common_components/common/base_object.h" 22 #include "common_components/heap/allocator/region_space.h" 23 #include "common_components/mutator/thread_local.h" 24 25 namespace common { 26 using namespace std; Allocator()27Allocator::Allocator() 28 { 29 allocBufferManager_ = new (std::nothrow) AllocBufferManager(); 30 LOGF_CHECK(allocBufferManager_ != nullptr) << "new alloc buffer manager failed"; 31 asyncAllocationInitSwitch_ = InitAyncAllocation(); 32 isAsyncAllocationEnable_.store(asyncAllocationInitSwitch_); 33 } 34 InitAyncAllocation()35bool Allocator::InitAyncAllocation() 36 { 37 auto enableAsyncAllocation = std::getenv("arkEnableAsyncAllocation"); 38 if (enableAsyncAllocation == nullptr) { 39 #if defined(PANDA_TARGET_OHOS) 40 return true; 41 #else 42 return false; 43 #endif 44 } 45 if (strlen(enableAsyncAllocation) != 1) { 46 LOG_COMMON(ERROR) << "Unsupported arkEnableAsyncAllocation, arkEnableAsyncAllocation should be 0 or 1.\n"; 47 #if defined(PANDA_TARGET_OHOS) 48 return true; 49 #else 50 return false; 51 #endif 52 } 53 54 switch (enableAsyncAllocation[0]) { 55 case '0': 56 return false; 57 case '1': 58 return true; 59 default: 60 LOG_COMMON(ERROR) << "Unsupported arkEnableAsyncAllocation, arkEnableAsyncAllocation should be 0 or 1.\n"; 61 } 62 return true; 63 } 64 65 // PageAlllocator 66 // the input parameter cat should be guaranteed in the range of value of enum type AllocationTag by 67 // external invoker, in order to avoid exceed the border of matrix Instance(AllocationTag tag)68AggregateAllocator& AggregateAllocator::Instance(AllocationTag tag) 69 { 70 static ImmortalWrapper<AggregateAllocator> instance[MAX_ALLOCATION_TAG]; 71 return *(instance[tag]); 72 } 73 74 // PagePool Instance()75PagePool& PagePool::Instance() noexcept 76 { 77 static ImmortalWrapper<PagePool> instance("PagePool"); 78 return *instance; 79 } 80 CreateAllocator()81Allocator* Allocator::CreateAllocator() 82 { 83 RegionSpace* heapSpace = new (std::nothrow) RegionSpace(); 84 LOGF_CHECK(heapSpace != nullptr) << "New RegionSpace failed"; 85 return heapSpace; 86 } 87 } // namespace common 88