X-Git-Url: http://plrg.eecs.uci.edu/git/?p=oota-llvm.git;a=blobdiff_plain;f=include%2Fllvm%2FSupport%2FAllocator.h;h=3243fd9cea57ecc8e18de21f118448069d2367b8;hp=cdb0fc85582193f4964d1ff991e5009af7adda79;hb=255f89faee13dc491cb64fbeae3c763e7e2ea4e6;hpb=aea1b501ceff5a10e9def91febcc80926196391d diff --git a/include/llvm/Support/Allocator.h b/include/llvm/Support/Allocator.h index cdb0fc85582..3243fd9cea5 100644 --- a/include/llvm/Support/Allocator.h +++ b/include/llvm/Support/Allocator.h @@ -15,14 +15,16 @@ #define LLVM_SUPPORT_ALLOCATOR_H #include "llvm/Support/AlignOf.h" +#include "llvm/Support/DataTypes.h" #include "llvm/Support/MathExtras.h" -#include "llvm/System/DataTypes.h" #include #include -#include #include +#include namespace llvm { +template struct ReferenceAdder { typedef T& result; }; +template struct ReferenceAdder { typedef T result; }; class MallocAllocator { public: @@ -77,8 +79,8 @@ class MallocSlabAllocator : public SlabAllocator { public: MallocSlabAllocator() : Allocator() { } virtual ~MallocSlabAllocator(); - virtual MemSlab *Allocate(size_t Size); - virtual void Deallocate(MemSlab *Slab); + virtual MemSlab *Allocate(size_t Size) LLVM_OVERRIDE; + virtual void Deallocate(MemSlab *Slab) LLVM_OVERRIDE; }; /// BumpPtrAllocator - This allocator is useful for containers that need @@ -86,8 +88,8 @@ public: /// allocating memory, and never deletes it until the entire block is dead. This /// makes allocation speedy, but must only be used when the trade-off is ok. class BumpPtrAllocator { - BumpPtrAllocator(const BumpPtrAllocator &); // do not implement - void operator=(const BumpPtrAllocator &); // do not implement + BumpPtrAllocator(const BumpPtrAllocator &) LLVM_DELETED_FUNCTION; + void operator=(const BumpPtrAllocator &) LLVM_DELETED_FUNCTION; /// SlabSize - Allocate data into slabs of this size unless we get an /// allocation above SizeThreshold. @@ -133,6 +135,7 @@ class BumpPtrAllocator { static MallocSlabAllocator DefaultSlabAllocator; + template friend class SpecificBumpPtrAllocator; public: BumpPtrAllocator(size_t size = 4096, size_t threshold = 4096, SlabAllocator &allocator = DefaultSlabAllocator); @@ -174,6 +177,48 @@ public: unsigned GetNumSlabs() const; void PrintStats() const; + + /// Compute the total physical memory allocated by this allocator. + size_t getTotalMemory() const; +}; + +/// SpecificBumpPtrAllocator - Same as BumpPtrAllocator but allows only +/// elements of one type to be allocated. This allows calling the destructor +/// in DestroyAll() and when the allocator is destroyed. +template +class SpecificBumpPtrAllocator { + BumpPtrAllocator Allocator; +public: + SpecificBumpPtrAllocator(size_t size = 4096, size_t threshold = 4096, + SlabAllocator &allocator = BumpPtrAllocator::DefaultSlabAllocator) + : Allocator(size, threshold, allocator) {} + + ~SpecificBumpPtrAllocator() { + DestroyAll(); + } + + /// Call the destructor of each allocated object and deallocate all but the + /// current slab and reset the current pointer to the beginning of it, freeing + /// all memory allocated so far. + void DestroyAll() { + MemSlab *Slab = Allocator.CurSlab; + while (Slab) { + char *End = Slab == Allocator.CurSlab ? Allocator.CurPtr : + (char *)Slab + Slab->Size; + for (char *Ptr = (char*)(Slab+1); Ptr < End; Ptr += sizeof(T)) { + Ptr = Allocator.AlignPtr(Ptr, alignOf()); + if (Ptr + sizeof(T) <= End) + reinterpret_cast(Ptr)->~T(); + } + Slab = Slab->NextPtr; + } + Allocator.Reset(); + } + + /// Allocate space for a specific count of elements. + T *Allocate(size_t num = 1) { + return Allocator.Allocate(num); + } }; } // end namespace llvm @@ -181,19 +226,17 @@ public: inline void *operator new(size_t Size, llvm::BumpPtrAllocator &Allocator) { struct S { char c; -#ifdef __GNUC__ - char x __attribute__((aligned)); -#else union { double D; long double LD; long long L; void *P; } x; -#endif }; return Allocator.Allocate(Size, std::min((size_t)llvm::NextPowerOf2(Size), - (size_t)offsetof(S, x))); + offsetof(S, x))); } +inline void operator delete(void *, llvm::BumpPtrAllocator &) {} + #endif // LLVM_SUPPORT_ALLOCATOR_H