• 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 #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:
29     MallocProxyAllocatorTest() = default;
30     ~MallocProxyAllocatorTest() = default;
31 
32 protected:
33     static constexpr size_t SIZE_ALLOC = 1_KB;
34 
AddMemoryPoolToAllocator(MallocProxyNonObjectAllocator & allocator)35     void AddMemoryPoolToAllocator([[maybe_unused]] MallocProxyNonObjectAllocator &allocator) override {}
AddMemoryPoolToAllocatorProtected(MallocProxyNonObjectAllocator & allocator)36     void AddMemoryPoolToAllocatorProtected([[maybe_unused]] MallocProxyNonObjectAllocator &allocator) override {}
AllocatedByThisAllocator(MallocProxyNonObjectAllocator & allocator,void * mem)37     bool AllocatedByThisAllocator([[maybe_unused]] MallocProxyNonObjectAllocator &allocator,
38                                   [[maybe_unused]] void *mem) override
39     {
40         return false;
41     }
42 };
43 
TEST_F(MallocProxyAllocatorTest,SimpleTest)44 TEST_F(MallocProxyAllocatorTest, SimpleTest)
45 {
46     static constexpr size_t SIZE = 23;
47     mem::MemStatsType *mem_stats = new mem::MemStatsType();
48     MallocProxyNonObjectAllocator allocator(mem_stats);
49     void *a1;
50     a1 = allocator.Alloc(SIZE);
51     allocator.Free(a1);
52     delete mem_stats;
53 }
54 
TEST_F(MallocProxyAllocatorTest,AlignedAllocFreeTest)55 TEST_F(MallocProxyAllocatorTest, AlignedAllocFreeTest)
56 {
57     AlignedAllocFreeTest<1, SIZE_ALLOC>();
58 }
59 
TEST_F(MallocProxyAllocatorTest,AllocFreeTest)60 TEST_F(MallocProxyAllocatorTest, AllocFreeTest)
61 {
62     AllocateFreeDifferentSizesTest<1, 4 * SIZE_ALLOC>();
63 }
64 
TEST_F(MallocProxyAllocatorTest,AdapterTest)65 TEST_F(MallocProxyAllocatorTest, AdapterTest)
66 {
67     mem::MemStatsType *mem_stats = new mem::MemStatsType();
68     MallocProxyNonObjectAllocator allocator(mem_stats);
69     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}};
70 
71     std::vector<void *> v;
72     for (auto i : arr) {
73         auto *mem = allocator.Alloc(i);
74         v.push_back(mem);
75     }
76     for (auto *mem : v) {
77         allocator.Free(mem);
78     }
79     delete mem_stats;
80 }
81 
82 }  // namespace panda::mem
83