1// <experimental/memory_resource> -*- C++ -*-
2
3// Copyright (C) 2015-2018 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library.  This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file experimental/memory_resource
26 *  This is a TS C++ Library header.
27 */
28
29#ifndef _GLIBCXX_EXPERIMENTAL_MEMORY_RESOURCE
30#define _GLIBCXX_EXPERIMENTAL_MEMORY_RESOURCE 1
31
32#include <memory>
33#include <new>
34#include <atomic>
35#include <cstddef>
36#include <bits/alloc_traits.h>
37#include <experimental/bits/lfts_config.h>
38
39namespace std {
40_GLIBCXX_BEGIN_NAMESPACE_VERSION
41
42namespace experimental {
43inline namespace fundamentals_v2 {
44namespace pmr {
45#define __cpp_lib_experimental_memory_resources 201402L
46
47  class memory_resource;
48
49  template <typename _Tp>
50    class polymorphic_allocator;
51
52  template <typename _Alloc>
53    class __resource_adaptor_imp;
54
55  template <typename _Alloc>
56    using resource_adaptor = __resource_adaptor_imp<
57      typename allocator_traits<_Alloc>::template rebind_alloc<char>>;
58
59  template <typename _Tp>
60    struct __uses_allocator_construction_helper;
61
62  // Global memory resources
63  memory_resource* new_delete_resource() noexcept;
64  memory_resource* null_memory_resource() noexcept;
65
66  // The default memory resource
67  memory_resource* get_default_resource() noexcept;
68  memory_resource* set_default_resource(memory_resource* __r) noexcept;
69
70  // Standard memory resources
71
72  // 8.5 Class memory_resource
73  class memory_resource
74  {
75  protected:
76    static constexpr size_t _S_max_align = alignof(max_align_t);
77
78  public:
79    virtual ~memory_resource() { }
80
81    void*
82    allocate(size_t __bytes, size_t __alignment = _S_max_align)
83    { return do_allocate(__bytes, __alignment); }
84
85    void
86    deallocate(void* __p, size_t __bytes, size_t __alignment = _S_max_align)
87    { return do_deallocate(__p, __bytes, __alignment); }
88
89    bool
90    is_equal(const memory_resource& __other) const noexcept
91    { return do_is_equal(__other); }
92
93  protected:
94    virtual void*
95    do_allocate(size_t __bytes, size_t __alignment) = 0;
96
97    virtual void
98    do_deallocate(void* __p, size_t __bytes, size_t __alignment) = 0;
99
100    virtual bool
101    do_is_equal(const memory_resource& __other) const noexcept = 0;
102  };
103
104  inline bool
105  operator==(const memory_resource& __a,
106	     const memory_resource& __b) noexcept
107  { return &__a == &__b || __a.is_equal(__b); }
108
109  inline bool
110  operator!=(const memory_resource& __a,
111	     const memory_resource& __b) noexcept
112  { return !(__a == __b); }
113
114
115  // 8.6 Class template polymorphic_allocator
116  template <class _Tp>
117    class polymorphic_allocator
118    {
119      using __uses_alloc1_ = __uses_alloc1<memory_resource*>;
120      using __uses_alloc2_ = __uses_alloc2<memory_resource*>;
121
122      template<typename _Tp1, typename... _Args>
123	void
124	_M_construct(__uses_alloc0, _Tp1* __p, _Args&&... __args)
125	{ ::new(__p) _Tp1(std::forward<_Args>(__args)...); }
126
127      template<typename _Tp1, typename... _Args>
128	void
129	_M_construct(__uses_alloc1_, _Tp1* __p, _Args&&...  __args)
130	{ ::new(__p) _Tp1(allocator_arg, this->resource(),
131			  std::forward<_Args>(__args)...); }
132
133      template<typename _Tp1, typename... _Args>
134	void
135	_M_construct(__uses_alloc2_, _Tp1* __p, _Args&&...  __args)
136	{ ::new(__p) _Tp1(std::forward<_Args>(__args)...,
137			  this->resource()); }
138
139    public:
140      using value_type = _Tp;
141
142      polymorphic_allocator() noexcept
143      : _M_resource(get_default_resource())
144      { }
145
146      polymorphic_allocator(memory_resource* __r)
147      : _M_resource(__r)
148      { _GLIBCXX_DEBUG_ASSERT(__r); }
149
150      polymorphic_allocator(const polymorphic_allocator& __other) = default;
151
152      template <typename _Up>
153	polymorphic_allocator(const polymorphic_allocator<_Up>&
154			      __other) noexcept
155	: _M_resource(__other.resource())
156	{ }
157
158      polymorphic_allocator&
159	operator=(const polymorphic_allocator& __rhs) = default;
160
161      _Tp* allocate(size_t __n)
162      { return static_cast<_Tp*>(_M_resource->allocate(__n * sizeof(_Tp),
163						       alignof(_Tp))); }
164
165      void deallocate(_Tp* __p, size_t __n)
166      { _M_resource->deallocate(__p, __n * sizeof(_Tp), alignof(_Tp)); }
167
168      template <typename _Tp1, typename... _Args> //used here
169	void construct(_Tp1* __p, _Args&&... __args)
170	{
171	  memory_resource* const __resource = this->resource();
172	  auto __use_tag
173	    = __use_alloc<_Tp1, memory_resource*, _Args...>(__resource);
174	  _M_construct(__use_tag, __p, std::forward<_Args>(__args)...);
175	}
176
177      // Specializations for pair using piecewise construction
178      template <typename _Tp1, typename _Tp2,
179	       typename... _Args1, typename... _Args2>
180	void construct(pair<_Tp1, _Tp2>* __p, piecewise_construct_t,
181		       tuple<_Args1...> __x,
182		       tuple<_Args2...> __y)
183	{
184	  memory_resource* const __resource = this->resource();
185	  auto __x_use_tag =
186	    __use_alloc<_Tp1, memory_resource*, _Args1...>(__resource);
187	  auto __y_use_tag =
188	    __use_alloc<_Tp2, memory_resource*, _Args2...>(__resource);
189
190	  ::new(__p) std::pair<_Tp1, _Tp2>(piecewise_construct,
191					   _M_construct_p(__x_use_tag, __x),
192					   _M_construct_p(__y_use_tag, __y));
193	}
194
195      template <typename _Tp1, typename _Tp2>
196	void construct(pair<_Tp1,_Tp2>* __p)
197	{ this->construct(__p, piecewise_construct, tuple<>(), tuple<>()); }
198
199      template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
200	void construct(pair<_Tp1,_Tp2>* __p, _Up&& __x, _Vp&& __y)
201	{ this->construct(__p, piecewise_construct,
202			  forward_as_tuple(std::forward<_Up>(__x)),
203			  forward_as_tuple(std::forward<_Vp>(__y))); }
204
205      template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
206	void construct(pair<_Tp1,_Tp2>* __p, const std::pair<_Up, _Vp>& __pr)
207	{ this->construct(__p, piecewise_construct, forward_as_tuple(__pr.first),
208			  forward_as_tuple(__pr.second)); }
209
210      template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
211	void construct(pair<_Tp1,_Tp2>* __p, pair<_Up, _Vp>&& __pr)
212	{ this->construct(__p, piecewise_construct,
213			  forward_as_tuple(std::forward<_Up>(__pr.first)),
214			  forward_as_tuple(std::forward<_Vp>(__pr.second))); }
215
216      template <typename _Up>
217	void destroy(_Up* __p)
218	{ __p->~_Up(); }
219
220      // Return a default-constructed allocator (no allocator propagation)
221      polymorphic_allocator select_on_container_copy_construction() const
222      { return polymorphic_allocator(); }
223
224      memory_resource* resource() const
225      { return _M_resource; }
226
227    private:
228      template<typename _Tuple>
229	_Tuple&&
230	_M_construct_p(__uses_alloc0, _Tuple& __t)
231	{ return std::move(__t); }
232
233      template<typename... _Args>
234	decltype(auto)
235	_M_construct_p(__uses_alloc1_ __ua, tuple<_Args...>& __t)
236	{ return tuple_cat(make_tuple(allocator_arg, *(__ua._M_a)),
237			   std::move(__t)); }
238
239      template<typename... _Args>
240	decltype(auto)
241	_M_construct_p(__uses_alloc2_ __ua, tuple<_Args...>& __t)
242	{ return tuple_cat(std::move(__t), make_tuple(*(__ua._M_a))); }
243
244      memory_resource* _M_resource;
245    };
246
247  template <class _Tp1, class _Tp2>
248    bool operator==(const polymorphic_allocator<_Tp1>& __a,
249		    const polymorphic_allocator<_Tp2>& __b) noexcept
250    { return *__a.resource() == *__b.resource(); }
251
252  template <class _Tp1, class _Tp2>
253    bool operator!=(const polymorphic_allocator<_Tp1>& __a,
254		    const polymorphic_allocator<_Tp2>& __b) noexcept
255    { return !(__a == __b); }
256
257  // 8.7.1 __resource_adaptor_imp
258  template <typename _Alloc>
259    class __resource_adaptor_imp : public memory_resource
260    {
261    public:
262      using allocator_type = _Alloc;
263
264      __resource_adaptor_imp() = default;
265      __resource_adaptor_imp(const __resource_adaptor_imp&) = default;
266      __resource_adaptor_imp(__resource_adaptor_imp&&) = default;
267
268      explicit __resource_adaptor_imp(const _Alloc& __a2)
269      : _M_alloc(__a2)
270      { }
271
272      explicit __resource_adaptor_imp(_Alloc&& __a2)
273      : _M_alloc(std::move(__a2))
274      { }
275
276      __resource_adaptor_imp&
277      operator=(const __resource_adaptor_imp&) = default;
278
279      allocator_type get_allocator() const { return _M_alloc; }
280
281    protected:
282      virtual void*
283      do_allocate(size_t __bytes, size_t __alignment)
284      {
285	using _Aligned_alloc = std::__alloc_rebind<_Alloc, char>;
286	size_t __new_size = _S_aligned_size(__bytes,
287					    _S_supported(__alignment) ?
288					    __alignment : _S_max_align);
289	return _Aligned_alloc(_M_alloc).allocate(__new_size);
290      }
291
292      virtual void
293      do_deallocate(void* __p, size_t __bytes, size_t __alignment)
294      {
295	using _Aligned_alloc = std::__alloc_rebind<_Alloc, char>;
296	size_t __new_size = _S_aligned_size(__bytes,
297					    _S_supported(__alignment) ?
298					    __alignment : _S_max_align);
299	using _Ptr = typename allocator_traits<_Aligned_alloc>::pointer;
300	_Aligned_alloc(_M_alloc).deallocate(static_cast<_Ptr>(__p),
301					    __new_size);
302      }
303
304      virtual bool
305      do_is_equal(const memory_resource& __other) const noexcept
306      {
307	auto __p = dynamic_cast<const __resource_adaptor_imp*>(&__other);
308	return __p ? (_M_alloc == __p->_M_alloc) : false;
309      }
310
311    private:
312      // Calculate Aligned Size
313      // Returns a size that is larger than or equal to __size and divisible
314      // by __alignment, where __alignment is required to be the power of 2.
315      static size_t
316      _S_aligned_size(size_t __size, size_t __alignment)
317      { return ((__size - 1)|(__alignment - 1)) + 1; }
318
319      // Determine whether alignment meets one of those preconditions:
320      // 1. Equals to Zero
321      // 2. Is power of two
322      static bool
323      _S_supported (size_t __x)
324      { return ((__x != 0) && !(__x & (__x - 1))); }
325
326      _Alloc _M_alloc;
327    };
328
329  // Global memory resources
330  inline std::atomic<memory_resource*>&
331  __get_default_resource()
332  {
333    static atomic<memory_resource*> _S_default_resource(new_delete_resource());
334    return _S_default_resource;
335  }
336
337  inline memory_resource*
338  new_delete_resource() noexcept
339  {
340    static resource_adaptor<std::allocator<char>> __r;
341    return static_cast<memory_resource*>(&__r);
342  }
343
344  template <typename _Alloc>
345    class __null_memory_resource : private memory_resource
346    {
347    protected:
348      void*
349      do_allocate(size_t, size_t)
350      { std::__throw_bad_alloc(); }
351
352      void
353      do_deallocate(void*, size_t, size_t) noexcept
354      { }
355
356      bool
357      do_is_equal(const memory_resource& __other) const noexcept
358      { return this == &__other; }
359
360      friend memory_resource* null_memory_resource() noexcept;
361    };
362
363  inline memory_resource*
364  null_memory_resource() noexcept
365  {
366    static __null_memory_resource<void> __r;
367    return static_cast<memory_resource*>(&__r);
368  }
369
370  // The default memory resource
371  inline memory_resource*
372  get_default_resource() noexcept
373  { return __get_default_resource().load(); }
374
375  inline memory_resource*
376  set_default_resource(memory_resource* __r) noexcept
377  {
378    if (__r == nullptr)
379      __r = new_delete_resource();
380    return __get_default_resource().exchange(__r);
381  }
382} // namespace pmr
383} // namespace fundamentals_v2
384} // namespace experimental
385
386_GLIBCXX_END_NAMESPACE_VERSION
387} // namespace std
388
389#endif
390