Refactor std::list node allocation logic.
The logic to allocate a node within std::list was repeated
in a bunch of places. This is unneeded. This patch refactors
the shared logic into a single function to reduce duplication.
This patch is part of a set to clean up node construction in
general, but refactoring construction requires some more work
to make it work cleanly in C++03
git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@316021 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/list b/include/list
index 9c70fff..9561a5f 100644
--- a/include/list
+++ b/include/list
@@ -1071,6 +1071,16 @@
bool __invariants() const;
+ typedef __allocator_destructor<__node_allocator> __node_destructor;
+ typedef unique_ptr<__node, __node_destructor> __hold_pointer;
+
+ _LIBCPP_INLINE_VISIBILITY
+ __hold_pointer __allocate_node(__node_allocator& __na) {
+ __node_pointer __p = __node_alloc_traits::allocate(__na, 1);
+ __p->__prev_ = nullptr;
+ return __hold_pointer(__p, __node_destructor(__na, 1));
+ }
+
#if _LIBCPP_DEBUG_LEVEL >= 2
bool __dereferenceable(const const_iterator* __i) const;
@@ -1397,9 +1407,7 @@
" referring to this list");
#endif
__node_allocator& __na = base::__node_alloc();
- typedef __allocator_destructor<__node_allocator> _Dp;
- unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
- __hold->__prev_ = 0;
+ __hold_pointer __hold = __allocate_node(__na);
__node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
__link_nodes(__p.__ptr_, __hold->__as_link(), __hold->__as_link());
++base::__sz();
@@ -1426,9 +1434,7 @@
{
size_type __ds = 0;
__node_allocator& __na = base::__node_alloc();
- typedef __allocator_destructor<__node_allocator> _Dp;
- unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
- __hold->__prev_ = 0;
+ __hold_pointer __hold = __allocate_node(__na);
__node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
++__ds;
#if _LIBCPP_DEBUG_LEVEL >= 2
@@ -1494,9 +1500,7 @@
{
size_type __ds = 0;
__node_allocator& __na = base::__node_alloc();
- typedef __allocator_destructor<__node_allocator> _Dp;
- unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
- __hold->__prev_ = 0;
+ __hold_pointer __hold = __allocate_node(__na);
__node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
++__ds;
#if _LIBCPP_DEBUG_LEVEL >= 2
@@ -1549,8 +1553,7 @@
list<_Tp, _Alloc>::push_front(const value_type& __x)
{
__node_allocator& __na = base::__node_alloc();
- typedef __allocator_destructor<__node_allocator> _Dp;
- unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
+ __hold_pointer __hold = __allocate_node(__na);
__node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
__link_pointer __nl = __hold->__as_link();
__link_nodes_at_front(__nl, __nl);
@@ -1563,8 +1566,7 @@
list<_Tp, _Alloc>::push_back(const value_type& __x)
{
__node_allocator& __na = base::__node_alloc();
- typedef __allocator_destructor<__node_allocator> _Dp;
- unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
+ __hold_pointer __hold = __allocate_node(__na);
__node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
__link_nodes_at_back(__hold.get()->__as_link(), __hold.get()->__as_link());
++base::__sz();
@@ -1578,8 +1580,7 @@
list<_Tp, _Alloc>::push_front(value_type&& __x)
{
__node_allocator& __na = base::__node_alloc();
- typedef __allocator_destructor<__node_allocator> _Dp;
- unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
+ __hold_pointer __hold = __allocate_node(__na);
__node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
__link_nodes_at_front(__hold.get()->__as_link(), __hold.get()->__as_link());
++base::__sz();
@@ -1591,8 +1592,7 @@
list<_Tp, _Alloc>::push_back(value_type&& __x)
{
__node_allocator& __na = base::__node_alloc();
- typedef __allocator_destructor<__node_allocator> _Dp;
- unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
+ __hold_pointer __hold = __allocate_node(__na);
__node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
__link_nodes_at_back(__hold.get()->__as_link(), __hold.get()->__as_link());
++base::__sz();
@@ -1609,8 +1609,7 @@
list<_Tp, _Alloc>::emplace_front(_Args&&... __args)
{
__node_allocator& __na = base::__node_alloc();
- typedef __allocator_destructor<__node_allocator> _Dp;
- unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
+ __hold_pointer __hold = __allocate_node(__na);
__node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
__link_nodes_at_front(__hold.get()->__as_link(), __hold.get()->__as_link());
++base::__sz();
@@ -1631,8 +1630,7 @@
list<_Tp, _Alloc>::emplace_back(_Args&&... __args)
{
__node_allocator& __na = base::__node_alloc();
- typedef __allocator_destructor<__node_allocator> _Dp;
- unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
+ __hold_pointer __hold = __allocate_node(__na);
__node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
__link_pointer __nl = __hold->__as_link();
__link_nodes_at_back(__nl, __nl);
@@ -1655,9 +1653,7 @@
" referring to this list");
#endif
__node_allocator& __na = base::__node_alloc();
- typedef __allocator_destructor<__node_allocator> _Dp;
- unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
- __hold->__prev_ = 0;
+ __hold_pointer __hold = __allocate_node(__na);
__node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
__link_pointer __nl = __hold.get()->__as_link();
__link_nodes(__p.__ptr_, __nl, __nl);
@@ -1680,9 +1676,7 @@
" referring to this list");
#endif
__node_allocator& __na = base::__node_alloc();
- typedef __allocator_destructor<__node_allocator> _Dp;
- unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
- __hold->__prev_ = 0;
+ __hold_pointer __hold = __allocate_node(__na);
__node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
__link_pointer __nl = __hold->__as_link();
__link_nodes(__p.__ptr_, __nl, __nl);
@@ -1855,9 +1849,7 @@
__n -= base::__sz();
size_type __ds = 0;
__node_allocator& __na = base::__node_alloc();
- typedef __allocator_destructor<__node_allocator> _Dp;
- unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
- __hold->__prev_ = 0;
+ __hold_pointer __hold = __allocate_node(__na);
__node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
++__ds;
#if _LIBCPP_DEBUG_LEVEL >= 2
@@ -1914,9 +1906,7 @@
__n -= base::__sz();
size_type __ds = 0;
__node_allocator& __na = base::__node_alloc();
- typedef __allocator_destructor<__node_allocator> _Dp;
- unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
- __hold->__prev_ = 0;
+ __hold_pointer __hold = __allocate_node(__na);
__node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
++__ds;
__link_pointer __nl = __hold.release()->__as_link();