blob: 7f15776d6f007797d4510803a8bda8575019a9bc [file] [log] [blame]
Reid Klecknerc2d882d2009-07-23 18:34:13 +00001//===- 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 Klecknerc2d882d2009-07-23 18:34:13 +000011#include "gtest/gtest.h"
Reid Kleckner4b1f2f42009-07-25 21:26:02 +000012#include <cstdlib>
Reid Klecknerc2d882d2009-07-23 18:34:13 +000013
14using namespace llvm;
15
16namespace {
17
18TEST(AllocatorTest, Basics) {
19 BumpPtrAllocator Alloc;
Hans Wennborgfd1f0f12014-08-19 23:35:33 +000020 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 Klecknerc2d882d2009-07-23 18:34:13 +000023 *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 Carruth0e31ed92014-04-16 10:48:27 +000032
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 Klecknerc2d882d2009-07-23 18:34:13 +000047}
48
49// Allocate enough bytes to create three slabs.
50TEST(AllocatorTest, ThreeSlabs) {
Chandler Carrutha05a2212014-03-30 11:36:32 +000051 BumpPtrAllocator Alloc;
Hans Wennborgfd1f0f12014-08-19 23:35:33 +000052 Alloc.Allocate(3000, 1);
Reid Klecknerc2d882d2009-07-23 18:34:13 +000053 EXPECT_EQ(1U, Alloc.GetNumSlabs());
Hans Wennborgfd1f0f12014-08-19 23:35:33 +000054 Alloc.Allocate(3000, 1);
Reid Klecknerc2d882d2009-07-23 18:34:13 +000055 EXPECT_EQ(2U, Alloc.GetNumSlabs());
Hans Wennborgfd1f0f12014-08-19 23:35:33 +000056 Alloc.Allocate(3000, 1);
Reid Klecknerc2d882d2009-07-23 18:34:13 +000057 EXPECT_EQ(3U, Alloc.GetNumSlabs());
58}
59
60// Allocate enough bytes to create two slabs, reset the allocator, and do it
61// again.
62TEST(AllocatorTest, TestReset) {
Chandler Carrutha05a2212014-03-30 11:36:32 +000063 BumpPtrAllocator Alloc;
Hans Wennborgfd1f0f12014-08-19 23:35:33 +000064 Alloc.Allocate(3000, 1);
Reid Klecknerc2d882d2009-07-23 18:34:13 +000065 EXPECT_EQ(1U, Alloc.GetNumSlabs());
Hans Wennborgfd1f0f12014-08-19 23:35:33 +000066 Alloc.Allocate(3000, 1);
Reid Klecknerc2d882d2009-07-23 18:34:13 +000067 EXPECT_EQ(2U, Alloc.GetNumSlabs());
68 Alloc.Reset();
69 EXPECT_EQ(1U, Alloc.GetNumSlabs());
Hans Wennborgfd1f0f12014-08-19 23:35:33 +000070 Alloc.Allocate(3000, 1);
Reid Klecknerc2d882d2009-07-23 18:34:13 +000071 EXPECT_EQ(1U, Alloc.GetNumSlabs());
Hans Wennborgfd1f0f12014-08-19 23:35:33 +000072 Alloc.Allocate(3000, 1);
Reid Klecknerc2d882d2009-07-23 18:34:13 +000073 EXPECT_EQ(2U, Alloc.GetNumSlabs());
74}
75
76// Test some allocations at varying alignments.
77TEST(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.
98TEST(AllocatorTest, TestOverflow) {
Chandler Carrutha05a2212014-03-30 11:36:32 +000099 BumpPtrAllocator Alloc;
Reid Klecknerc2d882d2009-07-23 18:34:13 +0000100
101 // Fill the slab right up until the end pointer.
Hans Wennborgfd1f0f12014-08-19 23:35:33 +0000102 Alloc.Allocate(4096, 1);
Reid Klecknerc2d882d2009-07-23 18:34:13 +0000103 EXPECT_EQ(1U, Alloc.GetNumSlabs());
104
Dan Gohman01b443f2010-03-01 17:51:02 +0000105 // If we don't allocate a new slab, then we will have overflowed.
Hans Wennborgfd1f0f12014-08-19 23:35:33 +0000106 Alloc.Allocate(1, 1);
Reid Klecknerc2d882d2009-07-23 18:34:13 +0000107 EXPECT_EQ(2U, Alloc.GetNumSlabs());
108}
109
Argyrios Kyrtzidis16558f42012-03-01 20:36:32 +0000110// Test allocating with a size larger than the initial slab size.
111TEST(AllocatorTest, TestSmallSlabSize) {
Chandler Carrutha05a2212014-03-30 11:36:32 +0000112 BumpPtrAllocator Alloc;
Argyrios Kyrtzidis16558f42012-03-01 20:36:32 +0000113
Hans Wennborgfd1f0f12014-08-19 23:35:33 +0000114 Alloc.Allocate(8000, 1);
Hans Wennborgd47b1d72014-08-17 18:31:18 +0000115 EXPECT_EQ(1U, Alloc.GetNumSlabs());
Argyrios Kyrtzidis16558f42012-03-01 20:36:32 +0000116}
117
Hans Wennborg44e27462014-09-07 04:24:31 +0000118// Test requesting alignment that goes past the end of the current slab.
119TEST(AllocatorTest, TestAlignmentPastSlab) {
120 BumpPtrAllocator Alloc;
Hans Wennborge5a96a52014-09-07 05:14:29 +0000121 Alloc.Allocate(4095, 1);
Hans Wennborg44e27462014-09-07 04:24:31 +0000122
Hans Wennborge5a96a52014-09-07 05:14:29 +0000123 // Aligning the current slab pointer is likely to move it past the end of the
124 // slab, which would confuse any unsigned comparisons with the difference of
125 // the the end pointer and the aligned pointer.
Hans Wennborg44e27462014-09-07 04:24:31 +0000126 Alloc.Allocate(1024, 8192);
127
128 EXPECT_EQ(2U, Alloc.GetNumSlabs());
129}
130
Reid Kleckner4b1f2f42009-07-25 21:26:02 +0000131// Mock slab allocator that returns slabs aligned on 4096 bytes. There is no
132// easy portable way to do this, so this is kind of a hack.
Chandler Carrutheed34662014-04-14 05:11:27 +0000133class MockSlabAllocator {
134 static size_t LastSlabSize;
Reid Kleckner4b1f2f42009-07-25 21:26:02 +0000135
136public:
Chandler Carrutheed34662014-04-14 05:11:27 +0000137 ~MockSlabAllocator() { }
Reid Kleckner4b1f2f42009-07-25 21:26:02 +0000138
Chandler Carruth785a9222014-04-15 09:44:09 +0000139 void *Allocate(size_t Size, size_t /*Alignment*/) {
Reid Kleckner4b1f2f42009-07-25 21:26:02 +0000140 // Allocate space for the alignment, the slab, and a void* that goes right
141 // before the slab.
142 size_t Alignment = 4096;
143 void *MemBase = malloc(Size + Alignment - 1 + sizeof(void*));
144
Chandler Carruthf5babf92014-04-14 03:55:11 +0000145 // Find the slab start.
Hans Wennborg3b84f592014-09-02 21:51:35 +0000146 void *Slab = (void *)alignAddr((char*)MemBase + sizeof(void *), Alignment);
Reid Kleckner4b1f2f42009-07-25 21:26:02 +0000147
148 // Hold a pointer to the base so we can free the whole malloced block.
149 ((void**)Slab)[-1] = MemBase;
150
Chandler Carruthf5babf92014-04-14 03:55:11 +0000151 LastSlabSize = Size;
Reid Kleckner4b1f2f42009-07-25 21:26:02 +0000152 return Slab;
153 }
154
Chandler Carrutheed34662014-04-14 05:11:27 +0000155 void Deallocate(void *Slab, size_t Size) {
Reid Kleckner4b1f2f42009-07-25 21:26:02 +0000156 free(((void**)Slab)[-1]);
157 }
158
Chandler Carrutheed34662014-04-14 05:11:27 +0000159 static size_t GetLastSlabSize() { return LastSlabSize; }
Reid Kleckner4b1f2f42009-07-25 21:26:02 +0000160};
161
Chandler Carrutheed34662014-04-14 05:11:27 +0000162size_t MockSlabAllocator::LastSlabSize = 0;
163
Reid Kleckner4b1f2f42009-07-25 21:26:02 +0000164// Allocate a large-ish block with a really large alignment so that the
165// allocator will think that it has space, but after it does the alignment it
166// will not.
167TEST(AllocatorTest, TestBigAlignment) {
Chandler Carrutheed34662014-04-14 05:11:27 +0000168 BumpPtrAllocatorImpl<MockSlabAllocator> Alloc;
Chandler Carruthf5babf92014-04-14 03:55:11 +0000169
170 // First allocate a tiny bit to ensure we have to re-align things.
Hans Wennborgfd1f0f12014-08-19 23:35:33 +0000171 (void)Alloc.Allocate(1, 1);
Chandler Carruthf5babf92014-04-14 03:55:11 +0000172
173 // Now the big chunk with a big alignment.
174 (void)Alloc.Allocate(3000, 2048);
175
176 // We test that the last slab size is not the default 4096 byte slab, but
177 // rather a custom sized slab that is larger.
Chandler Carrutheed34662014-04-14 05:11:27 +0000178 EXPECT_GT(MockSlabAllocator::GetLastSlabSize(), 4096u);
Reid Kleckner4b1f2f42009-07-25 21:26:02 +0000179}
180
Reid Klecknerc2d882d2009-07-23 18:34:13 +0000181} // anonymous namespace