[v3] Define __gnu_cxx::__aligned_buffer

Jonathan Wakely jwakely.gcc@gmail.com
Mon Apr 8 03:07:00 GMT 2013


This patch adds a new utility type that wraps a member of type
std::aligned_storage and provides some convenient accessors, to
replace repeated uses of the same code in the library.

The new type should also be used in the other node-based containers
when they gain C++11 allocator support.

        * include/Makefile.am: Add ext/aligned_buffer.h
        * include/Makefile.in: Regenerate.
        * include/ext/aligned_buffer.h: New.
        * include/std/future (_Result): Use __aligned_buffer.
        * include/bits/forward_list.h (_Fwd_list_node): Likewise.
        * include/bits/shared_ptr_base.h (_Sp_counted_ptr_inplace): Likewise.
        * testsuite/20_util/shared_ptr/cons/43820_neg.cc: Adjust dg-error line
        number.

Tested x86_64-linux, committed to trunk.
-------------- next part --------------
commit 69bb2ce62fd89844ffef503483631ead23a4aafa
Author: Jonathan Wakely <jwakely.gcc@gmail.com>
Date:   Sun Apr 7 17:42:47 2013 +0100

    	* include/Makefile.am: Add ext/aligned_buffer.h
    	* include/Makefile.in: Regenerate.
    	* include/ext/aligned_buffer.h: New.
    	* include/std/future (_Result): Use __aligned_buffer.
    	* include/bits/forward_list.h (_Fwd_list_node): Likewise.
    	* include/bits/shared_ptr_base.h (_Sp_counted_ptr_inplace): Likewise.
    	* testsuite/20_util/shared_ptr/cons/43820_neg.cc: Adjust dg-error line
    	number.

diff --git a/libstdc++-v3/include/Makefile.am b/libstdc++-v3/include/Makefile.am
index 6bd3b43..b0ea41d 100644
--- a/libstdc++-v3/include/Makefile.am
+++ b/libstdc++-v3/include/Makefile.am
@@ -495,6 +495,7 @@ ext_srcdir = ${glibcxx_srcdir}/include/ext
 ext_builddir = ./ext
 ext_headers = \
 	${ext_srcdir}/algorithm \
+	${ext_srcdir}/aligned_buffer.h \
 	${ext_srcdir}/alloc_traits.h \
 	${ext_srcdir}/atomicity.h \
 	${ext_srcdir}/array_allocator.h \
diff --git a/libstdc++-v3/include/bits/forward_list.h b/libstdc++-v3/include/bits/forward_list.h
index 8270c4e..e7c4bdd 100644
--- a/libstdc++-v3/include/bits/forward_list.h
+++ b/libstdc++-v3/include/bits/forward_list.h
@@ -39,6 +39,7 @@
 #include <bits/stl_function.h>
 #include <bits/allocator.h>
 #include <ext/alloc_traits.h>
+#include <ext/aligned_buffer.h>
 
 namespace std _GLIBCXX_VISIBILITY(default)
 {
@@ -99,20 +100,15 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
     {
       _Fwd_list_node() = default;
 
-      typename aligned_storage<sizeof(_Tp), alignment_of<_Tp>::value>::type
-	_M_storage;
+      __gnu_cxx::__aligned_buffer<_Tp> _M_storage;
 
       _Tp*
       _M_valptr() noexcept
-      {
-	return static_cast<_Tp*>(static_cast<void*>(&_M_storage));
-      }
+      { return _M_storage._M_ptr(); }
 
       const _Tp*
       _M_valptr() const noexcept
-      {
-	return static_cast<const _Tp*>(static_cast<const void*>(&_M_storage));
-      }
+      { return _M_storage._M_ptr(); }
     };
 
   /**
diff --git a/libstdc++-v3/include/bits/shared_ptr_base.h b/libstdc++-v3/include/bits/shared_ptr_base.h
index c5df1fb..f463645 100644
--- a/libstdc++-v3/include/bits/shared_ptr_base.h
+++ b/libstdc++-v3/include/bits/shared_ptr_base.h
@@ -49,6 +49,8 @@
 #ifndef _SHARED_PTR_BASE_H
 #define _SHARED_PTR_BASE_H 1
 
+#include <ext/aligned_buffer.h>
+
 namespace std _GLIBCXX_VISIBILITY(default)
 {
 _GLIBCXX_BEGIN_NAMESPACE_VERSION
@@ -393,7 +395,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	_Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
 	: _M_impl(__a), _M_storage()
 	{
-	  _M_impl._M_ptr = static_cast<_Tp*>(static_cast<void*>(&_M_storage));
+	  _M_impl._M_ptr = _M_storage._M_ptr();
 	  // _GLIBCXX_RESOLVE_LIB_DEFECTS
 	  // 2070.  allocate_shared should use allocator_traits<A>::construct
 	  allocator_traits<_Alloc>::construct(__a, _M_impl._M_ptr,
@@ -422,9 +424,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       _M_get_deleter(const std::type_info& __ti) noexcept
       {
 #ifdef __GXX_RTTI
-	return __ti == typeid(_Sp_make_shared_tag)
-	       ? static_cast<void*>(&_M_storage)
-	       : 0;
+	return __ti == typeid(_Sp_make_shared_tag) ? _M_storage._M_addr() : 0;
 #else
         return 0;
 #endif
@@ -432,8 +432,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
     private:
       _Impl _M_impl;
-      typename aligned_storage<sizeof(_Tp), alignment_of<_Tp>::value>::type
-	_M_storage;
+      __gnu_cxx::__aligned_buffer<_Tp> _M_storage;
     };
 
   template<_Lock_policy _Lp>
diff --git a/libstdc++-v3/include/ext/aligned_buffer.h b/libstdc++-v3/include/ext/aligned_buffer.h
new file mode 100644
index 0000000..ffed88c
--- /dev/null
+++ b/libstdc++-v3/include/ext/aligned_buffer.h
@@ -0,0 +1,73 @@
+// Aligned memory buffer -*- C++ -*-
+
+// Copyright (C) 2013 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.
+
+// Under Section 7 of GPL version 3, you are granted additional
+// permissions described in the GCC Runtime Library Exception, version
+// 3.1, as published by the Free Software Foundation.
+
+// You should have received a copy of the GNU General Public License and
+// a copy of the GCC Runtime Library Exception along with this program;
+// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
+// <http://www.gnu.org/licenses/>.
+
+/** @file ext/aligned_buffer.h
+ *  This file is a GNU extension to the Standard C++ Library.
+ */
+
+#ifndef _ALIGNED_BUFFER_H
+#define _ALIGNED_BUFFER_H 1
+
+#pragma GCC system_header
+
+#if __cplusplus >= 201103L
+# include <type_traits>
+#else
+# include <bits/c++0x_warning.h>
+#endif
+
+namespace __gnu_cxx
+{
+  template<typename _Tp>
+    struct __aligned_buffer
+    : std::aligned_storage<sizeof(_Tp), std::alignment_of<_Tp>::value>
+    {
+      typename
+	std::aligned_storage<sizeof(_Tp), std::alignment_of<_Tp>::value>::type
+	_M_storage;
+
+      void*
+      _M_addr() noexcept
+      {
+        return static_cast<void*>(&_M_storage);
+      }
+
+      const void*
+      _M_addr() const noexcept
+      {
+        return static_cast<const void*>(&_M_storage);
+      }
+
+      _Tp*
+      _M_ptr() noexcept
+      { return static_cast<_Tp*>(_M_addr()); }
+
+      const _Tp*
+      _M_ptr() const noexcept
+      { return static_cast<const _Tp*>(_M_addr()); }
+    };
+
+} // namespace
+
+#endif /* _ALIGNED_BUFFER_H */
diff --git a/libstdc++-v3/include/std/future b/libstdc++-v3/include/std/future
index 30100fe..a86a7ca 100644
--- a/libstdc++-v3/include/std/future
+++ b/libstdc++-v3/include/std/future
@@ -46,6 +46,7 @@
 #include <bits/shared_ptr.h>
 #include <bits/uses_allocator.h>
 #include <bits/alloc_traits.h>
+#include <ext/aligned_buffer.h>
 
 namespace std _GLIBCXX_VISIBILITY(default)
 {
@@ -206,12 +207,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       struct _Result : _Result_base
       {
       private:
-	typedef alignment_of<_Res>				__a_of;
-	typedef aligned_storage<sizeof(_Res), __a_of::value>	__align_storage;
-	typedef typename __align_storage::type			__align_type;
-
-	__align_type		_M_storage;
-	bool 			_M_initialized;
+	__gnu_cxx::__aligned_buffer<_Res>	_M_storage;
+	bool 					_M_initialized;
 
       public:
 	typedef _Res result_type;
@@ -226,26 +223,24 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
 	// Return lvalue, future will add const or rvalue-reference
 	_Res&
-	_M_value() noexcept { return *static_cast<_Res*>(_M_addr()); }
+	_M_value() noexcept { return *_M_storage._M_ptr(); }
 
 	void
 	_M_set(const _Res& __res)
 	{
-	  ::new (_M_addr()) _Res(__res);
+	  ::new (_M_storage._M_addr()) _Res(__res);
 	  _M_initialized = true;
 	}
 
 	void
 	_M_set(_Res&& __res)
 	{
-	  ::new (_M_addr()) _Res(std::move(__res));
+	  ::new (_M_storage._M_addr()) _Res(std::move(__res));
 	  _M_initialized = true;
 	}
 
       private:
 	void _M_destroy() { delete this; }
-
-	void* _M_addr() noexcept { return static_cast<void*>(&_M_storage); }
     };
 
     /// A unique_ptr based on the instantiating type.
diff --git a/libstdc++-v3/testsuite/20_util/shared_ptr/cons/43820_neg.cc b/libstdc++-v3/testsuite/20_util/shared_ptr/cons/43820_neg.cc
index 6fa34a9..3a5f053 100644
--- a/libstdc++-v3/testsuite/20_util/shared_ptr/cons/43820_neg.cc
+++ b/libstdc++-v3/testsuite/20_util/shared_ptr/cons/43820_neg.cc
@@ -32,7 +32,7 @@ void test01()
 {
   X* px = 0;
   std::shared_ptr<X> p1(px);   // { dg-error "here" }
-  // { dg-error "incomplete" "" { target *-*-* } 771 }
+  // { dg-error "incomplete" "" { target *-*-* } 770 }
 
   std::shared_ptr<X> p9(ap());  // { dg-error "here" }
   // { dg-error "incomplete" "" { target *-*-* } 307 }


More information about the Gcc-patches mailing list