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