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