malloc_allocator.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Allocator that wraps "C" malloc -*- 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/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. malloc_allocator() _GLIBCXX_USE_NOEXCEPT { }
  66. malloc_allocator(const malloc_allocator&) _GLIBCXX_USE_NOEXCEPT { }
  67. template<typename _Tp1>
  68. malloc_allocator(const malloc_allocator<_Tp1>&)
  69. _GLIBCXX_USE_NOEXCEPT { }
  70. ~malloc_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* = 0)
  81. {
  82. if (__n > this->max_size())
  83. std::__throw_bad_alloc();
  84. pointer __ret = 0;
  85. #if __cpp_aligned_new
  86. #if __cplusplus > 201402L && _GLIBCXX_HAVE_ALIGNED_ALLOC
  87. if (alignof(_Tp) > alignof(std::max_align_t))
  88. {
  89. __ret = static_cast<_Tp*>(::aligned_alloc(alignof(_Tp),
  90. __n * sizeof(_Tp)));
  91. }
  92. #else
  93. # define _GLIBCXX_CHECK_MALLOC_RESULT
  94. #endif
  95. #endif
  96. if (!__ret)
  97. __ret = static_cast<_Tp*>(std::malloc(__n * sizeof(_Tp)));
  98. if (!__ret)
  99. std::__throw_bad_alloc();
  100. #ifdef _GLIBCXX_CHECK_MALLOC_RESULT
  101. #undef _GLIBCXX_CHECK_MALLOC_RESULT
  102. if (reinterpret_cast<std::size_t>(__ret) % alignof(_Tp))
  103. {
  104. // Memory returned by malloc is not suitably aligned for _Tp.
  105. deallocate(__ret, __n);
  106. std::__throw_bad_alloc();
  107. }
  108. #endif
  109. return __ret;
  110. }
  111. // __p is not permitted to be a null pointer.
  112. void
  113. deallocate(pointer __p, size_type)
  114. { std::free(static_cast<void*>(__p)); }
  115. size_type
  116. max_size() const _GLIBCXX_USE_NOEXCEPT
  117. { return size_t(-1) / sizeof(_Tp); }
  118. #if __cplusplus >= 201103L
  119. template<typename _Up, typename... _Args>
  120. void
  121. construct(_Up* __p, _Args&&... __args)
  122. { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
  123. template<typename _Up>
  124. void
  125. destroy(_Up* __p) { __p->~_Up(); }
  126. #else
  127. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  128. // 402. wrong new expression in [some_] allocator::construct
  129. void
  130. construct(pointer __p, const _Tp& __val)
  131. { ::new((void *)__p) value_type(__val); }
  132. void
  133. destroy(pointer __p) { __p->~_Tp(); }
  134. #endif
  135. };
  136. template<typename _Tp>
  137. inline bool
  138. operator==(const malloc_allocator<_Tp>&, const malloc_allocator<_Tp>&)
  139. { return true; }
  140. template<typename _Tp>
  141. inline bool
  142. operator!=(const malloc_allocator<_Tp>&, const malloc_allocator<_Tp>&)
  143. { return false; }
  144. _GLIBCXX_END_NAMESPACE_VERSION
  145. } // namespace
  146. #endif