/// This is all the non-templated stuff common to all SmallVectors.
class SmallVectorBase {
protected:
- void *BeginX;
- unsigned Size = 0, Capacity;
+ void *BeginX, *EndX, *CapacityX;
- SmallVectorBase() = delete;
- SmallVectorBase(void *FirstEl, size_t Capacity)
- : BeginX(FirstEl), Capacity(Capacity) {}
+protected:
+ SmallVectorBase(void *FirstEl, size_t Size)
+ : BeginX(FirstEl), EndX(FirstEl), CapacityX((char*)FirstEl+Size) {}
/// This is an implementation of the grow() method which only works
/// on POD-like data types and is out of line to reduce code duplication.
- void grow_pod(void *FirstEl, size_t MinCapacity, size_t TSize);
+ void grow_pod(void *FirstEl, size_t MinSizeInBytes, size_t TSize);
public:
- size_t size() const { return Size; }
- size_t capacity() const { return Capacity; }
-
- LLVM_NODISCARD bool empty() const { return !Size; }
+ /// This returns size()*sizeof(T).
+ size_t size_in_bytes() const {
+ return size_t((char*)EndX - (char*)BeginX);
+ }
- /// Set the array size to \p N, which the current array must have enough
- /// capacity for.
- ///
- /// This does not construct or destroy any elements in the vector.
- ///
- /// Clients can use this in conjunction with capacity() to write past the end
- /// of the buffer when they know that more elements are available, and only
- /// update the size later. This avoids the cost of value initializing elements
- /// which will only be overwritten.
- void set_size(size_t Size) {
- assert(Size <= capacity());
- this->Size = Size;
+ /// capacity_in_bytes - This returns capacity()*sizeof(T).
+ size_t capacity_in_bytes() const {
+ return size_t((char*)CapacityX - (char*)BeginX);
}
+
+ LLVM_NODISCARD bool empty() const { return BeginX == EndX; }
};
/// This is the part of SmallVectorTemplateBase which does not depend on whether
protected:
SmallVectorTemplateCommon(size_t Size) : SmallVectorBase(&FirstEl, Size) {}
- void grow_pod(size_t MinCapacity, size_t TSize) {
- SmallVectorBase::grow_pod(&FirstEl, MinCapacity, TSize);
+ void grow_pod(size_t MinSizeInBytes, size_t TSize) {
+ SmallVectorBase::grow_pod(&FirstEl, MinSizeInBytes, TSize);
}
/// Return true if this is a smallvector which has not had dynamic
/// Put this vector in a state of being small.
void resetToSmall() {
- BeginX = &FirstEl;
- Size = Capacity = 0; // FIXME: Setting Capacity to 0 is suspect.
+ BeginX = EndX = CapacityX = &FirstEl;
}
+ void setEnd(T *P) { this->EndX = P; }
+
public:
using size_type = size_t;
using difference_type = ptrdiff_t;
LLVM_ATTRIBUTE_ALWAYS_INLINE
const_iterator begin() const { return (const_iterator)this->BeginX; }
LLVM_ATTRIBUTE_ALWAYS_INLINE
- iterator end() { return begin() + size(); }
+ iterator end() { return (iterator)this->EndX; }
LLVM_ATTRIBUTE_ALWAYS_INLINE
- const_iterator end() const { return begin() + size(); }
+ const_iterator end() const { return (const_iterator)this->EndX; }
+protected:
+ iterator capacity_ptr() { return (iterator)this->CapacityX; }
+ const_iterator capacity_ptr() const { return (const_iterator)this->CapacityX;}
+
+public:
// reverse iterator creation methods.
reverse_iterator rbegin() { return reverse_iterator(end()); }
const_reverse_iterator rbegin() const{ return const_reverse_iterator(end()); }
reverse_iterator rend() { return reverse_iterator(begin()); }
const_reverse_iterator rend() const { return const_reverse_iterator(begin());}
- size_type size_in_bytes() const { return size() * sizeof(T); }
+ LLVM_ATTRIBUTE_ALWAYS_INLINE
+ size_type size() const { return end()-begin(); }
size_type max_size() const { return size_type(-1) / sizeof(T); }
- size_t capacity_in_bytes() const { return capacity() * sizeof(T); }
+ /// Return the total number of elements in the currently allocated buffer.
+ size_t capacity() const { return capacity_ptr() - begin(); }
/// Return a pointer to the vector's buffer, even if empty().
pointer data() { return pointer(begin()); }
public:
void push_back(const T &Elt) {
- if (LLVM_UNLIKELY(this->size() >= this->capacity()))
+ if (LLVM_UNLIKELY(this->EndX >= this->CapacityX))
this->grow();
::new ((void*) this->end()) T(Elt);
- this->set_size(this->size() + 1);
+ this->setEnd(this->end()+1);
}
void push_back(T &&Elt) {
- if (LLVM_UNLIKELY(this->size() >= this->capacity()))
+ if (LLVM_UNLIKELY(this->EndX >= this->CapacityX))
this->grow();
::new ((void*) this->end()) T(::std::move(Elt));
- this->set_size(this->size() + 1);
+ this->setEnd(this->end()+1);
}
void pop_back() {
- this->set_size(this->size() - 1);
+ this->setEnd(this->end()-1);
this->end()->~T();
}
};
// Define this out-of-line to dissuade the C++ compiler from inlining it.
template <typename T, bool isPodLike>
void SmallVectorTemplateBase<T, isPodLike>::grow(size_t MinSize) {
- if (MinSize > UINT32_MAX)
- report_bad_alloc_error("SmallVector capacity overflow during allocation");
-
+ size_t CurCapacity = this->capacity();
+ size_t CurSize = this->size();
// Always grow, even from zero.
- size_t NewCapacity = size_t(NextPowerOf2(this->capacity() + 2));
- NewCapacity = std::min(std::max(NewCapacity, MinSize), size_t(UINT32_MAX));
+ size_t NewCapacity = size_t(NextPowerOf2(CurCapacity+2));
+ if (NewCapacity < MinSize)
+ NewCapacity = MinSize;
T *NewElts = static_cast<T*>(llvm::safe_malloc(NewCapacity*sizeof(T)));
// Move the elements over.
if (!this->isSmall())
free(this->begin());
+ this->setEnd(NewElts+CurSize);
this->BeginX = NewElts;
- this->Capacity = NewCapacity;
+ this->CapacityX = this->begin()+NewCapacity;
}
/// Double the size of the allocated memory, guaranteeing space for at
/// least one more element or MinSize if specified.
- void grow(size_t MinSize = 0) { this->grow_pod(MinSize, sizeof(T)); }
+ void grow(size_t MinSize = 0) {
+ this->grow_pod(MinSize*sizeof(T), sizeof(T));
+ }
public:
void push_back(const T &Elt) {
- if (LLVM_UNLIKELY(this->size() >= this->capacity()))
+ if (LLVM_UNLIKELY(this->EndX >= this->CapacityX))
this->grow();
memcpy(this->end(), &Elt, sizeof(T));
- this->set_size(this->size() + 1);
+ this->setEnd(this->end()+1);
}
- void pop_back() { this->set_size(this->size() - 1); }
+ void pop_back() {
+ this->setEnd(this->end()-1);
+ }
};
/// This class consists of common code factored out of the SmallVector class to
protected:
// Default ctor - Initialize to empty.
explicit SmallVectorImpl(unsigned N)
- : SmallVectorTemplateBase<T, isPodLike<T>::value>(N) {}
+ : SmallVectorTemplateBase<T, isPodLike<T>::value>(N*sizeof(T)) {
+ }
public:
SmallVectorImpl(const SmallVectorImpl &) = delete;
void clear() {
this->destroy_range(this->begin(), this->end());
- this->Size = 0;
+ this->EndX = this->BeginX;
}
void resize(size_type N) {
if (N < this->size()) {
this->destroy_range(this->begin()+N, this->end());
- this->set_size(N);
+ this->setEnd(this->begin()+N);
} else if (N > this->size()) {
if (this->capacity() < N)
this->grow(N);
for (auto I = this->end(), E = this->begin() + N; I != E; ++I)
new (&*I) T();
- this->set_size(N);
+ this->setEnd(this->begin()+N);
}
}
void resize(size_type N, const T &NV) {
if (N < this->size()) {
this->destroy_range(this->begin()+N, this->end());
- this->set_size(N);
+ this->setEnd(this->begin()+N);
} else if (N > this->size()) {
if (this->capacity() < N)
this->grow(N);
std::uninitialized_fill(this->end(), this->begin()+N, NV);
- this->set_size(N);
+ this->setEnd(this->begin()+N);
}
}
void append(in_iter in_start, in_iter in_end) {
size_type NumInputs = std::distance(in_start, in_end);
// Grow allocated space if needed.
- if (NumInputs > this->capacity() - this->size())
+ if (NumInputs > size_type(this->capacity_ptr()-this->end()))
this->grow(this->size()+NumInputs);
// Copy the new elements over.
this->uninitialized_copy(in_start, in_end, this->end());
- this->set_size(this->size() + NumInputs);
+ this->setEnd(this->end() + NumInputs);
}
/// Add the specified range to the end of the SmallVector.
void append(size_type NumInputs, const T &Elt) {
// Grow allocated space if needed.
- if (NumInputs > this->capacity() - this->size())
+ if (NumInputs > size_type(this->capacity_ptr()-this->end()))
this->grow(this->size()+NumInputs);
// Copy the new elements over.
std::uninitialized_fill_n(this->end(), NumInputs, Elt);
- this->set_size(this->size() + NumInputs);
+ this->setEnd(this->end() + NumInputs);
}
void append(std::initializer_list<T> IL) {
clear();
if (this->capacity() < NumElts)
this->grow(NumElts);
- this->set_size(NumElts);
+ this->setEnd(this->begin()+NumElts);
std::uninitialized_fill(this->begin(), this->end(), Elt);
}
iterator I = std::move(E, this->end(), S);
// Drop the last elts.
this->destroy_range(I, this->end());
- this->set_size(I - this->begin());
+ this->setEnd(I);
return(N);
}
assert(I >= this->begin() && "Insertion iterator is out of bounds.");
assert(I <= this->end() && "Inserting past the end of the vector.");
- if (this->size() >= this->capacity()) {
+ if (this->EndX >= this->CapacityX) {
size_t EltNo = I-this->begin();
this->grow();
I = this->begin()+EltNo;
::new ((void*) this->end()) T(::std::move(this->back()));
// Push everything else over.
std::move_backward(I, this->end()-1, this->end());
- this->set_size(this->size() + 1);
+ this->setEnd(this->end()+1);
// If we just moved the element we're inserting, be sure to update
// the reference.
T *EltPtr = &Elt;
- if (I <= EltPtr && EltPtr < this->end())
+ if (I <= EltPtr && EltPtr < this->EndX)
++EltPtr;
*I = ::std::move(*EltPtr);
assert(I >= this->begin() && "Insertion iterator is out of bounds.");
assert(I <= this->end() && "Inserting past the end of the vector.");
- if (this->size() >= this->capacity()) {
+ if (this->EndX >= this->CapacityX) {
size_t EltNo = I-this->begin();
this->grow();
I = this->begin()+EltNo;
::new ((void*) this->end()) T(std::move(this->back()));
// Push everything else over.
std::move_backward(I, this->end()-1, this->end());
- this->set_size(this->size() + 1);
+ this->setEnd(this->end()+1);
// If we just moved the element we're inserting, be sure to update
// the reference.
const T *EltPtr = &Elt;
- if (I <= EltPtr && EltPtr < this->end())
+ if (I <= EltPtr && EltPtr < this->EndX)
++EltPtr;
*I = *EltPtr;
// Move over the elements that we're about to overwrite.
T *OldEnd = this->end();
- this->set_size(this->size() + NumToInsert);
+ this->setEnd(this->end() + NumToInsert);
size_t NumOverwritten = OldEnd-I;
this->uninitialized_move(I, OldEnd, this->end()-NumOverwritten);
// Move over the elements that we're about to overwrite.
T *OldEnd = this->end();
- this->set_size(this->size() + NumToInsert);
+ this->setEnd(this->end() + NumToInsert);
size_t NumOverwritten = OldEnd-I;
this->uninitialized_move(I, OldEnd, this->end()-NumOverwritten);
}
template <typename... ArgTypes> void emplace_back(ArgTypes &&... Args) {
- if (LLVM_UNLIKELY(this->size() >= this->capacity()))
+ if (LLVM_UNLIKELY(this->EndX >= this->CapacityX))
this->grow();
::new ((void *)this->end()) T(std::forward<ArgTypes>(Args)...);
- this->set_size(this->size() + 1);
+ this->setEnd(this->end() + 1);
}
SmallVectorImpl &operator=(const SmallVectorImpl &RHS);
return std::lexicographical_compare(this->begin(), this->end(),
RHS.begin(), RHS.end());
}
+
+ /// Set the array size to \p N, which the current array must have enough
+ /// capacity for.
+ ///
+ /// This does not construct or destroy any elements in the vector.
+ ///
+ /// Clients can use this in conjunction with capacity() to write past the end
+ /// of the buffer when they know that more elements are available, and only
+ /// update the size later. This avoids the cost of value initializing elements
+ /// which will only be overwritten.
+ void set_size(size_type N) {
+ assert(N <= this->capacity());
+ this->setEnd(this->begin() + N);
+ }
};
template <typename T>
// We can only avoid copying elements if neither vector is small.
if (!this->isSmall() && !RHS.isSmall()) {
std::swap(this->BeginX, RHS.BeginX);
- std::swap(this->Size, RHS.Size);
- std::swap(this->Capacity, RHS.Capacity);
+ std::swap(this->EndX, RHS.EndX);
+ std::swap(this->CapacityX, RHS.CapacityX);
return;
}
if (RHS.size() > this->capacity())
if (this->size() > RHS.size()) {
size_t EltDiff = this->size() - RHS.size();
this->uninitialized_copy(this->begin()+NumShared, this->end(), RHS.end());
- RHS.set_size(RHS.size() + EltDiff);
+ RHS.setEnd(RHS.end()+EltDiff);
this->destroy_range(this->begin()+NumShared, this->end());
- this->set_size(NumShared);
+ this->setEnd(this->begin()+NumShared);
} else if (RHS.size() > this->size()) {
size_t EltDiff = RHS.size() - this->size();
this->uninitialized_copy(RHS.begin()+NumShared, RHS.end(), this->end());
- this->set_size(this->size() + EltDiff);
+ this->setEnd(this->end() + EltDiff);
this->destroy_range(RHS.begin()+NumShared, RHS.end());
- RHS.set_size(NumShared);
+ RHS.setEnd(RHS.begin()+NumShared);
}
}
this->destroy_range(NewEnd, this->end());
// Trim.
- this->set_size(RHSSize);
+ this->setEnd(NewEnd);
return *this;
}
if (this->capacity() < RHSSize) {
// Destroy current elements.
this->destroy_range(this->begin(), this->end());
- this->set_size(0);
+ this->setEnd(this->begin());
CurSize = 0;
this->grow(RHSSize);
} else if (CurSize) {
this->begin()+CurSize);
// Set end.
- this->set_size(RHSSize);
+ this->setEnd(this->begin()+RHSSize);
return *this;
}
this->destroy_range(this->begin(), this->end());
if (!this->isSmall()) free(this->begin());
this->BeginX = RHS.BeginX;
- this->Size = RHS.Size;
- this->Capacity = RHS.Capacity;
+ this->EndX = RHS.EndX;
+ this->CapacityX = RHS.CapacityX;
RHS.resetToSmall();
return *this;
}
// Destroy excess elements and trim the bounds.
this->destroy_range(NewEnd, this->end());
- this->set_size(RHSSize);
+ this->setEnd(NewEnd);
// Clear the RHS.
RHS.clear();
if (this->capacity() < RHSSize) {
// Destroy current elements.
this->destroy_range(this->begin(), this->end());
- this->set_size(0);
+ this->setEnd(this->begin());
CurSize = 0;
this->grow(RHSSize);
} else if (CurSize) {
this->begin()+CurSize);
// Set end.
- this->set_size(RHSSize);
+ this->setEnd(this->begin()+RHSSize);
RHS.clear();
return *this;