]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/shared_ptr_base.h
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / shared_ptr_base.h
1 // shared_ptr and weak_ptr implementation details -*- C++ -*-
2
3 // Copyright (C) 2007-2019 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24
25 // GCC Note: Based on files from version 1.32.0 of the Boost library.
26
27 // shared_count.hpp
28 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
29
30 // shared_ptr.hpp
31 // Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes.
32 // Copyright (C) 2001, 2002, 2003 Peter Dimov
33
34 // weak_ptr.hpp
35 // Copyright (C) 2001, 2002, 2003 Peter Dimov
36
37 // enable_shared_from_this.hpp
38 // Copyright (C) 2002 Peter Dimov
39
40 // Distributed under the Boost Software License, Version 1.0. (See
41 // accompanying file LICENSE_1_0.txt or copy at
42 // http://www.boost.org/LICENSE_1_0.txt)
43
44 /** @file bits/shared_ptr_base.h
45 * This is an internal header file, included by other library headers.
46 * Do not attempt to use it directly. @headername{memory}
47 */
48
49 #ifndef _SHARED_PTR_BASE_H
50 #define _SHARED_PTR_BASE_H 1
51
52 #include <typeinfo>
53 #include <bits/allocated_ptr.h>
54 #include <bits/refwrap.h>
55 #include <bits/stl_function.h>
56 #include <ext/aligned_buffer.h>
57
58 namespace std _GLIBCXX_VISIBILITY(default)
59 {
60 _GLIBCXX_BEGIN_NAMESPACE_VERSION
61
62 #if _GLIBCXX_USE_DEPRECATED
63 #pragma GCC diagnostic push
64 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
65 template<typename> class auto_ptr;
66 #pragma GCC diagnostic pop
67 #endif
68
69 /**
70 * @brief Exception possibly thrown by @c shared_ptr.
71 * @ingroup exceptions
72 */
73 class bad_weak_ptr : public std::exception
74 {
75 public:
76 virtual char const* what() const noexcept;
77
78 virtual ~bad_weak_ptr() noexcept;
79 };
80
81 // Substitute for bad_weak_ptr object in the case of -fno-exceptions.
82 inline void
83 __throw_bad_weak_ptr()
84 { _GLIBCXX_THROW_OR_ABORT(bad_weak_ptr()); }
85
86 using __gnu_cxx::_Lock_policy;
87 using __gnu_cxx::__default_lock_policy;
88 using __gnu_cxx::_S_single;
89 using __gnu_cxx::_S_mutex;
90 using __gnu_cxx::_S_atomic;
91
92 // Empty helper class except when the template argument is _S_mutex.
93 template<_Lock_policy _Lp>
94 class _Mutex_base
95 {
96 protected:
97 // The atomic policy uses fully-fenced builtins, single doesn't care.
98 enum { _S_need_barriers = 0 };
99 };
100
101 template<>
102 class _Mutex_base<_S_mutex>
103 : public __gnu_cxx::__mutex
104 {
105 protected:
106 // This policy is used when atomic builtins are not available.
107 // The replacement atomic operations might not have the necessary
108 // memory barriers.
109 enum { _S_need_barriers = 1 };
110 };
111
112 template<_Lock_policy _Lp = __default_lock_policy>
113 class _Sp_counted_base
114 : public _Mutex_base<_Lp>
115 {
116 public:
117 _Sp_counted_base() noexcept
118 : _M_use_count(1), _M_weak_count(1) { }
119
120 virtual
121 ~_Sp_counted_base() noexcept
122 { }
123
124 // Called when _M_use_count drops to zero, to release the resources
125 // managed by *this.
126 virtual void
127 _M_dispose() noexcept = 0;
128
129 // Called when _M_weak_count drops to zero.
130 virtual void
131 _M_destroy() noexcept
132 { delete this; }
133
134 virtual void*
135 _M_get_deleter(const std::type_info&) noexcept = 0;
136
137 void
138 _M_add_ref_copy()
139 { __gnu_cxx::__atomic_add_dispatch(&_M_use_count, 1); }
140
141 void
142 _M_add_ref_lock();
143
144 bool
145 _M_add_ref_lock_nothrow();
146
147 void
148 _M_release() noexcept
149 {
150 // Be race-detector-friendly. For more info see bits/c++config.
151 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count);
152 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
153 {
154 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count);
155 _M_dispose();
156 // There must be a memory barrier between dispose() and destroy()
157 // to ensure that the effects of dispose() are observed in the
158 // thread that runs destroy().
159 // See http://gcc.gnu.org/ml/libstdc++/2005-11/msg00136.html
160 if (_Mutex_base<_Lp>::_S_need_barriers)
161 {
162 __atomic_thread_fence (__ATOMIC_ACQ_REL);
163 }
164
165 // Be race-detector-friendly. For more info see bits/c++config.
166 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
167 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count,
168 -1) == 1)
169 {
170 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
171 _M_destroy();
172 }
173 }
174 }
175
176 void
177 _M_weak_add_ref() noexcept
178 { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count, 1); }
179
180 void
181 _M_weak_release() noexcept
182 {
183 // Be race-detector-friendly. For more info see bits/c++config.
184 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
185 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, -1) == 1)
186 {
187 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
188 if (_Mutex_base<_Lp>::_S_need_barriers)
189 {
190 // See _M_release(),
191 // destroy() must observe results of dispose()
192 __atomic_thread_fence (__ATOMIC_ACQ_REL);
193 }
194 _M_destroy();
195 }
196 }
197
198 long
199 _M_get_use_count() const noexcept
200 {
201 // No memory barrier is used here so there is no synchronization
202 // with other threads.
203 return __atomic_load_n(&_M_use_count, __ATOMIC_RELAXED);
204 }
205
206 private:
207 _Sp_counted_base(_Sp_counted_base const&) = delete;
208 _Sp_counted_base& operator=(_Sp_counted_base const&) = delete;
209
210 _Atomic_word _M_use_count; // #shared
211 _Atomic_word _M_weak_count; // #weak + (#shared != 0)
212 };
213
214 template<>
215 inline void
216 _Sp_counted_base<_S_single>::
217 _M_add_ref_lock()
218 {
219 if (_M_use_count == 0)
220 __throw_bad_weak_ptr();
221 ++_M_use_count;
222 }
223
224 template<>
225 inline void
226 _Sp_counted_base<_S_mutex>::
227 _M_add_ref_lock()
228 {
229 __gnu_cxx::__scoped_lock sentry(*this);
230 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
231 {
232 _M_use_count = 0;
233 __throw_bad_weak_ptr();
234 }
235 }
236
237 template<>
238 inline void
239 _Sp_counted_base<_S_atomic>::
240 _M_add_ref_lock()
241 {
242 // Perform lock-free add-if-not-zero operation.
243 _Atomic_word __count = _M_get_use_count();
244 do
245 {
246 if (__count == 0)
247 __throw_bad_weak_ptr();
248 // Replace the current counter value with the old value + 1, as
249 // long as it's not changed meanwhile.
250 }
251 while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1,
252 true, __ATOMIC_ACQ_REL,
253 __ATOMIC_RELAXED));
254 }
255
256 template<>
257 inline bool
258 _Sp_counted_base<_S_single>::
259 _M_add_ref_lock_nothrow()
260 {
261 if (_M_use_count == 0)
262 return false;
263 ++_M_use_count;
264 return true;
265 }
266
267 template<>
268 inline bool
269 _Sp_counted_base<_S_mutex>::
270 _M_add_ref_lock_nothrow()
271 {
272 __gnu_cxx::__scoped_lock sentry(*this);
273 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
274 {
275 _M_use_count = 0;
276 return false;
277 }
278 return true;
279 }
280
281 template<>
282 inline bool
283 _Sp_counted_base<_S_atomic>::
284 _M_add_ref_lock_nothrow()
285 {
286 // Perform lock-free add-if-not-zero operation.
287 _Atomic_word __count = _M_get_use_count();
288 do
289 {
290 if (__count == 0)
291 return false;
292 // Replace the current counter value with the old value + 1, as
293 // long as it's not changed meanwhile.
294 }
295 while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1,
296 true, __ATOMIC_ACQ_REL,
297 __ATOMIC_RELAXED));
298 return true;
299 }
300
301 template<>
302 inline void
303 _Sp_counted_base<_S_single>::_M_add_ref_copy()
304 { ++_M_use_count; }
305
306 template<>
307 inline void
308 _Sp_counted_base<_S_single>::_M_release() noexcept
309 {
310 if (--_M_use_count == 0)
311 {
312 _M_dispose();
313 if (--_M_weak_count == 0)
314 _M_destroy();
315 }
316 }
317
318 template<>
319 inline void
320 _Sp_counted_base<_S_single>::_M_weak_add_ref() noexcept
321 { ++_M_weak_count; }
322
323 template<>
324 inline void
325 _Sp_counted_base<_S_single>::_M_weak_release() noexcept
326 {
327 if (--_M_weak_count == 0)
328 _M_destroy();
329 }
330
331 template<>
332 inline long
333 _Sp_counted_base<_S_single>::_M_get_use_count() const noexcept
334 { return _M_use_count; }
335
336
337 // Forward declarations.
338 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
339 class __shared_ptr;
340
341 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
342 class __weak_ptr;
343
344 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
345 class __enable_shared_from_this;
346
347 template<typename _Tp>
348 class shared_ptr;
349
350 template<typename _Tp>
351 class weak_ptr;
352
353 template<typename _Tp>
354 struct owner_less;
355
356 template<typename _Tp>
357 class enable_shared_from_this;
358
359 template<_Lock_policy _Lp = __default_lock_policy>
360 class __weak_count;
361
362 template<_Lock_policy _Lp = __default_lock_policy>
363 class __shared_count;
364
365
366 // Counted ptr with no deleter or allocator support
367 template<typename _Ptr, _Lock_policy _Lp>
368 class _Sp_counted_ptr final : public _Sp_counted_base<_Lp>
369 {
370 public:
371 explicit
372 _Sp_counted_ptr(_Ptr __p) noexcept
373 : _M_ptr(__p) { }
374
375 virtual void
376 _M_dispose() noexcept
377 { delete _M_ptr; }
378
379 virtual void
380 _M_destroy() noexcept
381 { delete this; }
382
383 virtual void*
384 _M_get_deleter(const std::type_info&) noexcept
385 { return nullptr; }
386
387 _Sp_counted_ptr(const _Sp_counted_ptr&) = delete;
388 _Sp_counted_ptr& operator=(const _Sp_counted_ptr&) = delete;
389
390 private:
391 _Ptr _M_ptr;
392 };
393
394 template<>
395 inline void
396 _Sp_counted_ptr<nullptr_t, _S_single>::_M_dispose() noexcept { }
397
398 template<>
399 inline void
400 _Sp_counted_ptr<nullptr_t, _S_mutex>::_M_dispose() noexcept { }
401
402 template<>
403 inline void
404 _Sp_counted_ptr<nullptr_t, _S_atomic>::_M_dispose() noexcept { }
405
406 template<int _Nm, typename _Tp,
407 bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)>
408 struct _Sp_ebo_helper;
409
410 /// Specialization using EBO.
411 template<int _Nm, typename _Tp>
412 struct _Sp_ebo_helper<_Nm, _Tp, true> : private _Tp
413 {
414 explicit _Sp_ebo_helper(const _Tp& __tp) : _Tp(__tp) { }
415 explicit _Sp_ebo_helper(_Tp&& __tp) : _Tp(std::move(__tp)) { }
416
417 static _Tp&
418 _S_get(_Sp_ebo_helper& __eboh) { return static_cast<_Tp&>(__eboh); }
419 };
420
421 /// Specialization not using EBO.
422 template<int _Nm, typename _Tp>
423 struct _Sp_ebo_helper<_Nm, _Tp, false>
424 {
425 explicit _Sp_ebo_helper(const _Tp& __tp) : _M_tp(__tp) { }
426 explicit _Sp_ebo_helper(_Tp&& __tp) : _M_tp(std::move(__tp)) { }
427
428 static _Tp&
429 _S_get(_Sp_ebo_helper& __eboh)
430 { return __eboh._M_tp; }
431
432 private:
433 _Tp _M_tp;
434 };
435
436 // Support for custom deleter and/or allocator
437 template<typename _Ptr, typename _Deleter, typename _Alloc, _Lock_policy _Lp>
438 class _Sp_counted_deleter final : public _Sp_counted_base<_Lp>
439 {
440 class _Impl : _Sp_ebo_helper<0, _Deleter>, _Sp_ebo_helper<1, _Alloc>
441 {
442 typedef _Sp_ebo_helper<0, _Deleter> _Del_base;
443 typedef _Sp_ebo_helper<1, _Alloc> _Alloc_base;
444
445 public:
446 _Impl(_Ptr __p, _Deleter __d, const _Alloc& __a) noexcept
447 : _M_ptr(__p), _Del_base(std::move(__d)), _Alloc_base(__a)
448 { }
449
450 _Deleter& _M_del() noexcept { return _Del_base::_S_get(*this); }
451 _Alloc& _M_alloc() noexcept { return _Alloc_base::_S_get(*this); }
452
453 _Ptr _M_ptr;
454 };
455
456 public:
457 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_deleter>;
458
459 // __d(__p) must not throw.
460 _Sp_counted_deleter(_Ptr __p, _Deleter __d) noexcept
461 : _M_impl(__p, std::move(__d), _Alloc()) { }
462
463 // __d(__p) must not throw.
464 _Sp_counted_deleter(_Ptr __p, _Deleter __d, const _Alloc& __a) noexcept
465 : _M_impl(__p, std::move(__d), __a) { }
466
467 ~_Sp_counted_deleter() noexcept { }
468
469 virtual void
470 _M_dispose() noexcept
471 { _M_impl._M_del()(_M_impl._M_ptr); }
472
473 virtual void
474 _M_destroy() noexcept
475 {
476 __allocator_type __a(_M_impl._M_alloc());
477 __allocated_ptr<__allocator_type> __guard_ptr{ __a, this };
478 this->~_Sp_counted_deleter();
479 }
480
481 virtual void*
482 _M_get_deleter(const std::type_info& __ti) noexcept
483 {
484 #if __cpp_rtti
485 // _GLIBCXX_RESOLVE_LIB_DEFECTS
486 // 2400. shared_ptr's get_deleter() should use addressof()
487 return __ti == typeid(_Deleter)
488 ? std::__addressof(_M_impl._M_del())
489 : nullptr;
490 #else
491 return nullptr;
492 #endif
493 }
494
495 private:
496 _Impl _M_impl;
497 };
498
499 // helpers for make_shared / allocate_shared
500
501 struct _Sp_make_shared_tag
502 {
503 private:
504 template<typename _Tp, _Lock_policy _Lp>
505 friend class __shared_ptr;
506 template<typename _Tp, typename _Alloc, _Lock_policy _Lp>
507 friend class _Sp_counted_ptr_inplace;
508
509 static const type_info&
510 _S_ti() noexcept _GLIBCXX_VISIBILITY(default)
511 {
512 alignas(type_info) static constexpr char __tag[sizeof(type_info)] = { };
513 return reinterpret_cast<const type_info&>(__tag);
514 }
515 };
516
517 template<typename _Tp, typename _Alloc, _Lock_policy _Lp>
518 class _Sp_counted_ptr_inplace final : public _Sp_counted_base<_Lp>
519 {
520 class _Impl : _Sp_ebo_helper<0, _Alloc>
521 {
522 typedef _Sp_ebo_helper<0, _Alloc> _A_base;
523
524 public:
525 explicit _Impl(_Alloc __a) noexcept : _A_base(__a) { }
526
527 _Alloc& _M_alloc() noexcept { return _A_base::_S_get(*this); }
528
529 __gnu_cxx::__aligned_buffer<_Tp> _M_storage;
530 };
531
532 public:
533 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_ptr_inplace>;
534
535 // Alloc parameter is not a reference so doesn't alias anything in __args
536 template<typename... _Args>
537 _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
538 : _M_impl(__a)
539 {
540 // _GLIBCXX_RESOLVE_LIB_DEFECTS
541 // 2070. allocate_shared should use allocator_traits<A>::construct
542 allocator_traits<_Alloc>::construct(__a, _M_ptr(),
543 std::forward<_Args>(__args)...); // might throw
544 }
545
546 ~_Sp_counted_ptr_inplace() noexcept { }
547
548 virtual void
549 _M_dispose() noexcept
550 {
551 allocator_traits<_Alloc>::destroy(_M_impl._M_alloc(), _M_ptr());
552 }
553
554 // Override because the allocator needs to know the dynamic type
555 virtual void
556 _M_destroy() noexcept
557 {
558 __allocator_type __a(_M_impl._M_alloc());
559 __allocated_ptr<__allocator_type> __guard_ptr{ __a, this };
560 this->~_Sp_counted_ptr_inplace();
561 }
562
563 // Sneaky trick so __shared_ptr can get the managed pointer.
564 virtual void*
565 _M_get_deleter(const std::type_info& __ti) noexcept override
566 {
567 // Check for the fake type_info first, so we don't try to access it
568 // as a real type_info object.
569 if (&__ti == &_Sp_make_shared_tag::_S_ti())
570 return const_cast<typename remove_cv<_Tp>::type*>(_M_ptr());
571 #if __cpp_rtti
572 // Callers compiled with old libstdc++ headers and RTTI enabled
573 // might pass this instead:
574 else if (__ti == typeid(_Sp_make_shared_tag))
575 return const_cast<typename remove_cv<_Tp>::type*>(_M_ptr());
576 #endif
577 return nullptr;
578 }
579
580 private:
581 _Tp* _M_ptr() noexcept { return _M_impl._M_storage._M_ptr(); }
582
583 _Impl _M_impl;
584 };
585
586 // The default deleter for shared_ptr<T[]> and shared_ptr<T[N]>.
587 struct __sp_array_delete
588 {
589 template<typename _Yp>
590 void operator()(_Yp* __p) const { delete[] __p; }
591 };
592
593 template<_Lock_policy _Lp>
594 class __shared_count
595 {
596 public:
597 constexpr __shared_count() noexcept : _M_pi(0)
598 { }
599
600 template<typename _Ptr>
601 explicit
602 __shared_count(_Ptr __p) : _M_pi(0)
603 {
604 __try
605 {
606 _M_pi = new _Sp_counted_ptr<_Ptr, _Lp>(__p);
607 }
608 __catch(...)
609 {
610 delete __p;
611 __throw_exception_again;
612 }
613 }
614
615 template<typename _Ptr>
616 __shared_count(_Ptr __p, /* is_array = */ false_type)
617 : __shared_count(__p)
618 { }
619
620 template<typename _Ptr>
621 __shared_count(_Ptr __p, /* is_array = */ true_type)
622 : __shared_count(__p, __sp_array_delete{}, allocator<void>())
623 { }
624
625 template<typename _Ptr, typename _Deleter>
626 __shared_count(_Ptr __p, _Deleter __d)
627 : __shared_count(__p, std::move(__d), allocator<void>())
628 { }
629
630 template<typename _Ptr, typename _Deleter, typename _Alloc>
631 __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0)
632 {
633 typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
634 __try
635 {
636 typename _Sp_cd_type::__allocator_type __a2(__a);
637 auto __guard = std::__allocate_guarded(__a2);
638 _Sp_cd_type* __mem = __guard.get();
639 ::new (__mem) _Sp_cd_type(__p, std::move(__d), std::move(__a));
640 _M_pi = __mem;
641 __guard = nullptr;
642 }
643 __catch(...)
644 {
645 __d(__p); // Call _Deleter on __p.
646 __throw_exception_again;
647 }
648 }
649
650 template<typename _Tp, typename _Alloc, typename... _Args>
651 __shared_count(_Sp_make_shared_tag, _Tp*, const _Alloc& __a,
652 _Args&&... __args)
653 : _M_pi(0)
654 {
655 typedef _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> _Sp_cp_type;
656 typename _Sp_cp_type::__allocator_type __a2(__a);
657 auto __guard = std::__allocate_guarded(__a2);
658 _Sp_cp_type* __mem = __guard.get();
659 ::new (__mem) _Sp_cp_type(__a, std::forward<_Args>(__args)...);
660 _M_pi = __mem;
661 __guard = nullptr;
662 }
663
664 #if _GLIBCXX_USE_DEPRECATED
665 #pragma GCC diagnostic push
666 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
667 // Special case for auto_ptr<_Tp> to provide the strong guarantee.
668 template<typename _Tp>
669 explicit
670 __shared_count(std::auto_ptr<_Tp>&& __r);
671 #pragma GCC diagnostic pop
672 #endif
673
674 // Special case for unique_ptr<_Tp,_Del> to provide the strong guarantee.
675 template<typename _Tp, typename _Del>
676 explicit
677 __shared_count(std::unique_ptr<_Tp, _Del>&& __r) : _M_pi(0)
678 {
679 // _GLIBCXX_RESOLVE_LIB_DEFECTS
680 // 2415. Inconsistency between unique_ptr and shared_ptr
681 if (__r.get() == nullptr)
682 return;
683
684 using _Ptr = typename unique_ptr<_Tp, _Del>::pointer;
685 using _Del2 = typename conditional<is_reference<_Del>::value,
686 reference_wrapper<typename remove_reference<_Del>::type>,
687 _Del>::type;
688 using _Sp_cd_type
689 = _Sp_counted_deleter<_Ptr, _Del2, allocator<void>, _Lp>;
690 using _Alloc = allocator<_Sp_cd_type>;
691 using _Alloc_traits = allocator_traits<_Alloc>;
692 _Alloc __a;
693 _Sp_cd_type* __mem = _Alloc_traits::allocate(__a, 1);
694 _Alloc_traits::construct(__a, __mem, __r.release(),
695 __r.get_deleter()); // non-throwing
696 _M_pi = __mem;
697 }
698
699 // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
700 explicit __shared_count(const __weak_count<_Lp>& __r);
701
702 // Does not throw if __r._M_get_use_count() == 0, caller must check.
703 explicit __shared_count(const __weak_count<_Lp>& __r, std::nothrow_t);
704
705 ~__shared_count() noexcept
706 {
707 if (_M_pi != nullptr)
708 _M_pi->_M_release();
709 }
710
711 __shared_count(const __shared_count& __r) noexcept
712 : _M_pi(__r._M_pi)
713 {
714 if (_M_pi != 0)
715 _M_pi->_M_add_ref_copy();
716 }
717
718 __shared_count&
719 operator=(const __shared_count& __r) noexcept
720 {
721 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
722 if (__tmp != _M_pi)
723 {
724 if (__tmp != 0)
725 __tmp->_M_add_ref_copy();
726 if (_M_pi != 0)
727 _M_pi->_M_release();
728 _M_pi = __tmp;
729 }
730 return *this;
731 }
732
733 void
734 _M_swap(__shared_count& __r) noexcept
735 {
736 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
737 __r._M_pi = _M_pi;
738 _M_pi = __tmp;
739 }
740
741 long
742 _M_get_use_count() const noexcept
743 { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
744
745 bool
746 _M_unique() const noexcept
747 { return this->_M_get_use_count() == 1; }
748
749 void*
750 _M_get_deleter(const std::type_info& __ti) const noexcept
751 { return _M_pi ? _M_pi->_M_get_deleter(__ti) : nullptr; }
752
753 bool
754 _M_less(const __shared_count& __rhs) const noexcept
755 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
756
757 bool
758 _M_less(const __weak_count<_Lp>& __rhs) const noexcept
759 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
760
761 // Friend function injected into enclosing namespace and found by ADL
762 friend inline bool
763 operator==(const __shared_count& __a, const __shared_count& __b) noexcept
764 { return __a._M_pi == __b._M_pi; }
765
766 private:
767 friend class __weak_count<_Lp>;
768
769 _Sp_counted_base<_Lp>* _M_pi;
770 };
771
772
773 template<_Lock_policy _Lp>
774 class __weak_count
775 {
776 public:
777 constexpr __weak_count() noexcept : _M_pi(nullptr)
778 { }
779
780 __weak_count(const __shared_count<_Lp>& __r) noexcept
781 : _M_pi(__r._M_pi)
782 {
783 if (_M_pi != nullptr)
784 _M_pi->_M_weak_add_ref();
785 }
786
787 __weak_count(const __weak_count& __r) noexcept
788 : _M_pi(__r._M_pi)
789 {
790 if (_M_pi != nullptr)
791 _M_pi->_M_weak_add_ref();
792 }
793
794 __weak_count(__weak_count&& __r) noexcept
795 : _M_pi(__r._M_pi)
796 { __r._M_pi = nullptr; }
797
798 ~__weak_count() noexcept
799 {
800 if (_M_pi != nullptr)
801 _M_pi->_M_weak_release();
802 }
803
804 __weak_count&
805 operator=(const __shared_count<_Lp>& __r) noexcept
806 {
807 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
808 if (__tmp != nullptr)
809 __tmp->_M_weak_add_ref();
810 if (_M_pi != nullptr)
811 _M_pi->_M_weak_release();
812 _M_pi = __tmp;
813 return *this;
814 }
815
816 __weak_count&
817 operator=(const __weak_count& __r) noexcept
818 {
819 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
820 if (__tmp != nullptr)
821 __tmp->_M_weak_add_ref();
822 if (_M_pi != nullptr)
823 _M_pi->_M_weak_release();
824 _M_pi = __tmp;
825 return *this;
826 }
827
828 __weak_count&
829 operator=(__weak_count&& __r) noexcept
830 {
831 if (_M_pi != nullptr)
832 _M_pi->_M_weak_release();
833 _M_pi = __r._M_pi;
834 __r._M_pi = nullptr;
835 return *this;
836 }
837
838 void
839 _M_swap(__weak_count& __r) noexcept
840 {
841 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
842 __r._M_pi = _M_pi;
843 _M_pi = __tmp;
844 }
845
846 long
847 _M_get_use_count() const noexcept
848 { return _M_pi != nullptr ? _M_pi->_M_get_use_count() : 0; }
849
850 bool
851 _M_less(const __weak_count& __rhs) const noexcept
852 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
853
854 bool
855 _M_less(const __shared_count<_Lp>& __rhs) const noexcept
856 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
857
858 // Friend function injected into enclosing namespace and found by ADL
859 friend inline bool
860 operator==(const __weak_count& __a, const __weak_count& __b) noexcept
861 { return __a._M_pi == __b._M_pi; }
862
863 private:
864 friend class __shared_count<_Lp>;
865
866 _Sp_counted_base<_Lp>* _M_pi;
867 };
868
869 // Now that __weak_count is defined we can define this constructor:
870 template<_Lock_policy _Lp>
871 inline
872 __shared_count<_Lp>::__shared_count(const __weak_count<_Lp>& __r)
873 : _M_pi(__r._M_pi)
874 {
875 if (_M_pi != nullptr)
876 _M_pi->_M_add_ref_lock();
877 else
878 __throw_bad_weak_ptr();
879 }
880
881 // Now that __weak_count is defined we can define this constructor:
882 template<_Lock_policy _Lp>
883 inline
884 __shared_count<_Lp>::
885 __shared_count(const __weak_count<_Lp>& __r, std::nothrow_t)
886 : _M_pi(__r._M_pi)
887 {
888 if (_M_pi != nullptr)
889 if (!_M_pi->_M_add_ref_lock_nothrow())
890 _M_pi = nullptr;
891 }
892
893 #define __cpp_lib_shared_ptr_arrays 201603
894
895 // Helper traits for shared_ptr of array:
896
897 // A pointer type Y* is said to be compatible with a pointer type T* when
898 // either Y* is convertible to T* or Y is U[N] and T is U cv [].
899 template<typename _Yp_ptr, typename _Tp_ptr>
900 struct __sp_compatible_with
901 : false_type
902 { };
903
904 template<typename _Yp, typename _Tp>
905 struct __sp_compatible_with<_Yp*, _Tp*>
906 : is_convertible<_Yp*, _Tp*>::type
907 { };
908
909 template<typename _Up, size_t _Nm>
910 struct __sp_compatible_with<_Up(*)[_Nm], _Up(*)[]>
911 : true_type
912 { };
913
914 template<typename _Up, size_t _Nm>
915 struct __sp_compatible_with<_Up(*)[_Nm], const _Up(*)[]>
916 : true_type
917 { };
918
919 template<typename _Up, size_t _Nm>
920 struct __sp_compatible_with<_Up(*)[_Nm], volatile _Up(*)[]>
921 : true_type
922 { };
923
924 template<typename _Up, size_t _Nm>
925 struct __sp_compatible_with<_Up(*)[_Nm], const volatile _Up(*)[]>
926 : true_type
927 { };
928
929 // Test conversion from Y(*)[N] to U(*)[N] without forming invalid type Y[N].
930 template<typename _Up, size_t _Nm, typename _Yp, typename = void>
931 struct __sp_is_constructible_arrN
932 : false_type
933 { };
934
935 template<typename _Up, size_t _Nm, typename _Yp>
936 struct __sp_is_constructible_arrN<_Up, _Nm, _Yp, __void_t<_Yp[_Nm]>>
937 : is_convertible<_Yp(*)[_Nm], _Up(*)[_Nm]>::type
938 { };
939
940 // Test conversion from Y(*)[] to U(*)[] without forming invalid type Y[].
941 template<typename _Up, typename _Yp, typename = void>
942 struct __sp_is_constructible_arr
943 : false_type
944 { };
945
946 template<typename _Up, typename _Yp>
947 struct __sp_is_constructible_arr<_Up, _Yp, __void_t<_Yp[]>>
948 : is_convertible<_Yp(*)[], _Up(*)[]>::type
949 { };
950
951 // Trait to check if shared_ptr<T> can be constructed from Y*.
952 template<typename _Tp, typename _Yp>
953 struct __sp_is_constructible;
954
955 // When T is U[N], Y(*)[N] shall be convertible to T*;
956 template<typename _Up, size_t _Nm, typename _Yp>
957 struct __sp_is_constructible<_Up[_Nm], _Yp>
958 : __sp_is_constructible_arrN<_Up, _Nm, _Yp>::type
959 { };
960
961 // when T is U[], Y(*)[] shall be convertible to T*;
962 template<typename _Up, typename _Yp>
963 struct __sp_is_constructible<_Up[], _Yp>
964 : __sp_is_constructible_arr<_Up, _Yp>::type
965 { };
966
967 // otherwise, Y* shall be convertible to T*.
968 template<typename _Tp, typename _Yp>
969 struct __sp_is_constructible
970 : is_convertible<_Yp*, _Tp*>::type
971 { };
972
973
974 // Define operator* and operator-> for shared_ptr<T>.
975 template<typename _Tp, _Lock_policy _Lp,
976 bool = is_array<_Tp>::value, bool = is_void<_Tp>::value>
977 class __shared_ptr_access
978 {
979 public:
980 using element_type = _Tp;
981
982 element_type&
983 operator*() const noexcept
984 {
985 __glibcxx_assert(_M_get() != nullptr);
986 return *_M_get();
987 }
988
989 element_type*
990 operator->() const noexcept
991 {
992 _GLIBCXX_DEBUG_PEDASSERT(_M_get() != nullptr);
993 return _M_get();
994 }
995
996 private:
997 element_type*
998 _M_get() const noexcept
999 { return static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get(); }
1000 };
1001
1002 // Define operator-> for shared_ptr<cv void>.
1003 template<typename _Tp, _Lock_policy _Lp>
1004 class __shared_ptr_access<_Tp, _Lp, false, true>
1005 {
1006 public:
1007 using element_type = _Tp;
1008
1009 element_type*
1010 operator->() const noexcept
1011 {
1012 auto __ptr = static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get();
1013 _GLIBCXX_DEBUG_PEDASSERT(__ptr != nullptr);
1014 return __ptr;
1015 }
1016 };
1017
1018 // Define operator[] for shared_ptr<T[]> and shared_ptr<T[N]>.
1019 template<typename _Tp, _Lock_policy _Lp>
1020 class __shared_ptr_access<_Tp, _Lp, true, false>
1021 {
1022 public:
1023 using element_type = typename remove_extent<_Tp>::type;
1024
1025 #if __cplusplus <= 201402L
1026 [[__deprecated__("shared_ptr<T[]>::operator* is absent from C++17")]]
1027 element_type&
1028 operator*() const noexcept
1029 {
1030 __glibcxx_assert(_M_get() != nullptr);
1031 return *_M_get();
1032 }
1033
1034 [[__deprecated__("shared_ptr<T[]>::operator-> is absent from C++17")]]
1035 element_type*
1036 operator->() const noexcept
1037 {
1038 _GLIBCXX_DEBUG_PEDASSERT(_M_get() != nullptr);
1039 return _M_get();
1040 }
1041 #endif
1042
1043 element_type&
1044 operator[](ptrdiff_t __i) const
1045 {
1046 __glibcxx_assert(_M_get() != nullptr);
1047 __glibcxx_assert(!extent<_Tp>::value || __i < extent<_Tp>::value);
1048 return _M_get()[__i];
1049 }
1050
1051 private:
1052 element_type*
1053 _M_get() const noexcept
1054 { return static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get(); }
1055 };
1056
1057 template<typename _Tp, _Lock_policy _Lp>
1058 class __shared_ptr
1059 : public __shared_ptr_access<_Tp, _Lp>
1060 {
1061 public:
1062 using element_type = typename remove_extent<_Tp>::type;
1063
1064 private:
1065 // Constraint for taking ownership of a pointer of type _Yp*:
1066 template<typename _Yp>
1067 using _SafeConv
1068 = typename enable_if<__sp_is_constructible<_Tp, _Yp>::value>::type;
1069
1070 // Constraint for construction from shared_ptr and weak_ptr:
1071 template<typename _Yp, typename _Res = void>
1072 using _Compatible = typename
1073 enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type;
1074
1075 // Constraint for assignment from shared_ptr and weak_ptr:
1076 template<typename _Yp>
1077 using _Assignable = _Compatible<_Yp, __shared_ptr&>;
1078
1079 // Constraint for construction from unique_ptr:
1080 template<typename _Yp, typename _Del, typename _Res = void,
1081 typename _Ptr = typename unique_ptr<_Yp, _Del>::pointer>
1082 using _UniqCompatible = typename enable_if<__and_<
1083 __sp_compatible_with<_Yp*, _Tp*>, is_convertible<_Ptr, element_type*>
1084 >::value, _Res>::type;
1085
1086 // Constraint for assignment from unique_ptr:
1087 template<typename _Yp, typename _Del>
1088 using _UniqAssignable = _UniqCompatible<_Yp, _Del, __shared_ptr&>;
1089
1090 public:
1091
1092 #if __cplusplus > 201402L
1093 using weak_type = __weak_ptr<_Tp, _Lp>;
1094 #endif
1095
1096 constexpr __shared_ptr() noexcept
1097 : _M_ptr(0), _M_refcount()
1098 { }
1099
1100 template<typename _Yp, typename = _SafeConv<_Yp>>
1101 explicit
1102 __shared_ptr(_Yp* __p)
1103 : _M_ptr(__p), _M_refcount(__p, typename is_array<_Tp>::type())
1104 {
1105 static_assert( !is_void<_Yp>::value, "incomplete type" );
1106 static_assert( sizeof(_Yp) > 0, "incomplete type" );
1107 _M_enable_shared_from_this_with(__p);
1108 }
1109
1110 template<typename _Yp, typename _Deleter, typename = _SafeConv<_Yp>>
1111 __shared_ptr(_Yp* __p, _Deleter __d)
1112 : _M_ptr(__p), _M_refcount(__p, std::move(__d))
1113 {
1114 static_assert(__is_invocable<_Deleter&, _Yp*&>::value,
1115 "deleter expression d(p) is well-formed");
1116 _M_enable_shared_from_this_with(__p);
1117 }
1118
1119 template<typename _Yp, typename _Deleter, typename _Alloc,
1120 typename = _SafeConv<_Yp>>
1121 __shared_ptr(_Yp* __p, _Deleter __d, _Alloc __a)
1122 : _M_ptr(__p), _M_refcount(__p, std::move(__d), std::move(__a))
1123 {
1124 static_assert(__is_invocable<_Deleter&, _Yp*&>::value,
1125 "deleter expression d(p) is well-formed");
1126 _M_enable_shared_from_this_with(__p);
1127 }
1128
1129 template<typename _Deleter>
1130 __shared_ptr(nullptr_t __p, _Deleter __d)
1131 : _M_ptr(0), _M_refcount(__p, std::move(__d))
1132 { }
1133
1134 template<typename _Deleter, typename _Alloc>
1135 __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
1136 : _M_ptr(0), _M_refcount(__p, std::move(__d), std::move(__a))
1137 { }
1138
1139 template<typename _Yp>
1140 __shared_ptr(const __shared_ptr<_Yp, _Lp>& __r,
1141 element_type* __p) noexcept
1142 : _M_ptr(__p), _M_refcount(__r._M_refcount) // never throws
1143 { }
1144
1145 __shared_ptr(const __shared_ptr&) noexcept = default;
1146 __shared_ptr& operator=(const __shared_ptr&) noexcept = default;
1147 ~__shared_ptr() = default;
1148
1149 template<typename _Yp, typename = _Compatible<_Yp>>
1150 __shared_ptr(const __shared_ptr<_Yp, _Lp>& __r) noexcept
1151 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
1152 { }
1153
1154 __shared_ptr(__shared_ptr&& __r) noexcept
1155 : _M_ptr(__r._M_ptr), _M_refcount()
1156 {
1157 _M_refcount._M_swap(__r._M_refcount);
1158 __r._M_ptr = 0;
1159 }
1160
1161 template<typename _Yp, typename = _Compatible<_Yp>>
1162 __shared_ptr(__shared_ptr<_Yp, _Lp>&& __r) noexcept
1163 : _M_ptr(__r._M_ptr), _M_refcount()
1164 {
1165 _M_refcount._M_swap(__r._M_refcount);
1166 __r._M_ptr = 0;
1167 }
1168
1169 template<typename _Yp, typename = _Compatible<_Yp>>
1170 explicit __shared_ptr(const __weak_ptr<_Yp, _Lp>& __r)
1171 : _M_refcount(__r._M_refcount) // may throw
1172 {
1173 // It is now safe to copy __r._M_ptr, as
1174 // _M_refcount(__r._M_refcount) did not throw.
1175 _M_ptr = __r._M_ptr;
1176 }
1177
1178 // If an exception is thrown this constructor has no effect.
1179 template<typename _Yp, typename _Del,
1180 typename = _UniqCompatible<_Yp, _Del>>
1181 __shared_ptr(unique_ptr<_Yp, _Del>&& __r)
1182 : _M_ptr(__r.get()), _M_refcount()
1183 {
1184 auto __raw = __to_address(__r.get());
1185 _M_refcount = __shared_count<_Lp>(std::move(__r));
1186 _M_enable_shared_from_this_with(__raw);
1187 }
1188
1189 #if __cplusplus <= 201402L && _GLIBCXX_USE_DEPRECATED
1190 protected:
1191 // If an exception is thrown this constructor has no effect.
1192 template<typename _Tp1, typename _Del,
1193 typename enable_if<__and_<
1194 __not_<is_array<_Tp>>, is_array<_Tp1>,
1195 is_convertible<typename unique_ptr<_Tp1, _Del>::pointer, _Tp*>
1196 >::value, bool>::type = true>
1197 __shared_ptr(unique_ptr<_Tp1, _Del>&& __r, __sp_array_delete)
1198 : _M_ptr(__r.get()), _M_refcount()
1199 {
1200 auto __raw = __to_address(__r.get());
1201 _M_refcount = __shared_count<_Lp>(std::move(__r));
1202 _M_enable_shared_from_this_with(__raw);
1203 }
1204 public:
1205 #endif
1206
1207 #if _GLIBCXX_USE_DEPRECATED
1208 #pragma GCC diagnostic push
1209 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1210 // Postcondition: use_count() == 1 and __r.get() == 0
1211 template<typename _Yp, typename = _Compatible<_Yp>>
1212 __shared_ptr(auto_ptr<_Yp>&& __r);
1213 #pragma GCC diagnostic pop
1214 #endif
1215
1216 constexpr __shared_ptr(nullptr_t) noexcept : __shared_ptr() { }
1217
1218 template<typename _Yp>
1219 _Assignable<_Yp>
1220 operator=(const __shared_ptr<_Yp, _Lp>& __r) noexcept
1221 {
1222 _M_ptr = __r._M_ptr;
1223 _M_refcount = __r._M_refcount; // __shared_count::op= doesn't throw
1224 return *this;
1225 }
1226
1227 #if _GLIBCXX_USE_DEPRECATED
1228 #pragma GCC diagnostic push
1229 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1230 template<typename _Yp>
1231 _Assignable<_Yp>
1232 operator=(auto_ptr<_Yp>&& __r)
1233 {
1234 __shared_ptr(std::move(__r)).swap(*this);
1235 return *this;
1236 }
1237 #pragma GCC diagnostic pop
1238 #endif
1239
1240 __shared_ptr&
1241 operator=(__shared_ptr&& __r) noexcept
1242 {
1243 __shared_ptr(std::move(__r)).swap(*this);
1244 return *this;
1245 }
1246
1247 template<class _Yp>
1248 _Assignable<_Yp>
1249 operator=(__shared_ptr<_Yp, _Lp>&& __r) noexcept
1250 {
1251 __shared_ptr(std::move(__r)).swap(*this);
1252 return *this;
1253 }
1254
1255 template<typename _Yp, typename _Del>
1256 _UniqAssignable<_Yp, _Del>
1257 operator=(unique_ptr<_Yp, _Del>&& __r)
1258 {
1259 __shared_ptr(std::move(__r)).swap(*this);
1260 return *this;
1261 }
1262
1263 void
1264 reset() noexcept
1265 { __shared_ptr().swap(*this); }
1266
1267 template<typename _Yp>
1268 _SafeConv<_Yp>
1269 reset(_Yp* __p) // _Yp must be complete.
1270 {
1271 // Catch self-reset errors.
1272 __glibcxx_assert(__p == 0 || __p != _M_ptr);
1273 __shared_ptr(__p).swap(*this);
1274 }
1275
1276 template<typename _Yp, typename _Deleter>
1277 _SafeConv<_Yp>
1278 reset(_Yp* __p, _Deleter __d)
1279 { __shared_ptr(__p, std::move(__d)).swap(*this); }
1280
1281 template<typename _Yp, typename _Deleter, typename _Alloc>
1282 _SafeConv<_Yp>
1283 reset(_Yp* __p, _Deleter __d, _Alloc __a)
1284 { __shared_ptr(__p, std::move(__d), std::move(__a)).swap(*this); }
1285
1286 element_type*
1287 get() const noexcept
1288 { return _M_ptr; }
1289
1290 explicit operator bool() const // never throws
1291 { return _M_ptr == 0 ? false : true; }
1292
1293 bool
1294 unique() const noexcept
1295 { return _M_refcount._M_unique(); }
1296
1297 long
1298 use_count() const noexcept
1299 { return _M_refcount._M_get_use_count(); }
1300
1301 void
1302 swap(__shared_ptr<_Tp, _Lp>& __other) noexcept
1303 {
1304 std::swap(_M_ptr, __other._M_ptr);
1305 _M_refcount._M_swap(__other._M_refcount);
1306 }
1307
1308 template<typename _Tp1>
1309 bool
1310 owner_before(__shared_ptr<_Tp1, _Lp> const& __rhs) const noexcept
1311 { return _M_refcount._M_less(__rhs._M_refcount); }
1312
1313 template<typename _Tp1>
1314 bool
1315 owner_before(__weak_ptr<_Tp1, _Lp> const& __rhs) const noexcept
1316 { return _M_refcount._M_less(__rhs._M_refcount); }
1317
1318 protected:
1319 // This constructor is non-standard, it is used by allocate_shared.
1320 template<typename _Alloc, typename... _Args>
1321 __shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a,
1322 _Args&&... __args)
1323 : _M_ptr(), _M_refcount(__tag, (_Tp*)0, __a,
1324 std::forward<_Args>(__args)...)
1325 {
1326 // _M_ptr needs to point to the newly constructed object.
1327 // This relies on _Sp_counted_ptr_inplace::_M_get_deleter.
1328 void* __p = _M_refcount._M_get_deleter(_Sp_make_shared_tag::_S_ti());
1329 _M_ptr = static_cast<_Tp*>(__p);
1330 _M_enable_shared_from_this_with(_M_ptr);
1331 }
1332
1333 template<typename _Tp1, _Lock_policy _Lp1, typename _Alloc,
1334 typename... _Args>
1335 friend __shared_ptr<_Tp1, _Lp1>
1336 __allocate_shared(const _Alloc& __a, _Args&&... __args);
1337
1338 // This constructor is used by __weak_ptr::lock() and
1339 // shared_ptr::shared_ptr(const weak_ptr&, std::nothrow_t).
1340 __shared_ptr(const __weak_ptr<_Tp, _Lp>& __r, std::nothrow_t)
1341 : _M_refcount(__r._M_refcount, std::nothrow)
1342 {
1343 _M_ptr = _M_refcount._M_get_use_count() ? __r._M_ptr : nullptr;
1344 }
1345
1346 friend class __weak_ptr<_Tp, _Lp>;
1347
1348 private:
1349
1350 template<typename _Yp>
1351 using __esft_base_t = decltype(__enable_shared_from_this_base(
1352 std::declval<const __shared_count<_Lp>&>(),
1353 std::declval<_Yp*>()));
1354
1355 // Detect an accessible and unambiguous enable_shared_from_this base.
1356 template<typename _Yp, typename = void>
1357 struct __has_esft_base
1358 : false_type { };
1359
1360 template<typename _Yp>
1361 struct __has_esft_base<_Yp, __void_t<__esft_base_t<_Yp>>>
1362 : __not_<is_array<_Tp>> { }; // No enable shared_from_this for arrays
1363
1364 template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type>
1365 typename enable_if<__has_esft_base<_Yp2>::value>::type
1366 _M_enable_shared_from_this_with(_Yp* __p) noexcept
1367 {
1368 if (auto __base = __enable_shared_from_this_base(_M_refcount, __p))
1369 __base->_M_weak_assign(const_cast<_Yp2*>(__p), _M_refcount);
1370 }
1371
1372 template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type>
1373 typename enable_if<!__has_esft_base<_Yp2>::value>::type
1374 _M_enable_shared_from_this_with(_Yp*) noexcept
1375 { }
1376
1377 void*
1378 _M_get_deleter(const std::type_info& __ti) const noexcept
1379 { return _M_refcount._M_get_deleter(__ti); }
1380
1381 template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
1382 template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
1383
1384 template<typename _Del, typename _Tp1, _Lock_policy _Lp1>
1385 friend _Del* get_deleter(const __shared_ptr<_Tp1, _Lp1>&) noexcept;
1386
1387 template<typename _Del, typename _Tp1>
1388 friend _Del* get_deleter(const shared_ptr<_Tp1>&) noexcept;
1389
1390 element_type* _M_ptr; // Contained pointer.
1391 __shared_count<_Lp> _M_refcount; // Reference counter.
1392 };
1393
1394
1395 // 20.7.2.2.7 shared_ptr comparisons
1396 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1397 inline bool
1398 operator==(const __shared_ptr<_Tp1, _Lp>& __a,
1399 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1400 { return __a.get() == __b.get(); }
1401
1402 template<typename _Tp, _Lock_policy _Lp>
1403 inline bool
1404 operator==(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1405 { return !__a; }
1406
1407 template<typename _Tp, _Lock_policy _Lp>
1408 inline bool
1409 operator==(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1410 { return !__a; }
1411
1412 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1413 inline bool
1414 operator!=(const __shared_ptr<_Tp1, _Lp>& __a,
1415 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1416 { return __a.get() != __b.get(); }
1417
1418 template<typename _Tp, _Lock_policy _Lp>
1419 inline bool
1420 operator!=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1421 { return (bool)__a; }
1422
1423 template<typename _Tp, _Lock_policy _Lp>
1424 inline bool
1425 operator!=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1426 { return (bool)__a; }
1427
1428 template<typename _Tp, typename _Up, _Lock_policy _Lp>
1429 inline bool
1430 operator<(const __shared_ptr<_Tp, _Lp>& __a,
1431 const __shared_ptr<_Up, _Lp>& __b) noexcept
1432 {
1433 using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type;
1434 using _Up_elt = typename __shared_ptr<_Up, _Lp>::element_type;
1435 using _Vp = typename common_type<_Tp_elt*, _Up_elt*>::type;
1436 return less<_Vp>()(__a.get(), __b.get());
1437 }
1438
1439 template<typename _Tp, _Lock_policy _Lp>
1440 inline bool
1441 operator<(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1442 {
1443 using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type;
1444 return less<_Tp_elt*>()(__a.get(), nullptr);
1445 }
1446
1447 template<typename _Tp, _Lock_policy _Lp>
1448 inline bool
1449 operator<(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1450 {
1451 using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type;
1452 return less<_Tp_elt*>()(nullptr, __a.get());
1453 }
1454
1455 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1456 inline bool
1457 operator<=(const __shared_ptr<_Tp1, _Lp>& __a,
1458 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1459 { return !(__b < __a); }
1460
1461 template<typename _Tp, _Lock_policy _Lp>
1462 inline bool
1463 operator<=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1464 { return !(nullptr < __a); }
1465
1466 template<typename _Tp, _Lock_policy _Lp>
1467 inline bool
1468 operator<=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1469 { return !(__a < nullptr); }
1470
1471 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1472 inline bool
1473 operator>(const __shared_ptr<_Tp1, _Lp>& __a,
1474 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1475 { return (__b < __a); }
1476
1477 template<typename _Tp, _Lock_policy _Lp>
1478 inline bool
1479 operator>(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1480 { return nullptr < __a; }
1481
1482 template<typename _Tp, _Lock_policy _Lp>
1483 inline bool
1484 operator>(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1485 { return __a < nullptr; }
1486
1487 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1488 inline bool
1489 operator>=(const __shared_ptr<_Tp1, _Lp>& __a,
1490 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1491 { return !(__a < __b); }
1492
1493 template<typename _Tp, _Lock_policy _Lp>
1494 inline bool
1495 operator>=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1496 { return !(__a < nullptr); }
1497
1498 template<typename _Tp, _Lock_policy _Lp>
1499 inline bool
1500 operator>=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1501 { return !(nullptr < __a); }
1502
1503 // 20.7.2.2.8 shared_ptr specialized algorithms.
1504 template<typename _Tp, _Lock_policy _Lp>
1505 inline void
1506 swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b) noexcept
1507 { __a.swap(__b); }
1508
1509 // 20.7.2.2.9 shared_ptr casts
1510
1511 // The seemingly equivalent code:
1512 // shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get()))
1513 // will eventually result in undefined behaviour, attempting to
1514 // delete the same object twice.
1515 /// static_pointer_cast
1516 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
1517 inline __shared_ptr<_Tp, _Lp>
1518 static_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1519 {
1520 using _Sp = __shared_ptr<_Tp, _Lp>;
1521 return _Sp(__r, static_cast<typename _Sp::element_type*>(__r.get()));
1522 }
1523
1524 // The seemingly equivalent code:
1525 // shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get()))
1526 // will eventually result in undefined behaviour, attempting to
1527 // delete the same object twice.
1528 /// const_pointer_cast
1529 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
1530 inline __shared_ptr<_Tp, _Lp>
1531 const_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1532 {
1533 using _Sp = __shared_ptr<_Tp, _Lp>;
1534 return _Sp(__r, const_cast<typename _Sp::element_type*>(__r.get()));
1535 }
1536
1537 // The seemingly equivalent code:
1538 // shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))
1539 // will eventually result in undefined behaviour, attempting to
1540 // delete the same object twice.
1541 /// dynamic_pointer_cast
1542 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
1543 inline __shared_ptr<_Tp, _Lp>
1544 dynamic_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1545 {
1546 using _Sp = __shared_ptr<_Tp, _Lp>;
1547 if (auto* __p = dynamic_cast<typename _Sp::element_type*>(__r.get()))
1548 return _Sp(__r, __p);
1549 return _Sp();
1550 }
1551
1552 #if __cplusplus > 201402L
1553 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
1554 inline __shared_ptr<_Tp, _Lp>
1555 reinterpret_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1556 {
1557 using _Sp = __shared_ptr<_Tp, _Lp>;
1558 return _Sp(__r, reinterpret_cast<typename _Sp::element_type*>(__r.get()));
1559 }
1560 #endif
1561
1562 template<typename _Tp, _Lock_policy _Lp>
1563 class __weak_ptr
1564 {
1565 template<typename _Yp, typename _Res = void>
1566 using _Compatible = typename
1567 enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type;
1568
1569 // Constraint for assignment from shared_ptr and weak_ptr:
1570 template<typename _Yp>
1571 using _Assignable = _Compatible<_Yp, __weak_ptr&>;
1572
1573 public:
1574 using element_type = typename remove_extent<_Tp>::type;
1575
1576 constexpr __weak_ptr() noexcept
1577 : _M_ptr(nullptr), _M_refcount()
1578 { }
1579
1580 __weak_ptr(const __weak_ptr&) noexcept = default;
1581
1582 ~__weak_ptr() = default;
1583
1584 // The "obvious" converting constructor implementation:
1585 //
1586 // template<typename _Tp1>
1587 // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
1588 // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
1589 // { }
1590 //
1591 // has a serious problem.
1592 //
1593 // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
1594 // conversion may require access to *__r._M_ptr (virtual inheritance).
1595 //
1596 // It is not possible to avoid spurious access violations since
1597 // in multithreaded programs __r._M_ptr may be invalidated at any point.
1598 template<typename _Yp, typename = _Compatible<_Yp>>
1599 __weak_ptr(const __weak_ptr<_Yp, _Lp>& __r) noexcept
1600 : _M_refcount(__r._M_refcount)
1601 { _M_ptr = __r.lock().get(); }
1602
1603 template<typename _Yp, typename = _Compatible<_Yp>>
1604 __weak_ptr(const __shared_ptr<_Yp, _Lp>& __r) noexcept
1605 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
1606 { }
1607
1608 __weak_ptr(__weak_ptr&& __r) noexcept
1609 : _M_ptr(__r._M_ptr), _M_refcount(std::move(__r._M_refcount))
1610 { __r._M_ptr = nullptr; }
1611
1612 template<typename _Yp, typename = _Compatible<_Yp>>
1613 __weak_ptr(__weak_ptr<_Yp, _Lp>&& __r) noexcept
1614 : _M_ptr(__r.lock().get()), _M_refcount(std::move(__r._M_refcount))
1615 { __r._M_ptr = nullptr; }
1616
1617 __weak_ptr&
1618 operator=(const __weak_ptr& __r) noexcept = default;
1619
1620 template<typename _Yp>
1621 _Assignable<_Yp>
1622 operator=(const __weak_ptr<_Yp, _Lp>& __r) noexcept
1623 {
1624 _M_ptr = __r.lock().get();
1625 _M_refcount = __r._M_refcount;
1626 return *this;
1627 }
1628
1629 template<typename _Yp>
1630 _Assignable<_Yp>
1631 operator=(const __shared_ptr<_Yp, _Lp>& __r) noexcept
1632 {
1633 _M_ptr = __r._M_ptr;
1634 _M_refcount = __r._M_refcount;
1635 return *this;
1636 }
1637
1638 __weak_ptr&
1639 operator=(__weak_ptr&& __r) noexcept
1640 {
1641 _M_ptr = __r._M_ptr;
1642 _M_refcount = std::move(__r._M_refcount);
1643 __r._M_ptr = nullptr;
1644 return *this;
1645 }
1646
1647 template<typename _Yp>
1648 _Assignable<_Yp>
1649 operator=(__weak_ptr<_Yp, _Lp>&& __r) noexcept
1650 {
1651 _M_ptr = __r.lock().get();
1652 _M_refcount = std::move(__r._M_refcount);
1653 __r._M_ptr = nullptr;
1654 return *this;
1655 }
1656
1657 __shared_ptr<_Tp, _Lp>
1658 lock() const noexcept
1659 { return __shared_ptr<element_type, _Lp>(*this, std::nothrow); }
1660
1661 long
1662 use_count() const noexcept
1663 { return _M_refcount._M_get_use_count(); }
1664
1665 bool
1666 expired() const noexcept
1667 { return _M_refcount._M_get_use_count() == 0; }
1668
1669 template<typename _Tp1>
1670 bool
1671 owner_before(const __shared_ptr<_Tp1, _Lp>& __rhs) const noexcept
1672 { return _M_refcount._M_less(__rhs._M_refcount); }
1673
1674 template<typename _Tp1>
1675 bool
1676 owner_before(const __weak_ptr<_Tp1, _Lp>& __rhs) const noexcept
1677 { return _M_refcount._M_less(__rhs._M_refcount); }
1678
1679 void
1680 reset() noexcept
1681 { __weak_ptr().swap(*this); }
1682
1683 void
1684 swap(__weak_ptr& __s) noexcept
1685 {
1686 std::swap(_M_ptr, __s._M_ptr);
1687 _M_refcount._M_swap(__s._M_refcount);
1688 }
1689
1690 private:
1691 // Used by __enable_shared_from_this.
1692 void
1693 _M_assign(_Tp* __ptr, const __shared_count<_Lp>& __refcount) noexcept
1694 {
1695 if (use_count() == 0)
1696 {
1697 _M_ptr = __ptr;
1698 _M_refcount = __refcount;
1699 }
1700 }
1701
1702 template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
1703 template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
1704 friend class __enable_shared_from_this<_Tp, _Lp>;
1705 friend class enable_shared_from_this<_Tp>;
1706
1707 element_type* _M_ptr; // Contained pointer.
1708 __weak_count<_Lp> _M_refcount; // Reference counter.
1709 };
1710
1711 // 20.7.2.3.6 weak_ptr specialized algorithms.
1712 template<typename _Tp, _Lock_policy _Lp>
1713 inline void
1714 swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b) noexcept
1715 { __a.swap(__b); }
1716
1717 template<typename _Tp, typename _Tp1>
1718 struct _Sp_owner_less : public binary_function<_Tp, _Tp, bool>
1719 {
1720 bool
1721 operator()(const _Tp& __lhs, const _Tp& __rhs) const noexcept
1722 { return __lhs.owner_before(__rhs); }
1723
1724 bool
1725 operator()(const _Tp& __lhs, const _Tp1& __rhs) const noexcept
1726 { return __lhs.owner_before(__rhs); }
1727
1728 bool
1729 operator()(const _Tp1& __lhs, const _Tp& __rhs) const noexcept
1730 { return __lhs.owner_before(__rhs); }
1731 };
1732
1733 template<>
1734 struct _Sp_owner_less<void, void>
1735 {
1736 template<typename _Tp, typename _Up>
1737 auto
1738 operator()(const _Tp& __lhs, const _Up& __rhs) const noexcept
1739 -> decltype(__lhs.owner_before(__rhs))
1740 { return __lhs.owner_before(__rhs); }
1741
1742 using is_transparent = void;
1743 };
1744
1745 template<typename _Tp, _Lock_policy _Lp>
1746 struct owner_less<__shared_ptr<_Tp, _Lp>>
1747 : public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>>
1748 { };
1749
1750 template<typename _Tp, _Lock_policy _Lp>
1751 struct owner_less<__weak_ptr<_Tp, _Lp>>
1752 : public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>>
1753 { };
1754
1755
1756 template<typename _Tp, _Lock_policy _Lp>
1757 class __enable_shared_from_this
1758 {
1759 protected:
1760 constexpr __enable_shared_from_this() noexcept { }
1761
1762 __enable_shared_from_this(const __enable_shared_from_this&) noexcept { }
1763
1764 __enable_shared_from_this&
1765 operator=(const __enable_shared_from_this&) noexcept
1766 { return *this; }
1767
1768 ~__enable_shared_from_this() { }
1769
1770 public:
1771 __shared_ptr<_Tp, _Lp>
1772 shared_from_this()
1773 { return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
1774
1775 __shared_ptr<const _Tp, _Lp>
1776 shared_from_this() const
1777 { return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
1778
1779 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
1780 __weak_ptr<_Tp, _Lp>
1781 weak_from_this() noexcept
1782 { return this->_M_weak_this; }
1783
1784 __weak_ptr<const _Tp, _Lp>
1785 weak_from_this() const noexcept
1786 { return this->_M_weak_this; }
1787 #endif
1788
1789 private:
1790 template<typename _Tp1>
1791 void
1792 _M_weak_assign(_Tp1* __p, const __shared_count<_Lp>& __n) const noexcept
1793 { _M_weak_this._M_assign(__p, __n); }
1794
1795 friend const __enable_shared_from_this*
1796 __enable_shared_from_this_base(const __shared_count<_Lp>&,
1797 const __enable_shared_from_this* __p)
1798 { return __p; }
1799
1800 template<typename, _Lock_policy>
1801 friend class __shared_ptr;
1802
1803 mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
1804 };
1805
1806 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy,
1807 typename _Alloc, typename... _Args>
1808 inline __shared_ptr<_Tp, _Lp>
1809 __allocate_shared(const _Alloc& __a, _Args&&... __args)
1810 {
1811 return __shared_ptr<_Tp, _Lp>(_Sp_make_shared_tag(), __a,
1812 std::forward<_Args>(__args)...);
1813 }
1814
1815 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy,
1816 typename... _Args>
1817 inline __shared_ptr<_Tp, _Lp>
1818 __make_shared(_Args&&... __args)
1819 {
1820 typedef typename std::remove_const<_Tp>::type _Tp_nc;
1821 return std::__allocate_shared<_Tp, _Lp>(std::allocator<_Tp_nc>(),
1822 std::forward<_Args>(__args)...);
1823 }
1824
1825 /// std::hash specialization for __shared_ptr.
1826 template<typename _Tp, _Lock_policy _Lp>
1827 struct hash<__shared_ptr<_Tp, _Lp>>
1828 : public __hash_base<size_t, __shared_ptr<_Tp, _Lp>>
1829 {
1830 size_t
1831 operator()(const __shared_ptr<_Tp, _Lp>& __s) const noexcept
1832 {
1833 return hash<typename __shared_ptr<_Tp, _Lp>::element_type*>()(
1834 __s.get());
1835 }
1836 };
1837
1838 _GLIBCXX_END_NAMESPACE_VERSION
1839 } // namespace
1840
1841 #endif // _SHARED_PTR_BASE_H