allocator.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. // Allocators -*- C++ -*-
  2. // Copyright (C) 2001-2022 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. /*
  21. * Copyright (c) 1996-1997
  22. * Silicon Graphics Computer Systems, Inc.
  23. *
  24. * Permission to use, copy, modify, distribute and sell this software
  25. * and its documentation for any purpose is hereby granted without fee,
  26. * provided that the above copyright notice appear in all copies and
  27. * that both that copyright notice and this permission notice appear
  28. * in supporting documentation. Silicon Graphics makes no
  29. * representations about the suitability of this software for any
  30. * purpose. It is provided "as is" without express or implied warranty.
  31. */
  32. /** @file bits/allocator.h
  33. * This is an internal header file, included by other library headers.
  34. * Do not attempt to use it directly. @headername{memory}
  35. */
  36. #ifndef _ALLOCATOR_H
  37. #define _ALLOCATOR_H 1
  38. #include <bits/c++allocator.h> // Define the base class to std::allocator.
  39. #include <bits/memoryfwd.h>
  40. #if __cplusplus >= 201103L
  41. #include <type_traits>
  42. #endif
  43. #define __cpp_lib_incomplete_container_elements 201505L
  44. namespace std _GLIBCXX_VISIBILITY(default)
  45. {
  46. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  47. /**
  48. * @addtogroup allocators
  49. * @{
  50. */
  51. // Since C++20 the primary template should be used for allocator<void>,
  52. // but then it would have a non-trivial default ctor and dtor for C++20,
  53. // but trivial for C++98-17, which would be an ABI incompatibiliy between
  54. // different standard dialects. So C++20 still uses the allocator<void>
  55. // explicit specialization, with the historical ABI properties, but with
  56. // the same members that are present in the primary template.
  57. /// allocator<void> specialization.
  58. template<>
  59. class allocator<void>
  60. {
  61. public:
  62. typedef void value_type;
  63. typedef size_t size_type;
  64. typedef ptrdiff_t difference_type;
  65. #if __cplusplus <= 201703L
  66. // These were removed for C++20, allocator_traits does the right thing.
  67. typedef void* pointer;
  68. typedef const void* const_pointer;
  69. template<typename _Tp1>
  70. struct rebind
  71. { typedef allocator<_Tp1> other; };
  72. #endif
  73. #if __cplusplus >= 201103L
  74. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  75. // 2103. std::allocator propagate_on_container_move_assignment
  76. using propagate_on_container_move_assignment = true_type;
  77. using is_always_equal
  78. _GLIBCXX20_DEPRECATED_SUGGEST("std::allocator_traits::is_always_equal")
  79. = true_type;
  80. #if __cplusplus >= 202002L
  81. // As noted above, these members are present for C++20 to provide the
  82. // same API as the primary template, but still trivial as in pre-C++20.
  83. allocator() = default;
  84. ~allocator() = default;
  85. template<typename _Up>
  86. constexpr
  87. allocator(const allocator<_Up>&) noexcept { }
  88. // No allocate member because it's ill-formed by LWG 3307.
  89. // No deallocate member because it would be undefined to call it
  90. // with any pointer which wasn't obtained from allocate.
  91. #endif // C++20
  92. #endif // C++11
  93. };
  94. /**
  95. * @brief The @a standard allocator, as per C++03 [20.4.1].
  96. *
  97. * See https://gcc.gnu.org/onlinedocs/libstdc++/manual/memory.html#std.util.memory.allocator
  98. * for further details.
  99. *
  100. * @tparam _Tp Type of allocated object.
  101. */
  102. template<typename _Tp>
  103. class allocator : public __allocator_base<_Tp>
  104. {
  105. public:
  106. typedef _Tp value_type;
  107. typedef size_t size_type;
  108. typedef ptrdiff_t difference_type;
  109. #if __cplusplus <= 201703L
  110. // These were removed for C++20.
  111. typedef _Tp* pointer;
  112. typedef const _Tp* const_pointer;
  113. typedef _Tp& reference;
  114. typedef const _Tp& const_reference;
  115. template<typename _Tp1>
  116. struct rebind
  117. { typedef allocator<_Tp1> other; };
  118. #endif
  119. #if __cplusplus >= 201103L
  120. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  121. // 2103. std::allocator propagate_on_container_move_assignment
  122. using propagate_on_container_move_assignment = true_type;
  123. using is_always_equal
  124. _GLIBCXX20_DEPRECATED_SUGGEST("std::allocator_traits::is_always_equal")
  125. = true_type;
  126. #endif
  127. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  128. // 3035. std::allocator's constructors should be constexpr
  129. _GLIBCXX20_CONSTEXPR
  130. allocator() _GLIBCXX_NOTHROW { }
  131. _GLIBCXX20_CONSTEXPR
  132. allocator(const allocator& __a) _GLIBCXX_NOTHROW
  133. : __allocator_base<_Tp>(__a) { }
  134. #if __cplusplus >= 201103L
  135. // Avoid implicit deprecation.
  136. allocator& operator=(const allocator&) = default;
  137. #endif
  138. template<typename _Tp1>
  139. _GLIBCXX20_CONSTEXPR
  140. allocator(const allocator<_Tp1>&) _GLIBCXX_NOTHROW { }
  141. #if __cpp_constexpr_dynamic_alloc
  142. constexpr
  143. #endif
  144. ~allocator() _GLIBCXX_NOTHROW { }
  145. #if __cplusplus > 201703L
  146. [[nodiscard,__gnu__::__always_inline__]]
  147. constexpr _Tp*
  148. allocate(size_t __n)
  149. {
  150. if (std::__is_constant_evaluated())
  151. return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));
  152. return __allocator_base<_Tp>::allocate(__n, 0);
  153. }
  154. [[__gnu__::__always_inline__]]
  155. constexpr void
  156. deallocate(_Tp* __p, size_t __n)
  157. {
  158. if (std::__is_constant_evaluated())
  159. {
  160. ::operator delete(__p);
  161. return;
  162. }
  163. __allocator_base<_Tp>::deallocate(__p, __n);
  164. }
  165. #endif // C++20
  166. friend _GLIBCXX20_CONSTEXPR bool
  167. operator==(const allocator&, const allocator&) _GLIBCXX_NOTHROW
  168. { return true; }
  169. #if __cpp_impl_three_way_comparison < 201907L
  170. friend _GLIBCXX20_CONSTEXPR bool
  171. operator!=(const allocator&, const allocator&) _GLIBCXX_NOTHROW
  172. { return false; }
  173. #endif
  174. // Inherit everything else.
  175. };
  176. template<typename _T1, typename _T2>
  177. inline _GLIBCXX20_CONSTEXPR bool
  178. operator==(const allocator<_T1>&, const allocator<_T2>&)
  179. _GLIBCXX_NOTHROW
  180. { return true; }
  181. #if __cpp_impl_three_way_comparison < 201907L
  182. template<typename _T1, typename _T2>
  183. inline _GLIBCXX20_CONSTEXPR bool
  184. operator!=(const allocator<_T1>&, const allocator<_T2>&)
  185. _GLIBCXX_NOTHROW
  186. { return false; }
  187. #endif
  188. // Invalid allocator<cv T> partial specializations.
  189. // allocator_traits::rebind_alloc can be used to form a valid allocator type.
  190. template<typename _Tp>
  191. class allocator<const _Tp>
  192. {
  193. public:
  194. typedef _Tp value_type;
  195. template<typename _Up> allocator(const allocator<_Up>&) { }
  196. };
  197. template<typename _Tp>
  198. class allocator<volatile _Tp>
  199. {
  200. public:
  201. typedef _Tp value_type;
  202. template<typename _Up> allocator(const allocator<_Up>&) { }
  203. };
  204. template<typename _Tp>
  205. class allocator<const volatile _Tp>
  206. {
  207. public:
  208. typedef _Tp value_type;
  209. template<typename _Up> allocator(const allocator<_Up>&) { }
  210. };
  211. /// @} group allocator
  212. // Inhibit implicit instantiations for required instantiations,
  213. // which are defined via explicit instantiations elsewhere.
  214. #if _GLIBCXX_EXTERN_TEMPLATE
  215. extern template class allocator<char>;
  216. extern template class allocator<wchar_t>;
  217. #endif
  218. // Undefine.
  219. #undef __allocator_base
  220. // To implement Option 3 of DR 431.
  221. template<typename _Alloc, bool = __is_empty(_Alloc)>
  222. struct __alloc_swap
  223. { static void _S_do_it(_Alloc&, _Alloc&) _GLIBCXX_NOEXCEPT { } };
  224. template<typename _Alloc>
  225. struct __alloc_swap<_Alloc, false>
  226. {
  227. static void
  228. _S_do_it(_Alloc& __one, _Alloc& __two) _GLIBCXX_NOEXCEPT
  229. {
  230. // Precondition: swappable allocators.
  231. if (__one != __two)
  232. swap(__one, __two);
  233. }
  234. };
  235. // Optimize for stateless allocators.
  236. template<typename _Alloc, bool = __is_empty(_Alloc)>
  237. struct __alloc_neq
  238. {
  239. static bool
  240. _S_do_it(const _Alloc&, const _Alloc&)
  241. { return false; }
  242. };
  243. template<typename _Alloc>
  244. struct __alloc_neq<_Alloc, false>
  245. {
  246. static bool
  247. _S_do_it(const _Alloc& __one, const _Alloc& __two)
  248. { return __one != __two; }
  249. };
  250. #if __cplusplus >= 201103L
  251. template<typename _Tp, bool
  252. = __or_<is_copy_constructible<typename _Tp::value_type>,
  253. is_nothrow_move_constructible<typename _Tp::value_type>>::value>
  254. struct __shrink_to_fit_aux
  255. { static bool _S_do_it(_Tp&) noexcept { return false; } };
  256. template<typename _Tp>
  257. struct __shrink_to_fit_aux<_Tp, true>
  258. {
  259. _GLIBCXX20_CONSTEXPR
  260. static bool
  261. _S_do_it(_Tp& __c) noexcept
  262. {
  263. #if __cpp_exceptions
  264. try
  265. {
  266. _Tp(__make_move_if_noexcept_iterator(__c.begin()),
  267. __make_move_if_noexcept_iterator(__c.end()),
  268. __c.get_allocator()).swap(__c);
  269. return true;
  270. }
  271. catch(...)
  272. { return false; }
  273. #else
  274. return false;
  275. #endif
  276. }
  277. };
  278. #endif
  279. _GLIBCXX_END_NAMESPACE_VERSION
  280. } // namespace std
  281. #endif