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