malloc_allocator.h 5.6 KB

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