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
16 #include "common_interfaces/heap/heap_allocator.h"
17
18 #include "common_components/heap/heap_allocator-inl.h"
19 #include "common_components/common/type_def.h"
20 #include "common_components/heap/heap_manager.h"
21 #include "common_components/heap/allocator/region_manager.h"
22 #include "common_components/heap/allocator/region_space.h"
23
24 namespace common {
AllocateYoungInAllocBuffer(uintptr_t buffer,size_t size)25 Address AllocateYoungInAllocBuffer(uintptr_t buffer, size_t size)
26 {
27 ASSERT(buffer != 0);
28 AllocationBuffer *allocBuffer = reinterpret_cast<AllocationBuffer *>(buffer);
29 return allocBuffer->FastAllocateInTlab<AllocBufferType::YOUNG>(size);
30 }
31
AllocateOldInAllocBuffer(uintptr_t buffer,size_t size)32 Address AllocateOldInAllocBuffer(uintptr_t buffer, size_t size)
33 {
34 ASSERT(buffer != 0);
35 AllocationBuffer *allocBuffer = reinterpret_cast<AllocationBuffer *>(buffer);
36 return allocBuffer->FastAllocateInTlab<common::AllocBufferType::OLD>(size);
37 }
38
AllocateInYoungOrHuge(size_t size,LanguageType language)39 Address HeapAllocator::AllocateInYoungOrHuge(size_t size, LanguageType language)
40 {
41 auto address = HeapManager::Allocate(size);
42 BaseObject::Cast(address)->SetLanguageType(language);
43 return address;
44 }
45
AllocateInNonmoveOrHuge(size_t size,LanguageType language)46 Address HeapAllocator::AllocateInNonmoveOrHuge(size_t size, LanguageType language)
47 {
48 auto address = HeapManager::Allocate(size, AllocType::PINNED_OBJECT);
49 BaseObject::Cast(address)->SetLanguageType(language);
50 return address;
51 }
52
AllocateInOldOrHuge(size_t size,LanguageType language)53 Address HeapAllocator::AllocateInOldOrHuge(size_t size, LanguageType language)
54 {
55 auto address = HeapManager::Allocate(size, AllocType::MOVEABLE_OLD_OBJECT);
56 BaseObject::Cast(address)->SetLanguageType(language);
57 return address;
58 }
59
AllocateInHuge(size_t size,LanguageType language)60 Address HeapAllocator::AllocateInHuge(size_t size, LanguageType language)
61 {
62 auto address = HeapManager::Allocate(size);
63 BaseObject::Cast(address)->SetLanguageType(language);
64 return address;
65 }
66
AllocateInReadOnly(size_t size,LanguageType language)67 Address HeapAllocator::AllocateInReadOnly(size_t size, LanguageType language)
68 {
69 auto address = HeapManager::Allocate(size, AllocType::READ_ONLY_OBJECT);
70 BaseObject::Cast(address)->SetLanguageType(language);
71 return address;
72 }
73
AllocateLargeJitFortRegion(size_t size,LanguageType language)74 uintptr_t HeapAllocator::AllocateLargeJitFortRegion(size_t size, LanguageType language)
75 {
76 RegionSpace& allocator = reinterpret_cast<RegionSpace&>(Heap::GetHeap().GetAllocator());
77 auto address = allocator.AllocJitFortRegion(size);
78 BaseObject::Cast(address)->SetLanguageType(language);
79 return address;
80 }
81
82 // below are interfaces used for serialize
AllocateNoGC(size_t size)83 Address HeapAllocator::AllocateNoGC(size_t size)
84 {
85 return HeapManager::Allocate(size, AllocType::MOVEABLE_OBJECT, false);
86 }
87
AllocateOldOrLargeNoGC(size_t size)88 Address HeapAllocator::AllocateOldOrLargeNoGC(size_t size)
89 {
90 if (size >= RegionDesc::LARGE_OBJECT_DEFAULT_THRESHOLD) {
91 return AllocateLargeRegion(size);
92 }
93 return HeapManager::Allocate(size, AllocType::MOVEABLE_OLD_OBJECT, false);
94 }
95
AllocatePinNoGC(size_t size)96 Address HeapAllocator::AllocatePinNoGC(size_t size)
97 {
98 return HeapManager::Allocate(size, AllocType::PINNED_OBJECT, false);
99 }
100
AllocateOldRegion()101 Address HeapAllocator::AllocateOldRegion()
102 {
103 RegionSpace& allocator = reinterpret_cast<RegionSpace&>(Heap::GetHeap().GetAllocator());
104 return allocator.AllocOldRegion();
105 }
106
AllocatePinnedRegion()107 Address HeapAllocator::AllocatePinnedRegion()
108 {
109 RegionSpace& allocator = reinterpret_cast<RegionSpace&>(Heap::GetHeap().GetAllocator());
110 return allocator.AllocPinnedRegion();
111 }
112
AllocateLargeRegion(size_t size)113 Address HeapAllocator::AllocateLargeRegion(size_t size)
114 {
115 RegionSpace& allocator = reinterpret_cast<RegionSpace&>(Heap::GetHeap().GetAllocator());
116 return allocator.AllocLargeRegion(size);
117 }
118
119 } // namespace common
120