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