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