]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - libstdc++-v3/include/bits/vector.tcc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / vector.tcc
index 4eacec3f5165117cfe8b5299369b789a43c3cbd2..27e63388feb1f950187a756b52ae753b29715f86 100644 (file)
@@ -1,6 +1,6 @@
 // Vector implementation (out of line) -*- C++ -*-
 
-// Copyright (C) 2001-2014 Free Software Foundation, Inc.
+// Copyright (C) 2001-2020 Free Software Foundation, Inc.
 //
 // This file is part of the GNU ISO C++ Library.  This library is free
 // software; you can redistribute it and/or modify it under the
@@ -58,6 +58,7 @@
 
 namespace std _GLIBCXX_VISIBILITY(default)
 {
+_GLIBCXX_BEGIN_NAMESPACE_VERSION
 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
 
   template<typename _Tp, typename _Alloc>
@@ -70,11 +71,24 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       if (this->capacity() < __n)
        {
          const size_type __old_size = size();
-         pointer __tmp = _M_allocate_and_copy(__n,
-           _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(this->_M_impl._M_start),
-           _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(this->_M_impl._M_finish));
-         std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
-                       _M_get_Tp_allocator());
+         pointer __tmp;
+#if __cplusplus >= 201103L
+         if _GLIBCXX17_CONSTEXPR (_S_use_relocate())
+           {
+             __tmp = this->_M_allocate(__n);
+             _S_relocate(this->_M_impl._M_start, this->_M_impl._M_finish,
+                         __tmp, _M_get_Tp_allocator());
+           }
+         else
+#endif
+           {
+             __tmp = _M_allocate_and_copy(__n,
+               _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(this->_M_impl._M_start),
+               _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(this->_M_impl._M_finish));
+             std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
+                           _M_get_Tp_allocator());
+           }
+         _GLIBCXX_ASAN_ANNOTATE_REINIT;
          _M_deallocate(this->_M_impl._M_start,
                        this->_M_impl._M_end_of_storage
                        - this->_M_impl._M_start);
@@ -87,18 +101,27 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
 #if __cplusplus >= 201103L
   template<typename _Tp, typename _Alloc>
     template<typename... _Args>
+#if __cplusplus > 201402L
+      typename vector<_Tp, _Alloc>::reference
+#else
       void
+#endif
       vector<_Tp, _Alloc>::
       emplace_back(_Args&&... __args)
       {
        if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
          {
+           _GLIBCXX_ASAN_ANNOTATE_GROW(1);
            _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
                                     std::forward<_Args>(__args)...);
            ++this->_M_impl._M_finish;
+           _GLIBCXX_ASAN_ANNOTATE_GREW(1);
          }
        else
-         _M_emplace_back_aux(std::forward<_Args>(__args)...);
+         _M_realloc_insert(end(), std::forward<_Args>(__args)...);
+#if __cplusplus > 201402L
+       return back();
+#endif
       }
 #endif
 
@@ -112,27 +135,34 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
 #endif
     {
       const size_type __n = __position - begin();
-      if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage
-         && __position == end())
-       {
-         _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish, __x);
-         ++this->_M_impl._M_finish;
-       }
-      else
-       {
+      if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
+       if (__position == end())
+         {
+           _GLIBCXX_ASAN_ANNOTATE_GROW(1);
+           _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
+                                    __x);
+           ++this->_M_impl._M_finish;
+           _GLIBCXX_ASAN_ANNOTATE_GREW(1);
+         }
+       else
+         {
 #if __cplusplus >= 201103L
-         const auto __pos = begin() + (__position - cbegin());
-         if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
-           {
-             _Tp __x_copy = __x;
-             _M_insert_aux(__pos, std::move(__x_copy));
-           }
-         else
-           _M_insert_aux(__pos, __x);
+           const auto __pos = begin() + (__position - cbegin());
+           // __x could be an existing element of this vector, so make a
+           // copy of it before _M_insert_aux moves elements around.
+           _Temporary_value __x_copy(this, __x);
+           _M_insert_aux(__pos, std::move(__x_copy._M_val()));
 #else
            _M_insert_aux(__position, __x);
 #endif
-       }
+         }
+      else
+#if __cplusplus >= 201103L
+       _M_realloc_insert(begin() + (__position - cbegin()), __x);
+#else
+       _M_realloc_insert(__position, __x);
+#endif
+
       return iterator(this->_M_impl._M_start + __n);
     }
 
@@ -145,6 +175,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
        _GLIBCXX_MOVE3(__position + 1, end(), __position);
       --this->_M_impl._M_finish;
       _Alloc_traits::destroy(this->_M_impl, this->_M_impl._M_finish);
+      _GLIBCXX_ASAN_ANNOTATE_SHRINK(1);
       return __position;
     }
 
@@ -169,6 +200,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
     {
       if (&__x != this)
        {
+         _GLIBCXX_ASAN_ANNOTATE_REINIT;
 #if __cplusplus >= 201103L
          if (_Alloc_traits::_S_propagate_on_copy_assign())
            {
@@ -233,10 +265,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       else if (__n > size())
        {
          std::fill(begin(), end(), __val);
-         std::__uninitialized_fill_n_a(this->_M_impl._M_finish,
-                                       __n - size(), __val,
-                                       _M_get_Tp_allocator());
-         this->_M_impl._M_finish += __n - size();
+         const size_type __add = __n - size();
+         _GLIBCXX_ASAN_ANNOTATE_GROW(__add);
+         this->_M_impl._M_finish =
+           std::__uninitialized_fill_n_a(this->_M_impl._M_finish,
+                                         __add, __val, _M_get_Tp_allocator());
+         _GLIBCXX_ASAN_ANNOTATE_GREW(__add);
        }
       else
         _M_erase_at_end(std::fill_n(this->_M_impl._M_start, __n, __val));
@@ -251,12 +285,13 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       {
        pointer __cur(this->_M_impl._M_start);
        for (; __first != __last && __cur != this->_M_impl._M_finish;
-            ++__cur, ++__first)
+            ++__cur, (void)++__first)
          *__cur = *__first;
        if (__first == __last)
          _M_erase_at_end(__cur);
        else
-         insert(end(), __first, __last);
+         _M_range_insert(end(), __first, __last,
+                         std::__iterator_category(__first));
       }
 
   template<typename _Tp, typename _Alloc>
@@ -270,9 +305,11 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
 
        if (__len > capacity())
          {
+           _S_check_init_len(__len, _M_get_Tp_allocator());
            pointer __tmp(_M_allocate_and_copy(__len, __first, __last));
            std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
                          _M_get_Tp_allocator());
+           _GLIBCXX_ASAN_ANNOTATE_REINIT;
            _M_deallocate(this->_M_impl._M_start,
                          this->_M_impl._M_end_of_storage
                          - this->_M_impl._M_start);
@@ -287,39 +324,76 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
            _ForwardIterator __mid = __first;
            std::advance(__mid, size());
            std::copy(__first, __mid, this->_M_impl._M_start);
+           const size_type __attribute__((__unused__)) __n = __len - size();
+           _GLIBCXX_ASAN_ANNOTATE_GROW(__n);
            this->_M_impl._M_finish =
              std::__uninitialized_copy_a(__mid, __last,
                                          this->_M_impl._M_finish,
                                          _M_get_Tp_allocator());
+           _GLIBCXX_ASAN_ANNOTATE_GREW(__n);
          }
       }
 
 #if __cplusplus >= 201103L
   template<typename _Tp, typename _Alloc>
-    template<typename... _Args>
-      typename vector<_Tp, _Alloc>::iterator
-      vector<_Tp, _Alloc>::
-      emplace(const_iterator __position, _Args&&... __args)
-      {
-       const size_type __n = __position - begin();
-       if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage
-           && __position == end())
+    auto
+    vector<_Tp, _Alloc>::
+    _M_insert_rval(const_iterator __position, value_type&& __v) -> iterator
+    {
+      const auto __n = __position - cbegin();
+      if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
+       if (__position == cend())
          {
+           _GLIBCXX_ASAN_ANNOTATE_GROW(1);
            _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
-                                    std::forward<_Args>(__args)...);
+                                    std::move(__v));
            ++this->_M_impl._M_finish;
+           _GLIBCXX_ASAN_ANNOTATE_GREW(1);
          }
        else
-         _M_insert_aux(begin() + (__position - cbegin()),
-                       std::forward<_Args>(__args)...);
+         _M_insert_aux(begin() + __n, std::move(__v));
+      else
+       _M_realloc_insert(begin() + __n, std::move(__v));
+
+      return iterator(this->_M_impl._M_start + __n);
+    }
+
+  template<typename _Tp, typename _Alloc>
+    template<typename... _Args>
+      auto
+      vector<_Tp, _Alloc>::
+      _M_emplace_aux(const_iterator __position, _Args&&... __args)
+      -> iterator
+      {
+       const auto __n = __position - cbegin();
+       if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
+         if (__position == cend())
+           {
+             _GLIBCXX_ASAN_ANNOTATE_GROW(1);
+             _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
+                                      std::forward<_Args>(__args)...);
+             ++this->_M_impl._M_finish;
+             _GLIBCXX_ASAN_ANNOTATE_GREW(1);
+           }
+         else
+           {
+             // We need to construct a temporary because something in __args...
+             // could alias one of the elements of the container and so we
+             // need to use it before _M_insert_aux moves elements around.
+             _Temporary_value __tmp(this, std::forward<_Args>(__args)...);
+             _M_insert_aux(begin() + __n, std::move(__tmp._M_val()));
+           }
+       else
+         _M_realloc_insert(begin() + __n, std::forward<_Args>(__args)...);
+
        return iterator(this->_M_impl._M_start + __n);
       }
 
   template<typename _Tp, typename _Alloc>
-    template<typename... _Args>
+    template<typename _Arg>
       void
       vector<_Tp, _Alloc>::
-      _M_insert_aux(iterator __position, _Args&&... __args)
+      _M_insert_aux(iterator __position, _Arg&& __arg)
 #else
   template<typename _Tp, typename _Alloc>
     void
@@ -327,122 +401,108 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
     _M_insert_aux(iterator __position, const _Tp& __x)
 #endif
     {
-      if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
-       {
-         _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
-                                  _GLIBCXX_MOVE(*(this->_M_impl._M_finish
-                                                  - 1)));
-         ++this->_M_impl._M_finish;
+      _GLIBCXX_ASAN_ANNOTATE_GROW(1);
+      _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
+                              _GLIBCXX_MOVE(*(this->_M_impl._M_finish - 1)));
+      ++this->_M_impl._M_finish;
+      _GLIBCXX_ASAN_ANNOTATE_GREW(1);
 #if __cplusplus < 201103L
-         _Tp __x_copy = __x;
+      _Tp __x_copy = __x;
 #endif
-         _GLIBCXX_MOVE_BACKWARD3(__position.base(),
-                                 this->_M_impl._M_finish - 2,
-                                 this->_M_impl._M_finish - 1);
+      _GLIBCXX_MOVE_BACKWARD3(__position.base(),
+                             this->_M_impl._M_finish - 2,
+                             this->_M_impl._M_finish - 1);
 #if __cplusplus < 201103L
-         *__position = __x_copy;
+      *__position = __x_copy;
 #else
-         *__position = _Tp(std::forward<_Args>(__args)...);
+      *__position = std::forward<_Arg>(__arg);
 #endif
-       }
-      else
+    }
+
+#if __cplusplus >= 201103L
+  template<typename _Tp, typename _Alloc>
+    template<typename... _Args>
+      void
+      vector<_Tp, _Alloc>::
+      _M_realloc_insert(iterator __position, _Args&&... __args)
+#else
+  template<typename _Tp, typename _Alloc>
+    void
+    vector<_Tp, _Alloc>::
+    _M_realloc_insert(iterator __position, const _Tp& __x)
+#endif
+    {
+      const size_type __len =
+       _M_check_len(size_type(1), "vector::_M_realloc_insert");
+      pointer __old_start = this->_M_impl._M_start;
+      pointer __old_finish = this->_M_impl._M_finish;
+      const size_type __elems_before = __position - begin();
+      pointer __new_start(this->_M_allocate(__len));
+      pointer __new_finish(__new_start);
+      __try
        {
-         const size_type __len =
-           _M_check_len(size_type(1), "vector::_M_insert_aux");
-         const size_type __elems_before = __position - begin();
-         pointer __new_start(this->_M_allocate(__len));
-         pointer __new_finish(__new_start);
-         __try
-           {
-             // The order of the three operations is dictated by the C++0x
-             // case, where the moves could alter a new element belonging
-             // to the existing vector.  This is an issue only for callers
-             // taking the element by const lvalue ref (see 23.1/13).
-             _Alloc_traits::construct(this->_M_impl,
-                                      __new_start + __elems_before,
+         // The order of the three operations is dictated by the C++11
+         // case, where the moves could alter a new element belonging
+         // to the existing vector.  This is an issue only for callers
+         // taking the element by lvalue ref (see last bullet of C++11
+         // [res.on.arguments]).
+         _Alloc_traits::construct(this->_M_impl,
+                                  __new_start + __elems_before,
 #if __cplusplus >= 201103L
-                                      std::forward<_Args>(__args)...);
+                                  std::forward<_Args>(__args)...);
 #else
-                                      __x);
+                                  __x);
 #endif
-             __new_finish = pointer();
+         __new_finish = pointer();
+
+#if __cplusplus >= 201103L
+         if _GLIBCXX17_CONSTEXPR (_S_use_relocate())
+           {
+             __new_finish = _S_relocate(__old_start, __position.base(),
+                                        __new_start, _M_get_Tp_allocator());
 
+             ++__new_finish;
+
+             __new_finish = _S_relocate(__position.base(), __old_finish,
+                                        __new_finish, _M_get_Tp_allocator());
+           }
+         else
+#endif
+           {
              __new_finish
                = std::__uninitialized_move_if_noexcept_a
-               (this->_M_impl._M_start, __position.base(),
+               (__old_start, __position.base(),
                 __new_start, _M_get_Tp_allocator());
 
              ++__new_finish;
 
              __new_finish
                = std::__uninitialized_move_if_noexcept_a
-               (__position.base(), this->_M_impl._M_finish,
+               (__position.base(), __old_finish,
                 __new_finish, _M_get_Tp_allocator());
            }
-          __catch(...)
-           {
-             if (!__new_finish)
-               _Alloc_traits::destroy(this->_M_impl,
-                                      __new_start + __elems_before);
-             else
-               std::_Destroy(__new_start, __new_finish, _M_get_Tp_allocator());
-             _M_deallocate(__new_start, __len);
-             __throw_exception_again;
-           }
-         std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
-                       _M_get_Tp_allocator());
-         _M_deallocate(this->_M_impl._M_start,
-                       this->_M_impl._M_end_of_storage
-                       - this->_M_impl._M_start);
-         this->_M_impl._M_start = __new_start;
-         this->_M_impl._M_finish = __new_finish;
-         this->_M_impl._M_end_of_storage = __new_start + __len;
        }
-    }
-
+      __catch(...)
+       {
+         if (!__new_finish)
+           _Alloc_traits::destroy(this->_M_impl,
+                                  __new_start + __elems_before);
+         else
+           std::_Destroy(__new_start, __new_finish, _M_get_Tp_allocator());
+         _M_deallocate(__new_start, __len);
+         __throw_exception_again;
+       }
 #if __cplusplus >= 201103L
-  template<typename _Tp, typename _Alloc>
-    template<typename... _Args>
-      void
-      vector<_Tp, _Alloc>::
-      _M_emplace_back_aux(_Args&&... __args)
-      {
-       const size_type __len =
-         _M_check_len(size_type(1), "vector::_M_emplace_back_aux");
-       pointer __new_start(this->_M_allocate(__len));
-       pointer __new_finish(__new_start);
-       __try
-         {
-           _Alloc_traits::construct(this->_M_impl, __new_start + size(),
-                                    std::forward<_Args>(__args)...);
-           __new_finish = pointer();
-
-           __new_finish
-             = std::__uninitialized_move_if_noexcept_a
-             (this->_M_impl._M_start, this->_M_impl._M_finish,
-              __new_start, _M_get_Tp_allocator());
-
-           ++__new_finish;
-         }
-       __catch(...)
-         {
-           if (!__new_finish)
-             _Alloc_traits::destroy(this->_M_impl, __new_start + size());
-           else
-             std::_Destroy(__new_start, __new_finish, _M_get_Tp_allocator());
-           _M_deallocate(__new_start, __len);
-           __throw_exception_again;
-         }
-       std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
-                     _M_get_Tp_allocator());
-       _M_deallocate(this->_M_impl._M_start,
-                     this->_M_impl._M_end_of_storage
-                     - this->_M_impl._M_start);
-       this->_M_impl._M_start = __new_start;
-       this->_M_impl._M_finish = __new_finish;
-       this->_M_impl._M_end_of_storage = __new_start + __len;
-      }
+      if _GLIBCXX17_CONSTEXPR (!_S_use_relocate())
 #endif
+       std::_Destroy(__old_start, __old_finish, _M_get_Tp_allocator());
+      _GLIBCXX_ASAN_ANNOTATE_REINIT;
+      _M_deallocate(__old_start,
+                   this->_M_impl._M_end_of_storage - __old_start);
+      this->_M_impl._M_start = __new_start;
+      this->_M_impl._M_finish = __new_finish;
+      this->_M_impl._M_end_of_storage = __new_start + __len;
+    }
 
   template<typename _Tp, typename _Alloc>
     void
@@ -454,16 +514,23 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
          if (size_type(this->_M_impl._M_end_of_storage
                        - this->_M_impl._M_finish) >= __n)
            {
+#if __cplusplus < 201103L
              value_type __x_copy = __x;
+#else
+             _Temporary_value __tmp(this, __x);
+             value_type& __x_copy = __tmp._M_val();
+#endif
              const size_type __elems_after = end() - __position;
              pointer __old_finish(this->_M_impl._M_finish);
              if (__elems_after > __n)
                {
+                 _GLIBCXX_ASAN_ANNOTATE_GROW(__n);
                  std::__uninitialized_move_a(this->_M_impl._M_finish - __n,
                                              this->_M_impl._M_finish,
                                              this->_M_impl._M_finish,
                                              _M_get_Tp_allocator());
                  this->_M_impl._M_finish += __n;
+                 _GLIBCXX_ASAN_ANNOTATE_GREW(__n);
                  _GLIBCXX_MOVE_BACKWARD3(__position.base(),
                                          __old_finish - __n, __old_finish);
                  std::fill(__position.base(), __position.base() + __n,
@@ -471,15 +538,18 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
                }
              else
                {
-                 std::__uninitialized_fill_n_a(this->_M_impl._M_finish,
-                                               __n - __elems_after,
-                                               __x_copy,
-                                               _M_get_Tp_allocator());
-                 this->_M_impl._M_finish += __n - __elems_after;
+                 _GLIBCXX_ASAN_ANNOTATE_GROW(__n);
+                 this->_M_impl._M_finish =
+                   std::__uninitialized_fill_n_a(this->_M_impl._M_finish,
+                                                 __n - __elems_after,
+                                                 __x_copy,
+                                                 _M_get_Tp_allocator());
+                 _GLIBCXX_ASAN_ANNOTATE_GREW(__n - __elems_after);
                  std::__uninitialized_move_a(__position.base(), __old_finish,
                                              this->_M_impl._M_finish,
                                              _M_get_Tp_allocator());
                  this->_M_impl._M_finish += __elems_after;
+                 _GLIBCXX_ASAN_ANNOTATE_GREW(__elems_after);
                  std::fill(__position.base(), __old_finish, __x_copy);
                }
            }
@@ -492,7 +562,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
              pointer __new_finish(__new_start);
              __try
                {
-                 // See _M_insert_aux above.
+                 // See _M_realloc_insert above.
                  std::__uninitialized_fill_n_a(__new_start + __elems_before,
                                                __n, __x,
                                                _M_get_Tp_allocator());
@@ -524,6 +594,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
                }
              std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
                            _M_get_Tp_allocator());
+             _GLIBCXX_ASAN_ANNOTATE_REINIT;
              _M_deallocate(this->_M_impl._M_start,
                            this->_M_impl._M_end_of_storage
                            - this->_M_impl._M_start);
@@ -542,44 +613,70 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
     {
       if (__n != 0)
        {
-         if (size_type(this->_M_impl._M_end_of_storage
-                       - this->_M_impl._M_finish) >= __n)
+         const size_type __size = size();
+         size_type __navail = size_type(this->_M_impl._M_end_of_storage
+                                        - this->_M_impl._M_finish);
+
+         if (__size > max_size() || __navail > max_size() - __size)
+           __builtin_unreachable();
+
+         if (__navail >= __n)
            {
-             std::__uninitialized_default_n_a(this->_M_impl._M_finish,
-                                              __n, _M_get_Tp_allocator());
-             this->_M_impl._M_finish += __n;
+             _GLIBCXX_ASAN_ANNOTATE_GROW(__n);
+             this->_M_impl._M_finish =
+               std::__uninitialized_default_n_a(this->_M_impl._M_finish,
+                                                __n, _M_get_Tp_allocator());
+             _GLIBCXX_ASAN_ANNOTATE_GREW(__n);
            }
          else
            {
              const size_type __len =
                _M_check_len(__n, "vector::_M_default_append");
-             const size_type __old_size = this->size();
              pointer __new_start(this->_M_allocate(__len));
-             pointer __new_finish(__new_start);
-             __try
+             if _GLIBCXX17_CONSTEXPR (_S_use_relocate())
                {
-                 __new_finish
-                   = std::__uninitialized_move_if_noexcept_a
-                   (this->_M_impl._M_start, this->_M_impl._M_finish,
-                    __new_start, _M_get_Tp_allocator());
-                 std::__uninitialized_default_n_a(__new_finish, __n,
-                                                  _M_get_Tp_allocator());
-                 __new_finish += __n;
+                 __try
+                   {
+                     std::__uninitialized_default_n_a(__new_start + __size,
+                             __n, _M_get_Tp_allocator());
+                   }
+                 __catch(...)
+                   {
+                     _M_deallocate(__new_start, __len);
+                     __throw_exception_again;
+                   }
+                 _S_relocate(this->_M_impl._M_start, this->_M_impl._M_finish,
+                             __new_start, _M_get_Tp_allocator());
                }
-             __catch(...)
+             else
                {
-                 std::_Destroy(__new_start, __new_finish,
+                 pointer __destroy_from = pointer();
+                 __try
+                   {
+                     std::__uninitialized_default_n_a(__new_start + __size,
+                             __n, _M_get_Tp_allocator());
+                     __destroy_from = __new_start + __size;
+                     std::__uninitialized_move_if_noexcept_a(
+                             this->_M_impl._M_start, this->_M_impl._M_finish,
+                             __new_start, _M_get_Tp_allocator());
+                   }
+                 __catch(...)
+                   {
+                     if (__destroy_from)
+                       std::_Destroy(__destroy_from, __destroy_from + __n,
+                                     _M_get_Tp_allocator());
+                     _M_deallocate(__new_start, __len);
+                     __throw_exception_again;
+                   }
+                 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
                                _M_get_Tp_allocator());
-                 _M_deallocate(__new_start, __len);
-                 __throw_exception_again;
                }
-             std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
-                           _M_get_Tp_allocator());
+             _GLIBCXX_ASAN_ANNOTATE_REINIT;
              _M_deallocate(this->_M_impl._M_start,
                            this->_M_impl._M_end_of_storage
                            - this->_M_impl._M_start);
              this->_M_impl._M_start = __new_start;
-             this->_M_impl._M_finish = __new_finish;
+             this->_M_impl._M_finish = __new_start + __size + __n;
              this->_M_impl._M_end_of_storage = __new_start + __len;
            }
        }
@@ -592,6 +689,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
     {
       if (capacity() == size())
        return false;
+      _GLIBCXX_ASAN_ANNOTATE_REINIT;
       return std::__shrink_to_fit_aux<vector>::_S_do_it(*this);
     }
 #endif
@@ -603,10 +701,17 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       _M_range_insert(iterator __pos, _InputIterator __first,
                      _InputIterator __last, std::input_iterator_tag)
       {
-       for (; __first != __last; ++__first)
+       if (__pos == end())
          {
-           __pos = insert(__pos, *__first);
-           ++__pos;
+           for (; __first != __last; ++__first)
+             insert(end(), *__first);
+         }
+       else if (__first != __last)
+         {
+           vector __tmp(__first, __last, _M_get_Tp_allocator());
+           insert(__pos,
+                  _GLIBCXX_MAKE_MOVE_ITERATOR(__tmp.begin()),
+                  _GLIBCXX_MAKE_MOVE_ITERATOR(__tmp.end()));
          }
       }
 
@@ -627,11 +732,13 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
                pointer __old_finish(this->_M_impl._M_finish);
                if (__elems_after > __n)
                  {
+                   _GLIBCXX_ASAN_ANNOTATE_GROW(__n);
                    std::__uninitialized_move_a(this->_M_impl._M_finish - __n,
                                                this->_M_impl._M_finish,
                                                this->_M_impl._M_finish,
                                                _M_get_Tp_allocator());
                    this->_M_impl._M_finish += __n;
+                   _GLIBCXX_ASAN_ANNOTATE_GREW(__n);
                    _GLIBCXX_MOVE_BACKWARD3(__position.base(),
                                            __old_finish - __n, __old_finish);
                    std::copy(__first, __last, __position);
@@ -640,15 +747,18 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
                  {
                    _ForwardIterator __mid = __first;
                    std::advance(__mid, __elems_after);
+                   _GLIBCXX_ASAN_ANNOTATE_GROW(__n);
                    std::__uninitialized_copy_a(__mid, __last,
                                                this->_M_impl._M_finish,
                                                _M_get_Tp_allocator());
                    this->_M_impl._M_finish += __n - __elems_after;
+                   _GLIBCXX_ASAN_ANNOTATE_GREW(__n - __elems_after);
                    std::__uninitialized_move_a(__position.base(),
                                                __old_finish,
                                                this->_M_impl._M_finish,
                                                _M_get_Tp_allocator());
                    this->_M_impl._M_finish += __elems_after;
+                   _GLIBCXX_ASAN_ANNOTATE_GREW(__elems_after);
                    std::copy(__first, __mid, __position);
                  }
              }
@@ -682,6 +792,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
                  }
                std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
                              _M_get_Tp_allocator());
+               _GLIBCXX_ASAN_ANNOTATE_REINIT;
                _M_deallocate(this->_M_impl._M_start,
                              this->_M_impl._M_end_of_storage
                              - this->_M_impl._M_start);
@@ -699,11 +810,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
     vector<bool, _Alloc>::
     _M_reallocate(size_type __n)
     {
-      _Bit_type* __q = this->_M_allocate(__n);
-      this->_M_impl._M_finish = _M_copy_aligned(begin(), end(),
-                                               iterator(__q, 0));
+      _Bit_pointer __q = this->_M_allocate(__n);
+      iterator __start(std::__addressof(*__q), 0);
+      iterator __finish(_M_copy_aligned(begin(), end(), __start));
       this->_M_deallocate();
-      this->_M_impl._M_start = iterator(__q, 0);
+      this->_M_impl._M_start = __start;
+      this->_M_impl._M_finish = __finish;
       this->_M_impl._M_end_of_storage = __q + _S_nword(__n);
     }
 
@@ -725,15 +837,16 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
        {
          const size_type __len = 
            _M_check_len(__n, "vector<bool>::_M_fill_insert");
-         _Bit_type * __q = this->_M_allocate(__len);
-         iterator __i = _M_copy_aligned(begin(), __position,
-                                        iterator(__q, 0));
+         _Bit_pointer __q = this->_M_allocate(__len);
+         iterator __start(std::__addressof(*__q), 0);
+         iterator __i = _M_copy_aligned(begin(), __position, __start);
          std::fill(__i, __i + difference_type(__n), __x);
-         this->_M_impl._M_finish = std::copy(__position, end(),
-                                             __i + difference_type(__n));
+         iterator __finish = std::copy(__position, end(),
+                                       __i + difference_type(__n));
          this->_M_deallocate();
          this->_M_impl._M_end_of_storage = __q + _S_nword(__len);
-         this->_M_impl._M_start = iterator(__q, 0);
+         this->_M_impl._M_start = __start;
+         this->_M_impl._M_finish = __finish;
        }
     }
 
@@ -759,14 +872,15 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
              {
                const size_type __len =
                  _M_check_len(__n, "vector<bool>::_M_insert_range");
-               _Bit_type * __q = this->_M_allocate(__len);
-               iterator __i = _M_copy_aligned(begin(), __position,
-                                              iterator(__q, 0));
+               _Bit_pointer __q = this->_M_allocate(__len);
+               iterator __start(std::__addressof(*__q), 0);
+               iterator __i = _M_copy_aligned(begin(), __position, __start);
                __i = std::copy(__first, __last, __i);
-               this->_M_impl._M_finish = std::copy(__position, end(), __i);
+               iterator __finish = std::copy(__position, end(), __i);
                this->_M_deallocate();
                this->_M_impl._M_end_of_storage = __q + _S_nword(__len);
-               this->_M_impl._M_start = iterator(__q, 0);
+               this->_M_impl._M_start = __start;
+               this->_M_impl._M_finish = __finish;
              }
          }
       }
@@ -776,7 +890,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
     vector<bool, _Alloc>::
     _M_insert_aux(iterator __position, bool __x)
     {
-      if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_of_storage)
+      if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_addr())
        {
          std::copy_backward(__position, this->_M_impl._M_finish, 
                             this->_M_impl._M_finish + 1);
@@ -787,14 +901,15 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
        {
          const size_type __len =
            _M_check_len(size_type(1), "vector<bool>::_M_insert_aux");
-         _Bit_type * __q = this->_M_allocate(__len);
-         iterator __i = _M_copy_aligned(begin(), __position,
-                                        iterator(__q, 0));
+         _Bit_pointer __q = this->_M_allocate(__len);
+         iterator __start(std::__addressof(*__q), 0);
+         iterator __i = _M_copy_aligned(begin(), __position, __start);
          *__i++ = __x;
-         this->_M_impl._M_finish = std::copy(__position, end(), __i);
+         iterator __finish = std::copy(__position, end(), __i);
          this->_M_deallocate();
          this->_M_impl._M_end_of_storage = __q + _S_nword(__len);
-         this->_M_impl._M_start = iterator(__q, 0);
+         this->_M_impl._M_start = __start;
+         this->_M_impl._M_finish = __finish;
        }
     }
 
@@ -838,6 +953,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
 #endif
 
 _GLIBCXX_END_NAMESPACE_CONTAINER
+_GLIBCXX_END_NAMESPACE_VERSION
 } // namespace std
 
 #if __cplusplus >= 201103L
@@ -884,4 +1000,9 @@ _GLIBCXX_END_NAMESPACE_VERSION
 
 #endif // C++11
 
+#undef _GLIBCXX_ASAN_ANNOTATE_REINIT
+#undef _GLIBCXX_ASAN_ANNOTATE_GROW
+#undef _GLIBCXX_ASAN_ANNOTATE_GREW
+#undef _GLIBCXX_ASAN_ANNOTATE_SHRINK
+
 #endif /* _VECTOR_TCC */