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"
12 #include "gtest/gtest.h"
19 TEST(AllocatorTest, Basics) {
20 BumpPtrAllocator Alloc;
21 int *a = (int*)Alloc.Allocate(sizeof(int), 0);
22 int *b = (int*)Alloc.Allocate(sizeof(int) * 10, 0);
23 int *c = (int*)Alloc.Allocate(sizeof(int), 0);
32 EXPECT_EQ(1U, Alloc.GetNumSlabs());
35 // Allocate enough bytes to create three slabs.
36 TEST(AllocatorTest, ThreeSlabs) {
37 BumpPtrAllocator Alloc(4096, 4096);
38 Alloc.Allocate(3000, 0);
39 EXPECT_EQ(1U, Alloc.GetNumSlabs());
40 Alloc.Allocate(3000, 0);
41 EXPECT_EQ(2U, Alloc.GetNumSlabs());
42 Alloc.Allocate(3000, 0);
43 EXPECT_EQ(3U, Alloc.GetNumSlabs());
46 // Allocate enough bytes to create two slabs, reset the allocator, and do it
48 TEST(AllocatorTest, TestReset) {
49 BumpPtrAllocator Alloc(4096, 4096);
50 Alloc.Allocate(3000, 0);
51 EXPECT_EQ(1U, Alloc.GetNumSlabs());
52 Alloc.Allocate(3000, 0);
53 EXPECT_EQ(2U, Alloc.GetNumSlabs());
55 EXPECT_EQ(1U, Alloc.GetNumSlabs());
56 Alloc.Allocate(3000, 0);
57 EXPECT_EQ(1U, Alloc.GetNumSlabs());
58 Alloc.Allocate(3000, 0);
59 EXPECT_EQ(2U, Alloc.GetNumSlabs());
62 // Test some allocations at varying alignments.
63 TEST(AllocatorTest, TestAlignment) {
64 BumpPtrAllocator Alloc;
66 a = (uintptr_t)Alloc.Allocate(1, 2);
68 a = (uintptr_t)Alloc.Allocate(1, 4);
70 a = (uintptr_t)Alloc.Allocate(1, 8);
72 a = (uintptr_t)Alloc.Allocate(1, 16);
73 EXPECT_EQ(0U, a & 15);
74 a = (uintptr_t)Alloc.Allocate(1, 32);
75 EXPECT_EQ(0U, a & 31);
76 a = (uintptr_t)Alloc.Allocate(1, 64);
77 EXPECT_EQ(0U, a & 63);
78 a = (uintptr_t)Alloc.Allocate(1, 128);
79 EXPECT_EQ(0U, a & 127);
82 // Test allocating just over the slab size. This tests a bug where before the
83 // allocator incorrectly calculated the buffer end pointer.
84 TEST(AllocatorTest, TestOverflow) {
85 BumpPtrAllocator Alloc(4096, 4096);
87 // Fill the slab right up until the end pointer.
88 Alloc.Allocate(4096 - sizeof(MemSlab), 0);
89 EXPECT_EQ(1U, Alloc.GetNumSlabs());
91 // If we don't allocate a new slab, then we will have overflowed.
93 EXPECT_EQ(2U, Alloc.GetNumSlabs());
96 // Mock slab allocator that returns slabs aligned on 4096 bytes. There is no
97 // easy portable way to do this, so this is kind of a hack.
98 class MockSlabAllocator : public SlabAllocator {
102 virtual ~MockSlabAllocator() { }
104 virtual MemSlab *Allocate(size_t Size) {
105 // Allocate space for the alignment, the slab, and a void* that goes right
107 size_t Alignment = 4096;
108 void *MemBase = malloc(Size + Alignment - 1 + sizeof(void*));
111 MemSlab *Slab = (MemSlab*)(((uintptr_t)MemBase+sizeof(void*)+Alignment-1) &
112 ~(uintptr_t)(Alignment - 1));
116 // Hold a pointer to the base so we can free the whole malloced block.
117 ((void**)Slab)[-1] = MemBase;
123 virtual void Deallocate(MemSlab *Slab) {
124 free(((void**)Slab)[-1]);
127 MemSlab *GetLastSlab() {
132 // Allocate a large-ish block with a really large alignment so that the
133 // allocator will think that it has space, but after it does the alignment it
135 TEST(AllocatorTest, TestBigAlignment) {
136 MockSlabAllocator SlabAlloc;
137 BumpPtrAllocator Alloc(4096, 4096, SlabAlloc);
138 uintptr_t Ptr = (uintptr_t)Alloc.Allocate(3000, 2048);
139 MemSlab *Slab = SlabAlloc.GetLastSlab();
140 EXPECT_LE(Ptr + 3000, ((uintptr_t)Slab) + Slab->Size);
143 } // anonymous namespace