[llvm] r206265 - [Allocator] Pass the size to the deallocation function. This, on some

Chandler Carruth chandlerc at gmail.com
Tue Apr 15 01:59:52 PDT 2014


Author: chandlerc
Date: Tue Apr 15 03:59:52 2014
New Revision: 206265

URL: http://llvm.org/viewvc/llvm-project?rev=206265&view=rev
Log:
[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.

Modified:
    llvm/trunk/include/llvm/ADT/StringMap.h
    llvm/trunk/include/llvm/Support/Allocator.h
    llvm/trunk/include/llvm/Support/YAMLParser.h

Modified: llvm/trunk/include/llvm/ADT/StringMap.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/StringMap.h?rev=206265&r1=206264&r2=206265&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/StringMap.h (original)
+++ llvm/trunk/include/llvm/ADT/StringMap.h Tue Apr 15 03:59:52 2014
@@ -198,8 +198,10 @@ public:
   template<typename AllocatorTy>
   void Destroy(AllocatorTy &Allocator) {
     // Free memory referenced by the item.
+    unsigned AllocSize =
+        static_cast<unsigned>(sizeof(StringMapEntry)) + getKeyLength() + 1;
     this->~StringMapEntry();
-    Allocator.Deallocate(this);
+    Allocator.Deallocate(static_cast<void *>(this), AllocSize);
   }
 
   /// Destroy this object, releasing memory back to the malloc allocator.

Modified: llvm/trunk/include/llvm/Support/Allocator.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/Allocator.h?rev=206265&r1=206264&r2=206265&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/Allocator.h (original)
+++ llvm/trunk/include/llvm/Support/Allocator.h Tue Apr 15 03:59:52 2014
@@ -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<void (AllocatorBase::*)(const void *)>(
+    static_assert(static_cast<void (AllocatorBase::*)(const void *, size_t)>(
                       &AllocatorBase::Deallocate) !=
-                      static_cast<void (DerivedT::*)(const void *)>(
+                      static_cast<void (DerivedT::*)(const void *, size_t)>(
                           &DerivedT::Deallocate),
                   "Class derives from AllocatorBase without implementing the "
                   "core Deallocate(void *) overload!");
 #endif
-    return static_cast<DerivedT *>(this)->Deallocate(Ptr);
+    return static_cast<DerivedT *>(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<typename std::remove_cv<T>::type, void>::value, void>::type
   Deallocate(T *Ptr) {
-    Deallocate(static_cast<const void *>(Ptr));
+    Deallocate(static_cast<const void *>(Ptr), sizeof(T));
   }
 
   /// \brief Allocate space for an array of objects without constructing them.
   template <typename T>
   typename std::enable_if<
       !std::is_same<typename std::remove_cv<T>::type, void>::value, void>::type
-  Deallocate(T *Ptr, size_t /*Num*/) {
-    Deallocate(static_cast<const void *>(Ptr));
+  Deallocate(T *Ptr, size_t Num) {
+    Deallocate(static_cast<const void *>(Ptr), Num * sizeof(T));
   }
 };
 
@@ -125,7 +125,9 @@ public:
   // Pull in base class overloads.
   using AllocatorBase<MallocAllocator>::Allocate;
 
-  void Deallocate(const void *Ptr) { free(const_cast<void *>(Ptr)); }
+  void Deallocate(const void *Ptr, size_t /*Size*/) {
+    free(const_cast<void *>(Ptr));
+  }
 
   // Pull in base class overloads.
   using AllocatorBase<MallocAllocator>::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<BumpPtrAllocatorImpl>::Allocate;
 
-  void Deallocate(const void * /*Ptr*/) {}
+  void Deallocate(const void * /*Ptr*/, size_t /*Size*/) {}
 
   // Pull in base class overloads.
   using AllocatorBase<BumpPtrAllocatorImpl>::Deallocate;

Modified: llvm/trunk/include/llvm/Support/YAMLParser.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/YAMLParser.h?rev=206265&r1=206264&r2=206265&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/YAMLParser.h (original)
+++ llvm/trunk/include/llvm/Support/YAMLParser.h Tue Apr 15 03:59:52 2014
@@ -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:





More information about the llvm-commits mailing list