1// -*- C++ -*-
2//===------------------------ memory_resource -----------------------------===//
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 <experimental/__config>
68#include <experimental/__memory>
69#include <limits>
70#include <memory>
71#include <new>
72#include <stdexcept>
73#include <__tuple>
74#include <type_traits>
75#include <utility>
76#include <cstddef>
77#include <cstdlib>
78#include <__debug>
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// Round __s up to next multiple of __a.
90inline _LIBCPP_INLINE_VISIBILITY
91size_t __aligned_allocation_size(size_t __s, size_t __a) _NOEXCEPT
92{
93    _LIBCPP_ASSERT(__s + __a > __s, "aligned allocation size overflows");
94    return (__s + __a - 1) & ~(__a - 1);
95}
96
97// 8.5, memory.resource
98class _LIBCPP_TYPE_VIS memory_resource
99{
100    static const size_t __max_align = _LIBCPP_ALIGNOF(max_align_t);
101
102// 8.5.2, memory.resource.public
103public:
104    virtual ~memory_resource() = default;
105
106    _LIBCPP_INLINE_VISIBILITY
107    void* allocate(size_t __bytes, size_t __align = __max_align)
108        { return do_allocate(__bytes, __align); }
109
110    _LIBCPP_INLINE_VISIBILITY
111    void deallocate(void * __p, size_t __bytes, size_t __align = __max_align)
112        { do_deallocate(__p, __bytes, __align); }
113
114    _LIBCPP_INLINE_VISIBILITY
115    bool is_equal(memory_resource const & __other) const _NOEXCEPT
116        { return do_is_equal(__other); }
117
118// 8.5.3, memory.resource.priv
119protected:
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;
123};
124
125// 8.5.4, memory.resource.eq
126inline _LIBCPP_INLINE_VISIBILITY
127bool operator==(memory_resource const & __lhs,
128                memory_resource const & __rhs) _NOEXCEPT
129{
130    return &__lhs == &__rhs || __lhs.is_equal(__rhs);
131}
132
133inline _LIBCPP_INLINE_VISIBILITY
134bool operator!=(memory_resource const & __lhs,
135                memory_resource const & __rhs) _NOEXCEPT
136{
137    return !(__lhs == __rhs);
138}
139
140_LIBCPP_FUNC_VIS
141memory_resource * new_delete_resource() _NOEXCEPT;
142
143_LIBCPP_FUNC_VIS
144memory_resource * null_memory_resource() _NOEXCEPT;
145
146_LIBCPP_FUNC_VIS
147memory_resource * get_default_resource() _NOEXCEPT;
148
149_LIBCPP_FUNC_VIS
150memory_resource * set_default_resource(memory_resource * __new_res) _NOEXCEPT;
151
152// 8.6, memory.polymorphic.allocator.class
153
154// 8.6.1, memory.polymorphic.allocator.overview
155template <class _ValueType>
156class _LIBCPP_TEMPLATE_VIS polymorphic_allocator
157{
158public:
159    typedef _ValueType value_type;
160
161    // 8.6.2, memory.polymorphic.allocator.ctor
162    _LIBCPP_INLINE_VISIBILITY
163    polymorphic_allocator() _NOEXCEPT
164      : __res_(_VSTD_LFTS_PMR::get_default_resource())
165    {}
166
167    _LIBCPP_INLINE_VISIBILITY
168    polymorphic_allocator(memory_resource * __r) _NOEXCEPT
169      : __res_(__r)
170    {}
171
172    polymorphic_allocator(polymorphic_allocator const &) = default;
173
174    template <class _Tp>
175    _LIBCPP_INLINE_VISIBILITY
176    polymorphic_allocator(polymorphic_allocator<_Tp> const & __other) _NOEXCEPT
177      : __res_(__other.resource())
178    {}
179
180    polymorphic_allocator &
181    operator=(polymorphic_allocator const &) = delete;
182
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_length_error(
188                "std::experimental::pmr::polymorphic_allocator<T>::allocate(size_t n)"
189                " 'n' exceeds maximum supported size");
190        }
191        return static_cast<_ValueType*>(
192            __res_->allocate(__n * sizeof(_ValueType), _LIBCPP_ALIGNOF(_ValueType))
193        );
194    }
195
196    _LIBCPP_INLINE_VISIBILITY
197    void deallocate(_ValueType * __p, size_t __n) _NOEXCEPT {
198        _LIBCPP_ASSERT(__n <= __max_size(),
199                       "deallocate called for size which exceeds max_size()");
200        __res_->deallocate(__p, __n * sizeof(_ValueType), _LIBCPP_ALIGNOF(_ValueType));
201    }
202
203    template <class _Tp, class ..._Ts>
204    _LIBCPP_INLINE_VISIBILITY
205    void construct(_Tp* __p, _Ts &&... __args)
206    {
207        _VSTD_LFTS::__lfts_user_alloc_construct(
208            __p, *this, _VSTD::forward<_Ts>(__args)...
209          );
210    }
211
212    template <class _T1, class _T2, class ..._Args1, class ..._Args2>
213    _LIBCPP_INLINE_VISIBILITY
214    void construct(pair<_T1, _T2>* __p, piecewise_construct_t,
215                   tuple<_Args1...> __x, tuple<_Args2...> __y)
216    {
217        ::new ((void*)__p) pair<_T1, _T2>(piecewise_construct
218          , __transform_tuple(
219              typename __lfts_uses_alloc_ctor<
220                  _T1, polymorphic_allocator&, _Args1...
221              >::type()
222            , _VSTD::move(__x)
223            , typename __make_tuple_indices<sizeof...(_Args1)>::type{}
224          )
225          , __transform_tuple(
226              typename __lfts_uses_alloc_ctor<
227                  _T2, polymorphic_allocator&, _Args2...
228              >::type()
229            , _VSTD::move(__y)
230            , typename __make_tuple_indices<sizeof...(_Args2)>::type{}
231          )
232        );
233    }
234
235    template <class _T1, class _T2>
236    _LIBCPP_INLINE_VISIBILITY
237    void construct(pair<_T1, _T2>* __p) {
238        construct(__p, piecewise_construct, tuple<>(), tuple<>());
239    }
240
241    template <class _T1, class _T2, class _Up, class _Vp>
242    _LIBCPP_INLINE_VISIBILITY
243    void construct(pair<_T1, _T2> * __p, _Up && __u, _Vp && __v) {
244        construct(__p, piecewise_construct
245          , _VSTD::forward_as_tuple(_VSTD::forward<_Up>(__u))
246          , _VSTD::forward_as_tuple(_VSTD::forward<_Vp>(__v)));
247    }
248
249    template <class _T1, class _T2, class _U1, class _U2>
250    _LIBCPP_INLINE_VISIBILITY
251    void construct(pair<_T1, _T2> * __p, pair<_U1, _U2> const & __pr) {
252        construct(__p, piecewise_construct
253            , _VSTD::forward_as_tuple(__pr.first)
254            , _VSTD::forward_as_tuple(__pr.second));
255    }
256
257    template <class _T1, class _T2, class _U1, class _U2>
258    _LIBCPP_INLINE_VISIBILITY
259    void construct(pair<_T1, _T2> * __p, pair<_U1, _U2> && __pr){
260        construct(__p, piecewise_construct
261            , _VSTD::forward_as_tuple(_VSTD::forward<_U1>(__pr.first))
262            , _VSTD::forward_as_tuple(_VSTD::forward<_U2>(__pr.second)));
263    }
264
265    template <class _Tp>
266    _LIBCPP_INLINE_VISIBILITY
267    void destroy(_Tp * __p) _NOEXCEPT
268        { __p->~_Tp(); }
269
270    _LIBCPP_INLINE_VISIBILITY
271    polymorphic_allocator
272    select_on_container_copy_construction() const _NOEXCEPT
273        { return polymorphic_allocator(); }
274
275    _LIBCPP_INLINE_VISIBILITY
276    memory_resource * resource() const _NOEXCEPT
277        { return __res_; }
278
279private:
280    template <class ..._Args, size_t ..._Idx>
281    _LIBCPP_INLINE_VISIBILITY
282    tuple<_Args&&...>
283    __transform_tuple(integral_constant<int, 0>, tuple<_Args...>&& __t,
284                      __tuple_indices<_Idx...>) const
285    {
286        return _VSTD::forward_as_tuple(_VSTD::get<_Idx>(_VSTD::move(__t))...);
287    }
288
289    template <class ..._Args, size_t ..._Idx>
290    _LIBCPP_INLINE_VISIBILITY
291    tuple<allocator_arg_t const&, polymorphic_allocator&, _Args&&...>
292    __transform_tuple(integral_constant<int, 1>, tuple<_Args...> && __t,
293                      __tuple_indices<_Idx...>)
294    {
295        using _Tup = tuple<allocator_arg_t const&, polymorphic_allocator&, _Args&&...>;
296        return _Tup(allocator_arg, *this,
297                    _VSTD::get<_Idx>(_VSTD::move(__t))...);
298    }
299
300    template <class ..._Args, size_t ..._Idx>
301    _LIBCPP_INLINE_VISIBILITY
302    tuple<_Args&&..., polymorphic_allocator&>
303    __transform_tuple(integral_constant<int, 2>, tuple<_Args...> && __t,
304                      __tuple_indices<_Idx...>)
305    {
306        using _Tup = tuple<_Args&&..., polymorphic_allocator&>;
307        return _Tup(_VSTD::get<_Idx>(_VSTD::move(__t))..., *this);
308    }
309
310    _LIBCPP_INLINE_VISIBILITY
311    size_t __max_size() const _NOEXCEPT
312        { return numeric_limits<size_t>::max() / sizeof(value_type); }
313
314    memory_resource * __res_;
315};
316
317// 8.6.4, memory.polymorphic.allocator.eq
318
319template <class _Tp, class _Up>
320inline _LIBCPP_INLINE_VISIBILITY
321bool operator==(polymorphic_allocator<_Tp> const & __lhs,
322                polymorphic_allocator<_Up> const & __rhs) _NOEXCEPT
323{
324    return *__lhs.resource() == *__rhs.resource();
325}
326
327template <class _Tp, class _Up>
328inline _LIBCPP_INLINE_VISIBILITY
329bool operator!=(polymorphic_allocator<_Tp> const & __lhs,
330                polymorphic_allocator<_Up> const & __rhs) _NOEXCEPT
331{
332    return !(__lhs == __rhs);
333}
334
335// 8.7, memory.resource.adaptor
336
337// 8.7.1, memory.resource.adaptor.overview
338template <class _CharAlloc>
339class _LIBCPP_TEMPLATE_VIS __resource_adaptor_imp
340  : public memory_resource
341{
342    using _CTraits = allocator_traits<_CharAlloc>;
343    static_assert(is_same<typename _CTraits::value_type, char>::value
344               && is_same<typename _CTraits::pointer, char*>::value
345               && is_same<typename _CTraits::void_pointer, void*>::value, "");
346
347    static const size_t _MaxAlign = _LIBCPP_ALIGNOF(max_align_t);
348
349    using _Alloc = typename _CTraits::template rebind_alloc<
350            typename aligned_storage<_MaxAlign, _MaxAlign>::type
351        >;
352
353    using _ValueType = typename _Alloc::value_type;
354
355    _Alloc __alloc_;
356
357public:
358    typedef _CharAlloc allocator_type;
359
360    __resource_adaptor_imp() = default;
361    __resource_adaptor_imp(__resource_adaptor_imp const &) = default;
362    __resource_adaptor_imp(__resource_adaptor_imp &&) = default;
363
364    // 8.7.2, memory.resource.adaptor.ctor
365
366    _LIBCPP_INLINE_VISIBILITY
367    explicit __resource_adaptor_imp(allocator_type const & __a)
368      : __alloc_(__a)
369    {}
370
371    _LIBCPP_INLINE_VISIBILITY
372    explicit __resource_adaptor_imp(allocator_type && __a)
373      : __alloc_(_VSTD::move(__a))
374    {}
375
376    __resource_adaptor_imp &
377    operator=(__resource_adaptor_imp const &) = default;
378
379    _LIBCPP_INLINE_VISIBILITY
380    allocator_type get_allocator() const
381    { return __alloc_; }
382
383// 8.7.3, memory.resource.adaptor.mem
384protected:
385    virtual void * do_allocate(size_t __bytes, size_t)
386    {
387        if (__bytes > __max_size()) {
388            __throw_length_error(
389                "std::experimental::pmr::resource_adaptor<T>::do_allocate(size_t bytes, size_t align)"
390                " 'bytes' exceeds maximum supported size");
391        }
392        size_t __s = __aligned_allocation_size(__bytes, _MaxAlign) / _MaxAlign;
393        return __alloc_.allocate(__s);
394    }
395
396    virtual void do_deallocate(void * __p, size_t __bytes, size_t)
397    {
398        _LIBCPP_ASSERT(__bytes <= __max_size(),
399            "do_deallocate called for size which exceeds the maximum allocation size");
400        size_t __s = __aligned_allocation_size(__bytes, _MaxAlign) / _MaxAlign;
401        __alloc_.deallocate((_ValueType*)__p, __s);
402    }
403
404    virtual bool do_is_equal(memory_resource const & __other) const _NOEXCEPT {
405        __resource_adaptor_imp const * __p
406          = dynamic_cast<__resource_adaptor_imp const *>(&__other);
407        return __p  ? __alloc_ == __p->__alloc_ : false;
408    }
409
410private:
411    _LIBCPP_INLINE_VISIBILITY
412    size_t __max_size() const _NOEXCEPT {
413        return numeric_limits<size_t>::max() - _MaxAlign;
414    }
415};
416
417template <class _Alloc>
418using resource_adaptor = __resource_adaptor_imp<
419    typename allocator_traits<_Alloc>::template rebind_alloc<char>
420  >;
421
422_LIBCPP_END_NAMESPACE_LFTS_PMR
423
424_LIBCPP_POP_MACROS
425
426#endif /* _LIBCPP_EXPERIMENTAL_MEMORY_RESOURCE */
427