scoped_allocator 17 KB

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