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