xref: /freebsd/contrib/llvm-project/libcxx/include/__memory/shared_ptr.h (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
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(__tmp, _PointerTraits::pointer_to(*__storage), __size);
1138     }
1139 
1140     _LIBCPP_NO_UNIQUE_ADDRESS _Alloc __alloc_;
1141     size_t __count_;
1142     union {
1143         _Tp __data_[1];
1144     };
1145 };
1146 
1147 template<class _Array, class _Alloc, class... _Arg>
1148 _LIBCPP_HIDE_FROM_ABI
1149 shared_ptr<_Array> __allocate_shared_unbounded_array(const _Alloc& __a, size_t __n, _Arg&& ...__arg)
1150 {
1151     static_assert(__libcpp_is_unbounded_array<_Array>::value);
1152     // We compute the number of bytes necessary to hold the control block and the
1153     // array elements. Then, we allocate an array of properly-aligned dummy structs
1154     // large enough to hold the control block and array. This allows shifting the
1155     // burden of aligning memory properly from us to the allocator.
1156     using _ControlBlock = __unbounded_array_control_block<_Array, _Alloc>;
1157     using _AlignedStorage = __sp_aligned_storage<alignof(_ControlBlock)>;
1158     using _StorageAlloc = __allocator_traits_rebind_t<_Alloc, _AlignedStorage>;
1159     __allocation_guard<_StorageAlloc> __guard(__a, _ControlBlock::__bytes_for(__n) / sizeof(_AlignedStorage));
1160     _ControlBlock* __control_block = reinterpret_cast<_ControlBlock*>(std::addressof(*__guard.__get()));
1161     std::__construct_at(__control_block, __a, __n, std::forward<_Arg>(__arg)...);
1162     __guard.__release_ptr();
1163     return shared_ptr<_Array>::__create_with_control_block(__control_block->__get_data(), __control_block);
1164 }
1165 
1166 template <class _Tp, class _Alloc>
1167 struct __bounded_array_control_block;
1168 
1169 template <class _Tp, size_t _Count, class _Alloc>
1170 struct __bounded_array_control_block<_Tp[_Count], _Alloc>
1171     : __shared_weak_count
1172 {
1173     _LIBCPP_HIDE_FROM_ABI constexpr
1174     _Tp* __get_data() noexcept { return __data_; }
1175 
1176     _LIBCPP_HIDE_FROM_ABI
1177     explicit __bounded_array_control_block(_Alloc const& __alloc, _Tp const& __arg) : __alloc_(__alloc) {
1178         std::__uninitialized_allocator_fill_n_multidimensional(__alloc_, std::addressof(__data_[0]), _Count, __arg);
1179     }
1180 
1181     _LIBCPP_HIDE_FROM_ABI
1182     explicit __bounded_array_control_block(_Alloc const& __alloc) : __alloc_(__alloc) {
1183 #if _LIBCPP_STD_VER >= 20
1184         if constexpr (is_same_v<typename _Alloc::value_type, __for_overwrite_tag>) {
1185             // We are purposefully not using an allocator-aware default construction because the spec says so.
1186             // There's currently no way of expressing default initialization in an allocator-aware manner anyway.
1187             std::uninitialized_default_construct_n(std::addressof(__data_[0]), _Count);
1188         } else {
1189             std::__uninitialized_allocator_value_construct_n_multidimensional(__alloc_, std::addressof(__data_[0]), _Count);
1190         }
1191 #else
1192         std::__uninitialized_allocator_value_construct_n_multidimensional(__alloc_, std::addressof(__data_[0]), _Count);
1193 #endif
1194     }
1195 
1196     _LIBCPP_HIDE_FROM_ABI_VIRTUAL
1197     ~__bounded_array_control_block() override { } // can't be `= default` because of the sometimes-non-trivial union member __data_
1198 
1199 private:
1200     _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override {
1201 #if _LIBCPP_STD_VER >= 20
1202         if constexpr (is_same_v<typename _Alloc::value_type, __for_overwrite_tag>) {
1203             std::__reverse_destroy(__data_, __data_ + _Count);
1204         } else {
1205             __allocator_traits_rebind_t<_Alloc, _Tp> __value_alloc(__alloc_);
1206             std::__allocator_destroy_multidimensional(__value_alloc, __data_, __data_ + _Count);
1207         }
1208 #else
1209         __allocator_traits_rebind_t<_Alloc, _Tp> __value_alloc(__alloc_);
1210         std::__allocator_destroy_multidimensional(__value_alloc, __data_, __data_ + _Count);
1211 #endif
1212     }
1213 
1214     _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared_weak() _NOEXCEPT override {
1215         using _ControlBlockAlloc = __allocator_traits_rebind_t<_Alloc, __bounded_array_control_block>;
1216         using _PointerTraits = pointer_traits<typename allocator_traits<_ControlBlockAlloc>::pointer>;
1217 
1218         _ControlBlockAlloc __tmp(__alloc_);
1219         __alloc_.~_Alloc();
1220         allocator_traits<_ControlBlockAlloc>::deallocate(__tmp, _PointerTraits::pointer_to(*this), sizeof(*this));
1221     }
1222 
1223     _LIBCPP_NO_UNIQUE_ADDRESS _Alloc __alloc_;
1224     union {
1225         _Tp __data_[_Count];
1226     };
1227 };
1228 
1229 template<class _Array, class _Alloc, class... _Arg>
1230 _LIBCPP_HIDE_FROM_ABI
1231 shared_ptr<_Array> __allocate_shared_bounded_array(const _Alloc& __a, _Arg&& ...__arg)
1232 {
1233     static_assert(__libcpp_is_bounded_array<_Array>::value);
1234     using _ControlBlock = __bounded_array_control_block<_Array, _Alloc>;
1235     using _ControlBlockAlloc = __allocator_traits_rebind_t<_Alloc, _ControlBlock>;
1236 
1237     __allocation_guard<_ControlBlockAlloc> __guard(__a, 1);
1238     _ControlBlock* __control_block = reinterpret_cast<_ControlBlock*>(std::addressof(*__guard.__get()));
1239     std::__construct_at(__control_block, __a, std::forward<_Arg>(__arg)...);
1240     __guard.__release_ptr();
1241     return shared_ptr<_Array>::__create_with_control_block(__control_block->__get_data(), __control_block);
1242 }
1243 
1244 #endif // _LIBCPP_STD_VER >= 17
1245 
1246 #if _LIBCPP_STD_VER >= 20
1247 
1248 // bounded array variants
1249 template<class _Tp, class _Alloc, class = __enable_if_t<is_bounded_array<_Tp>::value>>
1250 _LIBCPP_HIDE_FROM_ABI
1251 shared_ptr<_Tp> allocate_shared(const _Alloc& __a)
1252 {
1253     return std::__allocate_shared_bounded_array<_Tp>(__a);
1254 }
1255 
1256 template<class _Tp, class _Alloc, class = __enable_if_t<is_bounded_array<_Tp>::value>>
1257 _LIBCPP_HIDE_FROM_ABI
1258 shared_ptr<_Tp> allocate_shared(const _Alloc& __a, const remove_extent_t<_Tp>& __u)
1259 {
1260     return std::__allocate_shared_bounded_array<_Tp>(__a, __u);
1261 }
1262 
1263 template<class _Tp, class _Alloc, __enable_if_t<is_bounded_array<_Tp>::value, int> = 0>
1264 _LIBCPP_HIDE_FROM_ABI
1265 shared_ptr<_Tp> allocate_shared_for_overwrite(const _Alloc& __a)
1266 {
1267     using _ForOverwriteAllocator = __allocator_traits_rebind_t<_Alloc, __for_overwrite_tag>;
1268     _ForOverwriteAllocator __alloc(__a);
1269     return std::__allocate_shared_bounded_array<_Tp>(__alloc);
1270 }
1271 
1272 template<class _Tp, class = __enable_if_t<is_bounded_array<_Tp>::value>>
1273 _LIBCPP_HIDE_FROM_ABI
1274 shared_ptr<_Tp> make_shared()
1275 {
1276     return std::__allocate_shared_bounded_array<_Tp>(allocator<_Tp>());
1277 }
1278 
1279 template<class _Tp, class = __enable_if_t<is_bounded_array<_Tp>::value>>
1280 _LIBCPP_HIDE_FROM_ABI
1281 shared_ptr<_Tp> make_shared(const remove_extent_t<_Tp>& __u)
1282 {
1283     return std::__allocate_shared_bounded_array<_Tp>(allocator<_Tp>(), __u);
1284 }
1285 
1286 template<class _Tp, __enable_if_t<is_bounded_array<_Tp>::value, int> = 0>
1287 _LIBCPP_HIDE_FROM_ABI
1288 shared_ptr<_Tp> make_shared_for_overwrite()
1289 {
1290     return std::__allocate_shared_bounded_array<_Tp>(allocator<__for_overwrite_tag>());
1291 }
1292 
1293 // unbounded array variants
1294 template<class _Tp, class _Alloc, class = __enable_if_t<is_unbounded_array<_Tp>::value>>
1295 _LIBCPP_HIDE_FROM_ABI
1296 shared_ptr<_Tp> allocate_shared(const _Alloc& __a, size_t __n)
1297 {
1298     return std::__allocate_shared_unbounded_array<_Tp>(__a, __n);
1299 }
1300 
1301 template<class _Tp, class _Alloc, class = __enable_if_t<is_unbounded_array<_Tp>::value>>
1302 _LIBCPP_HIDE_FROM_ABI
1303 shared_ptr<_Tp> allocate_shared(const _Alloc& __a, size_t __n, const remove_extent_t<_Tp>& __u)
1304 {
1305     return std::__allocate_shared_unbounded_array<_Tp>(__a, __n, __u);
1306 }
1307 
1308 template<class _Tp, class _Alloc, __enable_if_t<is_unbounded_array<_Tp>::value, int> = 0>
1309 _LIBCPP_HIDE_FROM_ABI
1310 shared_ptr<_Tp> allocate_shared_for_overwrite(const _Alloc& __a, size_t __n)
1311 {
1312     using _ForOverwriteAllocator = __allocator_traits_rebind_t<_Alloc, __for_overwrite_tag>;
1313     _ForOverwriteAllocator __alloc(__a);
1314     return std::__allocate_shared_unbounded_array<_Tp>(__alloc, __n);
1315 }
1316 
1317 template<class _Tp, class = __enable_if_t<is_unbounded_array<_Tp>::value>>
1318 _LIBCPP_HIDE_FROM_ABI
1319 shared_ptr<_Tp> make_shared(size_t __n)
1320 {
1321     return std::__allocate_shared_unbounded_array<_Tp>(allocator<_Tp>(), __n);
1322 }
1323 
1324 template<class _Tp, class = __enable_if_t<is_unbounded_array<_Tp>::value>>
1325 _LIBCPP_HIDE_FROM_ABI
1326 shared_ptr<_Tp> make_shared(size_t __n, const remove_extent_t<_Tp>& __u)
1327 {
1328     return std::__allocate_shared_unbounded_array<_Tp>(allocator<_Tp>(), __n, __u);
1329 }
1330 
1331 template<class _Tp, __enable_if_t<is_unbounded_array<_Tp>::value, int> = 0>
1332 _LIBCPP_HIDE_FROM_ABI
1333 shared_ptr<_Tp> make_shared_for_overwrite(size_t __n)
1334 {
1335     return std::__allocate_shared_unbounded_array<_Tp>(allocator<__for_overwrite_tag>(), __n);
1336 }
1337 
1338 #endif // _LIBCPP_STD_VER >= 20
1339 
1340 template<class _Tp, class _Up>
1341 inline _LIBCPP_INLINE_VISIBILITY
1342 bool
1343 operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
1344 {
1345     return __x.get() == __y.get();
1346 }
1347 
1348 #if _LIBCPP_STD_VER <= 17
1349 
1350 template<class _Tp, class _Up>
1351 inline _LIBCPP_INLINE_VISIBILITY
1352 bool
1353 operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
1354 {
1355     return !(__x == __y);
1356 }
1357 
1358 template<class _Tp, class _Up>
1359 inline _LIBCPP_INLINE_VISIBILITY
1360 bool
1361 operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
1362 {
1363 #if _LIBCPP_STD_VER <= 11
1364     typedef typename common_type<_Tp*, _Up*>::type _Vp;
1365     return less<_Vp>()(__x.get(), __y.get());
1366 #else
1367     return less<>()(__x.get(), __y.get());
1368 #endif
1369 
1370 }
1371 
1372 template<class _Tp, class _Up>
1373 inline _LIBCPP_INLINE_VISIBILITY
1374 bool
1375 operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
1376 {
1377     return __y < __x;
1378 }
1379 
1380 template<class _Tp, class _Up>
1381 inline _LIBCPP_INLINE_VISIBILITY
1382 bool
1383 operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
1384 {
1385     return !(__y < __x);
1386 }
1387 
1388 template<class _Tp, class _Up>
1389 inline _LIBCPP_INLINE_VISIBILITY
1390 bool
1391 operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
1392 {
1393     return !(__x < __y);
1394 }
1395 
1396 #endif // _LIBCPP_STD_VER <= 17
1397 
1398 #if _LIBCPP_STD_VER >= 20
1399 template<class _Tp, class _Up>
1400 _LIBCPP_HIDE_FROM_ABI strong_ordering
1401 operator<=>(shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) noexcept
1402 {
1403     return compare_three_way()(__x.get(), __y.get());
1404 }
1405 #endif
1406 
1407 template<class _Tp>
1408 inline _LIBCPP_INLINE_VISIBILITY
1409 bool
1410 operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
1411 {
1412     return !__x;
1413 }
1414 
1415 #if _LIBCPP_STD_VER <= 17
1416 
1417 template<class _Tp>
1418 inline _LIBCPP_INLINE_VISIBILITY
1419 bool
1420 operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
1421 {
1422     return !__x;
1423 }
1424 
1425 template<class _Tp>
1426 inline _LIBCPP_INLINE_VISIBILITY
1427 bool
1428 operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
1429 {
1430     return static_cast<bool>(__x);
1431 }
1432 
1433 template<class _Tp>
1434 inline _LIBCPP_INLINE_VISIBILITY
1435 bool
1436 operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
1437 {
1438     return static_cast<bool>(__x);
1439 }
1440 
1441 template<class _Tp>
1442 inline _LIBCPP_INLINE_VISIBILITY
1443 bool
1444 operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
1445 {
1446     return less<_Tp*>()(__x.get(), nullptr);
1447 }
1448 
1449 template<class _Tp>
1450 inline _LIBCPP_INLINE_VISIBILITY
1451 bool
1452 operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
1453 {
1454     return less<_Tp*>()(nullptr, __x.get());
1455 }
1456 
1457 template<class _Tp>
1458 inline _LIBCPP_INLINE_VISIBILITY
1459 bool
1460 operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
1461 {
1462     return nullptr < __x;
1463 }
1464 
1465 template<class _Tp>
1466 inline _LIBCPP_INLINE_VISIBILITY
1467 bool
1468 operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
1469 {
1470     return __x < nullptr;
1471 }
1472 
1473 template<class _Tp>
1474 inline _LIBCPP_INLINE_VISIBILITY
1475 bool
1476 operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
1477 {
1478     return !(nullptr < __x);
1479 }
1480 
1481 template<class _Tp>
1482 inline _LIBCPP_INLINE_VISIBILITY
1483 bool
1484 operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
1485 {
1486     return !(__x < nullptr);
1487 }
1488 
1489 template<class _Tp>
1490 inline _LIBCPP_INLINE_VISIBILITY
1491 bool
1492 operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
1493 {
1494     return !(__x < nullptr);
1495 }
1496 
1497 template<class _Tp>
1498 inline _LIBCPP_INLINE_VISIBILITY
1499 bool
1500 operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
1501 {
1502     return !(nullptr < __x);
1503 }
1504 
1505 #endif // _LIBCPP_STD_VER <= 17
1506 
1507 #if _LIBCPP_STD_VER >= 20
1508 template<class _Tp>
1509 _LIBCPP_HIDE_FROM_ABI strong_ordering
1510 operator<=>(shared_ptr<_Tp> const& __x, nullptr_t) noexcept
1511 {
1512     return compare_three_way()(__x.get(), static_cast<typename shared_ptr<_Tp>::element_type*>(nullptr));
1513 }
1514 #endif
1515 
1516 template<class _Tp>
1517 inline _LIBCPP_INLINE_VISIBILITY
1518 void
1519 swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
1520 {
1521     __x.swap(__y);
1522 }
1523 
1524 template<class _Tp, class _Up>
1525 inline _LIBCPP_INLINE_VISIBILITY
1526 shared_ptr<_Tp>
1527 static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
1528 {
1529     return shared_ptr<_Tp>(__r,
1530                            static_cast<
1531                                typename shared_ptr<_Tp>::element_type*>(__r.get()));
1532 }
1533 
1534 // LWG-2996
1535 // We don't backport because it is an evolutionary change.
1536 #if _LIBCPP_STD_VER >= 20
1537 template <class _Tp, class _Up>
1538 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> static_pointer_cast(shared_ptr<_Up>&& __r) noexcept {
1539   return shared_ptr<_Tp>(std::move(__r), static_cast<typename shared_ptr<_Tp>::element_type*>(__r.get()));
1540 }
1541 #endif
1542 
1543 template<class _Tp, class _Up>
1544 inline _LIBCPP_INLINE_VISIBILITY
1545 shared_ptr<_Tp>
1546 dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
1547 {
1548     typedef typename shared_ptr<_Tp>::element_type _ET;
1549     _ET* __p = dynamic_cast<_ET*>(__r.get());
1550     return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
1551 }
1552 
1553 // LWG-2996
1554 // We don't backport because it is an evolutionary change.
1555 #if _LIBCPP_STD_VER >= 20
1556 template <class _Tp, class _Up>
1557 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> dynamic_pointer_cast(shared_ptr<_Up>&& __r) noexcept {
1558   auto* __p = dynamic_cast<typename shared_ptr<_Tp>::element_type*>(__r.get());
1559   return __p ? shared_ptr<_Tp>(std::move(__r), __p) : shared_ptr<_Tp>();
1560 }
1561 #endif
1562 
1563 template<class _Tp, class _Up>
1564 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp>
1565 const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
1566 {
1567     typedef typename shared_ptr<_Tp>::element_type _RTp;
1568     return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
1569 }
1570 
1571 // LWG-2996
1572 // We don't backport because it is an evolutionary change.
1573 #if _LIBCPP_STD_VER >= 20
1574 template <class _Tp, class _Up>
1575 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> const_pointer_cast(shared_ptr<_Up>&& __r) noexcept {
1576   return shared_ptr<_Tp>(std::move(__r), const_cast<typename shared_ptr<_Tp>::element_type*>(__r.get()));
1577 }
1578 #endif
1579 
1580 template<class _Tp, class _Up>
1581 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp>
1582 reinterpret_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
1583 {
1584     return shared_ptr<_Tp>(__r,
1585                            reinterpret_cast<
1586                                typename shared_ptr<_Tp>::element_type*>(__r.get()));
1587 }
1588 
1589 // LWG-2996
1590 // We don't backport because it is an evolutionary change.
1591 #if _LIBCPP_STD_VER >= 20
1592 template <class _Tp, class _Up>
1593 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> reinterpret_pointer_cast(shared_ptr<_Up>&& __r) noexcept {
1594   return shared_ptr<_Tp>(std::move(__r), reinterpret_cast<typename shared_ptr<_Tp>::element_type*>(__r.get()));
1595 }
1596 #endif
1597 
1598 #ifndef _LIBCPP_HAS_NO_RTTI
1599 
1600 template<class _Dp, class _Tp>
1601 inline _LIBCPP_INLINE_VISIBILITY
1602 _Dp*
1603 get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
1604 {
1605     return __p.template __get_deleter<_Dp>();
1606 }
1607 
1608 #endif // _LIBCPP_HAS_NO_RTTI
1609 
1610 template<class _Tp>
1611 class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS weak_ptr
1612 {
1613 public:
1614 #if _LIBCPP_STD_VER >= 17
1615     typedef remove_extent_t<_Tp> element_type;
1616 #else
1617     typedef _Tp element_type;
1618 #endif
1619 
1620 private:
1621     element_type*        __ptr_;
1622     __shared_weak_count* __cntrl_;
1623 
1624 public:
1625     _LIBCPP_INLINE_VISIBILITY
1626     _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
1627     template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
1628                    typename enable_if<__compatible_with<_Yp, _Tp>::value, __nat*>::type = 0)
1629                         _NOEXCEPT;
1630     _LIBCPP_INLINE_VISIBILITY
1631     weak_ptr(weak_ptr const& __r) _NOEXCEPT;
1632     template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
1633                    typename enable_if<__compatible_with<_Yp, _Tp>::value, __nat*>::type = 0)
1634                          _NOEXCEPT;
1635 
1636     _LIBCPP_INLINE_VISIBILITY
1637     weak_ptr(weak_ptr&& __r) _NOEXCEPT;
1638     template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
1639                    typename enable_if<__compatible_with<_Yp, _Tp>::value, __nat*>::type = 0)
1640                          _NOEXCEPT;
1641     _LIBCPP_HIDE_FROM_ABI ~weak_ptr();
1642 
1643     _LIBCPP_INLINE_VISIBILITY
1644     weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
1645     template<class _Yp>
1646         typename enable_if
1647         <
1648             __compatible_with<_Yp, _Tp>::value,
1649             weak_ptr&
1650         >::type
1651         _LIBCPP_INLINE_VISIBILITY
1652         operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
1653 
1654     _LIBCPP_INLINE_VISIBILITY
1655     weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
1656     template<class _Yp>
1657         typename enable_if
1658         <
1659             __compatible_with<_Yp, _Tp>::value,
1660             weak_ptr&
1661         >::type
1662         _LIBCPP_INLINE_VISIBILITY
1663         operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
1664 
1665     template<class _Yp>
1666         typename enable_if
1667         <
1668             __compatible_with<_Yp, _Tp>::value,
1669             weak_ptr&
1670         >::type
1671         _LIBCPP_INLINE_VISIBILITY
1672         operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
1673 
1674     _LIBCPP_INLINE_VISIBILITY
1675     void swap(weak_ptr& __r) _NOEXCEPT;
1676     _LIBCPP_INLINE_VISIBILITY
1677     void reset() _NOEXCEPT;
1678 
1679     _LIBCPP_INLINE_VISIBILITY
1680     long use_count() const _NOEXCEPT
1681         {return __cntrl_ ? __cntrl_->use_count() : 0;}
1682     _LIBCPP_INLINE_VISIBILITY
1683     bool expired() const _NOEXCEPT
1684         {return __cntrl_ == nullptr || __cntrl_->use_count() == 0;}
1685     _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> lock() const _NOEXCEPT;
1686     template<class _Up>
1687         _LIBCPP_INLINE_VISIBILITY
1688         bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
1689         {return __cntrl_ < __r.__cntrl_;}
1690     template<class _Up>
1691         _LIBCPP_INLINE_VISIBILITY
1692         bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
1693         {return __cntrl_ < __r.__cntrl_;}
1694 
1695     template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
1696     template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
1697 };
1698 
1699 #if _LIBCPP_STD_VER >= 17
1700 template<class _Tp>
1701 weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>;
1702 #endif
1703 
1704 template<class _Tp>
1705 inline
1706 _LIBCPP_CONSTEXPR
1707 weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
1708     : __ptr_(nullptr),
1709       __cntrl_(nullptr)
1710 {
1711 }
1712 
1713 template<class _Tp>
1714 inline
1715 weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
1716     : __ptr_(__r.__ptr_),
1717       __cntrl_(__r.__cntrl_)
1718 {
1719     if (__cntrl_)
1720         __cntrl_->__add_weak();
1721 }
1722 
1723 template<class _Tp>
1724 template<class _Yp>
1725 inline
1726 weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
1727                         typename enable_if<__compatible_with<_Yp, _Tp>::value, __nat*>::type)
1728                          _NOEXCEPT
1729     : __ptr_(__r.__ptr_),
1730       __cntrl_(__r.__cntrl_)
1731 {
1732     if (__cntrl_)
1733         __cntrl_->__add_weak();
1734 }
1735 
1736 template<class _Tp>
1737 template<class _Yp>
1738 inline
1739 weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
1740                         typename enable_if<__compatible_with<_Yp, _Tp>::value, __nat*>::type)
1741          _NOEXCEPT
1742     : __ptr_(__r.__ptr_),
1743       __cntrl_(__r.__cntrl_)
1744 {
1745     if (__cntrl_)
1746         __cntrl_->__add_weak();
1747 }
1748 
1749 template<class _Tp>
1750 inline
1751 weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
1752     : __ptr_(__r.__ptr_),
1753       __cntrl_(__r.__cntrl_)
1754 {
1755     __r.__ptr_ = nullptr;
1756     __r.__cntrl_ = nullptr;
1757 }
1758 
1759 template<class _Tp>
1760 template<class _Yp>
1761 inline
1762 weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
1763                         typename enable_if<__compatible_with<_Yp, _Tp>::value, __nat*>::type)
1764          _NOEXCEPT
1765     : __ptr_(__r.__ptr_),
1766       __cntrl_(__r.__cntrl_)
1767 {
1768     __r.__ptr_ = nullptr;
1769     __r.__cntrl_ = nullptr;
1770 }
1771 
1772 template<class _Tp>
1773 weak_ptr<_Tp>::~weak_ptr()
1774 {
1775     if (__cntrl_)
1776         __cntrl_->__release_weak();
1777 }
1778 
1779 template<class _Tp>
1780 inline
1781 weak_ptr<_Tp>&
1782 weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
1783 {
1784     weak_ptr(__r).swap(*this);
1785     return *this;
1786 }
1787 
1788 template<class _Tp>
1789 template<class _Yp>
1790 inline
1791 typename enable_if
1792 <
1793     __compatible_with<_Yp, _Tp>::value,
1794     weak_ptr<_Tp>&
1795 >::type
1796 weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
1797 {
1798     weak_ptr(__r).swap(*this);
1799     return *this;
1800 }
1801 
1802 template<class _Tp>
1803 inline
1804 weak_ptr<_Tp>&
1805 weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
1806 {
1807     weak_ptr(_VSTD::move(__r)).swap(*this);
1808     return *this;
1809 }
1810 
1811 template<class _Tp>
1812 template<class _Yp>
1813 inline
1814 typename enable_if
1815 <
1816     __compatible_with<_Yp, _Tp>::value,
1817     weak_ptr<_Tp>&
1818 >::type
1819 weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
1820 {
1821     weak_ptr(_VSTD::move(__r)).swap(*this);
1822     return *this;
1823 }
1824 
1825 template<class _Tp>
1826 template<class _Yp>
1827 inline
1828 typename enable_if
1829 <
1830     __compatible_with<_Yp, _Tp>::value,
1831     weak_ptr<_Tp>&
1832 >::type
1833 weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
1834 {
1835     weak_ptr(__r).swap(*this);
1836     return *this;
1837 }
1838 
1839 template<class _Tp>
1840 inline
1841 void
1842 weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
1843 {
1844     _VSTD::swap(__ptr_, __r.__ptr_);
1845     _VSTD::swap(__cntrl_, __r.__cntrl_);
1846 }
1847 
1848 template<class _Tp>
1849 inline _LIBCPP_INLINE_VISIBILITY
1850 void
1851 swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
1852 {
1853     __x.swap(__y);
1854 }
1855 
1856 template<class _Tp>
1857 inline
1858 void
1859 weak_ptr<_Tp>::reset() _NOEXCEPT
1860 {
1861     weak_ptr().swap(*this);
1862 }
1863 
1864 template<class _Tp>
1865 shared_ptr<_Tp>
1866 weak_ptr<_Tp>::lock() const _NOEXCEPT
1867 {
1868     shared_ptr<_Tp> __r;
1869     __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
1870     if (__r.__cntrl_)
1871         __r.__ptr_ = __ptr_;
1872     return __r;
1873 }
1874 
1875 #if _LIBCPP_STD_VER >= 17
1876 template <class _Tp = void> struct owner_less;
1877 #else
1878 template <class _Tp> struct owner_less;
1879 #endif
1880 
1881 
1882 template <class _Tp>
1883 struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
1884     : __binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
1885 {
1886     _LIBCPP_INLINE_VISIBILITY
1887     bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
1888         {return __x.owner_before(__y);}
1889     _LIBCPP_INLINE_VISIBILITY
1890     bool operator()(shared_ptr<_Tp> const& __x,   weak_ptr<_Tp> const& __y) const _NOEXCEPT
1891         {return __x.owner_before(__y);}
1892     _LIBCPP_INLINE_VISIBILITY
1893     bool operator()(  weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
1894         {return __x.owner_before(__y);}
1895 };
1896 
1897 template <class _Tp>
1898 struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
1899     : __binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
1900 {
1901     _LIBCPP_INLINE_VISIBILITY
1902     bool operator()(  weak_ptr<_Tp> const& __x,   weak_ptr<_Tp> const& __y) const _NOEXCEPT
1903         {return __x.owner_before(__y);}
1904     _LIBCPP_INLINE_VISIBILITY
1905     bool operator()(shared_ptr<_Tp> const& __x,   weak_ptr<_Tp> const& __y) const _NOEXCEPT
1906         {return __x.owner_before(__y);}
1907     _LIBCPP_INLINE_VISIBILITY
1908     bool operator()(  weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
1909         {return __x.owner_before(__y);}
1910 };
1911 
1912 #if _LIBCPP_STD_VER >= 17
1913 template <>
1914 struct _LIBCPP_TEMPLATE_VIS owner_less<void>
1915 {
1916     template <class _Tp, class _Up>
1917     _LIBCPP_INLINE_VISIBILITY
1918     bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
1919         {return __x.owner_before(__y);}
1920     template <class _Tp, class _Up>
1921     _LIBCPP_INLINE_VISIBILITY
1922     bool operator()( shared_ptr<_Tp> const& __x,   weak_ptr<_Up> const& __y) const _NOEXCEPT
1923         {return __x.owner_before(__y);}
1924     template <class _Tp, class _Up>
1925     _LIBCPP_INLINE_VISIBILITY
1926     bool operator()(   weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
1927         {return __x.owner_before(__y);}
1928     template <class _Tp, class _Up>
1929     _LIBCPP_INLINE_VISIBILITY
1930     bool operator()(   weak_ptr<_Tp> const& __x,   weak_ptr<_Up> const& __y) const _NOEXCEPT
1931         {return __x.owner_before(__y);}
1932     typedef void is_transparent;
1933 };
1934 #endif
1935 
1936 template<class _Tp>
1937 class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
1938 {
1939     mutable weak_ptr<_Tp> __weak_this_;
1940 protected:
1941     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1942     enable_shared_from_this() _NOEXCEPT {}
1943     _LIBCPP_INLINE_VISIBILITY
1944     enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
1945     _LIBCPP_INLINE_VISIBILITY
1946     enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
1947         {return *this;}
1948     _LIBCPP_INLINE_VISIBILITY
1949     ~enable_shared_from_this() {}
1950 public:
1951     _LIBCPP_INLINE_VISIBILITY
1952     shared_ptr<_Tp> shared_from_this()
1953         {return shared_ptr<_Tp>(__weak_this_);}
1954     _LIBCPP_INLINE_VISIBILITY
1955     shared_ptr<_Tp const> shared_from_this() const
1956         {return shared_ptr<const _Tp>(__weak_this_);}
1957 
1958 #if _LIBCPP_STD_VER >= 17
1959     _LIBCPP_INLINE_VISIBILITY
1960     weak_ptr<_Tp> weak_from_this() _NOEXCEPT
1961        { return __weak_this_; }
1962 
1963     _LIBCPP_INLINE_VISIBILITY
1964     weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
1965         { return __weak_this_; }
1966 #endif // _LIBCPP_STD_VER >= 17
1967 
1968     template <class _Up> friend class shared_ptr;
1969 };
1970 
1971 template <class _Tp> struct _LIBCPP_TEMPLATE_VIS hash;
1972 
1973 template <class _Tp>
1974 struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
1975 {
1976 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
1977     _LIBCPP_DEPRECATED_IN_CXX17 typedef shared_ptr<_Tp> argument_type;
1978     _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t          result_type;
1979 #endif
1980 
1981     _LIBCPP_INLINE_VISIBILITY
1982     size_t operator()(const shared_ptr<_Tp>& __ptr) const _NOEXCEPT
1983     {
1984         return hash<typename shared_ptr<_Tp>::element_type*>()(__ptr.get());
1985     }
1986 };
1987 
1988 template<class _CharT, class _Traits, class _Yp>
1989 inline _LIBCPP_INLINE_VISIBILITY
1990 basic_ostream<_CharT, _Traits>&
1991 operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
1992 
1993 
1994 #if !defined(_LIBCPP_HAS_NO_THREADS)
1995 
1996 class _LIBCPP_EXPORTED_FROM_ABI __sp_mut
1997 {
1998     void* __lx_;
1999 public:
2000     void lock() _NOEXCEPT;
2001     void unlock() _NOEXCEPT;
2002 
2003 private:
2004     _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
2005     __sp_mut(const __sp_mut&);
2006     __sp_mut& operator=(const __sp_mut&);
2007 
2008     friend _LIBCPP_EXPORTED_FROM_ABI __sp_mut& __get_sp_mut(const void*);
2009 };
2010 
2011 _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
2012 __sp_mut& __get_sp_mut(const void*);
2013 
2014 template <class _Tp>
2015 inline _LIBCPP_INLINE_VISIBILITY
2016 bool
2017 atomic_is_lock_free(const shared_ptr<_Tp>*)
2018 {
2019     return false;
2020 }
2021 
2022 template <class _Tp>
2023 _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
2024 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp>
2025 atomic_load(const shared_ptr<_Tp>* __p)
2026 {
2027     __sp_mut& __m = std::__get_sp_mut(__p);
2028     __m.lock();
2029     shared_ptr<_Tp> __q = *__p;
2030     __m.unlock();
2031     return __q;
2032 }
2033 
2034 template <class _Tp>
2035 inline _LIBCPP_INLINE_VISIBILITY
2036 _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
2037 shared_ptr<_Tp>
2038 atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
2039 {
2040     return std::atomic_load(__p);
2041 }
2042 
2043 template <class _Tp>
2044 _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
2045 _LIBCPP_HIDE_FROM_ABI void
2046 atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
2047 {
2048     __sp_mut& __m = std::__get_sp_mut(__p);
2049     __m.lock();
2050     __p->swap(__r);
2051     __m.unlock();
2052 }
2053 
2054 template <class _Tp>
2055 inline _LIBCPP_INLINE_VISIBILITY
2056 _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
2057 void
2058 atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
2059 {
2060     std::atomic_store(__p, __r);
2061 }
2062 
2063 template <class _Tp>
2064 _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
2065 _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp>
2066 atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
2067 {
2068     __sp_mut& __m = std::__get_sp_mut(__p);
2069     __m.lock();
2070     __p->swap(__r);
2071     __m.unlock();
2072     return __r;
2073 }
2074 
2075 template <class _Tp>
2076 inline _LIBCPP_INLINE_VISIBILITY
2077 _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
2078 shared_ptr<_Tp>
2079 atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
2080 {
2081     return std::atomic_exchange(__p, __r);
2082 }
2083 
2084 template <class _Tp>
2085 _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
2086 _LIBCPP_HIDE_FROM_ABI bool
2087 atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
2088 {
2089     shared_ptr<_Tp> __temp;
2090     __sp_mut& __m = std::__get_sp_mut(__p);
2091     __m.lock();
2092     if (__p->__owner_equivalent(*__v))
2093     {
2094         _VSTD::swap(__temp, *__p);
2095         *__p = __w;
2096         __m.unlock();
2097         return true;
2098     }
2099     _VSTD::swap(__temp, *__v);
2100     *__v = *__p;
2101     __m.unlock();
2102     return false;
2103 }
2104 
2105 template <class _Tp>
2106 inline _LIBCPP_INLINE_VISIBILITY
2107 _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
2108 bool
2109 atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
2110 {
2111     return std::atomic_compare_exchange_strong(__p, __v, __w);
2112 }
2113 
2114 template <class _Tp>
2115 inline _LIBCPP_INLINE_VISIBILITY
2116 _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
2117 bool
2118 atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
2119                                         shared_ptr<_Tp> __w, memory_order, memory_order)
2120 {
2121     return std::atomic_compare_exchange_strong(__p, __v, __w);
2122 }
2123 
2124 template <class _Tp>
2125 inline _LIBCPP_INLINE_VISIBILITY
2126 _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
2127 bool
2128 atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
2129                                       shared_ptr<_Tp> __w, memory_order, memory_order)
2130 {
2131     return std::atomic_compare_exchange_weak(__p, __v, __w);
2132 }
2133 
2134 #endif // !defined(_LIBCPP_HAS_NO_THREADS)
2135 
2136 _LIBCPP_END_NAMESPACE_STD
2137 
2138 #endif // _LIBCPP___MEMORY_SHARED_PTR_H
2139