clang API Documentation

BumpVector.h
Go to the documentation of this file.
00001 //===-- BumpVector.h - Vector-like ADT that uses bump allocation --*- 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 provides BumpVector, a vector-like ADT whose contents are
00011 //  allocated from a BumpPtrAllocator.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 // FIXME: Most of this is copy-and-paste from SmallVector.h.  We can
00016 // refactor this core logic into something common that is shared between
00017 // the two.  The main thing that is different is the allocation strategy.
00018 
00019 #ifndef LLVM_CLANG_ANALYSIS_SUPPORT_BUMPVECTOR_H
00020 #define LLVM_CLANG_ANALYSIS_SUPPORT_BUMPVECTOR_H
00021 
00022 #include "llvm/ADT/PointerIntPair.h"
00023 #include "llvm/Support/Allocator.h"
00024 #include "llvm/Support/type_traits.h"
00025 #include <algorithm>
00026 #include <cstring>
00027 #include <iterator>
00028 #include <memory>
00029 
00030 namespace clang {
00031   
00032 class BumpVectorContext {
00033   llvm::PointerIntPair<llvm::BumpPtrAllocator*, 1> Alloc;
00034 public:
00035   /// Construct a new BumpVectorContext that creates a new BumpPtrAllocator
00036   /// and destroys it when the BumpVectorContext object is destroyed.
00037   BumpVectorContext() : Alloc(new llvm::BumpPtrAllocator(), 1) {}
00038   
00039   /// Construct a new BumpVectorContext that reuses an existing
00040   /// BumpPtrAllocator.  This BumpPtrAllocator is not destroyed when the
00041   /// BumpVectorContext object is destroyed.
00042   BumpVectorContext(llvm::BumpPtrAllocator &A) : Alloc(&A, 0) {}
00043   
00044   ~BumpVectorContext() {
00045     if (Alloc.getInt())
00046       delete Alloc.getPointer();
00047   }
00048   
00049   llvm::BumpPtrAllocator &getAllocator() { return *Alloc.getPointer(); }
00050 };
00051   
00052 template<typename T>
00053 class BumpVector {
00054   T *Begin, *End, *Capacity;
00055 public:
00056   // Default ctor - Initialize to empty.
00057   explicit BumpVector(BumpVectorContext &C, unsigned N)
00058   : Begin(nullptr), End(nullptr), Capacity(nullptr) {
00059     reserve(C, N);
00060   }
00061   
00062   ~BumpVector() {
00063     if (std::is_class<T>::value) {
00064       // Destroy the constructed elements in the vector.
00065       destroy_range(Begin, End);
00066     }
00067   }
00068   
00069   typedef size_t size_type;
00070   typedef ptrdiff_t difference_type;
00071   typedef T value_type;
00072   typedef T* iterator;
00073   typedef const T* const_iterator;
00074   
00075   typedef std::reverse_iterator<const_iterator>  const_reverse_iterator;
00076   typedef std::reverse_iterator<iterator>  reverse_iterator;
00077   
00078   typedef T& reference;
00079   typedef const T& const_reference;
00080   typedef T* pointer;
00081   typedef const T* const_pointer;
00082   
00083   // forward iterator creation methods.
00084   iterator begin() { return Begin; }
00085   const_iterator begin() const { return Begin; }
00086   iterator end() { return End; }
00087   const_iterator end() const { return End; }
00088   
00089   // reverse iterator creation methods.
00090   reverse_iterator rbegin()            { return reverse_iterator(end()); }
00091   const_reverse_iterator rbegin() const{ return const_reverse_iterator(end()); }
00092   reverse_iterator rend()              { return reverse_iterator(begin()); }
00093   const_reverse_iterator rend() const { return const_reverse_iterator(begin());}
00094     
00095   bool empty() const { return Begin == End; }
00096   size_type size() const { return End-Begin; }
00097 
00098   reference operator[](unsigned idx) {
00099     assert(Begin + idx < End);
00100     return Begin[idx];
00101   }
00102   const_reference operator[](unsigned idx) const {
00103     assert(Begin + idx < End);
00104     return Begin[idx];
00105   }
00106   
00107   reference front() {
00108     return begin()[0];
00109   }
00110   const_reference front() const {
00111     return begin()[0];
00112   }
00113   
00114   reference back() {
00115     return end()[-1];
00116   }
00117   const_reference back() const {
00118     return end()[-1];
00119   }
00120   
00121   void pop_back() {
00122     --End;
00123     End->~T();
00124   }
00125   
00126   T pop_back_val() {
00127     T Result = back();
00128     pop_back();
00129     return Result;
00130   }
00131   
00132   void clear() {
00133     if (std::is_class<T>::value) {
00134       destroy_range(Begin, End);
00135     }
00136     End = Begin;
00137   }
00138   
00139   /// data - Return a pointer to the vector's buffer, even if empty().
00140   pointer data() {
00141     return pointer(Begin);
00142   }
00143   
00144   /// data - Return a pointer to the vector's buffer, even if empty().
00145   const_pointer data() const {
00146     return const_pointer(Begin);
00147   }
00148   
00149   void push_back(const_reference Elt, BumpVectorContext &C) {
00150     if (End < Capacity) {
00151     Retry:
00152       new (End) T(Elt);
00153       ++End;
00154       return;
00155     }
00156     grow(C);
00157     goto Retry;    
00158   }
00159 
00160   /// insert - Insert some number of copies of element into a position. Return
00161   /// iterator to position after last inserted copy.
00162   iterator insert(iterator I, size_t Cnt, const_reference E,
00163       BumpVectorContext &C) {
00164     assert (I >= Begin && I <= End && "Iterator out of bounds.");
00165     if (End + Cnt <= Capacity) {
00166     Retry:
00167       move_range_right(I, End, Cnt);
00168       construct_range(I, I + Cnt, E);
00169       End += Cnt;
00170       return I + Cnt;
00171     }
00172     ptrdiff_t D = I - Begin;
00173     grow(C, size() + Cnt);
00174     I = Begin + D;
00175     goto Retry;
00176   }
00177 
00178   void reserve(BumpVectorContext &C, unsigned N) {
00179     if (unsigned(Capacity-Begin) < N)
00180       grow(C, N);
00181   }
00182 
00183   /// capacity - Return the total number of elements in the currently allocated
00184   /// buffer.
00185   size_t capacity() const { return Capacity - Begin; }  
00186     
00187 private:
00188   /// grow - double the size of the allocated memory, guaranteeing space for at
00189   /// least one more element or MinSize if specified.
00190   void grow(BumpVectorContext &C, size_type MinSize = 1);
00191   
00192   void construct_range(T *S, T *E, const T &Elt) {
00193     for (; S != E; ++S)
00194       new (S) T(Elt);
00195   }
00196   
00197   void destroy_range(T *S, T *E) {
00198     while (S != E) {
00199       --E;
00200       E->~T();
00201     }
00202   }
00203 
00204   void move_range_right(T *S, T *E, size_t D) {
00205     for (T *I = E + D - 1, *IL = S + D - 1; I != IL; --I) {
00206       --E;
00207       new (I) T(*E);
00208       E->~T();
00209     }
00210   }
00211 };
00212   
00213 // Define this out-of-line to dissuade the C++ compiler from inlining it.
00214 template <typename T>
00215 void BumpVector<T>::grow(BumpVectorContext &C, size_t MinSize) {
00216   size_t CurCapacity = Capacity-Begin;
00217   size_t CurSize = size();
00218   size_t NewCapacity = 2*CurCapacity;
00219   if (NewCapacity < MinSize)
00220     NewCapacity = MinSize;
00221 
00222   // Allocate the memory from the BumpPtrAllocator.
00223   T *NewElts = C.getAllocator().template Allocate<T>(NewCapacity);
00224   
00225   // Copy the elements over.
00226   if (std::is_class<T>::value) {
00227     std::uninitialized_copy(Begin, End, NewElts);
00228     // Destroy the original elements.
00229     destroy_range(Begin, End);
00230   }
00231   else {
00232     // Use memcpy for PODs (std::uninitialized_copy optimizes to memmove).
00233     memcpy(NewElts, Begin, CurSize * sizeof(T));
00234   }
00235 
00236   // For now, leak 'Begin'.  We can add it back to a freelist in
00237   // BumpVectorContext.
00238   Begin = NewElts;
00239   End = NewElts+CurSize;
00240   Capacity = Begin+NewCapacity;
00241 }
00242 
00243 } // end: clang namespace
00244 #endif