memory_resource.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. // <memory_resource> -*- C++ -*-
  2. // Copyright (C) 2018-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/bits/memory_resource.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{memory_resource}
  23. */
  24. #ifndef _GLIBCXX_MEMORY_RESOURCE_H
  25. #define _GLIBCXX_MEMORY_RESOURCE_H 1
  26. #pragma GCC system_header
  27. #if __cplusplus >= 201703L
  28. #include <new> // operator new(size_t, void*)
  29. #include <cstddef> // size_t, max_align_t, byte
  30. #include <bits/functexcept.h> // __throw_bad_array_new_length
  31. #include <bits/uses_allocator.h> // allocator_arg_t, __use_alloc
  32. #include <bits/uses_allocator_args.h> // uninitialized_construct_using_alloc
  33. #include <ext/numeric_traits.h> // __int_traits
  34. #include <debug/assertions.h>
  35. #if ! __cpp_lib_make_obj_using_allocator
  36. # include <bits/utility.h> // index_sequence
  37. # include <tuple> // tuple, forward_as_tuple
  38. #endif
  39. namespace std _GLIBCXX_VISIBILITY(default)
  40. {
  41. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  42. namespace pmr
  43. {
  44. /// Class memory_resource
  45. /**
  46. * @ingroup pmr
  47. * @headerfile memory_resource
  48. * @since C++17
  49. */
  50. class memory_resource
  51. {
  52. static constexpr size_t _S_max_align = alignof(max_align_t);
  53. public:
  54. memory_resource() = default;
  55. memory_resource(const memory_resource&) = default;
  56. virtual ~memory_resource(); // key function
  57. memory_resource& operator=(const memory_resource&) = default;
  58. [[nodiscard]]
  59. void*
  60. allocate(size_t __bytes, size_t __alignment = _S_max_align)
  61. __attribute__((__returns_nonnull__,__alloc_size__(2),__alloc_align__(3)))
  62. { return ::operator new(__bytes, do_allocate(__bytes, __alignment)); }
  63. void
  64. deallocate(void* __p, size_t __bytes, size_t __alignment = _S_max_align)
  65. __attribute__((__nonnull__))
  66. { return do_deallocate(__p, __bytes, __alignment); }
  67. [[nodiscard]]
  68. bool
  69. is_equal(const memory_resource& __other) const noexcept
  70. { return do_is_equal(__other); }
  71. private:
  72. virtual void*
  73. do_allocate(size_t __bytes, size_t __alignment) = 0;
  74. virtual void
  75. do_deallocate(void* __p, size_t __bytes, size_t __alignment) = 0;
  76. virtual bool
  77. do_is_equal(const memory_resource& __other) const noexcept = 0;
  78. };
  79. [[nodiscard]]
  80. inline bool
  81. operator==(const memory_resource& __a, const memory_resource& __b) noexcept
  82. { return &__a == &__b || __a.is_equal(__b); }
  83. #if __cpp_impl_three_way_comparison < 201907L
  84. [[nodiscard]]
  85. inline bool
  86. operator!=(const memory_resource& __a, const memory_resource& __b) noexcept
  87. { return !(__a == __b); }
  88. #endif
  89. // C++17 23.12.3 Class template polymorphic_allocator
  90. /// Class template polymorphic_allocator
  91. /**
  92. * @ingroup pmr
  93. * @headerfile memory_resource
  94. * @since C++17
  95. */
  96. template<typename _Tp>
  97. class polymorphic_allocator
  98. {
  99. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  100. // 2975. Missing case for pair construction in polymorphic allocators
  101. template<typename _Up>
  102. struct __not_pair { using type = void; };
  103. template<typename _Up1, typename _Up2>
  104. struct __not_pair<pair<_Up1, _Up2>> { };
  105. public:
  106. using value_type = _Tp;
  107. polymorphic_allocator() noexcept
  108. {
  109. extern memory_resource* get_default_resource() noexcept
  110. __attribute__((__returns_nonnull__));
  111. _M_resource = get_default_resource();
  112. }
  113. polymorphic_allocator(memory_resource* __r) noexcept
  114. __attribute__((__nonnull__))
  115. : _M_resource(__r)
  116. { _GLIBCXX_DEBUG_ASSERT(__r); }
  117. polymorphic_allocator(const polymorphic_allocator& __other) = default;
  118. template<typename _Up>
  119. polymorphic_allocator(const polymorphic_allocator<_Up>& __x) noexcept
  120. : _M_resource(__x.resource())
  121. { }
  122. polymorphic_allocator&
  123. operator=(const polymorphic_allocator&) = delete;
  124. [[nodiscard]]
  125. _Tp*
  126. allocate(size_t __n)
  127. __attribute__((__returns_nonnull__))
  128. {
  129. if ((__gnu_cxx::__int_traits<size_t>::__max / sizeof(_Tp)) < __n)
  130. std::__throw_bad_array_new_length();
  131. return static_cast<_Tp*>(_M_resource->allocate(__n * sizeof(_Tp),
  132. alignof(_Tp)));
  133. }
  134. void
  135. deallocate(_Tp* __p, size_t __n) noexcept
  136. __attribute__((__nonnull__))
  137. { _M_resource->deallocate(__p, __n * sizeof(_Tp), alignof(_Tp)); }
  138. #if __cplusplus > 201703L
  139. [[nodiscard]] void*
  140. allocate_bytes(size_t __nbytes,
  141. size_t __alignment = alignof(max_align_t))
  142. { return _M_resource->allocate(__nbytes, __alignment); }
  143. void
  144. deallocate_bytes(void* __p, size_t __nbytes,
  145. size_t __alignment = alignof(max_align_t))
  146. { _M_resource->deallocate(__p, __nbytes, __alignment); }
  147. template<typename _Up>
  148. [[nodiscard]] _Up*
  149. allocate_object(size_t __n = 1)
  150. {
  151. if ((__gnu_cxx::__int_traits<size_t>::__max / sizeof(_Up)) < __n)
  152. std::__throw_bad_array_new_length();
  153. return static_cast<_Up*>(allocate_bytes(__n * sizeof(_Up),
  154. alignof(_Up)));
  155. }
  156. template<typename _Up>
  157. void
  158. deallocate_object(_Up* __p, size_t __n = 1)
  159. { deallocate_bytes(__p, __n * sizeof(_Up), alignof(_Up)); }
  160. template<typename _Up, typename... _CtorArgs>
  161. [[nodiscard]] _Up*
  162. new_object(_CtorArgs&&... __ctor_args)
  163. {
  164. _Up* __p = allocate_object<_Up>();
  165. __try
  166. {
  167. construct(__p, std::forward<_CtorArgs>(__ctor_args)...);
  168. }
  169. __catch (...)
  170. {
  171. deallocate_object(__p);
  172. __throw_exception_again;
  173. }
  174. return __p;
  175. }
  176. template<typename _Up>
  177. void
  178. delete_object(_Up* __p)
  179. {
  180. __p->~_Up();
  181. deallocate_object(__p);
  182. }
  183. #endif // C++2a
  184. #if ! __cpp_lib_make_obj_using_allocator
  185. template<typename _Tp1, typename... _Args>
  186. __attribute__((__nonnull__))
  187. typename __not_pair<_Tp1>::type
  188. construct(_Tp1* __p, _Args&&... __args)
  189. {
  190. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  191. // 2969. polymorphic_allocator::construct() shouldn't pass resource()
  192. using __use_tag
  193. = std::__uses_alloc_t<_Tp1, polymorphic_allocator, _Args...>;
  194. if constexpr (is_base_of_v<__uses_alloc0, __use_tag>)
  195. ::new(__p) _Tp1(std::forward<_Args>(__args)...);
  196. else if constexpr (is_base_of_v<__uses_alloc1_, __use_tag>)
  197. ::new(__p) _Tp1(allocator_arg, *this,
  198. std::forward<_Args>(__args)...);
  199. else
  200. ::new(__p) _Tp1(std::forward<_Args>(__args)..., *this);
  201. }
  202. template<typename _Tp1, typename _Tp2,
  203. typename... _Args1, typename... _Args2>
  204. __attribute__((__nonnull__))
  205. void
  206. construct(pair<_Tp1, _Tp2>* __p, piecewise_construct_t,
  207. tuple<_Args1...> __x, tuple<_Args2...> __y)
  208. {
  209. auto __x_tag =
  210. __use_alloc<_Tp1, polymorphic_allocator, _Args1...>(*this);
  211. auto __y_tag =
  212. __use_alloc<_Tp2, polymorphic_allocator, _Args2...>(*this);
  213. index_sequence_for<_Args1...> __x_i;
  214. index_sequence_for<_Args2...> __y_i;
  215. ::new(__p) pair<_Tp1, _Tp2>(piecewise_construct,
  216. _S_construct_p(__x_tag, __x_i, __x),
  217. _S_construct_p(__y_tag, __y_i, __y));
  218. }
  219. template<typename _Tp1, typename _Tp2>
  220. __attribute__((__nonnull__))
  221. void
  222. construct(pair<_Tp1, _Tp2>* __p)
  223. { this->construct(__p, piecewise_construct, tuple<>(), tuple<>()); }
  224. template<typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
  225. __attribute__((__nonnull__))
  226. void
  227. construct(pair<_Tp1, _Tp2>* __p, _Up&& __x, _Vp&& __y)
  228. {
  229. this->construct(__p, piecewise_construct,
  230. std::forward_as_tuple(std::forward<_Up>(__x)),
  231. std::forward_as_tuple(std::forward<_Vp>(__y)));
  232. }
  233. template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
  234. __attribute__((__nonnull__))
  235. void
  236. construct(pair<_Tp1, _Tp2>* __p, const std::pair<_Up, _Vp>& __pr)
  237. {
  238. this->construct(__p, piecewise_construct,
  239. std::forward_as_tuple(__pr.first),
  240. std::forward_as_tuple(__pr.second));
  241. }
  242. template<typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
  243. __attribute__((__nonnull__))
  244. void
  245. construct(pair<_Tp1, _Tp2>* __p, pair<_Up, _Vp>&& __pr)
  246. {
  247. this->construct(__p, piecewise_construct,
  248. std::forward_as_tuple(std::forward<_Up>(__pr.first)),
  249. std::forward_as_tuple(std::forward<_Vp>(__pr.second)));
  250. }
  251. #else // make_obj_using_allocator
  252. template<typename _Tp1, typename... _Args>
  253. __attribute__((__nonnull__))
  254. void
  255. construct(_Tp1* __p, _Args&&... __args)
  256. {
  257. std::uninitialized_construct_using_allocator(__p, *this,
  258. std::forward<_Args>(__args)...);
  259. }
  260. #endif
  261. template<typename _Up>
  262. _GLIBCXX20_DEPRECATED_SUGGEST("allocator_traits::destroy")
  263. __attribute__((__nonnull__))
  264. void
  265. destroy(_Up* __p)
  266. { __p->~_Up(); }
  267. polymorphic_allocator
  268. select_on_container_copy_construction() const noexcept
  269. { return polymorphic_allocator(); }
  270. memory_resource*
  271. resource() const noexcept
  272. __attribute__((__returns_nonnull__))
  273. { return _M_resource; }
  274. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  275. // 3683. operator== for polymorphic_allocator cannot deduce template arg
  276. [[nodiscard]]
  277. friend bool
  278. operator==(const polymorphic_allocator& __a,
  279. const polymorphic_allocator& __b) noexcept
  280. { return *__a.resource() == *__b.resource(); }
  281. #if __cpp_impl_three_way_comparison < 201907L
  282. [[nodiscard]]
  283. friend bool
  284. operator!=(const polymorphic_allocator& __a,
  285. const polymorphic_allocator& __b) noexcept
  286. { return !(__a == __b); }
  287. #endif
  288. private:
  289. #if ! __cpp_lib_make_obj_using_allocator
  290. using __uses_alloc1_ = __uses_alloc1<polymorphic_allocator>;
  291. using __uses_alloc2_ = __uses_alloc2<polymorphic_allocator>;
  292. template<typename _Ind, typename... _Args>
  293. static tuple<_Args&&...>
  294. _S_construct_p(__uses_alloc0, _Ind, tuple<_Args...>& __t)
  295. { return std::move(__t); }
  296. template<size_t... _Ind, typename... _Args>
  297. static tuple<allocator_arg_t, polymorphic_allocator, _Args&&...>
  298. _S_construct_p(__uses_alloc1_ __ua, index_sequence<_Ind...>,
  299. tuple<_Args...>& __t)
  300. {
  301. return {
  302. allocator_arg, *__ua._M_a, std::get<_Ind>(std::move(__t))...
  303. };
  304. }
  305. template<size_t... _Ind, typename... _Args>
  306. static tuple<_Args&&..., polymorphic_allocator>
  307. _S_construct_p(__uses_alloc2_ __ua, index_sequence<_Ind...>,
  308. tuple<_Args...>& __t)
  309. { return { std::get<_Ind>(std::move(__t))..., *__ua._M_a }; }
  310. #endif
  311. memory_resource* _M_resource;
  312. };
  313. template<typename _Tp1, typename _Tp2>
  314. [[nodiscard]]
  315. inline bool
  316. operator==(const polymorphic_allocator<_Tp1>& __a,
  317. const polymorphic_allocator<_Tp2>& __b) noexcept
  318. { return *__a.resource() == *__b.resource(); }
  319. #if __cpp_impl_three_way_comparison < 201907L
  320. template<typename _Tp1, typename _Tp2>
  321. [[nodiscard]]
  322. inline bool
  323. operator!=(const polymorphic_allocator<_Tp1>& __a,
  324. const polymorphic_allocator<_Tp2>& __b) noexcept
  325. { return !(__a == __b); }
  326. #endif
  327. } // namespace pmr
  328. template<typename _Alloc> struct allocator_traits;
  329. /// Partial specialization for std::pmr::polymorphic_allocator
  330. template<typename _Tp>
  331. struct allocator_traits<pmr::polymorphic_allocator<_Tp>>
  332. {
  333. /// The allocator type
  334. using allocator_type = pmr::polymorphic_allocator<_Tp>;
  335. /// The allocated type
  336. using value_type = _Tp;
  337. /// The allocator's pointer type.
  338. using pointer = _Tp*;
  339. /// The allocator's const pointer type.
  340. using const_pointer = const _Tp*;
  341. /// The allocator's void pointer type.
  342. using void_pointer = void*;
  343. /// The allocator's const void pointer type.
  344. using const_void_pointer = const void*;
  345. /// The allocator's difference type
  346. using difference_type = std::ptrdiff_t;
  347. /// The allocator's size type
  348. using size_type = std::size_t;
  349. /** @{
  350. * A `polymorphic_allocator` does not propagate when a
  351. * container is copied, moved, or swapped.
  352. */
  353. using propagate_on_container_copy_assignment = false_type;
  354. using propagate_on_container_move_assignment = false_type;
  355. using propagate_on_container_swap = false_type;
  356. static allocator_type
  357. select_on_container_copy_construction(const allocator_type&) noexcept
  358. { return allocator_type(); }
  359. /// @}
  360. /// Whether all instances of the allocator type compare equal.
  361. using is_always_equal = false_type;
  362. template<typename _Up>
  363. using rebind_alloc = pmr::polymorphic_allocator<_Up>;
  364. template<typename _Up>
  365. using rebind_traits = allocator_traits<pmr::polymorphic_allocator<_Up>>;
  366. /**
  367. * @brief Allocate memory.
  368. * @param __a An allocator.
  369. * @param __n The number of objects to allocate space for.
  370. *
  371. * Calls `a.allocate(n)`.
  372. */
  373. [[nodiscard]] static pointer
  374. allocate(allocator_type& __a, size_type __n)
  375. { return __a.allocate(__n); }
  376. /**
  377. * @brief Allocate memory.
  378. * @param __a An allocator.
  379. * @param __n The number of objects to allocate space for.
  380. * @return Memory of suitable size and alignment for `n` objects
  381. * of type `value_type`.
  382. *
  383. * The third parameter is ignored..
  384. *
  385. * Returns `a.allocate(n)`.
  386. */
  387. [[nodiscard]] static pointer
  388. allocate(allocator_type& __a, size_type __n, const_void_pointer)
  389. { return __a.allocate(__n); }
  390. /**
  391. * @brief Deallocate memory.
  392. * @param __a An allocator.
  393. * @param __p Pointer to the memory to deallocate.
  394. * @param __n The number of objects space was allocated for.
  395. *
  396. * Calls `a.deallocate(p, n)`.
  397. */
  398. static void
  399. deallocate(allocator_type& __a, pointer __p, size_type __n)
  400. { __a.deallocate(__p, __n); }
  401. /**
  402. * @brief Construct an object of type `_Up`
  403. * @param __a An allocator.
  404. * @param __p Pointer to memory of suitable size and alignment for
  405. * an object of type `_Up`.
  406. * @param __args Constructor arguments.
  407. *
  408. * Calls `__a.construct(__p, std::forward<_Args>(__args)...)`
  409. * in C++11, C++14 and C++17. Changed in C++20 to call
  410. * `std::construct_at(__p, std::forward<_Args>(__args)...)` instead.
  411. */
  412. template<typename _Up, typename... _Args>
  413. static void
  414. construct(allocator_type& __a, _Up* __p, _Args&&... __args)
  415. { __a.construct(__p, std::forward<_Args>(__args)...); }
  416. /**
  417. * @brief Destroy an object of type `_Up`
  418. * @param __a An allocator.
  419. * @param __p Pointer to the object to destroy
  420. *
  421. * Calls `p->_Up()`.
  422. */
  423. template<typename _Up>
  424. static _GLIBCXX20_CONSTEXPR void
  425. destroy(allocator_type&, _Up* __p)
  426. noexcept(is_nothrow_destructible<_Up>::value)
  427. { __p->~_Up(); }
  428. /**
  429. * @brief The maximum supported allocation size
  430. * @return `numeric_limits<size_t>::max() / sizeof(value_type)`
  431. */
  432. static _GLIBCXX20_CONSTEXPR size_type
  433. max_size(const allocator_type&) noexcept
  434. { return size_t(-1) / sizeof(value_type); }
  435. };
  436. _GLIBCXX_END_NAMESPACE_VERSION
  437. } // namespace std
  438. #endif // C++17
  439. #endif // _GLIBCXX_MEMORY_RESOURCE_H