allocator.h 9.9 KB

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