#include "llvm/Support/Allocator.h"
#include "llvm/Support/DataTypes.h"
#include "llvm/Support/Streams.h"
+#include <ostream>
using namespace llvm;
//===----------------------------------------------------------------------===//
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();
+ }
};
}
((MemRegion*)TheMemory)->Deallocate();
}
+void BumpPtrAllocator::Reset() {
+ MemRegion *MRP = (MemRegion*)TheMemory;
+ MRP = MRP->DeallocateAllButLast();
+ MRP->Init(4096, 1, 0);
+ TheMemory = MRP;
+}
+
void *BumpPtrAllocator::Allocate(unsigned Size, unsigned Align) {
- return ((MemRegion*)TheMemory)->Allocate(Size, Align,(MemRegion**)&TheMemory);
+ MemRegion *MRP = (MemRegion*)TheMemory;
+ void *Ptr = MRP->Allocate(Size, Align, &MRP);
+ TheMemory = MRP;
+ return Ptr;
}
void BumpPtrAllocator::PrintStats() const {
for (; R; R = R->getNext(), ++NumRegions)
BytesUsed += R->getNumBytesAllocated();
- llvm_cerr << "\nNumber of memory regions: " << NumRegions << "\n";
- llvm_cerr << "Bytes allocated: " << BytesUsed << "\n";
+ cerr << "\nNumber of memory regions: " << NumRegions << "\n";
+ cerr << "Bytes allocated: " << BytesUsed << "\n";
}