libstdc++
|
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 #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 auto __use_tag = __use_alloc<_Tp1, memory_resource*, 00172 _Args...>(this->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 auto __x_use_tag = 00184 __use_alloc<_Tp1, memory_resource*, _Args1...>(this->resource()); 00185 auto __y_use_tag = 00186 __use_alloc<_Tp2, memory_resource*, _Args2...>(this->resource()); 00187 00188 ::new(__p) std::pair<_Tp1, _Tp2>(piecewise_construct, 00189 _M_construct_p(__x_use_tag, __x), 00190 _M_construct_p(__y_use_tag, __y)); 00191 } 00192 00193 template <typename _Tp1, typename _Tp2> 00194 void construct(pair<_Tp1,_Tp2>* __p) 00195 { this->construct(__p, piecewise_construct, tuple<>(), tuple<>()); } 00196 00197 template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp> 00198 void construct(pair<_Tp1,_Tp2>* __p, _Up&& __x, _Vp&& __y) 00199 { this->construct(__p, piecewise_construct, 00200 forward_as_tuple(std::forward<_Up>(__x)), 00201 forward_as_tuple(std::forward<_Vp>(__y))); } 00202 00203 template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp> 00204 void construct(pair<_Tp1,_Tp2>* __p, const std::pair<_Up, _Vp>& __pr) 00205 { this->construct(__p, piecewise_construct, forward_as_tuple(__pr.first), 00206 forward_as_tuple(__pr.second)); } 00207 00208 template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp> 00209 void construct(pair<_Tp1,_Tp2>* __p, pair<_Up, _Vp>&& __pr) 00210 { this->construct(__p, piecewise_construct, 00211 forward_as_tuple(std::forward<_Up>(__pr.first)), 00212 forward_as_tuple(std::forward<_Vp>(__pr.second))); } 00213 00214 template <typename _Up> 00215 void destroy(_Up* __p) 00216 { __p->~_Up(); } 00217 00218 // Return a default-constructed allocator (no allocator propagation) 00219 polymorphic_allocator select_on_container_copy_construction() const 00220 { return polymorphic_allocator(); } 00221 00222 memory_resource* resource() const 00223 { return _M_resource; } 00224 00225 private: 00226 template<typename _Tuple> 00227 _Tuple&& 00228 _M_construct_p(__uses_alloc0, _Tuple& __t) 00229 { return std::move(__t); } 00230 00231 template<typename... _Args> 00232 decltype(auto) 00233 _M_construct_p(__uses_alloc1_ __ua, tuple<_Args...>& __t) 00234 { return tuple_cat(make_tuple(allocator_arg, *(__ua._M_a)), 00235 std::move(__t)); } 00236 00237 template<typename... _Args> 00238 decltype(auto) 00239 _M_construct_p(__uses_alloc2_ __ua, tuple<_Args...>& __t) 00240 { return tuple_cat(std::move(__t), make_tuple(*(__ua._M_a))); } 00241 00242 memory_resource* _M_resource; 00243 }; 00244 00245 template <class _Tp1, class _Tp2> 00246 bool operator==(const polymorphic_allocator<_Tp1>& __a, 00247 const polymorphic_allocator<_Tp2>& __b) noexcept 00248 { return *__a.resource() == *__b.resource(); } 00249 00250 template <class _Tp1, class _Tp2> 00251 bool operator!=(const polymorphic_allocator<_Tp1>& __a, 00252 const polymorphic_allocator<_Tp2>& __b) noexcept 00253 { return !(__a == __b); } 00254 00255 // 8.7.1 __resource_adaptor_imp 00256 template <typename _Alloc> 00257 class __resource_adaptor_imp : public memory_resource 00258 { 00259 public: 00260 using allocator_type = _Alloc; 00261 00262 __resource_adaptor_imp() = default; 00263 __resource_adaptor_imp(const __resource_adaptor_imp&) = default; 00264 __resource_adaptor_imp(__resource_adaptor_imp&&) = default; 00265 00266 explicit __resource_adaptor_imp(const _Alloc& __a2) 00267 : _M_alloc(__a2) 00268 { } 00269 00270 explicit __resource_adaptor_imp(_Alloc&& __a2) 00271 : _M_alloc(std::move(__a2)) 00272 { } 00273 00274 __resource_adaptor_imp& 00275 operator=(const __resource_adaptor_imp&) = default; 00276 00277 allocator_type get_allocator() const { return _M_alloc; } 00278 00279 protected: 00280 virtual void* 00281 do_allocate(size_t __bytes, size_t __alignment) 00282 { 00283 using _Aligned_alloc = std::__alloc_rebind<_Alloc, char>; 00284 size_t __new_size = _S_aligned_size(__bytes, 00285 _S_supported(__alignment) ? 00286 __alignment : _S_max_align); 00287 return _Aligned_alloc(_M_alloc).allocate(__new_size); 00288 } 00289 00290 virtual void 00291 do_deallocate(void* __p, size_t __bytes, size_t __alignment) 00292 { 00293 using _Aligned_alloc = std::__alloc_rebind<_Alloc, char>; 00294 size_t __new_size = _S_aligned_size(__bytes, 00295 _S_supported(__alignment) ? 00296 __alignment : _S_max_align); 00297 using _Ptr = typename allocator_traits<_Aligned_alloc>::pointer; 00298 _Aligned_alloc(_M_alloc).deallocate(static_cast<_Ptr>(__p), 00299 __new_size); 00300 } 00301 00302 virtual bool 00303 do_is_equal(const memory_resource& __other) const noexcept 00304 { 00305 auto __p = dynamic_cast<const __resource_adaptor_imp*>(&__other); 00306 return __p ? (_M_alloc == __p->_M_alloc) : false; 00307 } 00308 00309 private: 00310 // Calculate Aligned Size 00311 // Returns a size that is larger than or equal to __size and divisible 00312 // by __alignment, where __alignment is required to be the power of 2. 00313 static size_t 00314 _S_aligned_size(size_t __size, size_t __alignment) 00315 { return ((__size - 1)|(__alignment - 1)) + 1; } 00316 00317 // Determine whether alignment meets one of those preconditions: 00318 // 1. Equals to Zero 00319 // 2. Is power of two 00320 static bool 00321 _S_supported (size_t __x) 00322 { return ((__x != 0) && !(__x & (__x - 1))); } 00323 00324 _Alloc _M_alloc; 00325 }; 00326 00327 // Global memory resources 00328 inline std::atomic<memory_resource*>& 00329 __get_default_resource() 00330 { 00331 static atomic<memory_resource*> _S_default_resource(new_delete_resource()); 00332 return _S_default_resource; 00333 } 00334 00335 inline memory_resource* 00336 new_delete_resource() noexcept 00337 { 00338 static resource_adaptor<std::allocator<char>> __r; 00339 return static_cast<memory_resource*>(&__r); 00340 } 00341 00342 template <typename _Alloc> 00343 class __null_memory_resource : private memory_resource 00344 { 00345 protected: 00346 void* 00347 do_allocate(size_t, size_t) 00348 { std::__throw_bad_alloc(); } 00349 00350 void 00351 do_deallocate(void*, size_t, size_t) noexcept 00352 { } 00353 00354 bool 00355 do_is_equal(const memory_resource& __other) const noexcept 00356 { return this == &__other; } 00357 00358 friend memory_resource* null_memory_resource() noexcept; 00359 }; 00360 00361 inline memory_resource* 00362 null_memory_resource() noexcept 00363 { 00364 static __null_memory_resource<void> __r; 00365 return static_cast<memory_resource*>(&__r); 00366 } 00367 00368 // The default memory resource 00369 inline memory_resource* 00370 get_default_resource() noexcept 00371 { return __get_default_resource().load(); } 00372 00373 inline memory_resource* 00374 set_default_resource(memory_resource* __r) noexcept 00375 { 00376 if (__r == nullptr) 00377 __r = new_delete_resource(); 00378 return __get_default_resource().exchange(__r); 00379 } 00380 00381 _GLIBCXX_END_NAMESPACE_VERSION 00382 } // namespace pmr 00383 } // namespace fundamentals_v2 00384 } // namespace experimental 00385 } // namespace std 00386 00387 #endif