scoped_allocator 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. // <scoped_allocator> -*- C++ -*-
  2. // Copyright (C) 2011-2023 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. /** @file include/scoped_allocator
  21. * This is a Standard C++ Library header.
  22. */
  23. #ifndef _SCOPED_ALLOCATOR
  24. #define _SCOPED_ALLOCATOR 1
  25. #pragma GCC system_header
  26. #if __cplusplus < 201103L
  27. # include <bits/c++0x_warning.h>
  28. #else
  29. #include <tuple>
  30. #include <bits/alloc_traits.h>
  31. #include <bits/stl_pair.h>
  32. #include <bits/uses_allocator.h>
  33. #if __cplusplus > 201703L
  34. # include <bits/uses_allocator_args.h>
  35. #endif
  36. namespace std _GLIBCXX_VISIBILITY(default)
  37. {
  38. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  39. /**
  40. * @addtogroup allocators
  41. * @{
  42. */
  43. template<typename _OuterAlloc, typename... _InnerAllocs>
  44. class scoped_allocator_adaptor;
  45. /// @cond undocumented
  46. template<typename _Alloc>
  47. using __outer_allocator_t
  48. = decltype(std::declval<_Alloc>().outer_allocator());
  49. template<typename _Alloc, typename = void>
  50. struct __outermost_type
  51. {
  52. using type = _Alloc;
  53. static type& _S_outermost(_Alloc& __a) noexcept { return __a; }
  54. };
  55. template<typename _Alloc>
  56. struct __outermost_type<_Alloc, __void_t<__outer_allocator_t<_Alloc>>>
  57. : __outermost_type<
  58. typename remove_reference<__outer_allocator_t<_Alloc>>::type
  59. >
  60. {
  61. using __base = __outermost_type<
  62. typename remove_reference<__outer_allocator_t<_Alloc>>::type
  63. >;
  64. static typename __base::type&
  65. _S_outermost(_Alloc& __a) noexcept
  66. { return __base::_S_outermost(__a.outer_allocator()); }
  67. };
  68. // Implementation of the OUTERMOST pseudofunction
  69. template<typename _Alloc>
  70. inline typename __outermost_type<_Alloc>::type&
  71. __outermost(_Alloc& __a)
  72. { return __outermost_type<_Alloc>::_S_outermost(__a); }
  73. template<typename...>
  74. struct __inner_type_impl;
  75. template<typename _Outer>
  76. struct __inner_type_impl<_Outer>
  77. {
  78. typedef scoped_allocator_adaptor<_Outer> __type;
  79. __inner_type_impl() = default;
  80. __inner_type_impl(const __inner_type_impl&) = default;
  81. __inner_type_impl(__inner_type_impl&&) = default;
  82. __inner_type_impl& operator=(const __inner_type_impl&) = default;
  83. __inner_type_impl& operator=(__inner_type_impl&&) = default;
  84. template<typename _Alloc>
  85. __inner_type_impl(const __inner_type_impl<_Alloc>&) noexcept
  86. { }
  87. template<typename _Alloc>
  88. __inner_type_impl(__inner_type_impl<_Alloc>&&) noexcept
  89. { }
  90. __type&
  91. _M_get(__type* __p) noexcept { return *__p; }
  92. const __type&
  93. _M_get(const __type* __p) const noexcept { return *__p; }
  94. tuple<>
  95. _M_tie() const noexcept { return tuple<>(); }
  96. bool
  97. operator==(const __inner_type_impl&) const noexcept
  98. { return true; }
  99. };
  100. template<typename _Outer, typename _InnerHead, typename... _InnerTail>
  101. struct __inner_type_impl<_Outer, _InnerHead, _InnerTail...>
  102. {
  103. typedef scoped_allocator_adaptor<_InnerHead, _InnerTail...> __type;
  104. __inner_type_impl() = default;
  105. __inner_type_impl(const __inner_type_impl&) = default;
  106. __inner_type_impl(__inner_type_impl&&) = default;
  107. __inner_type_impl& operator=(const __inner_type_impl&) = default;
  108. __inner_type_impl& operator=(__inner_type_impl&&) = default;
  109. template<typename... _Allocs>
  110. __inner_type_impl(const __inner_type_impl<_Allocs...>& __other) noexcept
  111. : _M_inner(__other._M_inner) { }
  112. template<typename... _Allocs>
  113. __inner_type_impl(__inner_type_impl<_Allocs...>&& __other) noexcept
  114. : _M_inner(std::move(__other._M_inner)) { }
  115. template<typename... _Args>
  116. explicit
  117. __inner_type_impl(_Args&&... __args) noexcept
  118. : _M_inner(std::forward<_Args>(__args)...) { }
  119. __type&
  120. _M_get(void*) noexcept { return _M_inner; }
  121. const __type&
  122. _M_get(const void*) const noexcept { return _M_inner; }
  123. tuple<const _InnerHead&, const _InnerTail&...>
  124. _M_tie() const noexcept
  125. { return _M_inner._M_tie(); }
  126. bool
  127. operator==(const __inner_type_impl& __other) const noexcept
  128. { return _M_inner == __other._M_inner; }
  129. private:
  130. template<typename...> friend struct __inner_type_impl;
  131. template<typename, typename...> friend class scoped_allocator_adaptor;
  132. __type _M_inner;
  133. };
  134. /// @endcond
  135. /// An adaptor to recursively pass an allocator to the objects it constructs
  136. template<typename _OuterAlloc, typename... _InnerAllocs>
  137. class scoped_allocator_adaptor
  138. : public _OuterAlloc
  139. {
  140. typedef allocator_traits<_OuterAlloc> __traits;
  141. typedef __inner_type_impl<_OuterAlloc, _InnerAllocs...> __inner_type;
  142. __inner_type _M_inner;
  143. template<typename _Outer, typename... _Inner>
  144. friend class scoped_allocator_adaptor;
  145. template<typename...>
  146. friend struct __inner_type_impl;
  147. tuple<const _OuterAlloc&, const _InnerAllocs&...>
  148. _M_tie() const noexcept
  149. { return std::tuple_cat(std::tie(outer_allocator()), _M_inner._M_tie()); }
  150. template<typename _Alloc>
  151. using __outermost_alloc_traits
  152. = allocator_traits<typename __outermost_type<_Alloc>::type>;
  153. #if ! __cpp_lib_make_obj_using_allocator
  154. template<typename _Tp, typename... _Args>
  155. void
  156. _M_construct(__uses_alloc0, _Tp* __p, _Args&&... __args)
  157. {
  158. typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
  159. _O_traits::construct(__outermost(*this), __p,
  160. std::forward<_Args>(__args)...);
  161. }
  162. typedef __uses_alloc1<typename __inner_type::__type> __uses_alloc1_;
  163. typedef __uses_alloc2<typename __inner_type::__type> __uses_alloc2_;
  164. template<typename _Tp, typename... _Args>
  165. void
  166. _M_construct(__uses_alloc1_, _Tp* __p, _Args&&... __args)
  167. {
  168. typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
  169. _O_traits::construct(__outermost(*this), __p,
  170. allocator_arg, inner_allocator(),
  171. std::forward<_Args>(__args)...);
  172. }
  173. template<typename _Tp, typename... _Args>
  174. void
  175. _M_construct(__uses_alloc2_, _Tp* __p, _Args&&... __args)
  176. {
  177. typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
  178. _O_traits::construct(__outermost(*this), __p,
  179. std::forward<_Args>(__args)...,
  180. inner_allocator());
  181. }
  182. #endif // ! make_obj_using_allocator
  183. template<typename _Alloc>
  184. static _Alloc
  185. _S_select_on_copy(const _Alloc& __a)
  186. {
  187. typedef allocator_traits<_Alloc> __a_traits;
  188. return __a_traits::select_on_container_copy_construction(__a);
  189. }
  190. template<std::size_t... _Indices>
  191. scoped_allocator_adaptor(tuple<const _OuterAlloc&,
  192. const _InnerAllocs&...> __refs,
  193. _Index_tuple<_Indices...>)
  194. : _OuterAlloc(_S_select_on_copy(std::get<0>(__refs))),
  195. _M_inner(_S_select_on_copy(std::get<_Indices+1>(__refs))...)
  196. { }
  197. // Used to constrain constructors to disallow invalid conversions.
  198. template<typename _Alloc>
  199. using _Constructible = typename enable_if<
  200. is_constructible<_OuterAlloc, _Alloc>::value
  201. >::type;
  202. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  203. // 2975. Missing case for pair construction in scoped [...] allocators
  204. template<typename _Tp>
  205. struct __not_pair { using type = void; };
  206. template<typename _Tp, typename _Up>
  207. struct __not_pair<pair<_Tp, _Up>> { };
  208. public:
  209. typedef _OuterAlloc outer_allocator_type;
  210. typedef typename __inner_type::__type inner_allocator_type;
  211. typedef typename __traits::value_type value_type;
  212. typedef typename __traits::size_type size_type;
  213. typedef typename __traits::difference_type difference_type;
  214. typedef typename __traits::pointer pointer;
  215. typedef typename __traits::const_pointer const_pointer;
  216. typedef typename __traits::void_pointer void_pointer;
  217. typedef typename __traits::const_void_pointer const_void_pointer;
  218. typedef typename __or_<
  219. typename __traits::propagate_on_container_copy_assignment,
  220. typename allocator_traits<_InnerAllocs>::
  221. propagate_on_container_copy_assignment...>::type
  222. propagate_on_container_copy_assignment;
  223. typedef typename __or_<
  224. typename __traits::propagate_on_container_move_assignment,
  225. typename allocator_traits<_InnerAllocs>::
  226. propagate_on_container_move_assignment...>::type
  227. propagate_on_container_move_assignment;
  228. typedef typename __or_<
  229. typename __traits::propagate_on_container_swap,
  230. typename allocator_traits<_InnerAllocs>::
  231. propagate_on_container_swap...>::type
  232. propagate_on_container_swap;
  233. typedef typename __and_<
  234. typename __traits::is_always_equal,
  235. typename allocator_traits<_InnerAllocs>::is_always_equal...>::type
  236. is_always_equal;
  237. template <class _Tp>
  238. struct rebind
  239. {
  240. typedef scoped_allocator_adaptor<
  241. typename __traits::template rebind_alloc<_Tp>,
  242. _InnerAllocs...> other;
  243. };
  244. scoped_allocator_adaptor() : _OuterAlloc(), _M_inner() { }
  245. template<typename _Outer2, typename = _Constructible<_Outer2>>
  246. scoped_allocator_adaptor(_Outer2&& __outer,
  247. const _InnerAllocs&... __inner) noexcept
  248. : _OuterAlloc(std::forward<_Outer2>(__outer)),
  249. _M_inner(__inner...)
  250. { }
  251. scoped_allocator_adaptor(const scoped_allocator_adaptor& __other) noexcept
  252. : _OuterAlloc(__other.outer_allocator()),
  253. _M_inner(__other._M_inner)
  254. { }
  255. scoped_allocator_adaptor(scoped_allocator_adaptor&& __other) noexcept
  256. : _OuterAlloc(std::move(__other.outer_allocator())),
  257. _M_inner(std::move(__other._M_inner))
  258. { }
  259. template<typename _Outer2, typename = _Constructible<const _Outer2&>>
  260. scoped_allocator_adaptor(
  261. const scoped_allocator_adaptor<_Outer2, _InnerAllocs...>& __other
  262. ) noexcept
  263. : _OuterAlloc(__other.outer_allocator()),
  264. _M_inner(__other._M_inner)
  265. { }
  266. template<typename _Outer2, typename = _Constructible<_Outer2>>
  267. scoped_allocator_adaptor(
  268. scoped_allocator_adaptor<_Outer2, _InnerAllocs...>&& __other) noexcept
  269. : _OuterAlloc(std::move(__other.outer_allocator())),
  270. _M_inner(std::move(__other._M_inner))
  271. { }
  272. scoped_allocator_adaptor&
  273. operator=(const scoped_allocator_adaptor&) = default;
  274. scoped_allocator_adaptor&
  275. operator=(scoped_allocator_adaptor&&) = default;
  276. inner_allocator_type&
  277. inner_allocator() noexcept
  278. { return _M_inner._M_get(this); }
  279. const inner_allocator_type&
  280. inner_allocator() const noexcept
  281. { return _M_inner._M_get(this); }
  282. outer_allocator_type&
  283. outer_allocator() noexcept
  284. { return static_cast<_OuterAlloc&>(*this); }
  285. const outer_allocator_type&
  286. outer_allocator() const noexcept
  287. { return static_cast<const _OuterAlloc&>(*this); }
  288. _GLIBCXX_NODISCARD pointer
  289. allocate(size_type __n)
  290. { return __traits::allocate(outer_allocator(), __n); }
  291. _GLIBCXX_NODISCARD pointer
  292. allocate(size_type __n, const_void_pointer __hint)
  293. { return __traits::allocate(outer_allocator(), __n, __hint); }
  294. void deallocate(pointer __p, size_type __n) noexcept
  295. { return __traits::deallocate(outer_allocator(), __p, __n); }
  296. size_type max_size() const
  297. { return __traits::max_size(outer_allocator()); }
  298. #if ! __cpp_lib_make_obj_using_allocator
  299. template<typename _Tp, typename... _Args>
  300. typename __not_pair<_Tp>::type
  301. construct(_Tp* __p, _Args&&... __args)
  302. {
  303. auto& __inner = inner_allocator();
  304. auto __use_tag
  305. = std::__use_alloc<_Tp, inner_allocator_type, _Args...>(__inner);
  306. _M_construct(__use_tag, __p, std::forward<_Args>(__args)...);
  307. }
  308. template<typename _T1, typename _T2, typename... _Args1,
  309. typename... _Args2>
  310. void
  311. construct(pair<_T1, _T2>* __p, piecewise_construct_t,
  312. tuple<_Args1...> __x, tuple<_Args2...> __y)
  313. {
  314. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  315. // 2203. wrong argument types for piecewise construction
  316. auto& __inner = inner_allocator();
  317. auto __x_use_tag
  318. = std::__use_alloc<_T1, inner_allocator_type, _Args1...>(__inner);
  319. auto __y_use_tag
  320. = std::__use_alloc<_T2, inner_allocator_type, _Args2...>(__inner);
  321. typename _Build_index_tuple<sizeof...(_Args1)>::__type __x_indices;
  322. typename _Build_index_tuple<sizeof...(_Args2)>::__type __y_indices;
  323. typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
  324. _O_traits::construct(__outermost(*this), __p, piecewise_construct,
  325. _M_construct_p(__x_use_tag, __x_indices, __x),
  326. _M_construct_p(__y_use_tag, __y_indices, __y));
  327. }
  328. template<typename _T1, typename _T2>
  329. void
  330. construct(pair<_T1, _T2>* __p)
  331. { construct(__p, piecewise_construct, tuple<>(), tuple<>()); }
  332. template<typename _T1, typename _T2, typename _Up, typename _Vp>
  333. void
  334. construct(pair<_T1, _T2>* __p, _Up&& __u, _Vp&& __v)
  335. {
  336. construct(__p, piecewise_construct,
  337. std::forward_as_tuple(std::forward<_Up>(__u)),
  338. std::forward_as_tuple(std::forward<_Vp>(__v)));
  339. }
  340. template<typename _T1, typename _T2, typename _Up, typename _Vp>
  341. void
  342. construct(pair<_T1, _T2>* __p, const pair<_Up, _Vp>& __x)
  343. {
  344. construct(__p, piecewise_construct,
  345. std::forward_as_tuple(__x.first),
  346. std::forward_as_tuple(__x.second));
  347. }
  348. template<typename _T1, typename _T2, typename _Up, typename _Vp>
  349. void
  350. construct(pair<_T1, _T2>* __p, pair<_Up, _Vp>&& __x)
  351. {
  352. construct(__p, piecewise_construct,
  353. std::forward_as_tuple(std::forward<_Up>(__x.first)),
  354. std::forward_as_tuple(std::forward<_Vp>(__x.second)));
  355. }
  356. #else // make_obj_using_allocator
  357. template<typename _Tp, typename... _Args>
  358. __attribute__((__nonnull__))
  359. void
  360. construct(_Tp* __p, _Args&&... __args)
  361. {
  362. typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
  363. std::apply([__p, this](auto&&... __newargs) {
  364. _O_traits::construct(__outermost(*this), __p,
  365. std::forward<decltype(__newargs)>(__newargs)...);
  366. },
  367. uses_allocator_construction_args<_Tp>(inner_allocator(),
  368. std::forward<_Args>(__args)...));
  369. }
  370. #endif
  371. template<typename _Tp>
  372. void destroy(_Tp* __p)
  373. {
  374. typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
  375. _O_traits::destroy(__outermost(*this), __p);
  376. }
  377. scoped_allocator_adaptor
  378. select_on_container_copy_construction() const
  379. {
  380. typedef typename _Build_index_tuple<sizeof...(_InnerAllocs)>::__type
  381. _Indices;
  382. return scoped_allocator_adaptor(_M_tie(), _Indices());
  383. }
  384. template <typename _OutA1, typename _OutA2, typename... _InA>
  385. friend bool
  386. operator==(const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
  387. const scoped_allocator_adaptor<_OutA2, _InA...>& __b) noexcept;
  388. private:
  389. #if ! __cpp_lib_make_obj_using_allocator
  390. template<typename _Ind, typename... _Args>
  391. tuple<_Args&&...>
  392. _M_construct_p(__uses_alloc0, _Ind, tuple<_Args...>& __t)
  393. { return std::move(__t); }
  394. template<size_t... _Ind, typename... _Args>
  395. tuple<allocator_arg_t, inner_allocator_type&, _Args&&...>
  396. _M_construct_p(__uses_alloc1_, _Index_tuple<_Ind...>,
  397. tuple<_Args...>& __t)
  398. {
  399. return { allocator_arg, inner_allocator(),
  400. std::get<_Ind>(std::move(__t))...
  401. };
  402. }
  403. template<size_t... _Ind, typename... _Args>
  404. tuple<_Args&&..., inner_allocator_type&>
  405. _M_construct_p(__uses_alloc2_, _Index_tuple<_Ind...>,
  406. tuple<_Args...>& __t)
  407. {
  408. return { std::get<_Ind>(std::move(__t))..., inner_allocator() };
  409. }
  410. #endif // ! make_obj_using_allocator
  411. };
  412. /// @related std::scoped_allocator_adaptor
  413. template <typename _OutA1, typename _OutA2, typename... _InA>
  414. inline bool
  415. operator==(const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
  416. const scoped_allocator_adaptor<_OutA2, _InA...>& __b) noexcept
  417. {
  418. return __a.outer_allocator() == __b.outer_allocator()
  419. && __a._M_inner == __b._M_inner;
  420. }
  421. #if __cpp_impl_three_way_comparison < 201907L
  422. /// @related std::scoped_allocator_adaptor
  423. template <typename _OutA1, typename _OutA2, typename... _InA>
  424. inline bool
  425. operator!=(const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
  426. const scoped_allocator_adaptor<_OutA2, _InA...>& __b) noexcept
  427. { return !(__a == __b); }
  428. #endif
  429. /// @}
  430. _GLIBCXX_END_NAMESPACE_VERSION
  431. } // namespace
  432. #endif // C++11
  433. #endif // _SCOPED_ALLOCATOR