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_components/heap/allocator/region_desc.h"
17 #include "common_components/heap/allocator/region_space.h"
18 #include "common_components/heap/allocator/region_space.cpp"
19 #include "common_components/heap/collector/collector_resources.h"
20 #include "common_components/tests/test_helper.h"
21 #include "common_interfaces/base_runtime.h"
22
23 using namespace common;
24
25 namespace common::test {
26 class RegionSpaceTest : public common::test::BaseTestWithScope {
27 protected:
SetUpTestCase()28 static void SetUpTestCase()
29 {
30 BaseRuntime::GetInstance()->Init();
31 }
32
TearDownTestCase()33 static void TearDownTestCase()
34 {
35 BaseRuntime::GetInstance()->Fini();
36 }
37
SetUp()38 void SetUp() override
39 {
40 holder_ = ThreadHolder::CreateAndRegisterNewThreadHolder(nullptr);
41 scope_ = new ThreadHolder::TryBindMutatorScope(holder_);
42 }
43
TearDown()44 void TearDown() override
45 {
46 if (scope_ != nullptr) {
47 delete scope_;
48 scope_ = nullptr;
49 }
50 }
51
52 ThreadHolder *holder_ {nullptr};
53 ThreadHolder::TryBindMutatorScope *scope_ {nullptr};
54 };
55
HWTEST_F_L0(RegionSpaceTest,FeedHungryBuffers2)56 HWTEST_F_L0(RegionSpaceTest, FeedHungryBuffers2)
57 {
58 auto* mutator = common::Mutator::GetMutator();
59 mutator->SetMutatorPhase(GCPhase::GC_PHASE_FIX);
60 ThreadLocal::SetThreadType(ThreadType::ARK_PROCESSOR);
61 AllocationBuffer* buffer1 = new (std::nothrow) AllocationBuffer();
62 AllocationBuffer* buffer2 = new (std::nothrow) AllocationBuffer();
63 RegionDesc* region = RegionDesc::InitRegion(0, 1, RegionDesc::UnitRole::LARGE_SIZED_UNITS);
64 region->InitFreeUnits();
65 buffer1->SetPreparedRegion(region);
66 buffer2->SetPreparedRegion(region);
67
68 Heap::GetHeap().GetAllocator().AddHungryBuffer(*buffer1);
69 Heap::GetHeap().GetAllocator().AddHungryBuffer(*buffer2);
70 Heap::GetHeap().GetAllocator().FeedHungryBuffers();
71 EXPECT_NE(buffer2->GetPreparedRegion(), nullptr);
72 delete buffer1;
73 delete buffer2;
74 }
75
HWTEST_F_L0(RegionSpaceTest,FeedHungryBuffers3)76 HWTEST_F_L0(RegionSpaceTest, FeedHungryBuffers3)
77 {
78 auto* mutator = common::Mutator::GetMutator();
79 mutator->SetMutatorPhase(GCPhase::GC_PHASE_FIX);
80 Heap::GetHeap().GetAllocator().FeedHungryBuffers();
81 AllocBufferManager::HungryBuffers hungryBuffers;
82 Heap::GetHeap().GetAllocator().SwapHungryBuffers(hungryBuffers);
83 EXPECT_EQ(hungryBuffers.size(), 0);
84 }
85
HWTEST_F_L0(RegionSpaceTest,AllocRegion_PhaseEnum)86 HWTEST_F_L0(RegionSpaceTest, AllocRegion_PhaseEnum)
87 {
88 auto* mutator = common::Mutator::GetMutator();
89 mutator->SetMutatorPhase(GCPhase::GC_PHASE_ENUM);
90 RegionSpace& theAllocator = reinterpret_cast<RegionSpace&>(Heap::GetHeap().GetAllocator());
91 uintptr_t addr = theAllocator.AllocOldRegion();
92 ASSERT_NE(addr, 0);
93 RegionDesc* region = RegionDesc::GetAliveRegionDescAt(addr);
94 EXPECT_EQ(region->GetMarkingLine(), region->GetRegionStart());
95 EXPECT_EQ(region->GetCopyLine(), std::numeric_limits<uintptr_t>::max());
96 }
97
HWTEST_F_L0(RegionSpaceTest,AllocRegion_PhaseMark)98 HWTEST_F_L0(RegionSpaceTest, AllocRegion_PhaseMark)
99 {
100 auto* mutator = common::Mutator::GetMutator();
101 mutator->SetMutatorPhase(GCPhase::GC_PHASE_MARK);
102 RegionSpace& theAllocator = reinterpret_cast<RegionSpace&>(Heap::GetHeap().GetAllocator());
103 uintptr_t addr = theAllocator.AllocOldRegion();
104 ASSERT_NE(addr, 0);
105 RegionDesc* region = RegionDesc::GetAliveRegionDescAt(addr);
106 EXPECT_EQ(region->GetMarkingLine(), region->GetRegionStart());
107 EXPECT_EQ(region->GetCopyLine(), std::numeric_limits<uintptr_t>::max());
108 }
109
HWTEST_F_L0(RegionSpaceTest,AllocRegion_PhaseRemarkStab)110 HWTEST_F_L0(RegionSpaceTest, AllocRegion_PhaseRemarkStab)
111 {
112 auto* mutator = common::Mutator::GetMutator();
113 mutator->SetMutatorPhase(GCPhase::GC_PHASE_REMARK_SATB);
114 RegionSpace& theAllocator = reinterpret_cast<RegionSpace&>(Heap::GetHeap().GetAllocator());
115 uintptr_t addr = theAllocator.AllocOldRegion();
116 ASSERT_NE(addr, 0);
117 RegionDesc* region = RegionDesc::GetAliveRegionDescAt(addr);
118 EXPECT_EQ(region->GetMarkingLine(), region->GetRegionStart());
119 EXPECT_EQ(region->GetCopyLine(), std::numeric_limits<uintptr_t>::max());
120 }
121
HWTEST_F_L0(RegionSpaceTest,AllocRegion_PhasePostMark)122 HWTEST_F_L0(RegionSpaceTest, AllocRegion_PhasePostMark)
123 {
124 auto* mutator = common::Mutator::GetMutator();
125 mutator->SetMutatorPhase(GCPhase::GC_PHASE_POST_MARK);
126 RegionSpace& theAllocator = reinterpret_cast<RegionSpace&>(Heap::GetHeap().GetAllocator());
127 uintptr_t addr = theAllocator.AllocOldRegion();
128 ASSERT_NE(addr, 0);
129 RegionDesc* region = RegionDesc::GetAliveRegionDescAt(addr);
130 EXPECT_EQ(region->GetMarkingLine(), region->GetRegionStart());
131 EXPECT_EQ(region->GetCopyLine(), std::numeric_limits<uintptr_t>::max());
132 }
133
HWTEST_F_L0(RegionSpaceTest,AllocRegion_PhasePrecopy)134 HWTEST_F_L0(RegionSpaceTest, AllocRegion_PhasePrecopy)
135 {
136 auto* mutator = common::Mutator::GetMutator();
137 mutator->SetMutatorPhase(GCPhase::GC_PHASE_PRECOPY);
138 RegionSpace& theAllocator = reinterpret_cast<RegionSpace&>(Heap::GetHeap().GetAllocator());
139 uintptr_t addr = theAllocator.AllocOldRegion();
140 ASSERT_NE(addr, 0);
141 RegionDesc* region = RegionDesc::GetAliveRegionDescAt(addr);
142 EXPECT_EQ(region->GetCopyLine(), region->GetRegionStart());
143 }
144
HWTEST_F_L0(RegionSpaceTest,AllocRegion_PhaseCopy)145 HWTEST_F_L0(RegionSpaceTest, AllocRegion_PhaseCopy)
146 {
147 auto* mutator = common::Mutator::GetMutator();
148 mutator->SetMutatorPhase(GCPhase::GC_PHASE_COPY);
149 RegionSpace& theAllocator = reinterpret_cast<RegionSpace&>(Heap::GetHeap().GetAllocator());
150 uintptr_t addr = theAllocator.AllocOldRegion();
151 ASSERT_NE(addr, 0);
152 RegionDesc* region = RegionDesc::GetAliveRegionDescAt(addr);
153 EXPECT_EQ(region->GetCopyLine(), region->GetRegionStart());
154 }
155
HWTEST_F_L0(RegionSpaceTest,AllocRegion_PhaseFix)156 HWTEST_F_L0(RegionSpaceTest, AllocRegion_PhaseFix)
157 {
158 auto* mutator = common::Mutator::GetMutator();
159 mutator->SetMutatorPhase(GCPhase::GC_PHASE_FIX);
160 RegionSpace& theAllocator = reinterpret_cast<RegionSpace&>(Heap::GetHeap().GetAllocator());
161 uintptr_t addr = theAllocator.AllocOldRegion();
162 ASSERT_NE(addr, 0);
163 RegionDesc* region = RegionDesc::GetAliveRegionDescAt(addr);
164 EXPECT_EQ(region->GetCopyLine(), region->GetRegionStart());
165 }
166
HWTEST_F_L0(RegionSpaceTest,AllocRegion_PhaseUndef)167 HWTEST_F_L0(RegionSpaceTest, AllocRegion_PhaseUndef)
168 {
169 auto* mutator = common::Mutator::GetMutator();
170 mutator->SetMutatorPhase(GCPhase::GC_PHASE_UNDEF);
171 RegionSpace& theAllocator = reinterpret_cast<RegionSpace&>(Heap::GetHeap().GetAllocator());
172 uintptr_t addr = theAllocator.AllocOldRegion();
173 ASSERT_NE(addr, 0);
174 RegionDesc* region = RegionDesc::GetAliveRegionDescAt(addr);
175 EXPECT_EQ(region->GetCopyLine(), std::numeric_limits<uintptr_t>::max());
176 }
177
HWTEST_F_L0(RegionSpaceTest,AllocPinnedRegion_PhaseEnum)178 HWTEST_F_L0(RegionSpaceTest, AllocPinnedRegion_PhaseEnum)
179 {
180 auto* mutator = common::Mutator::GetMutator();
181 mutator->SetMutatorPhase(GCPhase::GC_PHASE_ENUM);
182 RegionSpace& theAllocator = reinterpret_cast<RegionSpace&>(Heap::GetHeap().GetAllocator());
183 uintptr_t addr = theAllocator.AllocPinnedRegion();
184 ASSERT_NE(addr, 0);
185 RegionDesc* region = RegionDesc::GetAliveRegionDescAt(addr);
186 EXPECT_EQ(region->GetMarkingLine(), region->GetRegionStart());
187 EXPECT_EQ(region->GetCopyLine(), std::numeric_limits<uintptr_t>::max());
188 }
189
HWTEST_F_L0(RegionSpaceTest,AllocPinnedRegion_PhaseMark)190 HWTEST_F_L0(RegionSpaceTest, AllocPinnedRegion_PhaseMark)
191 {
192 auto* mutator = common::Mutator::GetMutator();
193 mutator->SetMutatorPhase(GCPhase::GC_PHASE_MARK);
194 RegionSpace& theAllocator = reinterpret_cast<RegionSpace&>(Heap::GetHeap().GetAllocator());
195 uintptr_t addr = theAllocator.AllocPinnedRegion();
196 ASSERT_NE(addr, 0);
197 RegionDesc* region = RegionDesc::GetAliveRegionDescAt(addr);
198 EXPECT_EQ(region->GetMarkingLine(), region->GetRegionStart());
199 EXPECT_EQ(region->GetCopyLine(), std::numeric_limits<uintptr_t>::max());
200 }
201
HWTEST_F_L0(RegionSpaceTest,AllocPinnedRegion_PhaseRemarkStab)202 HWTEST_F_L0(RegionSpaceTest, AllocPinnedRegion_PhaseRemarkStab)
203 {
204 auto* mutator = common::Mutator::GetMutator();
205 mutator->SetMutatorPhase(GCPhase::GC_PHASE_REMARK_SATB);
206 RegionSpace& theAllocator = reinterpret_cast<RegionSpace&>(Heap::GetHeap().GetAllocator());
207 uintptr_t addr = theAllocator.AllocPinnedRegion();
208 ASSERT_NE(addr, 0);
209 RegionDesc* region = RegionDesc::GetAliveRegionDescAt(addr);
210 EXPECT_EQ(region->GetMarkingLine(), region->GetRegionStart());
211 EXPECT_EQ(region->GetCopyLine(), std::numeric_limits<uintptr_t>::max());
212 }
213
HWTEST_F_L0(RegionSpaceTest,AllocPinnedRegion_PhasePostMark)214 HWTEST_F_L0(RegionSpaceTest, AllocPinnedRegion_PhasePostMark)
215 {
216 auto* mutator = common::Mutator::GetMutator();
217 mutator->SetMutatorPhase(GCPhase::GC_PHASE_POST_MARK);
218 RegionSpace& theAllocator = reinterpret_cast<RegionSpace&>(Heap::GetHeap().GetAllocator());
219 uintptr_t addr = theAllocator.AllocPinnedRegion();
220 ASSERT_NE(addr, 0);
221 RegionDesc* region = RegionDesc::GetAliveRegionDescAt(addr);
222 EXPECT_EQ(region->GetMarkingLine(), region->GetRegionStart());
223 EXPECT_EQ(region->GetCopyLine(), std::numeric_limits<uintptr_t>::max());
224 }
225
HWTEST_F_L0(RegionSpaceTest,AllocPinnedRegion_PhasePrecopy)226 HWTEST_F_L0(RegionSpaceTest, AllocPinnedRegion_PhasePrecopy)
227 {
228 auto* mutator = common::Mutator::GetMutator();
229 mutator->SetMutatorPhase(GCPhase::GC_PHASE_PRECOPY);
230 RegionSpace& theAllocator = reinterpret_cast<RegionSpace&>(Heap::GetHeap().GetAllocator());
231 uintptr_t addr = theAllocator.AllocPinnedRegion();
232 ASSERT_NE(addr, 0);
233 RegionDesc* region = RegionDesc::GetAliveRegionDescAt(addr);
234 EXPECT_EQ(region->GetCopyLine(), region->GetRegionStart());
235 }
236
HWTEST_F_L0(RegionSpaceTest,AllocPinnedRegion_PhaseCopy)237 HWTEST_F_L0(RegionSpaceTest, AllocPinnedRegion_PhaseCopy)
238 {
239 auto* mutator = common::Mutator::GetMutator();
240 mutator->SetMutatorPhase(GCPhase::GC_PHASE_COPY);
241 RegionSpace& theAllocator = reinterpret_cast<RegionSpace&>(Heap::GetHeap().GetAllocator());
242 uintptr_t addr = theAllocator.AllocPinnedRegion();
243 ASSERT_NE(addr, 0);
244 RegionDesc* region = RegionDesc::GetAliveRegionDescAt(addr);
245 EXPECT_EQ(region->GetCopyLine(), region->GetRegionStart());
246 }
247
HWTEST_F_L0(RegionSpaceTest,AllocPinnedRegion_PhaseFix)248 HWTEST_F_L0(RegionSpaceTest, AllocPinnedRegion_PhaseFix)
249 {
250 auto* mutator = common::Mutator::GetMutator();
251 mutator->SetMutatorPhase(GCPhase::GC_PHASE_FIX);
252 RegionSpace& theAllocator = reinterpret_cast<RegionSpace&>(Heap::GetHeap().GetAllocator());
253 uintptr_t addr = theAllocator.AllocPinnedRegion();
254 ASSERT_NE(addr, 0);
255 RegionDesc* region = RegionDesc::GetAliveRegionDescAt(addr);
256 EXPECT_EQ(region->GetCopyLine(), region->GetRegionStart());
257 }
258
HWTEST_F_L0(RegionSpaceTest,AllocPinnedRegion_PhaseUndef)259 HWTEST_F_L0(RegionSpaceTest, AllocPinnedRegion_PhaseUndef)
260 {
261 auto* mutator = common::Mutator::GetMutator();
262 mutator->SetMutatorPhase(GCPhase::GC_PHASE_UNDEF);
263 RegionSpace& theAllocator = reinterpret_cast<RegionSpace&>(Heap::GetHeap().GetAllocator());
264 uintptr_t addr = theAllocator.AllocPinnedRegion();
265 ASSERT_NE(addr, 0);
266 RegionDesc* region = RegionDesc::GetAliveRegionDescAt(addr);
267 EXPECT_EQ(region->GetCopyLine(), std::numeric_limits<uintptr_t>::max());
268 }
269
HWTEST_F_L0(RegionSpaceTest,AllocateThreadLocalRegion2)270 HWTEST_F_L0(RegionSpaceTest, AllocateThreadLocalRegion2)
271 {
272 auto* mutator = common::Mutator::GetMutator();
273 mutator->SetMutatorPhase(GCPhase::GC_PHASE_FIX);
274 ThreadLocal::SetThreadType(ThreadType::ARK_PROCESSOR);
275 RegionSpace& theAllocator = reinterpret_cast<RegionSpace&>(Heap::GetHeap().GetAllocator());
276 RegionDesc* region = theAllocator.AllocateThreadLocalRegion<AllocBufferType::OLD>(false);
277 EXPECT_EQ(region->GetCopyLine(), region->GetRegionStart());
278 }
279
HWTEST_F_L0(RegionSpaceTest,AllocateThreadLocalRegion3)280 HWTEST_F_L0(RegionSpaceTest, AllocateThreadLocalRegion3)
281 {
282 auto* mutator = common::Mutator::GetMutator();
283 mutator->SetMutatorPhase(GCPhase::GC_PHASE_COPY);
284 RegionSpace& theAllocator = reinterpret_cast<RegionSpace&>(Heap::GetHeap().GetAllocator());
285 ThreadLocal::SetThreadType(ThreadType::ARK_PROCESSOR);
286 RegionDesc* region = theAllocator.AllocateThreadLocalRegion<AllocBufferType::OLD>(false);
287 EXPECT_EQ(region->GetCopyLine(), region->GetRegionStart());
288 mutator->SetMutatorPhase(GCPhase::GC_PHASE_PRECOPY);
289 RegionDesc* region1 = theAllocator.AllocateThreadLocalRegion<AllocBufferType::OLD>(false);
290 EXPECT_EQ(region1->GetCopyLine(), region1->GetRegionStart());
291 }
292
HWTEST_F_L0(RegionSpaceTest,AllocateThreadLocalRegion4)293 HWTEST_F_L0(RegionSpaceTest, AllocateThreadLocalRegion4)
294 {
295 auto* mutator = common::Mutator::GetMutator();
296 mutator->SetMutatorPhase(GCPhase::GC_PHASE_ENUM);
297 RegionSpace& theAllocator = reinterpret_cast<RegionSpace&>(Heap::GetHeap().GetAllocator());
298 ThreadLocal::SetThreadType(ThreadType::ARK_PROCESSOR);
299 RegionDesc* region = theAllocator.AllocateThreadLocalRegion<AllocBufferType::OLD>(false);
300 EXPECT_EQ(region->GetMarkingLine(), region->GetRegionStart());
301 EXPECT_EQ(region->GetCopyLine(), std::numeric_limits<uintptr_t>::max());
302
303 mutator->SetMutatorPhase(GCPhase::GC_PHASE_MARK);
304 RegionDesc* region2 = theAllocator.AllocateThreadLocalRegion<AllocBufferType::OLD>(false);
305 EXPECT_EQ(region2->GetMarkingLine(), region2->GetRegionStart());
306 EXPECT_EQ(region2->GetCopyLine(), std::numeric_limits<uintptr_t>::max());
307
308 mutator->SetMutatorPhase(GCPhase::GC_PHASE_REMARK_SATB);
309 RegionDesc* region3 = theAllocator.AllocateThreadLocalRegion<AllocBufferType::OLD>(false);
310 EXPECT_EQ(region3->GetMarkingLine(), region3->GetRegionStart());
311 EXPECT_EQ(region3->GetCopyLine(), std::numeric_limits<uintptr_t>::max());
312
313 mutator->SetMutatorPhase(GCPhase::GC_PHASE_POST_MARK);
314 RegionDesc* region4 = theAllocator.AllocateThreadLocalRegion<AllocBufferType::OLD>(false);
315 EXPECT_EQ(region4->GetMarkingLine(), region4->GetRegionStart());
316 EXPECT_EQ(region4->GetCopyLine(), std::numeric_limits<uintptr_t>::max());
317 }
318
HWTEST_F_L0(RegionSpaceTest,CopyRegion)319 HWTEST_F_L0(RegionSpaceTest, CopyRegion)
320 {
321 auto* mutator = common::Mutator::GetMutator();
322 mutator->SetMutatorPhase(GCPhase::GC_PHASE_ENUM);
323 RegionSpace& theAllocator = reinterpret_cast<RegionSpace&>(Heap::GetHeap().GetAllocator());
324 uintptr_t addr = theAllocator.AllocOldRegion();
325 ASSERT_NE(addr, 0);
326 RegionDesc* region = RegionDesc::GetRegionDescAt(addr);
327 region->SetRegionType(RegionDesc::RegionType::FROM_REGION);
328 ASSERT(region->IsFromRegion());
329 theAllocator.CopyRegion(region);
330 EXPECT_EQ(theAllocator.FromSpaceSize(), 0);
331 }
332
HWTEST_F_L0(RegionSpaceTest,AllocateThreadLocalRegion1_NotGcThread_EntersElseBranch)333 HWTEST_F_L0(RegionSpaceTest, AllocateThreadLocalRegion1_NotGcThread_EntersElseBranch)
334 {
335 auto& heapAllocator = Heap::GetHeap().GetAllocator();
336 RegionSpace& regionSpace = reinterpret_cast<RegionSpace&>(heapAllocator);
337
338 Mutator::GetMutator()->SetMutatorPhase(GCPhase::GC_PHASE_ENUM);
339
340 auto* region = regionSpace.AllocateThreadLocalRegion<AllocBufferType::OLD>(false);
341 EXPECT_NE(region, nullptr);
342 }
343
HWTEST_F_L0(RegionSpaceTest,AllocateThreadLocalRegion2_NotGcThread_EntersElseBranch)344 HWTEST_F_L0(RegionSpaceTest, AllocateThreadLocalRegion2_NotGcThread_EntersElseBranch)
345 {
346 auto& heapAllocator = Heap::GetHeap().GetAllocator();
347 RegionSpace& regionSpace = reinterpret_cast<RegionSpace&>(heapAllocator);
348
349 Mutator::GetMutator()->SetMutatorPhase(GCPhase::GC_PHASE_PRECOPY);
350
351 auto* region = regionSpace.AllocateThreadLocalRegion<AllocBufferType::OLD>(false);
352 EXPECT_NE(region, nullptr);
353 }
354
HWTEST_F_L0(RegionSpaceTest,AllocateThreadLocalRegion3_NotGcThread_EntersElseBranch)355 HWTEST_F_L0(RegionSpaceTest, AllocateThreadLocalRegion3_NotGcThread_EntersElseBranch)
356 {
357 auto& heapAllocator = Heap::GetHeap().GetAllocator();
358 RegionSpace& regionSpace = reinterpret_cast<RegionSpace&>(heapAllocator);
359
360 Mutator::GetMutator()->SetMutatorPhase(GCPhase::GC_PHASE_FIX);
361
362 auto* region = regionSpace.AllocateThreadLocalRegion<AllocBufferType::OLD>(false);
363 EXPECT_NE(region, nullptr);
364 }
365
HWTEST_F_L0(RegionSpaceTest,Allocate_ValidSize_ReturnsNonNull)366 HWTEST_F_L0(RegionSpaceTest, Allocate_ValidSize_ReturnsNonNull)
367 {
368 RegionSpace& theAllocator = reinterpret_cast<RegionSpace&>(Heap::GetHeap().GetAllocator());
369 uintptr_t addr = theAllocator.AllocOldRegion();
370 ASSERT_NE(addr, 0);
371
372 RegionDesc* region = RegionDesc::GetRegionDescAt(addr);
373 region->InitFreeUnits();
374 region->SetRegionType(RegionDesc::RegionType::THREAD_LOCAL_REGION);
375 Mutator::GetMutator()->SetMutatorPhase(GCPhase::GC_PHASE_UNDEF);
376 Heap::GetHeap().EnableGC(true);
377 Heap::GetHeap().GetCollectorResources().SetGcStarted(false);
378
379 uintptr_t result = theAllocator.Allocate(16, AllocType::PINNED_OBJECT);
380 EXPECT_NE(result, 0u);
381 }
382
HWTEST_F_L0(RegionSpaceTest,FeedHungryBuffers_ShouldProvideValidRegions)383 HWTEST_F_L0(RegionSpaceTest, FeedHungryBuffers_ShouldProvideValidRegions)
384 {
385 RegionSpace& theAllocator = reinterpret_cast<RegionSpace&>(Heap::GetHeap().GetAllocator());
386 uintptr_t addr = theAllocator.AllocOldRegion();
387 ASSERT_NE(addr, 0);
388
389 AllocationBuffer* buffer1 = new (std::nothrow) AllocationBuffer();
390 AllocationBuffer* buffer2 = new (std::nothrow) AllocationBuffer();
391 ASSERT_NE(buffer1, nullptr);
392 ASSERT_NE(buffer2, nullptr);
393
394 RegionDesc* region = RegionDesc::GetRegionDescAt(addr);
395 ASSERT_NE(region, nullptr);
396 region->InitFreeUnits();
397 region->SetRegionType(RegionDesc::RegionType::THREAD_LOCAL_REGION);
398
399 buffer1->SetPreparedRegion(region);
400 buffer2->SetPreparedRegion(region);
401 Heap::GetHeap().GetAllocator().AddHungryBuffer(*buffer1);
402 Heap::GetHeap().GetAllocator().AddHungryBuffer(*buffer2);
403
404 Mutator::GetMutator()->SetMutatorPhase(GCPhase::GC_PHASE_FIX);
405
406 Heap::GetHeap().GetAllocator().FeedHungryBuffers();
407
408 EXPECT_NE(buffer2->GetPreparedRegion(), nullptr);
409 delete buffer1;
410 delete buffer2;
411 }
412
HWTEST_F_L0(RegionSpaceTest,AllocationBuffer_AllocateRawPointerObject_ValidSize_ReturnsNonNull)413 HWTEST_F_L0(RegionSpaceTest, AllocationBuffer_AllocateRawPointerObject_ValidSize_ReturnsNonNull)
414 {
415 RegionSpace& theAllocator = reinterpret_cast<RegionSpace&>(Heap::GetHeap().GetAllocator());
416 uintptr_t addr = theAllocator.AllocOldRegion();
417 ASSERT_NE(addr, 0);
418
419 RegionDesc* region = RegionDesc::GetRegionDescAt(addr);
420 ASSERT_NE(region, nullptr);
421 region->InitFreeUnits();
422 region->SetRegionType(RegionDesc::RegionType::THREAD_LOCAL_REGION);
423
424 AllocationBuffer* buffer = new (std::nothrow) AllocationBuffer();
425 ASSERT_NE(buffer, nullptr);
426 buffer->SetPreparedRegion(region);
427
428 Mutator::GetMutator()->SetMutatorPhase(GCPhase::GC_PHASE_UNDEF);
429 Heap::GetHeap().EnableGC(true);
430 Heap::GetHeap().GetCollectorResources().SetGcStarted(false);
431
432 uintptr_t result = theAllocator.Allocate(16, AllocType::PINNED_OBJECT);
433 EXPECT_NE(result, 0u);
434 delete buffer;
435 }
436 }
437