////////////////////////////////////////////////////////////////////////////// // // (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost // Software License, Version 1.0. (See accompanying file // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // // See http://www.boost.org/libs/container for documentation. // ////////////////////////////////////////////////////////////////////////////// #ifndef BOOST_CONTAINER_DETAIL_UTILITIES_HPP #define BOOST_CONTAINER_DETAIL_UTILITIES_HPP #include #include #include #include //for ::memmove / ::memcpy #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include //std::distance namespace boost { namespace container { ////////////////////////////////////////////////////////////////////////////// // // swap // ////////////////////////////////////////////////////////////////////////////// namespace container_swap { template::value > struct has_member_swap { static const bool value = boost::container::container_detail:: has_member_function_callable_with_swap::value; }; template struct has_member_swap { static const bool value = false; }; } //namespace container_swap { template inline typename container_detail::enable_if_c ::value, void>::type swap_dispatch(T &left, T &right) //swap using member swap { left.swap(right); // may throw } template inline typename container_detail::enable_if_c ::value && boost::has_move_emulation_enabled::value, void>::type swap_dispatch(T &left, T &right) { T temp(boost::move(left)); // may throw left = boost::move(right); // may throw right = boost::move(temp); // may throw } template inline typename container_detail::enable_if_c ::value && !boost::has_move_emulation_enabled::value, void>::type swap_dispatch(T &left, T &right) { using std::swap; swap(left, right); // may throw } namespace container_detail { template inline T* addressof(T& obj) { return static_cast( static_cast( const_cast( &reinterpret_cast(obj) ))); } template const T &max_value(const T &a, const T &b) { return a > b ? a : b; } template const T &min_value(const T &a, const T &b) { return a < b ? a : b; } enum NextCapacityOption { NextCapacityDouble, NextCapacity60Percent }; template struct next_capacity_calculator; template struct next_capacity_calculator { static SizeType get(const SizeType max_size ,const SizeType capacity ,const SizeType n) { const SizeType remaining = max_size - capacity; if ( remaining < n ) boost::container::throw_length_error("get_next_capacity, allocator's max_size reached"); const SizeType additional = max_value(n, capacity); return ( remaining < additional ) ? max_size : ( capacity + additional ); } }; template struct next_capacity_calculator { static SizeType get(const SizeType max_size ,const SizeType capacity ,const SizeType n) { const SizeType remaining = max_size - capacity; if ( remaining < n ) boost::container::throw_length_error("get_next_capacity, allocator's max_size reached"); const SizeType m3 = max_size/3; if (capacity < m3) return capacity + max_value(3*(capacity+1)/5, n); if (capacity < m3*2) return capacity + max_value((capacity+1)/2, n); return max_size; } }; template inline T* to_raw_pointer(T* p) { return p; } template inline typename boost::intrusive::pointer_traits::element_type* to_raw_pointer(const Pointer &p) { return boost::container::container_detail::to_raw_pointer(p.operator->()); } template inline T* iterator_to_pointer(T* i) { return i; } template inline typename std::iterator_traits::pointer iterator_to_pointer(const Iterator &i) { return i.operator->(); } template inline typename boost::intrusive::pointer_traits ::pointer>::element_type* iterator_to_raw_pointer(const Iterator &i) { return (to_raw_pointer)((iterator_to_pointer)(i)); } template inline void swap_alloc(AllocatorType &, AllocatorType &, container_detail::false_type) BOOST_CONTAINER_NOEXCEPT {} template inline void swap_alloc(AllocatorType &l, AllocatorType &r, container_detail::true_type) { boost::container::swap_dispatch(l, r); } template inline void assign_alloc(AllocatorType &, const AllocatorType &, container_detail::false_type) BOOST_CONTAINER_NOEXCEPT {} template inline void assign_alloc(AllocatorType &l, const AllocatorType &r, container_detail::true_type) { l = r; } template inline void move_alloc(AllocatorType &, AllocatorType &, container_detail::false_type) BOOST_CONTAINER_NOEXCEPT {} template inline void move_alloc(AllocatorType &l, AllocatorType &r, container_detail::true_type) { l = ::boost::move(r); } //Rounds "orig_size" by excess to round_to bytes template inline SizeType get_rounded_size(SizeType orig_size, SizeType round_to) { return ((orig_size-1)/round_to+1)*round_to; } template struct ct_rounded_size { enum { value = ((OrigSize-1)/RoundTo+1)*RoundTo }; }; template struct are_elements_contiguous { static const bool value = false; }; ///////////////////////// // raw pointers ///////////////////////// template struct are_elements_contiguous { static const bool value = true; }; ///////////////////////// // predeclarations ///////////////////////// #ifndef BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER template class vector_iterator; template class vector_const_iterator; #endif //BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER } //namespace container_detail { } //namespace container { namespace interprocess { template class offset_ptr; } //namespace interprocess { namespace container { namespace container_detail { ///////////////////////// //vector_[const_]iterator ///////////////////////// #ifndef BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER template struct are_elements_contiguous > { static const bool value = true; }; template struct are_elements_contiguous > { static const bool value = true; }; #endif //BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER ///////////////////////// // offset_ptr ///////////////////////// template struct are_elements_contiguous< ::boost::interprocess::offset_ptr > { static const bool value = true; }; template struct are_contiguous_and_same { static const bool is_same_io = is_same< typename remove_const< typename ::std::iterator_traits::value_type >::type , typename ::std::iterator_traits::value_type >::value; static const bool value = is_same_io && are_elements_contiguous::value && are_elements_contiguous::value; }; template struct is_memtransfer_copy_assignable { static const bool value = are_contiguous_and_same::value && boost::has_trivial_assign< typename ::std::iterator_traits::value_type >::value; }; template struct is_memtransfer_copy_constructible { static const bool value = are_contiguous_and_same::value && boost::has_trivial_copy< typename ::std::iterator_traits::value_type >::value; }; template struct enable_if_memtransfer_copy_constructible : public enable_if_c::value, R> {}; template struct disable_if_memtransfer_copy_constructible : public enable_if_c::value, R> {}; template struct enable_if_memtransfer_copy_assignable : public enable_if_c::value, R> {}; template struct disable_if_memtransfer_copy_assignable : public enable_if_c::value, R> {}; template // F models ForwardIterator inline F memmove(I f, I l, F r) BOOST_CONTAINER_NOEXCEPT { typedef typename std::iterator_traits::value_type value_type; typename std::iterator_traits::difference_type n = std::distance(f, l); ::memmove((iterator_to_raw_pointer)(r), (iterator_to_raw_pointer)(f), sizeof(value_type)*n); std::advance(r, n); return r; } template // F models ForwardIterator F memmove_n(I f, typename std::iterator_traits::difference_type n, F r) BOOST_CONTAINER_NOEXCEPT { typedef typename std::iterator_traits::value_type value_type; ::memmove((iterator_to_raw_pointer)(r), (iterator_to_raw_pointer)(f), sizeof(value_type)*n); std::advance(r, n); return r; } template // F models ForwardIterator I memmove_n_source(I f, typename std::iterator_traits::difference_type n, F r) BOOST_CONTAINER_NOEXCEPT { typedef typename std::iterator_traits::value_type value_type; ::memmove((iterator_to_raw_pointer)(r), (iterator_to_raw_pointer)(f), sizeof(value_type)*n); std::advance(f, n); return f; } template // F models ForwardIterator I memmove_n_source_dest(I f, typename std::iterator_traits::difference_type n, F &r) BOOST_CONTAINER_NOEXCEPT { typedef typename std::iterator_traits::value_type value_type; ::memmove((iterator_to_raw_pointer)(r), (iterator_to_raw_pointer)(f), sizeof(value_type)*n); std::advance(f, n); std::advance(r, n); return f; } template struct is_memzero_initializable { typedef typename ::std::iterator_traits::value_type value_type; static const bool value = are_elements_contiguous::value && ( ::boost::is_integral::value || ::boost::is_enum::value #if defined(BOOST_CONTAINER_MEMZEROED_POINTER_IS_NULL) || ::boost::is_pointer::value #endif #if defined(BOOST_CONTAINER_MEMZEROED_FLOATING_POINT_IS_ZERO) || ::boost::is_floating_point::value #endif #if defined(BOOST_CONTAINER_MEMZEROED_FLOATING_POINT_IS_ZERO) && defined(BOOST_CONTAINER_MEMZEROED_POINTER_IS_NULL) || ::boost::is_pod::value #endif ); }; template struct enable_if_memzero_initializable : public enable_if_c::value, R> {}; template struct disable_if_memzero_initializable : public enable_if_c::value, R> {}; } //namespace container_detail { ////////////////////////////////////////////////////////////////////////////// // // uninitialized_move_alloc // ////////////////////////////////////////////////////////////////////////////// //! Effects: //! \code //! for (; f != l; ++r, ++f) //! allocator_traits::construct(a, &*r, boost::move(*f)); //! \endcode //! //! Returns: r template // F models ForwardIterator inline typename container_detail::disable_if_memtransfer_copy_constructible::type uninitialized_move_alloc(A &a, I f, I l, F r) { F back = r; BOOST_TRY{ while (f != l) { allocator_traits::construct(a, container_detail::iterator_to_raw_pointer(r), boost::move(*f)); ++f; ++r; } } BOOST_CATCH(...){ for (; back != r; ++back){ allocator_traits::destroy(a, container_detail::iterator_to_raw_pointer(back)); } BOOST_RETHROW; } BOOST_CATCH_END return r; } template // F models ForwardIterator inline typename container_detail::enable_if_memtransfer_copy_constructible::type uninitialized_move_alloc(A &, I f, I l, F r) BOOST_CONTAINER_NOEXCEPT { return container_detail::memmove(f, l, r); } ////////////////////////////////////////////////////////////////////////////// // // uninitialized_move_alloc_n // ////////////////////////////////////////////////////////////////////////////// //! Effects: //! \code //! for (; n--; ++r, ++f) //! allocator_traits::construct(a, &*r, boost::move(*f)); //! \endcode //! //! Returns: r template // F models ForwardIterator inline typename container_detail::disable_if_memtransfer_copy_constructible::type uninitialized_move_alloc_n(A &a, I f, typename std::iterator_traits::difference_type n, F r) { F back = r; BOOST_TRY{ while (n--) { allocator_traits::construct(a, container_detail::iterator_to_raw_pointer(r), boost::move(*f)); ++f; ++r; } } BOOST_CATCH(...){ for (; back != r; ++back){ allocator_traits::destroy(a, container_detail::iterator_to_raw_pointer(back)); } BOOST_RETHROW; } BOOST_CATCH_END return r; } template // F models ForwardIterator inline typename container_detail::enable_if_memtransfer_copy_constructible::type uninitialized_move_alloc_n(A &, I f, typename std::iterator_traits::difference_type n, F r) BOOST_CONTAINER_NOEXCEPT { return container_detail::memmove_n(f, n, r); } ////////////////////////////////////////////////////////////////////////////// // // uninitialized_move_alloc_n_source // ////////////////////////////////////////////////////////////////////////////// //! Effects: //! \code //! for (; n--; ++r, ++f) //! allocator_traits::construct(a, &*r, boost::move(*f)); //! \endcode //! //! Returns: f (after incremented) template // F models ForwardIterator inline typename container_detail::disable_if_memtransfer_copy_constructible::type uninitialized_move_alloc_n_source(A &a, I f, typename std::iterator_traits::difference_type n, F r) { F back = r; BOOST_TRY{ while (n--) { allocator_traits::construct(a, container_detail::iterator_to_raw_pointer(r), boost::move(*f)); ++f; ++r; } } BOOST_CATCH(...){ for (; back != r; ++back){ allocator_traits::destroy(a, container_detail::iterator_to_raw_pointer(back)); } BOOST_RETHROW; } BOOST_CATCH_END return f; } template // F models ForwardIterator inline typename container_detail::enable_if_memtransfer_copy_constructible::type uninitialized_move_alloc_n_source(A &, I f, typename std::iterator_traits::difference_type n, F r) BOOST_CONTAINER_NOEXCEPT { return container_detail::memmove_n_source(f, n, r); } ////////////////////////////////////////////////////////////////////////////// // // uninitialized_copy_alloc // ////////////////////////////////////////////////////////////////////////////// //! Effects: //! \code //! for (; f != l; ++r, ++f) //! allocator_traits::construct(a, &*r, *f); //! \endcode //! //! Returns: r template // F models ForwardIterator inline typename container_detail::disable_if_memtransfer_copy_constructible::type uninitialized_copy_alloc(A &a, I f, I l, F r) { F back = r; BOOST_TRY{ while (f != l) { allocator_traits::construct(a, container_detail::iterator_to_raw_pointer(r), *f); ++f; ++r; } } BOOST_CATCH(...){ for (; back != r; ++back){ allocator_traits::destroy(a, container_detail::iterator_to_raw_pointer(back)); } BOOST_RETHROW; } BOOST_CATCH_END return r; } template // F models ForwardIterator inline typename container_detail::enable_if_memtransfer_copy_constructible::type uninitialized_copy_alloc(A &, I f, I l, F r) BOOST_CONTAINER_NOEXCEPT { return container_detail::memmove(f, l, r); } ////////////////////////////////////////////////////////////////////////////// // // uninitialized_copy_alloc_n // ////////////////////////////////////////////////////////////////////////////// //! Effects: //! \code //! for (; n--; ++r, ++f) //! allocator_traits::construct(a, &*r, *f); //! \endcode //! //! Returns: r template // F models ForwardIterator inline typename container_detail::disable_if_memtransfer_copy_constructible::type uninitialized_copy_alloc_n(A &a, I f, typename std::iterator_traits::difference_type n, F r) { F back = r; BOOST_TRY{ while (n--) { allocator_traits::construct(a, container_detail::iterator_to_raw_pointer(r), *f); ++f; ++r; } } BOOST_CATCH(...){ for (; back != r; ++back){ allocator_traits::destroy(a, container_detail::iterator_to_raw_pointer(back)); } BOOST_RETHROW; } BOOST_CATCH_END return r; } template // F models ForwardIterator inline typename container_detail::enable_if_memtransfer_copy_constructible::type uninitialized_copy_alloc_n(A &, I f, typename std::iterator_traits::difference_type n, F r) BOOST_CONTAINER_NOEXCEPT { return container_detail::memmove_n(f, n, r); } ////////////////////////////////////////////////////////////////////////////// // // uninitialized_copy_alloc_n_source // ////////////////////////////////////////////////////////////////////////////// //! Effects: //! \code //! for (; n--; ++r, ++f) //! allocator_traits::construct(a, &*r, *f); //! \endcode //! //! Returns: f (after incremented) template // F models ForwardIterator inline typename container_detail::disable_if_memtransfer_copy_constructible::type uninitialized_copy_alloc_n_source(A &a, I f, typename std::iterator_traits::difference_type n, F r) { F back = r; BOOST_TRY{ while (n--) { allocator_traits::construct(a, container_detail::iterator_to_raw_pointer(r), *f); ++f; ++r; } } BOOST_CATCH(...){ for (; back != r; ++back){ allocator_traits::destroy(a, container_detail::iterator_to_raw_pointer(back)); } BOOST_RETHROW; } BOOST_CATCH_END return f; } template // F models ForwardIterator inline typename container_detail::enable_if_memtransfer_copy_constructible::type uninitialized_copy_alloc_n_source(A &, I f, typename std::iterator_traits::difference_type n, F r) BOOST_CONTAINER_NOEXCEPT { return container_detail::memmove_n_source(f, n, r); } ////////////////////////////////////////////////////////////////////////////// // // uninitialized_value_init_alloc_n // ////////////////////////////////////////////////////////////////////////////// //! Effects: //! \code //! for (; n--; ++r, ++f) //! allocator_traits::construct(a, &*r); //! \endcode //! //! Returns: r template // F models ForwardIterator inline typename container_detail::disable_if_memzero_initializable::type uninitialized_value_init_alloc_n(A &a, typename allocator_traits::difference_type n, F r) { F back = r; BOOST_TRY{ while (n--) { allocator_traits::construct(a, container_detail::iterator_to_raw_pointer(r)); ++r; } } BOOST_CATCH(...){ for (; back != r; ++back){ allocator_traits::destroy(a, container_detail::iterator_to_raw_pointer(back)); } BOOST_RETHROW; } BOOST_CATCH_END return r; } template // F models ForwardIterator inline typename container_detail::enable_if_memzero_initializable::type uninitialized_value_init_alloc_n(A &, typename allocator_traits::difference_type n, F r) { typedef typename std::iterator_traits::value_type value_type; ::memset((void*)container_detail::iterator_to_raw_pointer(r), 0, sizeof(value_type)*n); std::advance(r, n); return r; } ////////////////////////////////////////////////////////////////////////////// // // uninitialized_default_init_alloc_n // ////////////////////////////////////////////////////////////////////////////// //! Effects: //! \code //! for (; n--; ++r, ++f) //! allocator_traits::construct(a, &*r); //! \endcode //! //! Returns: r template // F models ForwardIterator inline F uninitialized_default_init_alloc_n(A &a, typename allocator_traits::difference_type n, F r) { F back = r; BOOST_TRY{ while (n--) { allocator_traits::construct(a, container_detail::iterator_to_raw_pointer(r), default_init); ++r; } } BOOST_CATCH(...){ for (; back != r; ++back){ allocator_traits::destroy(a, container_detail::iterator_to_raw_pointer(back)); } BOOST_RETHROW; } BOOST_CATCH_END return r; } ////////////////////////////////////////////////////////////////////////////// // // uninitialized_fill_alloc // ////////////////////////////////////////////////////////////////////////////// //! Effects: //! \code //! for (; f != l; ++r, ++f) //! allocator_traits::construct(a, &*r, *f); //! \endcode //! //! Returns: r template inline void uninitialized_fill_alloc(A &a, F f, F l, const T &t) { F back = f; BOOST_TRY{ while (f != l) { allocator_traits::construct(a, container_detail::iterator_to_raw_pointer(f), t); ++f; } } BOOST_CATCH(...){ for (; back != l; ++back){ allocator_traits::destroy(a, container_detail::iterator_to_raw_pointer(back)); } BOOST_RETHROW; } BOOST_CATCH_END } ////////////////////////////////////////////////////////////////////////////// // // uninitialized_fill_alloc_n // ////////////////////////////////////////////////////////////////////////////// //! Effects: //! \code //! for (; n--; ++r, ++f) //! allocator_traits::construct(a, &*r, v); //! \endcode //! //! Returns: r template // F models ForwardIterator inline F uninitialized_fill_alloc_n(A &a, const T &v, typename allocator_traits::difference_type n, F r) { F back = r; BOOST_TRY{ while (n--) { allocator_traits::construct(a, container_detail::iterator_to_raw_pointer(r), v); ++r; } } BOOST_CATCH(...){ for (; back != r; ++back){ allocator_traits::destroy(a, container_detail::iterator_to_raw_pointer(back)); } BOOST_RETHROW; } BOOST_CATCH_END return r; } ////////////////////////////////////////////////////////////////////////////// // // copy // ////////////////////////////////////////////////////////////////////////////// template // F models ForwardIterator inline typename container_detail::disable_if_memtransfer_copy_assignable::type copy(I f, I l, F r) { while (f != l) { *r = *f; ++f; ++r; } return r; } template // F models ForwardIterator inline typename container_detail::enable_if_memtransfer_copy_assignable::type copy(I f, I l, F r) BOOST_CONTAINER_NOEXCEPT { return container_detail::memmove(f, l, r); } ////////////////////////////////////////////////////////////////////////////// // // copy_n // ////////////////////////////////////////////////////////////////////////////// template // F models ForwardIterator inline typename container_detail::disable_if_memtransfer_copy_assignable::type copy_n(I f, typename std::iterator_traits::difference_type n, F r) { while (n--) { *r = *f; ++f; ++r; } return r; } template // F models ForwardIterator inline typename container_detail::enable_if_memtransfer_copy_assignable::type copy_n(I f, typename std::iterator_traits::difference_type n, F r) BOOST_CONTAINER_NOEXCEPT { return container_detail::memmove_n(f, n, r); } ////////////////////////////////////////////////////////////////////////////// // // copy_n_source // ////////////////////////////////////////////////////////////////////////////// template // F models ForwardIterator inline typename container_detail::disable_if_memtransfer_copy_assignable::type copy_n_source(I f, typename std::iterator_traits::difference_type n, F r) { while (n--) { *r = *f; ++f; ++r; } return f; } template // F models ForwardIterator inline typename container_detail::enable_if_memtransfer_copy_assignable::type copy_n_source(I f, typename std::iterator_traits::difference_type n, F r) BOOST_CONTAINER_NOEXCEPT { return container_detail::memmove_n_source(f, n, r); } ////////////////////////////////////////////////////////////////////////////// // // copy_n_source_dest // ////////////////////////////////////////////////////////////////////////////// template // F models ForwardIterator inline typename container_detail::disable_if_memtransfer_copy_assignable::type copy_n_source_dest(I f, typename std::iterator_traits::difference_type n, F &r) { while (n--) { *r = *f; ++f; ++r; } return f; } template // F models ForwardIterator inline typename container_detail::enable_if_memtransfer_copy_assignable::type copy_n_source_dest(I f, typename std::iterator_traits::difference_type n, F &r) BOOST_CONTAINER_NOEXCEPT { return container_detail::memmove_n_source_dest(f, n, r); } ////////////////////////////////////////////////////////////////////////////// // // move // ////////////////////////////////////////////////////////////////////////////// template // F models ForwardIterator inline typename container_detail::disable_if_memtransfer_copy_assignable::type move(I f, I l, F r) { while (f != l) { *r = ::boost::move(*f); ++f; ++r; } return r; } template // F models ForwardIterator inline typename container_detail::enable_if_memtransfer_copy_assignable::type move(I f, I l, F r) BOOST_CONTAINER_NOEXCEPT { return container_detail::memmove(f, l, r); } ////////////////////////////////////////////////////////////////////////////// // // move_n // ////////////////////////////////////////////////////////////////////////////// template // F models ForwardIterator inline typename container_detail::disable_if_memtransfer_copy_assignable::type move_n(I f, typename std::iterator_traits::difference_type n, F r) { while (n--) { *r = ::boost::move(*f); ++f; ++r; } return r; } template // F models ForwardIterator inline typename container_detail::enable_if_memtransfer_copy_assignable::type move_n(I f, typename std::iterator_traits::difference_type n, F r) BOOST_CONTAINER_NOEXCEPT { return container_detail::memmove_n(f, n, r); } ////////////////////////////////////////////////////////////////////////////// // // move_n_source // ////////////////////////////////////////////////////////////////////////////// template // F models ForwardIterator inline typename container_detail::disable_if_memtransfer_copy_assignable::type move_n_source(I f, typename std::iterator_traits::difference_type n, F r) { while (n--) { *r = ::boost::move(*f); ++f; ++r; } return f; } template // F models ForwardIterator inline typename container_detail::enable_if_memtransfer_copy_assignable::type move_n_source(I f, typename std::iterator_traits::difference_type n, F r) BOOST_CONTAINER_NOEXCEPT { return container_detail::memmove_n_source(f, n, r); } ////////////////////////////////////////////////////////////////////////////// // // move_n_source_dest // ////////////////////////////////////////////////////////////////////////////// template // F models ForwardIterator inline typename container_detail::disable_if_memtransfer_copy_assignable::type move_n_source_dest(I f, typename std::iterator_traits::difference_type n, F &r) { while (n--) { *r = ::boost::move(*f); ++f; ++r; } return f; } template // F models ForwardIterator inline typename container_detail::enable_if_memtransfer_copy_assignable::type move_n_source_dest(I f, typename std::iterator_traits::difference_type n, F &r) BOOST_CONTAINER_NOEXCEPT { return container_detail::memmove_n_source_dest(f, n, r); } ////////////////////////////////////////////////////////////////////////////// // // destroy_n // ////////////////////////////////////////////////////////////////////////////// template // I models InputIterator inline void destroy_alloc_n(A &a, I f, typename std::iterator_traits::difference_type n ,typename boost::container::container_detail::enable_if_c < !boost::has_trivial_destructor::value_type>::value >::type* = 0) { while(n--){ allocator_traits::destroy(a, container_detail::iterator_to_raw_pointer(f++)); } } template // I models InputIterator inline void destroy_alloc_n(A &, I, typename std::iterator_traits::difference_type ,typename boost::container::container_detail::enable_if_c < boost::has_trivial_destructor::value_type>::value >::type* = 0) {} ////////////////////////////////////////////////////////////////////////////// // // deep_swap_alloc_n // ////////////////////////////////////////////////////////////////////////////// template inline typename container_detail::disable_if_memtransfer_copy_assignable::type deep_swap_alloc_n( A &a, F short_range_f, typename allocator_traits::size_type n_i , G large_range_f, typename allocator_traits::size_type n_j) { typename allocator_traits::size_type n = 0; for (; n != n_i ; ++short_range_f, ++large_range_f, ++n){ boost::container::swap_dispatch(*short_range_f, *large_range_f); } boost::container::uninitialized_move_alloc_n(a, large_range_f, n_j - n_i, short_range_f); // may throw boost::container::destroy_alloc_n(a, large_range_f, n_j - n_i); } static const std::size_t DeepSwapAllocNMaxStorage = std::size_t(1) << std::size_t(11); //2K bytes template inline typename container_detail::enable_if_c < container_detail::is_memtransfer_copy_assignable::value && (MaxTmpBytes <= DeepSwapAllocNMaxStorage) && false , void>::type deep_swap_alloc_n( A &a, F short_range_f, typename allocator_traits::size_type n_i , G large_range_f, typename allocator_traits::size_type n_j) { typedef typename allocator_traits::value_type value_type; typedef typename boost::aligned_storage ::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(container_detail::iterator_to_raw_pointer(large_range_f)); void *const short_ptr = static_cast(container_detail::iterator_to_raw_pointer(short_range_f)); void *const stora_ptr = static_cast(container_detail::iterator_to_raw_pointer(storage)); ::memcpy(stora_ptr, large_ptr, n_i_bytes); ::memcpy(large_ptr, short_ptr, n_i_bytes); ::memcpy(short_ptr, stora_ptr, n_i_bytes); std::advance(large_range_f, n_i); std::advance(short_range_f, n_i); boost::container::uninitialized_move_alloc_n(a, large_range_f, n_j - n_i, short_range_f); // may throw boost::container::destroy_alloc_n(a, large_range_f, n_j - n_i); } template inline typename container_detail::enable_if_c < container_detail::is_memtransfer_copy_assignable::value && true//(MaxTmpBytes > DeepSwapAllocNMaxStorage) , void>::type deep_swap_alloc_n( A &a, F short_range_f, typename allocator_traits::size_type n_i , G large_range_f, typename allocator_traits::size_type n_j) { typedef typename allocator_traits::value_type value_type; typedef typename boost::aligned_storage ::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(static_cast(container_detail::iterator_to_raw_pointer(large_range_f))); char *short_ptr = static_cast(static_cast(container_detail::iterator_to_raw_pointer(short_range_f))); char *stora_ptr = static_cast(static_cast(&storage)); std::size_t szt_times = n_i_bytes/sizeof_storage; const std::size_t szt_rem = n_i_bytes%sizeof_storage; //Loop unrolling using Duff's device, as it seems it helps on some architectures const std::size_t Unroll = 4; std::size_t n = (szt_times + (Unroll-1))/Unroll; const std::size_t branch_number = (!szt_times)*Unroll + (szt_times % Unroll); switch(branch_number){ case 4: break; case 0: do{ ::memcpy(stora_ptr, large_ptr, sizeof_storage); ::memcpy(large_ptr, short_ptr, sizeof_storage); ::memcpy(short_ptr, stora_ptr, sizeof_storage); large_ptr += sizeof_storage; short_ptr += sizeof_storage; BOOST_CONTAINER_FALLTHOUGH case 3: ::memcpy(stora_ptr, large_ptr, sizeof_storage); ::memcpy(large_ptr, short_ptr, sizeof_storage); ::memcpy(short_ptr, stora_ptr, sizeof_storage); large_ptr += sizeof_storage; short_ptr += sizeof_storage; BOOST_CONTAINER_FALLTHOUGH case 2: ::memcpy(stora_ptr, large_ptr, sizeof_storage); ::memcpy(large_ptr, short_ptr, sizeof_storage); ::memcpy(short_ptr, stora_ptr, sizeof_storage); large_ptr += sizeof_storage; short_ptr += sizeof_storage; BOOST_CONTAINER_FALLTHOUGH case 1: ::memcpy(stora_ptr, large_ptr, sizeof_storage); ::memcpy(large_ptr, short_ptr, sizeof_storage); ::memcpy(short_ptr, stora_ptr, sizeof_storage); large_ptr += sizeof_storage; short_ptr += sizeof_storage; } while(--n); } ::memcpy(stora_ptr, large_ptr, szt_rem); ::memcpy(large_ptr, short_ptr, szt_rem); ::memcpy(short_ptr, stora_ptr, szt_rem); std::advance(large_range_f, n_i); std::advance(short_range_f, n_i); boost::container::uninitialized_move_alloc_n(a, large_range_f, n_j - n_i, short_range_f); // may throw boost::container::destroy_alloc_n(a, large_range_f, n_j - n_i); } ////////////////////////////////////////////////////////////////////////////// // // copy_assign_range_alloc_n // ////////////////////////////////////////////////////////////////////////////// template void copy_assign_range_alloc_n( A &a, I inp_start, typename allocator_traits::size_type n_i , O out_start, typename allocator_traits::size_type n_o ) { if (n_o < n_i){ inp_start = boost::container::copy_n_source_dest(inp_start, n_o, out_start); // may throw boost::container::uninitialized_copy_alloc_n(a, inp_start, n_i - n_o, out_start);// may throw } else{ out_start = boost::container::copy_n(inp_start, n_i, out_start); // may throw boost::container::destroy_alloc_n(a, out_start, n_o - n_i); } } ////////////////////////////////////////////////////////////////////////////// // // move_assign_range_alloc_n // ////////////////////////////////////////////////////////////////////////////// template void move_assign_range_alloc_n( A &a, I inp_start, typename allocator_traits::size_type n_i , O out_start, typename allocator_traits::size_type n_o ) { if (n_o < n_i){ inp_start = boost::container::move_n_source_dest(inp_start, n_o, out_start); // may throw boost::container::uninitialized_move_alloc_n(a, inp_start, n_i - n_o, out_start); // may throw } else{ out_start = boost::container::move_n(inp_start, n_i, out_start); // may throw boost::container::destroy_alloc_n(a, out_start, n_o - n_i); } } } //namespace container { } //namespace boost { #include #endif //#ifndef BOOST_CONTAINER_DETAIL_UTILITIES_HPP