[llvm] r206230 - [Allocator] Add Deallocate support to the AllocatorBase CRTP class,

Chandler Carruth chandlerc at gmail.com
Mon Apr 14 17:47:47 PDT 2014


Author: chandlerc
Date: Mon Apr 14 19:47:47 2014
New Revision: 206230

URL: http://llvm.org/viewvc/llvm-project?rev=206230&view=rev
Log:
[Allocator] Add Deallocate support to the AllocatorBase CRTP class,
along with templated overloads much like we have for Allocate. These
will facilitate switching the Deallocate interface of all the Allocator
classes to accept the size by pre-filling it from the type size where we
can do so. I plan to convert several uses to the template variants in
subsequent patches prior to adding the Size parameter.

No functionality changed, WIP.

Modified:
    llvm/trunk/include/llvm/Support/Allocator.h

Modified: llvm/trunk/include/llvm/Support/Allocator.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/Allocator.h?rev=206230&r1=206229&r2=206230&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/Allocator.h (original)
+++ llvm/trunk/include/llvm/Support/Allocator.h Mon Apr 14 19:47:47 2014
@@ -61,6 +61,23 @@ public:
     return static_cast<DerivedT *>(this)->Allocate(Size, Alignment);
   }
 
+  /// \brief Deallocate \a Ptr to \a Size bytes of memory allocated by this
+  /// allocator.
+  void Deallocate(const void *Ptr) {
+#ifdef __clang__
+    static_assert(static_cast<void (AllocatorBase::*)(const void *)>(
+                      &AllocatorBase::Deallocate) !=
+                      static_cast<void (DerivedT::*)(const void *)>(
+                          &DerivedT::Deallocate),
+                  "Class derives from AllocatorBase without implementing the "
+                  "core Deallocate(void *) overload!");
+#endif
+    return static_cast<DerivedT *>(this)->Deallocate(Ptr);
+  }
+
+  // The rest of these methods are helpers that redirect to one of the above
+  // core methods.
+
   /// \brief Allocate space for one object without constructing it.
   template <typename T> T *Allocate() {
     return static_cast<T *>(Allocate(sizeof(T), AlignOf<T>::Alignment));
@@ -78,6 +95,16 @@ public:
     size_t EltSize = (sizeof(T) + Alignment - 1) & (-Alignment);
     return static_cast<T *>(Allocate(Num * EltSize, Alignment));
   }
+
+  /// \brief Deallocate space for one object without destroying it.
+  template <typename T> void Deallocate(T *Ptr) {
+    Deallocate(static_cast<const void *>(Ptr));
+  }
+
+  /// \brief Allocate space for an array of objects without constructing them.
+  template <typename T> void Deallocate(T *Ptr, size_t /*Num*/) {
+    Deallocate(static_cast<const void *>(Ptr));
+  }
 };
 
 class MallocAllocator : public AllocatorBase<MallocAllocator> {
@@ -94,6 +121,9 @@ public:
 
   void Deallocate(const void *Ptr) { free(const_cast<void *>(Ptr)); }
 
+  // Pull in base class overloads.
+  using AllocatorBase<MallocAllocator>::Deallocate;
+
   void PrintStats() const {}
 };
 
@@ -226,6 +256,9 @@ public:
 
   void Deallocate(const void * /*Ptr*/) {}
 
+  // Pull in base class overloads.
+  using AllocatorBase<BumpPtrAllocatorImpl>::Deallocate;
+
   size_t GetNumSlabs() const { return Slabs.size() + CustomSizedSlabs.size(); }
 
   size_t getTotalMemory() const {





More information about the llvm-commits mailing list