memory_resource 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. // <experimental/memory_resource> -*- C++ -*-
  2. // Copyright (C) 2015-2021 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 experimental/memory_resource
  21. * This is a TS C++ Library header.
  22. * @ingroup libfund-ts
  23. */
  24. #ifndef _GLIBCXX_EXPERIMENTAL_MEMORY_RESOURCE
  25. #define _GLIBCXX_EXPERIMENTAL_MEMORY_RESOURCE 1
  26. #pragma GCC system_header
  27. #if __cplusplus >= 201402L
  28. #include <memory> // align, uses_allocator, __uses_alloc
  29. #include <experimental/utility> // pair, experimental::erased_type
  30. #include <tuple> // tuple, forward_as_tuple
  31. #include <atomic> // atomic
  32. #include <new> // placement new
  33. #include <cstddef> // max_align_t
  34. #include <ext/new_allocator.h>
  35. #include <debug/assertions.h>
  36. /// @cond
  37. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  38. {
  39. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  40. template<typename _Tp> class malloc_allocator;
  41. _GLIBCXX_END_NAMESPACE_VERSION
  42. } // namespace __gnu_cxx
  43. /// @endcond
  44. namespace std {
  45. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  46. namespace experimental {
  47. inline namespace fundamentals_v2 {
  48. namespace pmr {
  49. #define __cpp_lib_experimental_memory_resources 201402L
  50. // Standard memory resources
  51. // 8.5 Class memory_resource
  52. class memory_resource;
  53. // 8.6 Class template polymorphic_allocator
  54. template<typename _Tp>
  55. class polymorphic_allocator;
  56. template<typename _Alloc, typename _Resource = memory_resource>
  57. class __resource_adaptor_imp;
  58. // 8.7 Alias template resource_adaptor
  59. template<typename _Alloc>
  60. using resource_adaptor = __resource_adaptor_imp<
  61. typename allocator_traits<_Alloc>::template rebind_alloc<char>>;
  62. // 8.8 Global memory resources
  63. memory_resource* new_delete_resource() noexcept;
  64. memory_resource* null_memory_resource() noexcept;
  65. memory_resource* get_default_resource() noexcept;
  66. memory_resource* set_default_resource(memory_resource* __r) noexcept;
  67. // TODO 8.9 Pool resource classes
  68. class memory_resource
  69. {
  70. static constexpr size_t _S_max_align = alignof(max_align_t);
  71. public:
  72. memory_resource() = default;
  73. memory_resource(const memory_resource&) = default;
  74. virtual ~memory_resource() = default;
  75. memory_resource& operator=(const memory_resource&) = default;
  76. _GLIBCXX_NODISCARD void*
  77. allocate(size_t __bytes, size_t __alignment = _S_max_align)
  78. { return do_allocate(__bytes, __alignment); }
  79. void
  80. deallocate(void* __p, size_t __bytes, size_t __alignment = _S_max_align)
  81. { return do_deallocate(__p, __bytes, __alignment); }
  82. bool
  83. is_equal(const memory_resource& __other) const noexcept
  84. { return do_is_equal(__other); }
  85. protected:
  86. virtual void*
  87. do_allocate(size_t __bytes, size_t __alignment) = 0;
  88. virtual void
  89. do_deallocate(void* __p, size_t __bytes, size_t __alignment) = 0;
  90. virtual bool
  91. do_is_equal(const memory_resource& __other) const noexcept = 0;
  92. };
  93. inline bool
  94. operator==(const memory_resource& __a, const memory_resource& __b) noexcept
  95. { return &__a == &__b || __a.is_equal(__b); }
  96. inline bool
  97. operator!=(const memory_resource& __a, const memory_resource& __b) noexcept
  98. { return !(__a == __b); }
  99. template<typename _Tp>
  100. class polymorphic_allocator
  101. {
  102. public:
  103. using value_type = _Tp;
  104. polymorphic_allocator() noexcept
  105. : _M_resource(get_default_resource())
  106. { }
  107. polymorphic_allocator(memory_resource* __r)
  108. : _M_resource(__r)
  109. { _GLIBCXX_DEBUG_ASSERT(__r); }
  110. polymorphic_allocator(const polymorphic_allocator& __other) = default;
  111. template <typename _Up>
  112. polymorphic_allocator(const polymorphic_allocator<_Up>&
  113. __other) noexcept
  114. : _M_resource(__other.resource())
  115. { }
  116. polymorphic_allocator&
  117. operator=(const polymorphic_allocator& __rhs) = default;
  118. _GLIBCXX_NODISCARD _Tp* allocate(size_t __n)
  119. { return static_cast<_Tp*>(_M_resource->allocate(__n * sizeof(_Tp),
  120. alignof(_Tp))); }
  121. void
  122. deallocate(_Tp* __p, size_t __n)
  123. { _M_resource->deallocate(__p, __n * sizeof(_Tp), alignof(_Tp)); }
  124. template <typename _Tp1, typename... _Args> //used here
  125. void
  126. construct(_Tp1* __p, _Args&&... __args)
  127. {
  128. std::__uses_allocator_construct(this->resource(), __p,
  129. std::forward<_Args>(__args)...);
  130. }
  131. // Specializations for pair using piecewise construction
  132. template <typename _Tp1, typename _Tp2,
  133. typename... _Args1, typename... _Args2>
  134. void
  135. construct(pair<_Tp1, _Tp2>* __p, piecewise_construct_t,
  136. tuple<_Args1...> __x, tuple<_Args2...> __y)
  137. {
  138. memory_resource* const __resource = this->resource();
  139. auto __x_use_tag =
  140. std::__use_alloc<_Tp1, memory_resource*, _Args1...>(__resource);
  141. auto __y_use_tag =
  142. std::__use_alloc<_Tp2, memory_resource*, _Args2...>(__resource);
  143. ::new(__p) std::pair<_Tp1, _Tp2>(piecewise_construct,
  144. _M_construct_p(__x_use_tag, __x),
  145. _M_construct_p(__y_use_tag, __y));
  146. }
  147. template <typename _Tp1, typename _Tp2>
  148. void
  149. construct(pair<_Tp1,_Tp2>* __p)
  150. { this->construct(__p, piecewise_construct, tuple<>(), tuple<>()); }
  151. template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
  152. void
  153. construct(pair<_Tp1,_Tp2>* __p, _Up&& __x, _Vp&& __y)
  154. {
  155. this->construct(__p, piecewise_construct,
  156. std::forward_as_tuple(std::forward<_Up>(__x)),
  157. std::forward_as_tuple(std::forward<_Vp>(__y)));
  158. }
  159. template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
  160. void
  161. construct(pair<_Tp1,_Tp2>* __p, const std::pair<_Up, _Vp>& __pr)
  162. {
  163. this->construct(__p, piecewise_construct,
  164. std::forward_as_tuple(__pr.first),
  165. std::forward_as_tuple(__pr.second));
  166. }
  167. template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
  168. void
  169. construct(pair<_Tp1,_Tp2>* __p, pair<_Up, _Vp>&& __pr)
  170. {
  171. this->construct(__p, piecewise_construct,
  172. std::forward_as_tuple(std::forward<_Up>(__pr.first)),
  173. std::forward_as_tuple(std::forward<_Vp>(__pr.second)));
  174. }
  175. template <typename _Up>
  176. void
  177. destroy(_Up* __p)
  178. { __p->~_Up(); }
  179. // Return a default-constructed allocator (no allocator propagation)
  180. polymorphic_allocator
  181. select_on_container_copy_construction() const
  182. { return polymorphic_allocator(); }
  183. memory_resource* resource() const { return _M_resource; }
  184. private:
  185. using __uses_alloc1_ = __uses_alloc1<memory_resource*>;
  186. using __uses_alloc2_ = __uses_alloc2<memory_resource*>;
  187. template<typename _Tuple>
  188. _Tuple&&
  189. _M_construct_p(__uses_alloc0, _Tuple& __t)
  190. { return std::move(__t); }
  191. template<typename... _Args>
  192. decltype(auto)
  193. _M_construct_p(__uses_alloc1_ __ua, tuple<_Args...>& __t)
  194. { return tuple_cat(make_tuple(allocator_arg, *(__ua._M_a)),
  195. std::move(__t)); }
  196. template<typename... _Args>
  197. decltype(auto)
  198. _M_construct_p(__uses_alloc2_ __ua, tuple<_Args...>& __t)
  199. { return tuple_cat(std::move(__t), make_tuple(*(__ua._M_a))); }
  200. memory_resource* _M_resource;
  201. };
  202. template <class _Tp1, class _Tp2>
  203. bool
  204. operator==(const polymorphic_allocator<_Tp1>& __a,
  205. const polymorphic_allocator<_Tp2>& __b) noexcept
  206. { return *__a.resource() == *__b.resource(); }
  207. template <class _Tp1, class _Tp2>
  208. bool
  209. operator!=(const polymorphic_allocator<_Tp1>& __a,
  210. const polymorphic_allocator<_Tp2>& __b) noexcept
  211. { return !(__a == __b); }
  212. /// @cond undocumented
  213. class __resource_adaptor_common
  214. {
  215. template<typename, typename> friend class __resource_adaptor_imp;
  216. struct _AlignMgr
  217. {
  218. _AlignMgr(size_t __nbytes, size_t __align)
  219. : _M_nbytes(__nbytes), _M_align(__align)
  220. { }
  221. // Total size that needs to be allocated.
  222. size_t
  223. _M_alloc_size() const { return _M_buf_size() + _M_token_size(); }
  224. void*
  225. _M_adjust(void* __ptr) const
  226. {
  227. const auto __orig_ptr = static_cast<char*>(__ptr);
  228. size_t __space = _M_buf_size();
  229. // Align the pointer within the buffer:
  230. std::align(_M_align, _M_nbytes, __ptr, __space);
  231. const auto __aligned_ptr = static_cast<char*>(__ptr);
  232. const auto __token_size = _M_token_size();
  233. // Store token immediately after the aligned block:
  234. char* const __end = __aligned_ptr + _M_nbytes;
  235. if (__token_size == 1)
  236. _S_write<unsigned char>(__end, __aligned_ptr - __orig_ptr);
  237. else if (__token_size == sizeof(short))
  238. _S_write<unsigned short>(__end, __aligned_ptr - __orig_ptr);
  239. else if (__token_size == sizeof(int) && sizeof(int) < sizeof(char*))
  240. _S_write<unsigned int>(__end, __aligned_ptr - __orig_ptr);
  241. else // (__token_size == sizeof(char*))
  242. // Just store the original pointer:
  243. _S_write<char*>(__end, __orig_ptr);
  244. return __aligned_ptr;
  245. }
  246. char*
  247. _M_unadjust(char* __ptr) const
  248. {
  249. const char* const __end = __ptr + _M_nbytes;
  250. char* __orig_ptr;
  251. const auto __token_size = _M_token_size();
  252. // Read the token and restore the original pointer:
  253. if (__token_size == 1)
  254. __orig_ptr = __ptr - _S_read<unsigned char>(__end);
  255. else if (__token_size == sizeof(short))
  256. __orig_ptr = __ptr - _S_read<unsigned short>(__end);
  257. else if (__token_size == sizeof(int)
  258. && sizeof(int) < sizeof(char*))
  259. __orig_ptr = __ptr - _S_read<unsigned int>(__end);
  260. else // (__token_size == sizeof(char*))
  261. __orig_ptr = _S_read<char*>(__end);
  262. // The adjustment is always less than the requested alignment,
  263. // so if that isn't true now then either the wrong size was passed
  264. // to deallocate or the token was overwritten by a buffer overflow:
  265. __glibcxx_assert(static_cast<size_t>(__ptr - __orig_ptr) < _M_align);
  266. return __orig_ptr;
  267. }
  268. private:
  269. size_t _M_nbytes;
  270. size_t _M_align;
  271. // Number of bytes needed to fit block of given size and alignment.
  272. size_t
  273. _M_buf_size() const { return _M_nbytes + _M_align - 1; }
  274. // Number of additional bytes needed to write the token.
  275. int
  276. _M_token_size() const
  277. {
  278. if (_M_align <= (1ul << __CHAR_BIT__))
  279. return 1;
  280. if (_M_align <= (1ul << (sizeof(short) * __CHAR_BIT__)))
  281. return sizeof(short);
  282. if (_M_align <= (1ull << (sizeof(int) * __CHAR_BIT__)))
  283. return sizeof(int);
  284. return sizeof(char*);
  285. }
  286. template<typename _Tp>
  287. static void
  288. _S_write(void* __to, _Tp __val)
  289. { __builtin_memcpy(__to, &__val, sizeof(_Tp)); }
  290. template<typename _Tp>
  291. static _Tp
  292. _S_read(const void* __from)
  293. {
  294. _Tp __val;
  295. __builtin_memcpy(&__val, __from, sizeof(_Tp));
  296. return __val;
  297. }
  298. };
  299. };
  300. /// @endcond
  301. // 8.7.1 __resource_adaptor_imp
  302. template<typename _Alloc, typename _Resource>
  303. class __resource_adaptor_imp
  304. : public _Resource, private __resource_adaptor_common
  305. {
  306. using memory_resource = _Resource;
  307. static_assert(is_same<char,
  308. typename allocator_traits<_Alloc>::value_type>::value,
  309. "Allocator's value_type is char");
  310. static_assert(is_same<char*,
  311. typename allocator_traits<_Alloc>::pointer>::value,
  312. "Allocator's pointer type is value_type*");
  313. static_assert(is_same<const char*,
  314. typename allocator_traits<_Alloc>::const_pointer>::value,
  315. "Allocator's const_pointer type is value_type const*");
  316. static_assert(is_same<void*,
  317. typename allocator_traits<_Alloc>::void_pointer>::value,
  318. "Allocator's void_pointer type is void*");
  319. static_assert(is_same<const void*,
  320. typename allocator_traits<_Alloc>::const_void_pointer>::value,
  321. "Allocator's const_void_pointer type is void const*");
  322. public:
  323. using allocator_type = _Alloc;
  324. __resource_adaptor_imp() = default;
  325. __resource_adaptor_imp(const __resource_adaptor_imp&) = default;
  326. __resource_adaptor_imp(__resource_adaptor_imp&&) = default;
  327. explicit __resource_adaptor_imp(const _Alloc& __a2)
  328. : _M_alloc(__a2)
  329. { }
  330. explicit __resource_adaptor_imp(_Alloc&& __a2)
  331. : _M_alloc(std::move(__a2))
  332. { }
  333. __resource_adaptor_imp&
  334. operator=(const __resource_adaptor_imp&) = default;
  335. allocator_type get_allocator() const noexcept { return _M_alloc; }
  336. protected:
  337. virtual void*
  338. do_allocate(size_t __bytes, size_t __alignment) override
  339. {
  340. // Cannot use max_align_t on 32-bit Solaris x86, see PR libstdc++/77691
  341. #if ! ((defined __sun__ || defined __VXWORKS__) && defined __i386__)
  342. if (__alignment == alignof(max_align_t))
  343. return _M_allocate<alignof(max_align_t)>(__bytes);
  344. #endif
  345. switch (__alignment)
  346. {
  347. case 1:
  348. return _M_alloc.allocate(__bytes);
  349. case 2:
  350. return _M_allocate<2>(__bytes);
  351. case 4:
  352. return _M_allocate<4>(__bytes);
  353. case 8:
  354. return _M_allocate<8>(__bytes);
  355. }
  356. const _AlignMgr __mgr(__bytes, __alignment);
  357. // Assume _M_alloc returns 1-byte aligned memory, so allocate enough
  358. // space to fit a block of the right size and alignment, plus some
  359. // extra bytes to store a token for retrieving the original pointer.
  360. return __mgr._M_adjust(_M_alloc.allocate(__mgr._M_alloc_size()));
  361. }
  362. virtual void
  363. do_deallocate(void* __ptr, size_t __bytes, size_t __alignment) noexcept
  364. override
  365. {
  366. #if ! ((defined __sun__ || defined __VXWORKS__) && defined __i386__)
  367. if (__alignment == alignof(max_align_t))
  368. return (void) _M_deallocate<alignof(max_align_t)>(__ptr, __bytes);
  369. #endif
  370. switch (__alignment)
  371. {
  372. case 1:
  373. return (void) _M_alloc.deallocate((char*)__ptr, __bytes);
  374. case 2:
  375. return (void) _M_deallocate<2>(__ptr, __bytes);
  376. case 4:
  377. return (void) _M_deallocate<4>(__ptr, __bytes);
  378. case 8:
  379. return (void) _M_deallocate<8>(__ptr, __bytes);
  380. }
  381. const _AlignMgr __mgr(__bytes, __alignment);
  382. // Use the stored token to retrieve the original pointer.
  383. _M_alloc.deallocate(__mgr._M_unadjust((char*)__ptr),
  384. __mgr._M_alloc_size());
  385. }
  386. virtual bool
  387. do_is_equal(const memory_resource& __other) const noexcept override
  388. {
  389. if (auto __p = dynamic_cast<const __resource_adaptor_imp*>(&__other))
  390. return _M_alloc == __p->_M_alloc;
  391. return false;
  392. }
  393. private:
  394. template<size_t _Num>
  395. struct _Aligned_type { alignas(_Num) char __c[_Num]; };
  396. // Rebind the allocator to the specified type and use it to allocate.
  397. template<size_t _Num, typename _Tp = _Aligned_type<_Num>>
  398. void*
  399. _M_allocate(size_t __bytes)
  400. {
  401. typename allocator_traits<_Alloc>::template
  402. rebind_alloc<_Tp> __a2(_M_alloc);
  403. const size_t __n = (__bytes + _Num - 1) / _Num;
  404. return __a2.allocate(__n);
  405. }
  406. // Rebind the allocator to the specified type and use it to deallocate.
  407. template<size_t _Num, typename _Tp = _Aligned_type<_Num>>
  408. void
  409. _M_deallocate(void* __ptr, size_t __bytes) noexcept
  410. {
  411. typename allocator_traits<_Alloc>::template
  412. rebind_alloc<_Tp> __a2(_M_alloc);
  413. const size_t __n = (__bytes + _Num - 1) / _Num;
  414. __a2.deallocate((_Tp*)__ptr, __n);
  415. }
  416. _Alloc _M_alloc{};
  417. };
  418. // Global memory resources
  419. inline memory_resource*
  420. new_delete_resource() noexcept
  421. {
  422. using type = resource_adaptor<__gnu_cxx::new_allocator<char>>;
  423. alignas(type) static unsigned char __buf[sizeof(type)];
  424. static type* __r = new(__buf) type;
  425. return __r;
  426. }
  427. inline memory_resource*
  428. null_memory_resource() noexcept
  429. {
  430. class type final : public memory_resource
  431. {
  432. void*
  433. do_allocate(size_t, size_t) override
  434. { std::__throw_bad_alloc(); }
  435. void
  436. do_deallocate(void*, size_t, size_t) noexcept override
  437. { }
  438. bool
  439. do_is_equal(const memory_resource& __other) const noexcept override
  440. { return this == &__other; }
  441. };
  442. alignas(type) static unsigned char __buf[sizeof(type)];
  443. static type* __r = new(__buf) type;
  444. return __r;
  445. }
  446. // The default memory resource
  447. /// @cond undocumented
  448. inline std::atomic<memory_resource*>&
  449. __get_default_resource()
  450. {
  451. using type = atomic<memory_resource*>;
  452. alignas(type) static unsigned char __buf[sizeof(type)];
  453. static type* __r = new(__buf) type(new_delete_resource());
  454. return *__r;
  455. }
  456. /// @endcond
  457. /// Get the current default resource.
  458. inline memory_resource*
  459. get_default_resource() noexcept
  460. { return __get_default_resource().load(); }
  461. /// Change the default resource and return the previous one.
  462. inline memory_resource*
  463. set_default_resource(memory_resource* __r) noexcept
  464. {
  465. if (__r == nullptr)
  466. __r = new_delete_resource();
  467. return __get_default_resource().exchange(__r);
  468. }
  469. } // namespace pmr
  470. } // namespace fundamentals_v2
  471. } // namespace experimental
  472. _GLIBCXX_END_NAMESPACE_VERSION
  473. } // namespace std
  474. #endif // C++14
  475. #endif // _GLIBCXX_EXPERIMENTAL_MEMORY_RESOURCE