2 //===----------------------------------------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP_EXPERIMENTAL_MEMORY_RESOURCE
11 #define _LIBCPP_EXPERIMENTAL_MEMORY_RESOURCE
14 experimental/memory_resource synopsis
19 namespace experimental {
20 inline namespace fundamentals_v1 {
23 class memory_resource;
25 bool operator==(const memory_resource& a,
26 const memory_resource& b) noexcept;
27 bool operator!=(const memory_resource& a,
28 const memory_resource& b) noexcept;
30 template <class Tp> class polymorphic_allocator;
32 template <class T1, class T2>
33 bool operator==(const polymorphic_allocator<T1>& a,
34 const polymorphic_allocator<T2>& b) noexcept;
35 template <class T1, class T2>
36 bool operator!=(const polymorphic_allocator<T1>& a,
37 const polymorphic_allocator<T2>& b) noexcept;
39 // The name resource_adaptor_imp is for exposition only.
40 template <class Allocator> class resource_adaptor_imp;
42 template <class Allocator>
43 using resource_adaptor = resource_adaptor_imp<
44 allocator_traits<Allocator>::rebind_alloc<char>>;
46 // Global memory resources
47 memory_resource* new_delete_resource() noexcept;
48 memory_resource* null_memory_resource() noexcept;
50 // The default memory resource
51 memory_resource* set_default_resource(memory_resource* r) noexcept;
52 memory_resource* get_default_resource() noexcept;
54 // Standard memory resources
56 class synchronized_pool_resource;
57 class unsynchronized_pool_resource;
58 class monotonic_buffer_resource;
61 } // namespace fundamentals_v1
62 } // namespace experimental
67 #include <__assert> // all public C++ headers provide the assertion handler
68 #include <__memory/allocator_traits.h>
69 #include <__type_traits/aligned_storage.h>
70 #include <__utility/move.h>
72 #include <experimental/__config>
73 #include <experimental/__memory>
79 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
80 # pragma GCC system_header
84 #include <__undef_macros>
86 _LIBCPP_BEGIN_NAMESPACE_LFTS_PMR
88 #define _LIBCPP_DEPCREATED_MEMORY_RESOURCE(name) \
89 _LIBCPP_DEPRECATED_("'std::experimental::pmr::" name \
90 "' is deprecated and will be removed in LLVM 18. Use 'std::pmr::" name "' instead.")
92 #ifndef _LIBCPP_CXX03_LANG
94 // Round __s up to next multiple of __a.
95 inline _LIBCPP_INLINE_VISIBILITY
96 size_t __aligned_allocation_size(size_t __s, size_t __a) _NOEXCEPT
98 _LIBCPP_ASSERT_UNCATEGORIZED(__s + __a > __s, "aligned allocation size overflows");
99 return (__s + __a - 1) & ~(__a - 1);
102 // 8.5, memory.resource
103 class _LIBCPP_DEPCREATED_MEMORY_RESOURCE("memory_resource") _LIBCPP_EXPORTED_FROM_ABI memory_resource
105 static const size_t __max_align = _LIBCPP_ALIGNOF(max_align_t);
107 // 8.5.2, memory.resource.public
109 _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual ~memory_resource() = default;
111 _LIBCPP_INLINE_VISIBILITY
112 void* allocate(size_t __bytes, size_t __align = __max_align)
113 { return do_allocate(__bytes, __align); }
115 _LIBCPP_INLINE_VISIBILITY
116 void deallocate(void * __p, size_t __bytes, size_t __align = __max_align)
117 { do_deallocate(__p, __bytes, __align); }
119 _LIBCPP_INLINE_VISIBILITY
120 bool is_equal(memory_resource const & __other) const _NOEXCEPT
121 { return do_is_equal(__other); }
123 // 8.5.3, memory.resource.priv
125 virtual void* do_allocate(size_t, size_t) = 0;
126 virtual void do_deallocate(void*, size_t, size_t) = 0;
127 virtual bool do_is_equal(memory_resource const &) const _NOEXCEPT = 0;
130 // 8.5.4, memory.resource.eq
131 _LIBCPP_DEPCREATED_MEMORY_RESOURCE("operator==(memory_resource, memory_resource)") inline _LIBCPP_INLINE_VISIBILITY
132 bool operator==(memory_resource const & __lhs,
133 memory_resource const & __rhs) _NOEXCEPT
135 return &__lhs == &__rhs || __lhs.is_equal(__rhs);
138 _LIBCPP_DEPCREATED_MEMORY_RESOURCE("operator!=(memory_resource, memory_resource)") inline _LIBCPP_INLINE_VISIBILITY
139 bool operator!=(memory_resource const & __lhs,
140 memory_resource const & __rhs) _NOEXCEPT
142 return !(__lhs == __rhs);
145 _LIBCPP_DEPCREATED_MEMORY_RESOURCE("new_delete_resource()") _LIBCPP_EXPORTED_FROM_ABI
146 memory_resource * new_delete_resource() _NOEXCEPT;
148 _LIBCPP_DEPCREATED_MEMORY_RESOURCE("null_memory_resource()") _LIBCPP_EXPORTED_FROM_ABI
149 memory_resource * null_memory_resource() _NOEXCEPT;
151 _LIBCPP_DEPCREATED_MEMORY_RESOURCE("get_default_resource()") _LIBCPP_EXPORTED_FROM_ABI
152 memory_resource * get_default_resource() _NOEXCEPT;
154 _LIBCPP_DEPCREATED_MEMORY_RESOURCE("set_default_resource()") _LIBCPP_EXPORTED_FROM_ABI
155 memory_resource * set_default_resource(memory_resource * __new_res) _NOEXCEPT;
157 // 8.6, memory.polymorphic.allocator.class
159 // 8.6.1, memory.polymorphic.allocator.overview
160 template <class _ValueType>
161 class _LIBCPP_DEPCREATED_MEMORY_RESOURCE("polymorphic_allocator") _LIBCPP_TEMPLATE_VIS polymorphic_allocator
164 typedef _ValueType value_type;
166 // 8.6.2, memory.polymorphic.allocator.ctor
167 _LIBCPP_INLINE_VISIBILITY
168 polymorphic_allocator() _NOEXCEPT
169 : __res_(_VSTD_LFTS_PMR::get_default_resource())
172 _LIBCPP_INLINE_VISIBILITY
173 polymorphic_allocator(memory_resource * __r) _NOEXCEPT
177 _LIBCPP_HIDE_FROM_ABI polymorphic_allocator(polymorphic_allocator const &) = default;
180 _LIBCPP_INLINE_VISIBILITY
181 polymorphic_allocator(polymorphic_allocator<_Tp> const & __other) _NOEXCEPT
182 : __res_(__other.resource())
185 polymorphic_allocator &
186 operator=(polymorphic_allocator const &) = delete;
188 // 8.6.3, memory.polymorphic.allocator.mem
189 _LIBCPP_INLINE_VISIBILITY
190 _ValueType* allocate(size_t __n) {
191 if (__n > __max_size())
192 __throw_bad_array_new_length();
193 return static_cast<_ValueType*>(
194 __res_->allocate(__n * sizeof(_ValueType), _LIBCPP_ALIGNOF(_ValueType))
198 _LIBCPP_INLINE_VISIBILITY
199 void deallocate(_ValueType * __p, size_t __n) _NOEXCEPT {
200 _LIBCPP_ASSERT_UNCATEGORIZED(__n <= __max_size(),
201 "deallocate called for size which exceeds max_size()");
202 __res_->deallocate(__p, __n * sizeof(_ValueType), _LIBCPP_ALIGNOF(_ValueType));
205 template <class _Tp, class ..._Ts>
206 _LIBCPP_INLINE_VISIBILITY
207 void construct(_Tp* __p, _Ts &&... __args)
209 _VSTD_LFTS::__lfts_user_alloc_construct(
210 __p, *this, _VSTD::forward<_Ts>(__args)...
214 template <class _T1, class _T2, class ..._Args1, class ..._Args2>
215 _LIBCPP_INLINE_VISIBILITY
216 void construct(pair<_T1, _T2>* __p, piecewise_construct_t,
217 tuple<_Args1...> __x, tuple<_Args2...> __y)
219 ::new ((void*)__p) pair<_T1, _T2>(piecewise_construct
221 typename __lfts_uses_alloc_ctor<
222 _T1, polymorphic_allocator&, _Args1...
225 , typename __make_tuple_indices<sizeof...(_Args1)>::type{}
228 typename __lfts_uses_alloc_ctor<
229 _T2, polymorphic_allocator&, _Args2...
232 , typename __make_tuple_indices<sizeof...(_Args2)>::type{}
237 template <class _T1, class _T2>
238 _LIBCPP_INLINE_VISIBILITY
239 void construct(pair<_T1, _T2>* __p) {
240 construct(__p, piecewise_construct, tuple<>(), tuple<>());
243 template <class _T1, class _T2, class _Up, class _Vp>
244 _LIBCPP_INLINE_VISIBILITY
245 void construct(pair<_T1, _T2> * __p, _Up && __u, _Vp && __v) {
246 construct(__p, piecewise_construct
247 , _VSTD::forward_as_tuple(_VSTD::forward<_Up>(__u))
248 , _VSTD::forward_as_tuple(_VSTD::forward<_Vp>(__v)));
251 template <class _T1, class _T2, class _U1, class _U2>
252 _LIBCPP_INLINE_VISIBILITY
253 void construct(pair<_T1, _T2> * __p, pair<_U1, _U2> const & __pr) {
254 construct(__p, piecewise_construct
255 , _VSTD::forward_as_tuple(__pr.first)
256 , _VSTD::forward_as_tuple(__pr.second));
259 template <class _T1, class _T2, class _U1, class _U2>
260 _LIBCPP_INLINE_VISIBILITY
261 void construct(pair<_T1, _T2> * __p, pair<_U1, _U2> && __pr){
262 construct(__p, piecewise_construct
263 , _VSTD::forward_as_tuple(_VSTD::forward<_U1>(__pr.first))
264 , _VSTD::forward_as_tuple(_VSTD::forward<_U2>(__pr.second)));
268 _LIBCPP_INLINE_VISIBILITY
269 void destroy(_Tp * __p) _NOEXCEPT
272 _LIBCPP_INLINE_VISIBILITY
273 polymorphic_allocator
274 select_on_container_copy_construction() const _NOEXCEPT
275 { return polymorphic_allocator(); }
277 _LIBCPP_INLINE_VISIBILITY
278 memory_resource * resource() const _NOEXCEPT
282 template <class ..._Args, size_t ..._Idx>
283 _LIBCPP_INLINE_VISIBILITY
285 __transform_tuple(integral_constant<int, 0>, tuple<_Args...>&& __t,
286 __tuple_indices<_Idx...>) const
288 return _VSTD::forward_as_tuple(_VSTD::get<_Idx>(_VSTD::move(__t))...);
291 template <class ..._Args, size_t ..._Idx>
292 _LIBCPP_INLINE_VISIBILITY
293 tuple<allocator_arg_t const&, polymorphic_allocator&, _Args&&...>
294 __transform_tuple(integral_constant<int, 1>, tuple<_Args...> && __t,
295 __tuple_indices<_Idx...>)
297 using _Tup = tuple<allocator_arg_t const&, polymorphic_allocator&, _Args&&...>;
298 return _Tup(allocator_arg, *this,
299 _VSTD::get<_Idx>(_VSTD::move(__t))...);
302 template <class ..._Args, size_t ..._Idx>
303 _LIBCPP_INLINE_VISIBILITY
304 tuple<_Args&&..., polymorphic_allocator&>
305 __transform_tuple(integral_constant<int, 2>, tuple<_Args...> && __t,
306 __tuple_indices<_Idx...>)
308 using _Tup = tuple<_Args&&..., polymorphic_allocator&>;
309 return _Tup(_VSTD::get<_Idx>(_VSTD::move(__t))..., *this);
312 _LIBCPP_INLINE_VISIBILITY
313 size_t __max_size() const _NOEXCEPT
314 { return numeric_limits<size_t>::max() / sizeof(value_type); }
316 memory_resource * __res_;
319 // 8.6.4, memory.polymorphic.allocator.eq
321 template <class _Tp, class _Up>
322 _LIBCPP_DEPCREATED_MEMORY_RESOURCE("operator==(const polymorphic_allocator&, const polymorphic_allocator&)")
323 inline _LIBCPP_INLINE_VISIBILITY
324 bool operator==(polymorphic_allocator<_Tp> const & __lhs,
325 polymorphic_allocator<_Up> const & __rhs) _NOEXCEPT
327 return *__lhs.resource() == *__rhs.resource();
330 template <class _Tp, class _Up>
331 _LIBCPP_DEPCREATED_MEMORY_RESOURCE("operator!=(const polymorphic_allocator&, const polymorphic_allocator&)")
332 inline _LIBCPP_INLINE_VISIBILITY
333 bool operator!=(polymorphic_allocator<_Tp> const & __lhs,
334 polymorphic_allocator<_Up> const & __rhs) _NOEXCEPT
336 return !(__lhs == __rhs);
339 // 8.7, memory.resource.adaptor
341 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
342 // 8.7.1, memory.resource.adaptor.overview
343 template <class _CharAlloc>
344 class _LIBCPP_TEMPLATE_VIS __resource_adaptor_imp
345 : public memory_resource
347 using _CTraits = allocator_traits<_CharAlloc>;
348 static_assert(is_same<typename _CTraits::value_type, char>::value
349 && is_same<typename _CTraits::pointer, char*>::value
350 && is_same<typename _CTraits::void_pointer, void*>::value, "");
352 static const size_t _MaxAlign = _LIBCPP_ALIGNOF(max_align_t);
354 using _Alloc = typename _CTraits::template rebind_alloc<
355 typename aligned_storage<_MaxAlign, _MaxAlign>::type
358 using _ValueType = typename _Alloc::value_type;
363 typedef _CharAlloc allocator_type;
365 _LIBCPP_HIDE_FROM_ABI __resource_adaptor_imp() = default;
366 _LIBCPP_HIDE_FROM_ABI __resource_adaptor_imp(__resource_adaptor_imp const &) = default;
367 _LIBCPP_HIDE_FROM_ABI __resource_adaptor_imp(__resource_adaptor_imp &&) = default;
369 // 8.7.2, memory.resource.adaptor.ctor
371 _LIBCPP_INLINE_VISIBILITY
372 explicit __resource_adaptor_imp(allocator_type const & __a)
376 _LIBCPP_INLINE_VISIBILITY
377 explicit __resource_adaptor_imp(allocator_type && __a)
378 : __alloc_(_VSTD::move(__a))
381 _LIBCPP_HIDE_FROM_ABI __resource_adaptor_imp &
382 operator=(__resource_adaptor_imp const &) = default;
384 _LIBCPP_INLINE_VISIBILITY
385 allocator_type get_allocator() const
388 // 8.7.3, memory.resource.adaptor.mem
390 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void * do_allocate(size_t __bytes, size_t) override
392 if (__bytes > __max_size())
393 __throw_bad_array_new_length();
394 size_t __s = __aligned_allocation_size(__bytes, _MaxAlign) / _MaxAlign;
395 return __alloc_.allocate(__s);
398 _LIBCPP_HIDE_FROM_ABI_VIRTUAL void do_deallocate(void * __p, size_t __bytes, size_t) override
400 _LIBCPP_ASSERT_UNCATEGORIZED(__bytes <= __max_size(),
401 "do_deallocate called for size which exceeds the maximum allocation size");
402 size_t __s = __aligned_allocation_size(__bytes, _MaxAlign) / _MaxAlign;
403 __alloc_.deallocate((_ValueType*)__p, __s);
406 _LIBCPP_HIDE_FROM_ABI_VIRTUAL bool do_is_equal(memory_resource const & __other) const _NOEXCEPT override {
407 __resource_adaptor_imp const * __p
408 = dynamic_cast<__resource_adaptor_imp const *>(&__other);
409 return __p ? __alloc_ == __p->__alloc_ : false;
412 _LIBCPP_INLINE_VISIBILITY
413 size_t __max_size() const _NOEXCEPT {
414 return numeric_limits<size_t>::max() - _MaxAlign;
418 template <class _Alloc>
419 using resource_adaptor = __resource_adaptor_imp<
420 typename allocator_traits<_Alloc>::template rebind_alloc<char>
422 _LIBCPP_SUPPRESS_DEPRECATED_POP
424 #endif // _LIBCPP_CXX03_LANG
426 _LIBCPP_END_NAMESPACE_LFTS_PMR
430 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
440 # include <type_traits>
444 #endif /* _LIBCPP_EXPERIMENTAL_MEMORY_RESOURCE */