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