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 #include "common_components/heap/allocator/region_desc.h"
18 #include "common_components/heap/allocator/region_space.h"
19 #include "common_components/tests/test_helper.h"
20 #include "common_interfaces/base_runtime.h"
21
22 using namespace common;
23
24 namespace common::test {
25 class HeapAllocatorTest : public BaseTestWithScope {
26 protected:
SetUpTestCase()27 static void SetUpTestCase()
28 {
29 BaseRuntime::GetInstance()->Init();
30 }
31
TearDownTestCase()32 static void TearDownTestCase()
33 {
34 BaseRuntime::GetInstance()->Fini();
35 }
36
SetUp()37 void SetUp() override
38 {
39 holder_ = ThreadHolder::CreateAndRegisterNewThreadHolder(nullptr);
40 scope_ = new ThreadHolder::TryBindMutatorScope(holder_);
41 }
42
TearDown()43 void TearDown() override
44 {
45 if (scope_ != nullptr) {
46 delete scope_;
47 scope_ = nullptr;
48 }
49 }
50
51 ThreadHolder *holder_ {nullptr};
52 ThreadHolder::TryBindMutatorScope *scope_ {nullptr};
53 };
54
HWTEST_F_L0(HeapAllocatorTest,AllocLargeObject)55 HWTEST_F_L0(HeapAllocatorTest, AllocLargeObject)
56 {
57 uintptr_t addr = common::HeapAllocator::AllocateInHuge(Heap::NORMAL_UNIT_SIZE, common::LanguageType::DYNAMIC);
58 ASSERT(addr > 0);
59 RegionDesc* region = RegionDesc::GetAliveRegionDescAt(addr);
60 ASSERT(region->IsLargeRegion());
61 }
62
HWTEST_F_L0(HeapAllocatorTest,AllocLargeRegion)63 HWTEST_F_L0(HeapAllocatorTest, AllocLargeRegion)
64 {
65 uintptr_t addr = common::HeapAllocator::AllocateLargeRegion(Heap::NORMAL_UNIT_SIZE);
66 ASSERT(addr > 0);
67 RegionDesc *region = RegionDesc::GetAliveRegionDescAt(addr);
68 ASSERT(region->IsLargeRegion());
69 }
70 }