libstdc++
memory_resource
Go to the documentation of this file.
00001 // <experimental/memory_resource> -*- C++ -*-
00002 
00003 // Copyright (C) 2015-2019 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file experimental/memory_resource
00026  *  This is a TS C++ Library header.
00027  */
00028 
00029 #ifndef _GLIBCXX_EXPERIMENTAL_MEMORY_RESOURCE
00030 #define _GLIBCXX_EXPERIMENTAL_MEMORY_RESOURCE 1
00031 
00032 #include <memory>                       // align, uses_allocator, __uses_alloc
00033 #include <experimental/utility>         // pair, experimental::erased_type
00034 #include <atomic>                       // atomic
00035 #include <new>                          // placement new
00036 #include <cstddef>                      // max_align_t
00037 #include <ext/new_allocator.h>
00038 #include <debug/assertions.h>
00039 
00040 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
00041 {
00042 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00043   template<typename _Tp> class malloc_allocator;
00044 _GLIBCXX_END_NAMESPACE_VERSION
00045 } // namespace __gnu_cxx
00046 
00047 namespace std {
00048 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00049 
00050 namespace experimental {
00051 inline namespace fundamentals_v2 {
00052 namespace pmr {
00053 #define __cpp_lib_experimental_memory_resources 201402L
00054 
00055   // Standard memory resources
00056 
00057   // 8.5 Class memory_resource
00058   class memory_resource;
00059 
00060   // 8.6 Class template polymorphic_allocator
00061   template<typename _Tp>
00062     class polymorphic_allocator;
00063 
00064   template<typename _Alloc, typename _Resource = memory_resource>
00065     class __resource_adaptor_imp;
00066 
00067   // 8.7 Alias template resource_adaptor
00068   template<typename _Alloc>
00069     using resource_adaptor = __resource_adaptor_imp<
00070       typename allocator_traits<_Alloc>::template rebind_alloc<char>>;
00071 
00072   // 8.8 Global memory resources
00073   memory_resource* new_delete_resource() noexcept;
00074   memory_resource* null_memory_resource() noexcept;
00075   memory_resource* get_default_resource() noexcept;
00076   memory_resource* set_default_resource(memory_resource* __r) noexcept;
00077 
00078   // TODO 8.9 Pool resource classes
00079 
00080   class memory_resource
00081   {
00082     static constexpr size_t _S_max_align = alignof(max_align_t);
00083 
00084   public:
00085     memory_resource() = default;
00086     memory_resource(const memory_resource&) = default;
00087     virtual ~memory_resource() = default;
00088 
00089     memory_resource& operator=(const memory_resource&) = default;
00090 
00091     _GLIBCXX_NODISCARD void*
00092     allocate(size_t __bytes, size_t __alignment = _S_max_align)
00093     { return do_allocate(__bytes, __alignment); }
00094 
00095     void
00096     deallocate(void* __p, size_t __bytes, size_t __alignment = _S_max_align)
00097     { return do_deallocate(__p, __bytes, __alignment); }
00098 
00099     bool
00100     is_equal(const memory_resource& __other) const noexcept
00101     { return do_is_equal(__other); }
00102 
00103   protected:
00104     virtual void*
00105     do_allocate(size_t __bytes, size_t __alignment) = 0;
00106 
00107     virtual void
00108     do_deallocate(void* __p, size_t __bytes, size_t __alignment) = 0;
00109 
00110     virtual bool
00111     do_is_equal(const memory_resource& __other) const noexcept = 0;
00112   };
00113 
00114   inline bool
00115   operator==(const memory_resource& __a, const memory_resource& __b) noexcept
00116   { return &__a == &__b || __a.is_equal(__b); }
00117 
00118   inline bool
00119   operator!=(const memory_resource& __a, const memory_resource& __b) noexcept
00120   { return !(__a == __b); }
00121 
00122 
00123   template<typename _Tp>
00124     class polymorphic_allocator
00125     {
00126     public:
00127       using value_type = _Tp;
00128 
00129       polymorphic_allocator() noexcept
00130       : _M_resource(get_default_resource())
00131       { }
00132 
00133       polymorphic_allocator(memory_resource* __r)
00134       : _M_resource(__r)
00135       { _GLIBCXX_DEBUG_ASSERT(__r); }
00136 
00137       polymorphic_allocator(const polymorphic_allocator& __other) = default;
00138 
00139       template <typename _Up>
00140         polymorphic_allocator(const polymorphic_allocator<_Up>&
00141                               __other) noexcept
00142         : _M_resource(__other.resource())
00143         { }
00144 
00145       polymorphic_allocator&
00146         operator=(const polymorphic_allocator& __rhs) = default;
00147 
00148       _GLIBCXX_NODISCARD _Tp* allocate(size_t __n)
00149       { return static_cast<_Tp*>(_M_resource->allocate(__n * sizeof(_Tp),
00150                                                        alignof(_Tp))); }
00151 
00152       void
00153       deallocate(_Tp* __p, size_t __n)
00154       { _M_resource->deallocate(__p, __n * sizeof(_Tp), alignof(_Tp)); }
00155 
00156       template <typename _Tp1, typename... _Args> //used here
00157         void
00158         construct(_Tp1* __p, _Args&&... __args)
00159         {
00160           std::__uses_allocator_construct(this->resource(), __p,
00161                                           std::forward<_Args>(__args)...);
00162         }
00163 
00164       // Specializations for pair using piecewise construction
00165       template <typename _Tp1, typename _Tp2,
00166                typename... _Args1, typename... _Args2>
00167         void
00168         construct(pair<_Tp1, _Tp2>* __p, piecewise_construct_t,
00169                   tuple<_Args1...> __x, tuple<_Args2...> __y)
00170         {
00171           memory_resource* const __resource = this->resource();
00172           auto __x_use_tag =
00173             std::__use_alloc<_Tp1, memory_resource*, _Args1...>(__resource);
00174           auto __y_use_tag =
00175             std::__use_alloc<_Tp2, memory_resource*, _Args2...>(__resource);
00176 
00177           ::new(__p) std::pair<_Tp1, _Tp2>(piecewise_construct,
00178                                            _M_construct_p(__x_use_tag, __x),
00179                                            _M_construct_p(__y_use_tag, __y));
00180         }
00181 
00182       template <typename _Tp1, typename _Tp2>
00183         void
00184         construct(pair<_Tp1,_Tp2>* __p)
00185         { this->construct(__p, piecewise_construct, tuple<>(), tuple<>()); }
00186 
00187       template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
00188         void
00189         construct(pair<_Tp1,_Tp2>* __p, _Up&& __x, _Vp&& __y)
00190         {
00191           this->construct(__p, piecewise_construct,
00192                           forward_as_tuple(std::forward<_Up>(__x)),
00193                           forward_as_tuple(std::forward<_Vp>(__y)));
00194         }
00195 
00196       template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
00197         void
00198         construct(pair<_Tp1,_Tp2>* __p, const std::pair<_Up, _Vp>& __pr)
00199         {
00200           this->construct(__p, piecewise_construct,
00201                           forward_as_tuple(__pr.first),
00202                           forward_as_tuple(__pr.second));
00203         }
00204 
00205       template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
00206         void
00207         construct(pair<_Tp1,_Tp2>* __p, pair<_Up, _Vp>&& __pr)
00208         {
00209           this->construct(__p, piecewise_construct,
00210                           forward_as_tuple(std::forward<_Up>(__pr.first)),
00211                           forward_as_tuple(std::forward<_Vp>(__pr.second)));
00212         }
00213 
00214       template <typename _Up>
00215         void
00216         destroy(_Up* __p)
00217         { __p->~_Up(); }
00218 
00219       // Return a default-constructed allocator (no allocator propagation)
00220       polymorphic_allocator
00221       select_on_container_copy_construction() const
00222       { return polymorphic_allocator(); }
00223 
00224       memory_resource* resource() const { return _M_resource; }
00225 
00226     private:
00227       using __uses_alloc1_ = __uses_alloc1<memory_resource*>;
00228       using __uses_alloc2_ = __uses_alloc2<memory_resource*>;
00229 
00230       template<typename _Tuple>
00231         _Tuple&&
00232         _M_construct_p(__uses_alloc0, _Tuple& __t)
00233         { return std::move(__t); }
00234 
00235       template<typename... _Args>
00236         decltype(auto)
00237         _M_construct_p(__uses_alloc1_ __ua, tuple<_Args...>& __t)
00238         { return tuple_cat(make_tuple(allocator_arg, *(__ua._M_a)),
00239                            std::move(__t)); }
00240 
00241       template<typename... _Args>
00242         decltype(auto)
00243         _M_construct_p(__uses_alloc2_ __ua, tuple<_Args...>& __t)
00244         { return tuple_cat(std::move(__t), make_tuple(*(__ua._M_a))); }
00245 
00246       memory_resource* _M_resource;
00247     };
00248 
00249   template <class _Tp1, class _Tp2>
00250     bool
00251     operator==(const polymorphic_allocator<_Tp1>& __a,
00252                const polymorphic_allocator<_Tp2>& __b) noexcept
00253     { return *__a.resource() == *__b.resource(); }
00254 
00255   template <class _Tp1, class _Tp2>
00256     bool
00257     operator!=(const polymorphic_allocator<_Tp1>& __a,
00258                const polymorphic_allocator<_Tp2>& __b) noexcept
00259     { return !(__a == __b); }
00260 
00261 
00262   class __resource_adaptor_common
00263   {
00264     template<typename, typename> friend class __resource_adaptor_imp;
00265 
00266     struct _AlignMgr
00267     {
00268       _AlignMgr(size_t __nbytes, size_t __align)
00269       : _M_nbytes(__nbytes), _M_align(__align)
00270       { }
00271 
00272       // Total size that needs to be allocated.
00273       size_t
00274       _M_alloc_size() const { return _M_buf_size() + _M_token_size(); }
00275 
00276       void*
00277       _M_adjust(void* __ptr) const
00278       {
00279         const auto __orig_ptr = static_cast<char*>(__ptr);
00280         size_t __space = _M_buf_size();
00281         // Align the pointer within the buffer:
00282         std::align(_M_align, _M_nbytes, __ptr, __space);
00283         const auto __aligned_ptr = static_cast<char*>(__ptr);
00284         const auto __token_size = _M_token_size();
00285         // Store token immediately after the aligned block:
00286         char* const __end = __aligned_ptr + _M_nbytes;
00287         if (__token_size == 1)
00288           _S_write<unsigned char>(__end, __aligned_ptr - __orig_ptr);
00289         else if (__token_size == sizeof(short))
00290           _S_write<unsigned short>(__end, __aligned_ptr - __orig_ptr);
00291         else if (__token_size == sizeof(int) && sizeof(int) < sizeof(char*))
00292           _S_write<unsigned int>(__end, __aligned_ptr - __orig_ptr);
00293         else // (__token_size == sizeof(char*))
00294           // Just store the original pointer:
00295           _S_write<char*>(__end, __orig_ptr);
00296         return __aligned_ptr;
00297       }
00298 
00299       char*
00300       _M_unadjust(char* __ptr) const
00301       {
00302         const char* const __end = __ptr + _M_nbytes;
00303         char* __orig_ptr;
00304         const auto __token_size = _M_token_size();
00305         // Read the token and restore the original pointer:
00306         if (__token_size == 1)
00307           __orig_ptr = __ptr - _S_read<unsigned char>(__end);
00308         else if (__token_size == sizeof(short))
00309           __orig_ptr = __ptr - _S_read<unsigned short>(__end);
00310         else if (__token_size == sizeof(int)
00311             && sizeof(int) < sizeof(char*))
00312           __orig_ptr = __ptr - _S_read<unsigned int>(__end);
00313         else // (__token_size == sizeof(char*))
00314           __orig_ptr = _S_read<char*>(__end);
00315         // The adjustment is always less than the requested alignment,
00316         // so if that isn't true now then either the wrong size was passed
00317         // to deallocate or the token was overwritten by a buffer overflow:
00318         __glibcxx_assert(static_cast<size_t>(__ptr - __orig_ptr) < _M_align);
00319         return __orig_ptr;
00320       }
00321 
00322     private:
00323       size_t _M_nbytes;
00324       size_t _M_align;
00325 
00326       // Number of bytes needed to fit block of given size and alignment.
00327       size_t
00328       _M_buf_size() const { return _M_nbytes + _M_align - 1; }
00329 
00330       // Number of additional bytes needed to write the token.
00331       int
00332       _M_token_size() const
00333       {
00334         if (_M_align <= (1ul << __CHAR_BIT__))
00335           return 1;
00336         if (_M_align <= (1ul << (sizeof(short) * __CHAR_BIT__)))
00337           return sizeof(short);
00338         if (_M_align <= (1ull << (sizeof(int) * __CHAR_BIT__)))
00339           return sizeof(int);
00340         return sizeof(char*);
00341       }
00342 
00343       template<typename _Tp>
00344         static void
00345         _S_write(void* __to, _Tp __val)
00346         { __builtin_memcpy(__to, &__val, sizeof(_Tp)); }
00347 
00348       template<typename _Tp>
00349         static _Tp
00350         _S_read(const void* __from)
00351         {
00352           _Tp __val;
00353           __builtin_memcpy(&__val, __from, sizeof(_Tp));
00354           return __val;
00355         }
00356     };
00357 
00358     template<typename _Alloc>
00359       struct __guaranteed_alignment : std::integral_constant<size_t, 1> { };
00360 
00361     template<typename _Tp>
00362       struct __guaranteed_alignment<__gnu_cxx::new_allocator<_Tp>>
00363       : std::alignment_of<std::max_align_t>::type { };
00364 
00365     template<typename _Tp>
00366       struct __guaranteed_alignment<__gnu_cxx::malloc_allocator<_Tp>>
00367       : std::alignment_of<std::max_align_t>::type { };
00368 
00369 #if _GLIBCXX_USE_ALLOCATOR_NEW
00370     template<typename _Tp>
00371       struct __guaranteed_alignment<std::allocator<_Tp>>
00372       : std::alignment_of<std::max_align_t>::type { };
00373 #endif
00374   };
00375 
00376   // 8.7.1 __resource_adaptor_imp
00377   template<typename _Alloc, typename _Resource>
00378     class __resource_adaptor_imp
00379     : public _Resource, private __resource_adaptor_common
00380     {
00381       using memory_resource = _Resource;
00382 
00383       static_assert(is_same<char,
00384           typename allocator_traits<_Alloc>::value_type>::value,
00385           "Allocator's value_type is char");
00386       static_assert(is_same<char*,
00387           typename allocator_traits<_Alloc>::pointer>::value,
00388           "Allocator's pointer type is value_type*");
00389       static_assert(is_same<const char*,
00390           typename allocator_traits<_Alloc>::const_pointer>::value,
00391           "Allocator's const_pointer type is value_type const*");
00392       static_assert(is_same<void*,
00393           typename allocator_traits<_Alloc>::void_pointer>::value,
00394           "Allocator's void_pointer type is void*");
00395       static_assert(is_same<const void*,
00396           typename allocator_traits<_Alloc>::const_void_pointer>::value,
00397           "Allocator's const_void_pointer type is void const*");
00398 
00399     public:
00400       using allocator_type = _Alloc;
00401 
00402       __resource_adaptor_imp() = default;
00403       __resource_adaptor_imp(const __resource_adaptor_imp&) = default;
00404       __resource_adaptor_imp(__resource_adaptor_imp&&) = default;
00405 
00406       explicit __resource_adaptor_imp(const _Alloc& __a2)
00407       : _M_alloc(__a2)
00408       { }
00409 
00410       explicit __resource_adaptor_imp(_Alloc&& __a2)
00411       : _M_alloc(std::move(__a2))
00412       { }
00413 
00414       __resource_adaptor_imp&
00415       operator=(const __resource_adaptor_imp&) = default;
00416 
00417       allocator_type get_allocator() const noexcept { return _M_alloc; }
00418 
00419     protected:
00420       virtual void*
00421       do_allocate(size_t __bytes, size_t __alignment) override
00422       {
00423         if (__alignment <= __guaranteed_alignment<_Alloc>::value)
00424           {
00425             if (__bytes < __alignment)
00426               __bytes = __alignment;
00427             return _M_alloc.allocate(__bytes);
00428           }
00429 
00430 
00431         const _AlignMgr __mgr(__bytes, __alignment);
00432         // Assume _M_alloc returns 1-byte aligned memory, so allocate enough
00433         // space to fit a block of the right size and alignment, plus some
00434         // extra bytes to store a token for retrieving the original pointer.
00435         return __mgr._M_adjust(_M_alloc.allocate(__mgr._M_alloc_size()));
00436       }
00437 
00438       virtual void
00439       do_deallocate(void* __p, size_t __bytes, size_t __alignment) noexcept
00440       override
00441       {
00442         auto __ptr = static_cast<char*>(__p);
00443         if (__alignment <= __guaranteed_alignment<_Alloc>::value)
00444           {
00445             if (__bytes < __alignment)
00446               __bytes = __alignment;
00447             _M_alloc.deallocate(__ptr, __bytes);
00448             return;
00449           }
00450 
00451         const _AlignMgr __mgr(__bytes, __alignment);
00452         // Use the stored token to retrieve the original pointer to deallocate.
00453         _M_alloc.deallocate(__mgr._M_unadjust(__ptr), __mgr._M_alloc_size());
00454       }
00455 
00456       virtual bool
00457       do_is_equal(const memory_resource& __other) const noexcept override
00458       {
00459         if (auto __p = dynamic_cast<const __resource_adaptor_imp*>(&__other))
00460           return _M_alloc == __p->_M_alloc;
00461         return false;
00462       }
00463 
00464     private:
00465       _Alloc _M_alloc{};
00466     };
00467 
00468   // Global memory resources
00469 
00470   inline memory_resource*
00471   new_delete_resource() noexcept
00472   {
00473     using type = resource_adaptor<__gnu_cxx::new_allocator<char>>;
00474     alignas(type) static unsigned char __buf[sizeof(type)];
00475     static type* __r = new(__buf) type;
00476     return __r;
00477   }
00478 
00479   inline memory_resource*
00480   null_memory_resource() noexcept
00481   {
00482     class type final : public memory_resource
00483     {
00484       void*
00485       do_allocate(size_t, size_t) override
00486       { std::__throw_bad_alloc(); }
00487 
00488       void
00489       do_deallocate(void*, size_t, size_t) noexcept override
00490       { }
00491 
00492       bool
00493       do_is_equal(const memory_resource& __other) const noexcept override
00494       { return this == &__other; }
00495     };
00496 
00497     alignas(type) static unsigned char __buf[sizeof(type)];
00498     static type* __r = new(__buf) type;
00499     return __r;
00500   }
00501 
00502   // The default memory resource
00503 
00504   inline std::atomic<memory_resource*>&
00505   __get_default_resource()
00506   {
00507     using type = atomic<memory_resource*>;
00508     alignas(type) static unsigned char __buf[sizeof(type)];
00509     static type* __r = new(__buf) type(new_delete_resource());
00510     return *__r;
00511   }
00512 
00513   inline memory_resource*
00514   get_default_resource() noexcept
00515   { return __get_default_resource().load(); }
00516 
00517   inline memory_resource*
00518   set_default_resource(memory_resource* __r) noexcept
00519   {
00520     if (__r == nullptr)
00521       __r = new_delete_resource();
00522     return __get_default_resource().exchange(__r);
00523   }
00524 
00525 } // namespace pmr
00526 } // namespace fundamentals_v2
00527 } // namespace experimental
00528 
00529 _GLIBCXX_END_NAMESPACE_VERSION
00530 } // namespace std
00531 #endif // _GLIBCXX_EXPERIMENTAL_MEMORY_RESOURCE