libstdc++
aligned_buffer.h
Go to the documentation of this file.
00001 // Aligned memory buffer -*- C++ -*-
00002 
00003 // Copyright (C) 2013-2015 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file ext/aligned_buffer.h
00026  *  This file is a GNU extension to the Standard C++ Library.
00027  */
00028 
00029 #ifndef _ALIGNED_BUFFER_H
00030 #define _ALIGNED_BUFFER_H 1
00031 
00032 #pragma GCC system_header
00033 
00034 #if __cplusplus >= 201103L
00035 # include <type_traits>
00036 #else
00037 # include <bits/c++0x_warning.h>
00038 #endif
00039 
00040 namespace __gnu_cxx
00041 {
00042   // A utility type containing a POD object that can hold an object of type
00043   // _Tp initialized via placement new or allocator_traits::construct.
00044   // Intended for use as a data member subobject, use __aligned_buffer for
00045   // complete objects.
00046   template<typename _Tp>
00047     struct __aligned_membuf
00048     {
00049       // Target macro ADJUST_FIELD_ALIGN can produce different alignment for
00050       // types when used as class members. __aligned_membuf is intended
00051       // for use as a class member, so align the buffer as for a class member.
00052       struct _Tp2 { _Tp _M_t; };
00053 
00054       alignas(__alignof__(_Tp2::_M_t)) unsigned char _M_storage[sizeof(_Tp)];
00055 
00056       __aligned_membuf() = default;
00057 
00058       // Can be used to avoid value-initialization zeroing _M_storage.
00059       __aligned_membuf(std::nullptr_t) { }
00060 
00061       void*
00062       _M_addr() noexcept
00063       { return static_cast<void*>(&_M_storage); }
00064 
00065       const void*
00066       _M_addr() const noexcept
00067       { return static_cast<const void*>(&_M_storage); }
00068 
00069       _Tp*
00070       _M_ptr() noexcept
00071       { return static_cast<_Tp*>(_M_addr()); }
00072 
00073       const _Tp*
00074       _M_ptr() const noexcept
00075       { return static_cast<const _Tp*>(_M_addr()); }
00076     };
00077 
00078   // Similar to __aligned_membuf but aligned for complete objects, not members.
00079   // This type is used in <forward_list>, <future>, <bits/shared_ptr_base.h>
00080   // and <bits/hashtable_policy.h>, but ideally they would use __aligned_membuf
00081   // instead, as it has smaller size for some types on some targets.
00082   // This type is still used to avoid an ABI change.
00083   template<typename _Tp>
00084     struct __aligned_buffer
00085     : std::aligned_storage<sizeof(_Tp), std::alignment_of<_Tp>::value>
00086     {
00087       typename
00088         std::aligned_storage<sizeof(_Tp), std::alignment_of<_Tp>::value>::type
00089         _M_storage;
00090 
00091       __aligned_buffer() = default;
00092 
00093       // Can be used to avoid value-initialization
00094       __aligned_buffer(std::nullptr_t) { }
00095 
00096       void*
00097       _M_addr() noexcept
00098       {
00099         return static_cast<void*>(&_M_storage);
00100       }
00101 
00102       const void*
00103       _M_addr() const noexcept
00104       {
00105         return static_cast<const void*>(&_M_storage);
00106       }
00107 
00108       _Tp*
00109       _M_ptr() noexcept
00110       { return static_cast<_Tp*>(_M_addr()); }
00111 
00112       const _Tp*
00113       _M_ptr() const noexcept
00114       { return static_cast<const _Tp*>(_M_addr()); }
00115     };
00116 
00117 } // namespace
00118 
00119 #endif /* _ALIGNED_BUFFER_H */