1// -*- C++ -*- 2//===----------------------------------------------------------------------===// 3// 4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5// See https://llvm.org/LICENSE.txt for license information. 6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7// 8//===----------------------------------------------------------------------===// 9 10#ifndef _LIBCPP_EXPERIMENTAL_MEMORY_RESOURCE 11#define _LIBCPP_EXPERIMENTAL_MEMORY_RESOURCE 12 13/** 14 experimental/memory_resource synopsis 15 16// C++1y 17 18namespace std { 19namespace experimental { 20inline namespace fundamentals_v1 { 21namespace pmr { 22 23 class memory_resource; 24 25 bool operator==(const memory_resource& a, 26 const memory_resource& b) noexcept; 27 bool operator!=(const memory_resource& a, 28 const memory_resource& b) noexcept; 29 30 template <class Tp> class polymorphic_allocator; 31 32 template <class T1, class T2> 33 bool operator==(const polymorphic_allocator<T1>& a, 34 const polymorphic_allocator<T2>& b) noexcept; 35 template <class T1, class T2> 36 bool operator!=(const polymorphic_allocator<T1>& a, 37 const polymorphic_allocator<T2>& b) noexcept; 38 39 // The name resource_adaptor_imp is for exposition only. 40 template <class Allocator> class resource_adaptor_imp; 41 42 template <class Allocator> 43 using resource_adaptor = resource_adaptor_imp< 44 allocator_traits<Allocator>::rebind_alloc<char>>; 45 46 // Global memory resources 47 memory_resource* new_delete_resource() noexcept; 48 memory_resource* null_memory_resource() noexcept; 49 50 // The default memory resource 51 memory_resource* set_default_resource(memory_resource* r) noexcept; 52 memory_resource* get_default_resource() noexcept; 53 54 // Standard memory resources 55 struct pool_options; 56 class synchronized_pool_resource; 57 class unsynchronized_pool_resource; 58 class monotonic_buffer_resource; 59 60} // namespace pmr 61} // namespace fundamentals_v1 62} // namespace experimental 63} // namespace std 64 65 */ 66 67#include <__assert> // all public C++ headers provide the assertion handler 68#include <__tuple> 69#include <__utility/move.h> 70#include <cstddef> 71#include <cstdlib> 72#include <experimental/__config> 73#include <experimental/__memory> 74#include <limits> 75#include <memory> 76#include <new> 77#include <stdexcept> 78#include <type_traits> 79 80#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 81# pragma GCC system_header 82#endif 83 84_LIBCPP_PUSH_MACROS 85#include <__undef_macros> 86 87_LIBCPP_BEGIN_NAMESPACE_LFTS_PMR 88 89// Round __s up to next multiple of __a. 90inline _LIBCPP_INLINE_VISIBILITY 91size_t __aligned_allocation_size(size_t __s, size_t __a) _NOEXCEPT 92{ 93 _LIBCPP_ASSERT(__s + __a > __s, "aligned allocation size overflows"); 94 return (__s + __a - 1) & ~(__a - 1); 95} 96 97// 8.5, memory.resource 98class _LIBCPP_TYPE_VIS memory_resource 99{ 100 static const size_t __max_align = _LIBCPP_ALIGNOF(max_align_t); 101 102// 8.5.2, memory.resource.public 103public: 104 virtual ~memory_resource() = default; 105 106 _LIBCPP_INLINE_VISIBILITY 107 void* allocate(size_t __bytes, size_t __align = __max_align) 108 { return do_allocate(__bytes, __align); } 109 110 _LIBCPP_INLINE_VISIBILITY 111 void deallocate(void * __p, size_t __bytes, size_t __align = __max_align) 112 { do_deallocate(__p, __bytes, __align); } 113 114 _LIBCPP_INLINE_VISIBILITY 115 bool is_equal(memory_resource const & __other) const _NOEXCEPT 116 { return do_is_equal(__other); } 117 118// 8.5.3, memory.resource.priv 119private: 120 virtual void* do_allocate(size_t, size_t) = 0; 121 virtual void do_deallocate(void*, size_t, size_t) = 0; 122 virtual bool do_is_equal(memory_resource const &) const _NOEXCEPT = 0; 123}; 124 125// 8.5.4, memory.resource.eq 126inline _LIBCPP_INLINE_VISIBILITY 127bool operator==(memory_resource const & __lhs, 128 memory_resource const & __rhs) _NOEXCEPT 129{ 130 return &__lhs == &__rhs || __lhs.is_equal(__rhs); 131} 132 133inline _LIBCPP_INLINE_VISIBILITY 134bool operator!=(memory_resource const & __lhs, 135 memory_resource const & __rhs) _NOEXCEPT 136{ 137 return !(__lhs == __rhs); 138} 139 140_LIBCPP_FUNC_VIS 141memory_resource * new_delete_resource() _NOEXCEPT; 142 143_LIBCPP_FUNC_VIS 144memory_resource * null_memory_resource() _NOEXCEPT; 145 146_LIBCPP_FUNC_VIS 147memory_resource * get_default_resource() _NOEXCEPT; 148 149_LIBCPP_FUNC_VIS 150memory_resource * set_default_resource(memory_resource * __new_res) _NOEXCEPT; 151 152// 8.6, memory.polymorphic.allocator.class 153 154// 8.6.1, memory.polymorphic.allocator.overview 155template <class _ValueType> 156class _LIBCPP_TEMPLATE_VIS polymorphic_allocator 157{ 158public: 159 typedef _ValueType value_type; 160 161 // 8.6.2, memory.polymorphic.allocator.ctor 162 _LIBCPP_INLINE_VISIBILITY 163 polymorphic_allocator() _NOEXCEPT 164 : __res_(_VSTD_LFTS_PMR::get_default_resource()) 165 {} 166 167 _LIBCPP_INLINE_VISIBILITY 168 polymorphic_allocator(memory_resource * __r) _NOEXCEPT 169 : __res_(__r) 170 {} 171 172 polymorphic_allocator(polymorphic_allocator const &) = default; 173 174 template <class _Tp> 175 _LIBCPP_INLINE_VISIBILITY 176 polymorphic_allocator(polymorphic_allocator<_Tp> const & __other) _NOEXCEPT 177 : __res_(__other.resource()) 178 {} 179 180 polymorphic_allocator & 181 operator=(polymorphic_allocator const &) = delete; 182 183 // 8.6.3, memory.polymorphic.allocator.mem 184 _LIBCPP_INLINE_VISIBILITY 185 _ValueType* allocate(size_t __n) { 186 if (__n > __max_size()) 187 __throw_bad_array_new_length(); 188 return static_cast<_ValueType*>( 189 __res_->allocate(__n * sizeof(_ValueType), _LIBCPP_ALIGNOF(_ValueType)) 190 ); 191 } 192 193 _LIBCPP_INLINE_VISIBILITY 194 void deallocate(_ValueType * __p, size_t __n) _NOEXCEPT { 195 _LIBCPP_ASSERT(__n <= __max_size(), 196 "deallocate called for size which exceeds max_size()"); 197 __res_->deallocate(__p, __n * sizeof(_ValueType), _LIBCPP_ALIGNOF(_ValueType)); 198 } 199 200 template <class _Tp, class ..._Ts> 201 _LIBCPP_INLINE_VISIBILITY 202 void construct(_Tp* __p, _Ts &&... __args) 203 { 204 _VSTD_LFTS::__lfts_user_alloc_construct( 205 __p, *this, _VSTD::forward<_Ts>(__args)... 206 ); 207 } 208 209 template <class _T1, class _T2, class ..._Args1, class ..._Args2> 210 _LIBCPP_INLINE_VISIBILITY 211 void construct(pair<_T1, _T2>* __p, piecewise_construct_t, 212 tuple<_Args1...> __x, tuple<_Args2...> __y) 213 { 214 ::new ((void*)__p) pair<_T1, _T2>(piecewise_construct 215 , __transform_tuple( 216 typename __lfts_uses_alloc_ctor< 217 _T1, polymorphic_allocator&, _Args1... 218 >::type() 219 , _VSTD::move(__x) 220 , typename __make_tuple_indices<sizeof...(_Args1)>::type{} 221 ) 222 , __transform_tuple( 223 typename __lfts_uses_alloc_ctor< 224 _T2, polymorphic_allocator&, _Args2... 225 >::type() 226 , _VSTD::move(__y) 227 , typename __make_tuple_indices<sizeof...(_Args2)>::type{} 228 ) 229 ); 230 } 231 232 template <class _T1, class _T2> 233 _LIBCPP_INLINE_VISIBILITY 234 void construct(pair<_T1, _T2>* __p) { 235 construct(__p, piecewise_construct, tuple<>(), tuple<>()); 236 } 237 238 template <class _T1, class _T2, class _Up, class _Vp> 239 _LIBCPP_INLINE_VISIBILITY 240 void construct(pair<_T1, _T2> * __p, _Up && __u, _Vp && __v) { 241 construct(__p, piecewise_construct 242 , _VSTD::forward_as_tuple(_VSTD::forward<_Up>(__u)) 243 , _VSTD::forward_as_tuple(_VSTD::forward<_Vp>(__v))); 244 } 245 246 template <class _T1, class _T2, class _U1, class _U2> 247 _LIBCPP_INLINE_VISIBILITY 248 void construct(pair<_T1, _T2> * __p, pair<_U1, _U2> const & __pr) { 249 construct(__p, piecewise_construct 250 , _VSTD::forward_as_tuple(__pr.first) 251 , _VSTD::forward_as_tuple(__pr.second)); 252 } 253 254 template <class _T1, class _T2, class _U1, class _U2> 255 _LIBCPP_INLINE_VISIBILITY 256 void construct(pair<_T1, _T2> * __p, pair<_U1, _U2> && __pr){ 257 construct(__p, piecewise_construct 258 , _VSTD::forward_as_tuple(_VSTD::forward<_U1>(__pr.first)) 259 , _VSTD::forward_as_tuple(_VSTD::forward<_U2>(__pr.second))); 260 } 261 262 template <class _Tp> 263 _LIBCPP_INLINE_VISIBILITY 264 void destroy(_Tp * __p) _NOEXCEPT 265 { __p->~_Tp(); } 266 267 _LIBCPP_INLINE_VISIBILITY 268 polymorphic_allocator 269 select_on_container_copy_construction() const _NOEXCEPT 270 { return polymorphic_allocator(); } 271 272 _LIBCPP_INLINE_VISIBILITY 273 memory_resource * resource() const _NOEXCEPT 274 { return __res_; } 275 276private: 277 template <class ..._Args, size_t ..._Idx> 278 _LIBCPP_INLINE_VISIBILITY 279 tuple<_Args&&...> 280 __transform_tuple(integral_constant<int, 0>, tuple<_Args...>&& __t, 281 __tuple_indices<_Idx...>) const 282 { 283 return _VSTD::forward_as_tuple(_VSTD::get<_Idx>(_VSTD::move(__t))...); 284 } 285 286 template <class ..._Args, size_t ..._Idx> 287 _LIBCPP_INLINE_VISIBILITY 288 tuple<allocator_arg_t const&, polymorphic_allocator&, _Args&&...> 289 __transform_tuple(integral_constant<int, 1>, tuple<_Args...> && __t, 290 __tuple_indices<_Idx...>) 291 { 292 using _Tup = tuple<allocator_arg_t const&, polymorphic_allocator&, _Args&&...>; 293 return _Tup(allocator_arg, *this, 294 _VSTD::get<_Idx>(_VSTD::move(__t))...); 295 } 296 297 template <class ..._Args, size_t ..._Idx> 298 _LIBCPP_INLINE_VISIBILITY 299 tuple<_Args&&..., polymorphic_allocator&> 300 __transform_tuple(integral_constant<int, 2>, tuple<_Args...> && __t, 301 __tuple_indices<_Idx...>) 302 { 303 using _Tup = tuple<_Args&&..., polymorphic_allocator&>; 304 return _Tup(_VSTD::get<_Idx>(_VSTD::move(__t))..., *this); 305 } 306 307 _LIBCPP_INLINE_VISIBILITY 308 size_t __max_size() const _NOEXCEPT 309 { return numeric_limits<size_t>::max() / sizeof(value_type); } 310 311 memory_resource * __res_; 312}; 313 314// 8.6.4, memory.polymorphic.allocator.eq 315 316template <class _Tp, class _Up> 317inline _LIBCPP_INLINE_VISIBILITY 318bool operator==(polymorphic_allocator<_Tp> const & __lhs, 319 polymorphic_allocator<_Up> const & __rhs) _NOEXCEPT 320{ 321 return *__lhs.resource() == *__rhs.resource(); 322} 323 324template <class _Tp, class _Up> 325inline _LIBCPP_INLINE_VISIBILITY 326bool operator!=(polymorphic_allocator<_Tp> const & __lhs, 327 polymorphic_allocator<_Up> const & __rhs) _NOEXCEPT 328{ 329 return !(__lhs == __rhs); 330} 331 332// 8.7, memory.resource.adaptor 333 334// 8.7.1, memory.resource.adaptor.overview 335template <class _CharAlloc> 336class _LIBCPP_TEMPLATE_VIS __resource_adaptor_imp 337 : public memory_resource 338{ 339 using _CTraits = allocator_traits<_CharAlloc>; 340 static_assert(is_same<typename _CTraits::value_type, char>::value 341 && is_same<typename _CTraits::pointer, char*>::value 342 && is_same<typename _CTraits::void_pointer, void*>::value, ""); 343 344 static const size_t _MaxAlign = _LIBCPP_ALIGNOF(max_align_t); 345 346 using _Alloc = typename _CTraits::template rebind_alloc< 347 typename aligned_storage<_MaxAlign, _MaxAlign>::type 348 >; 349 350 using _ValueType = typename _Alloc::value_type; 351 352 _Alloc __alloc_; 353 354public: 355 typedef _CharAlloc allocator_type; 356 357 __resource_adaptor_imp() = default; 358 __resource_adaptor_imp(__resource_adaptor_imp const &) = default; 359 __resource_adaptor_imp(__resource_adaptor_imp &&) = default; 360 361 // 8.7.2, memory.resource.adaptor.ctor 362 363 _LIBCPP_INLINE_VISIBILITY 364 explicit __resource_adaptor_imp(allocator_type const & __a) 365 : __alloc_(__a) 366 {} 367 368 _LIBCPP_INLINE_VISIBILITY 369 explicit __resource_adaptor_imp(allocator_type && __a) 370 : __alloc_(_VSTD::move(__a)) 371 {} 372 373 __resource_adaptor_imp & 374 operator=(__resource_adaptor_imp const &) = default; 375 376 _LIBCPP_INLINE_VISIBILITY 377 allocator_type get_allocator() const 378 { return __alloc_; } 379 380// 8.7.3, memory.resource.adaptor.mem 381private: 382 virtual void * do_allocate(size_t __bytes, size_t) 383 { 384 if (__bytes > __max_size()) 385 __throw_bad_array_new_length(); 386 size_t __s = __aligned_allocation_size(__bytes, _MaxAlign) / _MaxAlign; 387 return __alloc_.allocate(__s); 388 } 389 390 virtual void do_deallocate(void * __p, size_t __bytes, size_t) 391 { 392 _LIBCPP_ASSERT(__bytes <= __max_size(), 393 "do_deallocate called for size which exceeds the maximum allocation size"); 394 size_t __s = __aligned_allocation_size(__bytes, _MaxAlign) / _MaxAlign; 395 __alloc_.deallocate((_ValueType*)__p, __s); 396 } 397 398 virtual bool do_is_equal(memory_resource const & __other) const _NOEXCEPT { 399 __resource_adaptor_imp const * __p 400 = dynamic_cast<__resource_adaptor_imp const *>(&__other); 401 return __p ? __alloc_ == __p->__alloc_ : false; 402 } 403 404 _LIBCPP_INLINE_VISIBILITY 405 size_t __max_size() const _NOEXCEPT { 406 return numeric_limits<size_t>::max() - _MaxAlign; 407 } 408}; 409 410template <class _Alloc> 411using resource_adaptor = __resource_adaptor_imp< 412 typename allocator_traits<_Alloc>::template rebind_alloc<char> 413 >; 414 415_LIBCPP_END_NAMESPACE_LFTS_PMR 416 417_LIBCPP_POP_MACROS 418 419#endif /* _LIBCPP_EXPERIMENTAL_MEMORY_RESOURCE */ 420