Added a test and fixed a bug in BumpPtrAllocator relating to large alignment
authorReid Kleckner <reid@kleckner.net>
Sat, 25 Jul 2009 21:26:02 +0000 (21:26 +0000)
committerReid Kleckner <reid@kleckner.net>
Sat, 25 Jul 2009 21:26:02 +0000 (21:26 +0000)
values.  Hopefully this fixes PR4622.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@77088 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Support/Allocator.cpp
unittests/Support/AllocatorTest.cpp

index 230c421f1fe17bc681e0dbdfe3b891cbaeac46ba..36da4432073a20487898f339b97ab68209b4ca45 100644 (file)
@@ -95,8 +95,8 @@ void *BumpPtrAllocator::Allocate(size_t Size, size_t Alignment) {
   }
 
   // If Size is really big, allocate a separate slab for it.
-  if (Size > SizeThreshold) {
-    size_t PaddedSize = Size + sizeof(MemSlab) + Alignment - 1;
+  size_t PaddedSize = Size + sizeof(MemSlab) + Alignment - 1;
+  if (PaddedSize > SizeThreshold) {
     MemSlab *NewSlab = Allocator.Allocate(PaddedSize);
 
     // Put the new slab after the current slab, since we are not allocating
index cc3296a8d0150f0547f328c6ec2bcc52d603cc32..463760d2f0ef87241dc957eb7da4b0d61b4aa931 100644 (file)
@@ -10,6 +10,7 @@
 #include "llvm/Support/Allocator.h"
 
 #include "gtest/gtest.h"
+#include <cstdlib>
 
 using namespace llvm;
 
@@ -92,4 +93,51 @@ TEST(AllocatorTest, TestOverflow) {
   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;
+
+public:
+  virtual ~MockSlabAllocator() { }
+
+  virtual MemSlab *Allocate(size_t Size) {
+    // 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 + Alignment - 1) &
+                               ~(uintptr_t)(Alignment - 1));
+    Slab->Size = Size;
+    Slab->NextPtr = 0;
+
+    // Hold a pointer to the base so we can free the whole malloced block.
+    ((void**)Slab)[-1] = MemBase;
+
+    LastSlab = Slab;
+    return Slab;
+  }
+
+  virtual void Deallocate(MemSlab *Slab) {
+    free(((void**)Slab)[-1]);
+  }
+
+  MemSlab *GetLastSlab() {
+    return LastSlab;
+  }
+};
+
+// 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);
+}
+
 }  // anonymous namespace