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]

[v3] tr1/type_traits: alignment_of, aligned_storage


Hi,

tested x86/x86_64/ia64-linux, committed to mainline.

Paolo.

///////////////
2005-01-11  Paolo Carlini  <pcarlini@suse.de>

	* include/tr1/type_traits: Implement alignment_of and aligned_storage.
	* testsuite/tr1/4_metaprogramming/other_transformations/
	aligned_storage/aligned_storage.cc: New.
	* testsuite/tr1/4_metaprogramming/other_transformations/
	aligned_storage/typedefs.cc: Likewise.
	* testsuite/tr1/4_metaprogramming/type_properties/
	alignment_of/alignment_of.cc: Likewise.
	* testsuite/tr1/4_metaprogramming/type_properties/
	alignment_of/typedefs.cc: Likewise.
diff -urN libstdc++-v3-orig/include/tr1/type_traits libstdc++-v3/include/tr1/type_traits
--- libstdc++-v3-orig/include/tr1/type_traits	2004-12-31 19:15:19.000000000 +0100
+++ libstdc++-v3/include/tr1/type_traits	2005-01-11 10:52:22.000000000 +0100
@@ -1,6 +1,6 @@
 // TR1 type_traits -*- C++ -*-
 
-// Copyright (C) 2004 Free Software Foundation, Inc.
+// Copyright (C) 2004, 2005 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
@@ -144,10 +144,10 @@
   // template<typename>
   //   struct is_member_function_pointer
   //   : public false_type { };
-  //   _DEFINE_SPEC(2, is_member_function_pointer, _Tp _Cp::*,
-  //	            is_function<_Tp>::value)
-
-  // Ugly, temporary workaround for member functions with up to 15 arguments.
+  // _DEFINE_SPEC(2, is_member_function_pointer, _Tp _Cp::*,
+  //	          is_function<_Tp>::value)
+  //
+  // Temporary workaround for member functions with up to 15 arguments:
   template<typename>
     struct __is_mfp_helper
     { static const bool __value = false; };
@@ -516,6 +516,10 @@
   template<typename>
     struct has_virtual_destructor
     : public false_type { };
+
+  template<typename _Tp>
+    struct alignment_of
+    : public integral_constant<std::size_t, __alignof__(_Tp)> { };
   
   template<typename>
     struct rank
@@ -652,6 +656,77 @@
     { typedef typename remove_reference<_Tp>::type*     type; };
 
   /// @brief  other transformations [4.8].
+  
+  // Due to c++/19163 and c++/17743, for the time being we cannot use
+  // the correct, neat implementation :-(
+  // 
+  // template<std::size_t _Len, std::size_t _Align>
+  //   struct aligned_storage
+  //   { typedef char type[_Len] __attribute__((aligned(_Align))); }
+  //
+  // Temporary workaround, useful for Align up to 32:
+  template<std::size_t, std::size_t>
+    struct aligned_storage { };
+
+  template<std::size_t _Len>
+    struct aligned_storage<_Len, 1>
+    {
+      union type
+      {
+	unsigned char __data[_Len];
+	char __align __attribute__((aligned(1)));
+      };
+    };
+
+  template<std::size_t _Len>
+    struct aligned_storage<_Len, 2>
+    {
+      union type
+      {
+	unsigned char __data[_Len];
+	char __align __attribute__((aligned(2)));
+      };
+    };
+
+  template<std::size_t _Len>
+    struct aligned_storage<_Len, 4>
+    {
+      union type
+      {
+	unsigned char __data[_Len];
+	char __align __attribute__((aligned(4)));
+      };
+    };
+
+  template<std::size_t _Len>
+    struct aligned_storage<_Len, 8>
+    {
+      union type
+      {
+	unsigned char __data[_Len];
+	char __align __attribute__((aligned(8)));
+      };
+    };
+
+  template<std::size_t _Len>
+    struct aligned_storage<_Len, 16>
+    {
+      union type
+      {
+	unsigned char __data[_Len];
+	char __align __attribute__((aligned(16)));
+      };
+    };
+  
+  template<std::size_t _Len>
+    struct aligned_storage<_Len, 32>
+    {
+      union type
+      {
+	unsigned char __data[_Len];
+	char __align __attribute__((aligned(32)));
+      };
+    };
 
 #undef _DEFINE_SPEC_0_HELPER
 #undef _DEFINE_SPEC_1_HELPER
diff -urN libstdc++-v3-orig/testsuite/tr1/4_metaprogramming/other_transformations/aligned_storage/aligned_storage.cc libstdc++-v3/testsuite/tr1/4_metaprogramming/other_transformations/aligned_storage/aligned_storage.cc
--- libstdc++-v3-orig/testsuite/tr1/4_metaprogramming/other_transformations/aligned_storage/aligned_storage.cc	1970-01-01 01:00:00.000000000 +0100
+++ libstdc++-v3/testsuite/tr1/4_metaprogramming/other_transformations/aligned_storage/aligned_storage.cc	2005-01-11 12:10:01.000000000 +0100
@@ -0,0 +1,63 @@
+// 2005-01-11  Paolo Carlini  <pcarlini@suse.de>
+//
+// Copyright (C) 2005 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 2, 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 COPYING.  If not, write to the Free
+// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+// USA.
+
+// 4.8 Other transformations
+
+#include <tr1/type_traits>
+#include <testsuite_hooks.h>
+#include <testsuite_tr1.h>
+
+void test01()
+{
+  bool test __attribute__((unused)) = true;
+  using std::tr1::aligned_storage;
+  using std::tr1::alignment_of;
+  using namespace __gnu_test;
+  
+  const std::size_t align_c = alignment_of<char>::value;
+  VERIFY( (sizeof(aligned_storage<4, align_c>::type) >= 4) );
+  VERIFY( (__alignof__(aligned_storage<4, align_c>::type) == align_c) );
+
+  const std::size_t align_s = alignment_of<short>::value;
+  VERIFY( (sizeof(aligned_storage<1, align_s>::type) >= 1) );
+  VERIFY( (__alignof__(aligned_storage<1, align_s>::type) == align_s) );
+
+  const std::size_t align_i = alignment_of<int>::value;
+  VERIFY( (sizeof(aligned_storage<7, align_i>::type) >= 7) );
+  VERIFY( (__alignof__(aligned_storage<7, align_i>::type) == align_i) );
+
+  const std::size_t align_d = alignment_of<double>::value;
+  VERIFY( (sizeof(aligned_storage<2, align_d>::type) >= 2) );
+  VERIFY( (__alignof__(aligned_storage<2, align_d>::type) == align_d) );
+
+  const std::size_t align_ai = alignment_of<int[4]>::value;
+  VERIFY( (sizeof(aligned_storage<20, align_ai>::type) >= 20) );
+  VERIFY( (__alignof__(aligned_storage<20, align_ai>::type) == align_ai) );
+
+  const std::size_t align_ct = alignment_of<ClassType>::value;
+  VERIFY( (sizeof(aligned_storage<11, align_ct>::type) >= 11) );
+  VERIFY( (__alignof__(aligned_storage<11, align_ct>::type) == align_ct) );
+}
+
+int main()
+{
+  test01();
+  return 0;
+}
diff -urN libstdc++-v3-orig/testsuite/tr1/4_metaprogramming/other_transformations/aligned_storage/typedefs.cc libstdc++-v3/testsuite/tr1/4_metaprogramming/other_transformations/aligned_storage/typedefs.cc
--- libstdc++-v3-orig/testsuite/tr1/4_metaprogramming/other_transformations/aligned_storage/typedefs.cc	1970-01-01 01:00:00.000000000 +0100
+++ libstdc++-v3/testsuite/tr1/4_metaprogramming/other_transformations/aligned_storage/typedefs.cc	2005-01-11 11:53:31.000000000 +0100
@@ -0,0 +1,33 @@
+// 2005-01-11  Paolo Carlini  <pcarlini@suse.de>
+//
+// Copyright (C) 2005 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 2, 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 COPYING.  If not, write to the Free
+// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+// USA.
+
+// 
+// NB: This file is for testing tr1/type_traits with NO OTHER INCLUDES.
+
+#include <tr1/type_traits>
+
+// { dg-do compile }
+
+void test01()
+{
+  // Check for required typedefs
+  typedef std::tr1::aligned_storage<1, 1>     test_type;
+  typedef test_type::type                     type;
+}
diff -urN libstdc++-v3-orig/testsuite/tr1/4_metaprogramming/type_properties/alignment_of/alignment_of.cc libstdc++-v3/testsuite/tr1/4_metaprogramming/type_properties/alignment_of/alignment_of.cc
--- libstdc++-v3-orig/testsuite/tr1/4_metaprogramming/type_properties/alignment_of/alignment_of.cc	1970-01-01 01:00:00.000000000 +0100
+++ libstdc++-v3/testsuite/tr1/4_metaprogramming/type_properties/alignment_of/alignment_of.cc	2005-01-11 11:26:42.000000000 +0100
@@ -0,0 +1,45 @@
+// 2005-01-11  Paolo Carlini  <pcarlini@suse.de>
+//
+// Copyright (C) 2005 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 2, 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 COPYING.  If not, write to the Free
+// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+// USA.
+
+// 4.5.3 Type properties
+
+#include <tr1/type_traits>
+#include <testsuite_hooks.h>
+#include <testsuite_tr1.h>
+
+void test01()
+{
+  bool test __attribute__((unused)) = true;
+  using std::tr1::alignment_of;
+  using namespace __gnu_test;
+
+  VERIFY( (test_property<alignment_of, char>(__alignof__(char))) );
+  VERIFY( (test_property<alignment_of, short>(__alignof__(short))) );
+  VERIFY( (test_property<alignment_of, int>(__alignof__(int))) );
+  VERIFY( (test_property<alignment_of, double>(__alignof__(double))) );
+  VERIFY( (test_property<alignment_of, int[4]>(__alignof__(int[4]))) );
+  VERIFY( (test_property<alignment_of, ClassType>(__alignof__(ClassType))) );
+}
+
+int main()
+{
+  test01();
+  return 0;
+}
diff -urN libstdc++-v3-orig/testsuite/tr1/4_metaprogramming/type_properties/alignment_of/typedefs.cc libstdc++-v3/testsuite/tr1/4_metaprogramming/type_properties/alignment_of/typedefs.cc
--- libstdc++-v3-orig/testsuite/tr1/4_metaprogramming/type_properties/alignment_of/typedefs.cc	1970-01-01 01:00:00.000000000 +0100
+++ libstdc++-v3/testsuite/tr1/4_metaprogramming/type_properties/alignment_of/typedefs.cc	2005-01-11 11:19:08.000000000 +0100
@@ -0,0 +1,36 @@
+// 2005-01-11  Paolo Carlini  <pcarlini@suse.de>
+//
+// Copyright (C) 2005 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 2, 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 COPYING.  If not, write to the Free
+// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+// USA.
+
+// 
+// NB: This file is for testing tr1/type_traits with NO OTHER INCLUDES.
+
+#include <tr1/type_traits>
+
+// { dg-do compile }
+
+void test01()
+{
+  // Check for required typedefs
+  typedef std::tr1::alignment_of<int>         test_type;
+  typedef test_type::value_type               value_type;
+  typedef test_type::type                     type;
+  typedef test_type::type::value_type         type_value_type;
+  typedef test_type::type::type               type_type;
+}

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