libstdc++
memory_resource
Go to the documentation of this file.
1 // <experimental/memory_resource> -*- C++ -*-
2 
3 // Copyright (C) 2015-2016 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 
38 namespace std {
39 namespace experimental {
40 inline namespace fundamentals_v2 {
41 namespace pmr {
42 _GLIBCXX_BEGIN_NAMESPACE_VERSION
43 
44 #define __cpp_lib_experimental_memory_resources 201402L
45 
46  class memory_resource;
47 
48  template <typename _Tp>
49  class polymorphic_allocator;
50 
51  template <typename _Alloc>
52  class __resource_adaptor_imp;
53 
54  template <typename _Alloc>
55  using resource_adaptor = __resource_adaptor_imp<
56  typename allocator_traits<_Alloc>::template rebind_alloc<char>>;
57 
58  template <typename _Tp>
59  struct __uses_allocator_construction_helper;
60 
61  // Global memory resources
62  memory_resource* new_delete_resource() noexcept;
63  memory_resource* null_memory_resource() noexcept;
64 
65  // The default memory resource
66  memory_resource* get_default_resource() noexcept;
67  memory_resource* set_default_resource(memory_resource* __r) noexcept;
68 
69  // Standard memory resources
70 
71  // 8.5 Class memory_resource
72  class memory_resource
73  {
74  static constexpr size_t _S_max_align = alignof(max_align_t);
75 
76  public:
77  virtual ~memory_resource() { }
78 
79  void*
80  allocate(size_t __bytes, size_t __alignment = _S_max_align)
81  { return do_allocate(__bytes, __alignment); }
82 
83  void
84  deallocate(void* __p, size_t __bytes, size_t __alignment = _S_max_align)
85  { return do_deallocate(__p, __bytes, __alignment); }
86 
87  bool
88  is_equal(const memory_resource& __other) const noexcept
89  { return do_is_equal(__other); }
90 
91  protected:
92  virtual void*
93  do_allocate(size_t __bytes, size_t __alignment) = 0;
94 
95  virtual void
96  do_deallocate(void* __p, size_t __bytes, size_t __alignment) = 0;
97 
98  virtual bool
99  do_is_equal(const memory_resource& __other) const noexcept = 0;
100  };
101 
102  inline bool
103  operator==(const memory_resource& __a,
104  const memory_resource& __b) noexcept
105  { return &__a == &__b || __a.is_equal(__b); }
106 
107  inline bool
108  operator!=(const memory_resource& __a,
109  const memory_resource& __b) noexcept
110  { return !(__a == __b); }
111 
112 
113  // 8.6 Class template polymorphic_allocator
114  template <class _Tp>
115  class polymorphic_allocator
116  {
117  using __uses_alloc1_ = __uses_alloc1<memory_resource*>;
118  using __uses_alloc2_ = __uses_alloc2<memory_resource*>;
119 
120  template<typename _Tp1, typename... _Args>
121  void
122  _M_construct(__uses_alloc0, _Tp1* __p, _Args&&... __args)
123  { ::new(__p) _Tp1(std::forward<_Args>(__args)...); }
124 
125  template<typename _Tp1, typename... _Args>
126  void
127  _M_construct(__uses_alloc1_, _Tp1* __p, _Args&&... __args)
128  { ::new(__p) _Tp1(allocator_arg, this->resource(),
129  std::forward<_Args>(__args)...); }
130 
131  template<typename _Tp1, typename... _Args>
132  void
133  _M_construct(__uses_alloc2_, _Tp1* __p, _Args&&... __args)
134  { ::new(__p) _Tp1(std::forward<_Args>(__args)...,
135  this->resource()); }
136 
137  public:
138  using value_type = _Tp;
139 
140  polymorphic_allocator() noexcept
141  : _M_resource(get_default_resource())
142  { }
143 
144  polymorphic_allocator(memory_resource* __r)
145  : _M_resource(__r)
146  { _GLIBCXX_DEBUG_ASSERT(__r); }
147 
148  polymorphic_allocator(const polymorphic_allocator& __other) = default;
149 
150  template <typename _Up>
151  polymorphic_allocator(const polymorphic_allocator<_Up>&
152  __other) noexcept
153  : _M_resource(__other.resource())
154  { }
155 
156  polymorphic_allocator&
157  operator=(const polymorphic_allocator& __rhs) = default;
158 
159  _Tp* allocate(size_t __n)
160  { return static_cast<_Tp*>(_M_resource->allocate(__n * sizeof(_Tp),
161  alignof(_Tp))); }
162 
163  void deallocate(_Tp* __p, size_t __n)
164  { _M_resource->deallocate(__p, __n * sizeof(_Tp), alignof(_Tp)); }
165 
166  template <typename _Tp1, typename... _Args> //used here
167  void construct(_Tp1* __p, _Args&&... __args)
168  {
169  auto __use_tag = __use_alloc<_Tp1, memory_resource*,
170  _Args...>(this->resource());
171  _M_construct(__use_tag, __p, std::forward<_Args>(__args)...);
172  }
173 
174  // Specializations for pair using piecewise construction
175  template <typename _Tp1, typename _Tp2,
176  typename... _Args1, typename... _Args2>
177  void construct(pair<_Tp1, _Tp2>* __p, piecewise_construct_t,
178  tuple<_Args1...> __x,
179  tuple<_Args2...> __y)
180  {
181  auto __x_use_tag =
182  __use_alloc<_Tp1, memory_resource*, _Args1...>(this->resource());
183  auto __y_use_tag =
184  __use_alloc<_Tp2, memory_resource*, _Args2...>(this->resource());
185 
186  ::new(__p) std::pair<_Tp1, _Tp2>(piecewise_construct,
187  _M_construct_p(__x_use_tag, __x),
188  _M_construct_p(__y_use_tag, __y));
189  }
190 
191  template <typename _Tp1, typename _Tp2>
192  void construct(pair<_Tp1,_Tp2>* __p)
193  { this->construct(__p, piecewise_construct, tuple<>(), tuple<>()); }
194 
195  template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
196  void construct(pair<_Tp1,_Tp2>* __p, _Up&& __x, _Vp&& __y)
197  { this->construct(__p, piecewise_construct,
198  forward_as_tuple(std::forward<_Up>(__x)),
199  forward_as_tuple(std::forward<_Vp>(__y))); }
200 
201  template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
202  void construct(pair<_Tp1,_Tp2>* __p, const std::pair<_Up, _Vp>& __pr)
203  { this->construct(__p, piecewise_construct, forward_as_tuple(__pr.first),
204  forward_as_tuple(__pr.second)); }
205 
206  template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
207  void construct(pair<_Tp1,_Tp2>* __p, pair<_Up, _Vp>&& __pr)
208  { this->construct(__p, piecewise_construct,
209  forward_as_tuple(std::forward<_Up>(__pr.first)),
210  forward_as_tuple(std::forward<_Vp>(__pr.second))); }
211 
212  template <typename _Up>
213  void destroy(_Up* __p)
214  { __p->~_Up(); }
215 
216  // Return a default-constructed allocator (no allocator propagation)
217  polymorphic_allocator select_on_container_copy_construction() const
218  { return polymorphic_allocator(); }
219 
220  memory_resource* resource() const
221  { return _M_resource; }
222 
223  private:
224  template<typename _Tuple>
225  _Tuple&&
226  _M_construct_p(__uses_alloc0, _Tuple& __t)
227  { return std::move(__t); }
228 
229  template<typename... _Args>
230  decltype(auto)
231  _M_construct_p(__uses_alloc1_ __ua, tuple<_Args...>& __t)
232  { return tuple_cat(make_tuple(allocator_arg, *(__ua._M_a)),
233  std::move(__t)); }
234 
235  template<typename... _Args>
236  decltype(auto)
237  _M_construct_p(__uses_alloc2_ __ua, tuple<_Args...>& __t)
238  { return tuple_cat(std::move(__t), make_tuple(*(__ua._M_a))); }
239 
240  memory_resource* _M_resource;
241  };
242 
243  template <class _Tp1, class _Tp2>
244  bool operator==(const polymorphic_allocator<_Tp1>& __a,
245  const polymorphic_allocator<_Tp2>& __b) noexcept
246  { return *__a.resource() == *__b.resource(); }
247 
248  template <class _Tp1, class _Tp2>
249  bool operator!=(const polymorphic_allocator<_Tp1>& __a,
250  const polymorphic_allocator<_Tp2>& __b) noexcept
251  { return !(__a == __b); }
252 
253  // 8.7.1 __resource_adaptor_imp
254  template <typename _Alloc>
255  class __resource_adaptor_imp : public memory_resource
256  {
257  public:
258  using allocator_type = _Alloc;
259 
260  __resource_adaptor_imp() = default;
261  __resource_adaptor_imp(const __resource_adaptor_imp&) = default;
262  __resource_adaptor_imp(__resource_adaptor_imp&&) = default;
263 
264  explicit __resource_adaptor_imp(const _Alloc& __a2)
265  : _M_alloc(__a2)
266  { }
267 
268  explicit __resource_adaptor_imp(_Alloc&& __a2)
269  : _M_alloc(std::move(__a2))
270  { }
271 
272  __resource_adaptor_imp&
273  operator=(const __resource_adaptor_imp&) = default;
274 
275  allocator_type get_allocator() const { return _M_alloc; }
276 
277  protected:
278  virtual void*
279  do_allocate(size_t __bytes, size_t __alignment)
280  {
281  using _Aligned_alloc = std::__alloc_rebind<_Alloc, char>;
282  size_t __new_size = _S_aligned_size(__bytes,
283  _S_supported(__alignment) ?
284  __alignment : _S_max_align);
285  return _Aligned_alloc().allocate(__new_size);
286  }
287 
288  virtual void
289  do_deallocate(void* __p, size_t __bytes, size_t __alignment)
290  {
291  using _Aligned_alloc = std::__alloc_rebind<_Alloc, char>;
292  size_t __new_size = _S_aligned_size(__bytes,
293  _S_supported(__alignment) ?
294  __alignment : _S_max_align);
295  _Aligned_alloc().deallocate(static_cast<typename
296  _Aligned_alloc::pointer>(__p),
297  __new_size);
298  }
299 
300  virtual bool
301  do_is_equal(const memory_resource& __other) const noexcept
302  {
303  auto __p = dynamic_cast<const __resource_adaptor_imp*>(&__other);
304  return __p ? (_M_alloc == __p->_M_alloc) : false;
305  }
306 
307  private:
308  // Calculate Aligned Size
309  // Returns a size that is larger than or equal to __size and divided by
310  // __alignment, where __alignment is required to be the power of 2.
311  static size_t
312  _S_aligned_size(size_t __size, size_t __alignment)
313  { return ((__size - 1)|(__alignment - 1)) + 1; }
314 
315  // Determine whether alignment meets one of those preconditions:
316  // 1. Equals to Zero
317  // 2. Is power of two
318  static bool
319  _S_supported (size_t __x)
320  { return ((__x != 0) && !(__x & (__x - 1))); }
321 
322  _Alloc _M_alloc;
323  };
324 
325  // Global memory resources
326  inline std::atomic<memory_resource*>&
327  __get_default_resource()
328  {
329  static atomic<memory_resource*> _S_default_resource(new_delete_resource());
330  return _S_default_resource;
331  }
332 
333  inline memory_resource*
334  new_delete_resource() noexcept
335  {
336  static resource_adaptor<std::allocator<char>> __r;
337  return static_cast<memory_resource*>(&__r);
338  }
339 
340  template <typename _Alloc>
341  class __null_memory_resource : private memory_resource
342  {
343  protected:
344  void*
345  do_allocate(size_t __bytes, size_t __alignment)
346  { std::__throw_bad_alloc(); }
347 
348  void
349  do_deallocate(void* __p, size_t __bytes, size_t __alignment)
350  { }
351 
352  bool
353  do_is_equal(const memory_resource& __other) const noexcept
354  { }
355 
356  friend memory_resource* null_memory_resource() noexcept;
357  };
358 
359  inline memory_resource*
360  null_memory_resource() noexcept
361  {
362  static __null_memory_resource<void> __r;
363  return static_cast<memory_resource*>(&__r);
364  }
365 
366  // The default memory resource
367  inline memory_resource*
368  get_default_resource() noexcept
369  { return __get_default_resource().load(); }
370 
371  inline memory_resource*
372  set_default_resource(memory_resource* __r) noexcept
373  {
374  if (__r == nullptr)
375  __r = new_delete_resource();
376  return __get_default_resource().exchange(__r);
377  }
378 
379 _GLIBCXX_END_NAMESPACE_VERSION
380 } // namespace pmr
381 } // namespace fundamentals_v2
382 } // namespace experimental
383 } // namespace std
384 
385 #endif