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