1 //===- llvm/unittest/Support/AllocatorTest.cpp - BumpPtrAllocator tests ---===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/Allocator.h"
11 #include "gtest/gtest.h"
18 TEST(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);
31 EXPECT_EQ(1U, Alloc.GetNumSlabs());
34 // Allocate enough bytes to create three slabs.
35 TEST(AllocatorTest, ThreeSlabs) {
36 BumpPtrAllocator Alloc;
37 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());
45 // Allocate enough bytes to create two slabs, reset the allocator, and do it
47 TEST(AllocatorTest, TestReset) {
48 BumpPtrAllocator Alloc;
49 Alloc.Allocate(3000, 0);
50 EXPECT_EQ(1U, Alloc.GetNumSlabs());
51 Alloc.Allocate(3000, 0);
52 EXPECT_EQ(2U, Alloc.GetNumSlabs());
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());
61 // Test some allocations at varying alignments.
62 TEST(AllocatorTest, TestAlignment) {
63 BumpPtrAllocator Alloc;
65 a = (uintptr_t)Alloc.Allocate(1, 2);
67 a = (uintptr_t)Alloc.Allocate(1, 4);
69 a = (uintptr_t)Alloc.Allocate(1, 8);
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);
81 // Test allocating just over the slab size. This tests a bug where before the
82 // allocator incorrectly calculated the buffer end pointer.
83 TEST(AllocatorTest, TestOverflow) {
84 BumpPtrAllocator Alloc;
86 // Fill the slab right up until the end pointer.
87 Alloc.Allocate(4096, 0);
88 EXPECT_EQ(1U, Alloc.GetNumSlabs());
90 // If we don't allocate a new slab, then we will have overflowed.
92 EXPECT_EQ(2U, Alloc.GetNumSlabs());
95 // Test allocating with a size larger than the initial slab size.
96 TEST(AllocatorTest, TestSmallSlabSize) {
97 BumpPtrAllocator Alloc;
99 Alloc.Allocate(8000, 0);
100 EXPECT_EQ(2U, Alloc.GetNumSlabs());
103 // 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.
105 class MockSlabAllocator : public SlabAllocator {
109 virtual ~MockSlabAllocator() { }
111 virtual void *Allocate(size_t Size) {
112 // Allocate space for the alignment, the slab, and a void* that goes right
114 size_t Alignment = 4096;
115 void *MemBase = malloc(Size + Alignment - 1 + sizeof(void*));
117 // Find the slab start.
118 void *Slab = alignPtr((char *)MemBase + sizeof(void *), Alignment);
120 // Hold a pointer to the base so we can free the whole malloced block.
121 ((void**)Slab)[-1] = MemBase;
127 virtual void Deallocate(void *Slab, size_t Size) {
128 free(((void**)Slab)[-1]);
131 size_t GetLastSlabSize() { return LastSlabSize; }
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
137 TEST(AllocatorTest, TestBigAlignment) {
138 MockSlabAllocator SlabAlloc;
139 BumpPtrAllocator Alloc(SlabAlloc);
141 // First allocate a tiny bit to ensure we have to re-align things.
142 (void)Alloc.Allocate(1, 0);
144 // Now the big chunk with a big alignment.
145 (void)Alloc.Allocate(3000, 2048);
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);
152 } // anonymous namespace