LLVM API Documentation

ArrayRecycler.h
Go to the documentation of this file.
00001 //==- llvm/Support/ArrayRecycler.h - Recycling of Arrays ---------*- C++ -*-==//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the ArrayRecycler class template which can recycle small
00011 // arrays allocated from one of the allocators in Allocator.h
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_SUPPORT_ARRAYRECYCLER_H
00016 #define LLVM_SUPPORT_ARRAYRECYCLER_H
00017 
00018 #include "llvm/ADT/SmallVector.h"
00019 #include "llvm/Support/Allocator.h"
00020 #include "llvm/Support/MathExtras.h"
00021 
00022 namespace llvm {
00023 
00024 /// Recycle small arrays allocated from a BumpPtrAllocator.
00025 ///
00026 /// Arrays are allocated in a small number of fixed sizes. For each supported
00027 /// array size, the ArrayRecycler keeps a free list of available arrays.
00028 ///
00029 template<class T, size_t Align = AlignOf<T>::Alignment>
00030 class ArrayRecycler {
00031   // The free list for a given array size is a simple singly linked list.
00032   // We can't use iplist or Recycler here since those classes can't be copied.
00033   struct FreeList {
00034     FreeList *Next;
00035   };
00036 
00037   static_assert(Align >= AlignOf<FreeList>::Alignment, "Object underaligned");
00038   static_assert(sizeof(T) >= sizeof(FreeList), "Objects are too small");
00039 
00040   // Keep a free list for each array size.
00041   SmallVector<FreeList*, 8> Bucket;
00042 
00043   // Remove an entry from the free list in Bucket[Idx] and return it.
00044   // Return NULL if no entries are available.
00045   T *pop(unsigned Idx) {
00046     if (Idx >= Bucket.size())
00047       return nullptr;
00048     FreeList *Entry = Bucket[Idx];
00049     if (!Entry)
00050       return nullptr;
00051     Bucket[Idx] = Entry->Next;
00052     return reinterpret_cast<T*>(Entry);
00053   }
00054 
00055   // Add an entry to the free list at Bucket[Idx].
00056   void push(unsigned Idx, T *Ptr) {
00057     assert(Ptr && "Cannot recycle NULL pointer");
00058     FreeList *Entry = reinterpret_cast<FreeList*>(Ptr);
00059     if (Idx >= Bucket.size())
00060       Bucket.resize(size_t(Idx) + 1);
00061     Entry->Next = Bucket[Idx];
00062     Bucket[Idx] = Entry;
00063   }
00064 
00065 public:
00066   /// The size of an allocated array is represented by a Capacity instance.
00067   ///
00068   /// This class is much smaller than a size_t, and it provides methods to work
00069   /// with the set of legal array capacities.
00070   class Capacity {
00071     uint8_t Index;
00072     explicit Capacity(uint8_t idx) : Index(idx) {}
00073 
00074   public:
00075     Capacity() : Index(0) {}
00076 
00077     /// Get the capacity of an array that can hold at least N elements.
00078     static Capacity get(size_t N) {
00079       return Capacity(N ? Log2_64_Ceil(N) : 0);
00080     }
00081 
00082     /// Get the number of elements in an array with this capacity.
00083     size_t getSize() const { return size_t(1u) << Index; }
00084 
00085     /// Get the bucket number for this capacity.
00086     unsigned getBucket() const { return Index; }
00087 
00088     /// Get the next larger capacity. Large capacities grow exponentially, so
00089     /// this function can be used to reallocate incrementally growing vectors
00090     /// in amortized linear time.
00091     Capacity getNext() const { return Capacity(Index + 1); }
00092   };
00093 
00094   ~ArrayRecycler() {
00095     // The client should always call clear() so recycled arrays can be returned
00096     // to the allocator.
00097     assert(Bucket.empty() && "Non-empty ArrayRecycler deleted!");
00098   }
00099 
00100   /// Release all the tracked allocations to the allocator. The recycler must
00101   /// be free of any tracked allocations before being deleted.
00102   template<class AllocatorType>
00103   void clear(AllocatorType &Allocator) {
00104     for (; !Bucket.empty(); Bucket.pop_back())
00105       while (T *Ptr = pop(Bucket.size() - 1))
00106         Allocator.Deallocate(Ptr);
00107   }
00108 
00109   /// Special case for BumpPtrAllocator which has an empty Deallocate()
00110   /// function.
00111   ///
00112   /// There is no need to traverse the free lists, pulling all the objects into
00113   /// cache.
00114   void clear(BumpPtrAllocator&) {
00115     Bucket.clear();
00116   }
00117 
00118   /// Allocate an array of at least the requested capacity.
00119   ///
00120   /// Return an existing recycled array, or allocate one from Allocator if
00121   /// none are available for recycling.
00122   ///
00123   template<class AllocatorType>
00124   T *allocate(Capacity Cap, AllocatorType &Allocator) {
00125     // Try to recycle an existing array.
00126     if (T *Ptr = pop(Cap.getBucket()))
00127       return Ptr;
00128     // Nope, get more memory.
00129     return static_cast<T*>(Allocator.Allocate(sizeof(T)*Cap.getSize(), Align));
00130   }
00131 
00132   /// Deallocate an array with the specified Capacity.
00133   ///
00134   /// Cap must be the same capacity that was given to allocate().
00135   ///
00136   void deallocate(Capacity Cap, T *Ptr) {
00137     push(Cap.getBucket(), Ptr);
00138   }
00139 };
00140 
00141 } // end llvm namespace
00142 
00143 #endif