您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

126 行
3.9KB

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