#include "llvm/Support/Allocator.h"
#include "gtest/gtest.h"
#include <cstdlib>
using namespace llvm;
namespace {
TEST(AllocatorTest, Basics) {
BumpPtrAllocator Alloc;
int *a = (int*)Alloc.Allocate(sizeof(int), alignof(int));
int *b = (int*)Alloc.Allocate(sizeof(int) * 10, alignof(int));
int *c = (int*)Alloc.Allocate(sizeof(int), alignof(int));
*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());
BumpPtrAllocator Alloc2 = std::move(Alloc);
EXPECT_EQ(0U, Alloc.GetNumSlabs());
EXPECT_EQ(1U, Alloc2.GetNumSlabs());
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());
}
TEST(AllocatorTest, ThreeSlabs) {
BumpPtrAllocator Alloc;
Alloc.Allocate(3000, 1);
EXPECT_EQ(1U, Alloc.GetNumSlabs());
Alloc.Allocate(3000, 1);
EXPECT_EQ(2U, Alloc.GetNumSlabs());
Alloc.Allocate(3000, 1);
EXPECT_EQ(3U, Alloc.GetNumSlabs());
}
TEST(AllocatorTest, TestReset) {
BumpPtrAllocator Alloc;
(void)Alloc.Allocate(5000, 1);
Alloc.Reset();
EXPECT_EQ(0u, Alloc.GetNumSlabs());
Alloc.Allocate(3000, 1);
EXPECT_EQ(1U, Alloc.GetNumSlabs());
Alloc.Allocate(3000, 1);
EXPECT_EQ(2U, Alloc.GetNumSlabs());
Alloc.Reset();
EXPECT_EQ(1U, Alloc.GetNumSlabs());
Alloc.Allocate(3000, 1);
EXPECT_EQ(1U, Alloc.GetNumSlabs());
Alloc.Allocate(3000, 1);
EXPECT_EQ(2U, Alloc.GetNumSlabs());
}
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(AllocatorTest, TestZero) {
BumpPtrAllocator Alloc;
Alloc.setRedZoneSize(0); EXPECT_EQ(0u, Alloc.GetNumSlabs());
EXPECT_EQ(0u, Alloc.getBytesAllocated());
void *Empty = Alloc.Allocate(0, 1);
EXPECT_NE(Empty, nullptr) << "Allocate is __attribute__((returns_nonnull))";
EXPECT_EQ(1u, Alloc.GetNumSlabs()) << "Allocated a slab to point to";
EXPECT_EQ(0u, Alloc.getBytesAllocated());
void *Large = Alloc.Allocate(4096, 1);
EXPECT_EQ(1u, Alloc.GetNumSlabs());
EXPECT_EQ(4096u, Alloc.getBytesAllocated());
EXPECT_EQ(Empty, Large);
void *Empty2 = Alloc.Allocate(0, 1);
EXPECT_NE(Empty2, nullptr);
EXPECT_EQ(1u, Alloc.GetNumSlabs());
EXPECT_EQ(4096u, Alloc.getBytesAllocated());
}
TEST(AllocatorTest, TestOverflow) {
BumpPtrAllocator Alloc;
Alloc.Allocate(4096, 1);
EXPECT_EQ(1U, Alloc.GetNumSlabs());
Alloc.Allocate(1, 1);
EXPECT_EQ(2U, Alloc.GetNumSlabs());
}
TEST(AllocatorTest, TestSmallSlabSize) {
BumpPtrAllocator Alloc;
Alloc.Allocate(8000, 1);
EXPECT_EQ(1U, Alloc.GetNumSlabs());
}
TEST(AllocatorTest, TestAlignmentPastSlab) {
BumpPtrAllocator Alloc;
Alloc.Allocate(4095, 1);
Alloc.Allocate(1024, 8192);
EXPECT_EQ(2U, Alloc.GetNumSlabs());
}
TEST(AllocatorTest, TestFasterSlabGrowthDelay) {
const size_t SlabSize = 4096;
const size_t GrowthDelay = 1;
BumpPtrAllocatorImpl<MallocAllocator, SlabSize, SlabSize, GrowthDelay> Alloc;
Alloc.setRedZoneSize(0);
Alloc.Allocate(SlabSize, 1);
EXPECT_EQ(SlabSize, Alloc.getTotalMemory());
Alloc.Allocate(SlabSize, 1);
EXPECT_EQ(SlabSize * 3, Alloc.getTotalMemory());
Alloc.Allocate(SlabSize, 1);
EXPECT_EQ(SlabSize * 3, Alloc.getTotalMemory());
Alloc.Allocate(SlabSize, 1);
EXPECT_EQ(SlabSize * 7, Alloc.getTotalMemory());
}
TEST(AllocatorTest, TestSlowerSlabGrowthDelay) {
const size_t SlabSize = 16;
const size_t GrowthDelay = 256;
BumpPtrAllocatorImpl<MallocAllocator, SlabSize, SlabSize, GrowthDelay> Alloc;
Alloc.setRedZoneSize(0);
for (std::size_t i = 0; i < GrowthDelay; ++i)
Alloc.Allocate(SlabSize, 1);
EXPECT_EQ(SlabSize * GrowthDelay, Alloc.getTotalMemory());
Alloc.Allocate(SlabSize, 1);
EXPECT_EQ(SlabSize * GrowthDelay + SlabSize * 2, Alloc.getTotalMemory());
}
class MockSlabAllocator {
static size_t LastSlabSize;
public:
~MockSlabAllocator() { }
void *Allocate(size_t Size, size_t ) {
Align Alignment(4096);
void *MemBase = safe_malloc(Size + Alignment.value() - 1 + sizeof(void *));
void *Slab = (void *)alignAddr((char*)MemBase + sizeof(void *), Alignment);
((void**)Slab)[-1] = MemBase;
LastSlabSize = Size;
return Slab;
}
void Deallocate(void *Slab, size_t , size_t ) {
free(((void**)Slab)[-1]);
}
static size_t GetLastSlabSize() { return LastSlabSize; }
};
size_t MockSlabAllocator::LastSlabSize = 0;
TEST(AllocatorTest, TestBigAlignment) {
BumpPtrAllocatorImpl<MockSlabAllocator> Alloc;
(void)Alloc.Allocate(1, 1);
(void)Alloc.Allocate(3000, 2048);
EXPECT_GT(MockSlabAllocator::GetLastSlabSize(), 4096u);
}
}