malloc_allocator.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // Allocator that wraps "C" malloc -*- 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/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. using std::size_t;
  37. using std::ptrdiff_t;
  38. /**
  39. * @brief An allocator that uses malloc.
  40. * @ingroup allocators
  41. *
  42. * This is precisely the allocator defined in the C++ Standard.
  43. * - all allocation calls malloc
  44. * - all deallocation calls free
  45. */
  46. template<typename _Tp>
  47. class malloc_allocator
  48. {
  49. public:
  50. typedef size_t size_type;
  51. typedef ptrdiff_t difference_type;
  52. typedef _Tp* pointer;
  53. typedef const _Tp* const_pointer;
  54. typedef _Tp& reference;
  55. typedef const _Tp& const_reference;
  56. typedef _Tp value_type;
  57. template<typename _Tp1>
  58. struct rebind
  59. { typedef malloc_allocator<_Tp1> other; };
  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. ~malloc_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. pointer
  83. allocate(size_type __n, const void* = 0)
  84. {
  85. if (__n > this->max_size())
  86. std::__throw_bad_alloc();
  87. pointer __ret = 0;
  88. #if __cpp_aligned_new
  89. #if __cplusplus > 201402L && _GLIBCXX_HAVE_ALIGNED_ALLOC
  90. if (alignof(_Tp) > alignof(std::max_align_t))
  91. {
  92. __ret = static_cast<_Tp*>(::aligned_alloc(alignof(_Tp),
  93. __n * sizeof(_Tp)));
  94. }
  95. #else
  96. # define _GLIBCXX_CHECK_MALLOC_RESULT
  97. #endif
  98. #endif
  99. if (!__ret)
  100. __ret = static_cast<_Tp*>(std::malloc(__n * sizeof(_Tp)));
  101. if (!__ret)
  102. std::__throw_bad_alloc();
  103. #ifdef _GLIBCXX_CHECK_MALLOC_RESULT
  104. #undef _GLIBCXX_CHECK_MALLOC_RESULT
  105. if (reinterpret_cast<std::size_t>(__ret) % alignof(_Tp))
  106. {
  107. // Memory returned by malloc is not suitably aligned for _Tp.
  108. deallocate(__ret, __n);
  109. std::__throw_bad_alloc();
  110. }
  111. #endif
  112. return __ret;
  113. }
  114. // __p is not permitted to be a null pointer.
  115. void
  116. deallocate(pointer __p, size_type)
  117. { std::free(static_cast<void*>(__p)); }
  118. size_type
  119. max_size() const _GLIBCXX_USE_NOEXCEPT
  120. {
  121. #if __PTRDIFF_MAX__ < __SIZE_MAX__
  122. return size_t(__PTRDIFF_MAX__) / sizeof(_Tp);
  123. #else
  124. return size_t(-1) / sizeof(_Tp);
  125. #endif
  126. }
  127. #if __cplusplus >= 201103L
  128. template<typename _Up, typename... _Args>
  129. void
  130. construct(_Up* __p, _Args&&... __args)
  131. noexcept(noexcept(::new((void *)__p)
  132. _Up(std::forward<_Args>(__args)...)))
  133. { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
  134. template<typename _Up>
  135. void
  136. destroy(_Up* __p)
  137. noexcept(noexcept(__p->~_Up()))
  138. { __p->~_Up(); }
  139. #else
  140. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  141. // 402. wrong new expression in [some_] allocator::construct
  142. void
  143. construct(pointer __p, const _Tp& __val)
  144. { ::new((void *)__p) value_type(__val); }
  145. void
  146. destroy(pointer __p) { __p->~_Tp(); }
  147. #endif
  148. template<typename _Up>
  149. friend bool
  150. operator==(const malloc_allocator&, const malloc_allocator<_Up>&)
  151. _GLIBCXX_NOTHROW
  152. { return true; }
  153. template<typename _Up>
  154. friend bool
  155. operator!=(const malloc_allocator&, const malloc_allocator<_Up>&)
  156. _GLIBCXX_NOTHROW
  157. { return false; }
  158. };
  159. _GLIBCXX_END_NAMESPACE_VERSION
  160. } // namespace
  161. #endif