libstdc++
|
00001 // Aligned memory buffer -*- C++ -*- 00002 00003 // Copyright (C) 2013-2019 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 // Since GCC 8 we could just use alignof(_Tp) instead, but older 00053 // versions of non-GNU compilers might still need this trick. 00054 struct _Tp2 { _Tp _M_t; }; 00055 00056 alignas(__alignof__(_Tp2::_M_t)) unsigned char _M_storage[sizeof(_Tp)]; 00057 00058 __aligned_membuf() = default; 00059 00060 // Can be used to avoid value-initialization zeroing _M_storage. 00061 __aligned_membuf(std::nullptr_t) { } 00062 00063 void* 00064 _M_addr() noexcept 00065 { return static_cast<void*>(&_M_storage); } 00066 00067 const void* 00068 _M_addr() const noexcept 00069 { return static_cast<const void*>(&_M_storage); } 00070 00071 _Tp* 00072 _M_ptr() noexcept 00073 { return static_cast<_Tp*>(_M_addr()); } 00074 00075 const _Tp* 00076 _M_ptr() const noexcept 00077 { return static_cast<const _Tp*>(_M_addr()); } 00078 }; 00079 00080 #if _GLIBCXX_INLINE_VERSION 00081 template<typename _Tp> 00082 using __aligned_buffer = __aligned_membuf<_Tp>; 00083 #else 00084 // Similar to __aligned_membuf but aligned for complete objects, not members. 00085 // This type is used in <forward_list>, <future>, <bits/shared_ptr_base.h> 00086 // and <bits/hashtable_policy.h>, but ideally they would use __aligned_membuf 00087 // instead, as it has smaller size for some types on some targets. 00088 // This type is still used to avoid an ABI change. 00089 template<typename _Tp> 00090 struct __aligned_buffer 00091 : std::aligned_storage<sizeof(_Tp), __alignof__(_Tp)> 00092 { 00093 typename 00094 std::aligned_storage<sizeof(_Tp), __alignof__(_Tp)>::type _M_storage; 00095 00096 __aligned_buffer() = default; 00097 00098 // Can be used to avoid value-initialization 00099 __aligned_buffer(std::nullptr_t) { } 00100 00101 void* 00102 _M_addr() noexcept 00103 { 00104 return static_cast<void*>(&_M_storage); 00105 } 00106 00107 const void* 00108 _M_addr() const noexcept 00109 { 00110 return static_cast<const void*>(&_M_storage); 00111 } 00112 00113 _Tp* 00114 _M_ptr() noexcept 00115 { return static_cast<_Tp*>(_M_addr()); } 00116 00117 const _Tp* 00118 _M_ptr() const noexcept 00119 { return static_cast<const _Tp*>(_M_addr()); } 00120 }; 00121 #endif 00122 00123 } // namespace 00124 00125 #endif /* _ALIGNED_BUFFER_H */