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