• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2024 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 RUNTIME_INCLUDE_MEM_ALLOCATOR_INL_H
17 #define RUNTIME_INCLUDE_MEM_ALLOCATOR_INL_H
18 
19 #include "runtime/include/mem/allocator.h"
20 namespace ark::mem {
21 
22 template <typename AllocT, bool NEED_LOCK>
AllocateSafe(size_t size,Alignment align,AllocT * objectAllocator,size_t poolSize,SpaceType spaceType,HeapSpace * heapSpace)23 inline void *ObjectAllocatorBase::AllocateSafe(size_t size, Alignment align, AllocT *objectAllocator, size_t poolSize,
24                                                SpaceType spaceType, HeapSpace *heapSpace)
25 {
26     void *mem = objectAllocator->template Alloc<NEED_LOCK>(size, align);
27     if (UNLIKELY(mem == nullptr)) {
28         return AddPoolsAndAlloc<AllocT, NEED_LOCK>(size, align, objectAllocator, poolSize, spaceType, heapSpace);
29     }
30     return mem;
31 }
32 
33 template <typename AllocT, bool NEED_LOCK>
34 // CC-OFFNXT(G.FUD.06) perf critical
AddPoolsAndAlloc(size_t size,Alignment align,AllocT * objectAllocator,size_t poolSize,SpaceType spaceType,HeapSpace * heapSpace)35 inline void *ObjectAllocatorBase::AddPoolsAndAlloc(size_t size, Alignment align, AllocT *objectAllocator,
36                                                    size_t poolSize, SpaceType spaceType, HeapSpace *heapSpace)
37 {
38     void *mem = nullptr;
39     static os::memory::Mutex poolLock;
40     os::memory::LockHolder<os::memory::Mutex, NEED_LOCK> lock(poolLock);
41     while (true) {
42         auto pool = heapSpace->TryAllocPool(poolSize, spaceType, AllocT::GetAllocatorType(), objectAllocator);
43         if (UNLIKELY(pool.GetMem() == nullptr)) {
44             return nullptr;
45         }
46         bool addedMemoryPool = objectAllocator->AddMemoryPool(pool.GetMem(), pool.GetSize());
47         if (!addedMemoryPool) {
48             LOG(FATAL, ALLOC) << "ObjectAllocator: couldn't add memory pool to object allocator";
49         }
50         mem = objectAllocator->template Alloc<NEED_LOCK>(size, align);
51         if (mem != nullptr) {
52             break;
53         }
54     }
55     return mem;
56 }
57 
58 template <MTModeT MT_MODE>
59 template <bool NEED_LOCK>
AllocateTenuredImpl(size_t size)60 void *ObjectAllocatorGen<MT_MODE>::AllocateTenuredImpl(size_t size)
61 {
62     void *mem = nullptr;
63     Alignment align = DEFAULT_ALIGNMENT;
64     size_t alignedSize = AlignUp(size, GetAlignmentInBytes(align));
65     if (alignedSize <= ObjectAllocator::GetMaxSize()) {
66         size_t poolSize = std::max(PANDA_DEFAULT_POOL_SIZE, ObjectAllocator::GetMinPoolSize());
67         mem = AllocateSafe<ObjectAllocator, NEED_LOCK>(size, align, objectAllocator_, poolSize,
68                                                        SpaceType::SPACE_TYPE_OBJECT, &heapSpaces_);
69     } else if (alignedSize <= LargeObjectAllocator::GetMaxSize()) {
70         size_t poolSize = std::max(PANDA_DEFAULT_POOL_SIZE, LargeObjectAllocator::GetMinPoolSize());
71         mem = AllocateSafe<LargeObjectAllocator, NEED_LOCK>(size, align, largeObjectAllocator_, poolSize,
72                                                             SpaceType::SPACE_TYPE_OBJECT, &heapSpaces_);
73     } else {
74         size_t poolSize = std::max(PANDA_DEFAULT_POOL_SIZE, HumongousObjectAllocator::GetMinPoolSize(size));
75         mem = AllocateSafe<HumongousObjectAllocator, NEED_LOCK>(size, align, humongousObjectAllocator_, poolSize,
76                                                                 SpaceType::SPACE_TYPE_HUMONGOUS_OBJECT, &heapSpaces_);
77     }
78     return mem;
79 }
80 
81 }  // namespace ark::mem
82 
83 #endif  // RUNTIME_INCLUDE_MEM_ALLOCATOR_INL_H
84