new_allocator.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Allocator that wraps operator new -*- C++ -*-
  2. // Copyright (C) 2001-2019 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. using std::size_t;
  36. using std::ptrdiff_t;
  37. /**
  38. * @brief An allocator that uses global new, as per [20.4].
  39. * @ingroup allocators
  40. *
  41. * This is precisely the allocator defined in the C++ Standard.
  42. * - all allocation calls operator new
  43. * - all deallocation calls operator delete
  44. *
  45. * @tparam _Tp Type of allocated object.
  46. */
  47. template<typename _Tp>
  48. class new_allocator
  49. {
  50. public:
  51. typedef size_t size_type;
  52. typedef ptrdiff_t difference_type;
  53. typedef _Tp* pointer;
  54. typedef const _Tp* const_pointer;
  55. typedef _Tp& reference;
  56. typedef const _Tp& const_reference;
  57. typedef _Tp value_type;
  58. template<typename _Tp1>
  59. struct rebind
  60. { typedef new_allocator<_Tp1> other; };
  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. ~new_allocator() _GLIBCXX_USE_NOEXCEPT { }
  74. pointer
  75. address(reference __x) const _GLIBCXX_NOEXCEPT
  76. { return std::__addressof(__x); }
  77. const_pointer
  78. address(const_reference __x) const _GLIBCXX_NOEXCEPT
  79. { return std::__addressof(__x); }
  80. // NB: __n is permitted to be 0. The C++ standard says nothing
  81. // about what the return value is when __n == 0.
  82. _GLIBCXX_NODISCARD pointer
  83. allocate(size_type __n, const void* = static_cast<const void*>(0))
  84. {
  85. if (__n > this->max_size())
  86. std::__throw_bad_alloc();
  87. #if __cpp_aligned_new
  88. if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
  89. {
  90. std::align_val_t __al = std::align_val_t(alignof(_Tp));
  91. return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), __al));
  92. }
  93. #endif
  94. return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));
  95. }
  96. // __p is not permitted to be a null pointer.
  97. void
  98. deallocate(pointer __p, size_type)
  99. {
  100. #if __cpp_aligned_new
  101. if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
  102. {
  103. ::operator delete(__p, std::align_val_t(alignof(_Tp)));
  104. return;
  105. }
  106. #endif
  107. ::operator delete(__p);
  108. }
  109. size_type
  110. max_size() const _GLIBCXX_USE_NOEXCEPT
  111. {
  112. #if __PTRDIFF_MAX__ < __SIZE_MAX__
  113. return size_t(__PTRDIFF_MAX__) / sizeof(_Tp);
  114. #else
  115. return size_t(-1) / sizeof(_Tp);
  116. #endif
  117. }
  118. #if __cplusplus >= 201103L
  119. template<typename _Up, typename... _Args>
  120. void
  121. construct(_Up* __p, _Args&&... __args)
  122. noexcept(noexcept(::new((void *)__p)
  123. _Up(std::forward<_Args>(__args)...)))
  124. { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
  125. template<typename _Up>
  126. void
  127. destroy(_Up* __p)
  128. noexcept(noexcept( __p->~_Up()))
  129. { __p->~_Up(); }
  130. #else
  131. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  132. // 402. wrong new expression in [some_] allocator::construct
  133. void
  134. construct(pointer __p, const _Tp& __val)
  135. { ::new((void *)__p) _Tp(__val); }
  136. void
  137. destroy(pointer __p) { __p->~_Tp(); }
  138. #endif
  139. template<typename _Up>
  140. friend bool
  141. operator==(const new_allocator&, const new_allocator<_Up>&)
  142. _GLIBCXX_NOTHROW
  143. { return true; }
  144. template<typename _Up>
  145. friend bool
  146. operator!=(const new_allocator&, const new_allocator<_Up>&)
  147. _GLIBCXX_NOTHROW
  148. { return false; }
  149. };
  150. _GLIBCXX_END_NAMESPACE_VERSION
  151. } // namespace
  152. #endif