1// <experimental/memory_resource> -*- C++ -*- 2 3// Copyright (C) 2015-2021 Free Software Foundation, Inc. 4// 5// This file is part of the GNU ISO C++ Library. This library is free 6// software; you can redistribute it and/or modify it under the 7// terms of the GNU General Public License as published by the 8// Free Software Foundation; either version 3, or (at your option) 9// any later version. 10 11// This library is distributed in the hope that it will be useful, 12// but WITHOUT ANY WARRANTY; without even the implied warranty of 13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14// GNU General Public License for more details. 15 16// Under Section 7 of GPL version 3, you are granted additional 17// permissions described in the GCC Runtime Library Exception, version 18// 3.1, as published by the Free Software Foundation. 19 20// You should have received a copy of the GNU General Public License and 21// a copy of the GCC Runtime Library Exception along with this program; 22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23// <http://www.gnu.org/licenses/>. 24 25/** @file experimental/memory_resource 26 * This is a TS C++ Library header. 27 * @ingroup libfund-ts 28 */ 29 30#ifndef _GLIBCXX_EXPERIMENTAL_MEMORY_RESOURCE 31#define _GLIBCXX_EXPERIMENTAL_MEMORY_RESOURCE 1 32 33#pragma GCC system_header 34 35#if __cplusplus >= 201402L 36 37#include <memory> // align, uses_allocator, __uses_alloc 38#include <experimental/utility> // pair, experimental::erased_type 39#include <tuple> // tuple, forward_as_tuple 40#include <atomic> // atomic 41#include <new> // placement new 42#include <cstddef> // max_align_t 43#include <ext/new_allocator.h> 44#include <debug/assertions.h> 45 46/// @cond 47namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) 48{ 49_GLIBCXX_BEGIN_NAMESPACE_VERSION 50 template<typename _Tp> class malloc_allocator; 51_GLIBCXX_END_NAMESPACE_VERSION 52} // namespace __gnu_cxx 53/// @endcond 54 55namespace std { 56_GLIBCXX_BEGIN_NAMESPACE_VERSION 57 58namespace experimental { 59inline namespace fundamentals_v2 { 60namespace pmr { 61#define __cpp_lib_experimental_memory_resources 201402L 62 63 // Standard memory resources 64 65 // 8.5 Class memory_resource 66 class memory_resource; 67 68 // 8.6 Class template polymorphic_allocator 69 template<typename _Tp> 70 class polymorphic_allocator; 71 72 template<typename _Alloc, typename _Resource = memory_resource> 73 class __resource_adaptor_imp; 74 75 // 8.7 Alias template resource_adaptor 76 template<typename _Alloc> 77 using resource_adaptor = __resource_adaptor_imp< 78 typename allocator_traits<_Alloc>::template rebind_alloc<char>>; 79 80 // 8.8 Global memory resources 81 memory_resource* new_delete_resource() noexcept; 82 memory_resource* null_memory_resource() noexcept; 83 memory_resource* get_default_resource() noexcept; 84 memory_resource* set_default_resource(memory_resource* __r) noexcept; 85 86 // TODO 8.9 Pool resource classes 87 88 class memory_resource 89 { 90 static constexpr size_t _S_max_align = alignof(max_align_t); 91 92 public: 93 memory_resource() = default; 94 memory_resource(const memory_resource&) = default; 95 virtual ~memory_resource() = default; 96 97 memory_resource& operator=(const memory_resource&) = default; 98 99 _GLIBCXX_NODISCARD void* 100 allocate(size_t __bytes, size_t __alignment = _S_max_align) 101 { return do_allocate(__bytes, __alignment); } 102 103 void 104 deallocate(void* __p, size_t __bytes, size_t __alignment = _S_max_align) 105 { return do_deallocate(__p, __bytes, __alignment); } 106 107 bool 108 is_equal(const memory_resource& __other) const noexcept 109 { return do_is_equal(__other); } 110 111 protected: 112 virtual void* 113 do_allocate(size_t __bytes, size_t __alignment) = 0; 114 115 virtual void 116 do_deallocate(void* __p, size_t __bytes, size_t __alignment) = 0; 117 118 virtual bool 119 do_is_equal(const memory_resource& __other) const noexcept = 0; 120 }; 121 122 inline bool 123 operator==(const memory_resource& __a, const memory_resource& __b) noexcept 124 { return &__a == &__b || __a.is_equal(__b); } 125 126 inline bool 127 operator!=(const memory_resource& __a, const memory_resource& __b) noexcept 128 { return !(__a == __b); } 129 130 131 template<typename _Tp> 132 class polymorphic_allocator 133 { 134 public: 135 using value_type = _Tp; 136 137 polymorphic_allocator() noexcept 138 : _M_resource(get_default_resource()) 139 { } 140 141 polymorphic_allocator(memory_resource* __r) 142 : _M_resource(__r) 143 { _GLIBCXX_DEBUG_ASSERT(__r); } 144 145 polymorphic_allocator(const polymorphic_allocator& __other) = default; 146 147 template <typename _Up> 148 polymorphic_allocator(const polymorphic_allocator<_Up>& 149 __other) noexcept 150 : _M_resource(__other.resource()) 151 { } 152 153 polymorphic_allocator& 154 operator=(const polymorphic_allocator& __rhs) = default; 155 156 _GLIBCXX_NODISCARD _Tp* allocate(size_t __n) 157 { return static_cast<_Tp*>(_M_resource->allocate(__n * sizeof(_Tp), 158 alignof(_Tp))); } 159 160 void 161 deallocate(_Tp* __p, size_t __n) 162 { _M_resource->deallocate(__p, __n * sizeof(_Tp), alignof(_Tp)); } 163 164 template <typename _Tp1, typename... _Args> //used here 165 void 166 construct(_Tp1* __p, _Args&&... __args) 167 { 168 std::__uses_allocator_construct(this->resource(), __p, 169 std::forward<_Args>(__args)...); 170 } 171 172 // Specializations for pair using piecewise construction 173 template <typename _Tp1, typename _Tp2, 174 typename... _Args1, typename... _Args2> 175 void 176 construct(pair<_Tp1, _Tp2>* __p, piecewise_construct_t, 177 tuple<_Args1...> __x, tuple<_Args2...> __y) 178 { 179 memory_resource* const __resource = this->resource(); 180 auto __x_use_tag = 181 std::__use_alloc<_Tp1, memory_resource*, _Args1...>(__resource); 182 auto __y_use_tag = 183 std::__use_alloc<_Tp2, memory_resource*, _Args2...>(__resource); 184 185 ::new(__p) std::pair<_Tp1, _Tp2>(piecewise_construct, 186 _M_construct_p(__x_use_tag, __x), 187 _M_construct_p(__y_use_tag, __y)); 188 } 189 190 template <typename _Tp1, typename _Tp2> 191 void 192 construct(pair<_Tp1,_Tp2>* __p) 193 { this->construct(__p, piecewise_construct, tuple<>(), tuple<>()); } 194 195 template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp> 196 void 197 construct(pair<_Tp1,_Tp2>* __p, _Up&& __x, _Vp&& __y) 198 { 199 this->construct(__p, piecewise_construct, 200 std::forward_as_tuple(std::forward<_Up>(__x)), 201 std::forward_as_tuple(std::forward<_Vp>(__y))); 202 } 203 204 template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp> 205 void 206 construct(pair<_Tp1,_Tp2>* __p, const std::pair<_Up, _Vp>& __pr) 207 { 208 this->construct(__p, piecewise_construct, 209 std::forward_as_tuple(__pr.first), 210 std::forward_as_tuple(__pr.second)); 211 } 212 213 template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp> 214 void 215 construct(pair<_Tp1,_Tp2>* __p, pair<_Up, _Vp>&& __pr) 216 { 217 this->construct(__p, piecewise_construct, 218 std::forward_as_tuple(std::forward<_Up>(__pr.first)), 219 std::forward_as_tuple(std::forward<_Vp>(__pr.second))); 220 } 221 222 template <typename _Up> 223 void 224 destroy(_Up* __p) 225 { __p->~_Up(); } 226 227 // Return a default-constructed allocator (no allocator propagation) 228 polymorphic_allocator 229 select_on_container_copy_construction() const 230 { return polymorphic_allocator(); } 231 232 memory_resource* resource() const { return _M_resource; } 233 234 private: 235 using __uses_alloc1_ = __uses_alloc1<memory_resource*>; 236 using __uses_alloc2_ = __uses_alloc2<memory_resource*>; 237 238 template<typename _Tuple> 239 _Tuple&& 240 _M_construct_p(__uses_alloc0, _Tuple& __t) 241 { return std::move(__t); } 242 243 template<typename... _Args> 244 decltype(auto) 245 _M_construct_p(__uses_alloc1_ __ua, tuple<_Args...>& __t) 246 { return tuple_cat(make_tuple(allocator_arg, *(__ua._M_a)), 247 std::move(__t)); } 248 249 template<typename... _Args> 250 decltype(auto) 251 _M_construct_p(__uses_alloc2_ __ua, tuple<_Args...>& __t) 252 { return tuple_cat(std::move(__t), make_tuple(*(__ua._M_a))); } 253 254 memory_resource* _M_resource; 255 }; 256 257 template <class _Tp1, class _Tp2> 258 bool 259 operator==(const polymorphic_allocator<_Tp1>& __a, 260 const polymorphic_allocator<_Tp2>& __b) noexcept 261 { return *__a.resource() == *__b.resource(); } 262 263 template <class _Tp1, class _Tp2> 264 bool 265 operator!=(const polymorphic_allocator<_Tp1>& __a, 266 const polymorphic_allocator<_Tp2>& __b) noexcept 267 { return !(__a == __b); } 268 269 270 /// @cond undocumented 271 class __resource_adaptor_common 272 { 273 template<typename, typename> friend class __resource_adaptor_imp; 274 275 struct _AlignMgr 276 { 277 _AlignMgr(size_t __nbytes, size_t __align) 278 : _M_nbytes(__nbytes), _M_align(__align) 279 { } 280 281 // Total size that needs to be allocated. 282 size_t 283 _M_alloc_size() const { return _M_buf_size() + _M_token_size(); } 284 285 void* 286 _M_adjust(void* __ptr) const 287 { 288 const auto __orig_ptr = static_cast<char*>(__ptr); 289 size_t __space = _M_buf_size(); 290 // Align the pointer within the buffer: 291 std::align(_M_align, _M_nbytes, __ptr, __space); 292 const auto __aligned_ptr = static_cast<char*>(__ptr); 293 const auto __token_size = _M_token_size(); 294 // Store token immediately after the aligned block: 295 char* const __end = __aligned_ptr + _M_nbytes; 296 if (__token_size == 1) 297 _S_write<unsigned char>(__end, __aligned_ptr - __orig_ptr); 298 else if (__token_size == sizeof(short)) 299 _S_write<unsigned short>(__end, __aligned_ptr - __orig_ptr); 300 else if (__token_size == sizeof(int) && sizeof(int) < sizeof(char*)) 301 _S_write<unsigned int>(__end, __aligned_ptr - __orig_ptr); 302 else // (__token_size == sizeof(char*)) 303 // Just store the original pointer: 304 _S_write<char*>(__end, __orig_ptr); 305 return __aligned_ptr; 306 } 307 308 char* 309 _M_unadjust(char* __ptr) const 310 { 311 const char* const __end = __ptr + _M_nbytes; 312 char* __orig_ptr; 313 const auto __token_size = _M_token_size(); 314 // Read the token and restore the original pointer: 315 if (__token_size == 1) 316 __orig_ptr = __ptr - _S_read<unsigned char>(__end); 317 else if (__token_size == sizeof(short)) 318 __orig_ptr = __ptr - _S_read<unsigned short>(__end); 319 else if (__token_size == sizeof(int) 320 && sizeof(int) < sizeof(char*)) 321 __orig_ptr = __ptr - _S_read<unsigned int>(__end); 322 else // (__token_size == sizeof(char*)) 323 __orig_ptr = _S_read<char*>(__end); 324 // The adjustment is always less than the requested alignment, 325 // so if that isn't true now then either the wrong size was passed 326 // to deallocate or the token was overwritten by a buffer overflow: 327 __glibcxx_assert(static_cast<size_t>(__ptr - __orig_ptr) < _M_align); 328 return __orig_ptr; 329 } 330 331 private: 332 size_t _M_nbytes; 333 size_t _M_align; 334 335 // Number of bytes needed to fit block of given size and alignment. 336 size_t 337 _M_buf_size() const { return _M_nbytes + _M_align - 1; } 338 339 // Number of additional bytes needed to write the token. 340 int 341 _M_token_size() const 342 { 343 if (_M_align <= (1ul << __CHAR_BIT__)) 344 return 1; 345 if (_M_align <= (1ul << (sizeof(short) * __CHAR_BIT__))) 346 return sizeof(short); 347 if (_M_align <= (1ull << (sizeof(int) * __CHAR_BIT__))) 348 return sizeof(int); 349 return sizeof(char*); 350 } 351 352 template<typename _Tp> 353 static void 354 _S_write(void* __to, _Tp __val) 355 { __builtin_memcpy(__to, &__val, sizeof(_Tp)); } 356 357 template<typename _Tp> 358 static _Tp 359 _S_read(const void* __from) 360 { 361 _Tp __val; 362 __builtin_memcpy(&__val, __from, sizeof(_Tp)); 363 return __val; 364 } 365 }; 366 }; 367 /// @endcond 368 369 // 8.7.1 __resource_adaptor_imp 370 template<typename _Alloc, typename _Resource> 371 class __resource_adaptor_imp 372 : public _Resource, private __resource_adaptor_common 373 { 374 using memory_resource = _Resource; 375 376 static_assert(is_same<char, 377 typename allocator_traits<_Alloc>::value_type>::value, 378 "Allocator's value_type is char"); 379 static_assert(is_same<char*, 380 typename allocator_traits<_Alloc>::pointer>::value, 381 "Allocator's pointer type is value_type*"); 382 static_assert(is_same<const char*, 383 typename allocator_traits<_Alloc>::const_pointer>::value, 384 "Allocator's const_pointer type is value_type const*"); 385 static_assert(is_same<void*, 386 typename allocator_traits<_Alloc>::void_pointer>::value, 387 "Allocator's void_pointer type is void*"); 388 static_assert(is_same<const void*, 389 typename allocator_traits<_Alloc>::const_void_pointer>::value, 390 "Allocator's const_void_pointer type is void const*"); 391 392 public: 393 using allocator_type = _Alloc; 394 395 __resource_adaptor_imp() = default; 396 __resource_adaptor_imp(const __resource_adaptor_imp&) = default; 397 __resource_adaptor_imp(__resource_adaptor_imp&&) = default; 398 399 explicit __resource_adaptor_imp(const _Alloc& __a2) 400 : _M_alloc(__a2) 401 { } 402 403 explicit __resource_adaptor_imp(_Alloc&& __a2) 404 : _M_alloc(std::move(__a2)) 405 { } 406 407 __resource_adaptor_imp& 408 operator=(const __resource_adaptor_imp&) = default; 409 410 allocator_type get_allocator() const noexcept { return _M_alloc; } 411 412 protected: 413 virtual void* 414 do_allocate(size_t __bytes, size_t __alignment) override 415 { 416 // Cannot use max_align_t on 32-bit Solaris x86, see PR libstdc++/77691 417#if ! ((defined __sun__ || defined __VXWORKS__) && defined __i386__) 418 if (__alignment == alignof(max_align_t)) 419 return _M_allocate<alignof(max_align_t)>(__bytes); 420#endif 421 switch (__alignment) 422 { 423 case 1: 424 return _M_alloc.allocate(__bytes); 425 case 2: 426 return _M_allocate<2>(__bytes); 427 case 4: 428 return _M_allocate<4>(__bytes); 429 case 8: 430 return _M_allocate<8>(__bytes); 431 } 432 const _AlignMgr __mgr(__bytes, __alignment); 433 // Assume _M_alloc returns 1-byte aligned memory, so allocate enough 434 // space to fit a block of the right size and alignment, plus some 435 // extra bytes to store a token for retrieving the original pointer. 436 return __mgr._M_adjust(_M_alloc.allocate(__mgr._M_alloc_size())); 437 } 438 439 virtual void 440 do_deallocate(void* __ptr, size_t __bytes, size_t __alignment) noexcept 441 override 442 { 443#if ! ((defined __sun__ || defined __VXWORKS__) && defined __i386__) 444 if (__alignment == alignof(max_align_t)) 445 return (void) _M_deallocate<alignof(max_align_t)>(__ptr, __bytes); 446#endif 447 switch (__alignment) 448 { 449 case 1: 450 return (void) _M_alloc.deallocate((char*)__ptr, __bytes); 451 case 2: 452 return (void) _M_deallocate<2>(__ptr, __bytes); 453 case 4: 454 return (void) _M_deallocate<4>(__ptr, __bytes); 455 case 8: 456 return (void) _M_deallocate<8>(__ptr, __bytes); 457 } 458 const _AlignMgr __mgr(__bytes, __alignment); 459 // Use the stored token to retrieve the original pointer. 460 _M_alloc.deallocate(__mgr._M_unadjust((char*)__ptr), 461 __mgr._M_alloc_size()); 462 } 463 464 virtual bool 465 do_is_equal(const memory_resource& __other) const noexcept override 466 { 467 if (auto __p = dynamic_cast<const __resource_adaptor_imp*>(&__other)) 468 return _M_alloc == __p->_M_alloc; 469 return false; 470 } 471 472 private: 473 template<size_t _Num> 474 struct _Aligned_type { alignas(_Num) char __c[_Num]; }; 475 476 // Rebind the allocator to the specified type and use it to allocate. 477 template<size_t _Num, typename _Tp = _Aligned_type<_Num>> 478 void* 479 _M_allocate(size_t __bytes) 480 { 481 typename allocator_traits<_Alloc>::template 482 rebind_alloc<_Tp> __a2(_M_alloc); 483 const size_t __n = (__bytes + _Num - 1) / _Num; 484 return __a2.allocate(__n); 485 } 486 487 // Rebind the allocator to the specified type and use it to deallocate. 488 template<size_t _Num, typename _Tp = _Aligned_type<_Num>> 489 void 490 _M_deallocate(void* __ptr, size_t __bytes) noexcept 491 { 492 typename allocator_traits<_Alloc>::template 493 rebind_alloc<_Tp> __a2(_M_alloc); 494 const size_t __n = (__bytes + _Num - 1) / _Num; 495 __a2.deallocate((_Tp*)__ptr, __n); 496 } 497 498 _Alloc _M_alloc{}; 499 }; 500 501 // Global memory resources 502 503 inline memory_resource* 504 new_delete_resource() noexcept 505 { 506 using type = resource_adaptor<__gnu_cxx::new_allocator<char>>; 507 alignas(type) static unsigned char __buf[sizeof(type)]; 508 static type* __r = new(__buf) type; 509 return __r; 510 } 511 512 inline memory_resource* 513 null_memory_resource() noexcept 514 { 515 class type final : public memory_resource 516 { 517 void* 518 do_allocate(size_t, size_t) override 519 { std::__throw_bad_alloc(); } 520 521 void 522 do_deallocate(void*, size_t, size_t) noexcept override 523 { } 524 525 bool 526 do_is_equal(const memory_resource& __other) const noexcept override 527 { return this == &__other; } 528 }; 529 530 alignas(type) static unsigned char __buf[sizeof(type)]; 531 static type* __r = new(__buf) type; 532 return __r; 533 } 534 535 // The default memory resource 536 537 /// @cond undocumented 538 inline std::atomic<memory_resource*>& 539 __get_default_resource() 540 { 541 using type = atomic<memory_resource*>; 542 alignas(type) static unsigned char __buf[sizeof(type)]; 543 static type* __r = new(__buf) type(new_delete_resource()); 544 return *__r; 545 } 546 /// @endcond 547 548 /// Get the current default resource. 549 inline memory_resource* 550 get_default_resource() noexcept 551 { return __get_default_resource().load(); } 552 553 /// Change the default resource and return the previous one. 554 inline memory_resource* 555 set_default_resource(memory_resource* __r) noexcept 556 { 557 if (__r == nullptr) 558 __r = new_delete_resource(); 559 return __get_default_resource().exchange(__r); 560 } 561 562} // namespace pmr 563} // namespace fundamentals_v2 564} // namespace experimental 565 566_GLIBCXX_END_NAMESPACE_VERSION 567} // namespace std 568#endif // C++14 569#endif // _GLIBCXX_EXPERIMENTAL_MEMORY_RESOURCE 570