allocator.h 9.8 KB

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