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