xref: /aosp_15_r20/external/angle/src/common/PoolAlloc_unittest.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1 //
2 // Copyright 2019 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // PoolAlloc_unittest:
7 //   Tests of the PoolAlloc class
8 //
9 
10 #include <gtest/gtest.h>
11 
12 #include "common/PoolAlloc.h"
13 
14 namespace angle
15 {
16 // Verify the public interface of PoolAllocator class
TEST(PoolAllocatorTest,Interface)17 TEST(PoolAllocatorTest, Interface)
18 {
19     size_t numBytes               = 1024;
20     constexpr uint32_t kTestValue = 0xbaadbeef;
21     // Create a default pool allocator and allocate from it
22     PoolAllocator poolAllocator;
23     void *allocation = poolAllocator.allocate(numBytes);
24     // Verify non-zero ptr returned
25     EXPECT_NE(nullptr, allocation);
26     // Write to allocation to check later
27     uint32_t *writePtr = static_cast<uint32_t *>(allocation);
28     *writePtr          = kTestValue;
29     // Test push and creating a new allocation
30     poolAllocator.push();
31     allocation = poolAllocator.allocate(numBytes);
32     EXPECT_NE(nullptr, allocation);
33     // Make an allocation that spans multiple pages
34     allocation = poolAllocator.allocate(10 * 1024);
35     // pop previous two allocations
36     poolAllocator.pop();
37     // Verify first allocation still has data
38     EXPECT_EQ(kTestValue, *writePtr);
39     // Make a bunch of allocations
40     for (uint32_t i = 0; i < 1000; ++i)
41     {
42         numBytes   = (rand() % (1024 * 4)) + 1;
43         allocation = poolAllocator.allocate(numBytes);
44         EXPECT_NE(nullptr, allocation);
45         // Write data into full allocation. In debug case if we
46         //  overwrite any other allocation we get error.
47         memset(allocation, 0xb8, numBytes);
48     }
49     // Free everything
50     poolAllocator.popAll();
51 }
52 
53 #if !defined(ANGLE_POOL_ALLOC_GUARD_BLOCKS)
54 // Verify allocations are correctly aligned for different alignments
55 class PoolAllocatorAlignmentTest : public testing::TestWithParam<int>
56 {};
57 
TEST_P(PoolAllocatorAlignmentTest,Alignment)58 TEST_P(PoolAllocatorAlignmentTest, Alignment)
59 {
60     int alignment = GetParam();
61     // Create a pool allocator to allocate from
62     PoolAllocator poolAllocator(4096, alignment);
63     // Test a number of allocation sizes for each alignment
64     for (uint32_t i = 0; i < 100; ++i)
65     {
66         // Vary the allocation size around 4k to hit some multi-page allocations
67         const size_t numBytes = rand() % (1024 * 4) + 1;
68         void *allocation      = poolAllocator.allocate(numBytes);
69         // Verify alignment of allocation matches expected default
70         EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(allocation) % alignment)
71             << "Iteration " << i << " allocating " << numBytes;
72     }
73 }
74 
75 INSTANTIATE_TEST_SUITE_P(,
76                          PoolAllocatorAlignmentTest,
77                          testing::Values(2, 4, 8, 16, 32, 64, 128),
78                          testing::PrintToStringParamName());
79 #endif
80 }  // namespace angle
81