• 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 * object_allocator,size_t pool_size,SpaceType space_type,HeapSpace * heap_space)23 inline void *ObjectAllocatorBase::AllocateSafe(size_t size, Alignment align, AllocT *object_allocator, size_t pool_size,
24                                                SpaceType space_type, HeapSpace *heap_space)
25 {
26     void *mem = object_allocator->template Alloc<need_lock>(size, align);
27     if (UNLIKELY(mem == nullptr)) {
28         return AddPoolsAndAlloc<AllocT, need_lock>(size, align, object_allocator, pool_size, space_type, heap_space);
29     }
30     return mem;
31 }
32 
33 template <typename AllocT, bool need_lock>
AddPoolsAndAlloc(size_t size,Alignment align,AllocT * object_allocator,size_t pool_size,SpaceType space_type,HeapSpace * heap_space)34 inline void *ObjectAllocatorBase::AddPoolsAndAlloc(size_t size, Alignment align, AllocT *object_allocator,
35                                                    size_t pool_size, SpaceType space_type, HeapSpace *heap_space)
36 {
37     void *mem = nullptr;
38     static os::memory::Mutex pool_lock;
39     os::memory::LockHolder<os::memory::Mutex, need_lock> lock(pool_lock);
40     while (true) {
41         auto pool = heap_space->TryAllocPool(pool_size, space_type, AllocT::GetAllocatorType(), object_allocator);
42         if (UNLIKELY(pool.GetMem() == nullptr)) {
43             return nullptr;
44         }
45         bool added_memory_pool = object_allocator->AddMemoryPool(pool.GetMem(), pool.GetSize());
46         if (!added_memory_pool) {
47             LOG(FATAL, ALLOC) << "ObjectAllocator: couldn't add memory pool to object allocator";
48         }
49         mem = object_allocator->template Alloc<need_lock>(size, align);
50         if (mem != nullptr) {
51             break;
52         }
53     }
54     return mem;
55 }
56 
57 template <MTModeT MTMode>
58 template <bool need_lock>
AllocateTenuredImpl(size_t size)59 void *ObjectAllocatorGen<MTMode>::AllocateTenuredImpl(size_t size)
60 {
61     void *mem = nullptr;
62     Alignment align = DEFAULT_ALIGNMENT;
63     size_t aligned_size = AlignUp(size, GetAlignmentInBytes(align));
64     if (aligned_size <= ObjectAllocator::GetMaxSize()) {
65         size_t pool_size = std::max(PANDA_DEFAULT_POOL_SIZE, ObjectAllocator::GetMinPoolSize());
66         mem = AllocateSafe<ObjectAllocator, need_lock>(size, align, object_allocator_, pool_size,
67                                                        SpaceType::SPACE_TYPE_OBJECT, &heap_spaces_);
68     } else if (aligned_size <= LargeObjectAllocator::GetMaxSize()) {
69         size_t pool_size = std::max(PANDA_DEFAULT_POOL_SIZE, LargeObjectAllocator::GetMinPoolSize());
70         mem = AllocateSafe<LargeObjectAllocator, need_lock>(size, align, large_object_allocator_, pool_size,
71                                                             SpaceType::SPACE_TYPE_OBJECT, &heap_spaces_);
72     } else {
73         size_t pool_size = std::max(PANDA_DEFAULT_POOL_SIZE, HumongousObjectAllocator::GetMinPoolSize(size));
74         mem = AllocateSafe<HumongousObjectAllocator, need_lock>(size, align, humongous_object_allocator_, pool_size,
75                                                                 SpaceType::SPACE_TYPE_HUMONGOUS_OBJECT, &heap_spaces_);
76     }
77     return mem;
78 }
79 
80 }  // namespace panda::mem
81 
82 #endif  // RUNTIME_INCLUDE_MEM_ALLOCATOR_INL_H
83