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