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