]> git.proxmox.com Git - ceph.git/blobdiff - ceph/src/boost/boost/container/detail/copy_move_algo.hpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / boost / container / detail / copy_move_algo.hpp
index 5293260091dd0405a486d314f8bf0f3a708dfd17..8325cc65ed5c28ef22d462eba13c3efbea3f2683 100644 (file)
@@ -26,6 +26,7 @@
 #include <boost/container/detail/mpl.hpp>
 #include <boost/container/detail/type_traits.hpp>
 #include <boost/container/detail/construct_in_place.hpp>
+#include <boost/container/detail/destroyers.hpp>
 
 // move
 #include <boost/move/adl_move_swap.hpp>
 // other
 #include <boost/core/no_exceptions_support.hpp>
 // std
-#include <cstring> //for emmove/memcpy
+#include <cstring> //for memmove/memcpy
+
+#if defined(BOOST_GCC) && (BOOST_GCC >= 40600)
+#pragma GCC diagnostic push
+//pair memcpy optimizations rightfully detected by GCC
+#  if defined(BOOST_GCC) && (BOOST_GCC >= 80000)
+#     pragma GCC diagnostic ignored "-Wclass-memaccess"
+#  endif
+//GCC 8 seems a bit confused about array access error with static_vector
+//when out of bound exceptions are being thrown.
+#  if defined(BOOST_GCC) && (BOOST_GCC >= 80000) && (BOOST_GCC < 80200)
+#     pragma GCC diagnostic ignored "-Wstringop-overflow"
+#  endif
+#  pragma GCC diagnostic ignored "-Warray-bounds"
+#endif
 
 namespace boost {
 namespace container {
-namespace container_detail {
+namespace dtl {
 
 template<class I>
 struct are_elements_contiguous
@@ -65,17 +80,15 @@ struct are_elements_contiguous< ::boost::move_iterator<It> >
    : are_elements_contiguous<It>
 {};
 
+}  //namespace dtl {
+
 /////////////////////////
 //    predeclarations
 /////////////////////////
 
-template<class Pointer>
-class vector_iterator;
-
-template<class Pointer>
-class vector_const_iterator;
+template <class Pointer, bool IsConst>
+class vec_iterator;
 
-}  //namespace container_detail {
 }  //namespace container {
 
 namespace interprocess {
@@ -87,20 +100,14 @@ class offset_ptr;
 
 namespace container {
 
-namespace container_detail {
+namespace dtl {
 
 /////////////////////////
 //vector_[const_]iterator
 /////////////////////////
 
-template<class Pointer>
-struct are_elements_contiguous<boost::container::container_detail::vector_iterator<Pointer> >
-{
-   static const bool value = true;
-};
-
-template<class Pointer>
-struct are_elements_contiguous<boost::container::container_detail::vector_const_iterator<Pointer> >
+template <class Pointer, bool IsConst>
+struct are_elements_contiguous<boost::container::vec_iterator<Pointer, IsConst> >
 {
    static const bool value = true;
 };
@@ -130,7 +137,7 @@ template <typename I, typename O>
 struct is_memtransfer_copy_assignable
    : boost::move_detail::and_
       < are_contiguous_and_same<I, O>
-      , container_detail::is_trivially_copy_assignable< typename ::boost::container::iterator_traits<I>::value_type >
+      , dtl::is_trivially_copy_assignable< typename ::boost::container::iterator_traits<I>::value_type >
       >
 {};
 
@@ -138,40 +145,43 @@ template <typename I, typename O>
 struct is_memtransfer_copy_constructible
    : boost::move_detail::and_
       < are_contiguous_and_same<I, O>
-      , container_detail::is_trivially_copy_constructible< typename ::boost::container::iterator_traits<I>::value_type >
+      , dtl::is_trivially_copy_constructible< typename ::boost::container::iterator_traits<I>::value_type >
       >
 {};
 
 template <typename I, typename O, typename R>
 struct enable_if_memtransfer_copy_constructible
-   : enable_if<container_detail::is_memtransfer_copy_constructible<I, O>, R>
+   : enable_if<dtl::is_memtransfer_copy_constructible<I, O>, R>
 {};
 
 template <typename I, typename O, typename R>
 struct disable_if_memtransfer_copy_constructible
-   : disable_if<container_detail::is_memtransfer_copy_constructible<I, O>, R>
+   : disable_if<dtl::is_memtransfer_copy_constructible<I, O>, R>
 {};
 
 template <typename I, typename O, typename R>
 struct enable_if_memtransfer_copy_assignable
-   : enable_if<container_detail::is_memtransfer_copy_assignable<I, O>, R>
+   : enable_if<dtl::is_memtransfer_copy_assignable<I, O>, R>
 {};
 
 template <typename I, typename O, typename R>
 struct disable_if_memtransfer_copy_assignable
-   : disable_if<container_detail::is_memtransfer_copy_assignable<I, O>, R>
+   : disable_if<dtl::is_memtransfer_copy_assignable<I, O>, R>
 {};
 
 template
    <typename I, // I models InputIterator
     typename F> // F models ForwardIterator
-inline F memmove(I f, I l, F r) BOOST_NOEXCEPT_OR_NOTHROW
+BOOST_CONTAINER_FORCEINLINE F memmove(I f, I l, F r) BOOST_NOEXCEPT_OR_NOTHROW
 {
    typedef typename boost::container::iterator_traits<I>::value_type value_type;
-   typename boost::container::iterator_traits<I>::difference_type n = boost::container::iterator_distance(f, l);
-   if(n){
-      std::memmove(boost::movelib::iterator_to_raw_pointer(r), boost::movelib::iterator_to_raw_pointer(f), sizeof(value_type)*n);
-      boost::container::iterator_advance(r, n);
+   value_type *const dest_raw = boost::movelib::iterator_to_raw_pointer(r);
+   const value_type *const beg_raw = boost::movelib::iterator_to_raw_pointer(f);
+   const value_type *const end_raw = boost::movelib::iterator_to_raw_pointer(l);
+   if(BOOST_LIKELY(beg_raw != end_raw && dest_raw && beg_raw)){
+      const typename boost::container::iterator_traits<I>::difference_type n = end_raw - beg_raw;
+      std::memmove(dest_raw, beg_raw, sizeof(value_type)*n);
+      r += n;
    }
    return r;
 }
@@ -180,13 +190,14 @@ template
    <typename I, // I models InputIterator
     typename U, // U models unsigned integral constant
     typename F> // F models ForwardIterator
-F memmove_n(I f, U n, F r) BOOST_NOEXCEPT_OR_NOTHROW
+BOOST_CONTAINER_FORCEINLINE F memmove_n(I f, U n, F r) BOOST_NOEXCEPT_OR_NOTHROW
 {
    typedef typename boost::container::iterator_traits<I>::value_type value_type;
-   if(n){
+   if(BOOST_LIKELY(n)){
       std::memmove(boost::movelib::iterator_to_raw_pointer(r), boost::movelib::iterator_to_raw_pointer(f), sizeof(value_type)*n);
-      boost::container::iterator_advance(r, n);
+      r += n;
    }
+
    return r;
 }
 
@@ -194,12 +205,12 @@ template
    <typename I, // I models InputIterator
     typename U, // U models unsigned integral constant
     typename F> // F models ForwardIterator
-I memmove_n_source(I f, U n, F r) BOOST_NOEXCEPT_OR_NOTHROW
+BOOST_CONTAINER_FORCEINLINE I memmove_n_source(I f, U n, F r) BOOST_NOEXCEPT_OR_NOTHROW
 {
-   if(n){
+   if(BOOST_LIKELY(n)){
       typedef typename boost::container::iterator_traits<I>::value_type value_type;
       std::memmove(boost::movelib::iterator_to_raw_pointer(r), boost::movelib::iterator_to_raw_pointer(f), sizeof(value_type)*n);
-      boost::container::iterator_advance(f, n);
+      f += n;
    }
    return f;
 }
@@ -208,13 +219,13 @@ template
    <typename I, // I models InputIterator
     typename U, // U models unsigned integral constant
     typename F> // F models ForwardIterator
-I memmove_n_source_dest(I f, U n, F &r) BOOST_NOEXCEPT_OR_NOTHROW
+BOOST_CONTAINER_FORCEINLINE I memmove_n_source_dest(I f, U n, F &r) BOOST_NOEXCEPT_OR_NOTHROW
 {
    typedef typename boost::container::iterator_traits<I>::value_type value_type;
-   if(n){
+   if(BOOST_LIKELY(n)){
       std::memmove(boost::movelib::iterator_to_raw_pointer(r), boost::movelib::iterator_to_raw_pointer(f), sizeof(value_type)*n);
-      boost::container::iterator_advance(f, n);
-      boost::container::iterator_advance(r, n);
+      f += n;
+      r += n;
    }
    return f;
 }
@@ -224,44 +235,44 @@ struct is_memzero_initializable
 {
    typedef typename ::boost::container::iterator_traits<O>::value_type value_type;
    static const bool value = are_elements_contiguous<O>::value &&
-      (  container_detail::is_integral<value_type>::value || container_detail::is_enum<value_type>::value
+      (  dtl::is_integral<value_type>::value || dtl::is_enum<value_type>::value
       #if defined(BOOST_CONTAINER_MEMZEROED_POINTER_IS_NULL)
-      || container_detail::is_pointer<value_type>::value
+      || dtl::is_pointer<value_type>::value
       #endif
       #if defined(BOOST_CONTAINER_MEMZEROED_FLOATING_POINT_IS_ZERO)
-      || container_detail::is_floating_point<value_type>::value
+      || dtl::is_floating_point<value_type>::value
       #endif
       #if defined(BOOST_CONTAINER_MEMZEROED_FLOATING_POINT_IS_ZERO) && defined(BOOST_CONTAINER_MEMZEROED_POINTER_IS_NULL)
-      || container_detail::is_pod<value_type>::value
+      || dtl::is_pod<value_type>::value
       #endif
       );
 };
 
 template <typename O, typename R>
 struct enable_if_memzero_initializable
-   : enable_if_c<container_detail::is_memzero_initializable<O>::value, R>
+   : enable_if_c<dtl::is_memzero_initializable<O>::value, R>
 {};
 
 template <typename O, typename R>
 struct disable_if_memzero_initializable
-   : enable_if_c<!container_detail::is_memzero_initializable<O>::value, R>
+   : enable_if_c<!dtl::is_memzero_initializable<O>::value, R>
 {};
 
 template <typename I, typename R>
 struct enable_if_trivially_destructible
-   : enable_if_c < container_detail::is_trivially_destructible
+   : enable_if_c < dtl::is_trivially_destructible
                   <typename boost::container::iterator_traits<I>::value_type>::value
                , R>
 {};
 
 template <typename I, typename R>
 struct disable_if_trivially_destructible
-   : enable_if_c <!container_detail::is_trivially_destructible
+   : enable_if_c <!dtl::is_trivially_destructible
                   <typename boost::container::iterator_traits<I>::value_type>::value
                , R>
 {};
 
-}  //namespace container_detail {
+}  //namespace dtl {
 
 //////////////////////////////////////////////////////////////////////////////
 //
@@ -281,7 +292,7 @@ template
    <typename Allocator,
     typename I, // I models InputIterator
     typename F> // F models ForwardIterator
-inline typename container_detail::disable_if_memtransfer_copy_constructible<I, F, F>::type
+inline typename dtl::disable_if_memtransfer_copy_constructible<I, F, F>::type
    uninitialized_move_alloc(Allocator &a, I f, I l, F r)
 {
    F back = r;
@@ -305,9 +316,9 @@ template
    <typename Allocator,
     typename I, // I models InputIterator
     typename F> // F models ForwardIterator
-inline typename container_detail::enable_if_memtransfer_copy_constructible<I, F, F>::type
+BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_memtransfer_copy_constructible<I, F, F>::type
    uninitialized_move_alloc(Allocator &, I f, I l, F r) BOOST_NOEXCEPT_OR_NOTHROW
-{  return container_detail::memmove(f, l, r); }
+{  return dtl::memmove(f, l, r); }
 
 //////////////////////////////////////////////////////////////////////////////
 //
@@ -326,12 +337,13 @@ template
    <typename Allocator,
     typename I, // I models InputIterator
     typename F> // F models ForwardIterator
-inline typename container_detail::disable_if_memtransfer_copy_constructible<I, F, F>::type
+inline typename dtl::disable_if_memtransfer_copy_constructible<I, F, F>::type
    uninitialized_move_alloc_n(Allocator &a, I f, typename boost::container::allocator_traits<Allocator>::size_type n, F r)
 {
    F back = r;
    BOOST_TRY{
-      while (n--) {
+      while (n) {
+         --n;
          allocator_traits<Allocator>::construct(a, boost::movelib::iterator_to_raw_pointer(r), boost::move(*f));
          ++f; ++r;
       }
@@ -350,9 +362,9 @@ template
    <typename Allocator,
     typename I, // I models InputIterator
     typename F> // F models ForwardIterator
-inline typename container_detail::enable_if_memtransfer_copy_constructible<I, F, F>::type
+BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_memtransfer_copy_constructible<I, F, F>::type
    uninitialized_move_alloc_n(Allocator &, I f, typename boost::container::allocator_traits<Allocator>::size_type n, F r) BOOST_NOEXCEPT_OR_NOTHROW
-{  return container_detail::memmove_n(f, n, r); }
+{  return dtl::memmove_n(f, n, r); }
 
 //////////////////////////////////////////////////////////////////////////////
 //
@@ -371,12 +383,13 @@ template
    <typename Allocator,
     typename I, // I models InputIterator
     typename F> // F models ForwardIterator
-inline typename container_detail::disable_if_memtransfer_copy_constructible<I, F, I>::type
+inline typename dtl::disable_if_memtransfer_copy_constructible<I, F, I>::type
    uninitialized_move_alloc_n_source(Allocator &a, I f, typename boost::container::allocator_traits<Allocator>::size_type n, F r)
 {
    F back = r;
    BOOST_TRY{
-      while (n--) {
+      while (n) {
+         --n;
          allocator_traits<Allocator>::construct(a, boost::movelib::iterator_to_raw_pointer(r), boost::move(*f));
          ++f; ++r;
       }
@@ -395,9 +408,9 @@ template
    <typename Allocator,
     typename I, // I models InputIterator
     typename F> // F models ForwardIterator
-inline typename container_detail::enable_if_memtransfer_copy_constructible<I, F, I>::type
+BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_memtransfer_copy_constructible<I, F, I>::type
    uninitialized_move_alloc_n_source(Allocator &, I f, typename boost::container::allocator_traits<Allocator>::size_type n, F r) BOOST_NOEXCEPT_OR_NOTHROW
-{  return container_detail::memmove_n_source(f, n, r); }
+{  return dtl::memmove_n_source(f, n, r); }
 
 //////////////////////////////////////////////////////////////////////////////
 //
@@ -416,7 +429,7 @@ template
    <typename Allocator,
     typename I, // I models InputIterator
     typename F> // F models ForwardIterator
-inline typename container_detail::disable_if_memtransfer_copy_constructible<I, F, F>::type
+inline typename dtl::disable_if_memtransfer_copy_constructible<I, F, F>::type
    uninitialized_copy_alloc(Allocator &a, I f, I l, F r)
 {
    F back = r;
@@ -440,9 +453,9 @@ template
    <typename Allocator,
     typename I, // I models InputIterator
     typename F> // F models ForwardIterator
-inline typename container_detail::enable_if_memtransfer_copy_constructible<I, F, F>::type
+BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_memtransfer_copy_constructible<I, F, F>::type
    uninitialized_copy_alloc(Allocator &, I f, I l, F r) BOOST_NOEXCEPT_OR_NOTHROW
-{  return container_detail::memmove(f, l, r); }
+{  return dtl::memmove(f, l, r); }
 
 //////////////////////////////////////////////////////////////////////////////
 //
@@ -461,12 +474,13 @@ template
    <typename Allocator,
     typename I, // I models InputIterator
     typename F> // F models ForwardIterator
-inline typename container_detail::disable_if_memtransfer_copy_constructible<I, F, F>::type
+inline typename dtl::disable_if_memtransfer_copy_constructible<I, F, F>::type
    uninitialized_copy_alloc_n(Allocator &a, I f, typename boost::container::allocator_traits<Allocator>::size_type n, F r)
 {
    F back = r;
    BOOST_TRY{
-      while (n--) {
+      while (n) {
+         --n;
          allocator_traits<Allocator>::construct(a, boost::movelib::iterator_to_raw_pointer(r), *f);
          ++f; ++r;
       }
@@ -485,9 +499,9 @@ template
    <typename Allocator,
     typename I, // I models InputIterator
     typename F> // F models ForwardIterator
-inline typename container_detail::enable_if_memtransfer_copy_constructible<I, F, F>::type
+BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_memtransfer_copy_constructible<I, F, F>::type
    uninitialized_copy_alloc_n(Allocator &, I f, typename boost::container::allocator_traits<Allocator>::size_type n, F r) BOOST_NOEXCEPT_OR_NOTHROW
-{  return container_detail::memmove_n(f, n, r); }
+{  return dtl::memmove_n(f, n, r); }
 
 //////////////////////////////////////////////////////////////////////////////
 //
@@ -506,14 +520,14 @@ template
    <typename Allocator,
     typename I, // I models InputIterator
     typename F> // F models ForwardIterator
-inline typename container_detail::disable_if_memtransfer_copy_constructible<I, F, I>::type
+inline typename dtl::disable_if_memtransfer_copy_constructible<I, F, I>::type
    uninitialized_copy_alloc_n_source(Allocator &a, I f, typename boost::container::allocator_traits<Allocator>::size_type n, F r)
 {
    F back = r;
    BOOST_TRY{
-      while (n--) {
+      while (n) {
          boost::container::construct_in_place(a, boost::movelib::iterator_to_raw_pointer(r), f);
-         ++f; ++r;
+         ++f; ++r; --n;
       }
    }
    BOOST_CATCH(...){
@@ -530,9 +544,9 @@ template
    <typename Allocator,
     typename I, // I models InputIterator
     typename F> // F models ForwardIterator
-inline typename container_detail::enable_if_memtransfer_copy_constructible<I, F, I>::type
+BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_memtransfer_copy_constructible<I, F, I>::type
    uninitialized_copy_alloc_n_source(Allocator &, I f, typename boost::container::allocator_traits<Allocator>::size_type n, F r) BOOST_NOEXCEPT_OR_NOTHROW
-{  return container_detail::memmove_n_source(f, n, r); }
+{  return dtl::memmove_n_source(f, n, r); }
 
 //////////////////////////////////////////////////////////////////////////////
 //
@@ -550,12 +564,13 @@ inline typename container_detail::enable_if_memtransfer_copy_constructible<I, F,
 template
    <typename Allocator,
     typename F> // F models ForwardIterator
-inline typename container_detail::disable_if_memzero_initializable<F, F>::type
+inline typename dtl::disable_if_memzero_initializable<F, F>::type
    uninitialized_value_init_alloc_n(Allocator &a, typename boost::container::allocator_traits<Allocator>::size_type n, F r)
 {
    F back = r;
    BOOST_TRY{
-      while (n--) {
+      while (n) {
+         --n;
          allocator_traits<Allocator>::construct(a, boost::movelib::iterator_to_raw_pointer(r));
          ++r;
       }
@@ -573,12 +588,14 @@ inline typename container_detail::disable_if_memzero_initializable<F, F>::type
 template
    <typename Allocator,
     typename F> // F models ForwardIterator
-inline typename container_detail::enable_if_memzero_initializable<F, F>::type
+BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_memzero_initializable<F, F>::type
    uninitialized_value_init_alloc_n(Allocator &, typename boost::container::allocator_traits<Allocator>::size_type n, F r)
 {
    typedef typename boost::container::iterator_traits<F>::value_type value_type;
-   std::memset((void*)boost::movelib::iterator_to_raw_pointer(r), 0, sizeof(value_type)*n);
-   boost::container::iterator_advance(r, n);
+   if (BOOST_LIKELY(n)){
+      std::memset((void*)boost::movelib::iterator_to_raw_pointer(r), 0, sizeof(value_type)*n);
+      r += n;
+   }
    return r;
 }
 
@@ -602,7 +619,8 @@ inline F uninitialized_default_init_alloc_n(Allocator &a, typename boost::contai
 {
    F back = r;
    BOOST_TRY{
-      while (n--) {
+      while (n) {
+         --n;
          allocator_traits<Allocator>::construct(a, boost::movelib::iterator_to_raw_pointer(r), default_init);
          ++r;
       }
@@ -674,7 +692,8 @@ inline F uninitialized_fill_alloc_n(Allocator &a, const T &v, typename boost::co
 {
    F back = r;
    BOOST_TRY{
-      while (n--) {
+      while (n) {
+         --n;
          allocator_traits<Allocator>::construct(a, boost::movelib::iterator_to_raw_pointer(r), v);
          ++r;
       }
@@ -698,7 +717,7 @@ inline F uninitialized_fill_alloc_n(Allocator &a, const T &v, typename boost::co
 template
 <typename I,   // I models InputIterator
 typename F>    // F models ForwardIterator
-inline typename container_detail::disable_if_memtransfer_copy_assignable<I, F, F>::type
+inline typename dtl::disable_if_memtransfer_copy_assignable<I, F, F>::type
    copy(I f, I l, F r)
 {
    while (f != l) {
@@ -711,9 +730,9 @@ inline typename container_detail::disable_if_memtransfer_copy_assignable<I, F, F
 template
 <typename I,   // I models InputIterator
 typename F>    // F models ForwardIterator
-inline typename container_detail::enable_if_memtransfer_copy_assignable<I, F, F>::type
+inline typename dtl::enable_if_memtransfer_copy_assignable<I, F, F>::type
    copy(I f, I l, F r) BOOST_NOEXCEPT_OR_NOTHROW
-{  return container_detail::memmove(f, l, r); }
+{  return dtl::memmove(f, l, r); }
 
 //////////////////////////////////////////////////////////////////////////////
 //
@@ -725,10 +744,11 @@ template
 <typename I,   // I models InputIterator
 typename U,   // U models unsigned integral constant
 typename F>   // F models ForwardIterator
-inline typename container_detail::disable_if_memtransfer_copy_assignable<I, F, F>::type
+inline typename dtl::disable_if_memtransfer_copy_assignable<I, F, F>::type
    copy_n(I f, U n, F r)
 {
-   while (n--) {
+   while (n) {
+      --n;
       *r = *f;
       ++f; ++r;
    }
@@ -739,9 +759,9 @@ template
 <typename I,   // I models InputIterator
 typename U,   // U models unsigned integral constant
 typename F>   // F models ForwardIterator
-inline typename container_detail::enable_if_memtransfer_copy_assignable<I, F, F>::type
+BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_memtransfer_copy_assignable<I, F, F>::type
    copy_n(I f, U n, F r) BOOST_NOEXCEPT_OR_NOTHROW
-{  return container_detail::memmove_n(f, n, r); }
+{  return dtl::memmove_n(f, n, r); }
 
 //////////////////////////////////////////////////////////////////////////////
 //
@@ -753,10 +773,11 @@ template
 <typename I,   // I models InputIterator
 typename U,   // U models unsigned integral constant
 typename F>   // F models ForwardIterator
-inline typename container_detail::disable_if_memtransfer_copy_assignable<I, F, I>::type
+inline typename dtl::disable_if_memtransfer_copy_assignable<I, F, I>::type
    copy_n_source(I f, U n, F r)
 {
-   while (n--) {
+   while (n) {
+      --n;
       boost::container::assign_in_place(r, f);
       ++f; ++r;
    }
@@ -767,9 +788,9 @@ template
 <typename I,   // I models InputIterator
 typename U,   // U models unsigned integral constant
 typename F>   // F models ForwardIterator
-inline typename container_detail::enable_if_memtransfer_copy_assignable<I, F, I>::type
+BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_memtransfer_copy_assignable<I, F, I>::type
    copy_n_source(I f, U n, F r) BOOST_NOEXCEPT_OR_NOTHROW
-{  return container_detail::memmove_n_source(f, n, r); }
+{  return dtl::memmove_n_source(f, n, r); }
 
 //////////////////////////////////////////////////////////////////////////////
 //
@@ -781,10 +802,11 @@ template
 <typename I,   // I models InputIterator
 typename U,   // U models unsigned integral constant
 typename F>   // F models ForwardIterator
-inline typename container_detail::disable_if_memtransfer_copy_assignable<I, F, I>::type
+inline typename dtl::disable_if_memtransfer_copy_assignable<I, F, I>::type
    copy_n_source_dest(I f, U n, F &r)
 {
-   while (n--) {
+   while (n) {
+      --n;
       *r = *f;
       ++f; ++r;
    }
@@ -795,9 +817,9 @@ template
 <typename I,   // I models InputIterator
 typename U,   // U models unsigned integral constant
 typename F>   // F models ForwardIterator
-inline typename container_detail::enable_if_memtransfer_copy_assignable<I, F, I>::type
+BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_memtransfer_copy_assignable<I, F, I>::type
    copy_n_source_dest(I f, U n, F &r) BOOST_NOEXCEPT_OR_NOTHROW
-{  return container_detail::memmove_n_source_dest(f, n, r);  }
+{  return dtl::memmove_n_source_dest(f, n, r);  }
 
 //////////////////////////////////////////////////////////////////////////////
 //
@@ -808,7 +830,7 @@ inline typename container_detail::enable_if_memtransfer_copy_assignable<I, F, I>
 template
 <typename I,   // I models InputIterator
 typename F>   // F models ForwardIterator
-inline typename container_detail::disable_if_memtransfer_copy_assignable<I, F, F>::type
+inline typename dtl::disable_if_memtransfer_copy_assignable<I, F, F>::type
    move(I f, I l, F r)
 {
    while (f != l) {
@@ -821,9 +843,9 @@ inline typename container_detail::disable_if_memtransfer_copy_assignable<I, F, F
 template
 <typename I,   // I models InputIterator
 typename F>   // F models ForwardIterator
-inline typename container_detail::enable_if_memtransfer_copy_assignable<I, F, F>::type
+inline typename dtl::enable_if_memtransfer_copy_assignable<I, F, F>::type
    move(I f, I l, F r) BOOST_NOEXCEPT_OR_NOTHROW
-{  return container_detail::memmove(f, l, r); }
+{  return dtl::memmove(f, l, r); }
 
 //////////////////////////////////////////////////////////////////////////////
 //
@@ -835,10 +857,11 @@ template
 <typename I,   // I models InputIterator
 typename U,   // U models unsigned integral constant
 typename F>   // F models ForwardIterator
-inline typename container_detail::disable_if_memtransfer_copy_assignable<I, F, F>::type
+inline typename dtl::disable_if_memtransfer_copy_assignable<I, F, F>::type
    move_n(I f, U n, F r)
 {
-   while (n--) {
+   while (n) {
+      --n;
       *r = ::boost::move(*f);
       ++f; ++r;
    }
@@ -849,9 +872,9 @@ template
 <typename I,   // I models InputIterator
 typename U,   // U models unsigned integral constant
 typename F>   // F models ForwardIterator
-inline typename container_detail::enable_if_memtransfer_copy_assignable<I, F, F>::type
+BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_memtransfer_copy_assignable<I, F, F>::type
    move_n(I f, U n, F r) BOOST_NOEXCEPT_OR_NOTHROW
-{  return container_detail::memmove_n(f, n, r); }
+{  return dtl::memmove_n(f, n, r); }
 
 
 //////////////////////////////////////////////////////////////////////////////
@@ -863,7 +886,7 @@ inline typename container_detail::enable_if_memtransfer_copy_assignable<I, F, F>
 template
 <typename I,   // I models BidirectionalIterator
 typename F>    // F models ForwardIterator
-inline typename container_detail::disable_if_memtransfer_copy_assignable<I, F, F>::type
+inline typename dtl::disable_if_memtransfer_copy_assignable<I, F, F>::type
    move_backward(I f, I l, F r)
 {
    while (f != l) {
@@ -876,13 +899,15 @@ inline typename container_detail::disable_if_memtransfer_copy_assignable<I, F, F
 template
 <typename I,   // I models InputIterator
 typename F>   // F models ForwardIterator
-inline typename container_detail::enable_if_memtransfer_copy_assignable<I, F, F>::type
+BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_memtransfer_copy_assignable<I, F, F>::type
    move_backward(I f, I l, F r) BOOST_NOEXCEPT_OR_NOTHROW
 {
    typedef typename boost::container::iterator_traits<I>::value_type value_type;
    const typename boost::container::iterator_traits<I>::difference_type n = boost::container::iterator_distance(f, l);
-   r -= n;
-   std::memmove((boost::movelib::iterator_to_raw_pointer)(r), (boost::movelib::iterator_to_raw_pointer)(f), sizeof(value_type)*n);
+   if (BOOST_LIKELY(n)){
+      r -= n;
+      std::memmove((boost::movelib::iterator_to_raw_pointer)(r), (boost::movelib::iterator_to_raw_pointer)(f), sizeof(value_type)*n);
+   }
    return r;
 }
 
@@ -896,10 +921,11 @@ template
 <typename I    // I models InputIterator
 ,typename U    // U models unsigned integral constant
 ,typename F>   // F models ForwardIterator
-inline typename container_detail::disable_if_memtransfer_copy_assignable<I, F, I>::type
+inline typename dtl::disable_if_memtransfer_copy_assignable<I, F, I>::type
    move_n_source_dest(I f, U n, F &r)
 {
-   while (n--) {
+   while (n) {
+      --n;
       *r = ::boost::move(*f);
       ++f; ++r;
    }
@@ -910,9 +936,9 @@ template
 <typename I    // I models InputIterator
 ,typename U    // U models unsigned integral constant
 ,typename F>   // F models ForwardIterator
-inline typename container_detail::enable_if_memtransfer_copy_assignable<I, F, I>::type
+BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_memtransfer_copy_assignable<I, F, I>::type
    move_n_source_dest(I f, U n, F &r) BOOST_NOEXCEPT_OR_NOTHROW
-{  return container_detail::memmove_n_source_dest(f, n, r); }
+{  return dtl::memmove_n_source_dest(f, n, r); }
 
 //////////////////////////////////////////////////////////////////////////////
 //
@@ -924,10 +950,11 @@ template
 <typename I    // I models InputIterator
 ,typename U    // U models unsigned integral constant
 ,typename F>   // F models ForwardIterator
-inline typename container_detail::disable_if_memtransfer_copy_assignable<I, F, I>::type
+inline typename dtl::disable_if_memtransfer_copy_assignable<I, F, I>::type
    move_n_source(I f, U n, F r)
 {
-   while (n--) {
+   while (n) {
+      --n;
       *r = ::boost::move(*f);
       ++f; ++r;
    }
@@ -938,9 +965,9 @@ template
 <typename I    // I models InputIterator
 ,typename U    // U models unsigned integral constant
 ,typename F>   // F models ForwardIterator
-inline typename container_detail::enable_if_memtransfer_copy_assignable<I, F, I>::type
+BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_memtransfer_copy_assignable<I, F, I>::type
    move_n_source(I f, U n, F r) BOOST_NOEXCEPT_OR_NOTHROW
-{  return container_detail::memmove_n_source(f, n, r); }
+{  return dtl::memmove_n_source(f, n, r); }
 
 //////////////////////////////////////////////////////////////////////////////
 //
@@ -952,7 +979,7 @@ template
    <typename Allocator
    ,typename I   // I models InputIterator
    ,typename U>  // U models unsigned integral constant
-inline typename container_detail::disable_if_trivially_destructible<I, void>::type
+inline typename dtl::disable_if_trivially_destructible<I, void>::type
    destroy_alloc_n(Allocator &a, I f, U n)
 {
    while(n){
@@ -966,7 +993,7 @@ template
    <typename Allocator
    ,typename I   // I models InputIterator
    ,typename U>  // U models unsigned integral constant
-inline typename container_detail::enable_if_trivially_destructible<I, void>::type
+BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_trivially_destructible<I, void>::type
    destroy_alloc_n(Allocator &, I, U)
 {}
 
@@ -982,7 +1009,7 @@ template
    ,typename F // F models ForwardIterator
    ,typename G // G models ForwardIterator
    >
-inline typename container_detail::disable_if_memtransfer_copy_assignable<F, G, void>::type
+inline typename dtl::disable_if_memtransfer_copy_assignable<F, G, void>::type
    deep_swap_alloc_n( Allocator &a, F short_range_f, typename allocator_traits<Allocator>::size_type n_i
                     , G large_range_f, typename allocator_traits<Allocator>::size_type n_j)
 {
@@ -1002,21 +1029,21 @@ template
    ,typename F // F models ForwardIterator
    ,typename G // G models ForwardIterator
    >
-inline typename container_detail::enable_if_c
-   < container_detail::is_memtransfer_copy_assignable<F, G>::value && (MaxTmpBytes <= DeepSwapAllocNMaxStorage) && false
+inline typename dtl::enable_if_c
+   < dtl::is_memtransfer_copy_assignable<F, G>::value && (MaxTmpBytes <= DeepSwapAllocNMaxStorage) && false
    , void>::type
    deep_swap_alloc_n( Allocator &a, F short_range_f, typename allocator_traits<Allocator>::size_type n_i
                     , G large_range_f, typename allocator_traits<Allocator>::size_type n_j)
 {
    typedef typename allocator_traits<Allocator>::value_type value_type;
-   typedef typename container_detail::aligned_storage
-      <MaxTmpBytes, container_detail::alignment_of<value_type>::value>::type storage_type;
+   typedef typename dtl::aligned_storage
+      <MaxTmpBytes, dtl::alignment_of<value_type>::value>::type storage_type;
    storage_type storage;
 
    const std::size_t n_i_bytes = sizeof(value_type)*n_i;
    void *const large_ptr = static_cast<void*>(boost::movelib::iterator_to_raw_pointer(large_range_f));
    void *const short_ptr = static_cast<void*>(boost::movelib::iterator_to_raw_pointer(short_range_f));
-   void *const stora_ptr = static_cast<void*>(boost::movelib::iterator_to_raw_pointer(storage));
+   void *const stora_ptr = static_cast<void*>(boost::movelib::iterator_to_raw_pointer(storage.data));
    std::memcpy(stora_ptr, large_ptr, n_i_bytes);
    std::memcpy(large_ptr, short_ptr, n_i_bytes);
    std::memcpy(short_ptr, stora_ptr, n_i_bytes);
@@ -1032,22 +1059,22 @@ template
    ,typename F // F models ForwardIterator
    ,typename G // G models ForwardIterator
    >
-inline typename container_detail::enable_if_c
-   < container_detail::is_memtransfer_copy_assignable<F, G>::value && true//(MaxTmpBytes > DeepSwapAllocNMaxStorage)
+inline typename dtl::enable_if_c
+   < dtl::is_memtransfer_copy_assignable<F, G>::value && true//(MaxTmpBytes > DeepSwapAllocNMaxStorage)
    , void>::type
    deep_swap_alloc_n( Allocator &a, F short_range_f, typename allocator_traits<Allocator>::size_type n_i
                     , G large_range_f, typename allocator_traits<Allocator>::size_type n_j)
 {
    typedef typename allocator_traits<Allocator>::value_type value_type;
-   typedef typename container_detail::aligned_storage
-      <DeepSwapAllocNMaxStorage, container_detail::alignment_of<value_type>::value>::type storage_type;
+   typedef typename dtl::aligned_storage
+      <DeepSwapAllocNMaxStorage, dtl::alignment_of<value_type>::value>::type storage_type;
    storage_type storage;
    const std::size_t sizeof_storage = sizeof(storage);
 
    std::size_t n_i_bytes = sizeof(value_type)*n_i;
    char *large_ptr = static_cast<char*>(static_cast<void*>(boost::movelib::iterator_to_raw_pointer(large_range_f)));
    char *short_ptr = static_cast<char*>(static_cast<void*>(boost::movelib::iterator_to_raw_pointer(short_range_f)));
-   char *stora_ptr = static_cast<char*>(static_cast<void*>(&storage));
+   char *stora_ptr = static_cast<char*>(static_cast<void*>(storage.data));
 
    std::size_t szt_times = n_i_bytes/sizeof_storage;
    const std::size_t szt_rem = n_i_bytes%sizeof_storage;
@@ -1146,7 +1173,106 @@ void move_assign_range_alloc_n( Allocator &a, I inp_start, typename allocator_tr
    }
 }
 
+template<class Allocator, class Iterator>
+struct array_destructor
+{
+   typedef typename ::boost::container::iterator_traits<Iterator>::value_type value_type;
+   typedef typename dtl::if_c
+      <dtl::is_trivially_destructible<value_type>::value
+      ,dtl::null_scoped_destructor_range<Allocator>
+      ,dtl::scoped_destructor_range<Allocator>
+      >::type type;
+};
+
+template
+   <typename Allocator
+   ,typename F // F models ForwardIterator
+   ,typename O // G models OutputIterator
+   ,typename InsertionProxy
+   >
+void uninitialized_move_and_insert_alloc
+   ( Allocator &a
+   , F first
+   , F pos
+   , F last
+   , O d_first
+   , typename allocator_traits<Allocator>::size_type n
+   , InsertionProxy insert_range_proxy)
+{
+   typedef typename array_destructor<Allocator, F>::type array_destructor_t;
+
+   //Anti-exception rollbacks
+   array_destructor_t new_values_destroyer(d_first, d_first, a);
+
+   //Initialize with [begin(), pos) old buffer
+   //the start of the new buffer
+   O d_last = ::boost::container::uninitialized_move_alloc(a, first, pos, d_first);
+   new_values_destroyer.set_end(d_last);
+   //Initialize new objects, starting from previous point
+   insert_range_proxy.uninitialized_copy_n_and_update(a, d_last, n);
+   d_last += n;
+   new_values_destroyer.set_end(d_last);
+   //Initialize from the rest of the old buffer,
+   //starting from previous point
+   (void) ::boost::container::uninitialized_move_alloc(a, pos, last, d_last);
+   //All construction successful, disable rollbacks
+   new_values_destroyer.release();
+}
+
+template
+   <typename Allocator
+   ,typename F // F models ForwardIterator
+   ,typename InsertionProxy
+   >
+void expand_forward_and_insert_alloc
+   ( Allocator &a
+   , F pos
+   , F last
+   , typename allocator_traits<Allocator>::size_type n
+   , InsertionProxy insert_range_proxy)
+{
+   typedef typename array_destructor<Allocator, F>::type array_destructor_t;
+
+   if (BOOST_UNLIKELY(!n)){
+      return;
+   }
+   else if (last == pos){
+      insert_range_proxy.uninitialized_copy_n_and_update(a, last, n);
+   }
+   else{
+      typedef typename allocator_traits<Allocator>::size_type alloc_size_type;
+      const alloc_size_type elems_after = static_cast<alloc_size_type>(last - pos);
+      if(elems_after >= n){
+         //New elements can be just copied.
+         //Move to uninitialized memory last objects
+         ::boost::container::uninitialized_move_alloc_n(a, last - n, n, last);
+         array_destructor_t on_exception(last, last, a);
+         //Copy previous to last objects to the initialized end
+         boost::container::move_backward(pos, last - n, last);
+         //Insert new objects in the pos
+         insert_range_proxy.copy_n_and_update(a, pos, n);
+         on_exception.release();
+      }
+      else {
+         //The new elements don't fit in the [pos, end()) range.
+         //Copy old [pos, end()) elements to the uninitialized memory (a gap is created)
+         F new_last = ::boost::container::uninitialized_move_alloc(a, pos, last, pos + n);
+         array_destructor_t on_exception(pos + n, new_last, a);
+         //Copy first new elements in pos (gap is still there)
+         insert_range_proxy.copy_n_and_update(a, pos, elems_after);
+         //Copy to the beginning of the unallocated zone the last new elements (the gap is closed).
+         insert_range_proxy.uninitialized_copy_n_and_update(a, last, n - elems_after);
+         on_exception.release();
+      }
+   }
+}
+
 }  //namespace container {
 }  //namespace boost {
 
+//#pragma GCC diagnostic ignored "-Wclass-memaccess"
+#if defined(BOOST_GCC) && (BOOST_GCC >= 40600)
+#pragma GCC diagnostic pop
+#endif
+
 #endif   //#ifndef BOOST_CONTAINER_DETAIL_COPY_MOVE_ALGO_HPP