diff options
author | Reid Kleckner <reid@kleckner.net> | 2009-07-23 00:30:41 +0000 |
---|---|---|
committer | Reid Kleckner <reid@kleckner.net> | 2009-07-23 00:30:41 +0000 |
commit | e672dc2856f6334d4d218e260e3aae04ab97909c (patch) | |
tree | f2315ecd285be56068ff0b18a29450239fc5e31b /unittests | |
parent | 0144e6573682e62cd6b6e4d2211e686d11622d95 (diff) | |
download | external_llvm-e672dc2856f6334d4d218e260e3aae04ab97909c.zip external_llvm-e672dc2856f6334d4d218e260e3aae04ab97909c.tar.gz external_llvm-e672dc2856f6334d4d218e260e3aae04ab97909c.tar.bz2 |
Parameterize the BumpPtrAllocator over a slab allocator. It defaults to using
malloc, so there should be no functional changes to other code.
These changes are necessary since I have plans to use this allocator in the JIT
memory manager, and it needs a special allocator.
I also added some tests which helped me pinpoint some bugs.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@76825 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests')
-rw-r--r-- | unittests/Support/AllocatorTest.cpp | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/unittests/Support/AllocatorTest.cpp b/unittests/Support/AllocatorTest.cpp new file mode 100644 index 0000000..139270b --- /dev/null +++ b/unittests/Support/AllocatorTest.cpp @@ -0,0 +1,61 @@ +//===- 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" + +using namespace llvm; + +namespace { + +TEST(AllocatorTest, Basics) { + BumpPtrAllocator Alloc; + int *a = (int*)Alloc.Allocate(sizeof(int), 0); + int *b = (int*)Alloc.Allocate(sizeof(int) * 10, 0); + int *c = (int*)Alloc.Allocate(sizeof(int), 0); + *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()); +} + +// Allocate enough bytes to create three slabs. +TEST(AllocatorTest, ThreeSlabs) { + BumpPtrAllocator Alloc(4096, 4096); + Alloc.Allocate(3000, 0); + EXPECT_EQ(1U, Alloc.GetNumSlabs()); + Alloc.Allocate(3000, 0); + EXPECT_EQ(2U, Alloc.GetNumSlabs()); + Alloc.Allocate(3000, 0); + EXPECT_EQ(3U, Alloc.GetNumSlabs()); +} + +// Allocate enough bytes to create two slabs, reset the allocator, and do it +// again. +TEST(AllocatorTest, TestReset) { + BumpPtrAllocator Alloc(4096, 4096); + Alloc.Allocate(3000, 0); + EXPECT_EQ(1U, Alloc.GetNumSlabs()); + Alloc.Allocate(3000, 0); + EXPECT_EQ(2U, Alloc.GetNumSlabs()); + Alloc.Reset(); + EXPECT_EQ(1U, Alloc.GetNumSlabs()); + Alloc.Allocate(3000, 0); + EXPECT_EQ(1U, Alloc.GetNumSlabs()); + Alloc.Allocate(3000, 0); + EXPECT_EQ(2U, Alloc.GetNumSlabs()); +} + +} // anonymous namespace |