new_allocator.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // Allocator that wraps operator new -*- C++ -*-
  2. // Copyright (C) 2001-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/new_allocator.h
  21. * This file is a GNU extension to the Standard C++ Library.
  22. */
  23. #ifndef _NEW_ALLOCATOR_H
  24. #define _NEW_ALLOCATOR_H 1
  25. #include <bits/c++config.h>
  26. #include <new>
  27. #include <bits/functexcept.h>
  28. #include <bits/move.h>
  29. #if __cplusplus >= 201103L
  30. #include <type_traits>
  31. #endif
  32. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  33. {
  34. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  35. /**
  36. * @brief An allocator that uses global new, as per [20.4].
  37. * @ingroup allocators
  38. *
  39. * This is precisely the allocator defined in the C++ Standard.
  40. * - all allocation calls operator new
  41. * - all deallocation calls operator delete
  42. *
  43. * @tparam _Tp Type of allocated object.
  44. */
  45. template<typename _Tp>
  46. class new_allocator
  47. {
  48. public:
  49. typedef _Tp value_type;
  50. typedef std::size_t size_type;
  51. typedef std::ptrdiff_t difference_type;
  52. #if __cplusplus <= 201703L
  53. typedef _Tp* pointer;
  54. typedef const _Tp* const_pointer;
  55. typedef _Tp& reference;
  56. typedef const _Tp& const_reference;
  57. template<typename _Tp1>
  58. struct rebind
  59. { typedef new_allocator<_Tp1> other; };
  60. #endif
  61. #if __cplusplus >= 201103L
  62. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  63. // 2103. propagate_on_container_move_assignment
  64. typedef std::true_type propagate_on_container_move_assignment;
  65. #endif
  66. _GLIBCXX20_CONSTEXPR
  67. new_allocator() _GLIBCXX_USE_NOEXCEPT { }
  68. _GLIBCXX20_CONSTEXPR
  69. new_allocator(const new_allocator&) _GLIBCXX_USE_NOEXCEPT { }
  70. template<typename _Tp1>
  71. _GLIBCXX20_CONSTEXPR
  72. new_allocator(const new_allocator<_Tp1>&) _GLIBCXX_USE_NOEXCEPT { }
  73. #if __cplusplus <= 201703L
  74. ~new_allocator() _GLIBCXX_USE_NOEXCEPT { }
  75. pointer
  76. address(reference __x) const _GLIBCXX_NOEXCEPT
  77. { return std::__addressof(__x); }
  78. const_pointer
  79. address(const_reference __x) const _GLIBCXX_NOEXCEPT
  80. { return std::__addressof(__x); }
  81. #endif
  82. // NB: __n is permitted to be 0. The C++ standard says nothing
  83. // about what the return value is when __n == 0.
  84. _GLIBCXX_NODISCARD _Tp*
  85. allocate(size_type __n, const void* = static_cast<const void*>(0))
  86. {
  87. if (__n > this->_M_max_size())
  88. std::__throw_bad_alloc();
  89. #if __cpp_aligned_new
  90. if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
  91. {
  92. std::align_val_t __al = std::align_val_t(alignof(_Tp));
  93. return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), __al));
  94. }
  95. #endif
  96. return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));
  97. }
  98. // __p is not permitted to be a null pointer.
  99. void
  100. deallocate(_Tp* __p, size_type __t)
  101. {
  102. #if __cpp_aligned_new
  103. if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
  104. {
  105. ::operator delete(__p,
  106. # if __cpp_sized_deallocation
  107. __t * sizeof(_Tp),
  108. # endif
  109. std::align_val_t(alignof(_Tp)));
  110. return;
  111. }
  112. #endif
  113. ::operator delete(__p
  114. #if __cpp_sized_deallocation
  115. , __t * sizeof(_Tp)
  116. #endif
  117. );
  118. }
  119. #if __cplusplus <= 201703L
  120. size_type
  121. max_size() const _GLIBCXX_USE_NOEXCEPT
  122. { return _M_max_size(); }
  123. #if __cplusplus >= 201103L
  124. template<typename _Up, typename... _Args>
  125. void
  126. construct(_Up* __p, _Args&&... __args)
  127. noexcept(std::is_nothrow_constructible<_Up, _Args...>::value)
  128. { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
  129. template<typename _Up>
  130. void
  131. destroy(_Up* __p)
  132. noexcept(std::is_nothrow_destructible<_Up>::value)
  133. { __p->~_Up(); }
  134. #else
  135. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  136. // 402. wrong new expression in [some_] allocator::construct
  137. void
  138. construct(pointer __p, const _Tp& __val)
  139. { ::new((void *)__p) _Tp(__val); }
  140. void
  141. destroy(pointer __p) { __p->~_Tp(); }
  142. #endif
  143. #endif // ! C++20
  144. template<typename _Up>
  145. friend _GLIBCXX20_CONSTEXPR bool
  146. operator==(const new_allocator&, const new_allocator<_Up>&)
  147. _GLIBCXX_NOTHROW
  148. { return true; }
  149. #if __cpp_impl_three_way_comparison < 201907L
  150. template<typename _Up>
  151. friend _GLIBCXX20_CONSTEXPR bool
  152. operator!=(const new_allocator&, const new_allocator<_Up>&)
  153. _GLIBCXX_NOTHROW
  154. { return false; }
  155. #endif
  156. private:
  157. _GLIBCXX_CONSTEXPR size_type
  158. _M_max_size() const _GLIBCXX_USE_NOEXCEPT
  159. {
  160. #if __PTRDIFF_MAX__ < __SIZE_MAX__
  161. return std::size_t(__PTRDIFF_MAX__) / sizeof(_Tp);
  162. #else
  163. return std::size_t(-1) / sizeof(_Tp);
  164. #endif
  165. }
  166. };
  167. _GLIBCXX_END_NAMESPACE_VERSION
  168. } // namespace
  169. #endif