blob: bc39a149a0b8fec5fa3032dd6bb3be2d68dc04bf [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;
20 int *a = (int*)Alloc.Allocate(sizeof(int), 0);
21 int *b = (int*)Alloc.Allocate(sizeof(int) * 10, 0);
22 int *c = (int*)Alloc.Allocate(sizeof(int), 0);
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
34// Allocate enough bytes to create three slabs.
35TEST(AllocatorTest, ThreeSlabs) {
Chandler Carrutha05a2212014-03-30 11:36:32 +000036 BumpPtrAllocator Alloc;
Reid Klecknerc2d882d2009-07-23 18:34:13 +000037 Alloc.Allocate(3000, 0);
38 EXPECT_EQ(1U, Alloc.GetNumSlabs());
39 Alloc.Allocate(3000, 0);
40 EXPECT_EQ(2U, Alloc.GetNumSlabs());
41 Alloc.Allocate(3000, 0);
42 EXPECT_EQ(3U, Alloc.GetNumSlabs());
43}
44
45// Allocate enough bytes to create two slabs, reset the allocator, and do it
46// again.
47TEST(AllocatorTest, TestReset) {
Chandler Carrutha05a2212014-03-30 11:36:32 +000048 BumpPtrAllocator Alloc;
Reid Klecknerc2d882d2009-07-23 18:34:13 +000049 Alloc.Allocate(3000, 0);
50 EXPECT_EQ(1U, Alloc.GetNumSlabs());
51 Alloc.Allocate(3000, 0);
52 EXPECT_EQ(2U, Alloc.GetNumSlabs());
53 Alloc.Reset();
54 EXPECT_EQ(1U, Alloc.GetNumSlabs());
55 Alloc.Allocate(3000, 0);
56 EXPECT_EQ(1U, Alloc.GetNumSlabs());
57 Alloc.Allocate(3000, 0);
58 EXPECT_EQ(2U, Alloc.GetNumSlabs());
59}
60
61// Test some allocations at varying alignments.
62TEST(AllocatorTest, TestAlignment) {
63 BumpPtrAllocator Alloc;
64 uintptr_t a;
65 a = (uintptr_t)Alloc.Allocate(1, 2);
66 EXPECT_EQ(0U, a & 1);
67 a = (uintptr_t)Alloc.Allocate(1, 4);
68 EXPECT_EQ(0U, a & 3);
69 a = (uintptr_t)Alloc.Allocate(1, 8);
70 EXPECT_EQ(0U, a & 7);
71 a = (uintptr_t)Alloc.Allocate(1, 16);
72 EXPECT_EQ(0U, a & 15);
73 a = (uintptr_t)Alloc.Allocate(1, 32);
74 EXPECT_EQ(0U, a & 31);
75 a = (uintptr_t)Alloc.Allocate(1, 64);
76 EXPECT_EQ(0U, a & 63);
77 a = (uintptr_t)Alloc.Allocate(1, 128);
78 EXPECT_EQ(0U, a & 127);
79}
80
81// Test allocating just over the slab size. This tests a bug where before the
82// allocator incorrectly calculated the buffer end pointer.
83TEST(AllocatorTest, TestOverflow) {
Chandler Carrutha05a2212014-03-30 11:36:32 +000084 BumpPtrAllocator Alloc;
Reid Klecknerc2d882d2009-07-23 18:34:13 +000085
86 // Fill the slab right up until the end pointer.
Chandler Carruthf5babf92014-04-14 03:55:11 +000087 Alloc.Allocate(4096, 0);
Reid Klecknerc2d882d2009-07-23 18:34:13 +000088 EXPECT_EQ(1U, Alloc.GetNumSlabs());
89
Dan Gohman01b443f2010-03-01 17:51:02 +000090 // If we don't allocate a new slab, then we will have overflowed.
Reid Klecknerc2d882d2009-07-23 18:34:13 +000091 Alloc.Allocate(1, 0);
92 EXPECT_EQ(2U, Alloc.GetNumSlabs());
93}
94
Argyrios Kyrtzidis16558f42012-03-01 20:36:32 +000095// Test allocating with a size larger than the initial slab size.
96TEST(AllocatorTest, TestSmallSlabSize) {
Chandler Carrutha05a2212014-03-30 11:36:32 +000097 BumpPtrAllocator Alloc;
Argyrios Kyrtzidis16558f42012-03-01 20:36:32 +000098
Chandler Carrutha05a2212014-03-30 11:36:32 +000099 Alloc.Allocate(8000, 0);
Benjamin Kramerf7e02a02012-03-01 22:10:16 +0000100 EXPECT_EQ(2U, Alloc.GetNumSlabs());
Argyrios Kyrtzidis16558f42012-03-01 20:36:32 +0000101}
102
Reid Kleckner4b1f2f42009-07-25 21:26:02 +0000103// Mock slab allocator that returns slabs aligned on 4096 bytes. There is no
104// easy portable way to do this, so this is kind of a hack.
105class MockSlabAllocator : public SlabAllocator {
Chandler Carruthf5babf92014-04-14 03:55:11 +0000106 size_t LastSlabSize;
Reid Kleckner4b1f2f42009-07-25 21:26:02 +0000107
108public:
109 virtual ~MockSlabAllocator() { }
110
Chandler Carruthf5babf92014-04-14 03:55:11 +0000111 virtual void *Allocate(size_t Size) {
Reid Kleckner4b1f2f42009-07-25 21:26:02 +0000112 // Allocate space for the alignment, the slab, and a void* that goes right
113 // before the slab.
114 size_t Alignment = 4096;
115 void *MemBase = malloc(Size + Alignment - 1 + sizeof(void*));
116
Chandler Carruthf5babf92014-04-14 03:55:11 +0000117 // Find the slab start.
118 void *Slab = alignPtr((char *)MemBase + sizeof(void *), Alignment);
Reid Kleckner4b1f2f42009-07-25 21:26:02 +0000119
120 // Hold a pointer to the base so we can free the whole malloced block.
121 ((void**)Slab)[-1] = MemBase;
122
Chandler Carruthf5babf92014-04-14 03:55:11 +0000123 LastSlabSize = Size;
Reid Kleckner4b1f2f42009-07-25 21:26:02 +0000124 return Slab;
125 }
126
Chandler Carruthf5babf92014-04-14 03:55:11 +0000127 virtual void Deallocate(void *Slab, size_t Size) {
Reid Kleckner4b1f2f42009-07-25 21:26:02 +0000128 free(((void**)Slab)[-1]);
129 }
130
Chandler Carruthf5babf92014-04-14 03:55:11 +0000131 size_t GetLastSlabSize() { return LastSlabSize; }
Reid Kleckner4b1f2f42009-07-25 21:26:02 +0000132};
133
134// Allocate a large-ish block with a really large alignment so that the
135// allocator will think that it has space, but after it does the alignment it
136// will not.
137TEST(AllocatorTest, TestBigAlignment) {
138 MockSlabAllocator SlabAlloc;
Chandler Carruth9df0fd42014-03-30 12:07:07 +0000139 BumpPtrAllocator Alloc(SlabAlloc);
Chandler Carruthf5babf92014-04-14 03:55:11 +0000140
141 // First allocate a tiny bit to ensure we have to re-align things.
142 (void)Alloc.Allocate(1, 0);
143
144 // Now the big chunk with a big alignment.
145 (void)Alloc.Allocate(3000, 2048);
146
147 // We test that the last slab size is not the default 4096 byte slab, but
148 // rather a custom sized slab that is larger.
149 EXPECT_GT(SlabAlloc.GetLastSlabSize(), 4096u);
Reid Kleckner4b1f2f42009-07-25 21:26:02 +0000150}
151
Reid Klecknerc2d882d2009-07-23 18:34:13 +0000152} // anonymous namespace