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
71 #include <experimental/__config>
72 #include <experimental/__memory>
77 #include <type_traits>
80 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
81 #pragma GCC system_header
85 #include <__undef_macros>
87 _LIBCPP_BEGIN_NAMESPACE_LFTS_PMR
89 // Round __s up to next multiple of __a.
90 inline _LIBCPP_INLINE_VISIBILITY
91 size_t __aligned_allocation_size(size_t __s, size_t __a) _NOEXCEPT
93 _LIBCPP_ASSERT(__s + __a > __s, "aligned allocation size overflows");
94 return (__s + __a - 1) & ~(__a - 1);
97 // 8.5, memory.resource
98 class _LIBCPP_TYPE_VIS memory_resource
100 static const size_t __max_align = _LIBCPP_ALIGNOF(max_align_t);
102 // 8.5.2, memory.resource.public
104 virtual ~memory_resource() = default;
106 _LIBCPP_INLINE_VISIBILITY
107 void* allocate(size_t __bytes, size_t __align = __max_align)
108 { return do_allocate(__bytes, __align); }
110 _LIBCPP_INLINE_VISIBILITY
111 void deallocate(void * __p, size_t __bytes, size_t __align = __max_align)
112 { do_deallocate(__p, __bytes, __align); }
114 _LIBCPP_INLINE_VISIBILITY
115 bool is_equal(memory_resource const & __other) const _NOEXCEPT
116 { return do_is_equal(__other); }
118 // 8.5.3, memory.resource.priv
120 virtual void* do_allocate(size_t, size_t) = 0;
121 virtual void do_deallocate(void*, size_t, size_t) = 0;
122 virtual bool do_is_equal(memory_resource const &) const _NOEXCEPT = 0;
125 // 8.5.4, memory.resource.eq
126 inline _LIBCPP_INLINE_VISIBILITY
127 bool operator==(memory_resource const & __lhs,
128 memory_resource const & __rhs) _NOEXCEPT
130 return &__lhs == &__rhs || __lhs.is_equal(__rhs);
133 inline _LIBCPP_INLINE_VISIBILITY
134 bool operator!=(memory_resource const & __lhs,
135 memory_resource const & __rhs) _NOEXCEPT
137 return !(__lhs == __rhs);
141 memory_resource * new_delete_resource() _NOEXCEPT;
144 memory_resource * null_memory_resource() _NOEXCEPT;
147 memory_resource * get_default_resource() _NOEXCEPT;
150 memory_resource * set_default_resource(memory_resource * __new_res) _NOEXCEPT;
152 // 8.6, memory.polymorphic.allocator.class
154 // 8.6.1, memory.polymorphic.allocator.overview
155 template <class _ValueType>
156 class _LIBCPP_TEMPLATE_VIS polymorphic_allocator
159 typedef _ValueType value_type;
161 // 8.6.2, memory.polymorphic.allocator.ctor
162 _LIBCPP_INLINE_VISIBILITY
163 polymorphic_allocator() _NOEXCEPT
164 : __res_(_VSTD_LFTS_PMR::get_default_resource())
167 _LIBCPP_INLINE_VISIBILITY
168 polymorphic_allocator(memory_resource * __r) _NOEXCEPT
172 polymorphic_allocator(polymorphic_allocator const &) = default;
175 _LIBCPP_INLINE_VISIBILITY
176 polymorphic_allocator(polymorphic_allocator<_Tp> const & __other) _NOEXCEPT
177 : __res_(__other.resource())
180 polymorphic_allocator &
181 operator=(polymorphic_allocator const &) = delete;
183 // 8.6.3, memory.polymorphic.allocator.mem
184 _LIBCPP_INLINE_VISIBILITY
185 _ValueType* allocate(size_t __n) {
186 if (__n > __max_size())
187 __throw_bad_array_new_length();
188 return static_cast<_ValueType*>(
189 __res_->allocate(__n * sizeof(_ValueType), _LIBCPP_ALIGNOF(_ValueType))
193 _LIBCPP_INLINE_VISIBILITY
194 void deallocate(_ValueType * __p, size_t __n) _NOEXCEPT {
195 _LIBCPP_ASSERT(__n <= __max_size(),
196 "deallocate called for size which exceeds max_size()");
197 __res_->deallocate(__p, __n * sizeof(_ValueType), _LIBCPP_ALIGNOF(_ValueType));
200 template <class _Tp, class ..._Ts>
201 _LIBCPP_INLINE_VISIBILITY
202 void construct(_Tp* __p, _Ts &&... __args)
204 _VSTD_LFTS::__lfts_user_alloc_construct(
205 __p, *this, _VSTD::forward<_Ts>(__args)...
209 template <class _T1, class _T2, class ..._Args1, class ..._Args2>
210 _LIBCPP_INLINE_VISIBILITY
211 void construct(pair<_T1, _T2>* __p, piecewise_construct_t,
212 tuple<_Args1...> __x, tuple<_Args2...> __y)
214 ::new ((void*)__p) pair<_T1, _T2>(piecewise_construct
216 typename __lfts_uses_alloc_ctor<
217 _T1, polymorphic_allocator&, _Args1...
220 , typename __make_tuple_indices<sizeof...(_Args1)>::type{}
223 typename __lfts_uses_alloc_ctor<
224 _T2, polymorphic_allocator&, _Args2...
227 , typename __make_tuple_indices<sizeof...(_Args2)>::type{}
232 template <class _T1, class _T2>
233 _LIBCPP_INLINE_VISIBILITY
234 void construct(pair<_T1, _T2>* __p) {
235 construct(__p, piecewise_construct, tuple<>(), tuple<>());
238 template <class _T1, class _T2, class _Up, class _Vp>
239 _LIBCPP_INLINE_VISIBILITY
240 void construct(pair<_T1, _T2> * __p, _Up && __u, _Vp && __v) {
241 construct(__p, piecewise_construct
242 , _VSTD::forward_as_tuple(_VSTD::forward<_Up>(__u))
243 , _VSTD::forward_as_tuple(_VSTD::forward<_Vp>(__v)));
246 template <class _T1, class _T2, class _U1, class _U2>
247 _LIBCPP_INLINE_VISIBILITY
248 void construct(pair<_T1, _T2> * __p, pair<_U1, _U2> const & __pr) {
249 construct(__p, piecewise_construct
250 , _VSTD::forward_as_tuple(__pr.first)
251 , _VSTD::forward_as_tuple(__pr.second));
254 template <class _T1, class _T2, class _U1, class _U2>
255 _LIBCPP_INLINE_VISIBILITY
256 void construct(pair<_T1, _T2> * __p, pair<_U1, _U2> && __pr){
257 construct(__p, piecewise_construct
258 , _VSTD::forward_as_tuple(_VSTD::forward<_U1>(__pr.first))
259 , _VSTD::forward_as_tuple(_VSTD::forward<_U2>(__pr.second)));
263 _LIBCPP_INLINE_VISIBILITY
264 void destroy(_Tp * __p) _NOEXCEPT
267 _LIBCPP_INLINE_VISIBILITY
268 polymorphic_allocator
269 select_on_container_copy_construction() const _NOEXCEPT
270 { return polymorphic_allocator(); }
272 _LIBCPP_INLINE_VISIBILITY
273 memory_resource * resource() const _NOEXCEPT
277 template <class ..._Args, size_t ..._Idx>
278 _LIBCPP_INLINE_VISIBILITY
280 __transform_tuple(integral_constant<int, 0>, tuple<_Args...>&& __t,
281 __tuple_indices<_Idx...>) const
283 return _VSTD::forward_as_tuple(_VSTD::get<_Idx>(_VSTD::move(__t))...);
286 template <class ..._Args, size_t ..._Idx>
287 _LIBCPP_INLINE_VISIBILITY
288 tuple<allocator_arg_t const&, polymorphic_allocator&, _Args&&...>
289 __transform_tuple(integral_constant<int, 1>, tuple<_Args...> && __t,
290 __tuple_indices<_Idx...>)
292 using _Tup = tuple<allocator_arg_t const&, polymorphic_allocator&, _Args&&...>;
293 return _Tup(allocator_arg, *this,
294 _VSTD::get<_Idx>(_VSTD::move(__t))...);
297 template <class ..._Args, size_t ..._Idx>
298 _LIBCPP_INLINE_VISIBILITY
299 tuple<_Args&&..., polymorphic_allocator&>
300 __transform_tuple(integral_constant<int, 2>, tuple<_Args...> && __t,
301 __tuple_indices<_Idx...>)
303 using _Tup = tuple<_Args&&..., polymorphic_allocator&>;
304 return _Tup(_VSTD::get<_Idx>(_VSTD::move(__t))..., *this);
307 _LIBCPP_INLINE_VISIBILITY
308 size_t __max_size() const _NOEXCEPT
309 { return numeric_limits<size_t>::max() / sizeof(value_type); }
311 memory_resource * __res_;
314 // 8.6.4, memory.polymorphic.allocator.eq
316 template <class _Tp, class _Up>
317 inline _LIBCPP_INLINE_VISIBILITY
318 bool operator==(polymorphic_allocator<_Tp> const & __lhs,
319 polymorphic_allocator<_Up> const & __rhs) _NOEXCEPT
321 return *__lhs.resource() == *__rhs.resource();
324 template <class _Tp, class _Up>
325 inline _LIBCPP_INLINE_VISIBILITY
326 bool operator!=(polymorphic_allocator<_Tp> const & __lhs,
327 polymorphic_allocator<_Up> const & __rhs) _NOEXCEPT
329 return !(__lhs == __rhs);
332 // 8.7, memory.resource.adaptor
334 // 8.7.1, memory.resource.adaptor.overview
335 template <class _CharAlloc>
336 class _LIBCPP_TEMPLATE_VIS __resource_adaptor_imp
337 : public memory_resource
339 using _CTraits = allocator_traits<_CharAlloc>;
340 static_assert(is_same<typename _CTraits::value_type, char>::value
341 && is_same<typename _CTraits::pointer, char*>::value
342 && is_same<typename _CTraits::void_pointer, void*>::value, "");
344 static const size_t _MaxAlign = _LIBCPP_ALIGNOF(max_align_t);
346 using _Alloc = typename _CTraits::template rebind_alloc<
347 typename aligned_storage<_MaxAlign, _MaxAlign>::type
350 using _ValueType = typename _Alloc::value_type;
355 typedef _CharAlloc allocator_type;
357 __resource_adaptor_imp() = default;
358 __resource_adaptor_imp(__resource_adaptor_imp const &) = default;
359 __resource_adaptor_imp(__resource_adaptor_imp &&) = default;
361 // 8.7.2, memory.resource.adaptor.ctor
363 _LIBCPP_INLINE_VISIBILITY
364 explicit __resource_adaptor_imp(allocator_type const & __a)
368 _LIBCPP_INLINE_VISIBILITY
369 explicit __resource_adaptor_imp(allocator_type && __a)
370 : __alloc_(_VSTD::move(__a))
373 __resource_adaptor_imp &
374 operator=(__resource_adaptor_imp const &) = default;
376 _LIBCPP_INLINE_VISIBILITY
377 allocator_type get_allocator() const
380 // 8.7.3, memory.resource.adaptor.mem
382 virtual void * do_allocate(size_t __bytes, size_t)
384 if (__bytes > __max_size())
385 __throw_bad_array_new_length();
386 size_t __s = __aligned_allocation_size(__bytes, _MaxAlign) / _MaxAlign;
387 return __alloc_.allocate(__s);
390 virtual void do_deallocate(void * __p, size_t __bytes, size_t)
392 _LIBCPP_ASSERT(__bytes <= __max_size(),
393 "do_deallocate called for size which exceeds the maximum allocation size");
394 size_t __s = __aligned_allocation_size(__bytes, _MaxAlign) / _MaxAlign;
395 __alloc_.deallocate((_ValueType*)__p, __s);
398 virtual bool do_is_equal(memory_resource const & __other) const _NOEXCEPT {
399 __resource_adaptor_imp const * __p
400 = dynamic_cast<__resource_adaptor_imp const *>(&__other);
401 return __p ? __alloc_ == __p->__alloc_ : false;
404 _LIBCPP_INLINE_VISIBILITY
405 size_t __max_size() const _NOEXCEPT {
406 return numeric_limits<size_t>::max() - _MaxAlign;
410 template <class _Alloc>
411 using resource_adaptor = __resource_adaptor_imp<
412 typename allocator_traits<_Alloc>::template rebind_alloc<char>
415 _LIBCPP_END_NAMESPACE_LFTS_PMR
419 #endif /* _LIBCPP_EXPERIMENTAL_MEMORY_RESOURCE */