2006-07-26 06:22:30 +00:00
|
|
|
//===- llvm/ADT/SmallVector.h - 'Normally small' vectors --------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 08:50:56 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2006-07-26 06:22:30 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the SmallVector class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_ADT_SMALLVECTOR_H
|
|
|
|
#define LLVM_ADT_SMALLVECTOR_H
|
|
|
|
|
2014-03-10 13:43:46 +00:00
|
|
|
#include "llvm/ADT/iterator_range.h"
|
2012-08-22 00:11:07 +00:00
|
|
|
#include "llvm/Support/AlignOf.h"
|
2012-05-02 05:39:15 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2013-03-29 22:07:12 +00:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2018-06-09 05:19:45 +00:00
|
|
|
#include "llvm/Support/MemAlloc.h"
|
2008-06-30 21:45:13 +00:00
|
|
|
#include "llvm/Support/type_traits.h"
|
2017-07-20 01:30:39 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2006-07-27 03:38:08 +00:00
|
|
|
#include <algorithm>
|
2009-02-20 22:20:18 +00:00
|
|
|
#include <cassert>
|
2010-06-10 10:13:58 +00:00
|
|
|
#include <cstddef>
|
2010-06-08 11:44:30 +00:00
|
|
|
#include <cstdlib>
|
2008-07-01 05:27:28 +00:00
|
|
|
#include <cstring>
|
2015-03-02 21:16:04 +00:00
|
|
|
#include <initializer_list>
|
2010-12-19 20:43:38 +00:00
|
|
|
#include <iterator>
|
2006-07-26 06:22:30 +00:00
|
|
|
#include <memory>
|
2016-12-07 22:06:02 +00:00
|
|
|
#include <new>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <utility>
|
2006-07-26 06:22:30 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2014-09-03 20:08:51 +00:00
|
|
|
/// This is all the non-templated stuff common to all SmallVectors.
|
2009-12-15 07:40:44 +00:00
|
|
|
class SmallVectorBase {
|
2006-10-30 03:39:20 +00:00
|
|
|
protected:
|
2018-07-20 00:44:58 +00:00
|
|
|
void *BeginX;
|
|
|
|
unsigned Size = 0, Capacity;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2018-07-20 00:44:58 +00:00
|
|
|
SmallVectorBase() = delete;
|
|
|
|
SmallVectorBase(void *FirstEl, size_t Capacity)
|
|
|
|
: BeginX(FirstEl), Capacity(Capacity) {}
|
2012-05-02 05:39:15 +00:00
|
|
|
|
2014-09-03 20:08:51 +00:00
|
|
|
/// This is an implementation of the grow() method which only works
|
2011-07-27 18:40:45 +00:00
|
|
|
/// on POD-like data types and is out of line to reduce code duplication.
|
2018-07-20 00:44:58 +00:00
|
|
|
void grow_pod(void *FirstEl, size_t MinCapacity, size_t TSize);
|
2011-07-27 18:40:45 +00:00
|
|
|
|
|
|
|
public:
|
2018-07-20 00:44:58 +00:00
|
|
|
size_t size() const { return Size; }
|
|
|
|
size_t capacity() const { return Capacity; }
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2018-07-20 00:44:58 +00:00
|
|
|
LLVM_NODISCARD bool empty() const { return !Size; }
|
Revert "ADT: Shrink size of SmallVector by 8B on 64-bit platforms"
This reverts commit r337504 while I investigate a TSan bot failure that
seems related:
http://lab.llvm.org:8011/builders/sanitizer-x86_64-linux-autoconf/builds/26526
#8 0x000055581f2895d8 (/b/sanitizer-x86_64-linux-autoconf/build/tsan_debug_build/bin/clang-7+0x1eb45d8)
#9 0x000055581f294323 llvm::ConstantAggrKeyType<llvm::ConstantArray>::create(llvm::ArrayType*) const /b/sanitizer-x86_64-linux-autoconf/build/llvm/lib/IR/ConstantsContext.h:409:0
#10 0x000055581f294323 llvm::ConstantUniqueMap<llvm::ConstantArray>::create(llvm::ArrayType*, llvm::ConstantAggrKeyType<llvm::ConstantArray>, std::pair<unsigned int, std::pair<llvm::ArrayType*, llvm::ConstantAggrKeyType<llvm::ConstantArray> > >&) /b/sanitizer-x86_64-linux-autoconf/build/llvm/lib/IR/ConstantsContext.h:635:0
#11 0x000055581f294323 llvm::ConstantUniqueMap<llvm::ConstantArray>::getOrCreate(llvm::ArrayType*, llvm::ConstantAggrKeyType<llvm::ConstantArray>) /b/sanitizer-x86_64-linux-autoconf/build/llvm/lib/IR/ConstantsContext.h:654:0
#12 0x000055581f2944cb llvm::ConstantArray::get(llvm::ArrayType*, llvm::ArrayRef<llvm::Constant*>) /b/sanitizer-x86_64-linux-autoconf/build/llvm/lib/IR/Constants.cpp:964:0
#13 0x000055581fa27e19 llvm::SmallVectorBase::size() const /b/sanitizer-x86_64-linux-autoconf/build/llvm/include/llvm/ADT/SmallVector.h:53:0
#14 0x000055581fa27e19 llvm::SmallVectorImpl<llvm::Constant*>::resize(unsigned long) /b/sanitizer-x86_64-linux-autoconf/build/llvm/include/llvm/ADT/SmallVector.h:347:0
#15 0x000055581fa27e19 (anonymous namespace)::EmitArrayConstant(clang::CodeGen::CodeGenModule&, clang::ConstantArrayType const*, llvm::Type*, unsigned int, llvm::SmallVectorImpl<llvm::Constant*>&, llvm::Constant*) /b/sanitizer-x86_64-linux-autoconf/build/llvm/tools/clang/lib/CodeGen/CGExprConstant.cpp:669:0
llvm-svn: 337511
2018-07-20 00:09:56 +00:00
|
|
|
|
2018-07-20 00:44:58 +00:00
|
|
|
/// 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;
|
|
|
|
}
|
2009-12-15 08:29:22 +00:00
|
|
|
};
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2018-07-24 11:32:13 +00:00
|
|
|
/// Figure out the offset of the first element.
|
|
|
|
template <class T, typename = void> struct SmallVectorAlignmentAndSize {
|
|
|
|
AlignedCharArrayUnion<SmallVectorBase> Base;
|
|
|
|
AlignedCharArrayUnion<T> FirstEl;
|
|
|
|
};
|
|
|
|
|
2014-09-03 20:08:51 +00:00
|
|
|
/// This is the part of SmallVectorTemplateBase which does not depend on whether
|
|
|
|
/// the type T is a POD. The extra dummy template argument is used by ArrayRef
|
|
|
|
/// to avoid unnecessarily requiring T to be complete.
|
2012-08-22 00:11:07 +00:00
|
|
|
template <typename T, typename = void>
|
2009-12-16 06:55:45 +00:00
|
|
|
class SmallVectorTemplateCommon : public SmallVectorBase {
|
2018-07-24 11:32:13 +00:00
|
|
|
/// Find the address of the first element. For this pointer math to be valid
|
|
|
|
/// with small-size of 0 for T with lots of alignment, it's important that
|
|
|
|
/// SmallVectorStorage is properly-aligned even for small-size of 0.
|
|
|
|
void *getFirstEl() const {
|
|
|
|
return const_cast<void *>(reinterpret_cast<const void *>(
|
|
|
|
reinterpret_cast<const char *>(this) +
|
|
|
|
offsetof(SmallVectorAlignmentAndSize<T>, FirstEl)));
|
|
|
|
}
|
2012-08-22 00:11:07 +00:00
|
|
|
// Space after 'FirstEl' is clobbered, do not add any instance vars after it.
|
|
|
|
|
2009-12-16 08:05:48 +00:00
|
|
|
protected:
|
2018-07-24 11:32:13 +00:00
|
|
|
SmallVectorTemplateCommon(size_t Size)
|
|
|
|
: SmallVectorBase(getFirstEl(), Size) {}
|
2012-08-22 00:11:07 +00:00
|
|
|
|
2018-07-20 00:44:58 +00:00
|
|
|
void grow_pod(size_t MinCapacity, size_t TSize) {
|
2018-07-24 11:32:13 +00:00
|
|
|
SmallVectorBase::grow_pod(getFirstEl(), MinCapacity, TSize);
|
2012-08-22 00:11:07 +00:00
|
|
|
}
|
|
|
|
|
2014-09-03 20:08:51 +00:00
|
|
|
/// Return true if this is a smallvector which has not had dynamic
|
2012-08-22 00:11:07 +00:00
|
|
|
/// memory allocated for it.
|
2018-07-24 11:32:13 +00:00
|
|
|
bool isSmall() const { return BeginX == getFirstEl(); }
|
2012-08-22 00:11:07 +00:00
|
|
|
|
2014-09-03 20:08:51 +00:00
|
|
|
/// Put this vector in a state of being small.
|
2012-08-22 00:11:07 +00:00
|
|
|
void resetToSmall() {
|
2018-07-24 11:32:13 +00:00
|
|
|
BeginX = getFirstEl();
|
2018-07-20 00:44:58 +00:00
|
|
|
Size = Capacity = 0; // FIXME: Setting Capacity to 0 is suspect.
|
2012-08-22 00:11:07 +00:00
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2012-01-20 16:39:46 +00:00
|
|
|
public:
|
2017-05-16 23:10:25 +00:00
|
|
|
using size_type = size_t;
|
|
|
|
using difference_type = ptrdiff_t;
|
|
|
|
using value_type = T;
|
|
|
|
using iterator = T *;
|
|
|
|
using const_iterator = const T *;
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2017-05-16 23:10:25 +00:00
|
|
|
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
|
|
|
|
using reverse_iterator = std::reverse_iterator<iterator>;
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2017-05-16 23:10:25 +00:00
|
|
|
using reference = T &;
|
|
|
|
using const_reference = const T &;
|
|
|
|
using pointer = T *;
|
|
|
|
using const_pointer = const T *;
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2007-08-20 21:22:48 +00:00
|
|
|
// forward iterator creation methods.
|
2009-12-16 06:55:45 +00:00
|
|
|
iterator begin() { return (iterator)this->BeginX; }
|
|
|
|
const_iterator begin() const { return (const_iterator)this->BeginX; }
|
2018-07-20 00:44:58 +00:00
|
|
|
iterator end() { return begin() + size(); }
|
|
|
|
const_iterator end() const { return begin() + size(); }
|
2016-12-07 22:06:02 +00:00
|
|
|
|
2007-08-20 21:22:48 +00:00
|
|
|
// 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());}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2018-07-20 00:44:58 +00:00
|
|
|
size_type size_in_bytes() const { return size() * sizeof(T); }
|
2009-12-15 08:29:22 +00:00
|
|
|
size_type max_size() const { return size_type(-1) / sizeof(T); }
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2018-07-20 00:44:58 +00:00
|
|
|
size_t capacity_in_bytes() const { return capacity() * sizeof(T); }
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2014-09-03 20:08:51 +00:00
|
|
|
/// Return a pointer to the vector's buffer, even if empty().
|
2009-12-15 08:29:22 +00:00
|
|
|
pointer data() { return pointer(begin()); }
|
2014-09-03 20:08:51 +00:00
|
|
|
/// Return a pointer to the vector's buffer, even if empty().
|
2009-12-15 08:29:22 +00:00
|
|
|
const_pointer data() const { return const_pointer(begin()); }
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2014-11-14 07:02:38 +00:00
|
|
|
reference operator[](size_type idx) {
|
[ADT][SmallVector] Flip an assert comparison to avoid overflows yielding false-negatives. NFC.
r221973 changed SmallVector::operator[] to use size_t instead of unsigned.
Before that, on 64bit platforms, when a large index (say -1) was passed,
truncating it to unsigned avoided an overflow when computing 'begin() + idx',
and failed the range checking assertion, as expected.
With r221973, idx isn't truncated, so the addition wraps to
'(char*)begin() - 1', and doesn't fire anymore when it should have done so.
This commit changes the comparison to instead compute 'end() - begin()'
(i.e., 'size()'), which avoids potentially overflowing additions, and
correctly triggers the assertion when values such as -1 are passed.
Note that the problem already existed before that revision, on platforms
where sizeof(size_t) == sizeof(unsigned).
llvm-svn: 225338
2015-01-07 02:42:01 +00:00
|
|
|
assert(idx < size());
|
2009-12-15 08:29:22 +00:00
|
|
|
return begin()[idx];
|
2006-07-26 06:22:30 +00:00
|
|
|
}
|
2014-11-14 07:02:38 +00:00
|
|
|
const_reference operator[](size_type idx) const {
|
[ADT][SmallVector] Flip an assert comparison to avoid overflows yielding false-negatives. NFC.
r221973 changed SmallVector::operator[] to use size_t instead of unsigned.
Before that, on 64bit platforms, when a large index (say -1) was passed,
truncating it to unsigned avoided an overflow when computing 'begin() + idx',
and failed the range checking assertion, as expected.
With r221973, idx isn't truncated, so the addition wraps to
'(char*)begin() - 1', and doesn't fire anymore when it should have done so.
This commit changes the comparison to instead compute 'end() - begin()'
(i.e., 'size()'), which avoids potentially overflowing additions, and
correctly triggers the assertion when values such as -1 are passed.
Note that the problem already existed before that revision, on platforms
where sizeof(size_t) == sizeof(unsigned).
llvm-svn: 225338
2015-01-07 02:42:01 +00:00
|
|
|
assert(idx < size());
|
2009-12-15 08:29:22 +00:00
|
|
|
return begin()[idx];
|
2006-07-26 06:22:30 +00:00
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2006-08-22 06:27:16 +00:00
|
|
|
reference front() {
|
2013-01-24 04:29:24 +00:00
|
|
|
assert(!empty());
|
2006-08-22 06:27:16 +00:00
|
|
|
return begin()[0];
|
|
|
|
}
|
|
|
|
const_reference front() const {
|
2013-01-24 04:29:24 +00:00
|
|
|
assert(!empty());
|
2006-08-22 06:27:16 +00:00
|
|
|
return begin()[0];
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2006-07-26 06:22:30 +00:00
|
|
|
reference back() {
|
2013-01-24 04:29:24 +00:00
|
|
|
assert(!empty());
|
2006-07-26 06:22:30 +00:00
|
|
|
return end()[-1];
|
|
|
|
}
|
|
|
|
const_reference back() const {
|
2013-01-24 04:29:24 +00:00
|
|
|
assert(!empty());
|
2006-07-26 06:22:30 +00:00
|
|
|
return end()[-1];
|
|
|
|
}
|
2009-12-16 08:05:48 +00:00
|
|
|
};
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2019-01-20 21:19:56 +00:00
|
|
|
/// SmallVectorTemplateBase<TriviallyCopyable = false> - This is where we put method
|
2009-12-16 08:09:23 +00:00
|
|
|
/// implementations that are designed to work with non-POD-like T's.
|
2019-01-20 21:19:56 +00:00
|
|
|
template <typename T, bool = is_trivially_copyable<T>::value>
|
2009-12-16 08:05:48 +00:00
|
|
|
class SmallVectorTemplateBase : public SmallVectorTemplateCommon<T> {
|
2012-01-20 16:39:46 +00:00
|
|
|
protected:
|
2009-12-16 08:05:48 +00:00
|
|
|
SmallVectorTemplateBase(size_t Size) : SmallVectorTemplateCommon<T>(Size) {}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2009-12-16 08:09:23 +00:00
|
|
|
static void destroy_range(T *S, T *E) {
|
|
|
|
while (S != E) {
|
|
|
|
--E;
|
|
|
|
E->~T();
|
|
|
|
}
|
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2014-09-03 20:08:51 +00:00
|
|
|
/// Move the range [I, E) into the uninitialized memory starting with "Dest",
|
|
|
|
/// constructing elements as needed.
|
2012-05-02 05:39:15 +00:00
|
|
|
template<typename It1, typename It2>
|
|
|
|
static void uninitialized_move(It1 I, It1 E, It2 Dest) {
|
2016-05-27 19:05:14 +00:00
|
|
|
std::uninitialized_copy(std::make_move_iterator(I),
|
|
|
|
std::make_move_iterator(E), Dest);
|
2012-05-02 05:39:15 +00:00
|
|
|
}
|
|
|
|
|
2014-09-03 20:08:51 +00:00
|
|
|
/// Copy the range [I, E) onto the uninitialized memory starting with "Dest",
|
|
|
|
/// constructing elements as needed.
|
2009-12-16 08:09:23 +00:00
|
|
|
template<typename It1, typename It2>
|
|
|
|
static void uninitialized_copy(It1 I, It1 E, It2 Dest) {
|
|
|
|
std::uninitialized_copy(I, E, Dest);
|
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2014-09-03 20:08:51 +00:00
|
|
|
/// Grow the allocated memory (without initializing new elements), doubling
|
|
|
|
/// the size of the allocated memory. Guarantees space for at least one more
|
|
|
|
/// element, or MinSize more elements if specified.
|
2009-12-16 08:34:40 +00:00
|
|
|
void grow(size_t MinSize = 0);
|
2014-04-22 19:52:05 +00:00
|
|
|
|
2012-02-16 04:58:48 +00:00
|
|
|
public:
|
|
|
|
void push_back(const T &Elt) {
|
2018-07-20 00:44:58 +00:00
|
|
|
if (LLVM_UNLIKELY(this->size() >= this->capacity()))
|
2014-04-22 19:07:14 +00:00
|
|
|
this->grow();
|
|
|
|
::new ((void*) this->end()) T(Elt);
|
2018-07-20 00:44:58 +00:00
|
|
|
this->set_size(this->size() + 1);
|
2012-05-02 05:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void push_back(T &&Elt) {
|
2018-07-20 00:44:58 +00:00
|
|
|
if (LLVM_UNLIKELY(this->size() >= this->capacity()))
|
2014-04-22 19:07:14 +00:00
|
|
|
this->grow();
|
|
|
|
::new ((void*) this->end()) T(::std::move(Elt));
|
2018-07-20 00:44:58 +00:00
|
|
|
this->set_size(this->size() + 1);
|
2012-02-16 04:58:48 +00:00
|
|
|
}
|
2014-03-01 09:27:28 +00:00
|
|
|
|
2012-02-16 04:58:48 +00:00
|
|
|
void pop_back() {
|
2018-07-20 00:44:58 +00:00
|
|
|
this->set_size(this->size() - 1);
|
2012-02-16 04:58:48 +00:00
|
|
|
this->end()->~T();
|
|
|
|
}
|
2009-12-16 08:05:48 +00:00
|
|
|
};
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2009-12-16 08:34:40 +00:00
|
|
|
// Define this out-of-line to dissuade the C++ compiler from inlining it.
|
2019-01-20 21:19:56 +00:00
|
|
|
template <typename T, bool TriviallyCopyable>
|
|
|
|
void SmallVectorTemplateBase<T, TriviallyCopyable>::grow(size_t MinSize) {
|
2018-07-20 00:44:58 +00:00
|
|
|
if (MinSize > UINT32_MAX)
|
|
|
|
report_bad_alloc_error("SmallVector capacity overflow during allocation");
|
|
|
|
|
2014-04-22 19:52:05 +00:00
|
|
|
// Always grow, even from zero.
|
2018-07-20 00:44:58 +00:00
|
|
|
size_t NewCapacity = size_t(NextPowerOf2(this->capacity() + 2));
|
|
|
|
NewCapacity = std::min(std::max(NewCapacity, MinSize), size_t(UINT32_MAX));
|
2018-06-09 05:19:45 +00:00
|
|
|
T *NewElts = static_cast<T*>(llvm::safe_malloc(NewCapacity*sizeof(T)));
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2012-05-02 05:39:15 +00:00
|
|
|
// Move the elements over.
|
|
|
|
this->uninitialized_move(this->begin(), this->end(), NewElts);
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2009-12-16 08:34:40 +00:00
|
|
|
// Destroy the original elements.
|
|
|
|
destroy_range(this->begin(), this->end());
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2009-12-16 08:34:40 +00:00
|
|
|
// If this wasn't grown from the inline copy, deallocate the old space.
|
|
|
|
if (!this->isSmall())
|
2010-06-08 11:44:30 +00:00
|
|
|
free(this->begin());
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2009-12-16 08:34:40 +00:00
|
|
|
this->BeginX = NewElts;
|
2018-07-20 00:44:58 +00:00
|
|
|
this->Capacity = NewCapacity;
|
2009-12-16 08:34:40 +00:00
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2019-01-20 21:19:56 +00:00
|
|
|
/// SmallVectorTemplateBase<TriviallyCopyable = true> - This is where we put
|
|
|
|
/// method implementations that are designed to work with POD-like T's.
|
2009-12-16 08:05:48 +00:00
|
|
|
template <typename T>
|
|
|
|
class SmallVectorTemplateBase<T, true> : public SmallVectorTemplateCommon<T> {
|
2012-01-20 16:39:46 +00:00
|
|
|
protected:
|
2009-12-16 08:05:48 +00:00
|
|
|
SmallVectorTemplateBase(size_t Size) : SmallVectorTemplateCommon<T>(Size) {}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2009-12-16 08:09:23 +00:00
|
|
|
// No need to do a destroy loop for POD's.
|
2009-12-17 19:07:19 +00:00
|
|
|
static void destroy_range(T *, T *) {}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2014-09-03 20:08:51 +00:00
|
|
|
/// Move the range [I, E) onto the uninitialized memory
|
2012-05-02 05:39:15 +00:00
|
|
|
/// starting with "Dest", constructing elements into it as needed.
|
|
|
|
template<typename It1, typename It2>
|
|
|
|
static void uninitialized_move(It1 I, It1 E, It2 Dest) {
|
|
|
|
// Just do a copy.
|
|
|
|
uninitialized_copy(I, E, Dest);
|
|
|
|
}
|
|
|
|
|
2014-09-03 20:08:51 +00:00
|
|
|
/// Copy the range [I, E) onto the uninitialized memory
|
2009-12-16 08:09:23 +00:00
|
|
|
/// starting with "Dest", constructing elements into it as needed.
|
|
|
|
template<typename It1, typename It2>
|
|
|
|
static void uninitialized_copy(It1 I, It1 E, It2 Dest) {
|
2010-03-26 18:53:37 +00:00
|
|
|
// Arbitrary iterator types; just use the basic implementation.
|
|
|
|
std::uninitialized_copy(I, E, Dest);
|
2009-12-16 08:09:23 +00:00
|
|
|
}
|
2010-03-26 18:53:37 +00:00
|
|
|
|
2014-09-03 20:08:51 +00:00
|
|
|
/// Copy the range [I, E) onto the uninitialized memory
|
2010-03-26 18:53:37 +00:00
|
|
|
/// starting with "Dest", constructing elements into it as needed.
|
2015-02-13 20:45:14 +00:00
|
|
|
template <typename T1, typename T2>
|
|
|
|
static void uninitialized_copy(
|
|
|
|
T1 *I, T1 *E, T2 *Dest,
|
|
|
|
typename std::enable_if<std::is_same<typename std::remove_const<T1>::type,
|
|
|
|
T2>::value>::type * = nullptr) {
|
2010-03-26 18:53:37 +00:00
|
|
|
// Use memcpy for PODs iterated by pointers (which includes SmallVector
|
|
|
|
// iterators): std::uninitialized_copy optimizes to memmove, but we can
|
2015-08-04 00:44:07 +00:00
|
|
|
// use memcpy here. Note that I and E are iterators and thus might be
|
|
|
|
// invalid for memcpy if they are equal.
|
|
|
|
if (I != E)
|
2018-08-09 18:29:07 +00:00
|
|
|
memcpy(reinterpret_cast<void *>(Dest), I, (E - I) * sizeof(T));
|
2010-03-26 18:53:37 +00:00
|
|
|
}
|
|
|
|
|
2014-09-03 20:08:51 +00:00
|
|
|
/// Double the size of the allocated memory, guaranteeing space for at
|
2009-12-16 08:34:40 +00:00
|
|
|
/// least one more element or MinSize if specified.
|
2018-07-20 00:44:58 +00:00
|
|
|
void grow(size_t MinSize = 0) { this->grow_pod(MinSize, sizeof(T)); }
|
2016-12-07 22:06:02 +00:00
|
|
|
|
2012-02-16 04:58:48 +00:00
|
|
|
public:
|
|
|
|
void push_back(const T &Elt) {
|
2018-07-20 00:44:58 +00:00
|
|
|
if (LLVM_UNLIKELY(this->size() >= this->capacity()))
|
2014-04-22 19:07:14 +00:00
|
|
|
this->grow();
|
2018-08-09 18:29:07 +00:00
|
|
|
memcpy(reinterpret_cast<void *>(this->end()), &Elt, sizeof(T));
|
2018-07-20 00:44:58 +00:00
|
|
|
this->set_size(this->size() + 1);
|
2012-02-16 04:58:48 +00:00
|
|
|
}
|
2014-04-22 19:52:05 +00:00
|
|
|
|
2018-07-20 00:44:58 +00:00
|
|
|
void pop_back() { this->set_size(this->size() - 1); }
|
2009-12-16 08:05:48 +00:00
|
|
|
};
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2014-09-03 20:08:51 +00:00
|
|
|
/// This class consists of common code factored out of the SmallVector class to
|
|
|
|
/// reduce code duplication based on the SmallVector 'N' template parameter.
|
2009-12-16 08:05:48 +00:00
|
|
|
template <typename T>
|
2018-11-29 17:21:54 +00:00
|
|
|
class SmallVectorImpl : public SmallVectorTemplateBase<T> {
|
|
|
|
using SuperClass = SmallVectorTemplateBase<T>;
|
2010-11-26 04:16:08 +00:00
|
|
|
|
2009-12-16 08:05:48 +00:00
|
|
|
public:
|
2017-05-16 23:10:25 +00:00
|
|
|
using iterator = typename SuperClass::iterator;
|
|
|
|
using const_iterator = typename SuperClass::const_iterator;
|
|
|
|
using size_type = typename SuperClass::size_type;
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2012-01-20 16:39:46 +00:00
|
|
|
protected:
|
2009-12-16 08:05:48 +00:00
|
|
|
// Default ctor - Initialize to empty.
|
|
|
|
explicit SmallVectorImpl(unsigned N)
|
2019-01-20 21:19:56 +00:00
|
|
|
: SmallVectorTemplateBase<T>(N) {}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2012-01-20 16:39:46 +00:00
|
|
|
public:
|
2016-12-07 22:06:02 +00:00
|
|
|
SmallVectorImpl(const SmallVectorImpl &) = delete;
|
|
|
|
|
2009-12-16 08:05:48 +00:00
|
|
|
~SmallVectorImpl() {
|
SmallVector: fix use-after-poison MSAN error in destructor
Summary:
Addresses issue: https://bugs.llvm.org/show_bug.cgi?id=34595
The topmost class, `SmallVector`, has internal storage for some
elements; `N - 1` elements' bytes worth of space. Meanwhile a base
class `SmallVectorTemplateCommon` has room for one element as well,
totaling `N` elements' worth of space.
The space for the N elements is contiguous and straddles
`SmallVectorTemplateCommon` and `SmallVector`.
A class "between" those two owning the storage, `SmallVectorImpl`, in
its destructor, calls the destructor for elements contained in the
vector, if any. It uses `destroy_range(begin, end)` and deletes all
items in sequence, starting from the end.
By the time the destructor for `SmallVectorImpl` is running, though, the
memory for elements `[1, N)` is already poisoned, due to `SmallVector`'s
destructor having done its thing already.
So if the element type `T` has a nontrivial destructor that accesses any
members of the `T` instance being destroyed, we'll run into a
user-after-poison bug.
This patch moves the destruction loop into `SmallVector`'s destructor,
so any memory being accessed while dtors are running is not yet
poisoned.
Confirmed this broke before (and now works with this patch) with these
compiler flags:
-fsanitize=memory
-fsanitize-memory-use-after-dtor
-fsanitize-memory-track-origins
and with the cmake flag
`-DLLVM_USE_SANITIZER='MemoryWithOrigins;Undefined'` as well as
`MSAN_OPTIONS=poison_in_dtor=1`.
Patch By: elsteveogrande
Reviewers: eugenis, morehouse, dblaikie
Reviewed By: eugenis, dblaikie
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D41916
llvm-svn: 322241
2018-01-10 23:53:11 +00:00
|
|
|
// Subclass has already destructed this vector's elements.
|
2009-12-16 08:05:48 +00:00
|
|
|
// If this wasn't grown from the inline copy, deallocate the old space.
|
|
|
|
if (!this->isSmall())
|
2010-06-08 11:44:30 +00:00
|
|
|
free(this->begin());
|
2008-12-05 07:11:05 +00:00
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2006-08-07 05:45:34 +00:00
|
|
|
void clear() {
|
2009-12-16 09:17:12 +00:00
|
|
|
this->destroy_range(this->begin(), this->end());
|
2018-07-20 00:44:58 +00:00
|
|
|
this->Size = 0;
|
2006-08-22 06:27:16 +00:00
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2014-12-04 14:40:35 +00:00
|
|
|
void resize(size_type N) {
|
2009-12-16 08:05:48 +00:00
|
|
|
if (N < this->size()) {
|
|
|
|
this->destroy_range(this->begin()+N, this->end());
|
2018-07-20 00:44:58 +00:00
|
|
|
this->set_size(N);
|
2009-12-16 08:05:48 +00:00
|
|
|
} else if (N > this->size()) {
|
|
|
|
if (this->capacity() < N)
|
2009-12-16 08:34:40 +00:00
|
|
|
this->grow(N);
|
2014-06-09 22:26:20 +00:00
|
|
|
for (auto I = this->end(), E = this->begin() + N; I != E; ++I)
|
|
|
|
new (&*I) T();
|
2018-07-20 00:44:58 +00:00
|
|
|
this->set_size(N);
|
2006-08-07 05:45:34 +00:00
|
|
|
}
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2014-12-04 14:40:35 +00:00
|
|
|
void resize(size_type N, const T &NV) {
|
2009-12-16 08:05:48 +00:00
|
|
|
if (N < this->size()) {
|
2009-12-16 09:17:12 +00:00
|
|
|
this->destroy_range(this->begin()+N, this->end());
|
2018-07-20 00:44:58 +00:00
|
|
|
this->set_size(N);
|
2009-12-16 08:05:48 +00:00
|
|
|
} else if (N > this->size()) {
|
|
|
|
if (this->capacity() < N)
|
2009-12-16 08:34:40 +00:00
|
|
|
this->grow(N);
|
2012-02-05 22:48:31 +00:00
|
|
|
std::uninitialized_fill(this->end(), this->begin()+N, NV);
|
2018-07-20 00:44:58 +00:00
|
|
|
this->set_size(N);
|
2006-08-28 21:52:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2014-12-04 14:40:35 +00:00
|
|
|
void reserve(size_type N) {
|
2009-12-16 08:05:48 +00:00
|
|
|
if (this->capacity() < N)
|
2009-12-16 08:34:40 +00:00
|
|
|
this->grow(N);
|
2006-09-01 06:08:16 +00:00
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2016-10-16 20:42:34 +00:00
|
|
|
LLVM_NODISCARD T pop_back_val() {
|
2012-05-02 05:39:15 +00:00
|
|
|
T Result = ::std::move(this->back());
|
2012-02-16 04:58:48 +00:00
|
|
|
this->pop_back();
|
2009-12-16 08:05:48 +00:00
|
|
|
return Result;
|
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2009-12-16 08:05:48 +00:00
|
|
|
void swap(SmallVectorImpl &RHS);
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2014-09-03 20:08:51 +00:00
|
|
|
/// Add the specified range to the end of the SmallVector.
|
2017-06-09 20:31:53 +00:00
|
|
|
template <typename in_iter,
|
|
|
|
typename = typename std::enable_if<std::is_convertible<
|
|
|
|
typename std::iterator_traits<in_iter>::iterator_category,
|
|
|
|
std::input_iterator_tag>::value>::type>
|
2006-07-27 03:38:08 +00:00
|
|
|
void append(in_iter in_start, in_iter in_end) {
|
2008-05-05 18:30:58 +00:00
|
|
|
size_type NumInputs = std::distance(in_start, in_end);
|
2006-07-27 03:38:08 +00:00
|
|
|
// Grow allocated space if needed.
|
2018-07-20 00:44:58 +00:00
|
|
|
if (NumInputs > this->capacity() - this->size())
|
2009-12-16 08:34:40 +00:00
|
|
|
this->grow(this->size()+NumInputs);
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2006-07-27 03:38:08 +00:00
|
|
|
// Copy the new elements over.
|
2015-02-13 20:45:14 +00:00
|
|
|
this->uninitialized_copy(in_start, in_end, this->end());
|
2018-07-20 00:44:58 +00:00
|
|
|
this->set_size(this->size() + NumInputs);
|
2006-07-27 03:38:08 +00:00
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2014-09-03 20:08:51 +00:00
|
|
|
/// Add the specified range to the end of the SmallVector.
|
2008-10-16 00:15:24 +00:00
|
|
|
void append(size_type NumInputs, const T &Elt) {
|
|
|
|
// Grow allocated space if needed.
|
2018-07-20 00:44:58 +00:00
|
|
|
if (NumInputs > this->capacity() - this->size())
|
2009-12-16 08:34:40 +00:00
|
|
|
this->grow(this->size()+NumInputs);
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2008-10-16 00:15:24 +00:00
|
|
|
// Copy the new elements over.
|
2009-12-16 08:05:48 +00:00
|
|
|
std::uninitialized_fill_n(this->end(), NumInputs, Elt);
|
2018-07-20 00:44:58 +00:00
|
|
|
this->set_size(this->size() + NumInputs);
|
2008-10-16 00:15:24 +00:00
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2015-03-02 21:16:04 +00:00
|
|
|
void append(std::initializer_list<T> IL) {
|
|
|
|
append(IL.begin(), IL.end());
|
|
|
|
}
|
|
|
|
|
Tidy up a bit of r304516, use SmallVector::assign rather than for loop
This might give a few better opportunities to optimize these to memcpy
rather than loops - also a few minor cleanups (StringRef-izing,
templating (to avoid std::function indirection), etc).
The SmallVector::assign(iter, iter) could be improved with the use of
SFINAE, but the (iter, iter) ctor and append(iter, iter) need it to and
don't have it - so, workaround it for now rather than bothering with the
added complexity.
(also, as noted in the added FIXME, these assign ops could potentially
be optimized better at least for non-trivially-copyable types)
llvm-svn: 304566
2017-06-02 17:24:26 +00:00
|
|
|
// FIXME: Consider assigning over existing elements, rather than clearing &
|
|
|
|
// re-initializing them - for all assign(...) variants.
|
|
|
|
|
2014-12-04 14:40:35 +00:00
|
|
|
void assign(size_type NumElts, const T &Elt) {
|
2006-08-08 01:44:16 +00:00
|
|
|
clear();
|
2014-03-09 06:22:58 +00:00
|
|
|
if (this->capacity() < NumElts)
|
|
|
|
this->grow(NumElts);
|
2018-07-20 00:44:58 +00:00
|
|
|
this->set_size(NumElts);
|
2014-03-09 06:22:58 +00:00
|
|
|
std::uninitialized_fill(this->begin(), this->end(), Elt);
|
2006-08-08 01:44:16 +00:00
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2017-06-09 20:31:53 +00:00
|
|
|
template <typename in_iter,
|
|
|
|
typename = typename std::enable_if<std::is_convertible<
|
|
|
|
typename std::iterator_traits<in_iter>::iterator_category,
|
|
|
|
std::input_iterator_tag>::value>::type>
|
|
|
|
void assign(in_iter in_start, in_iter in_end) {
|
Tidy up a bit of r304516, use SmallVector::assign rather than for loop
This might give a few better opportunities to optimize these to memcpy
rather than loops - also a few minor cleanups (StringRef-izing,
templating (to avoid std::function indirection), etc).
The SmallVector::assign(iter, iter) could be improved with the use of
SFINAE, but the (iter, iter) ctor and append(iter, iter) need it to and
don't have it - so, workaround it for now rather than bothering with the
added complexity.
(also, as noted in the added FIXME, these assign ops could potentially
be optimized better at least for non-trivially-copyable types)
llvm-svn: 304566
2017-06-02 17:24:26 +00:00
|
|
|
clear();
|
|
|
|
append(in_start, in_end);
|
|
|
|
}
|
|
|
|
|
2015-03-02 21:16:04 +00:00
|
|
|
void assign(std::initializer_list<T> IL) {
|
|
|
|
clear();
|
|
|
|
append(IL);
|
|
|
|
}
|
|
|
|
|
2016-03-24 20:25:51 +00:00
|
|
|
iterator erase(const_iterator CI) {
|
|
|
|
// Just cast away constness because this is a non-const member function.
|
|
|
|
iterator I = const_cast<iterator>(CI);
|
|
|
|
|
2012-07-27 19:05:58 +00:00
|
|
|
assert(I >= this->begin() && "Iterator to erase is out of bounds.");
|
|
|
|
assert(I < this->end() && "Erasing at past-the-end iterator.");
|
|
|
|
|
2008-04-02 18:24:46 +00:00
|
|
|
iterator N = I;
|
2006-08-22 06:27:16 +00:00
|
|
|
// Shift all elts down one.
|
2016-05-27 19:05:14 +00:00
|
|
|
std::move(I+1, this->end(), I);
|
2006-08-22 06:27:16 +00:00
|
|
|
// Drop the last elt.
|
2012-02-16 04:58:48 +00:00
|
|
|
this->pop_back();
|
2008-04-02 18:24:46 +00:00
|
|
|
return(N);
|
2006-08-22 06:27:16 +00:00
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2016-03-24 20:25:51 +00:00
|
|
|
iterator erase(const_iterator CS, const_iterator CE) {
|
|
|
|
// Just cast away constness because this is a non-const member function.
|
|
|
|
iterator S = const_cast<iterator>(CS);
|
|
|
|
iterator E = const_cast<iterator>(CE);
|
|
|
|
|
2012-07-27 19:05:58 +00:00
|
|
|
assert(S >= this->begin() && "Range to erase is out of bounds.");
|
|
|
|
assert(S <= E && "Trying to erase invalid range.");
|
|
|
|
assert(E <= this->end() && "Trying to erase past the end.");
|
|
|
|
|
2008-04-02 18:24:46 +00:00
|
|
|
iterator N = S;
|
2006-08-22 06:27:16 +00:00
|
|
|
// Shift all elts down.
|
2016-05-27 19:05:14 +00:00
|
|
|
iterator I = std::move(E, this->end(), S);
|
2006-08-22 06:27:16 +00:00
|
|
|
// Drop the last elts.
|
2009-12-16 09:17:12 +00:00
|
|
|
this->destroy_range(I, this->end());
|
2018-07-20 00:44:58 +00:00
|
|
|
this->set_size(I - this->begin());
|
2008-04-02 18:24:46 +00:00
|
|
|
return(N);
|
2006-08-22 06:27:16 +00:00
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2012-05-02 05:39:15 +00:00
|
|
|
iterator insert(iterator I, T &&Elt) {
|
|
|
|
if (I == this->end()) { // Important special case for empty vector.
|
|
|
|
this->push_back(::std::move(Elt));
|
|
|
|
return this->end()-1;
|
|
|
|
}
|
|
|
|
|
2012-07-27 19:05:58 +00:00
|
|
|
assert(I >= this->begin() && "Insertion iterator is out of bounds.");
|
|
|
|
assert(I <= this->end() && "Inserting past the end of the vector.");
|
|
|
|
|
2018-07-20 00:44:58 +00:00
|
|
|
if (this->size() >= this->capacity()) {
|
2014-04-22 19:07:14 +00:00
|
|
|
size_t EltNo = I-this->begin();
|
|
|
|
this->grow();
|
|
|
|
I = this->begin()+EltNo;
|
2012-05-02 05:39:15 +00:00
|
|
|
}
|
2014-04-22 19:07:14 +00:00
|
|
|
|
|
|
|
::new ((void*) this->end()) T(::std::move(this->back()));
|
|
|
|
// Push everything else over.
|
2016-05-27 19:05:14 +00:00
|
|
|
std::move_backward(I, this->end()-1, this->end());
|
2018-07-20 00:44:58 +00:00
|
|
|
this->set_size(this->size() + 1);
|
2014-04-22 19:07:14 +00:00
|
|
|
|
|
|
|
// If we just moved the element we're inserting, be sure to update
|
|
|
|
// the reference.
|
|
|
|
T *EltPtr = &Elt;
|
2018-07-20 00:44:58 +00:00
|
|
|
if (I <= EltPtr && EltPtr < this->end())
|
2014-04-22 19:07:14 +00:00
|
|
|
++EltPtr;
|
|
|
|
|
|
|
|
*I = ::std::move(*EltPtr);
|
|
|
|
return I;
|
2012-05-02 05:39:15 +00:00
|
|
|
}
|
|
|
|
|
2006-08-22 06:27:16 +00:00
|
|
|
iterator insert(iterator I, const T &Elt) {
|
2009-12-16 08:05:48 +00:00
|
|
|
if (I == this->end()) { // Important special case for empty vector.
|
2012-02-16 04:58:48 +00:00
|
|
|
this->push_back(Elt);
|
2009-12-16 08:05:48 +00:00
|
|
|
return this->end()-1;
|
2006-08-22 06:27:16 +00:00
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2012-07-27 19:05:58 +00:00
|
|
|
assert(I >= this->begin() && "Insertion iterator is out of bounds.");
|
|
|
|
assert(I <= this->end() && "Inserting past the end of the vector.");
|
|
|
|
|
2018-07-20 00:44:58 +00:00
|
|
|
if (this->size() >= this->capacity()) {
|
2014-04-22 19:07:14 +00:00
|
|
|
size_t EltNo = I-this->begin();
|
|
|
|
this->grow();
|
|
|
|
I = this->begin()+EltNo;
|
2006-08-22 06:27:16 +00:00
|
|
|
}
|
2014-06-08 16:55:13 +00:00
|
|
|
::new ((void*) this->end()) T(std::move(this->back()));
|
2014-04-22 19:07:14 +00:00
|
|
|
// Push everything else over.
|
2016-05-27 19:05:14 +00:00
|
|
|
std::move_backward(I, this->end()-1, this->end());
|
2018-07-20 00:44:58 +00:00
|
|
|
this->set_size(this->size() + 1);
|
2014-04-22 19:07:14 +00:00
|
|
|
|
|
|
|
// If we just moved the element we're inserting, be sure to update
|
|
|
|
// the reference.
|
|
|
|
const T *EltPtr = &Elt;
|
2018-07-20 00:44:58 +00:00
|
|
|
if (I <= EltPtr && EltPtr < this->end())
|
2014-04-22 19:07:14 +00:00
|
|
|
++EltPtr;
|
|
|
|
|
|
|
|
*I = *EltPtr;
|
|
|
|
return I;
|
2006-08-22 06:27:16 +00:00
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2008-10-16 00:15:24 +00:00
|
|
|
iterator insert(iterator I, size_type NumToInsert, const T &Elt) {
|
2012-06-17 12:46:13 +00:00
|
|
|
// Convert iterator to elt# to avoid invalidating iterator when we reserve()
|
|
|
|
size_t InsertElt = I - this->begin();
|
|
|
|
|
2009-12-16 08:05:48 +00:00
|
|
|
if (I == this->end()) { // Important special case for empty vector.
|
2008-10-16 00:15:24 +00:00
|
|
|
append(NumToInsert, Elt);
|
2012-06-17 12:46:13 +00:00
|
|
|
return this->begin()+InsertElt;
|
2008-10-16 00:15:24 +00:00
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2012-07-27 19:05:58 +00:00
|
|
|
assert(I >= this->begin() && "Insertion iterator is out of bounds.");
|
|
|
|
assert(I <= this->end() && "Inserting past the end of the vector.");
|
|
|
|
|
2008-10-16 00:15:24 +00:00
|
|
|
// Ensure there is enough space.
|
2014-12-04 14:40:35 +00:00
|
|
|
reserve(this->size() + NumToInsert);
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2008-10-16 00:15:24 +00:00
|
|
|
// Uninvalidate the iterator.
|
2009-12-16 08:05:48 +00:00
|
|
|
I = this->begin()+InsertElt;
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2009-03-22 19:22:53 +00:00
|
|
|
// If there are more elements between the insertion point and the end of the
|
|
|
|
// range than there are being inserted, we can use a simple approach to
|
|
|
|
// insertion. Since we already reserved space, we know that this won't
|
|
|
|
// reallocate the vector.
|
2009-12-16 08:05:48 +00:00
|
|
|
if (size_t(this->end()-I) >= NumToInsert) {
|
|
|
|
T *OldEnd = this->end();
|
2014-06-08 19:12:28 +00:00
|
|
|
append(std::move_iterator<iterator>(this->end() - NumToInsert),
|
|
|
|
std::move_iterator<iterator>(this->end()));
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2008-10-16 00:15:24 +00:00
|
|
|
// Copy the existing elements that get replaced.
|
2016-05-27 19:05:14 +00:00
|
|
|
std::move_backward(I, OldEnd-NumToInsert, OldEnd);
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2008-10-16 00:15:24 +00:00
|
|
|
std::fill_n(I, NumToInsert, Elt);
|
|
|
|
return I;
|
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2008-10-16 00:15:24 +00:00
|
|
|
// Otherwise, we're inserting more elements than exist already, and we're
|
|
|
|
// not inserting at the end.
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2012-07-08 12:06:35 +00:00
|
|
|
// Move over the elements that we're about to overwrite.
|
2009-12-16 08:05:48 +00:00
|
|
|
T *OldEnd = this->end();
|
2018-07-20 00:44:58 +00:00
|
|
|
this->set_size(this->size() + NumToInsert);
|
2008-10-16 00:15:24 +00:00
|
|
|
size_t NumOverwritten = OldEnd-I;
|
2012-07-08 12:06:35 +00:00
|
|
|
this->uninitialized_move(I, OldEnd, this->end()-NumOverwritten);
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2008-10-16 00:15:24 +00:00
|
|
|
// Replace the overwritten part.
|
|
|
|
std::fill_n(I, NumOverwritten, Elt);
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2008-10-16 00:15:24 +00:00
|
|
|
// Insert the non-overwritten middle part.
|
|
|
|
std::uninitialized_fill_n(OldEnd, NumToInsert-NumOverwritten, Elt);
|
|
|
|
return I;
|
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2017-06-09 20:31:53 +00:00
|
|
|
template <typename ItTy,
|
|
|
|
typename = typename std::enable_if<std::is_convertible<
|
|
|
|
typename std::iterator_traits<ItTy>::iterator_category,
|
|
|
|
std::input_iterator_tag>::value>::type>
|
2006-10-30 05:07:51 +00:00
|
|
|
iterator insert(iterator I, ItTy From, ItTy To) {
|
2012-06-17 12:46:13 +00:00
|
|
|
// Convert iterator to elt# to avoid invalidating iterator when we reserve()
|
|
|
|
size_t InsertElt = I - this->begin();
|
|
|
|
|
2009-12-16 08:05:48 +00:00
|
|
|
if (I == this->end()) { // Important special case for empty vector.
|
2006-10-30 05:07:51 +00:00
|
|
|
append(From, To);
|
2012-06-17 12:46:13 +00:00
|
|
|
return this->begin()+InsertElt;
|
2006-10-30 05:07:51 +00:00
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2012-07-27 19:05:58 +00:00
|
|
|
assert(I >= this->begin() && "Insertion iterator is out of bounds.");
|
|
|
|
assert(I <= this->end() && "Inserting past the end of the vector.");
|
|
|
|
|
2008-05-05 18:30:58 +00:00
|
|
|
size_t NumToInsert = std::distance(From, To);
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2006-10-30 05:07:51 +00:00
|
|
|
// Ensure there is enough space.
|
2014-12-04 14:40:35 +00:00
|
|
|
reserve(this->size() + NumToInsert);
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2006-10-30 05:07:51 +00:00
|
|
|
// Uninvalidate the iterator.
|
2009-12-16 08:05:48 +00:00
|
|
|
I = this->begin()+InsertElt;
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2009-03-22 19:22:53 +00:00
|
|
|
// If there are more elements between the insertion point and the end of the
|
|
|
|
// range than there are being inserted, we can use a simple approach to
|
|
|
|
// insertion. Since we already reserved space, we know that this won't
|
|
|
|
// reallocate the vector.
|
2009-12-16 08:05:48 +00:00
|
|
|
if (size_t(this->end()-I) >= NumToInsert) {
|
|
|
|
T *OldEnd = this->end();
|
2014-06-08 19:12:31 +00:00
|
|
|
append(std::move_iterator<iterator>(this->end() - NumToInsert),
|
|
|
|
std::move_iterator<iterator>(this->end()));
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2006-10-30 05:07:51 +00:00
|
|
|
// Copy the existing elements that get replaced.
|
2016-05-27 19:05:14 +00:00
|
|
|
std::move_backward(I, OldEnd-NumToInsert, OldEnd);
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2006-10-30 05:07:51 +00:00
|
|
|
std::copy(From, To, I);
|
|
|
|
return I;
|
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2006-10-30 05:07:51 +00:00
|
|
|
// Otherwise, we're inserting more elements than exist already, and we're
|
|
|
|
// not inserting at the end.
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2012-07-08 12:06:35 +00:00
|
|
|
// Move over the elements that we're about to overwrite.
|
2009-12-16 08:05:48 +00:00
|
|
|
T *OldEnd = this->end();
|
2018-07-20 00:44:58 +00:00
|
|
|
this->set_size(this->size() + NumToInsert);
|
2008-05-05 18:30:58 +00:00
|
|
|
size_t NumOverwritten = OldEnd-I;
|
2012-07-08 12:06:35 +00:00
|
|
|
this->uninitialized_move(I, OldEnd, this->end()-NumOverwritten);
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2006-10-30 05:07:51 +00:00
|
|
|
// Replace the overwritten part.
|
2012-06-17 12:46:13 +00:00
|
|
|
for (T *J = I; NumOverwritten > 0; --NumOverwritten) {
|
|
|
|
*J = *From;
|
|
|
|
++J; ++From;
|
2010-03-26 18:53:37 +00:00
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2006-10-30 05:07:51 +00:00
|
|
|
// Insert the non-overwritten middle part.
|
2010-03-26 18:53:37 +00:00
|
|
|
this->uninitialized_copy(From, To, OldEnd);
|
2006-10-30 05:07:51 +00:00
|
|
|
return I;
|
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2015-03-02 21:16:04 +00:00
|
|
|
void insert(iterator I, std::initializer_list<T> IL) {
|
|
|
|
insert(I, IL.begin(), IL.end());
|
|
|
|
}
|
|
|
|
|
2015-02-07 16:41:02 +00:00
|
|
|
template <typename... ArgTypes> void emplace_back(ArgTypes &&... Args) {
|
2018-07-20 00:44:58 +00:00
|
|
|
if (LLVM_UNLIKELY(this->size() >= this->capacity()))
|
2015-02-07 16:41:02 +00:00
|
|
|
this->grow();
|
|
|
|
::new ((void *)this->end()) T(std::forward<ArgTypes>(Args)...);
|
2018-07-20 00:44:58 +00:00
|
|
|
this->set_size(this->size() + 1);
|
2015-02-07 16:41:02 +00:00
|
|
|
}
|
|
|
|
|
2012-05-02 05:39:15 +00:00
|
|
|
SmallVectorImpl &operator=(const SmallVectorImpl &RHS);
|
|
|
|
|
|
|
|
SmallVectorImpl &operator=(SmallVectorImpl &&RHS);
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2009-12-16 08:05:48 +00:00
|
|
|
bool operator==(const SmallVectorImpl &RHS) const {
|
|
|
|
if (this->size() != RHS.size()) return false;
|
|
|
|
return std::equal(this->begin(), this->end(), RHS.begin());
|
2008-01-11 18:42:02 +00:00
|
|
|
}
|
2009-12-16 08:05:48 +00:00
|
|
|
bool operator!=(const SmallVectorImpl &RHS) const {
|
2009-12-16 06:55:45 +00:00
|
|
|
return !(*this == RHS);
|
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2009-12-16 08:05:48 +00:00
|
|
|
bool operator<(const SmallVectorImpl &RHS) const {
|
|
|
|
return std::lexicographical_compare(this->begin(), this->end(),
|
2008-05-30 22:37:47 +00:00
|
|
|
RHS.begin(), RHS.end());
|
|
|
|
}
|
2006-08-11 23:40:23 +00:00
|
|
|
};
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2006-08-22 17:28:57 +00:00
|
|
|
template <typename T>
|
2009-12-16 08:05:48 +00:00
|
|
|
void SmallVectorImpl<T>::swap(SmallVectorImpl<T> &RHS) {
|
2006-08-22 17:28:57 +00:00
|
|
|
if (this == &RHS) return;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2006-08-22 17:28:57 +00:00
|
|
|
// We can only avoid copying elements if neither vector is small.
|
2009-12-16 06:55:45 +00:00
|
|
|
if (!this->isSmall() && !RHS.isSmall()) {
|
|
|
|
std::swap(this->BeginX, RHS.BeginX);
|
2018-07-20 00:44:58 +00:00
|
|
|
std::swap(this->Size, RHS.Size);
|
|
|
|
std::swap(this->Capacity, RHS.Capacity);
|
2006-08-22 17:28:57 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-12-16 08:05:48 +00:00
|
|
|
if (RHS.size() > this->capacity())
|
2009-12-16 08:34:40 +00:00
|
|
|
this->grow(RHS.size());
|
2009-12-16 08:05:48 +00:00
|
|
|
if (this->size() > RHS.capacity())
|
|
|
|
RHS.grow(this->size());
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2006-08-22 17:28:57 +00:00
|
|
|
// Swap the shared elements.
|
2009-12-16 08:05:48 +00:00
|
|
|
size_t NumShared = this->size();
|
2006-08-22 17:28:57 +00:00
|
|
|
if (NumShared > RHS.size()) NumShared = RHS.size();
|
2014-12-04 14:40:35 +00:00
|
|
|
for (size_type i = 0; i != NumShared; ++i)
|
2009-12-15 08:29:22 +00:00
|
|
|
std::swap((*this)[i], RHS[i]);
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2006-08-22 17:28:57 +00:00
|
|
|
// Copy over the extra elts.
|
2009-12-16 08:05:48 +00:00
|
|
|
if (this->size() > RHS.size()) {
|
|
|
|
size_t EltDiff = this->size() - RHS.size();
|
2009-12-16 09:17:12 +00:00
|
|
|
this->uninitialized_copy(this->begin()+NumShared, this->end(), RHS.end());
|
2018-07-20 00:44:58 +00:00
|
|
|
RHS.set_size(RHS.size() + EltDiff);
|
2009-12-16 09:17:12 +00:00
|
|
|
this->destroy_range(this->begin()+NumShared, this->end());
|
2018-07-20 00:44:58 +00:00
|
|
|
this->set_size(NumShared);
|
2009-12-16 08:05:48 +00:00
|
|
|
} else if (RHS.size() > this->size()) {
|
|
|
|
size_t EltDiff = RHS.size() - this->size();
|
2009-12-16 09:17:12 +00:00
|
|
|
this->uninitialized_copy(RHS.begin()+NumShared, RHS.end(), this->end());
|
2018-07-20 00:44:58 +00:00
|
|
|
this->set_size(this->size() + EltDiff);
|
2009-12-16 09:17:12 +00:00
|
|
|
this->destroy_range(RHS.begin()+NumShared, RHS.end());
|
2018-07-20 00:44:58 +00:00
|
|
|
RHS.set_size(NumShared);
|
2006-08-22 17:28:57 +00:00
|
|
|
}
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2006-08-11 23:40:23 +00:00
|
|
|
template <typename T>
|
2012-05-02 05:39:15 +00:00
|
|
|
SmallVectorImpl<T> &SmallVectorImpl<T>::
|
2009-12-16 08:05:48 +00:00
|
|
|
operator=(const SmallVectorImpl<T> &RHS) {
|
2006-08-11 23:40:23 +00:00
|
|
|
// Avoid self-assignment.
|
|
|
|
if (this == &RHS) return *this;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2006-08-11 23:40:23 +00:00
|
|
|
// If we already have sufficient space, assign the common elements, then
|
|
|
|
// destroy any excess.
|
2009-12-15 08:29:22 +00:00
|
|
|
size_t RHSSize = RHS.size();
|
2009-12-16 08:05:48 +00:00
|
|
|
size_t CurSize = this->size();
|
2006-08-11 23:40:23 +00:00
|
|
|
if (CurSize >= RHSSize) {
|
|
|
|
// Assign common elements.
|
2007-08-10 07:02:50 +00:00
|
|
|
iterator NewEnd;
|
|
|
|
if (RHSSize)
|
2009-12-16 08:05:48 +00:00
|
|
|
NewEnd = std::copy(RHS.begin(), RHS.begin()+RHSSize, this->begin());
|
2007-08-10 07:02:50 +00:00
|
|
|
else
|
2009-12-16 08:05:48 +00:00
|
|
|
NewEnd = this->begin();
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2006-08-11 23:40:23 +00:00
|
|
|
// Destroy excess elements.
|
2009-12-16 09:17:12 +00:00
|
|
|
this->destroy_range(NewEnd, this->end());
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2006-08-11 23:40:23 +00:00
|
|
|
// Trim.
|
2018-07-20 00:44:58 +00:00
|
|
|
this->set_size(RHSSize);
|
2006-08-11 23:40:23 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2006-08-11 23:40:23 +00:00
|
|
|
// If we have to grow to have enough elements, destroy the current elements.
|
|
|
|
// This allows us to avoid copying them during the grow.
|
2012-05-02 05:39:15 +00:00
|
|
|
// FIXME: don't do this if they're efficiently moveable.
|
2009-12-16 08:05:48 +00:00
|
|
|
if (this->capacity() < RHSSize) {
|
2006-08-11 23:40:23 +00:00
|
|
|
// Destroy current elements.
|
2009-12-16 09:17:12 +00:00
|
|
|
this->destroy_range(this->begin(), this->end());
|
2018-07-20 00:44:58 +00:00
|
|
|
this->set_size(0);
|
2006-08-11 23:40:23 +00:00
|
|
|
CurSize = 0;
|
2009-12-16 08:34:40 +00:00
|
|
|
this->grow(RHSSize);
|
2006-08-11 23:40:23 +00:00
|
|
|
} else if (CurSize) {
|
|
|
|
// Otherwise, use assignment for the already-constructed elements.
|
2009-12-16 08:05:48 +00:00
|
|
|
std::copy(RHS.begin(), RHS.begin()+CurSize, this->begin());
|
2006-07-26 06:22:30 +00:00
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2006-08-11 23:40:23 +00:00
|
|
|
// Copy construct the new elements in place.
|
2009-12-16 09:17:12 +00:00
|
|
|
this->uninitialized_copy(RHS.begin()+CurSize, RHS.end(),
|
|
|
|
this->begin()+CurSize);
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2006-08-11 23:40:23 +00:00
|
|
|
// Set end.
|
2018-07-20 00:44:58 +00:00
|
|
|
this->set_size(RHSSize);
|
2006-08-16 22:09:24 +00:00
|
|
|
return *this;
|
2006-08-11 23:40:23 +00:00
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2012-05-02 05:39:15 +00:00
|
|
|
template <typename T>
|
|
|
|
SmallVectorImpl<T> &SmallVectorImpl<T>::operator=(SmallVectorImpl<T> &&RHS) {
|
|
|
|
// Avoid self-assignment.
|
|
|
|
if (this == &RHS) return *this;
|
|
|
|
|
|
|
|
// If the RHS isn't small, clear this vector and then steal its buffer.
|
|
|
|
if (!RHS.isSmall()) {
|
|
|
|
this->destroy_range(this->begin(), this->end());
|
|
|
|
if (!this->isSmall()) free(this->begin());
|
|
|
|
this->BeginX = RHS.BeginX;
|
2018-07-20 00:44:58 +00:00
|
|
|
this->Size = RHS.Size;
|
|
|
|
this->Capacity = RHS.Capacity;
|
2012-05-02 05:39:15 +00:00
|
|
|
RHS.resetToSmall();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we already have sufficient space, assign the common elements, then
|
|
|
|
// destroy any excess.
|
|
|
|
size_t RHSSize = RHS.size();
|
|
|
|
size_t CurSize = this->size();
|
|
|
|
if (CurSize >= RHSSize) {
|
|
|
|
// Assign common elements.
|
|
|
|
iterator NewEnd = this->begin();
|
|
|
|
if (RHSSize)
|
2016-05-27 19:05:14 +00:00
|
|
|
NewEnd = std::move(RHS.begin(), RHS.end(), NewEnd);
|
2012-05-02 05:39:15 +00:00
|
|
|
|
|
|
|
// Destroy excess elements and trim the bounds.
|
|
|
|
this->destroy_range(NewEnd, this->end());
|
2018-07-20 00:44:58 +00:00
|
|
|
this->set_size(RHSSize);
|
2012-05-02 05:39:15 +00:00
|
|
|
|
|
|
|
// Clear the RHS.
|
|
|
|
RHS.clear();
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have to grow to have enough elements, destroy the current elements.
|
|
|
|
// This allows us to avoid copying them during the grow.
|
|
|
|
// FIXME: this may not actually make any sense if we can efficiently move
|
|
|
|
// elements.
|
|
|
|
if (this->capacity() < RHSSize) {
|
|
|
|
// Destroy current elements.
|
|
|
|
this->destroy_range(this->begin(), this->end());
|
2018-07-20 00:44:58 +00:00
|
|
|
this->set_size(0);
|
2012-05-02 05:39:15 +00:00
|
|
|
CurSize = 0;
|
|
|
|
this->grow(RHSSize);
|
|
|
|
} else if (CurSize) {
|
|
|
|
// Otherwise, use assignment for the already-constructed elements.
|
2016-05-27 19:05:14 +00:00
|
|
|
std::move(RHS.begin(), RHS.begin()+CurSize, this->begin());
|
2012-05-02 05:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Move-construct the new elements in place.
|
|
|
|
this->uninitialized_move(RHS.begin()+CurSize, RHS.end(),
|
|
|
|
this->begin()+CurSize);
|
|
|
|
|
|
|
|
// Set end.
|
2018-07-20 00:44:58 +00:00
|
|
|
this->set_size(RHSSize);
|
2012-05-02 05:39:15 +00:00
|
|
|
|
|
|
|
RHS.clear();
|
|
|
|
return *this;
|
|
|
|
}
|
2009-12-16 06:55:45 +00:00
|
|
|
|
2018-07-24 11:32:13 +00:00
|
|
|
/// Storage for the SmallVector elements. This is specialized for the N=0 case
|
2012-08-22 00:11:07 +00:00
|
|
|
/// to avoid allocating unnecessary storage.
|
|
|
|
template <typename T, unsigned N>
|
|
|
|
struct SmallVectorStorage {
|
2018-07-24 11:32:13 +00:00
|
|
|
AlignedCharArrayUnion<T> InlineElts[N];
|
2012-08-22 00:11:07 +00:00
|
|
|
};
|
2018-07-24 11:32:13 +00:00
|
|
|
|
|
|
|
/// We need the storage to be properly aligned even for small-size of 0 so that
|
|
|
|
/// the pointer math in \a SmallVectorTemplateCommon::getFirstEl() is
|
|
|
|
/// well-defined.
|
|
|
|
template <typename T> struct alignas(alignof(T)) SmallVectorStorage<T, 0> {};
|
2012-08-22 00:11:07 +00:00
|
|
|
|
2014-09-03 20:08:51 +00:00
|
|
|
/// This is a 'vector' (really, a variable-sized array), optimized
|
2006-08-11 23:19:51 +00:00
|
|
|
/// for the case when the array is small. It contains some number of elements
|
|
|
|
/// in-place, which allows it to avoid heap allocation when the actual number of
|
|
|
|
/// elements is below that threshold. This allows normal "small" cases to be
|
|
|
|
/// fast without losing generality for large inputs.
|
|
|
|
///
|
|
|
|
/// Note that this does not attempt to be exception safe.
|
|
|
|
///
|
|
|
|
template <typename T, unsigned N>
|
2018-06-23 18:39:44 +00:00
|
|
|
class SmallVector : public SmallVectorImpl<T>, SmallVectorStorage<T, N> {
|
2009-01-09 19:25:42 +00:00
|
|
|
public:
|
2017-05-16 23:10:25 +00:00
|
|
|
SmallVector() : SmallVectorImpl<T>(N) {}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
SmallVector: fix use-after-poison MSAN error in destructor
Summary:
Addresses issue: https://bugs.llvm.org/show_bug.cgi?id=34595
The topmost class, `SmallVector`, has internal storage for some
elements; `N - 1` elements' bytes worth of space. Meanwhile a base
class `SmallVectorTemplateCommon` has room for one element as well,
totaling `N` elements' worth of space.
The space for the N elements is contiguous and straddles
`SmallVectorTemplateCommon` and `SmallVector`.
A class "between" those two owning the storage, `SmallVectorImpl`, in
its destructor, calls the destructor for elements contained in the
vector, if any. It uses `destroy_range(begin, end)` and deletes all
items in sequence, starting from the end.
By the time the destructor for `SmallVectorImpl` is running, though, the
memory for elements `[1, N)` is already poisoned, due to `SmallVector`'s
destructor having done its thing already.
So if the element type `T` has a nontrivial destructor that accesses any
members of the `T` instance being destroyed, we'll run into a
user-after-poison bug.
This patch moves the destruction loop into `SmallVector`'s destructor,
so any memory being accessed while dtors are running is not yet
poisoned.
Confirmed this broke before (and now works with this patch) with these
compiler flags:
-fsanitize=memory
-fsanitize-memory-use-after-dtor
-fsanitize-memory-track-origins
and with the cmake flag
`-DLLVM_USE_SANITIZER='MemoryWithOrigins;Undefined'` as well as
`MSAN_OPTIONS=poison_in_dtor=1`.
Patch By: elsteveogrande
Reviewers: eugenis, morehouse, dblaikie
Reviewed By: eugenis, dblaikie
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D41916
llvm-svn: 322241
2018-01-10 23:53:11 +00:00
|
|
|
~SmallVector() {
|
|
|
|
// Destroy the constructed elements in the vector.
|
|
|
|
this->destroy_range(this->begin(), this->end());
|
|
|
|
}
|
|
|
|
|
2014-12-04 14:40:35 +00:00
|
|
|
explicit SmallVector(size_t Size, const T &Value = T())
|
2012-08-22 00:11:07 +00:00
|
|
|
: SmallVectorImpl<T>(N) {
|
2012-02-05 22:14:48 +00:00
|
|
|
this->assign(Size, Value);
|
2007-01-31 20:08:34 +00:00
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2017-06-09 20:31:53 +00:00
|
|
|
template <typename ItTy,
|
|
|
|
typename = typename std::enable_if<std::is_convertible<
|
|
|
|
typename std::iterator_traits<ItTy>::iterator_category,
|
|
|
|
std::input_iterator_tag>::value>::type>
|
2012-08-22 00:11:07 +00:00
|
|
|
SmallVector(ItTy S, ItTy E) : SmallVectorImpl<T>(N) {
|
2008-12-13 10:55:13 +00:00
|
|
|
this->append(S, E);
|
2006-08-11 23:19:51 +00:00
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2014-03-10 13:43:46 +00:00
|
|
|
template <typename RangeTy>
|
2016-12-07 22:06:02 +00:00
|
|
|
explicit SmallVector(const iterator_range<RangeTy> &R)
|
2014-03-10 13:43:46 +00:00
|
|
|
: SmallVectorImpl<T>(N) {
|
|
|
|
this->append(R.begin(), R.end());
|
|
|
|
}
|
|
|
|
|
2015-03-02 21:16:04 +00:00
|
|
|
SmallVector(std::initializer_list<T> IL) : SmallVectorImpl<T>(N) {
|
|
|
|
this->assign(IL);
|
|
|
|
}
|
|
|
|
|
2012-08-22 00:11:07 +00:00
|
|
|
SmallVector(const SmallVector &RHS) : SmallVectorImpl<T>(N) {
|
2008-04-28 06:32:08 +00:00
|
|
|
if (!RHS.empty())
|
2008-12-13 10:55:13 +00:00
|
|
|
SmallVectorImpl<T>::operator=(RHS);
|
2008-04-28 06:32:08 +00:00
|
|
|
}
|
|
|
|
|
2006-08-22 06:27:16 +00:00
|
|
|
const SmallVector &operator=(const SmallVector &RHS) {
|
|
|
|
SmallVectorImpl<T>::operator=(RHS);
|
|
|
|
return *this;
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2012-08-22 00:11:07 +00:00
|
|
|
SmallVector(SmallVector &&RHS) : SmallVectorImpl<T>(N) {
|
2012-05-02 05:39:15 +00:00
|
|
|
if (!RHS.empty())
|
|
|
|
SmallVectorImpl<T>::operator=(::std::move(RHS));
|
|
|
|
}
|
|
|
|
|
2015-01-23 06:25:17 +00:00
|
|
|
SmallVector(SmallVectorImpl<T> &&RHS) : SmallVectorImpl<T>(N) {
|
|
|
|
if (!RHS.empty())
|
|
|
|
SmallVectorImpl<T>::operator=(::std::move(RHS));
|
|
|
|
}
|
|
|
|
|
2017-05-16 23:10:25 +00:00
|
|
|
const SmallVector &operator=(SmallVector &&RHS) {
|
|
|
|
SmallVectorImpl<T>::operator=(::std::move(RHS));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-01-23 06:25:17 +00:00
|
|
|
const SmallVector &operator=(SmallVectorImpl<T> &&RHS) {
|
|
|
|
SmallVectorImpl<T>::operator=(::std::move(RHS));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-03-02 21:16:04 +00:00
|
|
|
const SmallVector &operator=(std::initializer_list<T> IL) {
|
|
|
|
this->assign(IL);
|
|
|
|
return *this;
|
|
|
|
}
|
2006-08-11 23:19:51 +00:00
|
|
|
};
|
|
|
|
|
2017-10-24 17:29:11 +00:00
|
|
|
template <typename T, unsigned N>
|
|
|
|
inline size_t capacity_in_bytes(const SmallVector<T, N> &X) {
|
2011-07-27 18:40:45 +00:00
|
|
|
return X.capacity_in_bytes();
|
|
|
|
}
|
|
|
|
|
2016-12-07 22:06:02 +00:00
|
|
|
} // end namespace llvm
|
2006-07-26 06:22:30 +00:00
|
|
|
|
2006-08-22 06:27:16 +00:00
|
|
|
namespace std {
|
2016-12-07 22:06:02 +00:00
|
|
|
|
2006-08-22 06:27:16 +00:00
|
|
|
/// Implement std::swap in terms of SmallVector swap.
|
|
|
|
template<typename T>
|
|
|
|
inline void
|
|
|
|
swap(llvm::SmallVectorImpl<T> &LHS, llvm::SmallVectorImpl<T> &RHS) {
|
|
|
|
LHS.swap(RHS);
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2006-08-22 06:27:16 +00:00
|
|
|
/// Implement std::swap in terms of SmallVector swap.
|
|
|
|
template<typename T, unsigned N>
|
|
|
|
inline void
|
|
|
|
swap(llvm::SmallVector<T, N> &LHS, llvm::SmallVector<T, N> &RHS) {
|
|
|
|
LHS.swap(RHS);
|
|
|
|
}
|
|
|
|
|
2016-12-07 22:06:02 +00:00
|
|
|
} // end namespace std
|
|
|
|
|
|
|
|
#endif // LLVM_ADT_SMALLVECTOR_H
|