From: Chandler Carruth Date: Tue, 15 Apr 2014 08:59:52 +0000 (+0000) Subject: [Allocator] Pass the size to the deallocation function. This, on some X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=47b284ce3539ef216e0ab994f4b7d762cfb1c69a;p=oota-llvm.git [Allocator] Pass the size to the deallocation function. This, on some allocation libraries, may allow more efficient allocation and deallocation. It at least makes the interface implementable by the JIT memory manager. However, this highlights problematic overloading between the void* and the T* deallocation functions. I'm looking into a better way to do this, but as it happens, it comes up rarely in the codebase. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@206265 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/ADT/StringMap.h b/include/llvm/ADT/StringMap.h index d4daf531782..f7f940944cf 100644 --- a/include/llvm/ADT/StringMap.h +++ b/include/llvm/ADT/StringMap.h @@ -198,8 +198,10 @@ public: template void Destroy(AllocatorTy &Allocator) { // Free memory referenced by the item. + unsigned AllocSize = + static_cast(sizeof(StringMapEntry)) + getKeyLength() + 1; this->~StringMapEntry(); - Allocator.Deallocate(this); + Allocator.Deallocate(static_cast(this), AllocSize); } /// Destroy this object, releasing memory back to the malloc allocator. diff --git a/include/llvm/Support/Allocator.h b/include/llvm/Support/Allocator.h index 617ab9fd7a3..5565c1ccf12 100644 --- a/include/llvm/Support/Allocator.h +++ b/include/llvm/Support/Allocator.h @@ -63,16 +63,16 @@ public: /// \brief Deallocate \a Ptr to \a Size bytes of memory allocated by this /// allocator. - void Deallocate(const void *Ptr) { + void Deallocate(const void *Ptr, size_t Size) { #ifdef __clang__ - static_assert(static_cast( + static_assert(static_cast( &AllocatorBase::Deallocate) != - static_cast( + static_cast( &DerivedT::Deallocate), "Class derives from AllocatorBase without implementing the " "core Deallocate(void *) overload!"); #endif - return static_cast(this)->Deallocate(Ptr); + return static_cast(this)->Deallocate(Ptr, Size); } // The rest of these methods are helpers that redirect to one of the above @@ -101,15 +101,15 @@ public: typename std::enable_if< !std::is_same::type, void>::value, void>::type Deallocate(T *Ptr) { - Deallocate(static_cast(Ptr)); + Deallocate(static_cast(Ptr), sizeof(T)); } /// \brief Allocate space for an array of objects without constructing them. template typename std::enable_if< !std::is_same::type, void>::value, void>::type - Deallocate(T *Ptr, size_t /*Num*/) { - Deallocate(static_cast(Ptr)); + Deallocate(T *Ptr, size_t Num) { + Deallocate(static_cast(Ptr), Num * sizeof(T)); } }; @@ -125,7 +125,9 @@ public: // Pull in base class overloads. using AllocatorBase::Allocate; - void Deallocate(const void *Ptr) { free(const_cast(Ptr)); } + void Deallocate(const void *Ptr, size_t /*Size*/) { + free(const_cast(Ptr)); + } // Pull in base class overloads. using AllocatorBase::Deallocate; @@ -143,7 +145,7 @@ class MallocSlabAllocator { public: void *Allocate(size_t Size) { return Allocator.Allocate(Size, 0); } - void Deallocate(void *Slab, size_t Size) { Allocator.Deallocate(Slab); } + void Deallocate(void *Slab, size_t Size) { Allocator.Deallocate(Slab, Size); } }; namespace detail { @@ -260,7 +262,7 @@ public: // Pull in base class overloads. using AllocatorBase::Allocate; - void Deallocate(const void * /*Ptr*/) {} + void Deallocate(const void * /*Ptr*/, size_t /*Size*/) {} // Pull in base class overloads. using AllocatorBase::Deallocate; diff --git a/include/llvm/Support/YAMLParser.h b/include/llvm/Support/YAMLParser.h index 6b1d4eaf6a3..c39874cbd2c 100644 --- a/include/llvm/Support/YAMLParser.h +++ b/include/llvm/Support/YAMLParser.h @@ -150,8 +150,8 @@ public: return Alloc.Allocate(Size, Alignment); } - void operator delete(void *Ptr, BumpPtrAllocator &Alloc, size_t) throw() { - Alloc.Deallocate(Ptr); + void operator delete(void *Ptr, BumpPtrAllocator &Alloc, size_t Size) throw() { + Alloc.Deallocate(Ptr, Size); } protected: