allocator.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___MEMORY_ALLOCATOR_H
  10. #define _LIBCPP___MEMORY_ALLOCATOR_H
  11. #include <__config>
  12. #include <__memory/allocator_traits.h>
  13. #include <__utility/forward.h>
  14. #include <cstddef>
  15. #include <new>
  16. #include <stdexcept>
  17. #include <type_traits>
  18. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  19. # pragma GCC system_header
  20. #endif
  21. _LIBCPP_BEGIN_NAMESPACE_STD
  22. template <class _Tp> class allocator;
  23. #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
  24. template <>
  25. class _LIBCPP_TEMPLATE_VIS allocator<void>
  26. {
  27. public:
  28. _LIBCPP_DEPRECATED_IN_CXX17 typedef void* pointer;
  29. _LIBCPP_DEPRECATED_IN_CXX17 typedef const void* const_pointer;
  30. _LIBCPP_DEPRECATED_IN_CXX17 typedef void value_type;
  31. template <class _Up> struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {typedef allocator<_Up> other;};
  32. };
  33. template <>
  34. class _LIBCPP_TEMPLATE_VIS allocator<const void>
  35. {
  36. public:
  37. _LIBCPP_DEPRECATED_IN_CXX17 typedef const void* pointer;
  38. _LIBCPP_DEPRECATED_IN_CXX17 typedef const void* const_pointer;
  39. _LIBCPP_DEPRECATED_IN_CXX17 typedef const void value_type;
  40. template <class _Up> struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {typedef allocator<_Up> other;};
  41. };
  42. #endif
  43. // This class provides a non-trivial default constructor to the class that derives from it
  44. // if the condition is satisfied.
  45. //
  46. // The second template parameter exists to allow giving a unique type to __non_trivial_if,
  47. // which makes it possible to avoid breaking the ABI when making this a base class of an
  48. // existing class. Without that, imagine we have classes D1 and D2, both of which used to
  49. // have no base classes, but which now derive from __non_trivial_if. The layout of a class
  50. // that inherits from both D1 and D2 will change because the two __non_trivial_if base
  51. // classes are not allowed to share the same address.
  52. //
  53. // By making those __non_trivial_if base classes unique, we work around this problem and
  54. // it is safe to start deriving from __non_trivial_if in existing classes.
  55. template <bool _Cond, class _Unique>
  56. struct __non_trivial_if { };
  57. template <class _Unique>
  58. struct __non_trivial_if<true, _Unique> {
  59. _LIBCPP_INLINE_VISIBILITY
  60. _LIBCPP_CONSTEXPR __non_trivial_if() _NOEXCEPT { }
  61. };
  62. // allocator
  63. //
  64. // Note: For ABI compatibility between C++20 and previous standards, we make
  65. // allocator<void> trivial in C++20.
  66. template <class _Tp>
  67. class _LIBCPP_TEMPLATE_VIS allocator
  68. : private __non_trivial_if<!is_void<_Tp>::value, allocator<_Tp> >
  69. {
  70. static_assert(!is_volatile<_Tp>::value, "std::allocator does not support volatile types");
  71. public:
  72. typedef size_t size_type;
  73. typedef ptrdiff_t difference_type;
  74. typedef _Tp value_type;
  75. typedef true_type propagate_on_container_move_assignment;
  76. typedef true_type is_always_equal;
  77. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  78. allocator() _NOEXCEPT = default;
  79. template <class _Up>
  80. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  81. allocator(const allocator<_Up>&) _NOEXCEPT { }
  82. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  83. _Tp* allocate(size_t __n) {
  84. if (__n > allocator_traits<allocator>::max_size(*this))
  85. __throw_bad_array_new_length();
  86. if (__libcpp_is_constant_evaluated()) {
  87. return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));
  88. } else {
  89. return static_cast<_Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
  90. }
  91. }
  92. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  93. void deallocate(_Tp* __p, size_t __n) _NOEXCEPT {
  94. if (__libcpp_is_constant_evaluated()) {
  95. ::operator delete((void*)__p);
  96. } else {
  97. _VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
  98. }
  99. }
  100. // C++20 Removed members
  101. #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
  102. _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp* pointer;
  103. _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
  104. _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp& reference;
  105. _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
  106. template <class _Up>
  107. struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
  108. typedef allocator<_Up> other;
  109. };
  110. _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
  111. pointer address(reference __x) const _NOEXCEPT {
  112. return _VSTD::addressof(__x);
  113. }
  114. _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
  115. const_pointer address(const_reference __x) const _NOEXCEPT {
  116. return _VSTD::addressof(__x);
  117. }
  118. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
  119. _Tp* allocate(size_t __n, const void*) {
  120. return allocate(__n);
  121. }
  122. _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT {
  123. return size_type(~0) / sizeof(_Tp);
  124. }
  125. template <class _Up, class... _Args>
  126. _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
  127. void construct(_Up* __p, _Args&&... __args) {
  128. ::new ((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
  129. }
  130. _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
  131. void destroy(pointer __p) {
  132. __p->~_Tp();
  133. }
  134. #endif
  135. };
  136. template <class _Tp>
  137. class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
  138. : private __non_trivial_if<!is_void<_Tp>::value, allocator<const _Tp> >
  139. {
  140. static_assert(!is_volatile<_Tp>::value, "std::allocator does not support volatile types");
  141. public:
  142. typedef size_t size_type;
  143. typedef ptrdiff_t difference_type;
  144. typedef const _Tp value_type;
  145. typedef true_type propagate_on_container_move_assignment;
  146. typedef true_type is_always_equal;
  147. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  148. allocator() _NOEXCEPT = default;
  149. template <class _Up>
  150. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  151. allocator(const allocator<_Up>&) _NOEXCEPT { }
  152. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  153. const _Tp* allocate(size_t __n) {
  154. if (__n > allocator_traits<allocator>::max_size(*this))
  155. __throw_bad_array_new_length();
  156. if (__libcpp_is_constant_evaluated()) {
  157. return static_cast<const _Tp*>(::operator new(__n * sizeof(_Tp)));
  158. } else {
  159. return static_cast<const _Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
  160. }
  161. }
  162. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  163. void deallocate(const _Tp* __p, size_t __n) {
  164. if (__libcpp_is_constant_evaluated()) {
  165. ::operator delete(const_cast<_Tp*>(__p));
  166. } else {
  167. _VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
  168. }
  169. }
  170. // C++20 Removed members
  171. #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
  172. _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* pointer;
  173. _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
  174. _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& reference;
  175. _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
  176. template <class _Up>
  177. struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
  178. typedef allocator<_Up> other;
  179. };
  180. _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
  181. const_pointer address(const_reference __x) const _NOEXCEPT {
  182. return _VSTD::addressof(__x);
  183. }
  184. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
  185. const _Tp* allocate(size_t __n, const void*) {
  186. return allocate(__n);
  187. }
  188. _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT {
  189. return size_type(~0) / sizeof(_Tp);
  190. }
  191. template <class _Up, class... _Args>
  192. _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
  193. void construct(_Up* __p, _Args&&... __args) {
  194. ::new ((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
  195. }
  196. _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
  197. void destroy(pointer __p) {
  198. __p->~_Tp();
  199. }
  200. #endif
  201. };
  202. template <class _Tp, class _Up>
  203. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  204. bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
  205. template <class _Tp, class _Up>
  206. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  207. bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
  208. _LIBCPP_END_NAMESPACE_STD
  209. #endif // _LIBCPP___MEMORY_ALLOCATOR_H