Fix an invalid check for duplicate option categories.
[oota-llvm.git] / include / llvm / Support / Allocator.h
index a58b9db27faafc3b4aa58adb3c5ae9f75eb17ee7..275086bffdfd6e5f43727a73b5192faac2eec6f3 100644 (file)
 #ifndef LLVM_SUPPORT_ALLOCATOR_H
 #define LLVM_SUPPORT_ALLOCATOR_H
 
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/StringRef.h"
 #include "llvm/Support/AlignOf.h"
+#include "llvm/Support/DataTypes.h"
 #include "llvm/Support/MathExtras.h"
-#include "llvm/System/DataTypes.h"
 #include <algorithm>
 #include <cassert>
-#include <cstdlib>
 #include <cstddef>
+#include <cstdlib>
 
 namespace llvm {
+template <typename T> struct ReferenceAdder { typedef T& result; };
+template <typename T> struct ReferenceAdder<T&> { typedef T result; };
 
 class MallocAllocator {
 public:
@@ -77,8 +81,8 @@ class MallocSlabAllocator : public SlabAllocator {
 public:
   MallocSlabAllocator() : Allocator() { }
   virtual ~MallocSlabAllocator();
-  virtual MemSlab *Allocate(size_t Size);
-  virtual void Deallocate(MemSlab *Slab);
+  virtual MemSlab *Allocate(size_t Size) LLVM_OVERRIDE;
+  virtual void Deallocate(MemSlab *Slab) LLVM_OVERRIDE;
 };
 
 /// BumpPtrAllocator - This allocator is useful for containers that need
@@ -86,8 +90,8 @@ public:
 /// allocating memory, and never deletes it until the entire block is dead. This
 /// makes allocation speedy, but must only be used when the trade-off is ok.
 class BumpPtrAllocator {
-  BumpPtrAllocator(const BumpPtrAllocator &); // do not implement
-  void operator=(const BumpPtrAllocator &);   // do not implement
+  BumpPtrAllocator(const BumpPtrAllocator &) LLVM_DELETED_FUNCTION;
+  void operator=(const BumpPtrAllocator &) LLVM_DELETED_FUNCTION;
 
   /// SlabSize - Allocate data into slabs of this size unless we get an
   /// allocation above SizeThreshold.
@@ -97,6 +101,9 @@ class BumpPtrAllocator {
   /// allocate a separate slab.
   size_t SizeThreshold;
 
+  /// \brief the default allocator used if one is not provided
+  MallocSlabAllocator DefaultSlabAllocator;
+
   /// Allocator - The underlying allocator we use to get slabs of memory.  This
   /// defaults to MallocSlabAllocator, which wraps malloc, but it could be
   /// changed to use a custom allocator.
@@ -131,23 +138,16 @@ class BumpPtrAllocator {
   /// one.
   void DeallocateSlabs(MemSlab *Slab);
 
-  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(size_t size = 4096, size_t threshold = 4096);
+  BumpPtrAllocator(size_t size, size_t threshold, SlabAllocator &allocator);
   ~BumpPtrAllocator();
 
   /// Reset - Deallocate all but the current slab and reset the current pointer
   /// 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 +180,84 @@ public:
   unsigned GetNumSlabs() const;
 
   void PrintStats() const;
+  
+  
+  /// Allocate space and copy content into it.
+  void *allocateCopy(const void *Src, size_t Size, size_t Alignment=1) {
+    void *P = Allocate(Size, Alignment);
+    memcpy(P, Src, Size);
+    return P;
+  }
+  
+  /// Allocate space for an array of type T, and use std::copy()
+  /// to copy the array contents.
+  template <typename T>
+  typename enable_if<isPodLike<T>, T*>::type
+  allocateCopy(const T Src[], size_t Num) {
+    T *P = Allocate<T>(Num);
+    std::copy(Src, &Src[Num], P);
+    return P;
+  }
+
+  /// Copy a StringRef by allocating copy in BumpPtrAllocator.
+  StringRef allocateCopy(StringRef Str) {
+    size_t Length = Str.size();
+    char *P = allocateCopy<char>(Str.data(), Length);
+    return StringRef(P, Length);
+  }
+
+  /// Copy a ArrayRef<T> by allocating copy in BumpPtrAllocator.
+  template <typename T>
+  typename enable_if<isPodLike<T>, ArrayRef<T> >::type
+  allocateCopy(ArrayRef<T> Src) {
+    size_t Length = Src.size();
+    T *P = allocateCopy<T>(Src.data(), Length);
+    return makeArrayRef(P, Length);
+  }
+
+  /// 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 <typename T>
+class SpecificBumpPtrAllocator {
+  BumpPtrAllocator Allocator;
+public:
+  SpecificBumpPtrAllocator(size_t size = 4096, size_t threshold = 4096)
+    : Allocator(size, threshold) {}
+  SpecificBumpPtrAllocator(size_t size, size_t threshold,
+                           SlabAllocator &allocator)
+    : 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
@@ -187,19 +265,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