libstdc++
memory_resource
Go to the documentation of this file.
00001 // <experimental/memory_resource> -*- C++ -*-
00002 
00003 // Copyright (C) 2015-2018 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>
00033 #include <new>
00034 #include <atomic>
00035 #include <cstddef>
00036 #include <experimental/bits/lfts_config.h>
00037 
00038 namespace std {
00039 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00040 
00041 namespace experimental {
00042 inline namespace fundamentals_v2 {
00043 namespace pmr {
00044 #define __cpp_lib_experimental_memory_resources 201402L
00045 
00046   class memory_resource;
00047 
00048   template <typename _Tp>
00049     class polymorphic_allocator;
00050 
00051   template <typename _Alloc>
00052     class __resource_adaptor_imp;
00053 
00054   template <typename _Alloc>
00055     using resource_adaptor = __resource_adaptor_imp<
00056       typename allocator_traits<_Alloc>::template rebind_alloc<char>>;
00057 
00058   template <typename _Tp>
00059     struct __uses_allocator_construction_helper;
00060 
00061   // Global memory resources
00062   memory_resource* new_delete_resource() noexcept;
00063   memory_resource* null_memory_resource() noexcept;
00064 
00065   // The default memory resource
00066   memory_resource* get_default_resource() noexcept;
00067   memory_resource* set_default_resource(memory_resource* __r) noexcept;
00068 
00069   // Standard memory resources
00070 
00071   // 8.5 Class memory_resource
00072   class memory_resource
00073   {
00074   protected:
00075     static constexpr size_t _S_max_align = alignof(max_align_t);
00076 
00077   public:
00078     virtual ~memory_resource() { }
00079 
00080     void*
00081     allocate(size_t __bytes, size_t __alignment = _S_max_align)
00082     { return do_allocate(__bytes, __alignment); }
00083 
00084     void
00085     deallocate(void* __p, size_t __bytes, size_t __alignment = _S_max_align)
00086     { return do_deallocate(__p, __bytes, __alignment); }
00087 
00088     bool
00089     is_equal(const memory_resource& __other) const noexcept
00090     { return do_is_equal(__other); }
00091 
00092   protected:
00093     virtual void*
00094     do_allocate(size_t __bytes, size_t __alignment) = 0;
00095 
00096     virtual void
00097     do_deallocate(void* __p, size_t __bytes, size_t __alignment) = 0;
00098 
00099     virtual bool
00100     do_is_equal(const memory_resource& __other) const noexcept = 0;
00101   };
00102 
00103   inline bool
00104   operator==(const memory_resource& __a,
00105              const memory_resource& __b) noexcept
00106   { return &__a == &__b || __a.is_equal(__b); }
00107 
00108   inline bool
00109   operator!=(const memory_resource& __a,
00110              const memory_resource& __b) noexcept
00111   { return !(__a == __b); }
00112 
00113 
00114   // 8.6 Class template polymorphic_allocator
00115   template <class _Tp>
00116     class polymorphic_allocator
00117     {
00118       using __uses_alloc1_ = __uses_alloc1<memory_resource*>;
00119       using __uses_alloc2_ = __uses_alloc2<memory_resource*>;
00120 
00121       template<typename _Tp1, typename... _Args>
00122         void
00123         _M_construct(__uses_alloc0, _Tp1* __p, _Args&&... __args)
00124         { ::new(__p) _Tp1(std::forward<_Args>(__args)...); }
00125 
00126       template<typename _Tp1, typename... _Args>
00127         void
00128         _M_construct(__uses_alloc1_, _Tp1* __p, _Args&&...  __args)
00129         { ::new(__p) _Tp1(allocator_arg, this->resource(),
00130                           std::forward<_Args>(__args)...); }
00131 
00132       template<typename _Tp1, typename... _Args>
00133         void
00134         _M_construct(__uses_alloc2_, _Tp1* __p, _Args&&...  __args)
00135         { ::new(__p) _Tp1(std::forward<_Args>(__args)...,
00136                           this->resource()); }
00137 
00138     public:
00139       using value_type = _Tp;
00140 
00141       polymorphic_allocator() noexcept
00142       : _M_resource(get_default_resource())
00143       { }
00144 
00145       polymorphic_allocator(memory_resource* __r)
00146       : _M_resource(__r)
00147       { _GLIBCXX_DEBUG_ASSERT(__r); }
00148 
00149       polymorphic_allocator(const polymorphic_allocator& __other) = default;
00150 
00151       template <typename _Up>
00152         polymorphic_allocator(const polymorphic_allocator<_Up>&
00153                               __other) noexcept
00154         : _M_resource(__other.resource())
00155         { }
00156 
00157       polymorphic_allocator&
00158         operator=(const polymorphic_allocator& __rhs) = default;
00159 
00160       _Tp* allocate(size_t __n)
00161       { return static_cast<_Tp*>(_M_resource->allocate(__n * sizeof(_Tp),
00162                                                        alignof(_Tp))); }
00163 
00164       void deallocate(_Tp* __p, size_t __n)
00165       { _M_resource->deallocate(__p, __n * sizeof(_Tp), alignof(_Tp)); }
00166 
00167       template <typename _Tp1, typename... _Args> //used here
00168         void construct(_Tp1* __p, _Args&&... __args)
00169         {
00170           memory_resource* const __resource = this->resource();
00171           auto __use_tag
00172             = __use_alloc<_Tp1, memory_resource*, _Args...>(__resource);
00173           _M_construct(__use_tag, __p, std::forward<_Args>(__args)...);
00174         }
00175 
00176       // Specializations for pair using piecewise construction
00177       template <typename _Tp1, typename _Tp2,
00178                typename... _Args1, typename... _Args2>
00179         void construct(pair<_Tp1, _Tp2>* __p, piecewise_construct_t,
00180                        tuple<_Args1...> __x,
00181                        tuple<_Args2...> __y)
00182         {
00183           memory_resource* const __resource = this->resource();
00184           auto __x_use_tag =
00185             __use_alloc<_Tp1, memory_resource*, _Args1...>(__resource);
00186           auto __y_use_tag =
00187             __use_alloc<_Tp2, memory_resource*, _Args2...>(__resource);
00188 
00189           ::new(__p) std::pair<_Tp1, _Tp2>(piecewise_construct,
00190                                            _M_construct_p(__x_use_tag, __x),
00191                                            _M_construct_p(__y_use_tag, __y));
00192         }
00193 
00194       template <typename _Tp1, typename _Tp2>
00195         void construct(pair<_Tp1,_Tp2>* __p)
00196         { this->construct(__p, piecewise_construct, tuple<>(), tuple<>()); }
00197 
00198       template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
00199         void construct(pair<_Tp1,_Tp2>* __p, _Up&& __x, _Vp&& __y)
00200         { this->construct(__p, piecewise_construct,
00201                           forward_as_tuple(std::forward<_Up>(__x)),
00202                           forward_as_tuple(std::forward<_Vp>(__y))); }
00203 
00204       template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
00205         void construct(pair<_Tp1,_Tp2>* __p, const std::pair<_Up, _Vp>& __pr)
00206         { this->construct(__p, piecewise_construct, forward_as_tuple(__pr.first),
00207                           forward_as_tuple(__pr.second)); }
00208 
00209       template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
00210         void construct(pair<_Tp1,_Tp2>* __p, pair<_Up, _Vp>&& __pr)
00211         { this->construct(__p, piecewise_construct,
00212                           forward_as_tuple(std::forward<_Up>(__pr.first)),
00213                           forward_as_tuple(std::forward<_Vp>(__pr.second))); }
00214 
00215       template <typename _Up>
00216         void destroy(_Up* __p)
00217         { __p->~_Up(); }
00218 
00219       // Return a default-constructed allocator (no allocator propagation)
00220       polymorphic_allocator select_on_container_copy_construction() const
00221       { return polymorphic_allocator(); }
00222 
00223       memory_resource* resource() const
00224       { return _M_resource; }
00225 
00226     private:
00227       template<typename _Tuple>
00228         _Tuple&&
00229         _M_construct_p(__uses_alloc0, _Tuple& __t)
00230         { return std::move(__t); }
00231 
00232       template<typename... _Args>
00233         decltype(auto)
00234         _M_construct_p(__uses_alloc1_ __ua, tuple<_Args...>& __t)
00235         { return tuple_cat(make_tuple(allocator_arg, *(__ua._M_a)),
00236                            std::move(__t)); }
00237 
00238       template<typename... _Args>
00239         decltype(auto)
00240         _M_construct_p(__uses_alloc2_ __ua, tuple<_Args...>& __t)
00241         { return tuple_cat(std::move(__t), make_tuple(*(__ua._M_a))); }
00242 
00243       memory_resource* _M_resource;
00244     };
00245 
00246   template <class _Tp1, class _Tp2>
00247     bool operator==(const polymorphic_allocator<_Tp1>& __a,
00248                     const polymorphic_allocator<_Tp2>& __b) noexcept
00249     { return *__a.resource() == *__b.resource(); }
00250 
00251   template <class _Tp1, class _Tp2>
00252     bool operator!=(const polymorphic_allocator<_Tp1>& __a,
00253                     const polymorphic_allocator<_Tp2>& __b) noexcept
00254     { return !(__a == __b); }
00255 
00256   // 8.7.1 __resource_adaptor_imp
00257   template <typename _Alloc>
00258     class __resource_adaptor_imp : public memory_resource
00259     {
00260       static_assert(is_same<char,
00261           typename allocator_traits<_Alloc>::value_type>::value,
00262           "Allocator's value_type is char");
00263       static_assert(is_same<char*,
00264           typename allocator_traits<_Alloc>::pointer>::value,
00265           "Allocator's pointer type is value_type*");
00266       static_assert(is_same<const char*,
00267           typename allocator_traits<_Alloc>::const_pointer>::value,
00268           "Allocator's const_pointer type is value_type const*");
00269       static_assert(is_same<void*,
00270           typename allocator_traits<_Alloc>::void_pointer>::value,
00271           "Allocator's void_pointer type is void*");
00272       static_assert(is_same<const void*,
00273           typename allocator_traits<_Alloc>::const_void_pointer>::value,
00274           "Allocator's const_void_pointer type is void const*");
00275 
00276     public:
00277       using allocator_type = _Alloc;
00278 
00279       __resource_adaptor_imp() = default;
00280       __resource_adaptor_imp(const __resource_adaptor_imp&) = default;
00281       __resource_adaptor_imp(__resource_adaptor_imp&&) = default;
00282 
00283       explicit __resource_adaptor_imp(const _Alloc& __a2)
00284       : _M_alloc(__a2)
00285       { }
00286 
00287       explicit __resource_adaptor_imp(_Alloc&& __a2)
00288       : _M_alloc(std::move(__a2))
00289       { }
00290 
00291       __resource_adaptor_imp&
00292       operator=(const __resource_adaptor_imp&) = default;
00293 
00294       allocator_type get_allocator() const noexcept { return _M_alloc; }
00295 
00296     protected:
00297       virtual void*
00298       do_allocate(size_t __bytes, size_t __alignment)
00299       {
00300         using _Aligned_alloc = std::__alloc_rebind<_Alloc, char>;
00301         size_t __new_size = _S_aligned_size(__bytes,
00302                                             _S_supported(__alignment) ?
00303                                             __alignment : _S_max_align);
00304         return _Aligned_alloc(_M_alloc).allocate(__new_size);
00305       }
00306 
00307       virtual void
00308       do_deallocate(void* __p, size_t __bytes, size_t __alignment)
00309       {
00310         using _Aligned_alloc = std::__alloc_rebind<_Alloc, char>;
00311         size_t __new_size = _S_aligned_size(__bytes,
00312                                             _S_supported(__alignment) ?
00313                                             __alignment : _S_max_align);
00314         using _Ptr = typename allocator_traits<_Aligned_alloc>::pointer;
00315         _Aligned_alloc(_M_alloc).deallocate(static_cast<_Ptr>(__p),
00316                                             __new_size);
00317       }
00318 
00319       virtual bool
00320       do_is_equal(const memory_resource& __other) const noexcept
00321       {
00322         auto __p = dynamic_cast<const __resource_adaptor_imp*>(&__other);
00323         return __p ? (_M_alloc == __p->_M_alloc) : false;
00324       }
00325 
00326     private:
00327       // Calculate Aligned Size
00328       // Returns a size that is larger than or equal to __size and divisible
00329       // by __alignment, where __alignment is required to be a power of 2.
00330       static size_t
00331       _S_aligned_size(size_t __size, size_t __alignment)
00332       { return ((__size - 1)|(__alignment - 1)) + 1; }
00333 
00334       // Determine whether alignment meets one of those preconditions:
00335       // 1. Equal to Zero
00336       // 2. Is power of two
00337       static bool
00338       _S_supported (size_t __x)
00339       { return ((__x != 0) && !(__x & (__x - 1))); }
00340 
00341       _Alloc _M_alloc;
00342     };
00343 
00344   // Global memory resources
00345 
00346   inline memory_resource*
00347   new_delete_resource() noexcept
00348   {
00349     using type = resource_adaptor<std::allocator<char>>;
00350     alignas(type) static unsigned char __buf[sizeof(type)];
00351     static type* __r = new(__buf) type;
00352     return __r;
00353   }
00354 
00355   inline memory_resource*
00356   null_memory_resource() noexcept
00357   {
00358     class type final : public memory_resource
00359     {
00360       void*
00361       do_allocate(size_t, size_t) override
00362       { std::__throw_bad_alloc(); }
00363 
00364       void
00365       do_deallocate(void*, size_t, size_t) noexcept override
00366       { }
00367 
00368       bool
00369       do_is_equal(const memory_resource& __other) const noexcept override
00370       { return this == &__other; }
00371     };
00372 
00373     alignas(type) static unsigned char __buf[sizeof(type)];
00374     static type* __r = new(__buf) type;
00375     return __r;
00376   }
00377 
00378   // The default memory resource
00379 
00380   inline std::atomic<memory_resource*>&
00381   __get_default_resource()
00382   {
00383     using type = atomic<memory_resource*>;
00384     alignas(type) static unsigned char __buf[sizeof(type)];
00385     static type* __r = new(__buf) type(new_delete_resource());
00386     return *__r;
00387   }
00388 
00389   inline memory_resource*
00390   get_default_resource() noexcept
00391   { return __get_default_resource().load(); }
00392 
00393   inline memory_resource*
00394   set_default_resource(memory_resource* __r) noexcept
00395   {
00396     if (__r == nullptr)
00397       __r = new_delete_resource();
00398     return __get_default_resource().exchange(__r);
00399   }
00400 } // namespace pmr
00401 } // namespace fundamentals_v2
00402 } // namespace experimental
00403 
00404 _GLIBCXX_END_NAMESPACE_VERSION
00405 } // namespace std
00406 
00407 #endif