You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

memory_resource 20KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. // <memory_resource> -*- C++ -*-
  2. // Copyright (C) 2018-2020 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/memory_resource
  21. * This is a Standard C++ Library header.
  22. */
  23. #ifndef _GLIBCXX_MEMORY_RESOURCE
  24. #define _GLIBCXX_MEMORY_RESOURCE 1
  25. #pragma GCC system_header
  26. #if __cplusplus >= 201703L
  27. #include <memory> // align, allocator_arg_t, __uses_alloc
  28. #include <utility> // pair, index_sequence
  29. #include <vector> // vector
  30. #include <cstddef> // size_t, max_align_t, byte
  31. #include <shared_mutex> // shared_mutex
  32. #include <bits/functexcept.h>
  33. #include <ext/numeric_traits.h>
  34. #include <debug/assertions.h>
  35. namespace std _GLIBCXX_VISIBILITY(default)
  36. {
  37. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  38. namespace pmr
  39. {
  40. #ifdef _GLIBCXX_HAS_GTHREADS
  41. // Header and all contents are present.
  42. # define __cpp_lib_memory_resource 201603
  43. #else
  44. // The pmr::synchronized_pool_resource type is missing.
  45. # define __cpp_lib_memory_resource 1
  46. #endif
  47. class memory_resource;
  48. #if __cplusplus == 201703L
  49. template<typename _Tp>
  50. class polymorphic_allocator;
  51. #else // C++20
  52. # define __cpp_lib_polymorphic_allocator 201902L
  53. template<typename _Tp = std::byte>
  54. class polymorphic_allocator;
  55. #endif
  56. // Global memory resources
  57. memory_resource* new_delete_resource() noexcept;
  58. memory_resource* null_memory_resource() noexcept;
  59. memory_resource* set_default_resource(memory_resource* __r) noexcept;
  60. memory_resource* get_default_resource() noexcept
  61. __attribute__((__returns_nonnull__));
  62. // Pool resource classes
  63. struct pool_options;
  64. #ifdef _GLIBCXX_HAS_GTHREADS
  65. class synchronized_pool_resource;
  66. #endif
  67. class unsynchronized_pool_resource;
  68. class monotonic_buffer_resource;
  69. /// Class memory_resource
  70. class memory_resource
  71. {
  72. static constexpr size_t _S_max_align = alignof(max_align_t);
  73. public:
  74. memory_resource() = default;
  75. memory_resource(const memory_resource&) = default;
  76. virtual ~memory_resource(); // key function
  77. memory_resource& operator=(const memory_resource&) = default;
  78. [[nodiscard]]
  79. void*
  80. allocate(size_t __bytes, size_t __alignment = _S_max_align)
  81. __attribute__((__returns_nonnull__,__alloc_size__(2),__alloc_align__(3)))
  82. { return do_allocate(__bytes, __alignment); }
  83. void
  84. deallocate(void* __p, size_t __bytes, size_t __alignment = _S_max_align)
  85. __attribute__((__nonnull__))
  86. { return do_deallocate(__p, __bytes, __alignment); }
  87. bool
  88. is_equal(const memory_resource& __other) const noexcept
  89. { return do_is_equal(__other); }
  90. private:
  91. virtual void*
  92. do_allocate(size_t __bytes, size_t __alignment) = 0;
  93. virtual void
  94. do_deallocate(void* __p, size_t __bytes, size_t __alignment) = 0;
  95. virtual bool
  96. do_is_equal(const memory_resource& __other) const noexcept = 0;
  97. };
  98. inline bool
  99. operator==(const memory_resource& __a, const memory_resource& __b) noexcept
  100. { return &__a == &__b || __a.is_equal(__b); }
  101. #if __cpp_impl_three_way_comparison < 201907L
  102. inline bool
  103. operator!=(const memory_resource& __a, const memory_resource& __b) noexcept
  104. { return !(__a == __b); }
  105. #endif
  106. // C++17 23.12.3 Class template polymorphic_allocator
  107. template<typename _Tp>
  108. class polymorphic_allocator
  109. {
  110. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  111. // 2975. Missing case for pair construction in polymorphic allocators
  112. template<typename _Up>
  113. struct __not_pair { using type = void; };
  114. template<typename _Up1, typename _Up2>
  115. struct __not_pair<pair<_Up1, _Up2>> { };
  116. public:
  117. using value_type = _Tp;
  118. polymorphic_allocator() noexcept
  119. : _M_resource(get_default_resource())
  120. { }
  121. polymorphic_allocator(memory_resource* __r) noexcept
  122. __attribute__((__nonnull__))
  123. : _M_resource(__r)
  124. { _GLIBCXX_DEBUG_ASSERT(__r); }
  125. polymorphic_allocator(const polymorphic_allocator& __other) = default;
  126. template<typename _Up>
  127. polymorphic_allocator(const polymorphic_allocator<_Up>& __x) noexcept
  128. : _M_resource(__x.resource())
  129. { }
  130. polymorphic_allocator&
  131. operator=(const polymorphic_allocator&) = delete;
  132. [[nodiscard]]
  133. _Tp*
  134. allocate(size_t __n)
  135. __attribute__((__returns_nonnull__))
  136. {
  137. if (__n > (__gnu_cxx::__int_traits<size_t>::__max / sizeof(_Tp)))
  138. _GLIBCXX_THROW_OR_ABORT(bad_array_new_length());
  139. return static_cast<_Tp*>(_M_resource->allocate(__n * sizeof(_Tp),
  140. alignof(_Tp)));
  141. }
  142. void
  143. deallocate(_Tp* __p, size_t __n) noexcept
  144. __attribute__((__nonnull__))
  145. { _M_resource->deallocate(__p, __n * sizeof(_Tp), alignof(_Tp)); }
  146. #if __cplusplus > 201703L
  147. [[nodiscard]] void*
  148. allocate_bytes(size_t __nbytes,
  149. size_t __alignment = alignof(max_align_t))
  150. { return _M_resource->allocate(__nbytes, __alignment); }
  151. void
  152. deallocate_bytes(void* __p, size_t __nbytes,
  153. size_t __alignment = alignof(max_align_t))
  154. { _M_resource->deallocate(__p, __nbytes, __alignment); }
  155. template<typename _Up>
  156. [[nodiscard]] _Up*
  157. allocate_object(size_t __n = 1)
  158. {
  159. if ((__gnu_cxx::__int_traits<size_t>::__max / sizeof(_Up)) < __n)
  160. _GLIBCXX_THROW_OR_ABORT(bad_array_new_length());
  161. return static_cast<_Up*>(allocate_bytes(__n * sizeof(_Up),
  162. alignof(_Up)));
  163. }
  164. template<typename _Up>
  165. void
  166. deallocate_object(_Up* __p, size_t __n = 1)
  167. { deallocate_bytes(__p, __n * sizeof(_Up), alignof(_Up)); }
  168. template<typename _Up, typename... _CtorArgs>
  169. [[nodiscard]] _Up*
  170. new_object(_CtorArgs&&... __ctor_args)
  171. {
  172. _Up* __p = allocate_object<_Up>();
  173. __try
  174. {
  175. construct(__p, std::forward<_CtorArgs>(__ctor_args)...);
  176. }
  177. __catch (...)
  178. {
  179. deallocate_object(__p);
  180. __throw_exception_again;
  181. }
  182. return __p;
  183. }
  184. template<typename _Up>
  185. void
  186. delete_object(_Up* __p)
  187. {
  188. destroy(__p);
  189. deallocate_object(__p);
  190. }
  191. #endif // C++2a
  192. #if __cplusplus == 201703L
  193. template<typename _Tp1, typename... _Args>
  194. __attribute__((__nonnull__))
  195. typename __not_pair<_Tp1>::type
  196. construct(_Tp1* __p, _Args&&... __args)
  197. {
  198. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  199. // 2969. polymorphic_allocator::construct() shouldn't pass resource()
  200. using __use_tag
  201. = std::__uses_alloc_t<_Tp1, polymorphic_allocator, _Args...>;
  202. if constexpr (is_base_of_v<__uses_alloc0, __use_tag>)
  203. ::new(__p) _Tp1(std::forward<_Args>(__args)...);
  204. else if constexpr (is_base_of_v<__uses_alloc1_, __use_tag>)
  205. ::new(__p) _Tp1(allocator_arg, *this,
  206. std::forward<_Args>(__args)...);
  207. else
  208. ::new(__p) _Tp1(std::forward<_Args>(__args)..., *this);
  209. }
  210. template<typename _Tp1, typename _Tp2,
  211. typename... _Args1, typename... _Args2>
  212. __attribute__((__nonnull__))
  213. void
  214. construct(pair<_Tp1, _Tp2>* __p, piecewise_construct_t,
  215. tuple<_Args1...> __x, tuple<_Args2...> __y)
  216. {
  217. auto __x_tag =
  218. __use_alloc<_Tp1, polymorphic_allocator, _Args1...>(*this);
  219. auto __y_tag =
  220. __use_alloc<_Tp2, polymorphic_allocator, _Args2...>(*this);
  221. index_sequence_for<_Args1...> __x_i;
  222. index_sequence_for<_Args2...> __y_i;
  223. ::new(__p) pair<_Tp1, _Tp2>(piecewise_construct,
  224. _S_construct_p(__x_tag, __x_i, __x),
  225. _S_construct_p(__y_tag, __y_i, __y));
  226. }
  227. template<typename _Tp1, typename _Tp2>
  228. __attribute__((__nonnull__))
  229. void
  230. construct(pair<_Tp1, _Tp2>* __p)
  231. { this->construct(__p, piecewise_construct, tuple<>(), tuple<>()); }
  232. template<typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
  233. __attribute__((__nonnull__))
  234. void
  235. construct(pair<_Tp1, _Tp2>* __p, _Up&& __x, _Vp&& __y)
  236. {
  237. this->construct(__p, piecewise_construct,
  238. forward_as_tuple(std::forward<_Up>(__x)),
  239. forward_as_tuple(std::forward<_Vp>(__y)));
  240. }
  241. template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
  242. __attribute__((__nonnull__))
  243. void
  244. construct(pair<_Tp1, _Tp2>* __p, const std::pair<_Up, _Vp>& __pr)
  245. {
  246. this->construct(__p, piecewise_construct,
  247. forward_as_tuple(__pr.first),
  248. forward_as_tuple(__pr.second));
  249. }
  250. template<typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
  251. __attribute__((__nonnull__))
  252. void
  253. construct(pair<_Tp1, _Tp2>* __p, pair<_Up, _Vp>&& __pr)
  254. {
  255. this->construct(__p, piecewise_construct,
  256. forward_as_tuple(std::forward<_Up>(__pr.first)),
  257. forward_as_tuple(std::forward<_Vp>(__pr.second)));
  258. }
  259. #else
  260. template<typename _Tp1, typename... _Args>
  261. __attribute__((__nonnull__))
  262. void
  263. construct(_Tp1* __p, _Args&&... __args)
  264. {
  265. std::uninitialized_construct_using_allocator(__p, *this,
  266. std::forward<_Args>(__args)...);
  267. }
  268. #endif
  269. template<typename _Up>
  270. __attribute__((__nonnull__))
  271. void
  272. destroy(_Up* __p)
  273. { __p->~_Up(); }
  274. polymorphic_allocator
  275. select_on_container_copy_construction() const noexcept
  276. { return polymorphic_allocator(); }
  277. memory_resource*
  278. resource() const noexcept
  279. __attribute__((__returns_nonnull__))
  280. { return _M_resource; }
  281. private:
  282. using __uses_alloc1_ = __uses_alloc1<polymorphic_allocator>;
  283. using __uses_alloc2_ = __uses_alloc2<polymorphic_allocator>;
  284. template<typename _Ind, typename... _Args>
  285. static tuple<_Args&&...>
  286. _S_construct_p(__uses_alloc0, _Ind, tuple<_Args...>& __t)
  287. { return std::move(__t); }
  288. template<size_t... _Ind, typename... _Args>
  289. static tuple<allocator_arg_t, polymorphic_allocator, _Args&&...>
  290. _S_construct_p(__uses_alloc1_ __ua, index_sequence<_Ind...>,
  291. tuple<_Args...>& __t)
  292. {
  293. return {
  294. allocator_arg, *__ua._M_a, std::get<_Ind>(std::move(__t))...
  295. };
  296. }
  297. template<size_t... _Ind, typename... _Args>
  298. static tuple<_Args&&..., polymorphic_allocator>
  299. _S_construct_p(__uses_alloc2_ __ua, index_sequence<_Ind...>,
  300. tuple<_Args...>& __t)
  301. { return { std::get<_Ind>(std::move(__t))..., *__ua._M_a }; }
  302. memory_resource* _M_resource;
  303. };
  304. template<typename _Tp1, typename _Tp2>
  305. inline bool
  306. operator==(const polymorphic_allocator<_Tp1>& __a,
  307. const polymorphic_allocator<_Tp2>& __b) noexcept
  308. { return *__a.resource() == *__b.resource(); }
  309. #if __cpp_impl_three_way_comparison < 201907L
  310. template<typename _Tp1, typename _Tp2>
  311. inline bool
  312. operator!=(const polymorphic_allocator<_Tp1>& __a,
  313. const polymorphic_allocator<_Tp2>& __b) noexcept
  314. { return !(__a == __b); }
  315. #endif
  316. /// Parameters for tuning a pool resource's behaviour.
  317. struct pool_options
  318. {
  319. /** @brief Upper limit on number of blocks in a chunk.
  320. *
  321. * A lower value prevents allocating huge chunks that could remain mostly
  322. * unused, but means pools will need to replenished more frequently.
  323. */
  324. size_t max_blocks_per_chunk = 0;
  325. /* @brief Largest block size (in bytes) that should be served from pools.
  326. *
  327. * Larger allocations will be served directly by the upstream resource,
  328. * not from one of the pools managed by the pool resource.
  329. */
  330. size_t largest_required_pool_block = 0;
  331. };
  332. // Common implementation details for un-/synchronized pool resources.
  333. class __pool_resource
  334. {
  335. friend class synchronized_pool_resource;
  336. friend class unsynchronized_pool_resource;
  337. __pool_resource(const pool_options& __opts, memory_resource* __upstream);
  338. ~__pool_resource();
  339. __pool_resource(const __pool_resource&) = delete;
  340. __pool_resource& operator=(const __pool_resource&) = delete;
  341. // Allocate a large unpooled block.
  342. void*
  343. allocate(size_t __bytes, size_t __alignment);
  344. // Deallocate a large unpooled block.
  345. void
  346. deallocate(void* __p, size_t __bytes, size_t __alignment);
  347. // Deallocate unpooled memory.
  348. void release() noexcept;
  349. memory_resource* resource() const noexcept
  350. { return _M_unpooled.get_allocator().resource(); }
  351. struct _Pool;
  352. _Pool* _M_alloc_pools();
  353. const pool_options _M_opts;
  354. struct _BigBlock;
  355. // Collection of blocks too big for any pool, sorted by address.
  356. // This also stores the only copy of the upstream memory resource pointer.
  357. _GLIBCXX_STD_C::pmr::vector<_BigBlock> _M_unpooled;
  358. const int _M_npools;
  359. };
  360. #ifdef _GLIBCXX_HAS_GTHREADS
  361. /// A thread-safe memory resource that manages pools of fixed-size blocks.
  362. class synchronized_pool_resource : public memory_resource
  363. {
  364. public:
  365. synchronized_pool_resource(const pool_options& __opts,
  366. memory_resource* __upstream)
  367. __attribute__((__nonnull__));
  368. synchronized_pool_resource()
  369. : synchronized_pool_resource(pool_options(), get_default_resource())
  370. { }
  371. explicit
  372. synchronized_pool_resource(memory_resource* __upstream)
  373. __attribute__((__nonnull__))
  374. : synchronized_pool_resource(pool_options(), __upstream)
  375. { }
  376. explicit
  377. synchronized_pool_resource(const pool_options& __opts)
  378. : synchronized_pool_resource(__opts, get_default_resource()) { }
  379. synchronized_pool_resource(const synchronized_pool_resource&) = delete;
  380. virtual ~synchronized_pool_resource();
  381. synchronized_pool_resource&
  382. operator=(const synchronized_pool_resource&) = delete;
  383. void release();
  384. memory_resource*
  385. upstream_resource() const noexcept
  386. __attribute__((__returns_nonnull__))
  387. { return _M_impl.resource(); }
  388. pool_options options() const noexcept { return _M_impl._M_opts; }
  389. protected:
  390. void*
  391. do_allocate(size_t __bytes, size_t __alignment) override;
  392. void
  393. do_deallocate(void* __p, size_t __bytes, size_t __alignment) override;
  394. bool
  395. do_is_equal(const memory_resource& __other) const noexcept override
  396. { return this == &__other; }
  397. public:
  398. // Thread-specific pools (only public for access by implementation details)
  399. struct _TPools;
  400. private:
  401. _TPools* _M_alloc_tpools(lock_guard<shared_mutex>&);
  402. _TPools* _M_alloc_shared_tpools(lock_guard<shared_mutex>&);
  403. auto _M_thread_specific_pools() noexcept;
  404. __pool_resource _M_impl;
  405. __gthread_key_t _M_key;
  406. // Linked list of thread-specific pools. All threads share _M_tpools[0].
  407. _TPools* _M_tpools = nullptr;
  408. mutable shared_mutex _M_mx;
  409. };
  410. #endif
  411. /// A non-thread-safe memory resource that manages pools of fixed-size blocks.
  412. class unsynchronized_pool_resource : public memory_resource
  413. {
  414. public:
  415. [[__gnu__::__nonnull__]]
  416. unsynchronized_pool_resource(const pool_options& __opts,
  417. memory_resource* __upstream);
  418. unsynchronized_pool_resource()
  419. : unsynchronized_pool_resource(pool_options(), get_default_resource())
  420. { }
  421. [[__gnu__::__nonnull__]]
  422. explicit
  423. unsynchronized_pool_resource(memory_resource* __upstream)
  424. : unsynchronized_pool_resource(pool_options(), __upstream)
  425. { }
  426. explicit
  427. unsynchronized_pool_resource(const pool_options& __opts)
  428. : unsynchronized_pool_resource(__opts, get_default_resource()) { }
  429. unsynchronized_pool_resource(const unsynchronized_pool_resource&) = delete;
  430. virtual ~unsynchronized_pool_resource();
  431. unsynchronized_pool_resource&
  432. operator=(const unsynchronized_pool_resource&) = delete;
  433. void release();
  434. [[__gnu__::__returns_nonnull__]]
  435. memory_resource*
  436. upstream_resource() const noexcept
  437. { return _M_impl.resource(); }
  438. pool_options options() const noexcept { return _M_impl._M_opts; }
  439. protected:
  440. void*
  441. do_allocate(size_t __bytes, size_t __alignment) override;
  442. void
  443. do_deallocate(void* __p, size_t __bytes, size_t __alignment) override;
  444. bool
  445. do_is_equal(const memory_resource& __other) const noexcept override
  446. { return this == &__other; }
  447. private:
  448. using _Pool = __pool_resource::_Pool;
  449. auto _M_find_pool(size_t) noexcept;
  450. __pool_resource _M_impl;
  451. _Pool* _M_pools = nullptr;
  452. };
  453. class monotonic_buffer_resource : public memory_resource
  454. {
  455. public:
  456. explicit
  457. monotonic_buffer_resource(memory_resource* __upstream) noexcept
  458. __attribute__((__nonnull__))
  459. : _M_upstream(__upstream)
  460. { _GLIBCXX_DEBUG_ASSERT(__upstream != nullptr); }
  461. monotonic_buffer_resource(size_t __initial_size,
  462. memory_resource* __upstream) noexcept
  463. __attribute__((__nonnull__))
  464. : _M_next_bufsiz(__initial_size),
  465. _M_upstream(__upstream)
  466. {
  467. _GLIBCXX_DEBUG_ASSERT(__upstream != nullptr);
  468. _GLIBCXX_DEBUG_ASSERT(__initial_size > 0);
  469. }
  470. monotonic_buffer_resource(void* __buffer, size_t __buffer_size,
  471. memory_resource* __upstream) noexcept
  472. __attribute__((__nonnull__(4)))
  473. : _M_current_buf(__buffer), _M_avail(__buffer_size),
  474. _M_next_bufsiz(_S_next_bufsize(__buffer_size)),
  475. _M_upstream(__upstream),
  476. _M_orig_buf(__buffer), _M_orig_size(__buffer_size)
  477. {
  478. _GLIBCXX_DEBUG_ASSERT(__upstream != nullptr);
  479. _GLIBCXX_DEBUG_ASSERT(__buffer != nullptr || __buffer_size == 0);
  480. }
  481. monotonic_buffer_resource() noexcept
  482. : monotonic_buffer_resource(get_default_resource())
  483. { }
  484. explicit
  485. monotonic_buffer_resource(size_t __initial_size) noexcept
  486. : monotonic_buffer_resource(__initial_size, get_default_resource())
  487. { }
  488. monotonic_buffer_resource(void* __buffer, size_t __buffer_size) noexcept
  489. : monotonic_buffer_resource(__buffer, __buffer_size, get_default_resource())
  490. { }
  491. monotonic_buffer_resource(const monotonic_buffer_resource&) = delete;
  492. virtual ~monotonic_buffer_resource(); // key function
  493. monotonic_buffer_resource&
  494. operator=(const monotonic_buffer_resource&) = delete;
  495. void
  496. release() noexcept
  497. {
  498. if (_M_head)
  499. _M_release_buffers();
  500. // reset to initial state at contruction:
  501. if ((_M_current_buf = _M_orig_buf))
  502. {
  503. _M_avail = _M_orig_size;
  504. _M_next_bufsiz = _S_next_bufsize(_M_orig_size);
  505. }
  506. else
  507. {
  508. _M_avail = 0;
  509. _M_next_bufsiz = _M_orig_size;
  510. }
  511. }
  512. memory_resource*
  513. upstream_resource() const noexcept
  514. __attribute__((__returns_nonnull__))
  515. { return _M_upstream; }
  516. protected:
  517. void*
  518. do_allocate(size_t __bytes, size_t __alignment) override
  519. {
  520. if (__bytes == 0)
  521. __bytes = 1; // Ensures we don't return the same pointer twice.
  522. void* __p = std::align(__alignment, __bytes, _M_current_buf, _M_avail);
  523. if (!__p)
  524. {
  525. _M_new_buffer(__bytes, __alignment);
  526. __p = _M_current_buf;
  527. }
  528. _M_current_buf = (char*)_M_current_buf + __bytes;
  529. _M_avail -= __bytes;
  530. return __p;
  531. }
  532. void
  533. do_deallocate(void*, size_t, size_t) override
  534. { }
  535. bool
  536. do_is_equal(const memory_resource& __other) const noexcept override
  537. { return this == &__other; }
  538. private:
  539. // Update _M_current_buf and _M_avail to refer to a new buffer with
  540. // at least the specified size and alignment, allocated from upstream.
  541. void
  542. _M_new_buffer(size_t __bytes, size_t __alignment);
  543. // Deallocate all buffers obtained from upstream.
  544. void
  545. _M_release_buffers() noexcept;
  546. static size_t
  547. _S_next_bufsize(size_t __buffer_size) noexcept
  548. {
  549. if (__buffer_size == 0)
  550. __buffer_size = 1;
  551. return __buffer_size * _S_growth_factor;
  552. }
  553. static constexpr size_t _S_init_bufsize = 128 * sizeof(void*);
  554. static constexpr float _S_growth_factor = 1.5;
  555. void* _M_current_buf = nullptr;
  556. size_t _M_avail = 0;
  557. size_t _M_next_bufsiz = _S_init_bufsize;
  558. // Initial values set at construction and reused by release():
  559. memory_resource* const _M_upstream;
  560. void* const _M_orig_buf = nullptr;
  561. size_t const _M_orig_size = _M_next_bufsiz;
  562. class _Chunk;
  563. _Chunk* _M_head = nullptr;
  564. };
  565. } // namespace pmr
  566. _GLIBCXX_END_NAMESPACE_VERSION
  567. } // namespace std
  568. #endif // C++17
  569. #endif // _GLIBCXX_MEMORY_RESOURCE