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_MEMORY 11#define _LIBCPP_MEMORY 12 13/* 14 memory synopsis 15 16namespace std 17{ 18 19struct allocator_arg_t { }; 20inline constexpr allocator_arg_t allocator_arg = allocator_arg_t(); 21 22template <class T, class Alloc> struct uses_allocator; 23 24template <class Ptr> 25struct pointer_traits 26{ 27 typedef Ptr pointer; 28 typedef <details> element_type; 29 typedef <details> difference_type; 30 31 template <class U> using rebind = <details>; 32 33 static pointer pointer_to(<details>); 34}; 35 36template <class T> 37struct pointer_traits<T*> 38{ 39 typedef T* pointer; 40 typedef T element_type; 41 typedef ptrdiff_t difference_type; 42 43 template <class U> using rebind = U*; 44 45 static pointer pointer_to(<details>) noexcept; // constexpr in C++20 46}; 47 48template <class T> constexpr T* to_address(T* p) noexcept; // C++20 49template <class Ptr> constexpr auto to_address(const Ptr& p) noexcept; // C++20 50 51template <class Alloc> 52struct allocator_traits 53{ 54 typedef Alloc allocator_type; 55 typedef typename allocator_type::value_type 56 value_type; 57 58 typedef Alloc::pointer | value_type* pointer; 59 typedef Alloc::const_pointer 60 | pointer_traits<pointer>::rebind<const value_type> 61 const_pointer; 62 typedef Alloc::void_pointer 63 | pointer_traits<pointer>::rebind<void> 64 void_pointer; 65 typedef Alloc::const_void_pointer 66 | pointer_traits<pointer>::rebind<const void> 67 const_void_pointer; 68 typedef Alloc::difference_type 69 | pointer_traits<pointer>::difference_type 70 difference_type; 71 typedef Alloc::size_type 72 | make_unsigned<difference_type>::type 73 size_type; 74 typedef Alloc::propagate_on_container_copy_assignment 75 | false_type propagate_on_container_copy_assignment; 76 typedef Alloc::propagate_on_container_move_assignment 77 | false_type propagate_on_container_move_assignment; 78 typedef Alloc::propagate_on_container_swap 79 | false_type propagate_on_container_swap; 80 typedef Alloc::is_always_equal 81 | is_empty is_always_equal; 82 83 template <class T> using rebind_alloc = Alloc::rebind<T>::other | Alloc<T, Args...>; 84 template <class T> using rebind_traits = allocator_traits<rebind_alloc<T>>; 85 86 static pointer allocate(allocator_type& a, size_type n); // constexpr and [[nodiscard]] in C++20 87 static pointer allocate(allocator_type& a, size_type n, const_void_pointer hint); // constexpr and [[nodiscard]] in C++20 88 89 static void deallocate(allocator_type& a, pointer p, size_type n) noexcept; // constexpr in C++20 90 91 template <class T, class... Args> 92 static void construct(allocator_type& a, T* p, Args&&... args); // constexpr in C++20 93 94 template <class T> 95 static void destroy(allocator_type& a, T* p); // constexpr in C++20 96 97 static size_type max_size(const allocator_type& a); // noexcept in C++14, constexpr in C++20 98 static allocator_type select_on_container_copy_construction(const allocator_type& a); // constexpr in C++20 99}; 100 101template <> 102class allocator<void> // removed in C++20 103{ 104public: 105 typedef void* pointer; 106 typedef const void* const_pointer; 107 typedef void value_type; 108 109 template <class _Up> struct rebind {typedef allocator<_Up> other;}; 110}; 111 112template <class T> 113class allocator 114{ 115public: 116 typedef size_t size_type; 117 typedef ptrdiff_t difference_type; 118 typedef T* pointer; // deprecated in C++17, removed in C++20 119 typedef const T* const_pointer; // deprecated in C++17, removed in C++20 120 typedef typename add_lvalue_reference<T>::type 121 reference; // deprecated in C++17, removed in C++20 122 typedef typename add_lvalue_reference<const T>::type 123 const_reference; // deprecated in C++17, removed in C++20 124 125 typedef T value_type; 126 127 template <class U> struct rebind {typedef allocator<U> other;}; // deprecated in C++17, removed in C++20 128 129 typedef true_type propagate_on_container_move_assignment; 130 typedef true_type is_always_equal; 131 132 constexpr allocator() noexcept; // constexpr in C++20 133 constexpr allocator(const allocator&) noexcept; // constexpr in C++20 134 template <class U> 135 constexpr allocator(const allocator<U>&) noexcept; // constexpr in C++20 136 ~allocator(); // constexpr in C++20 137 pointer address(reference x) const noexcept; // deprecated in C++17, removed in C++20 138 const_pointer address(const_reference x) const noexcept; // deprecated in C++17, removed in C++20 139 T* allocate(size_t n, const void* hint); // deprecated in C++17, removed in C++20 140 T* allocate(size_t n); // constexpr in C++20 141 void deallocate(T* p, size_t n) noexcept; // constexpr in C++20 142 size_type max_size() const noexcept; // deprecated in C++17, removed in C++20 143 template<class U, class... Args> 144 void construct(U* p, Args&&... args); // deprecated in C++17, removed in C++20 145 template <class U> 146 void destroy(U* p); // deprecated in C++17, removed in C++20 147}; 148 149template <class T, class U> 150bool operator==(const allocator<T>&, const allocator<U>&) noexcept; // constexpr in C++20 151 152template <class T, class U> 153bool operator!=(const allocator<T>&, const allocator<U>&) noexcept; // constexpr in C++20 154 155template <class OutputIterator, class T> 156class raw_storage_iterator // deprecated in C++17, removed in C++20 157 : public iterator<output_iterator_tag, void, void, void, void> // until C++17 158{ 159public: 160 typedef output_iterator_tag iterator_category; 161 typedef void value_type; 162 typedef void difference_type; // until C++20 163 typedef ptrdiff_t difference_type; // since C++20 164 typedef void pointer; 165 typedef void reference; 166 167 explicit raw_storage_iterator(OutputIterator x); 168 raw_storage_iterator& operator*(); 169 raw_storage_iterator& operator=(const T& element); 170 raw_storage_iterator& operator++(); 171 raw_storage_iterator operator++(int); 172}; 173 174template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept; 175template <class T> void return_temporary_buffer(T* p) noexcept; 176 177template <class T> T* addressof(T& r) noexcept; 178template <class T> T* addressof(const T&& r) noexcept = delete; 179 180template <class InputIterator, class ForwardIterator> 181ForwardIterator 182uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result); 183 184template <class InputIterator, class Size, class ForwardIterator> 185ForwardIterator 186uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result); 187 188template <class ForwardIterator, class T> 189void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x); 190 191template <class ForwardIterator, class Size, class T> 192ForwardIterator 193uninitialized_fill_n(ForwardIterator first, Size n, const T& x); 194 195template <class T, class ...Args> 196constexpr T* construct_at(T* location, Args&& ...args); // since C++20 197 198template <class T> 199void destroy_at(T* location); // constexpr in C++20 200 201template <class ForwardIterator> 202void destroy(ForwardIterator first, ForwardIterator last); // constexpr in C++20 203 204template <class ForwardIterator, class Size> 205ForwardIterator destroy_n(ForwardIterator first, Size n); // constexpr in C++20 206 207template <class InputIterator, class ForwardIterator> 208 ForwardIterator uninitialized_move(InputIterator first, InputIterator last, ForwardIterator result); 209 210template <class InputIterator, class Size, class ForwardIterator> 211 pair<InputIterator,ForwardIterator> uninitialized_move_n(InputIterator first, Size n, ForwardIterator result); 212 213template <class ForwardIterator> 214 void uninitialized_value_construct(ForwardIterator first, ForwardIterator last); 215 216template <class ForwardIterator, class Size> 217 ForwardIterator uninitialized_value_construct_n(ForwardIterator first, Size n); 218 219template <class ForwardIterator> 220 void uninitialized_default_construct(ForwardIterator first, ForwardIterator last); 221 222template <nothrow-forward-iterator ForwardIterator, nothrow-sentinel-for<ForwardIterator> Sentinel> 223 requires default_initializable<iter_value_t<ForwardIterator>> 224 ForwardIterator ranges::uninitialized_default_construct(ForwardIterator first, Sentinel last); // since C++20 225 226template <nothrow-forward-range ForwardRange> 227 requires default_initializable<range_value_t<ForwardRange>> 228 borrowed_iterator_t<ForwardRange> ranges::uninitialized_default_construct(ForwardRange&& r); // since C++20 229 230template <class ForwardIterator, class Size> 231 ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Size n); 232 233template <nothrow-forward-iterator ForwardIterator> 234 requires default_initializable<iter_value_t<ForwardIterator>> 235 ForwardIterator ranges::uninitialized_default_construct_n(ForwardIterator first, iter_difference_t<ForwardIterator> n); // since C++20 236 237template <class Y> struct auto_ptr_ref {}; // deprecated in C++11, removed in C++17 238 239template<class X> 240class auto_ptr // deprecated in C++11, removed in C++17 241{ 242public: 243 typedef X element_type; 244 245 explicit auto_ptr(X* p =0) throw(); 246 auto_ptr(auto_ptr&) throw(); 247 template<class Y> auto_ptr(auto_ptr<Y>&) throw(); 248 auto_ptr& operator=(auto_ptr&) throw(); 249 template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw(); 250 auto_ptr& operator=(auto_ptr_ref<X> r) throw(); 251 ~auto_ptr() throw(); 252 253 typename add_lvalue_reference<X>::type operator*() const throw(); 254 X* operator->() const throw(); 255 X* get() const throw(); 256 X* release() throw(); 257 void reset(X* p =0) throw(); 258 259 auto_ptr(auto_ptr_ref<X>) throw(); 260 template<class Y> operator auto_ptr_ref<Y>() throw(); 261 template<class Y> operator auto_ptr<Y>() throw(); 262}; 263 264template <class T> 265struct default_delete 266{ 267 constexpr default_delete() noexcept = default; 268 template <class U> default_delete(const default_delete<U>&) noexcept; 269 270 void operator()(T*) const noexcept; 271}; 272 273template <class T> 274struct default_delete<T[]> 275{ 276 constexpr default_delete() noexcept = default; 277 void operator()(T*) const noexcept; 278 template <class U> void operator()(U*) const = delete; 279}; 280 281template <class T, class D = default_delete<T>> 282class unique_ptr 283{ 284public: 285 typedef see below pointer; 286 typedef T element_type; 287 typedef D deleter_type; 288 289 // constructors 290 constexpr unique_ptr() noexcept; 291 explicit unique_ptr(pointer p) noexcept; 292 unique_ptr(pointer p, see below d1) noexcept; 293 unique_ptr(pointer p, see below d2) noexcept; 294 unique_ptr(unique_ptr&& u) noexcept; 295 unique_ptr(nullptr_t) noexcept : unique_ptr() { } 296 template <class U, class E> 297 unique_ptr(unique_ptr<U, E>&& u) noexcept; 298 template <class U> 299 unique_ptr(auto_ptr<U>&& u) noexcept; // removed in C++17 300 301 // destructor 302 ~unique_ptr(); 303 304 // assignment 305 unique_ptr& operator=(unique_ptr&& u) noexcept; 306 template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept; 307 unique_ptr& operator=(nullptr_t) noexcept; 308 309 // observers 310 typename add_lvalue_reference<T>::type operator*() const; 311 pointer operator->() const noexcept; 312 pointer get() const noexcept; 313 deleter_type& get_deleter() noexcept; 314 const deleter_type& get_deleter() const noexcept; 315 explicit operator bool() const noexcept; 316 317 // modifiers 318 pointer release() noexcept; 319 void reset(pointer p = pointer()) noexcept; 320 void swap(unique_ptr& u) noexcept; 321}; 322 323template <class T, class D> 324class unique_ptr<T[], D> 325{ 326public: 327 typedef implementation-defined pointer; 328 typedef T element_type; 329 typedef D deleter_type; 330 331 // constructors 332 constexpr unique_ptr() noexcept; 333 explicit unique_ptr(pointer p) noexcept; 334 unique_ptr(pointer p, see below d) noexcept; 335 unique_ptr(pointer p, see below d) noexcept; 336 unique_ptr(unique_ptr&& u) noexcept; 337 unique_ptr(nullptr_t) noexcept : unique_ptr() { } 338 339 // destructor 340 ~unique_ptr(); 341 342 // assignment 343 unique_ptr& operator=(unique_ptr&& u) noexcept; 344 unique_ptr& operator=(nullptr_t) noexcept; 345 346 // observers 347 T& operator[](size_t i) const; 348 pointer get() const noexcept; 349 deleter_type& get_deleter() noexcept; 350 const deleter_type& get_deleter() const noexcept; 351 explicit operator bool() const noexcept; 352 353 // modifiers 354 pointer release() noexcept; 355 void reset(pointer p = pointer()) noexcept; 356 void reset(nullptr_t) noexcept; 357 template <class U> void reset(U) = delete; 358 void swap(unique_ptr& u) noexcept; 359}; 360 361template <class T, class D> 362 void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept; 363 364template <class T1, class D1, class T2, class D2> 365 bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y); 366template <class T1, class D1, class T2, class D2> 367 bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y); 368template <class T1, class D1, class T2, class D2> 369 bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y); 370template <class T1, class D1, class T2, class D2> 371 bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y); 372template <class T1, class D1, class T2, class D2> 373 bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y); 374template <class T1, class D1, class T2, class D2> 375 bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y); 376 377template <class T, class D> 378 bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept; 379template <class T, class D> 380 bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept; 381template <class T, class D> 382 bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept; 383template <class T, class D> 384 bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept; 385 386template <class T, class D> 387 bool operator<(const unique_ptr<T, D>& x, nullptr_t); 388template <class T, class D> 389 bool operator<(nullptr_t, const unique_ptr<T, D>& y); 390template <class T, class D> 391 bool operator<=(const unique_ptr<T, D>& x, nullptr_t); 392template <class T, class D> 393 bool operator<=(nullptr_t, const unique_ptr<T, D>& y); 394template <class T, class D> 395 bool operator>(const unique_ptr<T, D>& x, nullptr_t); 396template <class T, class D> 397 bool operator>(nullptr_t, const unique_ptr<T, D>& y); 398template <class T, class D> 399 bool operator>=(const unique_ptr<T, D>& x, nullptr_t); 400template <class T, class D> 401 bool operator>=(nullptr_t, const unique_ptr<T, D>& y); 402 403class bad_weak_ptr 404 : public std::exception 405{ 406 bad_weak_ptr() noexcept; 407}; 408 409template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args); // C++14 410template<class T> unique_ptr<T> make_unique(size_t n); // C++14 411template<class T, class... Args> unspecified make_unique(Args&&...) = delete; // C++14, T == U[N] 412 413template<class E, class T, class Y, class D> 414 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, unique_ptr<Y, D> const& p); 415 416template<class T> 417class shared_ptr 418{ 419public: 420 typedef T element_type; // until C++17 421 typedef remove_extent_t<T> element_type; // since C++17 422 typedef weak_ptr<T> weak_type; // C++17 423 424 // constructors: 425 constexpr shared_ptr() noexcept; 426 template<class Y> explicit shared_ptr(Y* p); 427 template<class Y, class D> shared_ptr(Y* p, D d); 428 template<class Y, class D, class A> shared_ptr(Y* p, D d, A a); 429 template <class D> shared_ptr(nullptr_t p, D d); 430 template <class D, class A> shared_ptr(nullptr_t p, D d, A a); 431 template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept; 432 shared_ptr(const shared_ptr& r) noexcept; 433 template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept; 434 shared_ptr(shared_ptr&& r) noexcept; 435 template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept; 436 template<class Y> explicit shared_ptr(const weak_ptr<Y>& r); 437 template<class Y> shared_ptr(auto_ptr<Y>&& r); // removed in C++17 438 template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r); 439 shared_ptr(nullptr_t) : shared_ptr() { } 440 441 // destructor: 442 ~shared_ptr(); 443 444 // assignment: 445 shared_ptr& operator=(const shared_ptr& r) noexcept; 446 template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept; 447 shared_ptr& operator=(shared_ptr&& r) noexcept; 448 template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r); 449 template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r); // removed in C++17 450 template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r); 451 452 // modifiers: 453 void swap(shared_ptr& r) noexcept; 454 void reset() noexcept; 455 template<class Y> void reset(Y* p); 456 template<class Y, class D> void reset(Y* p, D d); 457 template<class Y, class D, class A> void reset(Y* p, D d, A a); 458 459 // observers: 460 T* get() const noexcept; 461 T& operator*() const noexcept; 462 T* operator->() const noexcept; 463 long use_count() const noexcept; 464 bool unique() const noexcept; 465 explicit operator bool() const noexcept; 466 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept; 467 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept; 468}; 469 470template<class T> 471shared_ptr(weak_ptr<T>) -> shared_ptr<T>; 472template<class T, class D> 473shared_ptr(unique_ptr<T, D>) -> shared_ptr<T>; 474 475// shared_ptr comparisons: 476template<class T, class U> 477 bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept; 478template<class T, class U> 479 bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept; 480template<class T, class U> 481 bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept; 482template<class T, class U> 483 bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept; 484template<class T, class U> 485 bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept; 486template<class T, class U> 487 bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept; 488 489template <class T> 490 bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept; 491template <class T> 492 bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept; 493template <class T> 494 bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept; 495template <class T> 496 bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept; 497template <class T> 498 bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept; 499template <class T> 500bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept; 501template <class T> 502 bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept; 503template <class T> 504 bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept; 505template <class T> 506 bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept; 507template <class T> 508 bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept; 509template <class T> 510 bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept; 511template <class T> 512 bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept; 513 514// shared_ptr specialized algorithms: 515template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept; 516 517// shared_ptr casts: 518template<class T, class U> 519 shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept; 520template<class T, class U> 521 shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept; 522template<class T, class U> 523 shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept; 524 525// shared_ptr I/O: 526template<class E, class T, class Y> 527 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p); 528 529// shared_ptr get_deleter: 530template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept; 531 532template<class T, class... Args> 533 shared_ptr<T> make_shared(Args&&... args); 534template<class T, class A, class... Args> 535 shared_ptr<T> allocate_shared(const A& a, Args&&... args); 536 537template<class T> 538class weak_ptr 539{ 540public: 541 typedef T element_type; // until C++17 542 typedef remove_extent_t<T> element_type; // since C++17 543 544 // constructors 545 constexpr weak_ptr() noexcept; 546 template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept; 547 weak_ptr(weak_ptr const& r) noexcept; 548 template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept; 549 weak_ptr(weak_ptr&& r) noexcept; // C++14 550 template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14 551 552 // destructor 553 ~weak_ptr(); 554 555 // assignment 556 weak_ptr& operator=(weak_ptr const& r) noexcept; 557 template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept; 558 template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept; 559 weak_ptr& operator=(weak_ptr&& r) noexcept; // C++14 560 template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14 561 562 // modifiers 563 void swap(weak_ptr& r) noexcept; 564 void reset() noexcept; 565 566 // observers 567 long use_count() const noexcept; 568 bool expired() const noexcept; 569 shared_ptr<T> lock() const noexcept; 570 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept; 571 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept; 572}; 573 574template<class T> 575weak_ptr(shared_ptr<T>) -> weak_ptr<T>; 576 577// weak_ptr specialized algorithms: 578template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept; 579 580// class owner_less: 581template<class T> struct owner_less; 582 583template<class T> 584struct owner_less<shared_ptr<T> > 585 : binary_function<shared_ptr<T>, shared_ptr<T>, bool> 586{ 587 typedef bool result_type; 588 bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const noexcept; 589 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept; 590 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept; 591}; 592 593template<class T> 594struct owner_less<weak_ptr<T> > 595 : binary_function<weak_ptr<T>, weak_ptr<T>, bool> 596{ 597 typedef bool result_type; 598 bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const noexcept; 599 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept; 600 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept; 601}; 602 603template <> // Added in C++14 604struct owner_less<void> 605{ 606 template <class _Tp, class _Up> 607 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept; 608 template <class _Tp, class _Up> 609 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept; 610 template <class _Tp, class _Up> 611 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept; 612 template <class _Tp, class _Up> 613 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept; 614 615 typedef void is_transparent; 616}; 617 618template<class T> 619class enable_shared_from_this 620{ 621protected: 622 constexpr enable_shared_from_this() noexcept; 623 enable_shared_from_this(enable_shared_from_this const&) noexcept; 624 enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept; 625 ~enable_shared_from_this(); 626public: 627 shared_ptr<T> shared_from_this(); 628 shared_ptr<T const> shared_from_this() const; 629}; 630 631template<class T> 632 bool atomic_is_lock_free(const shared_ptr<T>* p); 633template<class T> 634 shared_ptr<T> atomic_load(const shared_ptr<T>* p); 635template<class T> 636 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo); 637template<class T> 638 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r); 639template<class T> 640 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo); 641template<class T> 642 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r); 643template<class T> 644 shared_ptr<T> 645 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo); 646template<class T> 647 bool 648 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w); 649template<class T> 650 bool 651 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w); 652template<class T> 653 bool 654 atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v, 655 shared_ptr<T> w, memory_order success, 656 memory_order failure); 657template<class T> 658 bool 659 atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v, 660 shared_ptr<T> w, memory_order success, 661 memory_order failure); 662// Hash support 663template <class T> struct hash; 664template <class T, class D> struct hash<unique_ptr<T, D> >; 665template <class T> struct hash<shared_ptr<T> >; 666 667template <class T, class Alloc> 668 inline constexpr bool uses_allocator_v = uses_allocator<T, Alloc>::value; 669 670void* align(size_t alignment, size_t size, void*& ptr, size_t& space); 671 672} // std 673 674*/ 675 676#include <__config> 677#include <__functional_base> 678#include <__memory/addressof.h> 679#include <__memory/allocation_guard.h> 680#include <__memory/allocator.h> 681#include <__memory/allocator_arg_t.h> 682#include <__memory/allocator_traits.h> 683#include <__memory/compressed_pair.h> 684#include <__memory/concepts.h> 685#include <__memory/construct_at.h> 686#include <__memory/pointer_traits.h> 687#include <__memory/ranges_uninitialized_algorithms.h> 688#include <__memory/raw_storage_iterator.h> 689#include <__memory/shared_ptr.h> 690#include <__memory/temporary_buffer.h> 691#include <__memory/uninitialized_algorithms.h> 692#include <__memory/unique_ptr.h> 693#include <__memory/uses_allocator.h> 694#include <compare> 695#include <cstddef> 696#include <cstdint> 697#include <cstring> 698#include <iosfwd> 699#include <iterator> 700#include <new> 701#include <stdexcept> 702#include <tuple> 703#include <type_traits> 704#include <typeinfo> 705#include <utility> 706#include <version> 707 708#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR) 709# include <__memory/auto_ptr.h> 710#endif 711 712#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 713#pragma GCC system_header 714#endif 715 716_LIBCPP_BEGIN_NAMESPACE_STD 717 718template <class _Alloc, class _Ptr> 719_LIBCPP_INLINE_VISIBILITY 720void __construct_forward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2) { 721 static_assert(__is_cpp17_move_insertable<_Alloc>::value, 722 "The specified type does not meet the requirements of Cpp17MoveInsertable"); 723 typedef allocator_traits<_Alloc> _Traits; 724 for (; __begin1 != __end1; ++__begin1, (void)++__begin2) { 725 _Traits::construct(__a, _VSTD::__to_address(__begin2), 726#ifdef _LIBCPP_NO_EXCEPTIONS 727 _VSTD::move(*__begin1) 728#else 729 _VSTD::move_if_noexcept(*__begin1) 730#endif 731 ); 732 } 733} 734 735template <class _Alloc, class _Tp, typename enable_if< 736 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) && 737 is_trivially_move_constructible<_Tp>::value 738>::type> 739_LIBCPP_INLINE_VISIBILITY 740void __construct_forward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2) { 741 ptrdiff_t _Np = __end1 - __begin1; 742 if (_Np > 0) { 743 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp)); 744 __begin2 += _Np; 745 } 746} 747 748template <class _Alloc, class _Iter, class _Ptr> 749_LIBCPP_INLINE_VISIBILITY 750void __construct_range_forward(_Alloc& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2) { 751 typedef allocator_traits<_Alloc> _Traits; 752 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2) { 753 _Traits::construct(__a, _VSTD::__to_address(__begin2), *__begin1); 754 } 755} 756 757template <class _Alloc, class _Source, class _Dest, 758 class _RawSource = typename remove_const<_Source>::type, 759 class _RawDest = typename remove_const<_Dest>::type, 760 class = 761 typename enable_if< 762 is_trivially_copy_constructible<_Dest>::value && 763 is_same<_RawSource, _RawDest>::value && 764 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Dest*, _Source&>::value) 765 >::type> 766_LIBCPP_INLINE_VISIBILITY 767void __construct_range_forward(_Alloc&, _Source* __begin1, _Source* __end1, _Dest*& __begin2) { 768 ptrdiff_t _Np = __end1 - __begin1; 769 if (_Np > 0) { 770 _VSTD::memcpy(const_cast<_RawDest*>(__begin2), __begin1, _Np * sizeof(_Dest)); 771 __begin2 += _Np; 772 } 773} 774 775template <class _Alloc, class _Ptr> 776_LIBCPP_INLINE_VISIBILITY 777void __construct_backward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2) { 778 static_assert(__is_cpp17_move_insertable<_Alloc>::value, 779 "The specified type does not meet the requirements of Cpp17MoveInsertable"); 780 typedef allocator_traits<_Alloc> _Traits; 781 while (__end1 != __begin1) { 782 _Traits::construct(__a, _VSTD::__to_address(__end2 - 1), 783#ifdef _LIBCPP_NO_EXCEPTIONS 784 _VSTD::move(*--__end1) 785#else 786 _VSTD::move_if_noexcept(*--__end1) 787#endif 788 ); 789 --__end2; 790 } 791} 792 793template <class _Alloc, class _Tp, class = typename enable_if< 794 (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) && 795 is_trivially_move_constructible<_Tp>::value 796>::type> 797_LIBCPP_INLINE_VISIBILITY 798void __construct_backward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2) { 799 ptrdiff_t _Np = __end1 - __begin1; 800 __end2 -= _Np; 801 if (_Np > 0) 802 _VSTD::memcpy(static_cast<void*>(__end2), static_cast<void const*>(__begin1), _Np * sizeof(_Tp)); 803} 804 805struct __destruct_n 806{ 807private: 808 size_t __size_; 809 810 template <class _Tp> 811 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT 812 {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();} 813 814 template <class _Tp> 815 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT 816 {} 817 818 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT 819 {++__size_;} 820 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT 821 {} 822 823 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT 824 {__size_ = __s;} 825 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT 826 {} 827public: 828 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT 829 : __size_(__s) {} 830 831 template <class _Tp> 832 _LIBCPP_INLINE_VISIBILITY void __incr() _NOEXCEPT 833 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());} 834 835 template <class _Tp> 836 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT 837 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());} 838 839 template <class _Tp> 840 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT 841 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());} 842}; 843 844_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space); 845 846// --- Helper for container swap -- 847template <typename _Alloc> 848_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 849void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type) 850#if _LIBCPP_STD_VER > 11 851 _NOEXCEPT 852#else 853 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value) 854#endif 855{ 856 using _VSTD::swap; 857 swap(__a1, __a2); 858} 859 860template <typename _Alloc> 861inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 862void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {} 863 864template <typename _Alloc> 865inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 866void __swap_allocator(_Alloc & __a1, _Alloc & __a2) 867#if _LIBCPP_STD_VER > 11 868 _NOEXCEPT 869#else 870 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value) 871#endif 872{ 873 _VSTD::__swap_allocator(__a1, __a2, 874 integral_constant<bool, allocator_traits<_Alloc>::propagate_on_container_swap::value>()); 875} 876 877template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> > 878struct __noexcept_move_assign_container : public integral_constant<bool, 879 _Traits::propagate_on_container_move_assignment::value 880#if _LIBCPP_STD_VER > 14 881 || _Traits::is_always_equal::value 882#else 883 && is_nothrow_move_assignable<_Alloc>::value 884#endif 885 > {}; 886 887 888template <class _Tp, class _Alloc> 889struct __temp_value { 890 typedef allocator_traits<_Alloc> _Traits; 891 892 typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v; 893 _Alloc &__a; 894 895 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); } 896 _Tp & get() { return *__addr(); } 897 898 template<class... _Args> 899 _LIBCPP_NO_CFI 900 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) { 901 _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)), 902 _VSTD::forward<_Args>(__args)...); 903 } 904 905 ~__temp_value() { _Traits::destroy(__a, __addr()); } 906 }; 907 908template<typename _Alloc, typename = void, typename = void> 909struct __is_allocator : false_type {}; 910 911template<typename _Alloc> 912struct __is_allocator<_Alloc, 913 typename __void_t<typename _Alloc::value_type>::type, 914 typename __void_t<decltype(declval<_Alloc&>().allocate(size_t(0)))>::type 915 > 916 : true_type {}; 917 918// __builtin_new_allocator -- A non-templated helper for allocating and 919// deallocating memory using __builtin_operator_new and 920// __builtin_operator_delete. It should be used in preference to 921// `std::allocator<T>` to avoid additional instantiations. 922struct __builtin_new_allocator { 923 struct __builtin_new_deleter { 924 typedef void* pointer_type; 925 926 _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align) 927 : __size_(__size), __align_(__align) {} 928 929 void operator()(void* p) const _NOEXCEPT { 930 _VSTD::__libcpp_deallocate(p, __size_, __align_); 931 } 932 933 private: 934 size_t __size_; 935 size_t __align_; 936 }; 937 938 typedef unique_ptr<void, __builtin_new_deleter> __holder_t; 939 940 static __holder_t __allocate_bytes(size_t __s, size_t __align) { 941 return __holder_t(_VSTD::__libcpp_allocate(__s, __align), 942 __builtin_new_deleter(__s, __align)); 943 } 944 945 static void __deallocate_bytes(void* __p, size_t __s, 946 size_t __align) _NOEXCEPT { 947 _VSTD::__libcpp_deallocate(__p, __s, __align); 948 } 949 950 template <class _Tp> 951 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE 952 static __holder_t __allocate_type(size_t __n) { 953 return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)); 954 } 955 956 template <class _Tp> 957 _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE 958 static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT { 959 __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)); 960 } 961}; 962 963 964_LIBCPP_END_NAMESPACE_STD 965 966#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17 967# include <__pstl_memory> 968#endif 969 970#endif // _LIBCPP_MEMORY 971