memory_resource 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. // -*- C++ -*-
  2. //===------------------------ memory_resource -----------------------------===//
  3. //
  4. // The LLVM Compiler Infrastructure
  5. //
  6. // This file is distributed under the University of Illinois Open Source
  7. // License. See LICENSE.TXT for details.
  8. //
  9. //===----------------------------------------------------------------------===//
  10. #ifndef _LIBCPP_EXPERIMENTAL_MEMORY_RESOURCE
  11. #define _LIBCPP_EXPERIMENTAL_MEMORY_RESOURCE
  12. /**
  13. experimental/memory_resource synopsis
  14. // C++1y
  15. namespace std {
  16. namespace experimental {
  17. inline namespace fundamentals_v1 {
  18. namespace pmr {
  19. class memory_resource;
  20. bool operator==(const memory_resource& a,
  21. const memory_resource& b) noexcept;
  22. bool operator!=(const memory_resource& a,
  23. const memory_resource& b) noexcept;
  24. template <class Tp> class polymorphic_allocator;
  25. template <class T1, class T2>
  26. bool operator==(const polymorphic_allocator<T1>& a,
  27. const polymorphic_allocator<T2>& b) noexcept;
  28. template <class T1, class T2>
  29. bool operator!=(const polymorphic_allocator<T1>& a,
  30. const polymorphic_allocator<T2>& b) noexcept;
  31. // The name resource_adaptor_imp is for exposition only.
  32. template <class Allocator> class resource_adaptor_imp;
  33. template <class Allocator>
  34. using resource_adaptor = resource_adaptor_imp<
  35. allocator_traits<Allocator>::rebind_alloc<char>>;
  36. // Global memory resources
  37. memory_resource* new_delete_resource() noexcept;
  38. memory_resource* null_memory_resource() noexcept;
  39. // The default memory resource
  40. memory_resource* set_default_resource(memory_resource* r) noexcept;
  41. memory_resource* get_default_resource() noexcept;
  42. // Standard memory resources
  43. struct pool_options;
  44. class synchronized_pool_resource;
  45. class unsynchronized_pool_resource;
  46. class monotonic_buffer_resource;
  47. } // namespace pmr
  48. } // namespace fundamentals_v1
  49. } // namespace experimental
  50. } // namespace std
  51. */
  52. #include <experimental/__config>
  53. #include <experimental/__memory>
  54. #include <limits>
  55. #include <memory>
  56. #include <new>
  57. #include <stdexcept>
  58. #include <tuple>
  59. #include <type_traits>
  60. #include <utility>
  61. #include <cstddef>
  62. #include <cstdlib>
  63. #include <__debug>
  64. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  65. #pragma GCC system_header
  66. #endif
  67. _LIBCPP_BEGIN_NAMESPACE_LFTS_PMR
  68. // Round __s up to next multiple of __a.
  69. inline _LIBCPP_INLINE_VISIBILITY
  70. size_t __aligned_allocation_size(size_t __s, size_t __a) _NOEXCEPT
  71. {
  72. _LIBCPP_ASSERT(__s + __a > __s, "aligned allocation size overflows");
  73. return (__s + __a - 1) & ~(__a - 1);
  74. }
  75. // 8.5, memory.resource
  76. class _LIBCPP_TYPE_VIS_ONLY memory_resource
  77. {
  78. static const size_t __max_align = alignof(max_align_t);
  79. // 8.5.2, memory.resource.public
  80. public:
  81. virtual ~memory_resource() = default;
  82. _LIBCPP_INLINE_VISIBILITY
  83. void* allocate(size_t __bytes, size_t __align = __max_align)
  84. { return do_allocate(__bytes, __align); }
  85. _LIBCPP_INLINE_VISIBILITY
  86. void deallocate(void * __p, size_t __bytes, size_t __align = __max_align)
  87. { do_deallocate(__p, __bytes, __align); }
  88. _LIBCPP_INLINE_VISIBILITY
  89. bool is_equal(memory_resource const & __other) const _NOEXCEPT
  90. { return do_is_equal(__other); }
  91. // 8.5.3, memory.resource.priv
  92. protected:
  93. virtual void* do_allocate(size_t, size_t) = 0;
  94. virtual void do_deallocate(void*, size_t, size_t) = 0;
  95. virtual bool do_is_equal(memory_resource const &) const _NOEXCEPT = 0;
  96. };
  97. // 8.5.4, memory.resource.eq
  98. inline _LIBCPP_INLINE_VISIBILITY
  99. bool operator==(memory_resource const & __lhs,
  100. memory_resource const & __rhs) _NOEXCEPT
  101. {
  102. return &__lhs == &__rhs || __lhs.is_equal(__rhs);
  103. }
  104. inline _LIBCPP_INLINE_VISIBILITY
  105. bool operator!=(memory_resource const & __lhs,
  106. memory_resource const & __rhs) _NOEXCEPT
  107. {
  108. return !(__lhs == __rhs);
  109. }
  110. _LIBCPP_FUNC_VIS
  111. memory_resource * new_delete_resource() _NOEXCEPT;
  112. _LIBCPP_FUNC_VIS
  113. memory_resource * null_memory_resource() _NOEXCEPT;
  114. _LIBCPP_FUNC_VIS
  115. memory_resource * get_default_resource() _NOEXCEPT;
  116. _LIBCPP_FUNC_VIS
  117. memory_resource * set_default_resource(memory_resource * __new_res) _NOEXCEPT;
  118. // 8.6, memory.polymorphic.allocator.class
  119. // 8.6.1, memory.polymorphic.allocator.overview
  120. template <class _ValueType>
  121. class _LIBCPP_TYPE_VIS_ONLY polymorphic_allocator
  122. {
  123. public:
  124. typedef _ValueType value_type;
  125. // 8.6.2, memory.polymorphic.allocator.ctor
  126. _LIBCPP_INLINE_VISIBILITY
  127. polymorphic_allocator() _NOEXCEPT
  128. : __res_(_VSTD_LFTS_PMR::get_default_resource())
  129. {}
  130. _LIBCPP_INLINE_VISIBILITY
  131. polymorphic_allocator(memory_resource * __r) _NOEXCEPT
  132. : __res_(__r)
  133. {}
  134. polymorphic_allocator(polymorphic_allocator const &) = default;
  135. template <class _Tp>
  136. _LIBCPP_INLINE_VISIBILITY
  137. polymorphic_allocator(polymorphic_allocator<_Tp> const & __other) _NOEXCEPT
  138. : __res_(__other.resource())
  139. {}
  140. polymorphic_allocator &
  141. operator=(polymorphic_allocator const &) = delete;
  142. // 8.6.3, memory.polymorphic.allocator.mem
  143. _LIBCPP_INLINE_VISIBILITY
  144. _ValueType* allocate(size_t __n) {
  145. if (__n > max_size()) {
  146. __libcpp_throw(length_error(
  147. "std::experimental::pmr::polymorphic_allocator<T>::allocate(size_t n)"
  148. " 'n' exceeds maximum supported size"));
  149. }
  150. return static_cast<_ValueType*>(
  151. __res_->allocate(__n * sizeof(_ValueType), alignof(_ValueType))
  152. );
  153. }
  154. _LIBCPP_INLINE_VISIBILITY
  155. void deallocate(_ValueType * __p, size_t __n) _NOEXCEPT {
  156. _LIBCPP_ASSERT(__n <= max_size(),
  157. "deallocate called for size which exceeds max_size()");
  158. __res_->deallocate(__p, __n * sizeof(_ValueType), alignof(_ValueType));
  159. }
  160. template <class _Tp, class ..._Ts>
  161. _LIBCPP_INLINE_VISIBILITY
  162. void construct(_Tp* __p, _Ts &&... __args)
  163. {
  164. _VSTD_LFTS::__lfts_user_alloc_construct(
  165. __p, resource(), _VSTD::forward<_Ts>(__args)...
  166. );
  167. }
  168. template <class _T1, class _T2, class ..._Args1, class ..._Args2>
  169. _LIBCPP_INLINE_VISIBILITY
  170. void construct(pair<_T1, _T2>* __p, piecewise_construct_t,
  171. tuple<_Args1...> __x, tuple<_Args2...> __y)
  172. {
  173. ::new ((void*)__p) pair<_T1, _T2>(piecewise_construct
  174. , __transform_tuple(
  175. typename __lfts_uses_alloc_ctor<
  176. _T1, memory_resource*, _Args1...
  177. >::type()
  178. , _VSTD::move(__x)
  179. , typename __make_tuple_indices<sizeof...(_Args1)>::type{}
  180. )
  181. , __transform_tuple(
  182. typename __lfts_uses_alloc_ctor<
  183. _T2, memory_resource*, _Args2...
  184. >::type()
  185. , _VSTD::move(__y)
  186. , typename __make_tuple_indices<sizeof...(_Args2)>::type{}
  187. )
  188. );
  189. }
  190. template <class _T1, class _T2>
  191. _LIBCPP_INLINE_VISIBILITY
  192. void construct(pair<_T1, _T2>* __p) {
  193. construct(__p, piecewise_construct, tuple<>(), tuple<>());
  194. }
  195. template <class _T1, class _T2, class _Up, class _Vp>
  196. _LIBCPP_INLINE_VISIBILITY
  197. void construct(pair<_T1, _T2> * __p, _Up && __u, _Vp && __v) {
  198. construct(__p, piecewise_construct
  199. , _VSTD::forward_as_tuple(_VSTD::forward<_Up>(__u))
  200. , _VSTD::forward_as_tuple(_VSTD::forward<_Vp>(__v)));
  201. }
  202. template <class _T1, class _T2, class _U1, class _U2>
  203. _LIBCPP_INLINE_VISIBILITY
  204. void construct(pair<_T1, _T2> * __p, pair<_U1, _U2> const & __pr) {
  205. construct(__p, piecewise_construct
  206. , _VSTD::forward_as_tuple(__pr.first)
  207. , _VSTD::forward_as_tuple(__pr.second));
  208. }
  209. template <class _T1, class _T2, class _U1, class _U2>
  210. _LIBCPP_INLINE_VISIBILITY
  211. void construct(pair<_T1, _T2> * __p, pair<_U1, _U2> && __pr){
  212. construct(__p, piecewise_construct
  213. , _VSTD::forward_as_tuple(_VSTD::forward<_U1>(__pr.first))
  214. , _VSTD::forward_as_tuple(_VSTD::forward<_U2>(__pr.second)));
  215. }
  216. template <class _Tp>
  217. _LIBCPP_INLINE_VISIBILITY
  218. void destroy(_Tp * __p) _NOEXCEPT
  219. { __p->~_Tp(); }
  220. _LIBCPP_INLINE_VISIBILITY
  221. size_t max_size() const _NOEXCEPT
  222. { return numeric_limits<size_t>::max() / sizeof(value_type); }
  223. _LIBCPP_INLINE_VISIBILITY
  224. polymorphic_allocator
  225. select_on_container_copy_construction() const _NOEXCEPT
  226. { return polymorphic_allocator(); }
  227. _LIBCPP_INLINE_VISIBILITY
  228. memory_resource * resource() const _NOEXCEPT
  229. { return __res_; }
  230. private:
  231. template <class ..._Args, size_t ..._Idx>
  232. _LIBCPP_INLINE_VISIBILITY
  233. tuple<_Args&&...>
  234. __transform_tuple(integral_constant<int, 0>, tuple<_Args...>&& __t,
  235. __tuple_indices<_Idx...>) const
  236. {
  237. return _VSTD::forward_as_tuple(_VSTD::get<_Idx>(_VSTD::move(__t))...);
  238. }
  239. template <class ..._Args, size_t ..._Idx>
  240. _LIBCPP_INLINE_VISIBILITY
  241. tuple<allocator_arg_t const&, memory_resource*, _Args&&...>
  242. __transform_tuple(integral_constant<int, 1>, tuple<_Args...> && __t,
  243. __tuple_indices<_Idx...>) const
  244. {
  245. using _Tup = tuple<allocator_arg_t const&, memory_resource*, _Args&&...>;
  246. return _Tup(allocator_arg, resource(),
  247. _VSTD::get<_Idx>(_VSTD::move(__t))...);
  248. }
  249. template <class ..._Args, size_t ..._Idx>
  250. _LIBCPP_INLINE_VISIBILITY
  251. tuple<_Args&&..., memory_resource*>
  252. __transform_tuple(integral_constant<int, 2>, tuple<_Args...> && __t,
  253. __tuple_indices<_Idx...>) const
  254. {
  255. using _Tup = tuple<_Args&&..., memory_resource*>;
  256. return _Tup(_VSTD::get<_Idx>(_VSTD::move(__t))..., resource());
  257. }
  258. memory_resource * __res_;
  259. };
  260. // 8.6.4, memory.polymorphic.allocator.eq
  261. template <class _Tp, class _Up>
  262. inline _LIBCPP_INLINE_VISIBILITY
  263. bool operator==(polymorphic_allocator<_Tp> const & __lhs,
  264. polymorphic_allocator<_Up> const & __rhs) _NOEXCEPT
  265. {
  266. return *__lhs.resource() == *__rhs.resource();
  267. }
  268. template <class _Tp, class _Up>
  269. inline _LIBCPP_INLINE_VISIBILITY
  270. bool operator!=(polymorphic_allocator<_Tp> const & __lhs,
  271. polymorphic_allocator<_Up> const & __rhs) _NOEXCEPT
  272. {
  273. return !(__lhs == __rhs);
  274. }
  275. // 8.7, memory.resource.adaptor
  276. // 8.7.1, memory.resource.adaptor.overview
  277. template <class _CharAlloc>
  278. class _LIBCPP_TYPE_VIS_ONLY __resource_adaptor_imp
  279. : public memory_resource
  280. {
  281. using _CTraits = allocator_traits<_CharAlloc>;
  282. static_assert(is_same<typename _CTraits::value_type, char>::value
  283. && is_same<typename _CTraits::pointer, char*>::value
  284. && is_same<typename _CTraits::void_pointer, void*>::value, "");
  285. static const size_t _MaxAlign = alignof(max_align_t);
  286. using _Alloc = typename _CTraits::template rebind_alloc<
  287. typename aligned_storage<_MaxAlign, _MaxAlign>::type
  288. >;
  289. using _ValueType = typename _Alloc::value_type;
  290. _Alloc __alloc_;
  291. public:
  292. typedef _CharAlloc allocator_type;
  293. __resource_adaptor_imp() = default;
  294. __resource_adaptor_imp(__resource_adaptor_imp const &) = default;
  295. __resource_adaptor_imp(__resource_adaptor_imp &&) = default;
  296. // 8.7.2, memory.resource.adaptor.ctor
  297. _LIBCPP_INLINE_VISIBILITY
  298. explicit __resource_adaptor_imp(allocator_type const & __a)
  299. : __alloc_(__a)
  300. {}
  301. _LIBCPP_INLINE_VISIBILITY
  302. explicit __resource_adaptor_imp(allocator_type && __a)
  303. : __alloc_(_VSTD::move(__a))
  304. {}
  305. __resource_adaptor_imp &
  306. operator=(__resource_adaptor_imp const &) = default;
  307. _LIBCPP_INLINE_VISIBILITY
  308. allocator_type get_allocator() const
  309. { return __alloc_; }
  310. // 8.7.3, memory.resource.adaptor.mem
  311. protected:
  312. virtual void * do_allocate(size_t __bytes, size_t)
  313. {
  314. if (__bytes > __max_size()) {
  315. __libcpp_throw(length_error(
  316. "std::experimental::pmr::resource_adaptor<T>::do_allocate(size_t bytes, size_t align)"
  317. " 'bytes' exceeds maximum supported size"));
  318. }
  319. size_t __s = __aligned_allocation_size(__bytes, _MaxAlign) / _MaxAlign;
  320. return __alloc_.allocate(__s);
  321. }
  322. virtual void do_deallocate(void * __p, size_t __bytes, size_t)
  323. {
  324. _LIBCPP_ASSERT(__bytes <= __max_size(),
  325. "do_deallocate called for size which exceeds the maximum allocation size");
  326. size_t __s = __aligned_allocation_size(__bytes, _MaxAlign) / _MaxAlign;
  327. __alloc_.deallocate((_ValueType*)__p, __s);
  328. }
  329. virtual bool do_is_equal(memory_resource const & __other) const _NOEXCEPT {
  330. __resource_adaptor_imp const * __p
  331. = dynamic_cast<__resource_adaptor_imp const *>(&__other);
  332. return __p ? __alloc_ == __p->__alloc_ : false;
  333. }
  334. private:
  335. _LIBCPP_INLINE_VISIBILITY
  336. size_t __max_size() const _NOEXCEPT {
  337. return numeric_limits<size_t>::max() - _MaxAlign;
  338. }
  339. };
  340. template <class _Alloc>
  341. using resource_adaptor = __resource_adaptor_imp<
  342. typename allocator_traits<_Alloc>::template rebind_alloc<char>
  343. >;
  344. _LIBCPP_END_NAMESPACE_LFTS_PMR
  345. #endif /* _LIBCPP_EXPERIMENTAL_MEMORY_RESOURCE */