diff options
author | Benjamin Kramer <benny.kra@googlemail.com> | 2010-03-30 20:16:45 +0000 |
---|---|---|
committer | Benjamin Kramer <benny.kra@googlemail.com> | 2010-03-30 20:16:45 +0000 |
commit | 991de14dd62dcbab4b31357ae22dc5b053ba50a0 (patch) | |
tree | bda68de401067e89361cd37e175d087fe0b3e6f1 /include/llvm/Support/Allocator.h | |
parent | 820580dfa9ed3e70d3eda06138a59fd903f61b7a (diff) | |
download | external_llvm-991de14dd62dcbab4b31357ae22dc5b053ba50a0.zip external_llvm-991de14dd62dcbab4b31357ae22dc5b053ba50a0.tar.gz external_llvm-991de14dd62dcbab4b31357ae22dc5b053ba50a0.tar.bz2 |
Introduce SpecificBumpPtrAllocator, a wrapper for BumpPtrAllocator which allows
only a single type of object to be allocated. Use it to make VNInfo destruction
typesafe.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@99919 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'include/llvm/Support/Allocator.h')
-rw-r--r-- | include/llvm/Support/Allocator.h | 46 |
1 files changed, 40 insertions, 6 deletions
diff --git a/include/llvm/Support/Allocator.h b/include/llvm/Support/Allocator.h index a58b9db..bd38180 100644 --- a/include/llvm/Support/Allocator.h +++ b/include/llvm/Support/Allocator.h @@ -133,8 +133,8 @@ class BumpPtrAllocator { static MallocSlabAllocator DefaultSlabAllocator; + template<typename T> friend class SpecificBumpPtrAllocator; public: - typedef void (*DTorFunction)(void*); BumpPtrAllocator(size_t size = 4096, size_t threshold = 4096, SlabAllocator &allocator = DefaultSlabAllocator); ~BumpPtrAllocator(); @@ -143,11 +143,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); @@ -182,6 +177,45 @@ public: void PrintStats() 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 <typename T> +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<T>()); + if (Ptr + sizeof(T) <= End) + reinterpret_cast<T*>(Ptr)->~T(); + } + Slab = Slab->NextPtr; + } + Allocator.Reset(); + } + + /// Allocate space for a specific count of elements. + T *Allocate(size_t num = 1) { + return Allocator.Allocate<T>(num); + } +}; + } // end namespace llvm inline void *operator new(size_t Size, llvm::BumpPtrAllocator &Allocator) { |