2009-07-24 02:34:13 +08:00
|
|
|
//===- llvm/unittest/Support/AllocatorTest.cpp - BumpPtrAllocator tests ---===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Support/Allocator.h"
|
|
|
|
#include "gtest/gtest.h"
|
2009-07-26 05:26:02 +08:00
|
|
|
#include <cstdlib>
|
2009-07-24 02:34:13 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
TEST(AllocatorTest, Basics) {
|
|
|
|
BumpPtrAllocator Alloc;
|
2014-08-20 07:35:33 +08:00
|
|
|
int *a = (int*)Alloc.Allocate(sizeof(int), 1);
|
|
|
|
int *b = (int*)Alloc.Allocate(sizeof(int) * 10, 1);
|
|
|
|
int *c = (int*)Alloc.Allocate(sizeof(int), 1);
|
2009-07-24 02:34:13 +08:00
|
|
|
*a = 1;
|
|
|
|
b[0] = 2;
|
|
|
|
b[9] = 2;
|
|
|
|
*c = 3;
|
|
|
|
EXPECT_EQ(1, *a);
|
|
|
|
EXPECT_EQ(2, b[0]);
|
|
|
|
EXPECT_EQ(2, b[9]);
|
|
|
|
EXPECT_EQ(3, *c);
|
|
|
|
EXPECT_EQ(1U, Alloc.GetNumSlabs());
|
2014-04-16 18:48:27 +08:00
|
|
|
|
|
|
|
BumpPtrAllocator Alloc2 = std::move(Alloc);
|
|
|
|
EXPECT_EQ(0U, Alloc.GetNumSlabs());
|
|
|
|
EXPECT_EQ(1U, Alloc2.GetNumSlabs());
|
|
|
|
|
|
|
|
// Make sure the old pointers still work. These are especially interesting
|
|
|
|
// under ASan or Valgrind.
|
|
|
|
EXPECT_EQ(1, *a);
|
|
|
|
EXPECT_EQ(2, b[0]);
|
|
|
|
EXPECT_EQ(2, b[9]);
|
|
|
|
EXPECT_EQ(3, *c);
|
|
|
|
|
|
|
|
Alloc = std::move(Alloc2);
|
|
|
|
EXPECT_EQ(0U, Alloc2.GetNumSlabs());
|
|
|
|
EXPECT_EQ(1U, Alloc.GetNumSlabs());
|
2009-07-24 02:34:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate enough bytes to create three slabs.
|
|
|
|
TEST(AllocatorTest, ThreeSlabs) {
|
2014-03-30 19:36:32 +08:00
|
|
|
BumpPtrAllocator Alloc;
|
2014-08-20 07:35:33 +08:00
|
|
|
Alloc.Allocate(3000, 1);
|
2009-07-24 02:34:13 +08:00
|
|
|
EXPECT_EQ(1U, Alloc.GetNumSlabs());
|
2014-08-20 07:35:33 +08:00
|
|
|
Alloc.Allocate(3000, 1);
|
2009-07-24 02:34:13 +08:00
|
|
|
EXPECT_EQ(2U, Alloc.GetNumSlabs());
|
2014-08-20 07:35:33 +08:00
|
|
|
Alloc.Allocate(3000, 1);
|
2009-07-24 02:34:13 +08:00
|
|
|
EXPECT_EQ(3U, Alloc.GetNumSlabs());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate enough bytes to create two slabs, reset the allocator, and do it
|
|
|
|
// again.
|
|
|
|
TEST(AllocatorTest, TestReset) {
|
2014-03-30 19:36:32 +08:00
|
|
|
BumpPtrAllocator Alloc;
|
2015-05-19 00:54:17 +08:00
|
|
|
|
|
|
|
// Allocate something larger than the SizeThreshold=4096.
|
|
|
|
(void)Alloc.Allocate(5000, 1);
|
|
|
|
Alloc.Reset();
|
|
|
|
// Calling Reset should free all CustomSizedSlabs.
|
|
|
|
EXPECT_EQ(0u, Alloc.GetNumSlabs());
|
|
|
|
|
2014-08-20 07:35:33 +08:00
|
|
|
Alloc.Allocate(3000, 1);
|
2009-07-24 02:34:13 +08:00
|
|
|
EXPECT_EQ(1U, Alloc.GetNumSlabs());
|
2014-08-20 07:35:33 +08:00
|
|
|
Alloc.Allocate(3000, 1);
|
2009-07-24 02:34:13 +08:00
|
|
|
EXPECT_EQ(2U, Alloc.GetNumSlabs());
|
|
|
|
Alloc.Reset();
|
|
|
|
EXPECT_EQ(1U, Alloc.GetNumSlabs());
|
2014-08-20 07:35:33 +08:00
|
|
|
Alloc.Allocate(3000, 1);
|
2009-07-24 02:34:13 +08:00
|
|
|
EXPECT_EQ(1U, Alloc.GetNumSlabs());
|
2014-08-20 07:35:33 +08:00
|
|
|
Alloc.Allocate(3000, 1);
|
2009-07-24 02:34:13 +08:00
|
|
|
EXPECT_EQ(2U, Alloc.GetNumSlabs());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test some allocations at varying alignments.
|
|
|
|
TEST(AllocatorTest, TestAlignment) {
|
|
|
|
BumpPtrAllocator Alloc;
|
|
|
|
uintptr_t a;
|
|
|
|
a = (uintptr_t)Alloc.Allocate(1, 2);
|
|
|
|
EXPECT_EQ(0U, a & 1);
|
|
|
|
a = (uintptr_t)Alloc.Allocate(1, 4);
|
|
|
|
EXPECT_EQ(0U, a & 3);
|
|
|
|
a = (uintptr_t)Alloc.Allocate(1, 8);
|
|
|
|
EXPECT_EQ(0U, a & 7);
|
|
|
|
a = (uintptr_t)Alloc.Allocate(1, 16);
|
|
|
|
EXPECT_EQ(0U, a & 15);
|
|
|
|
a = (uintptr_t)Alloc.Allocate(1, 32);
|
|
|
|
EXPECT_EQ(0U, a & 31);
|
|
|
|
a = (uintptr_t)Alloc.Allocate(1, 64);
|
|
|
|
EXPECT_EQ(0U, a & 63);
|
|
|
|
a = (uintptr_t)Alloc.Allocate(1, 128);
|
|
|
|
EXPECT_EQ(0U, a & 127);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test allocating just over the slab size. This tests a bug where before the
|
|
|
|
// allocator incorrectly calculated the buffer end pointer.
|
|
|
|
TEST(AllocatorTest, TestOverflow) {
|
2014-03-30 19:36:32 +08:00
|
|
|
BumpPtrAllocator Alloc;
|
2009-07-24 02:34:13 +08:00
|
|
|
|
|
|
|
// Fill the slab right up until the end pointer.
|
2014-08-20 07:35:33 +08:00
|
|
|
Alloc.Allocate(4096, 1);
|
2009-07-24 02:34:13 +08:00
|
|
|
EXPECT_EQ(1U, Alloc.GetNumSlabs());
|
|
|
|
|
2010-03-02 01:51:02 +08:00
|
|
|
// If we don't allocate a new slab, then we will have overflowed.
|
2014-08-20 07:35:33 +08:00
|
|
|
Alloc.Allocate(1, 1);
|
2009-07-24 02:34:13 +08:00
|
|
|
EXPECT_EQ(2U, Alloc.GetNumSlabs());
|
|
|
|
}
|
|
|
|
|
2012-03-02 04:36:32 +08:00
|
|
|
// Test allocating with a size larger than the initial slab size.
|
|
|
|
TEST(AllocatorTest, TestSmallSlabSize) {
|
2014-03-30 19:36:32 +08:00
|
|
|
BumpPtrAllocator Alloc;
|
2012-03-02 04:36:32 +08:00
|
|
|
|
2014-08-20 07:35:33 +08:00
|
|
|
Alloc.Allocate(8000, 1);
|
2014-08-18 02:31:18 +08:00
|
|
|
EXPECT_EQ(1U, Alloc.GetNumSlabs());
|
2012-03-02 04:36:32 +08:00
|
|
|
}
|
|
|
|
|
2014-09-07 12:24:31 +08:00
|
|
|
// Test requesting alignment that goes past the end of the current slab.
|
|
|
|
TEST(AllocatorTest, TestAlignmentPastSlab) {
|
|
|
|
BumpPtrAllocator Alloc;
|
2014-09-07 13:14:29 +08:00
|
|
|
Alloc.Allocate(4095, 1);
|
2014-09-07 12:24:31 +08:00
|
|
|
|
2014-09-07 13:14:29 +08:00
|
|
|
// Aligning the current slab pointer is likely to move it past the end of the
|
|
|
|
// slab, which would confuse any unsigned comparisons with the difference of
|
2015-06-19 09:53:21 +08:00
|
|
|
// the end pointer and the aligned pointer.
|
2014-09-07 12:24:31 +08:00
|
|
|
Alloc.Allocate(1024, 8192);
|
|
|
|
|
|
|
|
EXPECT_EQ(2U, Alloc.GetNumSlabs());
|
|
|
|
}
|
|
|
|
|
2009-07-26 05:26:02 +08:00
|
|
|
// Mock slab allocator that returns slabs aligned on 4096 bytes. There is no
|
|
|
|
// easy portable way to do this, so this is kind of a hack.
|
2014-04-14 13:11:27 +08:00
|
|
|
class MockSlabAllocator {
|
|
|
|
static size_t LastSlabSize;
|
2009-07-26 05:26:02 +08:00
|
|
|
|
|
|
|
public:
|
2014-04-14 13:11:27 +08:00
|
|
|
~MockSlabAllocator() { }
|
2009-07-26 05:26:02 +08:00
|
|
|
|
2014-04-15 17:44:09 +08:00
|
|
|
void *Allocate(size_t Size, size_t /*Alignment*/) {
|
2009-07-26 05:26:02 +08:00
|
|
|
// Allocate space for the alignment, the slab, and a void* that goes right
|
|
|
|
// before the slab.
|
|
|
|
size_t Alignment = 4096;
|
|
|
|
void *MemBase = malloc(Size + Alignment - 1 + sizeof(void*));
|
|
|
|
|
2014-04-14 11:55:11 +08:00
|
|
|
// Find the slab start.
|
2014-09-03 05:51:35 +08:00
|
|
|
void *Slab = (void *)alignAddr((char*)MemBase + sizeof(void *), Alignment);
|
2009-07-26 05:26:02 +08:00
|
|
|
|
|
|
|
// Hold a pointer to the base so we can free the whole malloced block.
|
|
|
|
((void**)Slab)[-1] = MemBase;
|
|
|
|
|
2014-04-14 11:55:11 +08:00
|
|
|
LastSlabSize = Size;
|
2009-07-26 05:26:02 +08:00
|
|
|
return Slab;
|
|
|
|
}
|
|
|
|
|
2014-04-14 13:11:27 +08:00
|
|
|
void Deallocate(void *Slab, size_t Size) {
|
2009-07-26 05:26:02 +08:00
|
|
|
free(((void**)Slab)[-1]);
|
|
|
|
}
|
|
|
|
|
2014-04-14 13:11:27 +08:00
|
|
|
static size_t GetLastSlabSize() { return LastSlabSize; }
|
2009-07-26 05:26:02 +08:00
|
|
|
};
|
|
|
|
|
2014-04-14 13:11:27 +08:00
|
|
|
size_t MockSlabAllocator::LastSlabSize = 0;
|
|
|
|
|
2009-07-26 05:26:02 +08:00
|
|
|
// Allocate a large-ish block with a really large alignment so that the
|
|
|
|
// allocator will think that it has space, but after it does the alignment it
|
|
|
|
// will not.
|
|
|
|
TEST(AllocatorTest, TestBigAlignment) {
|
2014-04-14 13:11:27 +08:00
|
|
|
BumpPtrAllocatorImpl<MockSlabAllocator> Alloc;
|
2014-04-14 11:55:11 +08:00
|
|
|
|
|
|
|
// First allocate a tiny bit to ensure we have to re-align things.
|
2014-08-20 07:35:33 +08:00
|
|
|
(void)Alloc.Allocate(1, 1);
|
2014-04-14 11:55:11 +08:00
|
|
|
|
|
|
|
// Now the big chunk with a big alignment.
|
|
|
|
(void)Alloc.Allocate(3000, 2048);
|
|
|
|
|
|
|
|
// We test that the last slab size is not the default 4096 byte slab, but
|
|
|
|
// rather a custom sized slab that is larger.
|
2014-04-14 13:11:27 +08:00
|
|
|
EXPECT_GT(MockSlabAllocator::GetLastSlabSize(), 4096u);
|
2009-07-26 05:26:02 +08:00
|
|
|
}
|
|
|
|
|
2009-07-24 02:34:13 +08:00
|
|
|
} // anonymous namespace
|