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

llvm-svn: 316021
This commit is contained in:
Eric Fiselier 2017-10-17 19:12:23 +00:00
parent 85af63d044
commit 0a412f4db0
1 changed files with 23 additions and 33 deletions

View File

@ -1071,6 +1071,16 @@ public:
bool __invariants() const; 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 #if _LIBCPP_DEBUG_LEVEL >= 2
bool __dereferenceable(const const_iterator* __i) const; bool __dereferenceable(const const_iterator* __i) const;
@ -1397,9 +1407,7 @@ list<_Tp, _Alloc>::insert(const_iterator __p, const value_type& __x)
" referring to this list"); " referring to this list");
#endif #endif
__node_allocator& __na = base::__node_alloc(); __node_allocator& __na = base::__node_alloc();
typedef __allocator_destructor<__node_allocator> _Dp; __hold_pointer __hold = __allocate_node(__na);
unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
__hold->__prev_ = 0;
__node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x); __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
__link_nodes(__p.__ptr_, __hold->__as_link(), __hold->__as_link()); __link_nodes(__p.__ptr_, __hold->__as_link(), __hold->__as_link());
++base::__sz(); ++base::__sz();
@ -1426,9 +1434,7 @@ list<_Tp, _Alloc>::insert(const_iterator __p, size_type __n, const value_type& _
{ {
size_type __ds = 0; size_type __ds = 0;
__node_allocator& __na = base::__node_alloc(); __node_allocator& __na = base::__node_alloc();
typedef __allocator_destructor<__node_allocator> _Dp; __hold_pointer __hold = __allocate_node(__na);
unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
__hold->__prev_ = 0;
__node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x); __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
++__ds; ++__ds;
#if _LIBCPP_DEBUG_LEVEL >= 2 #if _LIBCPP_DEBUG_LEVEL >= 2
@ -1494,9 +1500,7 @@ list<_Tp, _Alloc>::insert(const_iterator __p, _InpIter __f, _InpIter __l,
{ {
size_type __ds = 0; size_type __ds = 0;
__node_allocator& __na = base::__node_alloc(); __node_allocator& __na = base::__node_alloc();
typedef __allocator_destructor<__node_allocator> _Dp; __hold_pointer __hold = __allocate_node(__na);
unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
__hold->__prev_ = 0;
__node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f); __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
++__ds; ++__ds;
#if _LIBCPP_DEBUG_LEVEL >= 2 #if _LIBCPP_DEBUG_LEVEL >= 2
@ -1549,8 +1553,7 @@ void
list<_Tp, _Alloc>::push_front(const value_type& __x) list<_Tp, _Alloc>::push_front(const value_type& __x)
{ {
__node_allocator& __na = base::__node_alloc(); __node_allocator& __na = base::__node_alloc();
typedef __allocator_destructor<__node_allocator> _Dp; __hold_pointer __hold = __allocate_node(__na);
unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
__node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x); __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
__link_pointer __nl = __hold->__as_link(); __link_pointer __nl = __hold->__as_link();
__link_nodes_at_front(__nl, __nl); __link_nodes_at_front(__nl, __nl);
@ -1563,8 +1566,7 @@ void
list<_Tp, _Alloc>::push_back(const value_type& __x) list<_Tp, _Alloc>::push_back(const value_type& __x)
{ {
__node_allocator& __na = base::__node_alloc(); __node_allocator& __na = base::__node_alloc();
typedef __allocator_destructor<__node_allocator> _Dp; __hold_pointer __hold = __allocate_node(__na);
unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
__node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x); __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
__link_nodes_at_back(__hold.get()->__as_link(), __hold.get()->__as_link()); __link_nodes_at_back(__hold.get()->__as_link(), __hold.get()->__as_link());
++base::__sz(); ++base::__sz();
@ -1578,8 +1580,7 @@ void
list<_Tp, _Alloc>::push_front(value_type&& __x) list<_Tp, _Alloc>::push_front(value_type&& __x)
{ {
__node_allocator& __na = base::__node_alloc(); __node_allocator& __na = base::__node_alloc();
typedef __allocator_destructor<__node_allocator> _Dp; __hold_pointer __hold = __allocate_node(__na);
unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
__node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x)); __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
__link_nodes_at_front(__hold.get()->__as_link(), __hold.get()->__as_link()); __link_nodes_at_front(__hold.get()->__as_link(), __hold.get()->__as_link());
++base::__sz(); ++base::__sz();
@ -1591,8 +1592,7 @@ void
list<_Tp, _Alloc>::push_back(value_type&& __x) list<_Tp, _Alloc>::push_back(value_type&& __x)
{ {
__node_allocator& __na = base::__node_alloc(); __node_allocator& __na = base::__node_alloc();
typedef __allocator_destructor<__node_allocator> _Dp; __hold_pointer __hold = __allocate_node(__na);
unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
__node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x)); __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
__link_nodes_at_back(__hold.get()->__as_link(), __hold.get()->__as_link()); __link_nodes_at_back(__hold.get()->__as_link(), __hold.get()->__as_link());
++base::__sz(); ++base::__sz();
@ -1609,8 +1609,7 @@ void
list<_Tp, _Alloc>::emplace_front(_Args&&... __args) list<_Tp, _Alloc>::emplace_front(_Args&&... __args)
{ {
__node_allocator& __na = base::__node_alloc(); __node_allocator& __na = base::__node_alloc();
typedef __allocator_destructor<__node_allocator> _Dp; __hold_pointer __hold = __allocate_node(__na);
unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
__node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...); __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()); __link_nodes_at_front(__hold.get()->__as_link(), __hold.get()->__as_link());
++base::__sz(); ++base::__sz();
@ -1631,8 +1630,7 @@ void
list<_Tp, _Alloc>::emplace_back(_Args&&... __args) list<_Tp, _Alloc>::emplace_back(_Args&&... __args)
{ {
__node_allocator& __na = base::__node_alloc(); __node_allocator& __na = base::__node_alloc();
typedef __allocator_destructor<__node_allocator> _Dp; __hold_pointer __hold = __allocate_node(__na);
unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
__node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...); __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
__link_pointer __nl = __hold->__as_link(); __link_pointer __nl = __hold->__as_link();
__link_nodes_at_back(__nl, __nl); __link_nodes_at_back(__nl, __nl);
@ -1655,9 +1653,7 @@ list<_Tp, _Alloc>::emplace(const_iterator __p, _Args&&... __args)
" referring to this list"); " referring to this list");
#endif #endif
__node_allocator& __na = base::__node_alloc(); __node_allocator& __na = base::__node_alloc();
typedef __allocator_destructor<__node_allocator> _Dp; __hold_pointer __hold = __allocate_node(__na);
unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
__hold->__prev_ = 0;
__node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...); __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
__link_pointer __nl = __hold.get()->__as_link(); __link_pointer __nl = __hold.get()->__as_link();
__link_nodes(__p.__ptr_, __nl, __nl); __link_nodes(__p.__ptr_, __nl, __nl);
@ -1680,9 +1676,7 @@ list<_Tp, _Alloc>::insert(const_iterator __p, value_type&& __x)
" referring to this list"); " referring to this list");
#endif #endif
__node_allocator& __na = base::__node_alloc(); __node_allocator& __na = base::__node_alloc();
typedef __allocator_destructor<__node_allocator> _Dp; __hold_pointer __hold = __allocate_node(__na);
unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
__hold->__prev_ = 0;
__node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x)); __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
__link_pointer __nl = __hold->__as_link(); __link_pointer __nl = __hold->__as_link();
__link_nodes(__p.__ptr_, __nl, __nl); __link_nodes(__p.__ptr_, __nl, __nl);
@ -1855,9 +1849,7 @@ list<_Tp, _Alloc>::resize(size_type __n)
__n -= base::__sz(); __n -= base::__sz();
size_type __ds = 0; size_type __ds = 0;
__node_allocator& __na = base::__node_alloc(); __node_allocator& __na = base::__node_alloc();
typedef __allocator_destructor<__node_allocator> _Dp; __hold_pointer __hold = __allocate_node(__na);
unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
__hold->__prev_ = 0;
__node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_)); __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
++__ds; ++__ds;
#if _LIBCPP_DEBUG_LEVEL >= 2 #if _LIBCPP_DEBUG_LEVEL >= 2
@ -1914,9 +1906,7 @@ list<_Tp, _Alloc>::resize(size_type __n, const value_type& __x)
__n -= base::__sz(); __n -= base::__sz();
size_type __ds = 0; size_type __ds = 0;
__node_allocator& __na = base::__node_alloc(); __node_allocator& __na = base::__node_alloc();
typedef __allocator_destructor<__node_allocator> _Dp; __hold_pointer __hold = __allocate_node(__na);
unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
__hold->__prev_ = 0;
__node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x); __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
++__ds; ++__ds;
__link_pointer __nl = __hold.release()->__as_link(); __link_pointer __nl = __hold.release()->__as_link();