BumpPtrAllocator: do the size check without moving any pointers
[oota-llvm.git] / unittests / Support / AllocatorTest.cpp
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"
11 #include "gtest/gtest.h"
12 #include <cstdlib>
13
14 using namespace llvm;
15
16 namespace {
17
18 TEST(AllocatorTest, Basics) {
19   BumpPtrAllocator Alloc;
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);
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());
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());
47 }
48
49 // Allocate enough bytes to create three slabs.
50 TEST(AllocatorTest, ThreeSlabs) {
51   BumpPtrAllocator Alloc;
52   Alloc.Allocate(3000, 1);
53   EXPECT_EQ(1U, Alloc.GetNumSlabs());
54   Alloc.Allocate(3000, 1);
55   EXPECT_EQ(2U, Alloc.GetNumSlabs());
56   Alloc.Allocate(3000, 1);
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) {
63   BumpPtrAllocator Alloc;
64   Alloc.Allocate(3000, 1);
65   EXPECT_EQ(1U, Alloc.GetNumSlabs());
66   Alloc.Allocate(3000, 1);
67   EXPECT_EQ(2U, Alloc.GetNumSlabs());
68   Alloc.Reset();
69   EXPECT_EQ(1U, Alloc.GetNumSlabs());
70   Alloc.Allocate(3000, 1);
71   EXPECT_EQ(1U, Alloc.GetNumSlabs());
72   Alloc.Allocate(3000, 1);
73   EXPECT_EQ(2U, Alloc.GetNumSlabs());
74 }
75
76 // Test some allocations at varying alignments.
77 TEST(AllocatorTest, TestAlignment) {
78   BumpPtrAllocator Alloc;
79   uintptr_t a;
80   a = (uintptr_t)Alloc.Allocate(1, 2);
81   EXPECT_EQ(0U, a & 1);
82   a = (uintptr_t)Alloc.Allocate(1, 4);
83   EXPECT_EQ(0U, a & 3);
84   a = (uintptr_t)Alloc.Allocate(1, 8);
85   EXPECT_EQ(0U, a & 7);
86   a = (uintptr_t)Alloc.Allocate(1, 16);
87   EXPECT_EQ(0U, a & 15);
88   a = (uintptr_t)Alloc.Allocate(1, 32);
89   EXPECT_EQ(0U, a & 31);
90   a = (uintptr_t)Alloc.Allocate(1, 64);
91   EXPECT_EQ(0U, a & 63);
92   a = (uintptr_t)Alloc.Allocate(1, 128);
93   EXPECT_EQ(0U, a & 127);
94 }
95
96 // Test allocating just over the slab size.  This tests a bug where before the
97 // allocator incorrectly calculated the buffer end pointer.
98 TEST(AllocatorTest, TestOverflow) {
99   BumpPtrAllocator Alloc;
100
101   // Fill the slab right up until the end pointer.
102   Alloc.Allocate(4096, 1);
103   EXPECT_EQ(1U, Alloc.GetNumSlabs());
104
105   // If we don't allocate a new slab, then we will have overflowed.
106   Alloc.Allocate(1, 1);
107   EXPECT_EQ(2U, Alloc.GetNumSlabs());
108 }
109
110 // Test allocating with a size larger than the initial slab size.
111 TEST(AllocatorTest, TestSmallSlabSize) {
112   BumpPtrAllocator Alloc;
113
114   Alloc.Allocate(8000, 1);
115   EXPECT_EQ(1U, Alloc.GetNumSlabs());
116 }
117
118 // Test requesting alignment that goes past the end of the current slab.
119 TEST(AllocatorTest, TestAlignmentPastSlab) {
120   BumpPtrAllocator Alloc;
121   Alloc.Allocate(1234, 1);
122
123   // Any attempt to align the pointer in the current slab would move it beyond
124   // the end of that slab.
125   Alloc.Allocate(1024, 8192);
126
127   EXPECT_EQ(2U, Alloc.GetNumSlabs());
128 }
129
130 // Mock slab allocator that returns slabs aligned on 4096 bytes.  There is no
131 // easy portable way to do this, so this is kind of a hack.
132 class MockSlabAllocator {
133   static size_t LastSlabSize;
134
135 public:
136   ~MockSlabAllocator() { }
137
138   void *Allocate(size_t Size, size_t /*Alignment*/) {
139     // Allocate space for the alignment, the slab, and a void* that goes right
140     // before the slab.
141     size_t Alignment = 4096;
142     void *MemBase = malloc(Size + Alignment - 1 + sizeof(void*));
143
144     // Find the slab start.
145     void *Slab = (void *)alignAddr((char*)MemBase + sizeof(void *), Alignment);
146
147     // Hold a pointer to the base so we can free the whole malloced block.
148     ((void**)Slab)[-1] = MemBase;
149
150     LastSlabSize = Size;
151     return Slab;
152   }
153
154   void Deallocate(void *Slab, size_t Size) {
155     free(((void**)Slab)[-1]);
156   }
157
158   static size_t GetLastSlabSize() { return LastSlabSize; }
159 };
160
161 size_t MockSlabAllocator::LastSlabSize = 0;
162
163 // Allocate a large-ish block with a really large alignment so that the
164 // allocator will think that it has space, but after it does the alignment it
165 // will not.
166 TEST(AllocatorTest, TestBigAlignment) {
167   BumpPtrAllocatorImpl<MockSlabAllocator> Alloc;
168
169   // First allocate a tiny bit to ensure we have to re-align things.
170   (void)Alloc.Allocate(1, 1);
171
172   // Now the big chunk with a big alignment.
173   (void)Alloc.Allocate(3000, 2048);
174
175   // We test that the last slab size is not the default 4096 byte slab, but
176   // rather a custom sized slab that is larger.
177   EXPECT_GT(MockSlabAllocator::GetLastSlabSize(), 4096u);
178 }
179
180 }  // anonymous namespace