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