malloc_allocator.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // Allocator that wraps "C" malloc -*- 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/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 (__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. _Tp* __ret = 0;
  96. #if __cpp_aligned_new
  97. #if __cplusplus > 201402L && _GLIBCXX_HAVE_ALIGNED_ALLOC
  98. if (alignof(_Tp) > alignof(std::max_align_t))
  99. {
  100. __ret = static_cast<_Tp*>(::aligned_alloc(alignof(_Tp),
  101. __n * sizeof(_Tp)));
  102. }
  103. #else
  104. # define _GLIBCXX_CHECK_MALLOC_RESULT
  105. #endif
  106. #endif
  107. if (!__ret)
  108. __ret = static_cast<_Tp*>(std::malloc(__n * sizeof(_Tp)));
  109. if (!__ret)
  110. std::__throw_bad_alloc();
  111. #ifdef _GLIBCXX_CHECK_MALLOC_RESULT
  112. #undef _GLIBCXX_CHECK_MALLOC_RESULT
  113. if (reinterpret_cast<std::size_t>(__ret) % alignof(_Tp))
  114. {
  115. // Memory returned by malloc is not suitably aligned for _Tp.
  116. deallocate(__ret, __n);
  117. std::__throw_bad_alloc();
  118. }
  119. #endif
  120. return __ret;
  121. }
  122. // __p is not permitted to be a null pointer.
  123. void
  124. deallocate(_Tp* __p, size_type)
  125. { std::free(static_cast<void*>(__p)); }
  126. #if __cplusplus <= 201703L
  127. size_type
  128. max_size() const _GLIBCXX_USE_NOEXCEPT
  129. { return _M_max_size(); }
  130. #if __cplusplus >= 201103L
  131. template<typename _Up, typename... _Args>
  132. void
  133. construct(_Up* __p, _Args&&... __args)
  134. noexcept(std::is_nothrow_constructible<_Up, _Args...>::value)
  135. { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
  136. template<typename _Up>
  137. void
  138. destroy(_Up* __p)
  139. noexcept(std::is_nothrow_destructible<_Up>::value)
  140. { __p->~_Up(); }
  141. #else
  142. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  143. // 402. wrong new expression in [some_] allocator::construct
  144. void
  145. construct(pointer __p, const _Tp& __val)
  146. { ::new((void *)__p) value_type(__val); }
  147. void
  148. destroy(pointer __p) { __p->~_Tp(); }
  149. #endif
  150. #endif // ! C++20
  151. template<typename _Up>
  152. friend _GLIBCXX20_CONSTEXPR bool
  153. operator==(const malloc_allocator&, const malloc_allocator<_Up>&)
  154. _GLIBCXX_NOTHROW
  155. { return true; }
  156. #if __cpp_impl_three_way_comparison < 201907L
  157. template<typename _Up>
  158. friend _GLIBCXX20_CONSTEXPR bool
  159. operator!=(const malloc_allocator&, const malloc_allocator<_Up>&)
  160. _GLIBCXX_NOTHROW
  161. { return false; }
  162. #endif
  163. private:
  164. _GLIBCXX_CONSTEXPR size_type
  165. _M_max_size() const _GLIBCXX_USE_NOEXCEPT
  166. {
  167. #if __PTRDIFF_MAX__ < __SIZE_MAX__
  168. return std::size_t(__PTRDIFF_MAX__) / sizeof(_Tp);
  169. #else
  170. return std::size_t(-1) / sizeof(_Tp);
  171. #endif
  172. }
  173. };
  174. _GLIBCXX_END_NAMESPACE_VERSION
  175. } // namespace
  176. #endif