Commit cc7f3d0e by Jonathan Wakely Committed by Jonathan Wakely

C++11 allocator support for std::list.

	PR libstdc++/55409
	* include/bits/list.tcc (_List_base::_M_clear()): Use allocator traits.
	(list::list(const list&)): Use allocator propagation trait. Use
	_M_assign_dispatch to copy elements.
	* include/bits/stl_list.h (_List_node): Use __aligned_membuf in C++11.
	(_List_node::_M_valptr()): Add accessor for stored value.
	(_List_iterator, _List_const_iterator, _List_base): Use _M_valptr().
	(_List_base, list): Use allocator traits.
	(_List_base::_M_get_Tp_allocator, _List_base::get_allocator): Remove.
	(_List_base::_M_move_nodes): New function.
	(_List_base(_List_base&&)): Use _M_move_nodes.
	(_List_base(_List_base&&, _Node_alloc_type&&)): New constructor.
	(list::_M_create_node, list::_M_erase, list::max_size): Use allocator
	traits.
	(list(size_type)): Add allocator parameter.
	(list(const list&)): Use allocator propagation trait.
	(list(const list&, const allocator_type&)): New constructor.
	(list(list&&, const allocator_type&)): Likewise.
	(list::operator=(list&&), list::swap(list&)): Use allocator
	propagation traits.
	(list::_M_move_assign): New functions.
	* include/debug/list: Add allocator-extended constructors.
	* include/profile/list: Likewise.
	* python/libstdcxx/v6/printers.py (get_value_from_list_node): New
	function to get value from _List_node.
	(StdListPrinter): Use get_value_from_list_node.
	* testsuite/23_containers/list/allocator/copy.cc: New.
	* testsuite/23_containers/list/allocator/copy_assign.cc: New.
	* testsuite/23_containers/list/allocator/minimal.cc: New.
	* testsuite/23_containers/list/allocator/move.cc: New.
	* testsuite/23_containers/list/allocator/move_assign.cc: New.
	* testsuite/23_containers/list/allocator/noexcept.cc: New.
	* testsuite/23_containers/list/allocator/swap.cc: New.
	* testsuite/23_containers/list/requirements/dr438/assign_neg.cc:
	Adjust dg-prune-output line number.
	* testsuite/23_containers/list/requirements/dr438/constructor_1_neg.cc:
	Likewise.
	* testsuite/23_containers/list/requirements/dr438/insert_neg.cc:
	Likewise.

From-SVN: r224580
parent 917ad3c6
2015-06-17 Jonathan Wakely <jwakely@redhat.com>
PR libstdc++/55409
* include/bits/list.tcc (_List_base::_M_clear()): Use allocator traits.
(list::list(const list&)): Use allocator propagation trait. Use
_M_assign_dispatch to copy elements.
* include/bits/stl_list.h (_List_node): Use __aligned_membuf in C++11.
(_List_node::_M_valptr()): Add accessor for stored value.
(_List_iterator, _List_const_iterator, _List_base): Use _M_valptr().
(_List_base, list): Use allocator traits.
(_List_base::_M_get_Tp_allocator, _List_base::get_allocator): Remove.
(_List_base::_M_move_nodes): New function.
(_List_base(_List_base&&)): Use _M_move_nodes.
(_List_base(_List_base&&, _Node_alloc_type&&)): New constructor.
(list::_M_create_node, list::_M_erase, list::max_size): Use allocator
traits.
(list(size_type)): Add allocator parameter.
(list(const list&)): Use allocator propagation trait.
(list(const list&, const allocator_type&)): New constructor.
(list(list&&, const allocator_type&)): Likewise.
(list::operator=(list&&), list::swap(list&)): Use allocator
propagation traits.
(list::_M_move_assign): New functions.
* include/debug/list: Add allocator-extended constructors.
* include/profile/list: Likewise.
* python/libstdcxx/v6/printers.py (get_value_from_list_node): New
function to get value from _List_node.
(StdListPrinter): Use get_value_from_list_node.
* testsuite/23_containers/list/allocator/copy.cc: New.
* testsuite/23_containers/list/allocator/copy_assign.cc: New.
* testsuite/23_containers/list/allocator/minimal.cc: New.
* testsuite/23_containers/list/allocator/move.cc: New.
* testsuite/23_containers/list/allocator/move_assign.cc: New.
* testsuite/23_containers/list/allocator/noexcept.cc: New.
* testsuite/23_containers/list/allocator/swap.cc: New.
* testsuite/23_containers/list/requirements/dr438/assign_neg.cc:
Adjust dg-prune-output line number.
* testsuite/23_containers/list/requirements/dr438/constructor_1_neg.cc:
Likewise.
* testsuite/23_containers/list/requirements/dr438/insert_neg.cc:
Likewise.
* include/bits/forward_list.h
(_Fwd_list_base(const _Node_alloc_type&)): Change parameter to
rvalue-reference.
......
......@@ -71,10 +71,11 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
{
_Node* __tmp = static_cast<_Node*>(__cur);
__cur = __tmp->_M_next;
_Tp* __val = __tmp->_M_valptr();
#if __cplusplus >= 201103L
_M_get_Node_allocator().destroy(__tmp);
_Node_alloc_traits::destroy(_M_get_Node_allocator(), __val);
#else
_M_get_Tp_allocator().destroy(std::__addressof(__tmp->_M_data));
_Tp_alloc_type(_M_get_Node_allocator()).destroy(__val);
#endif
_M_put_node(__tmp);
}
......@@ -267,17 +268,21 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
{
if (this != std::__addressof(__x))
{
iterator __first1 = begin();
iterator __last1 = end();
const_iterator __first2 = __x.begin();
const_iterator __last2 = __x.end();
for (; __first1 != __last1 && __first2 != __last2;
++__first1, ++__first2)
*__first1 = *__first2;
if (__first2 == __last2)
erase(__first1, __last1);
else
insert(__last1, __first2, __last2);
#if __cplusplus >= 201103L
if (_Node_alloc_traits::_S_propagate_on_copy_assign())
{
auto& __this_alloc = this->_M_get_Node_allocator();
auto& __that_alloc = __x._M_get_Node_allocator();
if (!_Node_alloc_traits::_S_always_equal()
&& __this_alloc != __that_alloc)
{
// replacement allocator cannot free existing storage
clear();
}
std::__alloc_on_copy(__this_alloc, __that_alloc);
}
#endif
_M_assign_dispatch(__x.begin(), __x.end(), __false_type());
}
return *this;
}
......
......@@ -94,6 +94,12 @@ namespace __debug
: _Base(__l, __a) { }
~list() = default;
list(const list& __x, const allocator_type& __a)
: _Base(__x, __a) { }
list(list&& __x, const allocator_type& __a)
: _Base(std::move(__x), __a) { }
#endif
explicit
......@@ -102,8 +108,8 @@ namespace __debug
#if __cplusplus >= 201103L
explicit
list(size_type __n)
: _Base(__n) { }
list(size_type __n, const allocator_type& __a = allocator_type())
: _Base(__n, __a) { }
list(size_type __n, const _Tp& __value,
const _Allocator& __a = _Allocator())
......
......@@ -145,6 +145,12 @@ namespace __profile
list(initializer_list<value_type> __l,
const allocator_type& __a = allocator_type())
: _Base(__l, __a) { }
list(const list& __x, const allocator_type& __a)
: _Base(__x, __a) { }
list(list&& __x, const allocator_type& __a)
: _Base(std::move(__x), __a) { }
#endif
explicit
......@@ -153,8 +159,8 @@ namespace __profile
#if __cplusplus >= 201103L
explicit
list(size_type __n)
: _Base(__n) { }
list(size_type __n, const allocator_type& __a = allocator_type())
: _Base(__n, __a) { }
list(size_type __n, const _Tp& __value,
const _Allocator& __a = _Allocator())
......
......@@ -128,6 +128,22 @@ class UniquePointerPrinter:
return ('std::unique_ptr<%s> containing %s' % (str(v.type.target()),
str(v)))
def get_value_from_list_node(node):
"""Returns the value held in an _List_node<_Val>"""
try:
member = node.type.fields()[1].name
if member == '_M_data':
# C++03 implementation, node contains the value as a member
return node['_M_data']
elif member == '_M_storage':
# C++11 implementation, node stores value in __aligned_membuf
p = node['_M_storage']['_M_storage'].address
p = p.cast(node.type.template_argument(0).pointer())
return p.dereference()
except:
pass
raise ValueError("Unsupported implementation for %s" % str(node.type))
class StdListPrinter:
"Print a std::list"
......@@ -148,7 +164,8 @@ class StdListPrinter:
self.base = elt['_M_next']
count = self.count
self.count = self.count + 1
return ('[%d]' % count, elt['_M_data'])
val = get_value_from_list_node(elt)
return ('[%d]' % count, val)
def __init__(self, typename, val):
self.typename = typename
......@@ -174,7 +191,8 @@ class StdListIteratorPrinter:
def to_string(self):
nodetype = find_type(self.val.type, '_Node')
nodetype = nodetype.strip_typedefs().pointer()
return self.val['_M_node'].cast(nodetype).dereference()['_M_data']
node = self.val['_M_node'].cast(nodetype).dereference()
return get_value_from_list_node(node)
class StdSlistPrinter:
"Print a __gnu_cxx::slist"
......@@ -440,7 +458,7 @@ def get_value_from_Rb_tree_node(node):
# C++03 implementation, node contains the value as a member
return node['_M_value_field']
elif member == '_M_storage':
# C++11 implementation, node stores value in __aligned_buffer
# C++11 implementation, node stores value in __aligned_membuf
p = node['_M_storage']['_M_storage'].address
p = p.cast(node.type.template_argument(0).pointer())
return p.dereference()
......
// Copyright (C) 2015 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++11" }
#include <list>
#include <testsuite_hooks.h>
#include <testsuite_allocator.h>
struct T { int i; };
using __gnu_test::propagating_allocator;
void test01()
{
bool test __attribute__((unused)) = true;
typedef propagating_allocator<T, false> alloc_type;
typedef std::list<T, alloc_type> test_type;
test_type v1(alloc_type(1));
v1.push_front(T());
test_type v2(v1);
VERIFY(1 == v1.get_allocator().get_personality());
VERIFY(0 == v2.get_allocator().get_personality());
}
void test02()
{
bool test __attribute__((unused)) = true;
typedef propagating_allocator<T, true> alloc_type;
typedef std::list<T, alloc_type> test_type;
test_type v1(alloc_type(1));
v1.push_front(T());
test_type v2(v1);
VERIFY(1 == v1.get_allocator().get_personality());
VERIFY(1 == v2.get_allocator().get_personality());
}
void test03()
{
bool test __attribute__((unused)) = true;
typedef propagating_allocator<T, true> alloc_type;
typedef std::list<T, alloc_type> test_type;
test_type v1(alloc_type(1));
v1.push_front(T());
test_type v2(v1, alloc_type(2));
VERIFY(1 == v1.get_allocator().get_personality());
VERIFY(2 == v2.get_allocator().get_personality());
}
int main()
{
test01();
test02();
test03();
return 0;
}
// Copyright (C) 2015 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++11" }
#include <list>
#include <testsuite_hooks.h>
#include <testsuite_allocator.h>
struct T { int i; };
using __gnu_test::propagating_allocator;
void test01()
{
bool test __attribute__((unused)) = true;
typedef propagating_allocator<T, false> alloc_type;
typedef std::list<T, alloc_type> test_type;
test_type v1(alloc_type(1));
v1.push_front(T());
test_type v2(alloc_type(2));
v2.push_front(T());
v2 = v1;
VERIFY(1 == v1.get_allocator().get_personality());
VERIFY(2 == v2.get_allocator().get_personality());
}
void test02()
{
bool test __attribute__((unused)) = true;
typedef propagating_allocator<T, true> alloc_type;
typedef std::list<T, alloc_type> test_type;
test_type v1(alloc_type(1));
v1.push_front(T());
test_type v2(alloc_type(2));
v2.push_front(T());
v2 = v1;
VERIFY(1 == v1.get_allocator().get_personality());
VERIFY(1 == v2.get_allocator().get_personality());
}
int main()
{
test01();
test02();
return 0;
}
// Copyright (C) 2015 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++11" }
#include <list>
#include <memory>
#include <testsuite_hooks.h>
#include <testsuite_allocator.h>
struct T { int i; };
bool operator==(const T& l, const T& r) { return l.i == r.i; }
bool operator<(const T& l, const T& r) { return l.i < r.i; }
using __gnu_test::SimpleAllocator;
template class std::list<T, SimpleAllocator<T>>;
void test01()
{
bool test __attribute__((unused)) = true;
typedef SimpleAllocator<T> alloc_type;
typedef std::allocator_traits<alloc_type> traits_type;
typedef std::list<T, alloc_type> test_type;
test_type v(alloc_type{});
v.push_front(T());
VERIFY( v.max_size() < traits_type::max_size(v.get_allocator()) );
}
int main()
{
test01();
return 0;
}
// Copyright (C) 2015 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++11" }
#include <list>
#include <testsuite_hooks.h>
#include <testsuite_allocator.h>
struct T { int i; };
using __gnu_test::uneq_allocator;
void test01()
{
bool test __attribute__((unused)) = true;
typedef uneq_allocator<T> alloc_type;
typedef std::list<T, alloc_type> test_type;
test_type v1(alloc_type(1));
v1 = { T() };
auto it = v1.begin();
test_type v2(std::move(v1));
VERIFY(1 == v1.get_allocator().get_personality());
VERIFY(1 == v2.get_allocator().get_personality());
VERIFY( it == v2.begin() );
}
void test02()
{
bool test __attribute__((unused)) = true;
typedef uneq_allocator<T> alloc_type;
typedef std::list<T, alloc_type> test_type;
test_type v1(alloc_type(1));
v1 = { T() };
test_type v2(std::move(v1), alloc_type(2));
VERIFY(1 == v1.get_allocator().get_personality());
VERIFY(2 == v2.get_allocator().get_personality());
}
int main()
{
test01();
test02();
return 0;
}
// Copyright (C) 2015 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++11" }
#include <list>
#include <testsuite_hooks.h>
#include <testsuite_allocator.h>
struct T { int i; };
using __gnu_test::propagating_allocator;
void test01()
{
bool test __attribute__((unused)) = true;
typedef propagating_allocator<T, false> alloc_type;
typedef std::list<T, alloc_type> test_type;
test_type v1(alloc_type(1));
v1.push_front(T());
test_type v2(alloc_type(2));
v2.push_front(T());
v2 = std::move(v1);
VERIFY(1 == v1.get_allocator().get_personality());
VERIFY(2 == v2.get_allocator().get_personality());
}
void test02()
{
bool test __attribute__((unused)) = true;
typedef propagating_allocator<T, true> alloc_type;
typedef std::list<T, alloc_type> test_type;
test_type v1(alloc_type(1));
v1.push_front(T());
auto it = v1.begin();
test_type v2(alloc_type(2));
v2.push_front(T());
v2 = std::move(v1);
VERIFY(0 == v1.get_allocator().get_personality());
VERIFY(1 == v2.get_allocator().get_personality());
VERIFY( it == v2.begin() );
}
int main()
{
test01();
test02();
return 0;
}
// Copyright (C) 2015 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// { dg-do compile }
// { dg-options "-std=gnu++11" }
#include <list>
#include <testsuite_allocator.h>
struct T { int i; };
namespace __gnu_test
{
template<typename U>
inline void
swap(propagating_allocator<U, true>& l, propagating_allocator<U, true>& r)
noexcept(false)
{ }
}
using __gnu_test::propagating_allocator;
void test01()
{
typedef std::allocator<T> alloc_type;
typedef std::list<T, alloc_type> test_type;
test_type v1;
test_type v2;
// this is a GNU extension for std::allocator
static_assert( noexcept( v1 = std::move(v2) ), "Move assign cannot throw" );
static_assert( noexcept( v1.swap(v2) ), "Swap cannot throw" );
}
void test02()
{
typedef propagating_allocator<T, false> alloc_type;
typedef std::list<T, alloc_type> test_type;
test_type v1(alloc_type(1));
test_type v2(alloc_type(2));
static_assert( !noexcept( v1 = std::move(v2) ), "Move assign can throw" );
static_assert( noexcept( v1.swap(v2) ), "Swap cannot throw" );
}
void test03()
{
typedef propagating_allocator<T, true> alloc_type;
typedef std::list<T, alloc_type> test_type;
test_type v1(alloc_type(1));
test_type v2(alloc_type(2));
static_assert( noexcept( v1 = std::move(v2) ), "Move assign cannot throw" );
static_assert( !noexcept( v1.swap(v2) ), "Swap can throw" );
}
// Copyright (C) 2015 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++11" }
#include <list>
#include <testsuite_hooks.h>
#include <testsuite_allocator.h>
struct T { int i; };
using __gnu_test::propagating_allocator;
// It is undefined behaviour to swap() containers wth unequal allocators
// if the allocator doesn't propagate, so ensure the allocators compare
// equal, while still being able to test propagation via get_personality().
bool
operator==(const propagating_allocator<T, false>&,
const propagating_allocator<T, false>&)
{
return true;
}
bool
operator!=(const propagating_allocator<T, false>&,
const propagating_allocator<T, false>&)
{
return false;
}
void test01()
{
bool test __attribute__((unused)) = true;
typedef propagating_allocator<T, false> alloc_type;
typedef std::list<T, alloc_type> test_type;
test_type v1(alloc_type(1));
v1.push_front(T());
test_type v2(alloc_type(2));
v2.push_front(T());
std::swap(v1, v2);
VERIFY(1 == v1.get_allocator().get_personality());
VERIFY(2 == v2.get_allocator().get_personality());
// swap back so assertions in uneq_allocator::deallocate don't fail
std::swap(v1, v2);
}
void test02()
{
bool test __attribute__((unused)) = true;
typedef propagating_allocator<T, true> alloc_type;
typedef std::list<T, alloc_type> test_type;
test_type v1(alloc_type(1));
v1.push_front(T());
test_type v2(alloc_type(2));
v2.push_front(T());
std::swap(v1, v2);
VERIFY(2 == v1.get_allocator().get_personality());
VERIFY(1 == v2.get_allocator().get_personality());
}
int main()
{
test01();
test02();
return 0;
}
......@@ -18,7 +18,7 @@
// <http://www.gnu.org/licenses/>.
// { dg-do compile }
// { dg-prune-output 1730 }
// { dg-prune-output 1741 }
#include <list>
......
......@@ -18,7 +18,7 @@
// <http://www.gnu.org/licenses/>.
// { dg-do compile }
// { dg-prune-output 1682 }
// { dg-prune-output 1693 }
#include <list>
......
......@@ -18,7 +18,7 @@
// <http://www.gnu.org/licenses/>.
// { dg-do compile }
// { dg-prune-output 1682 }
// { dg-prune-output 1693 }
#include <list>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment