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