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