136ac495dSmrg // Aligned memory buffer -*- C++ -*- 236ac495dSmrg 3*8feb0f0bSmrg // Copyright (C) 2013-2020 Free Software Foundation, Inc. 436ac495dSmrg // 536ac495dSmrg // This file is part of the GNU ISO C++ Library. This library is free 636ac495dSmrg // software; you can redistribute it and/or modify it under the 736ac495dSmrg // terms of the GNU General Public License as published by the 836ac495dSmrg // Free Software Foundation; either version 3, or (at your option) 936ac495dSmrg // any later version. 1036ac495dSmrg 1136ac495dSmrg // This library is distributed in the hope that it will be useful, 1236ac495dSmrg // but WITHOUT ANY WARRANTY; without even the implied warranty of 1336ac495dSmrg // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1436ac495dSmrg // GNU General Public License for more details. 1536ac495dSmrg 1636ac495dSmrg // Under Section 7 of GPL version 3, you are granted additional 1736ac495dSmrg // permissions described in the GCC Runtime Library Exception, version 1836ac495dSmrg // 3.1, as published by the Free Software Foundation. 1936ac495dSmrg 2036ac495dSmrg // You should have received a copy of the GNU General Public License and 2136ac495dSmrg // a copy of the GCC Runtime Library Exception along with this program; 2236ac495dSmrg // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 2336ac495dSmrg // <http://www.gnu.org/licenses/>. 2436ac495dSmrg 2536ac495dSmrg /** @file ext/aligned_buffer.h 2636ac495dSmrg * This file is a GNU extension to the Standard C++ Library. 2736ac495dSmrg */ 2836ac495dSmrg 2936ac495dSmrg #ifndef _ALIGNED_BUFFER_H 3036ac495dSmrg #define _ALIGNED_BUFFER_H 1 3136ac495dSmrg 3236ac495dSmrg #pragma GCC system_header 3336ac495dSmrg 3436ac495dSmrg #if __cplusplus >= 201103L 3536ac495dSmrg # include <type_traits> 3636ac495dSmrg #else 3736ac495dSmrg # include <bits/c++0x_warning.h> 3836ac495dSmrg #endif 3936ac495dSmrg 4036ac495dSmrg namespace __gnu_cxx 4136ac495dSmrg { 4236ac495dSmrg // A utility type containing a POD object that can hold an object of type 4336ac495dSmrg // _Tp initialized via placement new or allocator_traits::construct. 4436ac495dSmrg // Intended for use as a data member subobject, use __aligned_buffer for 4536ac495dSmrg // complete objects. 4636ac495dSmrg template<typename _Tp> 4736ac495dSmrg struct __aligned_membuf 4836ac495dSmrg { 4936ac495dSmrg // Target macro ADJUST_FIELD_ALIGN can produce different alignment for 5036ac495dSmrg // types when used as class members. __aligned_membuf is intended 5136ac495dSmrg // for use as a class member, so align the buffer as for a class member. 52a2dc1f3fSmrg // Since GCC 8 we could just use alignof(_Tp) instead, but older 53a2dc1f3fSmrg // versions of non-GNU compilers might still need this trick. 5436ac495dSmrg struct _Tp2 { _Tp _M_t; }; 5536ac495dSmrg 5636ac495dSmrg alignas(__alignof__(_Tp2::_M_t)) unsigned char _M_storage[sizeof(_Tp)]; 5736ac495dSmrg 5836ac495dSmrg __aligned_membuf() = default; 5936ac495dSmrg 6036ac495dSmrg // Can be used to avoid value-initialization zeroing _M_storage. __aligned_membuf__aligned_membuf6136ac495dSmrg __aligned_membuf(std::nullptr_t) { } 6236ac495dSmrg 6336ac495dSmrg void* _M_addr__aligned_membuf6436ac495dSmrg _M_addr() noexcept 6536ac495dSmrg { return static_cast<void*>(&_M_storage); } 6636ac495dSmrg 6736ac495dSmrg const void* _M_addr__aligned_membuf6836ac495dSmrg _M_addr() const noexcept 6936ac495dSmrg { return static_cast<const void*>(&_M_storage); } 7036ac495dSmrg 7136ac495dSmrg _Tp* _M_ptr__aligned_membuf7236ac495dSmrg _M_ptr() noexcept 7336ac495dSmrg { return static_cast<_Tp*>(_M_addr()); } 7436ac495dSmrg 7536ac495dSmrg const _Tp* _M_ptr__aligned_membuf7636ac495dSmrg _M_ptr() const noexcept 7736ac495dSmrg { return static_cast<const _Tp*>(_M_addr()); } 7836ac495dSmrg }; 7936ac495dSmrg 80a2dc1f3fSmrg #if _GLIBCXX_INLINE_VERSION 81a2dc1f3fSmrg template<typename _Tp> 82a2dc1f3fSmrg using __aligned_buffer = __aligned_membuf<_Tp>; 83a2dc1f3fSmrg #else 8436ac495dSmrg // Similar to __aligned_membuf but aligned for complete objects, not members. 8536ac495dSmrg // This type is used in <forward_list>, <future>, <bits/shared_ptr_base.h> 8636ac495dSmrg // and <bits/hashtable_policy.h>, but ideally they would use __aligned_membuf 8736ac495dSmrg // instead, as it has smaller size for some types on some targets. 8836ac495dSmrg // This type is still used to avoid an ABI change. 8936ac495dSmrg template<typename _Tp> 9036ac495dSmrg struct __aligned_buffer 91a2dc1f3fSmrg : std::aligned_storage<sizeof(_Tp), __alignof__(_Tp)> 9236ac495dSmrg { 9336ac495dSmrg typename 94a2dc1f3fSmrg std::aligned_storage<sizeof(_Tp), __alignof__(_Tp)>::type _M_storage; 9536ac495dSmrg 9636ac495dSmrg __aligned_buffer() = default; 9736ac495dSmrg 9836ac495dSmrg // Can be used to avoid value-initialization __aligned_buffer__aligned_buffer9936ac495dSmrg __aligned_buffer(std::nullptr_t) { } 10036ac495dSmrg 10136ac495dSmrg void* _M_addr__aligned_buffer10236ac495dSmrg _M_addr() noexcept 10336ac495dSmrg { 10436ac495dSmrg return static_cast<void*>(&_M_storage); 10536ac495dSmrg } 10636ac495dSmrg 10736ac495dSmrg const void* _M_addr__aligned_buffer10836ac495dSmrg _M_addr() const noexcept 10936ac495dSmrg { 11036ac495dSmrg return static_cast<const void*>(&_M_storage); 11136ac495dSmrg } 11236ac495dSmrg 11336ac495dSmrg _Tp* _M_ptr__aligned_buffer11436ac495dSmrg _M_ptr() noexcept 11536ac495dSmrg { return static_cast<_Tp*>(_M_addr()); } 11636ac495dSmrg 11736ac495dSmrg const _Tp* _M_ptr__aligned_buffer11836ac495dSmrg _M_ptr() const noexcept 11936ac495dSmrg { return static_cast<const _Tp*>(_M_addr()); } 12036ac495dSmrg }; 121a2dc1f3fSmrg #endif 12236ac495dSmrg 12336ac495dSmrg } // namespace 12436ac495dSmrg 12536ac495dSmrg #endif /* _ALIGNED_BUFFER_H */ 126