This file is empty.
[oota-llvm.git] / lib / Support / Allocator.cpp
index 7e7477357fe431bcb9f64d52ee70b129fc2a5db7..ba6a393c81fc9b1778a6b372e97e41709c2f641b 100644 (file)
@@ -2,8 +2,8 @@
 //
 //                     The LLVM Compiler Infrastructure
 //
-// This file was developed by Chris Lattner and is distributed under
-// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
 //
 //===----------------------------------------------------------------------===//
 //
@@ -13,7 +13,8 @@
 
 #include "llvm/Support/Allocator.h"
 #include "llvm/Support/DataTypes.h"
-#include <iostream>
+#include "llvm/Support/Streams.h"
+#include <ostream>
 using namespace llvm;
 
 //===----------------------------------------------------------------------===//
@@ -44,14 +45,17 @@ public:
   
   /// Allocate - Allocate and return at least the specified number of bytes.
   ///
-  void *Allocate(unsigned AllocSize, unsigned Alignment, MemRegion **RegPtr) {
-    // Round size up to an even multiple of the alignment.
-    AllocSize = (AllocSize+Alignment-1) & ~(Alignment-1);
+  void *Allocate(size_t AllocSize, size_t Alignment, MemRegion **RegPtr) {
     
-    // If there is space in this region, return it.
-    if (unsigned(NextPtr+AllocSize-(char*)this) <= RegionSize) {
-      void *Result = NextPtr;
-      NextPtr += AllocSize;
+    char* Result = (char*) (((uintptr_t) (NextPtr+Alignment-1)) 
+                            & ~((uintptr_t) Alignment-1));
+
+    // Speculate the new value of NextPtr.
+    char* NextPtrTmp = Result + AllocSize;
+    
+    // If we are still within the current region, return Result.
+    if (unsigned (NextPtrTmp - (char*) this) <= RegionSize) {
+      NextPtr = NextPtrTmp;
       return Result;
     }
     
@@ -67,14 +71,25 @@ public:
     return NewRegion->Allocate(AllocSize, Alignment, RegPtr);
   }
   
-  /// Deallocate - Release all memory for this region to the system.
-  ///
+  /// Deallocate - Recursively release all memory for this and its next regions
+  /// to the system.
   void Deallocate() {
     MemRegion *next = Next;
     free(this);
     if (next)
       next->Deallocate();
   }
+
+  /// DeallocateAllButLast - Recursively release all memory for this and its
+  /// next regions to the system stopping at the last region in the list.
+  /// Returns the pointer to the last region.
+  MemRegion *DeallocateAllButLast() {
+    MemRegion *next = Next;
+    if (!next)
+      return this;
+    free(this);
+    return next->DeallocateAllButLast();
+  }
 };
 }
 
@@ -91,8 +106,18 @@ BumpPtrAllocator::~BumpPtrAllocator() {
   ((MemRegion*)TheMemory)->Deallocate();
 }
 
-void *BumpPtrAllocator::Allocate(unsigned Size, unsigned Align) {
-  return ((MemRegion*)TheMemory)->Allocate(Size, Align,(MemRegion**)&TheMemory);
+void BumpPtrAllocator::Reset() {
+  MemRegion *MRP = (MemRegion*)TheMemory;
+  MRP = MRP->DeallocateAllButLast();
+  MRP->Init(4096, 1, 0);
+  TheMemory = MRP;
+}
+
+void *BumpPtrAllocator::Allocate(size_t Size, size_t Align) {
+  MemRegion *MRP = (MemRegion*)TheMemory;
+  void *Ptr = MRP->Allocate(Size, Align, &MRP);
+  TheMemory = MRP;
+  return Ptr;
 }
 
 void BumpPtrAllocator::PrintStats() const {
@@ -102,6 +127,6 @@ void BumpPtrAllocator::PrintStats() const {
   for (; R; R = R->getNext(), ++NumRegions)
     BytesUsed += R->getNumBytesAllocated();
 
-  std::cerr << "\nNumber of memory regions: " << NumRegions << "\n";
-  std::cerr << "Bytes allocated: " << BytesUsed << "\n";
+  cerr << "\nNumber of memory regions: " << NumRegions << "\n";
+  cerr << "Bytes allocated: " << BytesUsed << "\n";
 }