1*38fd1498Szrj // Aligned memory buffer -*- C++ -*- 2*38fd1498Szrj 3*38fd1498Szrj // Copyright (C) 2013-2018 Free Software Foundation, Inc. 4*38fd1498Szrj // 5*38fd1498Szrj // This file is part of the GNU ISO C++ Library. This library is free 6*38fd1498Szrj // software; you can redistribute it and/or modify it under the 7*38fd1498Szrj // terms of the GNU General Public License as published by the 8*38fd1498Szrj // Free Software Foundation; either version 3, or (at your option) 9*38fd1498Szrj // any later version. 10*38fd1498Szrj 11*38fd1498Szrj // This library is distributed in the hope that it will be useful, 12*38fd1498Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of 13*38fd1498Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14*38fd1498Szrj // GNU General Public License for more details. 15*38fd1498Szrj 16*38fd1498Szrj // Under Section 7 of GPL version 3, you are granted additional 17*38fd1498Szrj // permissions described in the GCC Runtime Library Exception, version 18*38fd1498Szrj // 3.1, as published by the Free Software Foundation. 19*38fd1498Szrj 20*38fd1498Szrj // You should have received a copy of the GNU General Public License and 21*38fd1498Szrj // a copy of the GCC Runtime Library Exception along with this program; 22*38fd1498Szrj // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23*38fd1498Szrj // <http://www.gnu.org/licenses/>. 24*38fd1498Szrj 25*38fd1498Szrj /** @file ext/aligned_buffer.h 26*38fd1498Szrj * This file is a GNU extension to the Standard C++ Library. 27*38fd1498Szrj */ 28*38fd1498Szrj 29*38fd1498Szrj #ifndef _ALIGNED_BUFFER_H 30*38fd1498Szrj #define _ALIGNED_BUFFER_H 1 31*38fd1498Szrj 32*38fd1498Szrj #pragma GCC system_header 33*38fd1498Szrj 34*38fd1498Szrj #if __cplusplus >= 201103L 35*38fd1498Szrj # include <type_traits> 36*38fd1498Szrj #else 37*38fd1498Szrj # include <bits/c++0x_warning.h> 38*38fd1498Szrj #endif 39*38fd1498Szrj 40*38fd1498Szrj namespace __gnu_cxx 41*38fd1498Szrj { 42*38fd1498Szrj // A utility type containing a POD object that can hold an object of type 43*38fd1498Szrj // _Tp initialized via placement new or allocator_traits::construct. 44*38fd1498Szrj // Intended for use as a data member subobject, use __aligned_buffer for 45*38fd1498Szrj // complete objects. 46*38fd1498Szrj template<typename _Tp> 47*38fd1498Szrj struct __aligned_membuf 48*38fd1498Szrj { 49*38fd1498Szrj // Target macro ADJUST_FIELD_ALIGN can produce different alignment for 50*38fd1498Szrj // types when used as class members. __aligned_membuf is intended 51*38fd1498Szrj // for use as a class member, so align the buffer as for a class member. 52*38fd1498Szrj struct _Tp2 { _Tp _M_t; }; 53*38fd1498Szrj 54*38fd1498Szrj alignas(__alignof__(_Tp2::_M_t)) unsigned char _M_storage[sizeof(_Tp)]; 55*38fd1498Szrj 56*38fd1498Szrj __aligned_membuf() = default; 57*38fd1498Szrj 58*38fd1498Szrj // Can be used to avoid value-initialization zeroing _M_storage. 59*38fd1498Szrj __aligned_membuf(std::nullptr_t) { } 60*38fd1498Szrj 61*38fd1498Szrj void* 62*38fd1498Szrj _M_addr() noexcept 63*38fd1498Szrj { return static_cast<void*>(&_M_storage); } 64*38fd1498Szrj 65*38fd1498Szrj const void* 66*38fd1498Szrj _M_addr() const noexcept 67*38fd1498Szrj { return static_cast<const void*>(&_M_storage); } 68*38fd1498Szrj 69*38fd1498Szrj _Tp* 70*38fd1498Szrj _M_ptr() noexcept 71*38fd1498Szrj { return static_cast<_Tp*>(_M_addr()); } 72*38fd1498Szrj 73*38fd1498Szrj const _Tp* 74*38fd1498Szrj _M_ptr() const noexcept 75*38fd1498Szrj { return static_cast<const _Tp*>(_M_addr()); } 76*38fd1498Szrj }; 77*38fd1498Szrj 78*38fd1498Szrj #if _GLIBCXX_INLINE_VERSION 79*38fd1498Szrj template<typename _Tp> 80*38fd1498Szrj using __aligned_buffer = __aligned_membuf<_Tp>; 81*38fd1498Szrj #else 82*38fd1498Szrj // Similar to __aligned_membuf but aligned for complete objects, not members. 83*38fd1498Szrj // This type is used in <forward_list>, <future>, <bits/shared_ptr_base.h> 84*38fd1498Szrj // and <bits/hashtable_policy.h>, but ideally they would use __aligned_membuf 85*38fd1498Szrj // instead, as it has smaller size for some types on some targets. 86*38fd1498Szrj // This type is still used to avoid an ABI change. 87*38fd1498Szrj template<typename _Tp> 88*38fd1498Szrj struct __aligned_buffer 89*38fd1498Szrj : std::aligned_storage<sizeof(_Tp), std::alignment_of<_Tp>::value> 90*38fd1498Szrj { 91*38fd1498Szrj typename 92*38fd1498Szrj std::aligned_storage<sizeof(_Tp), std::alignment_of<_Tp>::value>::type 93*38fd1498Szrj _M_storage; 94*38fd1498Szrj 95*38fd1498Szrj __aligned_buffer() = default; 96*38fd1498Szrj 97*38fd1498Szrj // Can be used to avoid value-initialization 98*38fd1498Szrj __aligned_buffer(std::nullptr_t) { } 99*38fd1498Szrj 100*38fd1498Szrj void* 101*38fd1498Szrj _M_addr() noexcept 102*38fd1498Szrj { 103*38fd1498Szrj return static_cast<void*>(&_M_storage); 104*38fd1498Szrj } 105*38fd1498Szrj 106*38fd1498Szrj const void* 107*38fd1498Szrj _M_addr() const noexcept 108*38fd1498Szrj { 109*38fd1498Szrj return static_cast<const void*>(&_M_storage); 110*38fd1498Szrj } 111*38fd1498Szrj 112*38fd1498Szrj _Tp* 113*38fd1498Szrj _M_ptr() noexcept 114*38fd1498Szrj { return static_cast<_Tp*>(_M_addr()); } 115*38fd1498Szrj 116*38fd1498Szrj const _Tp* 117*38fd1498Szrj _M_ptr() const noexcept 118*38fd1498Szrj { return static_cast<const _Tp*>(_M_addr()); } 119*38fd1498Szrj }; 120*38fd1498Szrj #endif 121*38fd1498Szrj 122*38fd1498Szrj } // namespace 123*38fd1498Szrj 124*38fd1498Szrj #endif /* _ALIGNED_BUFFER_H */ 125