xref: /openbsd-src/gnu/llvm/libcxx/include/experimental/memory_resource (revision 4bdff4bed0e3d54e55670334c7d0077db4170f86)
1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
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
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_EXPERIMENTAL_MEMORY_RESOURCE
11#define _LIBCPP_EXPERIMENTAL_MEMORY_RESOURCE
12
13/**
14    experimental/memory_resource synopsis
15
16// C++1y
17
18namespace std {
19namespace experimental {
20inline namespace fundamentals_v1 {
21namespace pmr {
22
23  class memory_resource;
24
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;
29
30  template <class Tp> class polymorphic_allocator;
31
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;
38
39  // The name resource_adaptor_imp is for exposition only.
40  template <class Allocator> class resource_adaptor_imp;
41
42  template <class Allocator>
43    using resource_adaptor = resource_adaptor_imp<
44      allocator_traits<Allocator>::rebind_alloc<char>>;
45
46  // Global memory resources
47  memory_resource* new_delete_resource() noexcept;
48  memory_resource* null_memory_resource() noexcept;
49
50  // The default memory resource
51  memory_resource* set_default_resource(memory_resource* r) noexcept;
52  memory_resource* get_default_resource() noexcept;
53
54  // Standard memory resources
55  struct pool_options;
56  class synchronized_pool_resource;
57  class unsynchronized_pool_resource;
58  class monotonic_buffer_resource;
59
60} // namespace pmr
61} // namespace fundamentals_v1
62} // namespace experimental
63} // namespace std
64
65 */
66
67#include <__assert> // all public C++ headers provide the assertion handler
68#include <__memory/allocator_traits.h>
69#include <__utility/move.h>
70#include <cstddef>
71#include <cstdlib>
72#include <experimental/__config>
73#include <experimental/__memory>
74#include <limits>
75#include <new>
76#include <stdexcept>
77#include <tuple>
78#include <type_traits>
79
80#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
81#  pragma GCC system_header
82#endif
83
84_LIBCPP_PUSH_MACROS
85#include <__undef_macros>
86
87_LIBCPP_BEGIN_NAMESPACE_LFTS_PMR
88
89#define _LIBCPP_DEPCREATED_MEMORY_RESOURCE(name)                                                                       \
90  _LIBCPP_DEPRECATED_("'std::experimental::pmr::" name                                                                 \
91                      "' is deprecated and will be removed in LLVM 18. Use 'std::pmr::" name "' instead.")
92
93#ifndef _LIBCPP_CXX03_LANG
94
95// Round __s up to next multiple of __a.
96inline _LIBCPP_INLINE_VISIBILITY
97size_t __aligned_allocation_size(size_t __s, size_t __a) _NOEXCEPT
98{
99    _LIBCPP_ASSERT(__s + __a > __s, "aligned allocation size overflows");
100    return (__s + __a - 1) & ~(__a - 1);
101}
102
103// 8.5, memory.resource
104class _LIBCPP_DEPCREATED_MEMORY_RESOURCE("memory_resource") _LIBCPP_TYPE_VIS memory_resource
105{
106    static const size_t __max_align = _LIBCPP_ALIGNOF(max_align_t);
107
108// 8.5.2, memory.resource.public
109public:
110    virtual ~memory_resource() = default;
111
112    _LIBCPP_INLINE_VISIBILITY
113    void* allocate(size_t __bytes, size_t __align = __max_align)
114        { return do_allocate(__bytes, __align); }
115
116    _LIBCPP_INLINE_VISIBILITY
117    void deallocate(void * __p, size_t __bytes, size_t __align = __max_align)
118        { do_deallocate(__p, __bytes, __align); }
119
120    _LIBCPP_INLINE_VISIBILITY
121    bool is_equal(memory_resource const & __other) const _NOEXCEPT
122        { return do_is_equal(__other); }
123
124// 8.5.3, memory.resource.priv
125private:
126    virtual void* do_allocate(size_t, size_t) = 0;
127    virtual void do_deallocate(void*, size_t, size_t) = 0;
128    virtual bool do_is_equal(memory_resource const &) const _NOEXCEPT = 0;
129};
130
131// 8.5.4, memory.resource.eq
132_LIBCPP_DEPCREATED_MEMORY_RESOURCE("operator==(memory_resource, memory_resource)") inline _LIBCPP_INLINE_VISIBILITY
133bool operator==(memory_resource const & __lhs,
134                memory_resource const & __rhs) _NOEXCEPT
135{
136    return &__lhs == &__rhs || __lhs.is_equal(__rhs);
137}
138
139_LIBCPP_DEPCREATED_MEMORY_RESOURCE("operator!=(memory_resource, memory_resource)") inline _LIBCPP_INLINE_VISIBILITY
140bool operator!=(memory_resource const & __lhs,
141                memory_resource const & __rhs) _NOEXCEPT
142{
143    return !(__lhs == __rhs);
144}
145
146_LIBCPP_DEPCREATED_MEMORY_RESOURCE("new_delete_resource()") _LIBCPP_FUNC_VIS
147memory_resource * new_delete_resource() _NOEXCEPT;
148
149_LIBCPP_DEPCREATED_MEMORY_RESOURCE("null_memory_resource()") _LIBCPP_FUNC_VIS
150memory_resource * null_memory_resource() _NOEXCEPT;
151
152_LIBCPP_DEPCREATED_MEMORY_RESOURCE("get_default_resource()") _LIBCPP_FUNC_VIS
153memory_resource * get_default_resource() _NOEXCEPT;
154
155_LIBCPP_DEPCREATED_MEMORY_RESOURCE("set_default_resource()") _LIBCPP_FUNC_VIS
156memory_resource * set_default_resource(memory_resource * __new_res) _NOEXCEPT;
157
158// 8.6, memory.polymorphic.allocator.class
159
160// 8.6.1, memory.polymorphic.allocator.overview
161template <class _ValueType>
162class _LIBCPP_DEPCREATED_MEMORY_RESOURCE("polymorphic_allocator") _LIBCPP_TEMPLATE_VIS polymorphic_allocator
163{
164public:
165    typedef _ValueType value_type;
166
167    // 8.6.2, memory.polymorphic.allocator.ctor
168    _LIBCPP_INLINE_VISIBILITY
169    polymorphic_allocator() _NOEXCEPT
170      : __res_(_VSTD_LFTS_PMR::get_default_resource())
171    {}
172
173    _LIBCPP_INLINE_VISIBILITY
174    polymorphic_allocator(memory_resource * __r) _NOEXCEPT
175      : __res_(__r)
176    {}
177
178    polymorphic_allocator(polymorphic_allocator const &) = default;
179
180    template <class _Tp>
181    _LIBCPP_INLINE_VISIBILITY
182    polymorphic_allocator(polymorphic_allocator<_Tp> const & __other) _NOEXCEPT
183      : __res_(__other.resource())
184    {}
185
186    polymorphic_allocator &
187    operator=(polymorphic_allocator const &) = delete;
188
189    // 8.6.3, memory.polymorphic.allocator.mem
190    _LIBCPP_INLINE_VISIBILITY
191    _ValueType* allocate(size_t __n) {
192        if (__n > __max_size())
193            __throw_bad_array_new_length();
194        return static_cast<_ValueType*>(
195            __res_->allocate(__n * sizeof(_ValueType), _LIBCPP_ALIGNOF(_ValueType))
196        );
197    }
198
199    _LIBCPP_INLINE_VISIBILITY
200    void deallocate(_ValueType * __p, size_t __n) _NOEXCEPT {
201        _LIBCPP_ASSERT(__n <= __max_size(),
202                       "deallocate called for size which exceeds max_size()");
203        __res_->deallocate(__p, __n * sizeof(_ValueType), _LIBCPP_ALIGNOF(_ValueType));
204    }
205
206    template <class _Tp, class ..._Ts>
207    _LIBCPP_INLINE_VISIBILITY
208    void construct(_Tp* __p, _Ts &&... __args)
209    {
210        _VSTD_LFTS::__lfts_user_alloc_construct(
211            __p, *this, _VSTD::forward<_Ts>(__args)...
212          );
213    }
214
215    template <class _T1, class _T2, class ..._Args1, class ..._Args2>
216    _LIBCPP_INLINE_VISIBILITY
217    void construct(pair<_T1, _T2>* __p, piecewise_construct_t,
218                   tuple<_Args1...> __x, tuple<_Args2...> __y)
219    {
220        ::new ((void*)__p) pair<_T1, _T2>(piecewise_construct
221          , __transform_tuple(
222              typename __lfts_uses_alloc_ctor<
223                  _T1, polymorphic_allocator&, _Args1...
224              >::type()
225            , _VSTD::move(__x)
226            , typename __make_tuple_indices<sizeof...(_Args1)>::type{}
227          )
228          , __transform_tuple(
229              typename __lfts_uses_alloc_ctor<
230                  _T2, polymorphic_allocator&, _Args2...
231              >::type()
232            , _VSTD::move(__y)
233            , typename __make_tuple_indices<sizeof...(_Args2)>::type{}
234          )
235        );
236    }
237
238    template <class _T1, class _T2>
239    _LIBCPP_INLINE_VISIBILITY
240    void construct(pair<_T1, _T2>* __p) {
241        construct(__p, piecewise_construct, tuple<>(), tuple<>());
242    }
243
244    template <class _T1, class _T2, class _Up, class _Vp>
245    _LIBCPP_INLINE_VISIBILITY
246    void construct(pair<_T1, _T2> * __p, _Up && __u, _Vp && __v) {
247        construct(__p, piecewise_construct
248          , _VSTD::forward_as_tuple(_VSTD::forward<_Up>(__u))
249          , _VSTD::forward_as_tuple(_VSTD::forward<_Vp>(__v)));
250    }
251
252    template <class _T1, class _T2, class _U1, class _U2>
253    _LIBCPP_INLINE_VISIBILITY
254    void construct(pair<_T1, _T2> * __p, pair<_U1, _U2> const & __pr) {
255        construct(__p, piecewise_construct
256            , _VSTD::forward_as_tuple(__pr.first)
257            , _VSTD::forward_as_tuple(__pr.second));
258    }
259
260    template <class _T1, class _T2, class _U1, class _U2>
261    _LIBCPP_INLINE_VISIBILITY
262    void construct(pair<_T1, _T2> * __p, pair<_U1, _U2> && __pr){
263        construct(__p, piecewise_construct
264            , _VSTD::forward_as_tuple(_VSTD::forward<_U1>(__pr.first))
265            , _VSTD::forward_as_tuple(_VSTD::forward<_U2>(__pr.second)));
266    }
267
268    template <class _Tp>
269    _LIBCPP_INLINE_VISIBILITY
270    void destroy(_Tp * __p) _NOEXCEPT
271        { __p->~_Tp(); }
272
273    _LIBCPP_INLINE_VISIBILITY
274    polymorphic_allocator
275    select_on_container_copy_construction() const _NOEXCEPT
276        { return polymorphic_allocator(); }
277
278    _LIBCPP_INLINE_VISIBILITY
279    memory_resource * resource() const _NOEXCEPT
280        { return __res_; }
281
282private:
283    template <class ..._Args, size_t ..._Idx>
284    _LIBCPP_INLINE_VISIBILITY
285    tuple<_Args&&...>
286    __transform_tuple(integral_constant<int, 0>, tuple<_Args...>&& __t,
287                      __tuple_indices<_Idx...>) const
288    {
289        return _VSTD::forward_as_tuple(_VSTD::get<_Idx>(_VSTD::move(__t))...);
290    }
291
292    template <class ..._Args, size_t ..._Idx>
293    _LIBCPP_INLINE_VISIBILITY
294    tuple<allocator_arg_t const&, polymorphic_allocator&, _Args&&...>
295    __transform_tuple(integral_constant<int, 1>, tuple<_Args...> && __t,
296                      __tuple_indices<_Idx...>)
297    {
298        using _Tup = tuple<allocator_arg_t const&, polymorphic_allocator&, _Args&&...>;
299        return _Tup(allocator_arg, *this,
300                    _VSTD::get<_Idx>(_VSTD::move(__t))...);
301    }
302
303    template <class ..._Args, size_t ..._Idx>
304    _LIBCPP_INLINE_VISIBILITY
305    tuple<_Args&&..., polymorphic_allocator&>
306    __transform_tuple(integral_constant<int, 2>, tuple<_Args...> && __t,
307                      __tuple_indices<_Idx...>)
308    {
309        using _Tup = tuple<_Args&&..., polymorphic_allocator&>;
310        return _Tup(_VSTD::get<_Idx>(_VSTD::move(__t))..., *this);
311    }
312
313    _LIBCPP_INLINE_VISIBILITY
314    size_t __max_size() const _NOEXCEPT
315        { return numeric_limits<size_t>::max() / sizeof(value_type); }
316
317    memory_resource * __res_;
318};
319
320// 8.6.4, memory.polymorphic.allocator.eq
321
322template <class _Tp, class _Up>
323_LIBCPP_DEPCREATED_MEMORY_RESOURCE("operator==(const polymorphic_allocator&, const polymorphic_allocator&)")
324inline _LIBCPP_INLINE_VISIBILITY
325bool operator==(polymorphic_allocator<_Tp> const & __lhs,
326                polymorphic_allocator<_Up> const & __rhs) _NOEXCEPT
327{
328    return *__lhs.resource() == *__rhs.resource();
329}
330
331template <class _Tp, class _Up>
332_LIBCPP_DEPCREATED_MEMORY_RESOURCE("operator!=(const polymorphic_allocator&, const polymorphic_allocator&)")
333inline _LIBCPP_INLINE_VISIBILITY
334bool operator!=(polymorphic_allocator<_Tp> const & __lhs,
335                polymorphic_allocator<_Up> const & __rhs) _NOEXCEPT
336{
337    return !(__lhs == __rhs);
338}
339
340// 8.7, memory.resource.adaptor
341
342_LIBCPP_SUPPRESS_DEPRECATED_PUSH
343// 8.7.1, memory.resource.adaptor.overview
344template <class _CharAlloc>
345class _LIBCPP_TEMPLATE_VIS __resource_adaptor_imp
346  : public memory_resource
347{
348    using _CTraits = allocator_traits<_CharAlloc>;
349    static_assert(is_same<typename _CTraits::value_type, char>::value
350               && is_same<typename _CTraits::pointer, char*>::value
351               && is_same<typename _CTraits::void_pointer, void*>::value, "");
352
353    static const size_t _MaxAlign = _LIBCPP_ALIGNOF(max_align_t);
354
355    using _Alloc = typename _CTraits::template rebind_alloc<
356            typename aligned_storage<_MaxAlign, _MaxAlign>::type
357        >;
358
359    using _ValueType = typename _Alloc::value_type;
360
361    _Alloc __alloc_;
362
363public:
364    typedef _CharAlloc allocator_type;
365
366    __resource_adaptor_imp() = default;
367    __resource_adaptor_imp(__resource_adaptor_imp const &) = default;
368    __resource_adaptor_imp(__resource_adaptor_imp &&) = default;
369
370    // 8.7.2, memory.resource.adaptor.ctor
371
372    _LIBCPP_INLINE_VISIBILITY
373    explicit __resource_adaptor_imp(allocator_type const & __a)
374      : __alloc_(__a)
375    {}
376
377    _LIBCPP_INLINE_VISIBILITY
378    explicit __resource_adaptor_imp(allocator_type && __a)
379      : __alloc_(_VSTD::move(__a))
380    {}
381
382    __resource_adaptor_imp &
383    operator=(__resource_adaptor_imp const &) = default;
384
385    _LIBCPP_INLINE_VISIBILITY
386    allocator_type get_allocator() const
387    { return __alloc_; }
388
389// 8.7.3, memory.resource.adaptor.mem
390private:
391    void * do_allocate(size_t __bytes, size_t) override
392    {
393        if (__bytes > __max_size())
394            __throw_bad_array_new_length();
395        size_t __s = __aligned_allocation_size(__bytes, _MaxAlign) / _MaxAlign;
396        return __alloc_.allocate(__s);
397    }
398
399    void do_deallocate(void * __p, size_t __bytes, size_t) override
400    {
401        _LIBCPP_ASSERT(__bytes <= __max_size(),
402            "do_deallocate called for size which exceeds the maximum allocation size");
403        size_t __s = __aligned_allocation_size(__bytes, _MaxAlign) / _MaxAlign;
404        __alloc_.deallocate((_ValueType*)__p, __s);
405    }
406
407    bool do_is_equal(memory_resource const & __other) const _NOEXCEPT override {
408        __resource_adaptor_imp const * __p
409          = dynamic_cast<__resource_adaptor_imp const *>(&__other);
410        return __p  ? __alloc_ == __p->__alloc_ : false;
411    }
412
413    _LIBCPP_INLINE_VISIBILITY
414    size_t __max_size() const _NOEXCEPT {
415        return numeric_limits<size_t>::max() - _MaxAlign;
416    }
417};
418
419template <class _Alloc>
420using resource_adaptor = __resource_adaptor_imp<
421    typename allocator_traits<_Alloc>::template rebind_alloc<char>
422  >;
423_LIBCPP_SUPPRESS_DEPRECATED_POP
424
425#endif // _LIBCPP_CXX03_LANG
426
427_LIBCPP_END_NAMESPACE_LFTS_PMR
428
429_LIBCPP_POP_MACROS
430
431#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
432#  include <atomic>
433#  include <climits>
434#  include <concepts>
435#  include <cstring>
436#  include <ctime>
437#  include <iterator>
438#  include <memory>
439#  include <ratio>
440#  include <variant>
441#endif
442
443#endif /* _LIBCPP_EXPERIMENTAL_MEMORY_RESOURCE */
444