allocator.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. {
  152. if (__builtin_mul_overflow(__n, sizeof(_Tp), &__n))
  153. std::__throw_bad_array_new_length();
  154. return static_cast<_Tp*>(::operator new(__n));
  155. }
  156. return __allocator_base<_Tp>::allocate(__n, 0);
  157. }
  158. [[__gnu__::__always_inline__]]
  159. constexpr void
  160. deallocate(_Tp* __p, size_t __n)
  161. {
  162. if (std::__is_constant_evaluated())
  163. {
  164. ::operator delete(__p);
  165. return;
  166. }
  167. __allocator_base<_Tp>::deallocate(__p, __n);
  168. }
  169. #endif // C++20
  170. friend _GLIBCXX20_CONSTEXPR bool
  171. operator==(const allocator&, const allocator&) _GLIBCXX_NOTHROW
  172. { return true; }
  173. #if __cpp_impl_three_way_comparison < 201907L
  174. friend _GLIBCXX20_CONSTEXPR bool
  175. operator!=(const allocator&, const allocator&) _GLIBCXX_NOTHROW
  176. { return false; }
  177. #endif
  178. // Inherit everything else.
  179. };
  180. template<typename _T1, typename _T2>
  181. inline _GLIBCXX20_CONSTEXPR bool
  182. operator==(const allocator<_T1>&, const allocator<_T2>&)
  183. _GLIBCXX_NOTHROW
  184. { return true; }
  185. #if __cpp_impl_three_way_comparison < 201907L
  186. template<typename _T1, typename _T2>
  187. inline _GLIBCXX20_CONSTEXPR bool
  188. operator!=(const allocator<_T1>&, const allocator<_T2>&)
  189. _GLIBCXX_NOTHROW
  190. { return false; }
  191. #endif
  192. // Invalid allocator<cv T> partial specializations.
  193. // allocator_traits::rebind_alloc can be used to form a valid allocator type.
  194. template<typename _Tp>
  195. class allocator<const _Tp>
  196. {
  197. public:
  198. typedef _Tp value_type;
  199. template<typename _Up> allocator(const allocator<_Up>&) { }
  200. };
  201. template<typename _Tp>
  202. class allocator<volatile _Tp>
  203. {
  204. public:
  205. typedef _Tp value_type;
  206. template<typename _Up> allocator(const allocator<_Up>&) { }
  207. };
  208. template<typename _Tp>
  209. class allocator<const volatile _Tp>
  210. {
  211. public:
  212. typedef _Tp value_type;
  213. template<typename _Up> allocator(const allocator<_Up>&) { }
  214. };
  215. /// @} group allocator
  216. // Inhibit implicit instantiations for required instantiations,
  217. // which are defined via explicit instantiations elsewhere.
  218. #if _GLIBCXX_EXTERN_TEMPLATE
  219. extern template class allocator<char>;
  220. extern template class allocator<wchar_t>;
  221. #endif
  222. // Undefine.
  223. #undef __allocator_base
  224. // To implement Option 3 of DR 431.
  225. template<typename _Alloc, bool = __is_empty(_Alloc)>
  226. struct __alloc_swap
  227. { static void _S_do_it(_Alloc&, _Alloc&) _GLIBCXX_NOEXCEPT { } };
  228. template<typename _Alloc>
  229. struct __alloc_swap<_Alloc, false>
  230. {
  231. static void
  232. _S_do_it(_Alloc& __one, _Alloc& __two) _GLIBCXX_NOEXCEPT
  233. {
  234. // Precondition: swappable allocators.
  235. if (__one != __two)
  236. swap(__one, __two);
  237. }
  238. };
  239. // Optimize for stateless allocators.
  240. template<typename _Alloc, bool = __is_empty(_Alloc)>
  241. struct __alloc_neq
  242. {
  243. static bool
  244. _S_do_it(const _Alloc&, const _Alloc&)
  245. { return false; }
  246. };
  247. template<typename _Alloc>
  248. struct __alloc_neq<_Alloc, false>
  249. {
  250. static bool
  251. _S_do_it(const _Alloc& __one, const _Alloc& __two)
  252. { return __one != __two; }
  253. };
  254. #if __cplusplus >= 201103L
  255. template<typename _Tp, bool
  256. = __or_<is_copy_constructible<typename _Tp::value_type>,
  257. is_nothrow_move_constructible<typename _Tp::value_type>>::value>
  258. struct __shrink_to_fit_aux
  259. { static bool _S_do_it(_Tp&) noexcept { return false; } };
  260. template<typename _Tp>
  261. struct __shrink_to_fit_aux<_Tp, true>
  262. {
  263. _GLIBCXX20_CONSTEXPR
  264. static bool
  265. _S_do_it(_Tp& __c) noexcept
  266. {
  267. #if __cpp_exceptions
  268. try
  269. {
  270. _Tp(__make_move_if_noexcept_iterator(__c.begin()),
  271. __make_move_if_noexcept_iterator(__c.end()),
  272. __c.get_allocator()).swap(__c);
  273. return true;
  274. }
  275. catch(...)
  276. { return false; }
  277. #else
  278. return false;
  279. #endif
  280. }
  281. };
  282. #endif
  283. _GLIBCXX_END_NAMESPACE_VERSION
  284. } // namespace std
  285. #endif