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_SHARED_PTR_H
11 #define _LIBCPP___MEMORY_SHARED_PTR_H
12 
13 #include <__availability>
14 #include <__config>
15 #include <__functional/binary_function.h>
16 #include <__functional/operations.h>
17 #include <__functional/reference_wrapper.h>
18 #include <__iterator/access.h>
19 #include <__memory/addressof.h>
20 #include <__memory/allocation_guard.h>
21 #include <__memory/allocator.h>
22 #include <__memory/allocator_traits.h>
23 #include <__memory/compressed_pair.h>
24 #include <__memory/construct_at.h>
25 #include <__memory/pointer_traits.h>
26 #include <__memory/uninitialized_algorithms.h>
27 #include <__memory/unique_ptr.h>
28 #include <__utility/forward.h>
29 #include <__utility/move.h>
30 #include <__utility/swap.h>
31 #include <cstddef>
32 #include <cstdlib> // abort
33 #include <iosfwd>
34 #include <stdexcept>
35 #include <type_traits>
36 #include <typeinfo>
37 #if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
38 #  include <atomic>
39 #endif
40 
41 #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
42 #   include <__memory/auto_ptr.h>
43 #endif
44 
45 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
46 #  pragma GCC system_header
47 #endif
48 
49 _LIBCPP_BEGIN_NAMESPACE_STD
50 
51 template <class _Alloc>
52 class __allocator_destructor
53 {
54     typedef _LIBCPP_NODEBUG allocator_traits<_Alloc> __alloc_traits;
55 public:
56     typedef _LIBCPP_NODEBUG typename __alloc_traits::pointer pointer;
57     typedef _LIBCPP_NODEBUG typename __alloc_traits::size_type size_type;
58 private:
59     _Alloc& __alloc_;
60     size_type __s_;
61 public:
62     _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
63              _NOEXCEPT
64         : __alloc_(__a), __s_(__s) {}
65     _LIBCPP_INLINE_VISIBILITY
66     void operator()(pointer __p) _NOEXCEPT
67         {__alloc_traits::deallocate(__alloc_, __p, __s_);}
68 };
69 
70 // NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
71 // should be sufficient for thread safety.
72 // See https://llvm.org/PR22803
73 #if defined(__clang__) && __has_builtin(__atomic_add_fetch)          \
74                        && defined(__ATOMIC_RELAXED)                  \
75                        && defined(__ATOMIC_ACQ_REL)
76 #   define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
77 #elif defined(_LIBCPP_COMPILER_GCC)
78 #   define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
79 #endif
80 
81 template <class _ValueType>
82 inline _LIBCPP_INLINE_VISIBILITY
83 _ValueType __libcpp_relaxed_load(_ValueType const* __value) {
84 #if !defined(_LIBCPP_HAS_NO_THREADS) && \
85     defined(__ATOMIC_RELAXED) &&        \
86     (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
87     return __atomic_load_n(__value, __ATOMIC_RELAXED);
88 #else
89     return *__value;
90 #endif
91 }
92 
93 template <class _ValueType>
94 inline _LIBCPP_INLINE_VISIBILITY
95 _ValueType __libcpp_acquire_load(_ValueType const* __value) {
96 #if !defined(_LIBCPP_HAS_NO_THREADS) && \
97     defined(__ATOMIC_ACQUIRE) &&        \
98     (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
99     return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
100 #else
101     return *__value;
102 #endif
103 }
104 
105 template <class _Tp>
106 inline _LIBCPP_INLINE_VISIBILITY _Tp
107 __libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
108 {
109 #if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
110     return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
111 #else
112     return __t += 1;
113 #endif
114 }
115 
116 template <class _Tp>
117 inline _LIBCPP_INLINE_VISIBILITY _Tp
118 __libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
119 {
120 #if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
121     return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
122 #else
123     return __t -= 1;
124 #endif
125 }
126 
127 class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
128     : public std::exception
129 {
130 public:
131     bad_weak_ptr() _NOEXCEPT = default;
132     bad_weak_ptr(const bad_weak_ptr&) _NOEXCEPT = default;
133     virtual ~bad_weak_ptr() _NOEXCEPT;
134     virtual const char* what() const  _NOEXCEPT;
135 };
136 
137 _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
138 void __throw_bad_weak_ptr()
139 {
140 #ifndef _LIBCPP_NO_EXCEPTIONS
141     throw bad_weak_ptr();
142 #else
143     _VSTD::abort();
144 #endif
145 }
146 
147 template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
148 
149 class _LIBCPP_TYPE_VIS __shared_count
150 {
151     __shared_count(const __shared_count&);
152     __shared_count& operator=(const __shared_count&);
153 
154 protected:
155     long __shared_owners_;
156     virtual ~__shared_count();
157 private:
158     virtual void __on_zero_shared() _NOEXCEPT = 0;
159 
160 public:
161     _LIBCPP_INLINE_VISIBILITY
162     explicit __shared_count(long __refs = 0) _NOEXCEPT
163         : __shared_owners_(__refs) {}
164 
165 #if defined(_LIBCPP_BUILDING_LIBRARY) && \
166     defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
167     void __add_shared() _NOEXCEPT;
168     bool __release_shared() _NOEXCEPT;
169 #else
170     _LIBCPP_INLINE_VISIBILITY
171     void __add_shared() _NOEXCEPT {
172       __libcpp_atomic_refcount_increment(__shared_owners_);
173     }
174     _LIBCPP_INLINE_VISIBILITY
175     bool __release_shared() _NOEXCEPT {
176       if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
177         __on_zero_shared();
178         return true;
179       }
180       return false;
181     }
182 #endif
183     _LIBCPP_INLINE_VISIBILITY
184     long use_count() const _NOEXCEPT {
185         return __libcpp_relaxed_load(&__shared_owners_) + 1;
186     }
187 };
188 
189 class _LIBCPP_TYPE_VIS __shared_weak_count
190     : private __shared_count
191 {
192     long __shared_weak_owners_;
193 
194 public:
195     _LIBCPP_INLINE_VISIBILITY
196     explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
197         : __shared_count(__refs),
198           __shared_weak_owners_(__refs) {}
199 protected:
200     virtual ~__shared_weak_count();
201 
202 public:
203 #if defined(_LIBCPP_BUILDING_LIBRARY) && \
204     defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
205     void __add_shared() _NOEXCEPT;
206     void __add_weak() _NOEXCEPT;
207     void __release_shared() _NOEXCEPT;
208 #else
209     _LIBCPP_INLINE_VISIBILITY
210     void __add_shared() _NOEXCEPT {
211       __shared_count::__add_shared();
212     }
213     _LIBCPP_INLINE_VISIBILITY
214     void __add_weak() _NOEXCEPT {
215       __libcpp_atomic_refcount_increment(__shared_weak_owners_);
216     }
217     _LIBCPP_INLINE_VISIBILITY
218     void __release_shared() _NOEXCEPT {
219       if (__shared_count::__release_shared())
220         __release_weak();
221     }
222 #endif
223     void __release_weak() _NOEXCEPT;
224     _LIBCPP_INLINE_VISIBILITY
225     long use_count() const _NOEXCEPT {return __shared_count::use_count();}
226     __shared_weak_count* lock() _NOEXCEPT;
227 
228     virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
229 private:
230     virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
231 };
232 
233 template <class _Tp, class _Dp, class _Alloc>
234 class __shared_ptr_pointer
235     : public __shared_weak_count
236 {
237     __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
238 public:
239     _LIBCPP_INLINE_VISIBILITY
240     __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
241         :  __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
242 
243 #ifndef _LIBCPP_NO_RTTI
244     virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
245 #endif
246 
247 private:
248     virtual void __on_zero_shared() _NOEXCEPT;
249     virtual void __on_zero_shared_weak() _NOEXCEPT;
250 };
251 
252 #ifndef _LIBCPP_NO_RTTI
253 
254 template <class _Tp, class _Dp, class _Alloc>
255 const void*
256 __shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
257 {
258     return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
259 }
260 
261 #endif // _LIBCPP_NO_RTTI
262 
263 template <class _Tp, class _Dp, class _Alloc>
264 void
265 __shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
266 {
267     __data_.first().second()(__data_.first().first());
268     __data_.first().second().~_Dp();
269 }
270 
271 template <class _Tp, class _Dp, class _Alloc>
272 void
273 __shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
274 {
275     typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
276     typedef allocator_traits<_Al> _ATraits;
277     typedef pointer_traits<typename _ATraits::pointer> _PTraits;
278 
279     _Al __a(__data_.second());
280     __data_.second().~_Alloc();
281     __a.deallocate(_PTraits::pointer_to(*this), 1);
282 }
283 
284 template <class _Tp, class _Alloc>
285 struct __shared_ptr_emplace
286     : __shared_weak_count
287 {
288     template<class ..._Args>
289     _LIBCPP_HIDE_FROM_ABI
290     explicit __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
291         : __storage_(_VSTD::move(__a))
292     {
293 #if _LIBCPP_STD_VER > 17
294         using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
295         _TpAlloc __tmp(*__get_alloc());
296         allocator_traits<_TpAlloc>::construct(__tmp, __get_elem(), _VSTD::forward<_Args>(__args)...);
297 #else
298         ::new ((void*)__get_elem()) _Tp(_VSTD::forward<_Args>(__args)...);
299 #endif
300     }
301 
302     _LIBCPP_HIDE_FROM_ABI
303     _Alloc* __get_alloc() _NOEXCEPT { return __storage_.__get_alloc(); }
304 
305     _LIBCPP_HIDE_FROM_ABI
306     _Tp* __get_elem() _NOEXCEPT { return __storage_.__get_elem(); }
307 
308 private:
309     virtual void __on_zero_shared() _NOEXCEPT {
310 #if _LIBCPP_STD_VER > 17
311         using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
312         _TpAlloc __tmp(*__get_alloc());
313         allocator_traits<_TpAlloc>::destroy(__tmp, __get_elem());
314 #else
315         __get_elem()->~_Tp();
316 #endif
317     }
318 
319     virtual void __on_zero_shared_weak() _NOEXCEPT {
320         using _ControlBlockAlloc = typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type;
321         using _ControlBlockPointer = typename allocator_traits<_ControlBlockAlloc>::pointer;
322         _ControlBlockAlloc __tmp(*__get_alloc());
323         __storage_.~_Storage();
324         allocator_traits<_ControlBlockAlloc>::deallocate(__tmp,
325             pointer_traits<_ControlBlockPointer>::pointer_to(*this), 1);
326     }
327 
328     // This class implements the control block for non-array shared pointers created
329     // through `std::allocate_shared` and `std::make_shared`.
330     //
331     // In previous versions of the library, we used a compressed pair to store
332     // both the _Alloc and the _Tp. This implies using EBO, which is incompatible
333     // with Allocator construction for _Tp. To allow implementing P0674 in C++20,
334     // we now use a properly aligned char buffer while making sure that we maintain
335     // the same layout that we had when we used a compressed pair.
336     using _CompressedPair = __compressed_pair<_Alloc, _Tp>;
337     struct _ALIGNAS_TYPE(_CompressedPair) _Storage {
338         char __blob_[sizeof(_CompressedPair)];
339 
340         _LIBCPP_HIDE_FROM_ABI explicit _Storage(_Alloc&& __a) {
341             ::new ((void*)__get_alloc()) _Alloc(_VSTD::move(__a));
342         }
343         _LIBCPP_HIDE_FROM_ABI ~_Storage() {
344             __get_alloc()->~_Alloc();
345         }
346         _Alloc* __get_alloc() _NOEXCEPT {
347             _CompressedPair *__as_pair = reinterpret_cast<_CompressedPair*>(__blob_);
348             typename _CompressedPair::_Base1* __first = _CompressedPair::__get_first_base(__as_pair);
349             _Alloc *__alloc = reinterpret_cast<_Alloc*>(__first);
350             return __alloc;
351         }
352         _LIBCPP_NO_CFI _Tp* __get_elem() _NOEXCEPT {
353             _CompressedPair *__as_pair = reinterpret_cast<_CompressedPair*>(__blob_);
354             typename _CompressedPair::_Base2* __second = _CompressedPair::__get_second_base(__as_pair);
355             _Tp *__elem = reinterpret_cast<_Tp*>(__second);
356             return __elem;
357         }
358     };
359 
360     static_assert(_LIBCPP_ALIGNOF(_Storage) == _LIBCPP_ALIGNOF(_CompressedPair), "");
361     static_assert(sizeof(_Storage) == sizeof(_CompressedPair), "");
362     _Storage __storage_;
363 };
364 
365 struct __shared_ptr_dummy_rebind_allocator_type;
366 template <>
367 class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
368 {
369 public:
370     template <class _Other>
371     struct rebind
372     {
373         typedef allocator<_Other> other;
374     };
375 };
376 
377 template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
378 
379 template<class _Tp, class _Up>
380 struct __compatible_with
381 #if _LIBCPP_STD_VER > 14
382     : is_convertible<remove_extent_t<_Tp>*, remove_extent_t<_Up>*> {};
383 #else
384     : is_convertible<_Tp*, _Up*> {};
385 #endif // _LIBCPP_STD_VER > 14
386 
387 template <class _Ptr, class = void>
388 struct __is_deletable : false_type { };
389 template <class _Ptr>
390 struct __is_deletable<_Ptr, decltype(delete declval<_Ptr>())> : true_type { };
391 
392 template <class _Ptr, class = void>
393 struct __is_array_deletable : false_type { };
394 template <class _Ptr>
395 struct __is_array_deletable<_Ptr, decltype(delete[] declval<_Ptr>())> : true_type { };
396 
397 template <class _Dp, class _Pt,
398     class = decltype(declval<_Dp>()(declval<_Pt>()))>
399 static true_type __well_formed_deleter_test(int);
400 
401 template <class, class>
402 static false_type __well_formed_deleter_test(...);
403 
404 template <class _Dp, class _Pt>
405 struct __well_formed_deleter : decltype(__well_formed_deleter_test<_Dp, _Pt>(0)) {};
406 
407 template<class _Dp, class _Tp, class _Yp>
408 struct __shared_ptr_deleter_ctor_reqs
409 {
410     static const bool value = __compatible_with<_Tp, _Yp>::value &&
411                               is_move_constructible<_Dp>::value &&
412                               __well_formed_deleter<_Dp, _Tp*>::value;
413 };
414 
415 #if defined(_LIBCPP_ABI_ENABLE_SHARED_PTR_TRIVIAL_ABI)
416 #  define _LIBCPP_SHARED_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
417 #else
418 #  define _LIBCPP_SHARED_PTR_TRIVIAL_ABI
419 #endif
420 
421 template<class _Tp>
422 class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr
423 {
424 public:
425 #if _LIBCPP_STD_VER > 14
426     typedef weak_ptr<_Tp> weak_type;
427     typedef remove_extent_t<_Tp> element_type;
428 #else
429     typedef _Tp element_type;
430 #endif
431 
432 private:
433     element_type*      __ptr_;
434     __shared_weak_count* __cntrl_;
435 
436 public:
437     _LIBCPP_HIDE_FROM_ABI
438     _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT
439         : __ptr_(nullptr),
440           __cntrl_(nullptr)
441     { }
442 
443     _LIBCPP_HIDE_FROM_ABI
444     _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT
445         : __ptr_(nullptr),
446           __cntrl_(nullptr)
447     { }
448 
449     template<class _Yp, class = __enable_if_t<
450         _And<
451             __compatible_with<_Yp, _Tp>
452             // In C++03 we get errors when trying to do SFINAE with the
453             // delete operator, so we always pretend that it's deletable.
454             // The same happens on GCC.
455 #if !defined(_LIBCPP_CXX03_LANG) && !defined(_LIBCPP_COMPILER_GCC)
456             , _If<is_array<_Tp>::value, __is_array_deletable<_Yp*>, __is_deletable<_Yp*> >
457 #endif
458         >::value
459     > >
460     explicit shared_ptr(_Yp* __p) : __ptr_(__p) {
461         unique_ptr<_Yp> __hold(__p);
462         typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
463         typedef __shared_ptr_pointer<_Yp*, __shared_ptr_default_delete<_Tp, _Yp>, _AllocT> _CntrlBlk;
464         __cntrl_ = new _CntrlBlk(__p, __shared_ptr_default_delete<_Tp, _Yp>(), _AllocT());
465         __hold.release();
466         __enable_weak_this(__p, __p);
467     }
468 
469     template<class _Yp, class _Dp, class = __enable_if_t<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value> >
470     _LIBCPP_HIDE_FROM_ABI
471     shared_ptr(_Yp* __p, _Dp __d)
472         : __ptr_(__p)
473     {
474 #ifndef _LIBCPP_NO_EXCEPTIONS
475         try
476         {
477 #endif // _LIBCPP_NO_EXCEPTIONS
478             typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
479             typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT> _CntrlBlk;
480 #ifndef _LIBCPP_CXX03_LANG
481             __cntrl_ = new _CntrlBlk(__p, _VSTD::move(__d), _AllocT());
482 #else
483             __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
484 #endif // not _LIBCPP_CXX03_LANG
485             __enable_weak_this(__p, __p);
486 #ifndef _LIBCPP_NO_EXCEPTIONS
487         }
488         catch (...)
489         {
490             __d(__p);
491             throw;
492         }
493 #endif // _LIBCPP_NO_EXCEPTIONS
494     }
495 
496     template<class _Yp, class _Dp, class _Alloc, class = __enable_if_t<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value> >
497     _LIBCPP_HIDE_FROM_ABI
498     shared_ptr(_Yp* __p, _Dp __d, _Alloc __a)
499         : __ptr_(__p)
500     {
501 #ifndef _LIBCPP_NO_EXCEPTIONS
502         try
503         {
504 #endif // _LIBCPP_NO_EXCEPTIONS
505             typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
506             typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
507             typedef __allocator_destructor<_A2> _D2;
508             _A2 __a2(__a);
509             unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
510             ::new ((void*)_VSTD::addressof(*__hold2.get()))
511 #ifndef _LIBCPP_CXX03_LANG
512                 _CntrlBlk(__p, _VSTD::move(__d), __a);
513 #else
514                 _CntrlBlk(__p, __d, __a);
515 #endif // not _LIBCPP_CXX03_LANG
516             __cntrl_ = _VSTD::addressof(*__hold2.release());
517             __enable_weak_this(__p, __p);
518 #ifndef _LIBCPP_NO_EXCEPTIONS
519         }
520         catch (...)
521         {
522             __d(__p);
523             throw;
524         }
525 #endif // _LIBCPP_NO_EXCEPTIONS
526     }
527 
528     template<class _Dp>
529     _LIBCPP_HIDE_FROM_ABI
530     shared_ptr(nullptr_t __p, _Dp __d)
531         : __ptr_(nullptr)
532     {
533 #ifndef _LIBCPP_NO_EXCEPTIONS
534         try
535         {
536 #endif // _LIBCPP_NO_EXCEPTIONS
537             typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
538             typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT> _CntrlBlk;
539 #ifndef _LIBCPP_CXX03_LANG
540             __cntrl_ = new _CntrlBlk(__p, _VSTD::move(__d), _AllocT());
541 #else
542             __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
543 #endif // not _LIBCPP_CXX03_LANG
544 #ifndef _LIBCPP_NO_EXCEPTIONS
545         }
546         catch (...)
547         {
548             __d(__p);
549             throw;
550         }
551 #endif // _LIBCPP_NO_EXCEPTIONS
552     }
553 
554     template<class _Dp, class _Alloc>
555     _LIBCPP_HIDE_FROM_ABI
556     shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
557         : __ptr_(nullptr)
558     {
559 #ifndef _LIBCPP_NO_EXCEPTIONS
560         try
561         {
562 #endif // _LIBCPP_NO_EXCEPTIONS
563             typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
564             typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
565             typedef __allocator_destructor<_A2> _D2;
566             _A2 __a2(__a);
567             unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
568             ::new ((void*)_VSTD::addressof(*__hold2.get()))
569 #ifndef _LIBCPP_CXX03_LANG
570                 _CntrlBlk(__p, _VSTD::move(__d), __a);
571 #else
572                 _CntrlBlk(__p, __d, __a);
573 #endif // not _LIBCPP_CXX03_LANG
574             __cntrl_ = _VSTD::addressof(*__hold2.release());
575 #ifndef _LIBCPP_NO_EXCEPTIONS
576         }
577         catch (...)
578         {
579             __d(__p);
580             throw;
581         }
582 #endif // _LIBCPP_NO_EXCEPTIONS
583     }
584 
585     template<class _Yp>
586     _LIBCPP_HIDE_FROM_ABI
587     shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
588         : __ptr_(__p),
589           __cntrl_(__r.__cntrl_)
590     {
591         if (__cntrl_)
592             __cntrl_->__add_shared();
593     }
594 
595     _LIBCPP_HIDE_FROM_ABI
596     shared_ptr(const shared_ptr& __r) _NOEXCEPT
597         : __ptr_(__r.__ptr_),
598           __cntrl_(__r.__cntrl_)
599     {
600         if (__cntrl_)
601             __cntrl_->__add_shared();
602     }
603 
604     template<class _Yp, class = __enable_if_t<__compatible_with<_Yp, _Tp>::value> >
605     _LIBCPP_HIDE_FROM_ABI
606     shared_ptr(const shared_ptr<_Yp>& __r) _NOEXCEPT
607         : __ptr_(__r.__ptr_),
608           __cntrl_(__r.__cntrl_)
609     {
610         if (__cntrl_)
611             __cntrl_->__add_shared();
612     }
613 
614     _LIBCPP_HIDE_FROM_ABI
615     shared_ptr(shared_ptr&& __r) _NOEXCEPT
616         : __ptr_(__r.__ptr_),
617           __cntrl_(__r.__cntrl_)
618     {
619         __r.__ptr_ = nullptr;
620         __r.__cntrl_ = nullptr;
621     }
622 
623     template<class _Yp, class = __enable_if_t<__compatible_with<_Yp, _Tp>::value> >
624     _LIBCPP_HIDE_FROM_ABI
625     shared_ptr(shared_ptr<_Yp>&& __r) _NOEXCEPT
626         : __ptr_(__r.__ptr_),
627           __cntrl_(__r.__cntrl_)
628     {
629         __r.__ptr_ = nullptr;
630         __r.__cntrl_ = nullptr;
631     }
632 
633     template<class _Yp, class = __enable_if_t<__compatible_with<_Yp, _Tp>::value> >
634     _LIBCPP_HIDE_FROM_ABI
635     explicit shared_ptr(const weak_ptr<_Yp>& __r)
636         : __ptr_(__r.__ptr_),
637           __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
638     {
639         if (__cntrl_ == nullptr)
640             __throw_bad_weak_ptr();
641     }
642 
643 #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
644     template<class _Yp, class = __enable_if_t<is_convertible<_Yp*, element_type*>::value> >
645     _LIBCPP_HIDE_FROM_ABI
646     shared_ptr(auto_ptr<_Yp>&& __r)
647         : __ptr_(__r.get())
648     {
649         typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
650         __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
651         __enable_weak_this(__r.get(), __r.get());
652         __r.release();
653     }
654 #endif
655 
656     template <class _Yp, class _Dp, class = __enable_if_t<
657         !is_lvalue_reference<_Dp>::value &&
658          is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value
659     > >
660     _LIBCPP_HIDE_FROM_ABI
661     shared_ptr(unique_ptr<_Yp, _Dp>&& __r)
662         : __ptr_(__r.get())
663     {
664 #if _LIBCPP_STD_VER > 11
665         if (__ptr_ == nullptr)
666             __cntrl_ = nullptr;
667         else
668 #endif
669         {
670             typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
671             typedef __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer, _Dp, _AllocT> _CntrlBlk;
672             __cntrl_ = new _CntrlBlk(__r.get(), std::move(__r.get_deleter()), _AllocT());
673             __enable_weak_this(__r.get(), __r.get());
674         }
675         __r.release();
676     }
677 
678     template <class _Yp, class _Dp, class = void, class = __enable_if_t<
679         is_lvalue_reference<_Dp>::value &&
680         is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value
681     > >
682     _LIBCPP_HIDE_FROM_ABI
683     shared_ptr(unique_ptr<_Yp, _Dp>&& __r)
684         : __ptr_(__r.get())
685     {
686 #if _LIBCPP_STD_VER > 11
687         if (__ptr_ == nullptr)
688             __cntrl_ = nullptr;
689         else
690 #endif
691         {
692             typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
693             typedef __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer,
694                                         reference_wrapper<typename remove_reference<_Dp>::type>,
695                                         _AllocT> _CntrlBlk;
696             __cntrl_ = new _CntrlBlk(__r.get(), _VSTD::ref(__r.get_deleter()), _AllocT());
697             __enable_weak_this(__r.get(), __r.get());
698         }
699         __r.release();
700     }
701 
702     _LIBCPP_HIDE_FROM_ABI
703     ~shared_ptr()
704     {
705         if (__cntrl_)
706             __cntrl_->__release_shared();
707     }
708 
709     _LIBCPP_HIDE_FROM_ABI
710     shared_ptr<_Tp>& operator=(const shared_ptr& __r) _NOEXCEPT
711     {
712         shared_ptr(__r).swap(*this);
713         return *this;
714     }
715 
716     template<class _Yp, class = __enable_if_t<__compatible_with<_Yp, _Tp>::value> >
717     _LIBCPP_HIDE_FROM_ABI
718     shared_ptr<_Tp>& operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
719     {
720         shared_ptr(__r).swap(*this);
721         return *this;
722     }
723 
724     _LIBCPP_HIDE_FROM_ABI
725     shared_ptr<_Tp>& operator=(shared_ptr&& __r) _NOEXCEPT
726     {
727         shared_ptr(_VSTD::move(__r)).swap(*this);
728         return *this;
729     }
730 
731     template<class _Yp, class = __enable_if_t<__compatible_with<_Yp, _Tp>::value> >
732     _LIBCPP_HIDE_FROM_ABI
733     shared_ptr<_Tp>& operator=(shared_ptr<_Yp>&& __r)
734     {
735         shared_ptr(_VSTD::move(__r)).swap(*this);
736         return *this;
737     }
738 
739 #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
740     template<class _Yp, class = __enable_if_t<
741         !is_array<_Yp>::value &&
742         is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value
743     > >
744     _LIBCPP_HIDE_FROM_ABI
745     shared_ptr<_Tp>& operator=(auto_ptr<_Yp>&& __r)
746     {
747         shared_ptr(_VSTD::move(__r)).swap(*this);
748         return *this;
749     }
750 #endif
751 
752     template <class _Yp, class _Dp, class = __enable_if_t<
753         is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value
754     > >
755     _LIBCPP_HIDE_FROM_ABI
756     shared_ptr<_Tp>& operator=(unique_ptr<_Yp, _Dp>&& __r)
757     {
758         shared_ptr(_VSTD::move(__r)).swap(*this);
759         return *this;
760     }
761 
762     _LIBCPP_HIDE_FROM_ABI
763     void swap(shared_ptr& __r) _NOEXCEPT
764     {
765         _VSTD::swap(__ptr_, __r.__ptr_);
766         _VSTD::swap(__cntrl_, __r.__cntrl_);
767     }
768 
769     _LIBCPP_HIDE_FROM_ABI
770     void reset() _NOEXCEPT
771     {
772         shared_ptr().swap(*this);
773     }
774 
775     template<class _Yp, class = __enable_if_t<
776         __compatible_with<_Yp, _Tp>::value
777     > >
778     _LIBCPP_HIDE_FROM_ABI
779     void reset(_Yp* __p)
780     {
781         shared_ptr(__p).swap(*this);
782     }
783 
784     template<class _Yp, class _Dp, class = __enable_if_t<
785         __compatible_with<_Yp, _Tp>::value
786     > >
787     _LIBCPP_HIDE_FROM_ABI
788     void reset(_Yp* __p, _Dp __d)
789     {
790         shared_ptr(__p, __d).swap(*this);
791     }
792 
793     template<class _Yp, class _Dp, class _Alloc, class = __enable_if_t<
794         __compatible_with<_Yp, _Tp>::value
795     > >
796     _LIBCPP_HIDE_FROM_ABI
797     void reset(_Yp* __p, _Dp __d, _Alloc __a)
798     {
799         shared_ptr(__p, __d, __a).swap(*this);
800     }
801 
802     _LIBCPP_HIDE_FROM_ABI
803     element_type* get() const _NOEXCEPT
804     {
805         return __ptr_;
806     }
807 
808     _LIBCPP_HIDE_FROM_ABI
809     typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
810     {
811         return *__ptr_;
812     }
813 
814     _LIBCPP_HIDE_FROM_ABI
815     element_type* operator->() const _NOEXCEPT
816     {
817         static_assert(!is_array<_Tp>::value,
818                       "std::shared_ptr<T>::operator-> is only valid when T is not an array type.");
819         return __ptr_;
820     }
821 
822     _LIBCPP_HIDE_FROM_ABI
823     long use_count() const _NOEXCEPT
824     {
825         return __cntrl_ ? __cntrl_->use_count() : 0;
826     }
827 
828     _LIBCPP_HIDE_FROM_ABI
829     bool unique() const _NOEXCEPT
830     {
831         return use_count() == 1;
832     }
833 
834     _LIBCPP_HIDE_FROM_ABI
835     explicit operator bool() const _NOEXCEPT
836     {
837         return get() != nullptr;
838     }
839 
840     template <class _Up>
841     _LIBCPP_HIDE_FROM_ABI
842     bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
843     {
844         return __cntrl_ < __p.__cntrl_;
845     }
846 
847     template <class _Up>
848     _LIBCPP_HIDE_FROM_ABI
849     bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
850     {
851         return __cntrl_ < __p.__cntrl_;
852     }
853 
854     _LIBCPP_HIDE_FROM_ABI
855     bool __owner_equivalent(const shared_ptr& __p) const
856     {
857         return __cntrl_ == __p.__cntrl_;
858     }
859 
860 #if _LIBCPP_STD_VER > 14
861     _LIBCPP_HIDE_FROM_ABI
862     typename add_lvalue_reference<element_type>::type operator[](ptrdiff_t __i) const
863     {
864             static_assert(is_array<_Tp>::value,
865                           "std::shared_ptr<T>::operator[] is only valid when T is an array type.");
866             return __ptr_[__i];
867     }
868 #endif
869 
870 #ifndef _LIBCPP_NO_RTTI
871     template <class _Dp>
872     _LIBCPP_HIDE_FROM_ABI
873     _Dp* __get_deleter() const _NOEXCEPT
874     {
875         return static_cast<_Dp*>(__cntrl_
876                     ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
877                       : nullptr);
878     }
879 #endif // _LIBCPP_NO_RTTI
880 
881     template<class _Yp, class _CntrlBlk>
882     _LIBCPP_HIDE_FROM_ABI
883     static shared_ptr<_Tp> __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl) _NOEXCEPT
884     {
885         shared_ptr<_Tp> __r;
886         __r.__ptr_ = __p;
887         __r.__cntrl_ = __cntrl;
888         __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
889         return __r;
890     }
891 
892 private:
893     template <class _Yp, bool = is_function<_Yp>::value>
894     struct __shared_ptr_default_allocator
895     {
896         typedef allocator<_Yp> type;
897     };
898 
899     template <class _Yp>
900     struct __shared_ptr_default_allocator<_Yp, true>
901     {
902         typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
903     };
904 
905     template <class _Yp, class _OrigPtr, class = __enable_if_t<
906         is_convertible<_OrigPtr*, const enable_shared_from_this<_Yp>*>::value
907     > >
908     _LIBCPP_HIDE_FROM_ABI
909     void __enable_weak_this(const enable_shared_from_this<_Yp>* __e, _OrigPtr* __ptr) _NOEXCEPT
910     {
911         typedef typename remove_cv<_Yp>::type _RawYp;
912         if (__e && __e->__weak_this_.expired())
913         {
914             __e->__weak_this_ = shared_ptr<_RawYp>(*this,
915                 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
916         }
917     }
918 
919     _LIBCPP_HIDE_FROM_ABI void __enable_weak_this(...) _NOEXCEPT { }
920 
921     template <class, class _Yp>
922     struct __shared_ptr_default_delete
923         : default_delete<_Yp>
924     { };
925 
926     template <class _Yp, class _Un, size_t _Sz>
927     struct __shared_ptr_default_delete<_Yp[_Sz], _Un>
928         : default_delete<_Yp[]>
929     { };
930 
931     template <class _Yp, class _Un>
932     struct __shared_ptr_default_delete<_Yp[], _Un>
933         : default_delete<_Yp[]>
934     { };
935 
936     template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
937     template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
938 };
939 
940 #if _LIBCPP_STD_VER > 14
941 template<class _Tp>
942 shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>;
943 template<class _Tp, class _Dp>
944 shared_ptr(unique_ptr<_Tp, _Dp>) -> shared_ptr<_Tp>;
945 #endif
946 
947 //
948 // std::allocate_shared and std::make_shared
949 //
950 template<class _Tp, class _Alloc, class ..._Args, class = __enable_if_t<!is_array<_Tp>::value> >
951 _LIBCPP_HIDE_FROM_ABI
952 shared_ptr<_Tp> allocate_shared(const _Alloc& __a, _Args&& ...__args)
953 {
954     using _ControlBlock = __shared_ptr_emplace<_Tp, _Alloc>;
955     using _ControlBlockAllocator = typename __allocator_traits_rebind<_Alloc, _ControlBlock>::type;
956     __allocation_guard<_ControlBlockAllocator> __guard(__a, 1);
957     ::new ((void*)_VSTD::addressof(*__guard.__get())) _ControlBlock(__a, _VSTD::forward<_Args>(__args)...);
958     auto __control_block = __guard.__release_ptr();
959     return shared_ptr<_Tp>::__create_with_control_block((*__control_block).__get_elem(), _VSTD::addressof(*__control_block));
960 }
961 
962 template<class _Tp, class ..._Args, class = __enable_if_t<!is_array<_Tp>::value> >
963 _LIBCPP_HIDE_FROM_ABI
964 shared_ptr<_Tp> make_shared(_Args&& ...__args)
965 {
966     return _VSTD::allocate_shared<_Tp>(allocator<_Tp>(), _VSTD::forward<_Args>(__args)...);
967 }
968 
969 #if _LIBCPP_STD_VER > 17
970 
971 template <size_t _Alignment>
972 struct __sp_aligned_storage {
973     alignas(_Alignment) char __storage[_Alignment];
974 };
975 
976 template <class _Tp, class _Alloc>
977 struct __unbounded_array_control_block;
978 
979 template <class _Tp, class _Alloc>
980 struct __unbounded_array_control_block<_Tp[], _Alloc> : __shared_weak_count
981 {
982     _LIBCPP_HIDE_FROM_ABI constexpr
983     _Tp* __get_data() noexcept { return __data_; }
984 
985     _LIBCPP_HIDE_FROM_ABI
986     explicit __unbounded_array_control_block(_Alloc const& __alloc, size_t __count, _Tp const& __arg)
987         : __alloc_(__alloc), __count_(__count)
988     {
989         std::__uninitialized_allocator_fill_n(__alloc_, std::begin(__data_), __count_, __arg);
990     }
991 
992     _LIBCPP_HIDE_FROM_ABI
993     explicit __unbounded_array_control_block(_Alloc const& __alloc, size_t __count)
994         : __alloc_(__alloc), __count_(__count)
995     {
996         std::__uninitialized_allocator_value_construct_n(__alloc_, std::begin(__data_), __count_);
997     }
998 
999     // Returns the number of bytes required to store a control block followed by the given number
1000     // of elements of _Tp, with the whole storage being aligned to a multiple of _Tp's alignment.
1001     _LIBCPP_HIDE_FROM_ABI
1002     static constexpr size_t __bytes_for(size_t __elements) {
1003         // When there's 0 elements, the control block alone is enough since it holds one element.
1004         // Otherwise, we allocate one fewer element than requested because the control block already
1005         // holds one. Also, we use the bitwise formula below to ensure that we allocate enough bytes
1006         // for the whole allocation to be a multiple of _Tp's alignment. That formula is taken from [1].
1007         //
1008         // [1]: https://en.wikipedia.org/wiki/Data_structure_alignment#Computing_padding
1009         size_t __bytes = __elements == 0 ? sizeof(__unbounded_array_control_block)
1010                                          : (__elements - 1) * sizeof(_Tp) + sizeof(__unbounded_array_control_block);
1011         constexpr size_t __align = alignof(_Tp);
1012         return (__bytes + __align - 1) & ~(__align - 1);
1013     }
1014 
1015     _LIBCPP_HIDE_FROM_ABI
1016     ~__unbounded_array_control_block() override { } // can't be `= default` because of the sometimes-non-trivial union member __data_
1017 
1018 private:
1019     void __on_zero_shared() _NOEXCEPT override {
1020         __allocator_traits_rebind_t<_Alloc, _Tp> __value_alloc(__alloc_);
1021         std::__allocator_destroy_multidimensional(__value_alloc, __data_, __data_ + __count_);
1022     }
1023 
1024     void __on_zero_shared_weak() _NOEXCEPT override {
1025         using _AlignedStorage = __sp_aligned_storage<alignof(__unbounded_array_control_block)>;
1026         using _StorageAlloc = __allocator_traits_rebind_t<_Alloc, _AlignedStorage>;
1027         using _PointerTraits = pointer_traits<typename allocator_traits<_StorageAlloc>::pointer>;
1028 
1029         _StorageAlloc __tmp(__alloc_);
1030         __alloc_.~_Alloc();
1031         size_t __size = __unbounded_array_control_block::__bytes_for(__count_);
1032         _AlignedStorage* __storage = reinterpret_cast<_AlignedStorage*>(this);
1033         allocator_traits<_StorageAlloc>::deallocate(__tmp, _PointerTraits::pointer_to(*__storage), __size);
1034     }
1035 
1036     _LIBCPP_NO_UNIQUE_ADDRESS _Alloc __alloc_;
1037     size_t __count_;
1038     union {
1039         _Tp __data_[1];
1040     };
1041 };
1042 
1043 template<class _Array, class _Alloc, class... _Arg>
1044 _LIBCPP_HIDE_FROM_ABI
1045 shared_ptr<_Array> __allocate_shared_unbounded_array(const _Alloc& __a, size_t __n, _Arg&& ...__arg)
1046 {
1047     static_assert(is_unbounded_array_v<_Array>);
1048     // We compute the number of bytes necessary to hold the control block and the
1049     // array elements. Then, we allocate an array of properly-aligned dummy structs
1050     // large enough to hold the control block and array. This allows shifting the
1051     // burden of aligning memory properly from us to the allocator.
1052     using _ControlBlock = __unbounded_array_control_block<_Array, _Alloc>;
1053     using _AlignedStorage = __sp_aligned_storage<alignof(_ControlBlock)>;
1054     using _StorageAlloc = __allocator_traits_rebind_t<_Alloc, _AlignedStorage>;
1055     __allocation_guard<_StorageAlloc> __guard(__a, _ControlBlock::__bytes_for(__n) / sizeof(_AlignedStorage));
1056     _ControlBlock* __control_block = reinterpret_cast<_ControlBlock*>(std::addressof(*__guard.__get()));
1057     std::construct_at(__control_block, __a, __n, std::forward<_Arg>(__arg)...);
1058     __guard.__release_ptr();
1059     return shared_ptr<_Array>::__create_with_control_block(__control_block->__get_data(), __control_block);
1060 }
1061 
1062 template <class _Tp, class _Alloc>
1063 struct __bounded_array_control_block;
1064 
1065 template <class _Tp, size_t _Count, class _Alloc>
1066 struct __bounded_array_control_block<_Tp[_Count], _Alloc>
1067     : __shared_weak_count
1068 {
1069     _LIBCPP_HIDE_FROM_ABI constexpr
1070     _Tp* __get_data() noexcept { return __data_; }
1071 
1072     _LIBCPP_HIDE_FROM_ABI
1073     explicit __bounded_array_control_block(_Alloc const& __alloc, _Tp const& __arg) : __alloc_(__alloc) {
1074         std::__uninitialized_allocator_fill_n(__alloc_, std::addressof(__data_[0]), _Count, __arg);
1075     }
1076 
1077     _LIBCPP_HIDE_FROM_ABI
1078     explicit __bounded_array_control_block(_Alloc const& __alloc) : __alloc_(__alloc) {
1079         std::__uninitialized_allocator_value_construct_n(__alloc_, std::addressof(__data_[0]), _Count);
1080     }
1081 
1082     _LIBCPP_HIDE_FROM_ABI
1083     ~__bounded_array_control_block() override { } // can't be `= default` because of the sometimes-non-trivial union member __data_
1084 
1085 private:
1086     void __on_zero_shared() _NOEXCEPT override {
1087         __allocator_traits_rebind_t<_Alloc, _Tp> __value_alloc(__alloc_);
1088         std::__allocator_destroy_multidimensional(__value_alloc, __data_, __data_ + _Count);
1089     }
1090 
1091     void __on_zero_shared_weak() _NOEXCEPT override {
1092         using _ControlBlockAlloc = __allocator_traits_rebind_t<_Alloc, __bounded_array_control_block>;
1093         using _PointerTraits = pointer_traits<typename allocator_traits<_ControlBlockAlloc>::pointer>;
1094 
1095         _ControlBlockAlloc __tmp(__alloc_);
1096         __alloc_.~_Alloc();
1097         allocator_traits<_ControlBlockAlloc>::deallocate(__tmp, _PointerTraits::pointer_to(*this), sizeof(*this));
1098     }
1099 
1100     _LIBCPP_NO_UNIQUE_ADDRESS _Alloc __alloc_;
1101     union {
1102         _Tp __data_[_Count];
1103     };
1104 };
1105 
1106 template<class _Array, class _Alloc, class... _Arg>
1107 _LIBCPP_HIDE_FROM_ABI
1108 shared_ptr<_Array> __allocate_shared_bounded_array(const _Alloc& __a, _Arg&& ...__arg)
1109 {
1110     static_assert(is_bounded_array_v<_Array>);
1111     using _ControlBlock = __bounded_array_control_block<_Array, _Alloc>;
1112     using _ControlBlockAlloc = __allocator_traits_rebind_t<_Alloc, _ControlBlock>;
1113 
1114     __allocation_guard<_ControlBlockAlloc> __guard(__a, 1);
1115     _ControlBlock* __control_block = reinterpret_cast<_ControlBlock*>(std::addressof(*__guard.__get()));
1116     std::construct_at(__control_block, __a, std::forward<_Arg>(__arg)...);
1117     __guard.__release_ptr();
1118     return shared_ptr<_Array>::__create_with_control_block(__control_block->__get_data(), __control_block);
1119 }
1120 
1121 template<class _Tp, class _Alloc, class = __enable_if_t<is_bounded_array<_Tp>::value>>
1122 _LIBCPP_HIDE_FROM_ABI
1123 shared_ptr<_Tp> allocate_shared(const _Alloc& __a)
1124 {
1125     return std::__allocate_shared_bounded_array<_Tp>(__a);
1126 }
1127 
1128 template<class _Tp, class _Alloc, class = __enable_if_t<is_bounded_array<_Tp>::value>>
1129 _LIBCPP_HIDE_FROM_ABI
1130 shared_ptr<_Tp> allocate_shared(const _Alloc& __a, const remove_extent_t<_Tp>& __u)
1131 {
1132     return std::__allocate_shared_bounded_array<_Tp>(__a, __u);
1133 }
1134 
1135 template<class _Tp, class _Alloc, class = __enable_if_t<is_unbounded_array<_Tp>::value>>
1136 _LIBCPP_HIDE_FROM_ABI
1137 shared_ptr<_Tp> allocate_shared(const _Alloc& __a, size_t __n)
1138 {
1139     return std::__allocate_shared_unbounded_array<_Tp>(__a, __n);
1140 }
1141 
1142 template<class _Tp, class _Alloc, class = __enable_if_t<is_unbounded_array<_Tp>::value>>
1143 _LIBCPP_HIDE_FROM_ABI
1144 shared_ptr<_Tp> allocate_shared(const _Alloc& __a, size_t __n, const remove_extent_t<_Tp>& __u)
1145 {
1146     return std::__allocate_shared_unbounded_array<_Tp>(__a, __n, __u);
1147 }
1148 
1149 template<class _Tp, class = __enable_if_t<is_bounded_array<_Tp>::value>>
1150 _LIBCPP_HIDE_FROM_ABI
1151 shared_ptr<_Tp> make_shared()
1152 {
1153     return std::__allocate_shared_bounded_array<_Tp>(allocator<_Tp>());
1154 }
1155 
1156 template<class _Tp, class = __enable_if_t<is_bounded_array<_Tp>::value>>
1157 _LIBCPP_HIDE_FROM_ABI
1158 shared_ptr<_Tp> make_shared(const remove_extent_t<_Tp>& __u)
1159 {
1160     return std::__allocate_shared_bounded_array<_Tp>(allocator<_Tp>(), __u);
1161 }
1162 
1163 template<class _Tp, class = __enable_if_t<is_unbounded_array<_Tp>::value>>
1164 _LIBCPP_HIDE_FROM_ABI
1165 shared_ptr<_Tp> make_shared(size_t __n)
1166 {
1167     return std::__allocate_shared_unbounded_array<_Tp>(allocator<_Tp>(), __n);
1168 }
1169 
1170 template<class _Tp, class = __enable_if_t<is_unbounded_array<_Tp>::value>>
1171 _LIBCPP_HIDE_FROM_ABI
1172 shared_ptr<_Tp> make_shared(size_t __n, const remove_extent_t<_Tp>& __u)
1173 {
1174     return std::__allocate_shared_unbounded_array<_Tp>(allocator<_Tp>(), __n, __u);
1175 }
1176 
1177 #endif // _LIBCPP_STD_VER > 17
1178 
1179 template<class _Tp, class _Up>
1180 inline _LIBCPP_INLINE_VISIBILITY
1181 bool
1182 operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
1183 {
1184     return __x.get() == __y.get();
1185 }
1186 
1187 template<class _Tp, class _Up>
1188 inline _LIBCPP_INLINE_VISIBILITY
1189 bool
1190 operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
1191 {
1192     return !(__x == __y);
1193 }
1194 
1195 template<class _Tp, class _Up>
1196 inline _LIBCPP_INLINE_VISIBILITY
1197 bool
1198 operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
1199 {
1200 #if _LIBCPP_STD_VER <= 11
1201     typedef typename common_type<_Tp*, _Up*>::type _Vp;
1202     return less<_Vp>()(__x.get(), __y.get());
1203 #else
1204     return less<>()(__x.get(), __y.get());
1205 #endif
1206 
1207 }
1208 
1209 template<class _Tp, class _Up>
1210 inline _LIBCPP_INLINE_VISIBILITY
1211 bool
1212 operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
1213 {
1214     return __y < __x;
1215 }
1216 
1217 template<class _Tp, class _Up>
1218 inline _LIBCPP_INLINE_VISIBILITY
1219 bool
1220 operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
1221 {
1222     return !(__y < __x);
1223 }
1224 
1225 template<class _Tp, class _Up>
1226 inline _LIBCPP_INLINE_VISIBILITY
1227 bool
1228 operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
1229 {
1230     return !(__x < __y);
1231 }
1232 
1233 template<class _Tp>
1234 inline _LIBCPP_INLINE_VISIBILITY
1235 bool
1236 operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
1237 {
1238     return !__x;
1239 }
1240 
1241 template<class _Tp>
1242 inline _LIBCPP_INLINE_VISIBILITY
1243 bool
1244 operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
1245 {
1246     return !__x;
1247 }
1248 
1249 template<class _Tp>
1250 inline _LIBCPP_INLINE_VISIBILITY
1251 bool
1252 operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
1253 {
1254     return static_cast<bool>(__x);
1255 }
1256 
1257 template<class _Tp>
1258 inline _LIBCPP_INLINE_VISIBILITY
1259 bool
1260 operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
1261 {
1262     return static_cast<bool>(__x);
1263 }
1264 
1265 template<class _Tp>
1266 inline _LIBCPP_INLINE_VISIBILITY
1267 bool
1268 operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
1269 {
1270     return less<_Tp*>()(__x.get(), nullptr);
1271 }
1272 
1273 template<class _Tp>
1274 inline _LIBCPP_INLINE_VISIBILITY
1275 bool
1276 operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
1277 {
1278     return less<_Tp*>()(nullptr, __x.get());
1279 }
1280 
1281 template<class _Tp>
1282 inline _LIBCPP_INLINE_VISIBILITY
1283 bool
1284 operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
1285 {
1286     return nullptr < __x;
1287 }
1288 
1289 template<class _Tp>
1290 inline _LIBCPP_INLINE_VISIBILITY
1291 bool
1292 operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
1293 {
1294     return __x < nullptr;
1295 }
1296 
1297 template<class _Tp>
1298 inline _LIBCPP_INLINE_VISIBILITY
1299 bool
1300 operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
1301 {
1302     return !(nullptr < __x);
1303 }
1304 
1305 template<class _Tp>
1306 inline _LIBCPP_INLINE_VISIBILITY
1307 bool
1308 operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
1309 {
1310     return !(__x < nullptr);
1311 }
1312 
1313 template<class _Tp>
1314 inline _LIBCPP_INLINE_VISIBILITY
1315 bool
1316 operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
1317 {
1318     return !(__x < nullptr);
1319 }
1320 
1321 template<class _Tp>
1322 inline _LIBCPP_INLINE_VISIBILITY
1323 bool
1324 operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
1325 {
1326     return !(nullptr < __x);
1327 }
1328 
1329 template<class _Tp>
1330 inline _LIBCPP_INLINE_VISIBILITY
1331 void
1332 swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
1333 {
1334     __x.swap(__y);
1335 }
1336 
1337 template<class _Tp, class _Up>
1338 inline _LIBCPP_INLINE_VISIBILITY
1339 shared_ptr<_Tp>
1340 static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
1341 {
1342     return shared_ptr<_Tp>(__r,
1343                            static_cast<
1344                                typename shared_ptr<_Tp>::element_type*>(__r.get()));
1345 }
1346 
1347 template<class _Tp, class _Up>
1348 inline _LIBCPP_INLINE_VISIBILITY
1349 shared_ptr<_Tp>
1350 dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
1351 {
1352     typedef typename shared_ptr<_Tp>::element_type _ET;
1353     _ET* __p = dynamic_cast<_ET*>(__r.get());
1354     return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
1355 }
1356 
1357 template<class _Tp, class _Up>
1358 shared_ptr<_Tp>
1359 const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
1360 {
1361     typedef typename shared_ptr<_Tp>::element_type _RTp;
1362     return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
1363 }
1364 
1365 template<class _Tp, class _Up>
1366 shared_ptr<_Tp>
1367 reinterpret_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
1368 {
1369     return shared_ptr<_Tp>(__r,
1370                            reinterpret_cast<
1371                                typename shared_ptr<_Tp>::element_type*>(__r.get()));
1372 }
1373 
1374 #ifndef _LIBCPP_NO_RTTI
1375 
1376 template<class _Dp, class _Tp>
1377 inline _LIBCPP_INLINE_VISIBILITY
1378 _Dp*
1379 get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
1380 {
1381     return __p.template __get_deleter<_Dp>();
1382 }
1383 
1384 #endif // _LIBCPP_NO_RTTI
1385 
1386 template<class _Tp>
1387 class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS weak_ptr
1388 {
1389 public:
1390 #if _LIBCPP_STD_VER > 14
1391     typedef remove_extent_t<_Tp> element_type;
1392 #else
1393     typedef _Tp element_type;
1394 #endif
1395 
1396 private:
1397     element_type*        __ptr_;
1398     __shared_weak_count* __cntrl_;
1399 
1400 public:
1401     _LIBCPP_INLINE_VISIBILITY
1402     _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
1403     template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
1404                    typename enable_if<__compatible_with<_Yp, _Tp>::value, __nat*>::type = 0)
1405                         _NOEXCEPT;
1406     _LIBCPP_INLINE_VISIBILITY
1407     weak_ptr(weak_ptr const& __r) _NOEXCEPT;
1408     template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
1409                    typename enable_if<__compatible_with<_Yp, _Tp>::value, __nat*>::type = 0)
1410                          _NOEXCEPT;
1411 
1412     _LIBCPP_INLINE_VISIBILITY
1413     weak_ptr(weak_ptr&& __r) _NOEXCEPT;
1414     template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
1415                    typename enable_if<__compatible_with<_Yp, _Tp>::value, __nat*>::type = 0)
1416                          _NOEXCEPT;
1417     ~weak_ptr();
1418 
1419     _LIBCPP_INLINE_VISIBILITY
1420     weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
1421     template<class _Yp>
1422         typename enable_if
1423         <
1424             __compatible_with<_Yp, _Tp>::value,
1425             weak_ptr&
1426         >::type
1427         _LIBCPP_INLINE_VISIBILITY
1428         operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
1429 
1430     _LIBCPP_INLINE_VISIBILITY
1431     weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
1432     template<class _Yp>
1433         typename enable_if
1434         <
1435             __compatible_with<_Yp, _Tp>::value,
1436             weak_ptr&
1437         >::type
1438         _LIBCPP_INLINE_VISIBILITY
1439         operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
1440 
1441     template<class _Yp>
1442         typename enable_if
1443         <
1444             __compatible_with<_Yp, _Tp>::value,
1445             weak_ptr&
1446         >::type
1447         _LIBCPP_INLINE_VISIBILITY
1448         operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
1449 
1450     _LIBCPP_INLINE_VISIBILITY
1451     void swap(weak_ptr& __r) _NOEXCEPT;
1452     _LIBCPP_INLINE_VISIBILITY
1453     void reset() _NOEXCEPT;
1454 
1455     _LIBCPP_INLINE_VISIBILITY
1456     long use_count() const _NOEXCEPT
1457         {return __cntrl_ ? __cntrl_->use_count() : 0;}
1458     _LIBCPP_INLINE_VISIBILITY
1459     bool expired() const _NOEXCEPT
1460         {return __cntrl_ == nullptr || __cntrl_->use_count() == 0;}
1461     shared_ptr<_Tp> lock() const _NOEXCEPT;
1462     template<class _Up>
1463         _LIBCPP_INLINE_VISIBILITY
1464         bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
1465         {return __cntrl_ < __r.__cntrl_;}
1466     template<class _Up>
1467         _LIBCPP_INLINE_VISIBILITY
1468         bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
1469         {return __cntrl_ < __r.__cntrl_;}
1470 
1471     template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
1472     template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
1473 };
1474 
1475 #if _LIBCPP_STD_VER > 14
1476 template<class _Tp>
1477 weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>;
1478 #endif
1479 
1480 template<class _Tp>
1481 inline
1482 _LIBCPP_CONSTEXPR
1483 weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
1484     : __ptr_(nullptr),
1485       __cntrl_(nullptr)
1486 {
1487 }
1488 
1489 template<class _Tp>
1490 inline
1491 weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
1492     : __ptr_(__r.__ptr_),
1493       __cntrl_(__r.__cntrl_)
1494 {
1495     if (__cntrl_)
1496         __cntrl_->__add_weak();
1497 }
1498 
1499 template<class _Tp>
1500 template<class _Yp>
1501 inline
1502 weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
1503                         typename enable_if<__compatible_with<_Yp, _Tp>::value, __nat*>::type)
1504                          _NOEXCEPT
1505     : __ptr_(__r.__ptr_),
1506       __cntrl_(__r.__cntrl_)
1507 {
1508     if (__cntrl_)
1509         __cntrl_->__add_weak();
1510 }
1511 
1512 template<class _Tp>
1513 template<class _Yp>
1514 inline
1515 weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
1516                         typename enable_if<__compatible_with<_Yp, _Tp>::value, __nat*>::type)
1517          _NOEXCEPT
1518     : __ptr_(__r.__ptr_),
1519       __cntrl_(__r.__cntrl_)
1520 {
1521     if (__cntrl_)
1522         __cntrl_->__add_weak();
1523 }
1524 
1525 template<class _Tp>
1526 inline
1527 weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
1528     : __ptr_(__r.__ptr_),
1529       __cntrl_(__r.__cntrl_)
1530 {
1531     __r.__ptr_ = nullptr;
1532     __r.__cntrl_ = nullptr;
1533 }
1534 
1535 template<class _Tp>
1536 template<class _Yp>
1537 inline
1538 weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
1539                         typename enable_if<__compatible_with<_Yp, _Tp>::value, __nat*>::type)
1540          _NOEXCEPT
1541     : __ptr_(__r.__ptr_),
1542       __cntrl_(__r.__cntrl_)
1543 {
1544     __r.__ptr_ = nullptr;
1545     __r.__cntrl_ = nullptr;
1546 }
1547 
1548 template<class _Tp>
1549 weak_ptr<_Tp>::~weak_ptr()
1550 {
1551     if (__cntrl_)
1552         __cntrl_->__release_weak();
1553 }
1554 
1555 template<class _Tp>
1556 inline
1557 weak_ptr<_Tp>&
1558 weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
1559 {
1560     weak_ptr(__r).swap(*this);
1561     return *this;
1562 }
1563 
1564 template<class _Tp>
1565 template<class _Yp>
1566 inline
1567 typename enable_if
1568 <
1569     __compatible_with<_Yp, _Tp>::value,
1570     weak_ptr<_Tp>&
1571 >::type
1572 weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
1573 {
1574     weak_ptr(__r).swap(*this);
1575     return *this;
1576 }
1577 
1578 template<class _Tp>
1579 inline
1580 weak_ptr<_Tp>&
1581 weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
1582 {
1583     weak_ptr(_VSTD::move(__r)).swap(*this);
1584     return *this;
1585 }
1586 
1587 template<class _Tp>
1588 template<class _Yp>
1589 inline
1590 typename enable_if
1591 <
1592     __compatible_with<_Yp, _Tp>::value,
1593     weak_ptr<_Tp>&
1594 >::type
1595 weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
1596 {
1597     weak_ptr(_VSTD::move(__r)).swap(*this);
1598     return *this;
1599 }
1600 
1601 template<class _Tp>
1602 template<class _Yp>
1603 inline
1604 typename enable_if
1605 <
1606     __compatible_with<_Yp, _Tp>::value,
1607     weak_ptr<_Tp>&
1608 >::type
1609 weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
1610 {
1611     weak_ptr(__r).swap(*this);
1612     return *this;
1613 }
1614 
1615 template<class _Tp>
1616 inline
1617 void
1618 weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
1619 {
1620     _VSTD::swap(__ptr_, __r.__ptr_);
1621     _VSTD::swap(__cntrl_, __r.__cntrl_);
1622 }
1623 
1624 template<class _Tp>
1625 inline _LIBCPP_INLINE_VISIBILITY
1626 void
1627 swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
1628 {
1629     __x.swap(__y);
1630 }
1631 
1632 template<class _Tp>
1633 inline
1634 void
1635 weak_ptr<_Tp>::reset() _NOEXCEPT
1636 {
1637     weak_ptr().swap(*this);
1638 }
1639 
1640 template<class _Tp>
1641 shared_ptr<_Tp>
1642 weak_ptr<_Tp>::lock() const _NOEXCEPT
1643 {
1644     shared_ptr<_Tp> __r;
1645     __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
1646     if (__r.__cntrl_)
1647         __r.__ptr_ = __ptr_;
1648     return __r;
1649 }
1650 
1651 #if _LIBCPP_STD_VER > 14
1652 template <class _Tp = void> struct owner_less;
1653 #else
1654 template <class _Tp> struct owner_less;
1655 #endif
1656 
1657 
1658 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1659 template <class _Tp>
1660 struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
1661 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
1662     : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
1663 #endif
1664 {
1665 _LIBCPP_SUPPRESS_DEPRECATED_POP
1666 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
1667     _LIBCPP_DEPRECATED_IN_CXX17 typedef bool result_type;
1668     _LIBCPP_DEPRECATED_IN_CXX17 typedef shared_ptr<_Tp> first_argument_type;
1669     _LIBCPP_DEPRECATED_IN_CXX17 typedef shared_ptr<_Tp> second_argument_type;
1670 #endif
1671     _LIBCPP_INLINE_VISIBILITY
1672     bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
1673         {return __x.owner_before(__y);}
1674     _LIBCPP_INLINE_VISIBILITY
1675     bool operator()(shared_ptr<_Tp> const& __x,   weak_ptr<_Tp> const& __y) const _NOEXCEPT
1676         {return __x.owner_before(__y);}
1677     _LIBCPP_INLINE_VISIBILITY
1678     bool operator()(  weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
1679         {return __x.owner_before(__y);}
1680 };
1681 
1682 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
1683 template <class _Tp>
1684 struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
1685 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
1686     : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
1687 #endif
1688 {
1689 _LIBCPP_SUPPRESS_DEPRECATED_POP
1690 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
1691     _LIBCPP_DEPRECATED_IN_CXX17 typedef bool result_type;
1692     _LIBCPP_DEPRECATED_IN_CXX17 typedef weak_ptr<_Tp> first_argument_type;
1693     _LIBCPP_DEPRECATED_IN_CXX17 typedef weak_ptr<_Tp> second_argument_type;
1694 #endif
1695     _LIBCPP_INLINE_VISIBILITY
1696     bool operator()(  weak_ptr<_Tp> const& __x,   weak_ptr<_Tp> const& __y) const _NOEXCEPT
1697         {return __x.owner_before(__y);}
1698     _LIBCPP_INLINE_VISIBILITY
1699     bool operator()(shared_ptr<_Tp> const& __x,   weak_ptr<_Tp> const& __y) const _NOEXCEPT
1700         {return __x.owner_before(__y);}
1701     _LIBCPP_INLINE_VISIBILITY
1702     bool operator()(  weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
1703         {return __x.owner_before(__y);}
1704 };
1705 
1706 #if _LIBCPP_STD_VER > 14
1707 template <>
1708 struct _LIBCPP_TEMPLATE_VIS owner_less<void>
1709 {
1710     template <class _Tp, class _Up>
1711     _LIBCPP_INLINE_VISIBILITY
1712     bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
1713         {return __x.owner_before(__y);}
1714     template <class _Tp, class _Up>
1715     _LIBCPP_INLINE_VISIBILITY
1716     bool operator()( shared_ptr<_Tp> const& __x,   weak_ptr<_Up> const& __y) const _NOEXCEPT
1717         {return __x.owner_before(__y);}
1718     template <class _Tp, class _Up>
1719     _LIBCPP_INLINE_VISIBILITY
1720     bool operator()(   weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
1721         {return __x.owner_before(__y);}
1722     template <class _Tp, class _Up>
1723     _LIBCPP_INLINE_VISIBILITY
1724     bool operator()(   weak_ptr<_Tp> const& __x,   weak_ptr<_Up> const& __y) const _NOEXCEPT
1725         {return __x.owner_before(__y);}
1726     typedef void is_transparent;
1727 };
1728 #endif
1729 
1730 template<class _Tp>
1731 class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
1732 {
1733     mutable weak_ptr<_Tp> __weak_this_;
1734 protected:
1735     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1736     enable_shared_from_this() _NOEXCEPT {}
1737     _LIBCPP_INLINE_VISIBILITY
1738     enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
1739     _LIBCPP_INLINE_VISIBILITY
1740     enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
1741         {return *this;}
1742     _LIBCPP_INLINE_VISIBILITY
1743     ~enable_shared_from_this() {}
1744 public:
1745     _LIBCPP_INLINE_VISIBILITY
1746     shared_ptr<_Tp> shared_from_this()
1747         {return shared_ptr<_Tp>(__weak_this_);}
1748     _LIBCPP_INLINE_VISIBILITY
1749     shared_ptr<_Tp const> shared_from_this() const
1750         {return shared_ptr<const _Tp>(__weak_this_);}
1751 
1752 #if _LIBCPP_STD_VER > 14
1753     _LIBCPP_INLINE_VISIBILITY
1754     weak_ptr<_Tp> weak_from_this() _NOEXCEPT
1755        { return __weak_this_; }
1756 
1757     _LIBCPP_INLINE_VISIBILITY
1758     weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
1759         { return __weak_this_; }
1760 #endif // _LIBCPP_STD_VER > 14
1761 
1762     template <class _Up> friend class shared_ptr;
1763 };
1764 
1765 template <class _Tp> struct _LIBCPP_TEMPLATE_VIS hash;
1766 
1767 template <class _Tp>
1768 struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
1769 {
1770 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
1771     _LIBCPP_DEPRECATED_IN_CXX17 typedef shared_ptr<_Tp> argument_type;
1772     _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t          result_type;
1773 #endif
1774 
1775     _LIBCPP_INLINE_VISIBILITY
1776     size_t operator()(const shared_ptr<_Tp>& __ptr) const _NOEXCEPT
1777     {
1778         return hash<typename shared_ptr<_Tp>::element_type*>()(__ptr.get());
1779     }
1780 };
1781 
1782 template<class _CharT, class _Traits, class _Yp>
1783 inline _LIBCPP_INLINE_VISIBILITY
1784 basic_ostream<_CharT, _Traits>&
1785 operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
1786 
1787 
1788 #if !defined(_LIBCPP_HAS_NO_THREADS)
1789 
1790 class _LIBCPP_TYPE_VIS __sp_mut
1791 {
1792     void* __lx;
1793 public:
1794     void lock() _NOEXCEPT;
1795     void unlock() _NOEXCEPT;
1796 
1797 private:
1798     _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
1799     __sp_mut(const __sp_mut&);
1800     __sp_mut& operator=(const __sp_mut&);
1801 
1802     friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
1803 };
1804 
1805 _LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
1806 __sp_mut& __get_sp_mut(const void*);
1807 
1808 template <class _Tp>
1809 inline _LIBCPP_INLINE_VISIBILITY
1810 bool
1811 atomic_is_lock_free(const shared_ptr<_Tp>*)
1812 {
1813     return false;
1814 }
1815 
1816 template <class _Tp>
1817 _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
1818 shared_ptr<_Tp>
1819 atomic_load(const shared_ptr<_Tp>* __p)
1820 {
1821     __sp_mut& __m = __get_sp_mut(__p);
1822     __m.lock();
1823     shared_ptr<_Tp> __q = *__p;
1824     __m.unlock();
1825     return __q;
1826 }
1827 
1828 template <class _Tp>
1829 inline _LIBCPP_INLINE_VISIBILITY
1830 _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
1831 shared_ptr<_Tp>
1832 atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
1833 {
1834     return atomic_load(__p);
1835 }
1836 
1837 template <class _Tp>
1838 _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
1839 void
1840 atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
1841 {
1842     __sp_mut& __m = __get_sp_mut(__p);
1843     __m.lock();
1844     __p->swap(__r);
1845     __m.unlock();
1846 }
1847 
1848 template <class _Tp>
1849 inline _LIBCPP_INLINE_VISIBILITY
1850 _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
1851 void
1852 atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
1853 {
1854     atomic_store(__p, __r);
1855 }
1856 
1857 template <class _Tp>
1858 _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
1859 shared_ptr<_Tp>
1860 atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
1861 {
1862     __sp_mut& __m = __get_sp_mut(__p);
1863     __m.lock();
1864     __p->swap(__r);
1865     __m.unlock();
1866     return __r;
1867 }
1868 
1869 template <class _Tp>
1870 inline _LIBCPP_INLINE_VISIBILITY
1871 _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
1872 shared_ptr<_Tp>
1873 atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
1874 {
1875     return atomic_exchange(__p, __r);
1876 }
1877 
1878 template <class _Tp>
1879 _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
1880 bool
1881 atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
1882 {
1883     shared_ptr<_Tp> __temp;
1884     __sp_mut& __m = __get_sp_mut(__p);
1885     __m.lock();
1886     if (__p->__owner_equivalent(*__v))
1887     {
1888         _VSTD::swap(__temp, *__p);
1889         *__p = __w;
1890         __m.unlock();
1891         return true;
1892     }
1893     _VSTD::swap(__temp, *__v);
1894     *__v = *__p;
1895     __m.unlock();
1896     return false;
1897 }
1898 
1899 template <class _Tp>
1900 inline _LIBCPP_INLINE_VISIBILITY
1901 _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
1902 bool
1903 atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
1904 {
1905     return atomic_compare_exchange_strong(__p, __v, __w);
1906 }
1907 
1908 template <class _Tp>
1909 inline _LIBCPP_INLINE_VISIBILITY
1910 _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
1911 bool
1912 atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
1913                                         shared_ptr<_Tp> __w, memory_order, memory_order)
1914 {
1915     return atomic_compare_exchange_strong(__p, __v, __w);
1916 }
1917 
1918 template <class _Tp>
1919 inline _LIBCPP_INLINE_VISIBILITY
1920 _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
1921 bool
1922 atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
1923                                       shared_ptr<_Tp> __w, memory_order, memory_order)
1924 {
1925     return atomic_compare_exchange_weak(__p, __v, __w);
1926 }
1927 
1928 #endif // !defined(_LIBCPP_HAS_NO_THREADS)
1929 
1930 _LIBCPP_END_NAMESPACE_STD
1931 
1932 #endif // _LIBCPP___MEMORY_SHARED_PTR_H
1933