This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Containers default initialization


Hi

I have eventually adapt the test to all containers and the result is successful for map/set/unordered_map/unordered_set. It is failing for deque/list/forward_list/vector/vector<bool>.

I even try to change the test to look at the difference between an explicit call to the default constructor done through the placement new call and an implicit call done on normal declaration. I wondered if we would have the same kind of difference we have between a int i; and a int i(); I tried to set the stack to ~0 before declaring the instance. I know there is no guarantee on the content of the stack for the following declaration but do you think it is reliable enough to commit it ?

    Ok to commit the successful tests ?

Franckly I don't understand the result of those tests. I would have expect map/set to fail and others to succeed. We might need help from compiler guys, no ?

François



Index: testsuite/23_containers/map/allocator/default_init.cc
===================================================================
--- testsuite/23_containers/map/allocator/default_init.cc	(nonexistent)
+++ testsuite/23_containers/map/allocator/default_init.cc	(working copy)
@@ -0,0 +1,57 @@
+// Copyright (C) 2017 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 run { target c++11 } }
+
+#include <map>
+#include <testsuite_hooks.h>
+#include <testsuite_allocator.h>
+
+#include <ext/aligned_buffer.h>
+
+using T = int;
+
+using __gnu_test::default_init_allocator;
+
+void test01()
+{
+  typedef default_init_allocator<std::pair<const T, T>> alloc_type;
+  typedef std::map<T, T, std::less<T>, alloc_type> test_type;
+
+  {
+    __gnu_cxx::__aligned_buffer<test_type> buf;
+    __builtin_memset(buf._M_addr(), ~0, sizeof(test_type));
+
+    VERIFY( buf._M_ptr()->get_allocator().state != 0 );
+  
+    test_type *tmp = ::new(buf._M_addr()) test_type();
+
+    VERIFY( tmp->get_allocator().state == 0 );
+
+    tmp->~test_type();
+    __builtin_memset(buf._M_addr(), ~0, sizeof(test_type));
+  }
+
+  test_type tmp;
+  VERIFY( tmp.get_allocator().state == 0 );
+}
+
+int main()
+{
+  test01();
+  return 0;
+}
Index: testsuite/23_containers/set/allocator/default_init.cc
===================================================================
--- testsuite/23_containers/set/allocator/default_init.cc	(nonexistent)
+++ testsuite/23_containers/set/allocator/default_init.cc	(working copy)
@@ -0,0 +1,57 @@
+// Copyright (C) 2017 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 run { target c++11 } }
+
+#include <set>
+#include <testsuite_hooks.h>
+#include <testsuite_allocator.h>
+
+#include <ext/aligned_buffer.h>
+
+using T = int;
+
+using __gnu_test::default_init_allocator;
+
+void test01()
+{
+  typedef default_init_allocator<T> alloc_type;
+  typedef std::set<T, std::less<T>, alloc_type> test_type;
+
+  {
+    __gnu_cxx::__aligned_buffer<test_type> buf;
+    __builtin_memset(buf._M_addr(), ~0, sizeof(test_type));
+
+    VERIFY( buf._M_ptr()->get_allocator().state != 0 );
+  
+    test_type *tmp = ::new(buf._M_addr()) test_type();
+
+    VERIFY( tmp->get_allocator().state == 0 );
+
+    tmp->~test_type();
+    __builtin_memset(buf._M_addr(), ~0, sizeof(test_type));
+  }
+
+  test_type tmp;
+  VERIFY( tmp.get_allocator().state == 0 );
+}
+
+int main()
+{
+  test01();
+  return 0;
+}
Index: testsuite/23_containers/unordered_map/allocator/default_init.cc
===================================================================
--- testsuite/23_containers/unordered_map/allocator/default_init.cc	(nonexistent)
+++ testsuite/23_containers/unordered_map/allocator/default_init.cc	(working copy)
@@ -0,0 +1,58 @@
+// Copyright (C) 2017 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 run { target c++11 } }
+
+#include <unordered_map>
+#include <testsuite_hooks.h>
+#include <testsuite_allocator.h>
+
+#include <ext/aligned_buffer.h>
+
+using T = int;
+
+using __gnu_test::default_init_allocator;
+
+void test01()
+{
+  typedef default_init_allocator<std::pair<const T, T>> alloc_type;
+  typedef std::unordered_map<T, T, std::hash<T>, std::equal_to<T>,
+			     alloc_type> test_type;
+
+  {
+    __gnu_cxx::__aligned_buffer<test_type> buf;
+    __builtin_memset(buf._M_addr(), ~0, sizeof(test_type));
+
+    VERIFY( buf._M_ptr()->get_allocator().state != 0 );
+  
+    test_type *tmp = ::new(buf._M_addr()) test_type();
+
+    VERIFY( tmp->get_allocator().state == 0 );
+
+    tmp->~test_type();
+    __builtin_memset(buf._M_addr(), ~0, sizeof(test_type));
+  }
+
+  test_type tmp;
+  VERIFY( tmp.get_allocator().state == 0 );
+}
+
+int main()
+{
+  test01();
+  return 0;
+}
Index: testsuite/23_containers/unordered_set/allocator/default_init.cc
===================================================================
--- testsuite/23_containers/unordered_set/allocator/default_init.cc	(nonexistent)
+++ testsuite/23_containers/unordered_set/allocator/default_init.cc	(working copy)
@@ -0,0 +1,58 @@
+// Copyright (C) 2017 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 run { target c++11 } }
+
+#include <unordered_set>
+#include <testsuite_hooks.h>
+#include <testsuite_allocator.h>
+
+#include <ext/aligned_buffer.h>
+
+using T = int;
+
+using __gnu_test::default_init_allocator;
+
+void test01()
+{
+  typedef default_init_allocator<T> alloc_type;
+  typedef std::unordered_set<T, std::hash<T>, std::equal_to<T>,
+			     alloc_type> test_type;
+
+  {
+    __gnu_cxx::__aligned_buffer<test_type> buf;
+    __builtin_memset(buf._M_addr(), ~0, sizeof(test_type));
+
+    VERIFY( buf._M_ptr()->get_allocator().state != 0 );
+  
+    test_type *tmp = ::new(buf._M_addr()) test_type();
+
+    VERIFY( tmp->get_allocator().state == 0 );
+
+    tmp->~test_type();
+    __builtin_memset(buf._M_addr(), ~0, sizeof(test_type));
+  }
+
+  test_type tmp;
+  VERIFY( tmp.get_allocator().state == 0 );
+}
+
+int main()
+{
+  test01();
+  return 0;
+}
Index: testsuite/util/testsuite_allocator.h
===================================================================
--- testsuite/util/testsuite_allocator.h	(revision 248855)
+++ testsuite/util/testsuite_allocator.h	(working copy)
@@ -508,6 +508,38 @@
     bool operator!=(const SimpleAllocator<T>&, const SimpleAllocator<U>&)
     { return false; }
 
+  template<typename T>
+    struct default_init_allocator
+    {
+      using value_type = T;
+
+      default_init_allocator() = default;
+
+      template<typename U>
+        default_init_allocator(const default_init_allocator<U>& a)
+	  : state(a.state)
+        { }
+
+      T*
+      allocate(std::size_t n)
+      { return std::allocator<T>().allocate(n); }
+
+      void
+      deallocate(T* p, std::size_t n)
+      { std::allocator<T>().deallocate(p, n); }
+
+      int state;
+    };
+
+  template<typename T, typename U>
+    bool operator==(const default_init_allocator<T>& t,
+		    const default_init_allocator<U>& u)
+    { return t.state == u.state; }
+
+  template<typename T, typename U>
+    bool operator!=(const default_init_allocator<T>& t,
+		    const default_init_allocator<U>& u)
+    { return !(t == u); }
 #endif
 
   template<typename Tp>

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]