• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 
17 #include "ecmascript/ecma_vm.h"
18 
19 #include "ecmascript/mem/mem_map_allocator.h"
20 #include "ecmascript/mem/mem_common.h"
21 #include "ecmascript/tests/test_helper.h"
22 
23 using namespace panda::ecmascript;
24 using namespace panda::ecmascript::base;
25 
26 namespace panda::test {
27 
28 constexpr size_t HUGE_OBJECT_CAPACITY = 1024_MB;
29 
30 class MemMapAllocatorTest : public testing::Test {
31 public:
SetUpTestCase()32     static void SetUpTestCase()
33     {
34         GTEST_LOG_(INFO) << "SetUpTestCase";
35     }
36 
TearDownTestCase()37     static void TearDownTestCase()
38     {
39         GTEST_LOG_(INFO) << "TearDownCase";
40     }
41 
SetUp()42     void SetUp() override
43     {
44         TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
45     }
46 
TearDown()47     void TearDown() override
48     {
49         TestHelper::DestroyEcmaVMWithScope(instance, scope);
50     }
51 
52     EcmaVM *instance {nullptr};
53     EcmaHandleScope *scope {nullptr};
54     JSThread *thread {nullptr};
55 };
56 
HWTEST_F_L0(MemMapAllocatorTest,GetMemFromList)57 HWTEST_F_L0(MemMapAllocatorTest, GetMemFromList)
58 {
59     MemMap memMap = PageMap(HUGE_OBJECT_CAPACITY, PAGE_PROT_NONE, DEFAULT_REGION_SIZE);
60     PageRelease(memMap.GetMem(), memMap.GetSize());
61     MemMapFreeList memMapFreeList;
62     memMapFreeList.Initialize(memMap, memMap.GetSize() * 2);
63 
64     // From FreeList
65     size_t size1 = 256 * 1024 * 1024;
66     auto mem1 = memMapFreeList.GetMemFromList(size1);
67     EXPECT_EQ(mem1.GetSize(), size1);
68 
69     // From FreeList
70     size_t size2 = 512 * 1024 * 1024;
71     auto mem2 = memMapFreeList.GetMemFromList(size2);
72     EXPECT_EQ(mem2.GetSize(), size2);
73 
74     // From PageMap
75     size_t size3 = 512 * 1024 * 1024;
76     auto mem3 = memMapFreeList.GetMemFromList(size3);
77     EXPECT_EQ(mem3.GetSize(), size3);
78 
79     memMapFreeList.AddMemToList(mem3);
80     memMapFreeList.Finalize();
81 }
82 
83 }  // namespace panda::test
84