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 #include <mem/gc/hybrid-gc/hybrid_object_allocator.h>
17
18 #include "libpandabase/mem/mem.h"
19 #include "runtime/include/class.h"
20 #include "runtime/mem/freelist_allocator-inl.h"
21 #include "runtime/mem/humongous_obj_allocator-inl.h"
22 #include "runtime/mem/region_allocator-inl.h"
23
24 namespace panda::mem {
HybridObjectAllocator(mem::MemStatsType * mem_stats,bool create_pygote_space_allocator)25 HybridObjectAllocator::HybridObjectAllocator(mem::MemStatsType *mem_stats, bool create_pygote_space_allocator)
26 : ObjectAllocatorBase(mem_stats, GCCollectMode::GC_ALL, create_pygote_space_allocator)
27 {
28 // TODO(ipetrov): Where do we use HybridObjectAllocator? Maybe remove?
29 const auto &options = Runtime::GetOptions();
30 heap_space_.Initialize(options.GetInitYoungSpaceSize(), options.WasSetInitYoungSpaceSize(),
31 options.GetYoungSpaceSize(), options.WasSetYoungSpaceSize(),
32 MemConfig::GetInitialHeapSizeLimit(), MemConfig::GetHeapSizeLimit(),
33 options.GetMinHeapFreePercentage(), options.GetMaxHeapFreePercentage());
34 if (create_pygote_space_allocator) {
35 ASSERT(pygote_space_allocator_ != nullptr);
36 pygote_space_allocator_->SetHeapSpace(&heap_space_);
37 }
38 object_allocator_ = new (std::nothrow) ObjectAllocator(mem_stats, &heap_space_);
39 large_object_allocator_ = new (std::nothrow) LargeObjectAllocator(mem_stats);
40 humongous_object_allocator_ = new (std::nothrow) HumongousObjectAllocator(mem_stats);
41 }
42
Allocate(size_t size,Alignment align,panda::ManagedThread * thread)43 void *HybridObjectAllocator::Allocate(size_t size, Alignment align, [[maybe_unused]] panda::ManagedThread *thread)
44 {
45 void *mem = nullptr;
46 size_t aligned_size = AlignUp(size, GetAlignmentInBytes(align));
47 mem = object_allocator_->Alloc(aligned_size, align);
48 return mem;
49 }
50
AllocateInLargeAllocator(size_t size,Alignment align,BaseClass * base_cls)51 void *HybridObjectAllocator::AllocateInLargeAllocator(size_t size, Alignment align, BaseClass *base_cls)
52 {
53 if (base_cls->IsDynamicClass()) {
54 return nullptr;
55 }
56 auto cls = static_cast<Class *>(base_cls);
57 size_t aligned_size = AlignUp(size, GetAlignmentInBytes(align));
58 void *mem = nullptr;
59 if ((aligned_size >= GetLargeThreshold()) &&
60 (cls->IsStringClass() || (cls->IsArrayClass() && cls->GetComponentType()->IsPrimitive()))) {
61 if (aligned_size <= LargeObjectAllocator::GetMaxSize()) {
62 mem = large_object_allocator_->Alloc(size, align);
63 if (UNLIKELY(mem == nullptr)) {
64 size_t pool_size = std::max(PANDA_DEFAULT_POOL_SIZE, LargeObjectAllocator::GetMinPoolSize());
65 auto pool = heap_space_.TryAllocPool(pool_size, SpaceType::SPACE_TYPE_OBJECT,
66 LargeObjectAllocator::GetAllocatorType(), large_object_allocator_);
67 if (pool.GetMem() == nullptr ||
68 !large_object_allocator_->AddMemoryPool(pool.GetMem(), pool.GetSize())) {
69 LOG(FATAL, ALLOC) << "HybridObjectAllocator: couldn't add memory pool to large object allocator";
70 }
71 mem = large_object_allocator_->Alloc(size, align);
72 }
73 } else {
74 mem = humongous_object_allocator_->Alloc(size, align);
75 if (UNLIKELY(mem == nullptr)) {
76 size_t pool_size;
77 pool_size = std::max(PANDA_DEFAULT_POOL_SIZE, HumongousObjectAllocator::GetMinPoolSize(size));
78 auto pool =
79 heap_space_.TryAllocPool(pool_size, SpaceType::SPACE_TYPE_HUMONGOUS_OBJECT,
80 HumongousObjectAllocator::GetAllocatorType(), humongous_object_allocator_);
81 if (pool.GetMem() == nullptr ||
82 !humongous_object_allocator_->AddMemoryPool(pool.GetMem(), pool.GetSize())) {
83 LOG(FATAL, ALLOC)
84 << "HybridObjectAllocator: couldn't add memory pool to humongous object allocator";
85 }
86 mem = humongous_object_allocator_->Alloc(size, align);
87 }
88 }
89 }
90 return mem;
91 }
92
ContainObject(const ObjectHeader * obj) const93 bool HybridObjectAllocator::ContainObject(const ObjectHeader *obj) const
94 {
95 if (object_allocator_->ContainObject(obj)) {
96 return true;
97 }
98 if (large_object_allocator_->ContainObject(obj)) {
99 return true;
100 }
101 if (humongous_object_allocator_->ContainObject(obj)) {
102 return true;
103 }
104 return false;
105 }
106
IsLive(const ObjectHeader * obj)107 bool HybridObjectAllocator::IsLive(const ObjectHeader *obj)
108 {
109 if (object_allocator_->ContainObject(obj)) {
110 return object_allocator_->IsLive(obj);
111 }
112 if (large_object_allocator_->ContainObject(obj)) {
113 return large_object_allocator_->IsLive(obj);
114 }
115 if (humongous_object_allocator_->ContainObject(obj)) {
116 return humongous_object_allocator_->IsLive(obj);
117 }
118 return false;
119 }
120
VerifyAllocatorStatus()121 size_t HybridObjectAllocator::VerifyAllocatorStatus()
122 {
123 // TODO: implement it.
124 return 0;
125 }
126
CreateNewTLAB(ManagedThread * thread)127 TLAB *HybridObjectAllocator::CreateNewTLAB(ManagedThread *thread)
128 {
129 return object_allocator_->CreateNewTLAB(thread);
130 }
131
GetTLABMaxAllocSize()132 size_t HybridObjectAllocator::GetTLABMaxAllocSize()
133 {
134 return ObjectAllocator::GetMaxRegularObjectSize();
135 }
136
~HybridObjectAllocator()137 HybridObjectAllocator::~HybridObjectAllocator()
138 {
139 delete object_allocator_;
140 delete large_object_allocator_;
141 delete humongous_object_allocator_;
142 }
143
144 } // namespace panda::mem
145