malloc_allocator.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // Allocator that wraps "C" malloc -*- C++ -*-
  2. // Copyright (C) 2001-2023 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 <bits/requires_hosted.h> // malloc
  26. #include <cstdlib>
  27. #include <cstddef>
  28. #include <new>
  29. #include <bits/functexcept.h>
  30. #include <bits/move.h>
  31. #if __cplusplus >= 201103L
  32. #include <type_traits>
  33. #endif
  34. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  35. {
  36. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  37. /**
  38. * @brief An allocator that uses malloc.
  39. * @ingroup allocators
  40. *
  41. * This is precisely the allocator defined in the C++ Standard.
  42. * - all allocation calls malloc
  43. * - all deallocation calls free
  44. */
  45. template<typename _Tp>
  46. class malloc_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 malloc_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. malloc_allocator() _GLIBCXX_USE_NOEXCEPT { }
  68. _GLIBCXX20_CONSTEXPR
  69. malloc_allocator(const malloc_allocator&) _GLIBCXX_USE_NOEXCEPT { }
  70. template<typename _Tp1>
  71. _GLIBCXX20_CONSTEXPR
  72. malloc_allocator(const malloc_allocator<_Tp1>&)
  73. _GLIBCXX_USE_NOEXCEPT { }
  74. #if __cplusplus <= 201703L
  75. ~malloc_allocator() _GLIBCXX_USE_NOEXCEPT { }
  76. pointer
  77. address(reference __x) const _GLIBCXX_NOEXCEPT
  78. { return std::__addressof(__x); }
  79. const_pointer
  80. address(const_reference __x) const _GLIBCXX_NOEXCEPT
  81. { return std::__addressof(__x); }
  82. #endif
  83. // NB: __n is permitted to be 0. The C++ standard says nothing
  84. // about what the return value is when __n == 0.
  85. _GLIBCXX_NODISCARD _Tp*
  86. allocate(size_type __n, const void* = 0)
  87. {
  88. #if __cplusplus >= 201103L
  89. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  90. // 3308. std::allocator<void>().allocate(n)
  91. static_assert(sizeof(_Tp) != 0, "cannot allocate incomplete types");
  92. #endif
  93. if (__builtin_expect(__n > this->_M_max_size(), false))
  94. {
  95. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  96. // 3190. allocator::allocate sometimes returns too little storage
  97. if (__n > (std::size_t(-1) / sizeof(_Tp)))
  98. std::__throw_bad_array_new_length();
  99. std::__throw_bad_alloc();
  100. }
  101. _Tp* __ret = 0;
  102. #if __cpp_aligned_new
  103. #if __cplusplus > 201402L && _GLIBCXX_HAVE_ALIGNED_ALLOC
  104. if (alignof(_Tp) > alignof(std::max_align_t))
  105. {
  106. __ret = static_cast<_Tp*>(::aligned_alloc(alignof(_Tp),
  107. __n * sizeof(_Tp)));
  108. }
  109. #else
  110. # define _GLIBCXX_CHECK_MALLOC_RESULT
  111. #endif
  112. #endif
  113. if (!__ret)
  114. __ret = static_cast<_Tp*>(std::malloc(__n * sizeof(_Tp)));
  115. if (!__ret)
  116. std::__throw_bad_alloc();
  117. #ifdef _GLIBCXX_CHECK_MALLOC_RESULT
  118. #undef _GLIBCXX_CHECK_MALLOC_RESULT
  119. if (reinterpret_cast<std::size_t>(__ret) % alignof(_Tp))
  120. {
  121. // Memory returned by malloc is not suitably aligned for _Tp.
  122. deallocate(__ret, __n);
  123. std::__throw_bad_alloc();
  124. }
  125. #endif
  126. return __ret;
  127. }
  128. // __p is not permitted to be a null pointer.
  129. void
  130. deallocate(_Tp* __p, size_type)
  131. { std::free(static_cast<void*>(__p)); }
  132. #if __cplusplus <= 201703L
  133. size_type
  134. max_size() const _GLIBCXX_USE_NOEXCEPT
  135. { return _M_max_size(); }
  136. #if __cplusplus >= 201103L
  137. template<typename _Up, typename... _Args>
  138. void
  139. construct(_Up* __p, _Args&&... __args)
  140. noexcept(std::is_nothrow_constructible<_Up, _Args...>::value)
  141. { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
  142. template<typename _Up>
  143. void
  144. destroy(_Up* __p)
  145. noexcept(std::is_nothrow_destructible<_Up>::value)
  146. { __p->~_Up(); }
  147. #else
  148. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  149. // 402. wrong new expression in [some_] allocator::construct
  150. void
  151. construct(pointer __p, const _Tp& __val)
  152. { ::new((void *)__p) value_type(__val); }
  153. void
  154. destroy(pointer __p) { __p->~_Tp(); }
  155. #endif
  156. #endif // ! C++20
  157. template<typename _Up>
  158. friend _GLIBCXX20_CONSTEXPR bool
  159. operator==(const malloc_allocator&, const malloc_allocator<_Up>&)
  160. _GLIBCXX_NOTHROW
  161. { return true; }
  162. #if __cpp_impl_three_way_comparison < 201907L
  163. template<typename _Up>
  164. friend _GLIBCXX20_CONSTEXPR bool
  165. operator!=(const malloc_allocator&, const malloc_allocator<_Up>&)
  166. _GLIBCXX_NOTHROW
  167. { return false; }
  168. #endif
  169. private:
  170. _GLIBCXX_CONSTEXPR size_type
  171. _M_max_size() const _GLIBCXX_USE_NOEXCEPT
  172. {
  173. #if __PTRDIFF_MAX__ < __SIZE_MAX__
  174. return std::size_t(__PTRDIFF_MAX__) / sizeof(_Tp);
  175. #else
  176. return std::size_t(-1) / sizeof(_Tp);
  177. #endif
  178. }
  179. };
  180. _GLIBCXX_END_NAMESPACE_VERSION
  181. } // namespace
  182. #endif