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