allocator.h 9.4 KB

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