libstdc++
memory_resource
Go to the documentation of this file.
1 // <experimental/memory_resource> -*- C++ -*-
2 
3 // Copyright (C) 2015-2019 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 #pragma GCC system_header
33 
34 #if __cplusplus >= 201402L
35 
36 #include <memory> // align, uses_allocator, __uses_alloc
37 #include <experimental/utility> // pair, experimental::erased_type
38 #include <atomic> // atomic
39 #include <new> // placement new
40 #include <cstddef> // max_align_t
41 #include <ext/new_allocator.h>
42 #include <debug/assertions.h>
43 
44 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
45 {
46 _GLIBCXX_BEGIN_NAMESPACE_VERSION
47  template<typename _Tp> class malloc_allocator;
48 _GLIBCXX_END_NAMESPACE_VERSION
49 } // namespace __gnu_cxx
50 
51 namespace std {
52 _GLIBCXX_BEGIN_NAMESPACE_VERSION
53 
54 namespace experimental {
55 inline namespace fundamentals_v2 {
56 namespace pmr {
57 #define __cpp_lib_experimental_memory_resources 201402L
58 
59  // Standard memory resources
60 
61  // 8.5 Class memory_resource
62  class memory_resource;
63 
64  // 8.6 Class template polymorphic_allocator
65  template<typename _Tp>
66  class polymorphic_allocator;
67 
68  template<typename _Alloc, typename _Resource = memory_resource>
69  class __resource_adaptor_imp;
70 
71  // 8.7 Alias template resource_adaptor
72  template<typename _Alloc>
73  using resource_adaptor = __resource_adaptor_imp<
74  typename allocator_traits<_Alloc>::template rebind_alloc<char>>;
75 
76  // 8.8 Global memory resources
77  memory_resource* new_delete_resource() noexcept;
78  memory_resource* null_memory_resource() noexcept;
79  memory_resource* get_default_resource() noexcept;
80  memory_resource* set_default_resource(memory_resource* __r) noexcept;
81 
82  // TODO 8.9 Pool resource classes
83 
84  class memory_resource
85  {
86  static constexpr size_t _S_max_align = alignof(max_align_t);
87 
88  public:
89  memory_resource() = default;
90  memory_resource(const memory_resource&) = default;
91  virtual ~memory_resource() = default;
92 
93  memory_resource& operator=(const memory_resource&) = default;
94 
95  _GLIBCXX_NODISCARD void*
96  allocate(size_t __bytes, size_t __alignment = _S_max_align)
97  { return do_allocate(__bytes, __alignment); }
98 
99  void
100  deallocate(void* __p, size_t __bytes, size_t __alignment = _S_max_align)
101  { return do_deallocate(__p, __bytes, __alignment); }
102 
103  bool
104  is_equal(const memory_resource& __other) const noexcept
105  { return do_is_equal(__other); }
106 
107  protected:
108  virtual void*
109  do_allocate(size_t __bytes, size_t __alignment) = 0;
110 
111  virtual void
112  do_deallocate(void* __p, size_t __bytes, size_t __alignment) = 0;
113 
114  virtual bool
115  do_is_equal(const memory_resource& __other) const noexcept = 0;
116  };
117 
118  inline bool
119  operator==(const memory_resource& __a, const memory_resource& __b) noexcept
120  { return &__a == &__b || __a.is_equal(__b); }
121 
122  inline bool
123  operator!=(const memory_resource& __a, const memory_resource& __b) noexcept
124  { return !(__a == __b); }
125 
126 
127  template<typename _Tp>
128  class polymorphic_allocator
129  {
130  public:
131  using value_type = _Tp;
132 
133  polymorphic_allocator() noexcept
134  : _M_resource(get_default_resource())
135  { }
136 
137  polymorphic_allocator(memory_resource* __r)
138  : _M_resource(__r)
139  { _GLIBCXX_DEBUG_ASSERT(__r); }
140 
141  polymorphic_allocator(const polymorphic_allocator& __other) = default;
142 
143  template <typename _Up>
144  polymorphic_allocator(const polymorphic_allocator<_Up>&
145  __other) noexcept
146  : _M_resource(__other.resource())
147  { }
148 
149  polymorphic_allocator&
150  operator=(const polymorphic_allocator& __rhs) = default;
151 
152  _GLIBCXX_NODISCARD _Tp* allocate(size_t __n)
153  { return static_cast<_Tp*>(_M_resource->allocate(__n * sizeof(_Tp),
154  alignof(_Tp))); }
155 
156  void
157  deallocate(_Tp* __p, size_t __n)
158  { _M_resource->deallocate(__p, __n * sizeof(_Tp), alignof(_Tp)); }
159 
160  template <typename _Tp1, typename... _Args> //used here
161  void
162  construct(_Tp1* __p, _Args&&... __args)
163  {
164  std::__uses_allocator_construct(this->resource(), __p,
165  std::forward<_Args>(__args)...);
166  }
167 
168  // Specializations for pair using piecewise construction
169  template <typename _Tp1, typename _Tp2,
170  typename... _Args1, typename... _Args2>
171  void
172  construct(pair<_Tp1, _Tp2>* __p, piecewise_construct_t,
173  tuple<_Args1...> __x, tuple<_Args2...> __y)
174  {
175  memory_resource* const __resource = this->resource();
176  auto __x_use_tag =
177  std::__use_alloc<_Tp1, memory_resource*, _Args1...>(__resource);
178  auto __y_use_tag =
179  std::__use_alloc<_Tp2, memory_resource*, _Args2...>(__resource);
180 
181  ::new(__p) std::pair<_Tp1, _Tp2>(piecewise_construct,
182  _M_construct_p(__x_use_tag, __x),
183  _M_construct_p(__y_use_tag, __y));
184  }
185 
186  template <typename _Tp1, typename _Tp2>
187  void
188  construct(pair<_Tp1,_Tp2>* __p)
189  { this->construct(__p, piecewise_construct, tuple<>(), tuple<>()); }
190 
191  template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
192  void
193  construct(pair<_Tp1,_Tp2>* __p, _Up&& __x, _Vp&& __y)
194  {
195  this->construct(__p, piecewise_construct,
196  forward_as_tuple(std::forward<_Up>(__x)),
197  forward_as_tuple(std::forward<_Vp>(__y)));
198  }
199 
200  template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
201  void
202  construct(pair<_Tp1,_Tp2>* __p, const std::pair<_Up, _Vp>& __pr)
203  {
204  this->construct(__p, piecewise_construct,
205  forward_as_tuple(__pr.first),
206  forward_as_tuple(__pr.second));
207  }
208 
209  template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
210  void
211  construct(pair<_Tp1,_Tp2>* __p, pair<_Up, _Vp>&& __pr)
212  {
213  this->construct(__p, piecewise_construct,
214  forward_as_tuple(std::forward<_Up>(__pr.first)),
215  forward_as_tuple(std::forward<_Vp>(__pr.second)));
216  }
217 
218  template <typename _Up>
219  void
220  destroy(_Up* __p)
221  { __p->~_Up(); }
222 
223  // Return a default-constructed allocator (no allocator propagation)
224  polymorphic_allocator
225  select_on_container_copy_construction() const
226  { return polymorphic_allocator(); }
227 
228  memory_resource* resource() const { return _M_resource; }
229 
230  private:
231  using __uses_alloc1_ = __uses_alloc1<memory_resource*>;
232  using __uses_alloc2_ = __uses_alloc2<memory_resource*>;
233 
234  template<typename _Tuple>
235  _Tuple&&
236  _M_construct_p(__uses_alloc0, _Tuple& __t)
237  { return std::move(__t); }
238 
239  template<typename... _Args>
240  decltype(auto)
241  _M_construct_p(__uses_alloc1_ __ua, tuple<_Args...>& __t)
242  { return tuple_cat(make_tuple(allocator_arg, *(__ua._M_a)),
243  std::move(__t)); }
244 
245  template<typename... _Args>
246  decltype(auto)
247  _M_construct_p(__uses_alloc2_ __ua, tuple<_Args...>& __t)
248  { return tuple_cat(std::move(__t), make_tuple(*(__ua._M_a))); }
249 
250  memory_resource* _M_resource;
251  };
252 
253  template <class _Tp1, class _Tp2>
254  bool
255  operator==(const polymorphic_allocator<_Tp1>& __a,
256  const polymorphic_allocator<_Tp2>& __b) noexcept
257  { return *__a.resource() == *__b.resource(); }
258 
259  template <class _Tp1, class _Tp2>
260  bool
261  operator!=(const polymorphic_allocator<_Tp1>& __a,
262  const polymorphic_allocator<_Tp2>& __b) noexcept
263  { return !(__a == __b); }
264 
265 
266  class __resource_adaptor_common
267  {
268  template<typename, typename> friend class __resource_adaptor_imp;
269 
270  struct _AlignMgr
271  {
272  _AlignMgr(size_t __nbytes, size_t __align)
273  : _M_nbytes(__nbytes), _M_align(__align)
274  { }
275 
276  // Total size that needs to be allocated.
277  size_t
278  _M_alloc_size() const { return _M_buf_size() + _M_token_size(); }
279 
280  void*
281  _M_adjust(void* __ptr) const
282  {
283  const auto __orig_ptr = static_cast<char*>(__ptr);
284  size_t __space = _M_buf_size();
285  // Align the pointer within the buffer:
286  std::align(_M_align, _M_nbytes, __ptr, __space);
287  const auto __aligned_ptr = static_cast<char*>(__ptr);
288  const auto __token_size = _M_token_size();
289  // Store token immediately after the aligned block:
290  char* const __end = __aligned_ptr + _M_nbytes;
291  if (__token_size == 1)
292  _S_write<unsigned char>(__end, __aligned_ptr - __orig_ptr);
293  else if (__token_size == sizeof(short))
294  _S_write<unsigned short>(__end, __aligned_ptr - __orig_ptr);
295  else if (__token_size == sizeof(int) && sizeof(int) < sizeof(char*))
296  _S_write<unsigned int>(__end, __aligned_ptr - __orig_ptr);
297  else // (__token_size == sizeof(char*))
298  // Just store the original pointer:
299  _S_write<char*>(__end, __orig_ptr);
300  return __aligned_ptr;
301  }
302 
303  char*
304  _M_unadjust(char* __ptr) const
305  {
306  const char* const __end = __ptr + _M_nbytes;
307  char* __orig_ptr;
308  const auto __token_size = _M_token_size();
309  // Read the token and restore the original pointer:
310  if (__token_size == 1)
311  __orig_ptr = __ptr - _S_read<unsigned char>(__end);
312  else if (__token_size == sizeof(short))
313  __orig_ptr = __ptr - _S_read<unsigned short>(__end);
314  else if (__token_size == sizeof(int)
315  && sizeof(int) < sizeof(char*))
316  __orig_ptr = __ptr - _S_read<unsigned int>(__end);
317  else // (__token_size == sizeof(char*))
318  __orig_ptr = _S_read<char*>(__end);
319  // The adjustment is always less than the requested alignment,
320  // so if that isn't true now then either the wrong size was passed
321  // to deallocate or the token was overwritten by a buffer overflow:
322  __glibcxx_assert(static_cast<size_t>(__ptr - __orig_ptr) < _M_align);
323  return __orig_ptr;
324  }
325 
326  private:
327  size_t _M_nbytes;
328  size_t _M_align;
329 
330  // Number of bytes needed to fit block of given size and alignment.
331  size_t
332  _M_buf_size() const { return _M_nbytes + _M_align - 1; }
333 
334  // Number of additional bytes needed to write the token.
335  int
336  _M_token_size() const
337  {
338  if (_M_align <= (1ul << __CHAR_BIT__))
339  return 1;
340  if (_M_align <= (1ul << (sizeof(short) * __CHAR_BIT__)))
341  return sizeof(short);
342  if (_M_align <= (1ull << (sizeof(int) * __CHAR_BIT__)))
343  return sizeof(int);
344  return sizeof(char*);
345  }
346 
347  template<typename _Tp>
348  static void
349  _S_write(void* __to, _Tp __val)
350  { __builtin_memcpy(__to, &__val, sizeof(_Tp)); }
351 
352  template<typename _Tp>
353  static _Tp
354  _S_read(const void* __from)
355  {
356  _Tp __val;
357  __builtin_memcpy(&__val, __from, sizeof(_Tp));
358  return __val;
359  }
360  };
361 
362  template<typename _Alloc>
363  struct __guaranteed_alignment : std::integral_constant<size_t, 1> { };
364 
365  template<typename _Tp>
366  struct __guaranteed_alignment<__gnu_cxx::new_allocator<_Tp>>
367  : std::alignment_of<std::max_align_t>::type { };
368 
369  template<typename _Tp>
370  struct __guaranteed_alignment<__gnu_cxx::malloc_allocator<_Tp>>
371  : std::alignment_of<std::max_align_t>::type { };
372 
373 #if _GLIBCXX_USE_ALLOCATOR_NEW
374  template<typename _Tp>
375  struct __guaranteed_alignment<std::allocator<_Tp>>
376  : std::alignment_of<std::max_align_t>::type { };
377 #endif
378  };
379 
380  // 8.7.1 __resource_adaptor_imp
381  template<typename _Alloc, typename _Resource>
382  class __resource_adaptor_imp
383  : public _Resource, private __resource_adaptor_common
384  {
385  using memory_resource = _Resource;
386 
387  static_assert(is_same<char,
388  typename allocator_traits<_Alloc>::value_type>::value,
389  "Allocator's value_type is char");
390  static_assert(is_same<char*,
391  typename allocator_traits<_Alloc>::pointer>::value,
392  "Allocator's pointer type is value_type*");
393  static_assert(is_same<const char*,
394  typename allocator_traits<_Alloc>::const_pointer>::value,
395  "Allocator's const_pointer type is value_type const*");
396  static_assert(is_same<void*,
397  typename allocator_traits<_Alloc>::void_pointer>::value,
398  "Allocator's void_pointer type is void*");
399  static_assert(is_same<const void*,
400  typename allocator_traits<_Alloc>::const_void_pointer>::value,
401  "Allocator's const_void_pointer type is void const*");
402 
403  public:
404  using allocator_type = _Alloc;
405 
406  __resource_adaptor_imp() = default;
407  __resource_adaptor_imp(const __resource_adaptor_imp&) = default;
408  __resource_adaptor_imp(__resource_adaptor_imp&&) = default;
409 
410  explicit __resource_adaptor_imp(const _Alloc& __a2)
411  : _M_alloc(__a2)
412  { }
413 
414  explicit __resource_adaptor_imp(_Alloc&& __a2)
415  : _M_alloc(std::move(__a2))
416  { }
417 
418  __resource_adaptor_imp&
419  operator=(const __resource_adaptor_imp&) = default;
420 
421  allocator_type get_allocator() const noexcept { return _M_alloc; }
422 
423  protected:
424  virtual void*
425  do_allocate(size_t __bytes, size_t __alignment) override
426  {
427  if (__alignment <= __guaranteed_alignment<_Alloc>::value)
428  {
429  if (__bytes < __alignment)
430  __bytes = __alignment;
431  return _M_alloc.allocate(__bytes);
432  }
433 
434 
435  const _AlignMgr __mgr(__bytes, __alignment);
436  // Assume _M_alloc returns 1-byte aligned memory, so allocate enough
437  // space to fit a block of the right size and alignment, plus some
438  // extra bytes to store a token for retrieving the original pointer.
439  return __mgr._M_adjust(_M_alloc.allocate(__mgr._M_alloc_size()));
440  }
441 
442  virtual void
443  do_deallocate(void* __p, size_t __bytes, size_t __alignment) noexcept
444  override
445  {
446  auto __ptr = static_cast<char*>(__p);
447  if (__alignment <= __guaranteed_alignment<_Alloc>::value)
448  {
449  if (__bytes < __alignment)
450  __bytes = __alignment;
451  _M_alloc.deallocate(__ptr, __bytes);
452  return;
453  }
454 
455  const _AlignMgr __mgr(__bytes, __alignment);
456  // Use the stored token to retrieve the original pointer to deallocate.
457  _M_alloc.deallocate(__mgr._M_unadjust(__ptr), __mgr._M_alloc_size());
458  }
459 
460  virtual bool
461  do_is_equal(const memory_resource& __other) const noexcept override
462  {
463  if (auto __p = dynamic_cast<const __resource_adaptor_imp*>(&__other))
464  return _M_alloc == __p->_M_alloc;
465  return false;
466  }
467 
468  private:
469  _Alloc _M_alloc{};
470  };
471 
472  // Global memory resources
473 
474  inline memory_resource*
475  new_delete_resource() noexcept
476  {
477  using type = resource_adaptor<__gnu_cxx::new_allocator<char>>;
478  alignas(type) static unsigned char __buf[sizeof(type)];
479  static type* __r = new(__buf) type;
480  return __r;
481  }
482 
483  inline memory_resource*
484  null_memory_resource() noexcept
485  {
486  class type final : public memory_resource
487  {
488  void*
489  do_allocate(size_t, size_t) override
490  { std::__throw_bad_alloc(); }
491 
492  void
493  do_deallocate(void*, size_t, size_t) noexcept override
494  { }
495 
496  bool
497  do_is_equal(const memory_resource& __other) const noexcept override
498  { return this == &__other; }
499  };
500 
501  alignas(type) static unsigned char __buf[sizeof(type)];
502  static type* __r = new(__buf) type;
503  return __r;
504  }
505 
506  // The default memory resource
507 
508  inline std::atomic<memory_resource*>&
509  __get_default_resource()
510  {
511  using type = atomic<memory_resource*>;
512  alignas(type) static unsigned char __buf[sizeof(type)];
513  static type* __r = new(__buf) type(new_delete_resource());
514  return *__r;
515  }
516 
517  inline memory_resource*
518  get_default_resource() noexcept
519  { return __get_default_resource().load(); }
520 
521  inline memory_resource*
522  set_default_resource(memory_resource* __r) noexcept
523  {
524  if (__r == nullptr)
525  __r = new_delete_resource();
526  return __get_default_resource().exchange(__r);
527  }
528 
529 } // namespace pmr
530 } // namespace fundamentals_v2
531 } // namespace experimental
532 
533 _GLIBCXX_END_NAMESPACE_VERSION
534 } // namespace std
535 #endif // C++14
536 #endif // _GLIBCXX_EXPERIMENTAL_MEMORY_RESOURCE