• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2024 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 <sys/mman.h>
17 
18 #include <array>
19 
20 #include "libpandabase/utils/utils.h"
21 #include "runtime/mem/malloc-proxy-allocator-inl.h"
22 #include "runtime/tests/allocator_test_base.h"
23 
24 namespace ark::mem {
25 
26 using MallocProxyNonObjectAllocator = MallocProxyAllocator<EmptyAllocConfigWithCrossingMap>;
27 
28 class MallocProxyAllocatorTest : public AllocatorTest<MallocProxyNonObjectAllocator> {
29 public:
30     NO_COPY_SEMANTIC(MallocProxyAllocatorTest);
31     NO_MOVE_SEMANTIC(MallocProxyAllocatorTest);
32 
33     // NOLINTNEXTLINE(modernize-use-equals-default)
MallocProxyAllocatorTest()34     MallocProxyAllocatorTest()
35     {
36         // Logger::InitializeStdLogging(Logger::Level::DEBUG, Logger::Component::ALL);
37     }
38 
39     // NOLINTNEXTLINE(modernize-use-equals-default)
~MallocProxyAllocatorTest()40     ~MallocProxyAllocatorTest() override
41     {
42         // Logger::Destroy();
43     }
44 
45 protected:
46     static constexpr size_t SIZE_ALLOC = 1_KB;
47 
AddMemoryPoolToAllocator(MallocProxyNonObjectAllocator & allocator)48     void AddMemoryPoolToAllocator([[maybe_unused]] MallocProxyNonObjectAllocator &allocator) override {}
AddMemoryPoolToAllocatorProtected(MallocProxyNonObjectAllocator & allocator)49     void AddMemoryPoolToAllocatorProtected([[maybe_unused]] MallocProxyNonObjectAllocator &allocator) override {}
AllocatedByThisAllocator(MallocProxyNonObjectAllocator & allocator,void * mem)50     bool AllocatedByThisAllocator([[maybe_unused]] MallocProxyNonObjectAllocator &allocator,
51                                   [[maybe_unused]] void *mem) override
52     {
53         return false;
54     }
55 };
56 
TEST_F(MallocProxyAllocatorTest,SimpleTest)57 TEST_F(MallocProxyAllocatorTest, SimpleTest)
58 {
59     static constexpr size_t SIZE = 23;
60     auto *memStats = new mem::MemStatsType();
61     MallocProxyNonObjectAllocator allocator(memStats);
62     void *a1 = allocator.Alloc(SIZE);
63     allocator.Free(a1);
64     delete memStats;
65 }
66 
TEST_F(MallocProxyAllocatorTest,AlignedAllocFreeTest)67 TEST_F(MallocProxyAllocatorTest, AlignedAllocFreeTest)
68 {
69     AlignedAllocFreeTest<1, SIZE_ALLOC>();
70 }
71 
TEST_F(MallocProxyAllocatorTest,AllocFreeTest)72 TEST_F(MallocProxyAllocatorTest, AllocFreeTest)
73 {
74     static constexpr size_t POOLS_COUNT = 1;
75     AllocateFreeDifferentSizesTest<1, 4U * SIZE_ALLOC>(4U * SIZE_ALLOC, POOLS_COUNT);
76 }
77 
TEST_F(MallocProxyAllocatorTest,AdapterTest)78 TEST_F(MallocProxyAllocatorTest, AdapterTest)
79 {
80     auto *memStats = new mem::MemStatsType();
81     MallocProxyNonObjectAllocator allocator(memStats);
82     // NOLINTBEGIN(readability-magic-numbers)
83     std::array<int, 20U> arr {{12_I, 14_I, 3_I,  5_I,  43_I, 12_I, 22_I, 42_I, 89_I, 10_I,
84                                89_I, 32_I, 43_I, 12_I, 43_I, 12_I, 54_I, 89_I, 27_I, 84_I}};
85     // NOLINTEND(readability-magic-numbers)
86 
87     std::vector<void *> v;
88     for (auto i : arr) {
89         auto *mem = allocator.Alloc(i);
90         v.push_back(mem);
91     }
92     for (auto *mem : v) {
93         allocator.Free(mem);
94     }
95     delete memStats;
96 }
97 
98 }  // namespace ark::mem
99