diff mbox

[v3] add max_size and rebind to __alloc_traits

Message ID CAH6eHdSRiC5VWo9ix0HTaOL93XEXnekE9QCqzmetXsXFoCxbUg@mail.gmail.com
State New
Headers show

Commit Message

Jonathan Wakely Oct. 4, 2011, 8:35 p.m. UTC
2011-10-04  Jonathan Wakely  <jwakely.gcc@gmail.com>

	* include/ext/alloc_traits.h (__alloc_traits::max_size): Define.
	(__alloc_traits::rebind): Define.
	* include/bits/stl_vector.h: Use them.
	* testsuite/util/testsuite_allocator.h (SimpleAllocator): Define.
	* testsuite/23_containers/vector/allocator/minimal.cc: New.
	* testsuite/23_containers/vector/requirements/dr438/assign_neg.cc:
	Adjust dg-error line numbers.
	* testsuite/23_containers/vector/requirements/dr438/insert_neg.cc:
	Likewise.
	* testsuite/23_containers/vector/requirements/dr438/
	constructor_1_neg.cc: Likewise.
	* testsuite/23_containers/vector/requirements/dr438/
	constructor_2_neg.cc: Likewise.

This adds some more members to the alloc_traits compatibility layer
and uses them in std::vector.  This allows std::vector to be used with
a minimal C++11 allocator, as shown in the example in
[allocator.requirements] (N.B. LWG 2065 will add the equality
operators.)  That minimal allocator has been added to the testsuite.

Tested x86_64-linux, committed to trunk.

Comments

Paolo Carlini Oct. 6, 2011, 1:57 a.m. UTC | #1
Hi,
> 2011-10-04  Jonathan Wakely<jwakely.gcc@gmail.com>
>
> 	* include/ext/alloc_traits.h (__alloc_traits::max_size): Define.
> 	(__alloc_traits::rebind): Define.
> 	* include/bits/stl_vector.h: Use them.
> 	* testsuite/util/testsuite_allocator.h (SimpleAllocator): Define.
> 	* testsuite/23_containers/vector/allocator/minimal.cc: New.
> 	* testsuite/23_containers/vector/requirements/dr438/assign_neg.cc:
> 	Adjust dg-error line numbers.
> 	* testsuite/23_containers/vector/requirements/dr438/insert_neg.cc:
> 	Likewise.
> 	* testsuite/23_containers/vector/requirements/dr438/
> 	constructor_1_neg.cc: Likewise.
> 	* testsuite/23_containers/vector/requirements/dr438/
> 	constructor_2_neg.cc: Likewise.
>
today I ran the whole testsuite in C++0x mode and I'm pretty sure that 
23_containers/vector/modifiers/swap/3.cc, which is now failing, wasn't a 
couple of days ago (I ran the whole testsuite like that in order to 
validate my std::list changes). When you have time, could you please 
double check? (maybe after all we *do* want it to fail in C++0x mode, 
but I'd like to understand if the behavior changed inadvertently!)

Thanks in advance,
Paolo.
Jonathan Wakely Oct. 6, 2011, 7:23 a.m. UTC | #2
On 6 October 2011 02:57, Paolo Carlini wrote:
>
> today I ran the whole testsuite in C++0x mode and I'm pretty sure that
> 23_containers/vector/modifiers/swap/3.cc, which is now failing, wasn't a
> couple of days ago (I ran the whole testsuite like that in order to validate
> my std::list changes). When you have time, could you please double check?
> (maybe after all we *do* want it to fail in C++0x mode, but I'd like to
> understand if the behavior changed inadvertently!)

I think you're right it wasn't failing before, as I ran the whole
testsuite in C++0x mode when I first added alloc_traits - I'll check
it today and see how I broke it.
diff mbox

Patch

Index: include/ext/alloc_traits.h
===================================================================
--- include/ext/alloc_traits.h	(revision 179472)
+++ include/ext/alloc_traits.h	(working copy)
@@ -106,6 +106,7 @@  template<typename _Alloc>
     using _Base_type::deallocate;
     using _Base_type::construct;
     using _Base_type::destroy;
+    using _Base_type::max_size;
 
   private:
     template<typename _Ptr>
@@ -115,6 +116,7 @@  template<typename _Alloc>
       { };
 
   public:
+    // overload construct for non-standard pointer types
     template<typename _Ptr, typename... _Args>
       static typename std::enable_if<__is_custom_pointer<_Ptr>::value>::type
       construct(_Alloc& __a, _Ptr __p, _Args&&... __args)
@@ -123,6 +125,7 @@  template<typename _Alloc>
 			      std::forward<_Args>(__args)...);
       }
 
+    // overload destroy for non-standard pointer types
     template<typename _Ptr>
       static typename std::enable_if<__is_custom_pointer<_Ptr>::value>::type
       destroy(_Alloc& __a, _Ptr __p)
@@ -156,6 +159,9 @@  template<typename _Alloc>
        	|| noexcept(swap(std::declval<_Alloc&>(), std::declval<_Alloc&>()));
     }
 
+    template<typename _Tp>
+      struct rebind
+      { typedef typename _Base_type::template __rebind_alloc<_Tp>::__type other; };
 #else
 
     typedef typename _Alloc::pointer                pointer;
@@ -179,6 +185,9 @@  template<typename _Alloc>
     static void destroy(_Alloc& __a, pointer __p)
     { __a.destroy(__p); }
 
+    static size_type max_size(const _Alloc& __a)
+    { return __a.max_size(); }
+
     static const _Alloc& _S_select_on_copy(const _Alloc& __a) { return __a; }
 
     static void _S_on_swap(_Alloc& __a, _Alloc& __b)
@@ -187,6 +196,10 @@  template<typename _Alloc>
       // 431. Swapping containers with unequal allocators.
       std::__alloc_swap<_Alloc>::_S_do_it(__a, __b);
     }
+
+    template<typename _Tp>
+      struct rebind
+      { typedef typename _Alloc::template rebind<_Tp>::other other; };
 #endif
   };
 
Index: include/bits/stl_vector.h
===================================================================
--- include/bits/stl_vector.h	(revision 179472)
+++ include/bits/stl_vector.h	(working copy)
@@ -70,7 +70,8 @@  _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
   template<typename _Tp, typename _Alloc>
     struct _Vector_base
     {
-      typedef typename _Alloc::template rebind<_Tp>::other _Tp_alloc_type;
+      typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
+        rebind<_Tp>::other _Tp_alloc_type;
       typedef typename __gnu_cxx::__alloc_traits<_Tp_alloc_type>::pointer
        	pointer;
 
@@ -643,7 +644,7 @@  _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       /**  Returns the size() of the largest possible %vector.  */
       size_type
       max_size() const _GLIBCXX_NOEXCEPT
-      { return _M_get_Tp_allocator().max_size(); }
+      { return _Alloc_traits::max_size(_M_get_Tp_allocator()); }
 
 #ifdef __GXX_EXPERIMENTAL_CXX0X__
       /**
Index: testsuite/util/testsuite_allocator.h
===================================================================
--- testsuite/util/testsuite_allocator.h	(revision 179472)
+++ testsuite/util/testsuite_allocator.h	(working copy)
@@ -386,6 +386,8 @@  namespace __gnu_test
       typedef std::integral_constant<bool, Propagate> trait_type;
 
     public:
+      // default allocator_traits::rebind_alloc would select
+      // uneq_allocator::rebind so we must define rebind here
       template<typename Up>
 	struct rebind { typedef propagating_allocator<Up, Propagate> other; };
 
@@ -433,6 +435,32 @@  namespace __gnu_test
       { return Propagate ? *this : propagating_allocator(); }
     };
 
+  // Class template supporting the minimal interface that satisfies the
+  // Allocator requirements, from example in [allocator.requirements]
+  template <class Tp>
+    struct SimpleAllocator
+    {
+      typedef Tp value_type;
+
+      SimpleAllocator() { }
+
+      template <class T>
+        SimpleAllocator(const SimpleAllocator<T>& other) { }
+
+      Tp *allocate(std::size_t n)
+      { return std::allocator<Tp>().allocate(n); }
+
+      void deallocate(Tp *p, std::size_t n)
+      { std::allocator<Tp>().deallocate(p, n); }
+    };
+
+  template <class T, class U>
+    bool operator==(const SimpleAllocator<T>&, const SimpleAllocator<U>&)
+    { return true; }
+  template <class T, class U>
+    bool operator!=(const SimpleAllocator<T>&, const SimpleAllocator<U>&)
+    { return false; }
+
 #endif
 
   template<typename Tp>
Index: testsuite/23_containers/vector/allocator/minimal.cc
===================================================================
--- testsuite/23_containers/vector/allocator/minimal.cc	(revision 0)
+++ testsuite/23_containers/vector/allocator/minimal.cc	(revision 0)
@@ -0,0 +1,45 @@ 
+// Copyright (C) 2011 Free Software Foundation
+//
+// 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++0x" }
+
+#include <vector>
+#include <memory>
+#include <testsuite_hooks.h>
+#include <testsuite_allocator.h>
+ 
+struct T { int i; };
+
+using __gnu_test::SimpleAllocator;
+
+template class std::vector<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::vector<T, alloc_type> test_type;
+  test_type v(alloc_type{});
+  VERIFY( v.max_size() == traits_type::max_size(v.get_allocator()) );
+}
+
+int main()
+{
+  test01();
+  return 0;
+}
Index: testsuite/23_containers/vector/requirements/dr438/assign_neg.cc
===================================================================
--- testsuite/23_containers/vector/requirements/dr438/assign_neg.cc	(revision 179472)
+++ testsuite/23_containers/vector/requirements/dr438/assign_neg.cc	(working copy)
@@ -18,7 +18,7 @@ 
 // <http://www.gnu.org/licenses/>.
 
 // { dg-do compile }
-// { dg-error "no matching" "" { target *-*-* } 1222 }
+// { dg-error "no matching" "" { target *-*-* } 1223 }
 
 #include <vector>
 
Index: testsuite/23_containers/vector/requirements/dr438/insert_neg.cc
===================================================================
--- testsuite/23_containers/vector/requirements/dr438/insert_neg.cc	(revision 179472)
+++ testsuite/23_containers/vector/requirements/dr438/insert_neg.cc	(working copy)
@@ -18,7 +18,7 @@ 
 // <http://www.gnu.org/licenses/>.
 
 // { dg-do compile }
-// { dg-error "no matching" "" { target *-*-* } 1263 }
+// { dg-error "no matching" "" { target *-*-* } 1264 }
 
 #include <vector>
 
Index: testsuite/23_containers/vector/requirements/dr438/constructor_1_neg.cc
===================================================================
--- testsuite/23_containers/vector/requirements/dr438/constructor_1_neg.cc	(revision 179472)
+++ testsuite/23_containers/vector/requirements/dr438/constructor_1_neg.cc	(working copy)
@@ -18,7 +18,7 @@ 
 // <http://www.gnu.org/licenses/>.
 
 // { dg-do compile }
-// { dg-error "no matching" "" { target *-*-* } 1152 }
+// { dg-error "no matching" "" { target *-*-* } 1153 }
 
 #include <vector>
 
Index: testsuite/23_containers/vector/requirements/dr438/constructor_2_neg.cc
===================================================================
--- testsuite/23_containers/vector/requirements/dr438/constructor_2_neg.cc	(revision 179472)
+++ testsuite/23_containers/vector/requirements/dr438/constructor_2_neg.cc	(working copy)
@@ -18,7 +18,7 @@ 
 // <http://www.gnu.org/licenses/>.
 
 // { dg-do compile }
-// { dg-error "no matching" "" { target *-*-* } 1152 }
+// { dg-error "no matching" "" { target *-*-* } 1153 }
 
 #include <vector>
 #include <utility>