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