X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=include%2Fllvm%2FSupport%2FAllocator.h;h=a2ad24ffead93bd995f50101e9c37063e30e9960;hb=a00b80b04c5edb08639c1c6b32e9231fd8b066f7;hp=a58b9db27faafc3b4aa58adb3c5ae9f75eb17ee7;hpb=2a22004f6857c57dc483ea6180f0e1f86854bd13;p=oota-llvm.git diff --git a/include/llvm/Support/Allocator.h b/include/llvm/Support/Allocator.h index a58b9db27fa..a2ad24ffead 100644 --- a/include/llvm/Support/Allocator.h +++ b/include/llvm/Support/Allocator.h @@ -16,13 +16,15 @@ #include "llvm/Support/AlignOf.h" #include "llvm/Support/MathExtras.h" -#include "llvm/System/DataTypes.h" +#include "llvm/Support/DataTypes.h" #include #include #include #include namespace llvm { +template struct ReferenceAdder { typedef T& result; }; +template struct ReferenceAdder { typedef T result; }; class MallocAllocator { public: @@ -133,8 +135,8 @@ class BumpPtrAllocator { static MallocSlabAllocator DefaultSlabAllocator; + template friend class SpecificBumpPtrAllocator; public: - typedef void (*DTorFunction)(void*); BumpPtrAllocator(size_t size = 4096, size_t threshold = 4096, SlabAllocator &allocator = DefaultSlabAllocator); ~BumpPtrAllocator(); @@ -143,11 +145,6 @@ public: /// to the beginning of it, freeing all memory allocated so far. void Reset(); - /// Reset - like Reset(), but call DTorFunction for each allocated - /// object. This assumes that all objects allocated with this allocator - /// had the same size and alignment specified here. - void Reset(size_t Size, size_t Alignment, DTorFunction DTor); - /// Allocate - Allocate space at the specified alignment. /// void *Allocate(size_t Size, size_t Alignment); @@ -180,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 @@ -187,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), offsetof(S, x))); } +inline void operator delete(void *, llvm::BumpPtrAllocator &) {} + #endif // LLVM_SUPPORT_ALLOCATOR_H