new_allocator.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Allocator that wraps operator new -*- C++ -*-
  2. // Copyright (C) 2001-2018 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. new_allocator() _GLIBCXX_USE_NOEXCEPT { }
  67. new_allocator(const new_allocator&) _GLIBCXX_USE_NOEXCEPT { }
  68. template<typename _Tp1>
  69. new_allocator(const new_allocator<_Tp1>&) _GLIBCXX_USE_NOEXCEPT { }
  70. ~new_allocator() _GLIBCXX_USE_NOEXCEPT { }
  71. pointer
  72. address(reference __x) const _GLIBCXX_NOEXCEPT
  73. { return std::__addressof(__x); }
  74. const_pointer
  75. address(const_reference __x) const _GLIBCXX_NOEXCEPT
  76. { return std::__addressof(__x); }
  77. // NB: __n is permitted to be 0. The C++ standard says nothing
  78. // about what the return value is when __n == 0.
  79. pointer
  80. allocate(size_type __n, const void* = static_cast<const void*>(0))
  81. {
  82. if (__n > this->max_size())
  83. std::__throw_bad_alloc();
  84. #if __cpp_aligned_new
  85. if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
  86. {
  87. std::align_val_t __al = std::align_val_t(alignof(_Tp));
  88. return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), __al));
  89. }
  90. #endif
  91. return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));
  92. }
  93. // __p is not permitted to be a null pointer.
  94. void
  95. deallocate(pointer __p, size_type)
  96. {
  97. #if __cpp_aligned_new
  98. if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
  99. {
  100. ::operator delete(__p, std::align_val_t(alignof(_Tp)));
  101. return;
  102. }
  103. #endif
  104. ::operator delete(__p);
  105. }
  106. size_type
  107. max_size() const _GLIBCXX_USE_NOEXCEPT
  108. { return size_t(-1) / sizeof(_Tp); }
  109. #if __cplusplus >= 201103L
  110. template<typename _Up, typename... _Args>
  111. void
  112. construct(_Up* __p, _Args&&... __args)
  113. { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
  114. template<typename _Up>
  115. void
  116. destroy(_Up* __p) { __p->~_Up(); }
  117. #else
  118. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  119. // 402. wrong new expression in [some_] allocator::construct
  120. void
  121. construct(pointer __p, const _Tp& __val)
  122. { ::new((void *)__p) _Tp(__val); }
  123. void
  124. destroy(pointer __p) { __p->~_Tp(); }
  125. #endif
  126. };
  127. template<typename _Tp>
  128. inline bool
  129. operator==(const new_allocator<_Tp>&, const new_allocator<_Tp>&)
  130. { return true; }
  131. template<typename _Tp>
  132. inline bool
  133. operator!=(const new_allocator<_Tp>&, const new_allocator<_Tp>&)
  134. { return false; }
  135. _GLIBCXX_END_NAMESPACE_VERSION
  136. } // namespace
  137. #endif