new_allocator.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Allocator that wraps operator new -*- C++ -*-
  2. // Copyright (C) 2001-2021 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 (__builtin_expect(__n > this->_M_max_size(), false))
  88. {
  89. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  90. // 3190. allocator::allocate sometimes returns too little storage
  91. if (__n > (std::size_t(-1) / sizeof(_Tp)))
  92. std::__throw_bad_array_new_length();
  93. std::__throw_bad_alloc();
  94. }
  95. #if __cpp_aligned_new
  96. if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
  97. {
  98. std::align_val_t __al = std::align_val_t(alignof(_Tp));
  99. return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), __al));
  100. }
  101. #endif
  102. return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));
  103. }
  104. // __p is not permitted to be a null pointer.
  105. void
  106. deallocate(_Tp* __p, size_type __t __attribute__ ((__unused__)))
  107. {
  108. #if __cpp_aligned_new
  109. if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
  110. {
  111. ::operator delete(__p,
  112. # if __cpp_sized_deallocation
  113. __t * sizeof(_Tp),
  114. # endif
  115. std::align_val_t(alignof(_Tp)));
  116. return;
  117. }
  118. #endif
  119. ::operator delete(__p
  120. #if __cpp_sized_deallocation
  121. , __t * sizeof(_Tp)
  122. #endif
  123. );
  124. }
  125. #if __cplusplus <= 201703L
  126. size_type
  127. max_size() const _GLIBCXX_USE_NOEXCEPT
  128. { return _M_max_size(); }
  129. #if __cplusplus >= 201103L
  130. template<typename _Up, typename... _Args>
  131. void
  132. construct(_Up* __p, _Args&&... __args)
  133. noexcept(std::is_nothrow_constructible<_Up, _Args...>::value)
  134. { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
  135. template<typename _Up>
  136. void
  137. destroy(_Up* __p)
  138. noexcept(std::is_nothrow_destructible<_Up>::value)
  139. { __p->~_Up(); }
  140. #else
  141. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  142. // 402. wrong new expression in [some_] allocator::construct
  143. void
  144. construct(pointer __p, const _Tp& __val)
  145. { ::new((void *)__p) _Tp(__val); }
  146. void
  147. destroy(pointer __p) { __p->~_Tp(); }
  148. #endif
  149. #endif // ! C++20
  150. template<typename _Up>
  151. friend _GLIBCXX20_CONSTEXPR bool
  152. operator==(const new_allocator&, const new_allocator<_Up>&)
  153. _GLIBCXX_NOTHROW
  154. { return true; }
  155. #if __cpp_impl_three_way_comparison < 201907L
  156. template<typename _Up>
  157. friend _GLIBCXX20_CONSTEXPR bool
  158. operator!=(const new_allocator&, const new_allocator<_Up>&)
  159. _GLIBCXX_NOTHROW
  160. { return false; }
  161. #endif
  162. private:
  163. _GLIBCXX_CONSTEXPR size_type
  164. _M_max_size() const _GLIBCXX_USE_NOEXCEPT
  165. {
  166. #if __PTRDIFF_MAX__ < __SIZE_MAX__
  167. return std::size_t(__PTRDIFF_MAX__) / sizeof(_Tp);
  168. #else
  169. return std::size_t(-1) / sizeof(_Tp);
  170. #endif
  171. }
  172. };
  173. _GLIBCXX_END_NAMESPACE_VERSION
  174. } // namespace
  175. #endif