Reid Kleckner | c2d882d | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/Support/AllocatorTest.cpp - BumpPtrAllocator tests ---===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Reid Kleckner | c2d882d | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "llvm/Support/Allocator.h" |
Reid Kleckner | c2d882d | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 10 | #include "gtest/gtest.h" |
Reid Kleckner | 4b1f2f4 | 2009-07-25 21:26:02 +0000 | [diff] [blame] | 11 | #include <cstdlib> |
Reid Kleckner | c2d882d | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 12 | |
| 13 | using namespace llvm; |
| 14 | |
| 15 | namespace { |
| 16 | |
| 17 | TEST(AllocatorTest, Basics) { |
| 18 | BumpPtrAllocator Alloc; |
Jordan Rose | ca9ca54 | 2017-03-11 01:24:56 +0000 | [diff] [blame] | 19 | int *a = (int*)Alloc.Allocate(sizeof(int), alignof(int)); |
| 20 | int *b = (int*)Alloc.Allocate(sizeof(int) * 10, alignof(int)); |
| 21 | int *c = (int*)Alloc.Allocate(sizeof(int), alignof(int)); |
Reid Kleckner | c2d882d | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 22 | *a = 1; |
| 23 | b[0] = 2; |
| 24 | b[9] = 2; |
| 25 | *c = 3; |
| 26 | EXPECT_EQ(1, *a); |
| 27 | EXPECT_EQ(2, b[0]); |
| 28 | EXPECT_EQ(2, b[9]); |
| 29 | EXPECT_EQ(3, *c); |
| 30 | EXPECT_EQ(1U, Alloc.GetNumSlabs()); |
Chandler Carruth | 0e31ed9 | 2014-04-16 10:48:27 +0000 | [diff] [blame] | 31 | |
| 32 | BumpPtrAllocator Alloc2 = std::move(Alloc); |
| 33 | EXPECT_EQ(0U, Alloc.GetNumSlabs()); |
| 34 | EXPECT_EQ(1U, Alloc2.GetNumSlabs()); |
| 35 | |
| 36 | // Make sure the old pointers still work. These are especially interesting |
| 37 | // under ASan or Valgrind. |
| 38 | EXPECT_EQ(1, *a); |
| 39 | EXPECT_EQ(2, b[0]); |
| 40 | EXPECT_EQ(2, b[9]); |
| 41 | EXPECT_EQ(3, *c); |
| 42 | |
| 43 | Alloc = std::move(Alloc2); |
| 44 | EXPECT_EQ(0U, Alloc2.GetNumSlabs()); |
| 45 | EXPECT_EQ(1U, Alloc.GetNumSlabs()); |
Reid Kleckner | c2d882d | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | // Allocate enough bytes to create three slabs. |
| 49 | TEST(AllocatorTest, ThreeSlabs) { |
Chandler Carruth | a05a221 | 2014-03-30 11:36:32 +0000 | [diff] [blame] | 50 | BumpPtrAllocator Alloc; |
Hans Wennborg | fd1f0f1 | 2014-08-19 23:35:33 +0000 | [diff] [blame] | 51 | Alloc.Allocate(3000, 1); |
Reid Kleckner | c2d882d | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 52 | EXPECT_EQ(1U, Alloc.GetNumSlabs()); |
Hans Wennborg | fd1f0f1 | 2014-08-19 23:35:33 +0000 | [diff] [blame] | 53 | Alloc.Allocate(3000, 1); |
Reid Kleckner | c2d882d | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 54 | EXPECT_EQ(2U, Alloc.GetNumSlabs()); |
Hans Wennborg | fd1f0f1 | 2014-08-19 23:35:33 +0000 | [diff] [blame] | 55 | Alloc.Allocate(3000, 1); |
Reid Kleckner | c2d882d | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 56 | EXPECT_EQ(3U, Alloc.GetNumSlabs()); |
| 57 | } |
| 58 | |
| 59 | // Allocate enough bytes to create two slabs, reset the allocator, and do it |
| 60 | // again. |
| 61 | TEST(AllocatorTest, TestReset) { |
Chandler Carruth | a05a221 | 2014-03-30 11:36:32 +0000 | [diff] [blame] | 62 | BumpPtrAllocator Alloc; |
Hans Wennborg | da5ddff7 | 2015-05-18 16:54:17 +0000 | [diff] [blame] | 63 | |
| 64 | // Allocate something larger than the SizeThreshold=4096. |
| 65 | (void)Alloc.Allocate(5000, 1); |
| 66 | Alloc.Reset(); |
| 67 | // Calling Reset should free all CustomSizedSlabs. |
| 68 | EXPECT_EQ(0u, Alloc.GetNumSlabs()); |
| 69 | |
Hans Wennborg | fd1f0f1 | 2014-08-19 23:35:33 +0000 | [diff] [blame] | 70 | Alloc.Allocate(3000, 1); |
Reid Kleckner | c2d882d | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 71 | EXPECT_EQ(1U, Alloc.GetNumSlabs()); |
Hans Wennborg | fd1f0f1 | 2014-08-19 23:35:33 +0000 | [diff] [blame] | 72 | Alloc.Allocate(3000, 1); |
Reid Kleckner | c2d882d | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 73 | EXPECT_EQ(2U, Alloc.GetNumSlabs()); |
| 74 | Alloc.Reset(); |
| 75 | EXPECT_EQ(1U, Alloc.GetNumSlabs()); |
Hans Wennborg | fd1f0f1 | 2014-08-19 23:35:33 +0000 | [diff] [blame] | 76 | Alloc.Allocate(3000, 1); |
Reid Kleckner | c2d882d | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 77 | EXPECT_EQ(1U, Alloc.GetNumSlabs()); |
Hans Wennborg | fd1f0f1 | 2014-08-19 23:35:33 +0000 | [diff] [blame] | 78 | Alloc.Allocate(3000, 1); |
Reid Kleckner | c2d882d | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 79 | EXPECT_EQ(2U, Alloc.GetNumSlabs()); |
| 80 | } |
| 81 | |
| 82 | // Test some allocations at varying alignments. |
| 83 | TEST(AllocatorTest, TestAlignment) { |
| 84 | BumpPtrAllocator Alloc; |
| 85 | uintptr_t a; |
| 86 | a = (uintptr_t)Alloc.Allocate(1, 2); |
| 87 | EXPECT_EQ(0U, a & 1); |
| 88 | a = (uintptr_t)Alloc.Allocate(1, 4); |
| 89 | EXPECT_EQ(0U, a & 3); |
| 90 | a = (uintptr_t)Alloc.Allocate(1, 8); |
| 91 | EXPECT_EQ(0U, a & 7); |
| 92 | a = (uintptr_t)Alloc.Allocate(1, 16); |
| 93 | EXPECT_EQ(0U, a & 15); |
| 94 | a = (uintptr_t)Alloc.Allocate(1, 32); |
| 95 | EXPECT_EQ(0U, a & 31); |
| 96 | a = (uintptr_t)Alloc.Allocate(1, 64); |
| 97 | EXPECT_EQ(0U, a & 63); |
| 98 | a = (uintptr_t)Alloc.Allocate(1, 128); |
| 99 | EXPECT_EQ(0U, a & 127); |
| 100 | } |
| 101 | |
| 102 | // Test allocating just over the slab size. This tests a bug where before the |
| 103 | // allocator incorrectly calculated the buffer end pointer. |
| 104 | TEST(AllocatorTest, TestOverflow) { |
Chandler Carruth | a05a221 | 2014-03-30 11:36:32 +0000 | [diff] [blame] | 105 | BumpPtrAllocator Alloc; |
Reid Kleckner | c2d882d | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 106 | |
| 107 | // Fill the slab right up until the end pointer. |
Hans Wennborg | fd1f0f1 | 2014-08-19 23:35:33 +0000 | [diff] [blame] | 108 | Alloc.Allocate(4096, 1); |
Reid Kleckner | c2d882d | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 109 | EXPECT_EQ(1U, Alloc.GetNumSlabs()); |
| 110 | |
Dan Gohman | 01b443f | 2010-03-01 17:51:02 +0000 | [diff] [blame] | 111 | // If we don't allocate a new slab, then we will have overflowed. |
Hans Wennborg | fd1f0f1 | 2014-08-19 23:35:33 +0000 | [diff] [blame] | 112 | Alloc.Allocate(1, 1); |
Reid Kleckner | c2d882d | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 113 | EXPECT_EQ(2U, Alloc.GetNumSlabs()); |
| 114 | } |
| 115 | |
Argyrios Kyrtzidis | 16558f4 | 2012-03-01 20:36:32 +0000 | [diff] [blame] | 116 | // Test allocating with a size larger than the initial slab size. |
| 117 | TEST(AllocatorTest, TestSmallSlabSize) { |
Chandler Carruth | a05a221 | 2014-03-30 11:36:32 +0000 | [diff] [blame] | 118 | BumpPtrAllocator Alloc; |
Argyrios Kyrtzidis | 16558f4 | 2012-03-01 20:36:32 +0000 | [diff] [blame] | 119 | |
Hans Wennborg | fd1f0f1 | 2014-08-19 23:35:33 +0000 | [diff] [blame] | 120 | Alloc.Allocate(8000, 1); |
Hans Wennborg | d47b1d7 | 2014-08-17 18:31:18 +0000 | [diff] [blame] | 121 | EXPECT_EQ(1U, Alloc.GetNumSlabs()); |
Argyrios Kyrtzidis | 16558f4 | 2012-03-01 20:36:32 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Hans Wennborg | 44e2746 | 2014-09-07 04:24:31 +0000 | [diff] [blame] | 124 | // Test requesting alignment that goes past the end of the current slab. |
| 125 | TEST(AllocatorTest, TestAlignmentPastSlab) { |
| 126 | BumpPtrAllocator Alloc; |
Hans Wennborg | e5a96a5 | 2014-09-07 05:14:29 +0000 | [diff] [blame] | 127 | Alloc.Allocate(4095, 1); |
Hans Wennborg | 44e2746 | 2014-09-07 04:24:31 +0000 | [diff] [blame] | 128 | |
Hans Wennborg | e5a96a5 | 2014-09-07 05:14:29 +0000 | [diff] [blame] | 129 | // Aligning the current slab pointer is likely to move it past the end of the |
| 130 | // slab, which would confuse any unsigned comparisons with the difference of |
Eric Christopher | 572e03a | 2015-06-19 01:53:21 +0000 | [diff] [blame] | 131 | // the end pointer and the aligned pointer. |
Hans Wennborg | 44e2746 | 2014-09-07 04:24:31 +0000 | [diff] [blame] | 132 | Alloc.Allocate(1024, 8192); |
| 133 | |
| 134 | EXPECT_EQ(2U, Alloc.GetNumSlabs()); |
| 135 | } |
| 136 | |
Reid Kleckner | 4b1f2f4 | 2009-07-25 21:26:02 +0000 | [diff] [blame] | 137 | // Mock slab allocator that returns slabs aligned on 4096 bytes. There is no |
| 138 | // easy portable way to do this, so this is kind of a hack. |
Chandler Carruth | eed3466 | 2014-04-14 05:11:27 +0000 | [diff] [blame] | 139 | class MockSlabAllocator { |
| 140 | static size_t LastSlabSize; |
Reid Kleckner | 4b1f2f4 | 2009-07-25 21:26:02 +0000 | [diff] [blame] | 141 | |
| 142 | public: |
Chandler Carruth | eed3466 | 2014-04-14 05:11:27 +0000 | [diff] [blame] | 143 | ~MockSlabAllocator() { } |
Reid Kleckner | 4b1f2f4 | 2009-07-25 21:26:02 +0000 | [diff] [blame] | 144 | |
Chandler Carruth | 785a922 | 2014-04-15 09:44:09 +0000 | [diff] [blame] | 145 | void *Allocate(size_t Size, size_t /*Alignment*/) { |
Reid Kleckner | 4b1f2f4 | 2009-07-25 21:26:02 +0000 | [diff] [blame] | 146 | // Allocate space for the alignment, the slab, and a void* that goes right |
| 147 | // before the slab. |
Guillaume Chatelet | ce56e1a | 2019-10-14 13:14:34 +0000 | [diff] [blame] | 148 | Align Alignment(4096); |
| 149 | void *MemBase = safe_malloc(Size + Alignment.value() - 1 + sizeof(void *)); |
Reid Kleckner | 4b1f2f4 | 2009-07-25 21:26:02 +0000 | [diff] [blame] | 150 | |
Chandler Carruth | f5babf9 | 2014-04-14 03:55:11 +0000 | [diff] [blame] | 151 | // Find the slab start. |
Hans Wennborg | 3b84f59 | 2014-09-02 21:51:35 +0000 | [diff] [blame] | 152 | void *Slab = (void *)alignAddr((char*)MemBase + sizeof(void *), Alignment); |
Reid Kleckner | 4b1f2f4 | 2009-07-25 21:26:02 +0000 | [diff] [blame] | 153 | |
| 154 | // Hold a pointer to the base so we can free the whole malloced block. |
| 155 | ((void**)Slab)[-1] = MemBase; |
| 156 | |
Chandler Carruth | f5babf9 | 2014-04-14 03:55:11 +0000 | [diff] [blame] | 157 | LastSlabSize = Size; |
Reid Kleckner | 4b1f2f4 | 2009-07-25 21:26:02 +0000 | [diff] [blame] | 158 | return Slab; |
| 159 | } |
| 160 | |
Chandler Carruth | eed3466 | 2014-04-14 05:11:27 +0000 | [diff] [blame] | 161 | void Deallocate(void *Slab, size_t Size) { |
Reid Kleckner | 4b1f2f4 | 2009-07-25 21:26:02 +0000 | [diff] [blame] | 162 | free(((void**)Slab)[-1]); |
| 163 | } |
| 164 | |
Chandler Carruth | eed3466 | 2014-04-14 05:11:27 +0000 | [diff] [blame] | 165 | static size_t GetLastSlabSize() { return LastSlabSize; } |
Reid Kleckner | 4b1f2f4 | 2009-07-25 21:26:02 +0000 | [diff] [blame] | 166 | }; |
| 167 | |
Chandler Carruth | eed3466 | 2014-04-14 05:11:27 +0000 | [diff] [blame] | 168 | size_t MockSlabAllocator::LastSlabSize = 0; |
| 169 | |
Reid Kleckner | 4b1f2f4 | 2009-07-25 21:26:02 +0000 | [diff] [blame] | 170 | // Allocate a large-ish block with a really large alignment so that the |
| 171 | // allocator will think that it has space, but after it does the alignment it |
| 172 | // will not. |
| 173 | TEST(AllocatorTest, TestBigAlignment) { |
Chandler Carruth | eed3466 | 2014-04-14 05:11:27 +0000 | [diff] [blame] | 174 | BumpPtrAllocatorImpl<MockSlabAllocator> Alloc; |
Chandler Carruth | f5babf9 | 2014-04-14 03:55:11 +0000 | [diff] [blame] | 175 | |
| 176 | // First allocate a tiny bit to ensure we have to re-align things. |
Hans Wennborg | fd1f0f1 | 2014-08-19 23:35:33 +0000 | [diff] [blame] | 177 | (void)Alloc.Allocate(1, 1); |
Chandler Carruth | f5babf9 | 2014-04-14 03:55:11 +0000 | [diff] [blame] | 178 | |
| 179 | // Now the big chunk with a big alignment. |
| 180 | (void)Alloc.Allocate(3000, 2048); |
| 181 | |
| 182 | // We test that the last slab size is not the default 4096 byte slab, but |
| 183 | // rather a custom sized slab that is larger. |
Chandler Carruth | eed3466 | 2014-04-14 05:11:27 +0000 | [diff] [blame] | 184 | EXPECT_GT(MockSlabAllocator::GetLastSlabSize(), 4096u); |
Reid Kleckner | 4b1f2f4 | 2009-07-25 21:26:02 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Reid Kleckner | c2d882d | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 187 | } // anonymous namespace |