Try to unflake AllocatorTest.TestAlignmentPastSlab
[oota-llvm.git] / unittests / Support / AllocatorTest.cpp
index 43ce0c86bee26a4affd154a496da00d6d589b4a7..7f15776d6f007797d4510803a8bda8575019a9bc 100644 (file)
@@ -17,9 +17,9 @@ 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);
+  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);
   *a = 1;
   b[0] = 2;
   b[9] = 2;
@@ -29,32 +29,47 @@ TEST(AllocatorTest, Basics) {
   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());
+
+  // 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());
 }
 
 // Allocate enough bytes to create three slabs.
 TEST(AllocatorTest, ThreeSlabs) {
-  BumpPtrAllocator Alloc(4096, 4096);
-  Alloc.Allocate(3000, 0);
+  BumpPtrAllocator Alloc;
+  Alloc.Allocate(3000, 1);
   EXPECT_EQ(1U, Alloc.GetNumSlabs());
-  Alloc.Allocate(3000, 0);
+  Alloc.Allocate(3000, 1);
   EXPECT_EQ(2U, Alloc.GetNumSlabs());
-  Alloc.Allocate(3000, 0);
+  Alloc.Allocate(3000, 1);
   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);
+  BumpPtrAllocator Alloc;
+  Alloc.Allocate(3000, 1);
   EXPECT_EQ(1U, Alloc.GetNumSlabs());
-  Alloc.Allocate(3000, 0);
+  Alloc.Allocate(3000, 1);
   EXPECT_EQ(2U, Alloc.GetNumSlabs());
   Alloc.Reset();
   EXPECT_EQ(1U, Alloc.GetNumSlabs());
-  Alloc.Allocate(3000, 0);
+  Alloc.Allocate(3000, 1);
   EXPECT_EQ(1U, Alloc.GetNumSlabs());
-  Alloc.Allocate(3000, 0);
+  Alloc.Allocate(3000, 1);
   EXPECT_EQ(2U, Alloc.GetNumSlabs());
 }
 
@@ -81,98 +96,86 @@ TEST(AllocatorTest, TestAlignment) {
 // Test allocating just over the slab size.  This tests a bug where before the
 // allocator incorrectly calculated the buffer end pointer.
 TEST(AllocatorTest, TestOverflow) {
-  BumpPtrAllocator Alloc(4096, 4096);
+  BumpPtrAllocator Alloc;
 
   // Fill the slab right up until the end pointer.
-  Alloc.Allocate(4096 - sizeof(MemSlab), 0);
+  Alloc.Allocate(4096, 1);
   EXPECT_EQ(1U, Alloc.GetNumSlabs());
 
   // If we don't allocate a new slab, then we will have overflowed.
-  Alloc.Allocate(1, 0);
+  Alloc.Allocate(1, 1);
   EXPECT_EQ(2U, Alloc.GetNumSlabs());
 }
 
 // Test allocating with a size larger than the initial slab size.
 TEST(AllocatorTest, TestSmallSlabSize) {
-  BumpPtrAllocator Alloc(128);
+  BumpPtrAllocator Alloc;
+
+  Alloc.Allocate(8000, 1);
+  EXPECT_EQ(1U, Alloc.GetNumSlabs());
+}
+
+// Test requesting alignment that goes past the end of the current slab.
+TEST(AllocatorTest, TestAlignmentPastSlab) {
+  BumpPtrAllocator Alloc;
+  Alloc.Allocate(4095, 1);
+
+  // 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
+  // the the end pointer and the aligned pointer.
+  Alloc.Allocate(1024, 8192);
 
-  Alloc.Allocate(200, 0);
   EXPECT_EQ(2U, Alloc.GetNumSlabs());
 }
 
 // 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.
-class MockSlabAllocator : public SlabAllocator {
-  MemSlab *LastSlab;
+class MockSlabAllocator {
+  static size_t LastSlabSize;
 
 public:
-  virtual ~MockSlabAllocator() { }
+  ~MockSlabAllocator() { }
 
-  virtual MemSlab *Allocate(size_t Size) {
+  void *Allocate(size_t Size, size_t /*Alignment*/) {
     // 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*));
 
-    // Make the slab.
-    MemSlab *Slab = (MemSlab*)(((uintptr_t)MemBase+sizeof(void*)+Alignment-1) &
-                               ~(uintptr_t)(Alignment - 1));
-    Slab->Size = Size;
-    Slab->NextPtr = 0;
+    // Find the slab start.
+    void *Slab = (void *)alignAddr((char*)MemBase + sizeof(void *), Alignment);
 
     // Hold a pointer to the base so we can free the whole malloced block.
     ((void**)Slab)[-1] = MemBase;
 
-    LastSlab = Slab;
+    LastSlabSize = Size;
     return Slab;
   }
 
-  virtual void Deallocate(MemSlab *Slab) {
+  void Deallocate(void *Slab, size_t Size) {
     free(((void**)Slab)[-1]);
   }
 
-  MemSlab *GetLastSlab() {
-    return LastSlab;
-  }
+  static size_t GetLastSlabSize() { return LastSlabSize; }
 };
 
+size_t MockSlabAllocator::LastSlabSize = 0;
+
 // 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) {
-  MockSlabAllocator SlabAlloc;
-  BumpPtrAllocator Alloc(4096, 4096, SlabAlloc);
-  uintptr_t Ptr = (uintptr_t)Alloc.Allocate(3000, 2048);
-  MemSlab *Slab = SlabAlloc.GetLastSlab();
-  EXPECT_LE(Ptr + 3000, ((uintptr_t)Slab) + Slab->Size);
-}
+  BumpPtrAllocatorImpl<MockSlabAllocator> Alloc;
 
-TEST(AllocatorTest, CopyStringRef) {
-  BumpPtrAllocator Alloc;
-  StringRef Str1 = "hello";
-  StringRef Str2 = "bye";
-  StringRef Str1c = Alloc.allocateCopy(Str1);
-  StringRef Str2c = Alloc.allocateCopy(Str2);
-  EXPECT_TRUE(Str1.equals(Str1c));
-  EXPECT_NE(Str1.data(), Str1c.data());
-  EXPECT_TRUE(Str2.equals(Str2c));
-  EXPECT_NE(Str2.data(), Str2c.data());
-}
-
-TEST(AllocatorTest, CopyArrayRef) {
-  BumpPtrAllocator Alloc;
-  static const uint16_t Words1[] = { 1, 4, 200, 37 };
-  ArrayRef<uint16_t> Array1 = makeArrayRef(Words1, 4);
-  static const uint16_t Words2[] = { 11, 4003, 67, 64000, 13 };
-  ArrayRef<uint16_t> Array2 = makeArrayRef(Words2, 5);
-  ArrayRef<uint16_t> Array1c = Alloc.allocateCopy(Array1);
-  ArrayRef<uint16_t> Array2c = Alloc.allocateCopy(Array2);
-  EXPECT_TRUE(Array1.equals(Array1c));
-  EXPECT_NE(Array1.data(), Array1c.data());
-  EXPECT_TRUE(Array2.equals(Array2c));
-  EXPECT_NE(Array2.data(), Array2c.data());
-}
+  // First allocate a tiny bit to ensure we have to re-align things.
+  (void)Alloc.Allocate(1, 1);
 
+  // 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.
+  EXPECT_GT(MockSlabAllocator::GetLastSlabSize(), 4096u);
+}
 
 }  // anonymous namespace