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