X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FSupport%2FAllocator.cpp;h=ba6a393c81fc9b1778a6b372e97e41709c2f641b;hb=84701836bfb1889e2e26e361ebd5d29d972ab396;hp=632b5f7a2284db0915a3ed3279dd1a29d95f397a;hpb=e81561909d128c6e2d8033cb5465a49b2596b26a;p=oota-llvm.git diff --git a/lib/Support/Allocator.cpp b/lib/Support/Allocator.cpp index 632b5f7a228..ba6a393c81f 100644 --- a/lib/Support/Allocator.cpp +++ b/lib/Support/Allocator.cpp @@ -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. // //===----------------------------------------------------------------------===// // @@ -14,6 +14,7 @@ #include "llvm/Support/Allocator.h" #include "llvm/Support/DataTypes.h" #include "llvm/Support/Streams.h" +#include 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 {