From: Hans Wennborg <hans@hanshq.net>
Date: Mon, 18 May 2015 16:54:17 +0000 (+0000)
Subject: Fix llvm::BumpPtrAllocatorImpl::Reset()
X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=febe7813c8cc0f783e6398303cd1c9ad7f7e852d;p=oota-llvm.git

Fix llvm::BumpPtrAllocatorImpl::Reset()

BumpPtrAllocator's Reset wouldn't clear CustomSizedSlabs if Slabs.size() == 0.

Patch by Kal <b17c0de@gmail.com>!

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

diff --git a/include/llvm/Support/Allocator.h b/include/llvm/Support/Allocator.h
index f633857f46e..f9b5cf22f97 100644
--- a/include/llvm/Support/Allocator.h
+++ b/include/llvm/Support/Allocator.h
@@ -187,6 +187,9 @@ public:
   /// \brief Deallocate all but the current slab and reset the current pointer
   /// to the beginning of it, freeing all memory allocated so far.
   void Reset() {
+    DeallocateCustomSizedSlabs();
+    CustomSizedSlabs.clear();
+
     if (Slabs.empty())
       return;
 
@@ -198,8 +201,6 @@ public:
     // Deallocate all but the first slab, and deallocate all custom-sized slabs.
     DeallocateSlabs(std::next(Slabs.begin()), Slabs.end());
     Slabs.erase(std::next(Slabs.begin()), Slabs.end());
-    DeallocateCustomSizedSlabs();
-    CustomSizedSlabs.clear();
   }
 
   /// \brief Allocate space at the specified alignment.
diff --git a/unittests/Support/AllocatorTest.cpp b/unittests/Support/AllocatorTest.cpp
index 7f15776d6f0..38c7fcba8af 100644
--- a/unittests/Support/AllocatorTest.cpp
+++ b/unittests/Support/AllocatorTest.cpp
@@ -61,6 +61,13 @@ TEST(AllocatorTest, ThreeSlabs) {
 // again.
 TEST(AllocatorTest, TestReset) {
   BumpPtrAllocator Alloc;
+
+  // 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());
+
   Alloc.Allocate(3000, 1);
   EXPECT_EQ(1U, Alloc.GetNumSlabs());
   Alloc.Allocate(3000, 1);