memory_resource 20 KB

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