Skip to content

Commit d9a4f93

Browse files
committed
[libc++] Move <memory> helpers outside of std::allocator_traits
They don't really belong as members of allocator_traits.
1 parent 4b84682 commit d9a4f93

File tree

2 files changed

+91
-118
lines changed

2 files changed

+91
-118
lines changed

libcxx/include/memory

Lines changed: 87 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,118 +1458,7 @@ struct _LIBCPP_TEMPLATE_VIS allocator_traits
14581458
__has_select_on_container_copy_construction<const allocator_type>(),
14591459
__a);}
14601460

1461-
template <class _Ptr>
1462-
_LIBCPP_INLINE_VISIBILITY
1463-
static
1464-
void
1465-
__construct_forward_with_exception_guarantees(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
1466-
{
1467-
static_assert(__is_cpp17_move_insertable<allocator_type>::value,
1468-
"The specified type does not meet the requirements of Cpp17MoveInsertible");
1469-
for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
1470-
construct(__a, _VSTD::__to_address(__begin2),
1471-
#ifdef _LIBCPP_NO_EXCEPTIONS
1472-
_VSTD::move(*__begin1)
1473-
#else
1474-
_VSTD::move_if_noexcept(*__begin1)
1475-
#endif
1476-
);
1477-
}
1478-
1479-
template <class _Tp>
1480-
_LIBCPP_INLINE_VISIBILITY
1481-
static
1482-
typename enable_if
1483-
<
1484-
(__is_default_allocator<allocator_type>::value
1485-
|| !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1486-
is_trivially_move_constructible<_Tp>::value,
1487-
void
1488-
>::type
1489-
__construct_forward_with_exception_guarantees(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
1490-
{
1491-
ptrdiff_t _Np = __end1 - __begin1;
1492-
if (_Np > 0)
1493-
{
1494-
_VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
1495-
__begin2 += _Np;
1496-
}
1497-
}
1498-
1499-
template <class _Iter, class _Ptr>
1500-
_LIBCPP_INLINE_VISIBILITY
1501-
static
1502-
void
1503-
__construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
1504-
{
1505-
for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
1506-
construct(__a, _VSTD::__to_address(__begin2), *__begin1);
1507-
}
1508-
1509-
template <class _SourceTp, class _DestTp,
1510-
class _RawSourceTp = typename remove_const<_SourceTp>::type,
1511-
class _RawDestTp = typename remove_const<_DestTp>::type>
1512-
_LIBCPP_INLINE_VISIBILITY
1513-
static
1514-
typename enable_if
1515-
<
1516-
is_trivially_copy_constructible<_DestTp>::value &&
1517-
is_same<_RawSourceTp, _RawDestTp>::value &&
1518-
(__is_default_allocator<allocator_type>::value ||
1519-
!__has_construct<allocator_type, _DestTp*, _SourceTp&>::value),
1520-
void
1521-
>::type
1522-
__construct_range_forward(allocator_type&, _SourceTp* __begin1, _SourceTp* __end1, _DestTp*& __begin2)
1523-
{
1524-
ptrdiff_t _Np = __end1 - __begin1;
1525-
if (_Np > 0)
1526-
{
1527-
_VSTD::memcpy(const_cast<_RawDestTp*>(__begin2), __begin1, _Np * sizeof(_DestTp));
1528-
__begin2 += _Np;
1529-
}
1530-
}
1531-
1532-
template <class _Ptr>
1533-
_LIBCPP_INLINE_VISIBILITY
1534-
static
1535-
void
1536-
__construct_backward_with_exception_guarantees(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
1537-
{
1538-
static_assert(__is_cpp17_move_insertable<allocator_type>::value,
1539-
"The specified type does not meet the requirements of Cpp17MoveInsertable");
1540-
while (__end1 != __begin1)
1541-
{
1542-
construct(__a, _VSTD::__to_address(__end2 - 1),
1543-
#ifdef _LIBCPP_NO_EXCEPTIONS
1544-
_VSTD::move(*--__end1)
1545-
#else
1546-
_VSTD::move_if_noexcept(*--__end1)
1547-
#endif
1548-
);
1549-
--__end2;
1550-
}
1551-
}
1552-
1553-
template <class _Tp>
1554-
_LIBCPP_INLINE_VISIBILITY
1555-
static
1556-
typename enable_if
1557-
<
1558-
(__is_default_allocator<allocator_type>::value
1559-
|| !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1560-
is_trivially_move_constructible<_Tp>::value,
1561-
void
1562-
>::type
1563-
__construct_backward_with_exception_guarantees(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
1564-
{
1565-
ptrdiff_t _Np = __end1 - __begin1;
1566-
__end2 -= _Np;
1567-
if (_Np > 0)
1568-
_VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
1569-
}
1570-
15711461
private:
1572-
15731462
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
15741463
static pointer __allocate(allocator_type& __a, size_type __n,
15751464
const_void_pointer __hint, true_type)
@@ -1822,6 +1711,93 @@ template <class _Tp, class _Up>
18221711
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
18231712
bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
18241713

1714+
template <class _Alloc, class _Ptr>
1715+
_LIBCPP_INLINE_VISIBILITY
1716+
void __construct_forward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2) {
1717+
static_assert(__is_cpp17_move_insertable<_Alloc>::value,
1718+
"The specified type does not meet the requirements of Cpp17MoveInsertible");
1719+
typedef allocator_traits<_Alloc> _Traits;
1720+
for (; __begin1 != __end1; ++__begin1, (void)++__begin2) {
1721+
_Traits::construct(__a, _VSTD::__to_address(__begin2),
1722+
#ifdef _LIBCPP_NO_EXCEPTIONS
1723+
_VSTD::move(*__begin1)
1724+
#else
1725+
_VSTD::move_if_noexcept(*__begin1)
1726+
#endif
1727+
);
1728+
}
1729+
}
1730+
1731+
template <class _Alloc, class _Tp, typename enable_if<
1732+
(__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
1733+
is_trivially_move_constructible<_Tp>::value
1734+
>::type>
1735+
_LIBCPP_INLINE_VISIBILITY
1736+
void __construct_forward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2) {
1737+
ptrdiff_t _Np = __end1 - __begin1;
1738+
if (_Np > 0) {
1739+
_VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
1740+
__begin2 += _Np;
1741+
}
1742+
}
1743+
1744+
template <class _Alloc, class _Iter, class _Ptr>
1745+
_LIBCPP_INLINE_VISIBILITY
1746+
void __construct_range_forward(_Alloc& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2) {
1747+
typedef allocator_traits<_Alloc> _Traits;
1748+
for (; __begin1 != __end1; ++__begin1, (void) ++__begin2) {
1749+
_Traits::construct(__a, _VSTD::__to_address(__begin2), *__begin1);
1750+
}
1751+
}
1752+
1753+
template <class _Alloc, class _Source, class _Dest,
1754+
class _RawSource = typename remove_const<_Source>::type,
1755+
class _RawDest = typename remove_const<_Dest>::type,
1756+
class =
1757+
typename enable_if<
1758+
is_trivially_copy_constructible<_Dest>::value &&
1759+
is_same<_RawSource, _RawDest>::value &&
1760+
(__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Dest*, _Source&>::value)
1761+
>::type>
1762+
_LIBCPP_INLINE_VISIBILITY
1763+
void __construct_range_forward(_Alloc&, _Source* __begin1, _Source* __end1, _Dest*& __begin2) {
1764+
ptrdiff_t _Np = __end1 - __begin1;
1765+
if (_Np > 0) {
1766+
_VSTD::memcpy(const_cast<_RawDest*>(__begin2), __begin1, _Np * sizeof(_Dest));
1767+
__begin2 += _Np;
1768+
}
1769+
}
1770+
1771+
template <class _Alloc, class _Ptr>
1772+
_LIBCPP_INLINE_VISIBILITY
1773+
void __construct_backward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2) {
1774+
static_assert(__is_cpp17_move_insertable<_Alloc>::value,
1775+
"The specified type does not meet the requirements of Cpp17MoveInsertable");
1776+
typedef allocator_traits<_Alloc> _Traits;
1777+
while (__end1 != __begin1) {
1778+
_Traits::construct(__a, _VSTD::__to_address(__end2 - 1),
1779+
#ifdef _LIBCPP_NO_EXCEPTIONS
1780+
_VSTD::move(*--__end1)
1781+
#else
1782+
_VSTD::move_if_noexcept(*--__end1)
1783+
#endif
1784+
);
1785+
--__end2;
1786+
}
1787+
}
1788+
1789+
template <class _Alloc, class _Tp, class = typename enable_if<
1790+
(__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
1791+
is_trivially_move_constructible<_Tp>::value
1792+
>::type>
1793+
_LIBCPP_INLINE_VISIBILITY
1794+
void __construct_backward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2) {
1795+
ptrdiff_t _Np = __end1 - __begin1;
1796+
__end2 -= _Np;
1797+
if (_Np > 0)
1798+
_VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
1799+
}
1800+
18251801
template <class _OutputIterator, class _Tp>
18261802
class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
18271803
: public iterator<output_iterator_tag,

libcxx/include/vector

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -951,8 +951,7 @@ vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, a
951951
{
952952

953953
__annotate_delete();
954-
__alloc_traits::__construct_backward_with_exception_guarantees(
955-
this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
954+
__construct_backward_with_exception_guarantees(this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
956955
_VSTD::swap(this->__begin_, __v.__begin_);
957956
_VSTD::swap(this->__end_, __v.__end_);
958957
_VSTD::swap(this->__end_cap(), __v.__end_cap());
@@ -967,10 +966,8 @@ vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, a
967966
{
968967
__annotate_delete();
969968
pointer __r = __v.__begin_;
970-
__alloc_traits::__construct_backward_with_exception_guarantees(
971-
this->__alloc(), this->__begin_, __p, __v.__begin_);
972-
__alloc_traits::__construct_forward_with_exception_guarantees(
973-
this->__alloc(), __p, this->__end_, __v.__end_);
969+
__construct_backward_with_exception_guarantees(this->__alloc(), this->__begin_, __p, __v.__begin_);
970+
__construct_forward_with_exception_guarantees(this->__alloc(), __p, this->__end_, __v.__end_);
974971
_VSTD::swap(this->__begin_, __v.__begin_);
975972
_VSTD::swap(this->__end_, __v.__end_);
976973
_VSTD::swap(this->__end_cap(), __v.__end_cap());
@@ -1077,7 +1074,7 @@ typename enable_if
10771074
vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n)
10781075
{
10791076
_ConstructTransaction __tx(*this, __n);
1080-
__alloc_traits::__construct_range_forward(this->__alloc(), __first, __last, __tx.__pos_);
1077+
__construct_range_forward(this->__alloc(), __first, __last, __tx.__pos_);
10811078
}
10821079

10831080
// Default constructs __n objects starting at __end_

0 commit comments

Comments
 (0)