1 //===-- BumpVector.h - Vector-like ADT that uses bump allocation --*- C++ -*-=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provides BumpVector, a vector-like ADT whose contents are
11 // allocated from a BumpPtrAllocator.
13 //===----------------------------------------------------------------------===//
15 // FIXME: Most of this is copy-and-paste from SmallVector.h. We can
16 // refactor this core logic into something common that is shared between
17 // the two. The main thing that is different is the allocation strategy.
19 #ifndef LLVM_CLANG_ANALYSIS_SUPPORT_BUMPVECTOR_H
20 #define LLVM_CLANG_ANALYSIS_SUPPORT_BUMPVECTOR_H
22 #include "llvm/ADT/PointerIntPair.h"
23 #include "llvm/Support/Allocator.h"
24 #include "llvm/Support/type_traits.h"
32 class BumpVectorContext {
33 llvm::PointerIntPair<llvm::BumpPtrAllocator*, 1> Alloc;
35 /// Construct a new BumpVectorContext that creates a new BumpPtrAllocator
36 /// and destroys it when the BumpVectorContext object is destroyed.
37 BumpVectorContext() : Alloc(new llvm::BumpPtrAllocator(), 1) {}
39 BumpVectorContext(BumpVectorContext &&Other) : Alloc(Other.Alloc) {
40 Other.Alloc.setInt(false);
41 Other.Alloc.setPointer(nullptr);
44 /// Construct a new BumpVectorContext that reuses an existing
45 /// BumpPtrAllocator. This BumpPtrAllocator is not destroyed when the
46 /// BumpVectorContext object is destroyed.
47 BumpVectorContext(llvm::BumpPtrAllocator &A) : Alloc(&A, 0) {}
49 ~BumpVectorContext() {
51 delete Alloc.getPointer();
54 llvm::BumpPtrAllocator &getAllocator() { return *Alloc.getPointer(); }
59 T *Begin, *End, *Capacity;
61 // Default ctor - Initialize to empty.
62 explicit BumpVector(BumpVectorContext &C, unsigned N)
63 : Begin(nullptr), End(nullptr), Capacity(nullptr) {
68 if (std::is_class<T>::value) {
69 // Destroy the constructed elements in the vector.
70 destroy_range(Begin, End);
74 typedef size_t size_type;
75 typedef ptrdiff_t difference_type;
78 typedef const T* const_iterator;
80 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
81 typedef std::reverse_iterator<iterator> reverse_iterator;
84 typedef const T& const_reference;
86 typedef const T* const_pointer;
88 // forward iterator creation methods.
89 iterator begin() { return Begin; }
90 const_iterator begin() const { return Begin; }
91 iterator end() { return End; }
92 const_iterator end() const { return End; }
94 // reverse iterator creation methods.
95 reverse_iterator rbegin() { return reverse_iterator(end()); }
96 const_reverse_iterator rbegin() const{ return const_reverse_iterator(end()); }
97 reverse_iterator rend() { return reverse_iterator(begin()); }
98 const_reverse_iterator rend() const { return const_reverse_iterator(begin());}
100 bool empty() const { return Begin == End; }
101 size_type size() const { return End-Begin; }
103 reference operator[](unsigned idx) {
104 assert(Begin + idx < End);
107 const_reference operator[](unsigned idx) const {
108 assert(Begin + idx < End);
115 const_reference front() const {
122 const_reference back() const {
138 if (std::is_class<T>::value) {
139 destroy_range(Begin, End);
144 /// data - Return a pointer to the vector's buffer, even if empty().
146 return pointer(Begin);
149 /// data - Return a pointer to the vector's buffer, even if empty().
150 const_pointer data() const {
151 return const_pointer(Begin);
154 void push_back(const_reference Elt, BumpVectorContext &C) {
155 if (End < Capacity) {
165 /// insert - Insert some number of copies of element into a position. Return
166 /// iterator to position after last inserted copy.
167 iterator insert(iterator I, size_t Cnt, const_reference E,
168 BumpVectorContext &C) {
169 assert (I >= Begin && I <= End && "Iterator out of bounds.");
170 if (End + Cnt <= Capacity) {
172 move_range_right(I, End, Cnt);
173 construct_range(I, I + Cnt, E);
177 ptrdiff_t D = I - Begin;
178 grow(C, size() + Cnt);
183 void reserve(BumpVectorContext &C, unsigned N) {
184 if (unsigned(Capacity-Begin) < N)
188 /// capacity - Return the total number of elements in the currently allocated
190 size_t capacity() const { return Capacity - Begin; }
193 /// grow - double the size of the allocated memory, guaranteeing space for at
194 /// least one more element or MinSize if specified.
195 void grow(BumpVectorContext &C, size_type MinSize = 1);
197 void construct_range(T *S, T *E, const T &Elt) {
202 void destroy_range(T *S, T *E) {
209 void move_range_right(T *S, T *E, size_t D) {
210 for (T *I = E + D - 1, *IL = S + D - 1; I != IL; --I) {
218 // Define this out-of-line to dissuade the C++ compiler from inlining it.
219 template <typename T>
220 void BumpVector<T>::grow(BumpVectorContext &C, size_t MinSize) {
221 size_t CurCapacity = Capacity-Begin;
222 size_t CurSize = size();
223 size_t NewCapacity = 2*CurCapacity;
224 if (NewCapacity < MinSize)
225 NewCapacity = MinSize;
227 // Allocate the memory from the BumpPtrAllocator.
228 T *NewElts = C.getAllocator().template Allocate<T>(NewCapacity);
230 // Copy the elements over.
232 if (std::is_class<T>::value) {
233 std::uninitialized_copy(Begin, End, NewElts);
234 // Destroy the original elements.
235 destroy_range(Begin, End);
237 // Use memcpy for PODs (std::uninitialized_copy optimizes to memmove).
238 memcpy(NewElts, Begin, CurSize * sizeof(T));
242 // For now, leak 'Begin'. We can add it back to a freelist in
243 // BumpVectorContext.
245 End = NewElts+CurSize;
246 Capacity = Begin+NewCapacity;
249 } // end: clang namespace