Merge from mainline (168000:168310).
[official-gcc/graphite-test-results.git] / libstdc++-v3 / include / bits / shared_ptr_base.h
blobba2f004c77136503529b00d78c93d896a4fcf5e8
1 // shared_ptr and weak_ptr implementation details -*- C++ -*-
3 // Copyright (C) 2007, 2008, 2009, 2010 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.
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.
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.
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/>.
25 // GCC Note: Based on files from version 1.32.0 of the Boost library.
27 // shared_count.hpp
28 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
30 // shared_ptr.hpp
31 // Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes.
32 // Copyright (C) 2001, 2002, 2003 Peter Dimov
34 // weak_ptr.hpp
35 // Copyright (C) 2001, 2002, 2003 Peter Dimov
37 // enable_shared_from_this.hpp
38 // Copyright (C) 2002 Peter Dimov
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)
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}
49 #ifndef _SHARED_PTR_BASE_H
50 #define _SHARED_PTR_BASE_H 1
52 _GLIBCXX_BEGIN_NAMESPACE(std)
54 /**
55 * @brief Exception possibly thrown by @c shared_ptr.
56 * @ingroup exceptions
58 class bad_weak_ptr : public std::exception
60 public:
61 virtual char const*
62 what() const throw()
63 { return "std::bad_weak_ptr"; }
66 // Substitute for bad_weak_ptr object in the case of -fno-exceptions.
67 inline void
68 __throw_bad_weak_ptr()
70 #if __EXCEPTIONS
71 throw bad_weak_ptr();
72 #else
73 __builtin_abort();
74 #endif
77 using __gnu_cxx::_Lock_policy;
78 using __gnu_cxx::__default_lock_policy;
79 using __gnu_cxx::_S_single;
80 using __gnu_cxx::_S_mutex;
81 using __gnu_cxx::_S_atomic;
83 // Empty helper class except when the template argument is _S_mutex.
84 template<_Lock_policy _Lp>
85 class _Mutex_base
87 protected:
88 // The atomic policy uses fully-fenced builtins, single doesn't care.
89 enum { _S_need_barriers = 0 };
92 template<>
93 class _Mutex_base<_S_mutex>
94 : public __gnu_cxx::__mutex
96 protected:
97 // This policy is used when atomic builtins are not available.
98 // The replacement atomic operations might not have the necessary
99 // memory barriers.
100 enum { _S_need_barriers = 1 };
103 template<_Lock_policy _Lp = __default_lock_policy>
104 class _Sp_counted_base
105 : public _Mutex_base<_Lp>
107 public:
108 _Sp_counted_base()
109 : _M_use_count(1), _M_weak_count(1) { }
111 virtual
112 ~_Sp_counted_base() // nothrow
115 // Called when _M_use_count drops to zero, to release the resources
116 // managed by *this.
117 virtual void
118 _M_dispose() = 0; // nothrow
120 // Called when _M_weak_count drops to zero.
121 virtual void
122 _M_destroy() // nothrow
123 { delete this; }
125 virtual void*
126 _M_get_deleter(const std::type_info&) = 0;
128 void
129 _M_add_ref_copy()
130 { __gnu_cxx::__atomic_add_dispatch(&_M_use_count, 1); }
132 void
133 _M_add_ref_lock();
135 void
136 _M_release() // nothrow
138 // Be race-detector-friendly. For more info see bits/c++config.
139 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count);
140 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
142 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count);
143 _M_dispose();
144 // There must be a memory barrier between dispose() and destroy()
145 // to ensure that the effects of dispose() are observed in the
146 // thread that runs destroy().
147 // See http://gcc.gnu.org/ml/libstdc++/2005-11/msg00136.html
148 if (_Mutex_base<_Lp>::_S_need_barriers)
150 _GLIBCXX_READ_MEM_BARRIER;
151 _GLIBCXX_WRITE_MEM_BARRIER;
154 // Be race-detector-friendly. For more info see bits/c++config.
155 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
156 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count,
157 -1) == 1)
159 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
160 _M_destroy();
165 void
166 _M_weak_add_ref() // nothrow
167 { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count, 1); }
169 void
170 _M_weak_release() // nothrow
172 // Be race-detector-friendly. For more info see bits/c++config.
173 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
174 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, -1) == 1)
176 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
177 if (_Mutex_base<_Lp>::_S_need_barriers)
179 // See _M_release(),
180 // destroy() must observe results of dispose()
181 _GLIBCXX_READ_MEM_BARRIER;
182 _GLIBCXX_WRITE_MEM_BARRIER;
184 _M_destroy();
188 long
189 _M_get_use_count() const // nothrow
191 // No memory barrier is used here so there is no synchronization
192 // with other threads.
193 return const_cast<const volatile _Atomic_word&>(_M_use_count);
196 private:
197 _Sp_counted_base(_Sp_counted_base const&);
198 _Sp_counted_base& operator=(_Sp_counted_base const&);
200 _Atomic_word _M_use_count; // #shared
201 _Atomic_word _M_weak_count; // #weak + (#shared != 0)
204 template<>
205 inline void
206 _Sp_counted_base<_S_single>::
207 _M_add_ref_lock()
209 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
211 _M_use_count = 0;
212 __throw_bad_weak_ptr();
216 template<>
217 inline void
218 _Sp_counted_base<_S_mutex>::
219 _M_add_ref_lock()
221 __gnu_cxx::__scoped_lock sentry(*this);
222 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
224 _M_use_count = 0;
225 __throw_bad_weak_ptr();
229 template<>
230 inline void
231 _Sp_counted_base<_S_atomic>::
232 _M_add_ref_lock()
234 // Perform lock-free add-if-not-zero operation.
235 _Atomic_word __count;
238 __count = _M_use_count;
239 if (__count == 0)
240 __throw_bad_weak_ptr();
242 // Replace the current counter value with the old value + 1, as
243 // long as it's not changed meanwhile.
245 while (!__sync_bool_compare_and_swap(&_M_use_count, __count,
246 __count + 1));
250 // Forward declarations.
251 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
252 class __shared_ptr;
254 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
255 class __weak_ptr;
257 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
258 class __enable_shared_from_this;
260 template<typename _Tp>
261 class shared_ptr;
263 template<typename _Tp>
264 class weak_ptr;
266 template<typename _Tp>
267 struct owner_less;
269 template<typename _Tp>
270 class enable_shared_from_this;
272 template<_Lock_policy _Lp = __default_lock_policy>
273 class __weak_count;
275 template<_Lock_policy _Lp = __default_lock_policy>
276 class __shared_count;
279 // Counted ptr with no deleter or allocator support
280 template<typename _Ptr, _Lock_policy _Lp>
281 class _Sp_counted_ptr : public _Sp_counted_base<_Lp>
283 public:
284 explicit
285 _Sp_counted_ptr(_Ptr __p)
286 : _M_ptr(__p) { }
288 virtual void
289 _M_dispose() // nothrow
290 { delete _M_ptr; }
292 virtual void
293 _M_destroy() // nothrow
294 { delete this; }
296 virtual void*
297 _M_get_deleter(const std::type_info&)
298 { return 0; }
300 _Sp_counted_ptr(const _Sp_counted_ptr&) = delete;
301 _Sp_counted_ptr& operator=(const _Sp_counted_ptr&) = delete;
303 protected:
304 _Ptr _M_ptr; // copy constructor must not throw
307 template<>
308 inline void
309 _Sp_counted_ptr<nullptr_t, _S_single>::_M_dispose() { }
311 template<>
312 inline void
313 _Sp_counted_ptr<nullptr_t, _S_mutex>::_M_dispose() { }
315 template<>
316 inline void
317 _Sp_counted_ptr<nullptr_t, _S_atomic>::_M_dispose() { }
319 // Support for custom deleter and/or allocator
320 template<typename _Ptr, typename _Deleter, typename _Alloc, _Lock_policy _Lp>
321 class _Sp_counted_deleter : public _Sp_counted_base<_Lp>
323 typedef typename _Alloc::template
324 rebind<_Sp_counted_deleter>::other _My_alloc_type;
326 // Helper class that stores the Deleter and also acts as an allocator.
327 // Used to dispose of the owned pointer and the internal refcount
328 // Requires that copies of _Alloc can free each other's memory.
329 struct _My_Deleter
330 : public _My_alloc_type // copy constructor must not throw
332 _Deleter _M_del; // copy constructor must not throw
333 _My_Deleter(_Deleter __d, const _Alloc& __a)
334 : _My_alloc_type(__a), _M_del(__d) { }
337 public:
338 // __d(__p) must not throw.
339 _Sp_counted_deleter(_Ptr __p, _Deleter __d)
340 : _M_ptr(__p), _M_del(__d, _Alloc()) { }
342 // __d(__p) must not throw.
343 _Sp_counted_deleter(_Ptr __p, _Deleter __d, const _Alloc& __a)
344 : _M_ptr(__p), _M_del(__d, __a) { }
346 virtual void
347 _M_dispose() // nothrow
348 { _M_del._M_del(_M_ptr); }
350 virtual void
351 _M_destroy() // nothrow
353 _My_alloc_type __a(_M_del);
354 this->~_Sp_counted_deleter();
355 __a.deallocate(this, 1);
358 virtual void*
359 _M_get_deleter(const std::type_info& __ti)
361 #ifdef __GXX_RTTI
362 return __ti == typeid(_Deleter) ? &_M_del._M_del : 0;
363 #else
364 return 0;
365 #endif
368 protected:
369 _Ptr _M_ptr; // copy constructor must not throw
370 _My_Deleter _M_del; // copy constructor must not throw
373 // helpers for make_shared / allocate_shared
375 template<typename _Tp>
376 struct _Sp_destroy_inplace
378 void operator()(_Tp* __p) const { if (__p) __p->~_Tp(); }
381 struct _Sp_make_shared_tag { };
383 template<typename _Tp, typename _Alloc, _Lock_policy _Lp>
384 class _Sp_counted_ptr_inplace
385 : public _Sp_counted_deleter<_Tp*, _Sp_destroy_inplace<_Tp>, _Alloc, _Lp>
387 typedef _Sp_counted_deleter<_Tp*, _Sp_destroy_inplace<_Tp>, _Alloc, _Lp>
388 _Base_type;
390 public:
391 explicit
392 _Sp_counted_ptr_inplace(_Alloc __a)
393 : _Base_type(static_cast<_Tp*>(0), _Sp_destroy_inplace<_Tp>(), __a)
394 , _M_storage()
396 void* __p = &_M_storage;
397 ::new (__p) _Tp(); // might throw
398 _Base_type::_M_ptr = static_cast<_Tp*>(__p);
401 template<typename... _Args>
402 _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
403 : _Base_type(static_cast<_Tp*>(0), _Sp_destroy_inplace<_Tp>(), __a)
404 , _M_storage()
406 void* __p = &_M_storage;
407 ::new (__p) _Tp(std::forward<_Args>(__args)...); // might throw
408 _Base_type::_M_ptr = static_cast<_Tp*>(__p);
411 // Override because the allocator needs to know the dynamic type
412 virtual void
413 _M_destroy() // nothrow
415 typedef typename _Alloc::template
416 rebind<_Sp_counted_ptr_inplace>::other _My_alloc_type;
417 _My_alloc_type __a(_Base_type::_M_del);
418 this->~_Sp_counted_ptr_inplace();
419 __a.deallocate(this, 1);
422 // Sneaky trick so __shared_ptr can get the managed pointer
423 virtual void*
424 _M_get_deleter(const std::type_info& __ti)
426 #ifdef __GXX_RTTI
427 return __ti == typeid(_Sp_make_shared_tag)
428 ? static_cast<void*>(&_M_storage)
429 : _Base_type::_M_get_deleter(__ti);
430 #else
431 return 0;
432 #endif
435 private:
436 typename aligned_storage<sizeof(_Tp), alignment_of<_Tp>::value>::type
437 _M_storage;
440 template<_Lock_policy _Lp>
441 class __shared_count
443 public:
444 constexpr __shared_count() : _M_pi(0) // nothrow
447 template<typename _Ptr>
448 explicit
449 __shared_count(_Ptr __p) : _M_pi(0)
451 __try
453 _M_pi = new _Sp_counted_ptr<_Ptr, _Lp>(__p);
455 __catch(...)
457 delete __p;
458 __throw_exception_again;
462 template<typename _Ptr, typename _Deleter>
463 __shared_count(_Ptr __p, _Deleter __d) : _M_pi(0)
465 // The allocator's value_type doesn't matter, will rebind it anyway.
466 typedef std::allocator<int> _Alloc;
467 typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
468 typedef std::allocator<_Sp_cd_type> _Alloc2;
469 _Alloc2 __a2;
470 __try
472 _M_pi = __a2.allocate(1);
473 ::new(static_cast<void*>(_M_pi)) _Sp_cd_type(__p, __d);
475 __catch(...)
477 __d(__p); // Call _Deleter on __p.
478 if (_M_pi)
479 __a2.deallocate(static_cast<_Sp_cd_type*>(_M_pi), 1);
480 __throw_exception_again;
484 template<typename _Ptr, typename _Deleter, typename _Alloc>
485 __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0)
487 typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
488 typedef typename _Alloc::template rebind<_Sp_cd_type>::other _Alloc2;
489 _Alloc2 __a2(__a);
490 __try
492 _M_pi = __a2.allocate(1);
493 ::new(static_cast<void*>(_M_pi)) _Sp_cd_type(__p, __d, __a);
495 __catch(...)
497 __d(__p); // Call _Deleter on __p.
498 if (_M_pi)
499 __a2.deallocate(static_cast<_Sp_cd_type*>(_M_pi), 1);
500 __throw_exception_again;
504 template<typename _Tp, typename _Alloc, typename... _Args>
505 __shared_count(_Sp_make_shared_tag, _Tp*, const _Alloc& __a,
506 _Args&&... __args)
507 : _M_pi(0)
509 typedef _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> _Sp_cp_type;
510 typedef typename _Alloc::template rebind<_Sp_cp_type>::other _Alloc2;
511 _Alloc2 __a2(__a);
512 __try
514 _M_pi = __a2.allocate(1);
515 ::new(static_cast<void*>(_M_pi)) _Sp_cp_type(__a,
516 std::forward<_Args>(__args)...);
518 __catch(...)
520 if (_M_pi)
521 __a2.deallocate(static_cast<_Sp_cp_type*>(_M_pi), 1);
522 __throw_exception_again;
526 #if _GLIBCXX_DEPRECATED
527 // Special case for auto_ptr<_Tp> to provide the strong guarantee.
528 template<typename _Tp>
529 explicit
530 __shared_count(std::auto_ptr<_Tp>&& __r)
531 : _M_pi(new _Sp_counted_ptr<_Tp*, _Lp>(__r.get()))
532 { __r.release(); }
533 #endif
535 // Special case for unique_ptr<_Tp,_Del> to provide the strong guarantee.
536 template<typename _Tp, typename _Del>
537 explicit
538 __shared_count(std::unique_ptr<_Tp, _Del>&& __r)
539 : _M_pi(_S_create_from_up(std::move(__r)))
540 { __r.release(); }
542 // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
543 explicit __shared_count(const __weak_count<_Lp>& __r);
545 ~__shared_count() // nothrow
547 if (_M_pi != 0)
548 _M_pi->_M_release();
551 __shared_count(const __shared_count& __r)
552 : _M_pi(__r._M_pi) // nothrow
554 if (_M_pi != 0)
555 _M_pi->_M_add_ref_copy();
558 __shared_count&
559 operator=(const __shared_count& __r) // nothrow
561 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
562 if (__tmp != _M_pi)
564 if (__tmp != 0)
565 __tmp->_M_add_ref_copy();
566 if (_M_pi != 0)
567 _M_pi->_M_release();
568 _M_pi = __tmp;
570 return *this;
573 void
574 _M_swap(__shared_count& __r) // nothrow
576 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
577 __r._M_pi = _M_pi;
578 _M_pi = __tmp;
581 long
582 _M_get_use_count() const // nothrow
583 { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
585 bool
586 _M_unique() const // nothrow
587 { return this->_M_get_use_count() == 1; }
589 void*
590 _M_get_deleter(const std::type_info& __ti) const
591 { return _M_pi ? _M_pi->_M_get_deleter(__ti) : 0; }
593 bool
594 _M_less(const __shared_count& __rhs) const
595 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
597 bool
598 _M_less(const __weak_count<_Lp>& __rhs) const
599 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
601 // Friend function injected into enclosing namespace and found by ADL
602 friend inline bool
603 operator==(const __shared_count& __a, const __shared_count& __b)
604 { return __a._M_pi == __b._M_pi; }
606 private:
607 friend class __weak_count<_Lp>;
609 template<typename _Tp, typename _Del>
610 static _Sp_counted_base<_Lp>*
611 _S_create_from_up(std::unique_ptr<_Tp, _Del>&& __r,
612 typename std::enable_if<!std::is_reference<_Del>::value>::type* = 0)
614 return new _Sp_counted_deleter<_Tp*, _Del, std::allocator<_Tp>,
615 _Lp>(__r.get(), __r.get_deleter());
618 template<typename _Tp, typename _Del>
619 static _Sp_counted_base<_Lp>*
620 _S_create_from_up(std::unique_ptr<_Tp, _Del>&& __r,
621 typename std::enable_if<std::is_reference<_Del>::value>::type* = 0)
623 typedef typename std::remove_reference<_Del>::type _Del1;
624 typedef std::reference_wrapper<_Del1> _Del2;
625 return new _Sp_counted_deleter<_Tp*, _Del2, std::allocator<_Tp>,
626 _Lp>(__r.get(), std::ref(__r.get_deleter()));
629 _Sp_counted_base<_Lp>* _M_pi;
633 template<_Lock_policy _Lp>
634 class __weak_count
636 public:
637 constexpr __weak_count() : _M_pi(0) // nothrow
640 __weak_count(const __shared_count<_Lp>& __r) : _M_pi(__r._M_pi) // nothrow
642 if (_M_pi != 0)
643 _M_pi->_M_weak_add_ref();
646 __weak_count(const __weak_count<_Lp>& __r) : _M_pi(__r._M_pi) // nothrow
648 if (_M_pi != 0)
649 _M_pi->_M_weak_add_ref();
652 ~__weak_count() // nothrow
654 if (_M_pi != 0)
655 _M_pi->_M_weak_release();
658 __weak_count<_Lp>&
659 operator=(const __shared_count<_Lp>& __r) // nothrow
661 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
662 if (__tmp != 0)
663 __tmp->_M_weak_add_ref();
664 if (_M_pi != 0)
665 _M_pi->_M_weak_release();
666 _M_pi = __tmp;
667 return *this;
670 __weak_count<_Lp>&
671 operator=(const __weak_count<_Lp>& __r) // nothrow
673 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
674 if (__tmp != 0)
675 __tmp->_M_weak_add_ref();
676 if (_M_pi != 0)
677 _M_pi->_M_weak_release();
678 _M_pi = __tmp;
679 return *this;
682 void
683 _M_swap(__weak_count<_Lp>& __r) // nothrow
685 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
686 __r._M_pi = _M_pi;
687 _M_pi = __tmp;
690 long
691 _M_get_use_count() const // nothrow
692 { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
694 bool
695 _M_less(const __weak_count& __rhs) const
696 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
698 bool
699 _M_less(const __shared_count<_Lp>& __rhs) const
700 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
702 // Friend function injected into enclosing namespace and found by ADL
703 friend inline bool
704 operator==(const __weak_count& __a, const __weak_count& __b)
705 { return __a._M_pi == __b._M_pi; }
707 private:
708 friend class __shared_count<_Lp>;
710 _Sp_counted_base<_Lp>* _M_pi;
713 // Now that __weak_count is defined we can define this constructor:
714 template<_Lock_policy _Lp>
715 inline __shared_count<_Lp>:: __shared_count(const __weak_count<_Lp>& __r)
716 : _M_pi(__r._M_pi)
718 if (_M_pi != 0)
719 _M_pi->_M_add_ref_lock();
720 else
721 __throw_bad_weak_ptr();
725 // Support for enable_shared_from_this.
727 // Friend of __enable_shared_from_this.
728 template<_Lock_policy _Lp, typename _Tp1, typename _Tp2>
729 void
730 __enable_shared_from_this_helper(const __shared_count<_Lp>&,
731 const __enable_shared_from_this<_Tp1,
732 _Lp>*, const _Tp2*);
734 // Friend of enable_shared_from_this.
735 template<typename _Tp1, typename _Tp2>
736 void
737 __enable_shared_from_this_helper(const __shared_count<>&,
738 const enable_shared_from_this<_Tp1>*,
739 const _Tp2*);
741 template<_Lock_policy _Lp>
742 inline void
743 __enable_shared_from_this_helper(const __shared_count<_Lp>&, ...)
747 template<typename _Tp, _Lock_policy _Lp>
748 class __shared_ptr
750 public:
751 typedef _Tp element_type;
753 constexpr __shared_ptr()
754 : _M_ptr(0), _M_refcount() // never throws
757 template<typename _Tp1>
758 explicit __shared_ptr(_Tp1* __p)
759 : _M_ptr(__p), _M_refcount(__p)
761 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
762 static_assert( sizeof(_Tp1) > 0, "incomplete type" );
763 __enable_shared_from_this_helper(_M_refcount, __p, __p);
766 template<typename _Tp1, typename _Deleter>
767 __shared_ptr(_Tp1* __p, _Deleter __d)
768 : _M_ptr(__p), _M_refcount(__p, __d)
770 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
771 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
772 __enable_shared_from_this_helper(_M_refcount, __p, __p);
775 template<typename _Tp1, typename _Deleter, typename _Alloc>
776 __shared_ptr(_Tp1* __p, _Deleter __d, _Alloc __a)
777 : _M_ptr(__p), _M_refcount(__p, __d, std::move(__a))
779 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
780 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
781 __enable_shared_from_this_helper(_M_refcount, __p, __p);
784 template<typename _Deleter>
785 __shared_ptr(nullptr_t __p, _Deleter __d)
786 : _M_ptr(0), _M_refcount(__p, __d)
789 template<typename _Deleter, typename _Alloc>
790 __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
791 : _M_ptr(0), _M_refcount(__p, __d, std::move(__a))
794 template<typename _Tp1>
795 __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r, _Tp* __p)
796 : _M_ptr(__p), _M_refcount(__r._M_refcount) // never throws
799 // generated copy constructor, assignment, destructor are fine.
801 template<typename _Tp1, typename = typename
802 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
803 __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r)
804 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
807 __shared_ptr(__shared_ptr&& __r)
808 : _M_ptr(__r._M_ptr), _M_refcount() // never throws
810 _M_refcount._M_swap(__r._M_refcount);
811 __r._M_ptr = 0;
814 template<typename _Tp1, typename = typename
815 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
816 __shared_ptr(__shared_ptr<_Tp1, _Lp>&& __r)
817 : _M_ptr(__r._M_ptr), _M_refcount() // never throws
819 _M_refcount._M_swap(__r._M_refcount);
820 __r._M_ptr = 0;
823 template<typename _Tp1>
824 explicit __shared_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
825 : _M_refcount(__r._M_refcount) // may throw
827 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
829 // It is now safe to copy __r._M_ptr, as
830 // _M_refcount(__r._M_refcount) did not throw.
831 _M_ptr = __r._M_ptr;
834 // If an exception is thrown this constructor has no effect.
835 template<typename _Tp1, typename _Del>
836 __shared_ptr(std::unique_ptr<_Tp1, _Del>&& __r)
837 : _M_ptr(__r.get()), _M_refcount()
839 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
840 _Tp1* __tmp = __r.get();
841 _M_refcount = __shared_count<_Lp>(std::move(__r));
842 __enable_shared_from_this_helper(_M_refcount, __tmp, __tmp);
845 #if _GLIBCXX_DEPRECATED
846 // Postcondition: use_count() == 1 and __r.get() == 0
847 template<typename _Tp1>
848 __shared_ptr(std::auto_ptr<_Tp1>&& __r)
849 : _M_ptr(__r.get()), _M_refcount()
851 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
852 static_assert( sizeof(_Tp1) > 0, "incomplete type" );
853 _Tp1* __tmp = __r.get();
854 _M_refcount = __shared_count<_Lp>(std::move(__r));
855 __enable_shared_from_this_helper(_M_refcount, __tmp, __tmp);
857 #endif
859 /* TODO: use delegating constructor */
860 constexpr __shared_ptr(nullptr_t)
861 : _M_ptr(0), _M_refcount() // never throws
864 template<typename _Tp1>
865 __shared_ptr&
866 operator=(const __shared_ptr<_Tp1, _Lp>& __r) // never throws
868 _M_ptr = __r._M_ptr;
869 _M_refcount = __r._M_refcount; // __shared_count::op= doesn't throw
870 return *this;
873 #if _GLIBCXX_DEPRECATED
874 template<typename _Tp1>
875 __shared_ptr&
876 operator=(std::auto_ptr<_Tp1>&& __r)
878 __shared_ptr(std::move(__r)).swap(*this);
879 return *this;
881 #endif
883 __shared_ptr&
884 operator=(__shared_ptr&& __r)
886 __shared_ptr(std::move(__r)).swap(*this);
887 return *this;
890 template<class _Tp1>
891 __shared_ptr&
892 operator=(__shared_ptr<_Tp1, _Lp>&& __r)
894 __shared_ptr(std::move(__r)).swap(*this);
895 return *this;
898 template<typename _Tp1, typename _Del>
899 __shared_ptr&
900 operator=(std::unique_ptr<_Tp1, _Del>&& __r)
902 __shared_ptr(std::move(__r)).swap(*this);
903 return *this;
906 void
907 reset() // never throws
908 { __shared_ptr().swap(*this); }
910 template<typename _Tp1>
911 void
912 reset(_Tp1* __p) // _Tp1 must be complete.
914 // Catch self-reset errors.
915 _GLIBCXX_DEBUG_ASSERT(__p == 0 || __p != _M_ptr);
916 __shared_ptr(__p).swap(*this);
919 template<typename _Tp1, typename _Deleter>
920 void
921 reset(_Tp1* __p, _Deleter __d)
922 { __shared_ptr(__p, __d).swap(*this); }
924 template<typename _Tp1, typename _Deleter, typename _Alloc>
925 void
926 reset(_Tp1* __p, _Deleter __d, _Alloc __a)
927 { __shared_ptr(__p, __d, std::move(__a)).swap(*this); }
929 // Allow class instantiation when _Tp is [cv-qual] void.
930 typename std::add_lvalue_reference<_Tp>::type
931 operator*() const // never throws
933 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
934 return *_M_ptr;
937 _Tp*
938 operator->() const // never throws
940 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
941 return _M_ptr;
944 _Tp*
945 get() const // never throws
946 { return _M_ptr; }
948 explicit operator bool() const // never throws
949 { return _M_ptr == 0 ? false : true; }
951 bool
952 unique() const // never throws
953 { return _M_refcount._M_unique(); }
955 long
956 use_count() const // never throws
957 { return _M_refcount._M_get_use_count(); }
959 void
960 swap(__shared_ptr<_Tp, _Lp>& __other) // never throws
962 std::swap(_M_ptr, __other._M_ptr);
963 _M_refcount._M_swap(__other._M_refcount);
966 template<typename _Tp1>
967 bool
968 owner_before(__shared_ptr<_Tp1, _Lp> const& __rhs) const
969 { return _M_refcount._M_less(__rhs._M_refcount); }
971 template<typename _Tp1>
972 bool
973 owner_before(__weak_ptr<_Tp1, _Lp> const& __rhs) const
974 { return _M_refcount._M_less(__rhs._M_refcount); }
976 #ifdef __GXX_RTTI
977 protected:
978 // This constructor is non-standard, it is used by allocate_shared.
979 template<typename _Alloc, typename... _Args>
980 __shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a,
981 _Args&&... __args)
982 : _M_ptr(), _M_refcount(__tag, (_Tp*)0, __a,
983 std::forward<_Args>(__args)...)
985 // _M_ptr needs to point to the newly constructed object.
986 // This relies on _Sp_counted_ptr_inplace::_M_get_deleter.
987 void* __p = _M_refcount._M_get_deleter(typeid(__tag));
988 _M_ptr = static_cast<_Tp*>(__p);
989 __enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr);
991 #else
992 template<typename _Alloc>
993 struct _Deleter
995 void operator()(_Tp* __ptr)
997 _M_alloc.destroy(__ptr);
998 _M_alloc.deallocate(__ptr, 1);
1000 _Alloc _M_alloc;
1003 template<typename _Alloc, typename... _Args>
1004 __shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a,
1005 _Args&&... __args)
1006 : _M_ptr(), _M_refcount()
1008 typedef typename _Alloc::template rebind<_Tp>::other _Alloc2;
1009 _Deleter<_Alloc2> __del = { _Alloc2(__a) };
1010 _M_ptr = __del._M_alloc.allocate(1);
1011 __try
1013 __del._M_alloc.construct(_M_ptr, std::forward<_Args>(__args)...);
1015 __catch(...)
1017 __del._M_alloc.deallocate(_M_ptr, 1);
1018 __throw_exception_again;
1020 __shared_count<_Lp> __count(_M_ptr, __del, __del._M_alloc);
1021 _M_refcount._M_swap(__count);
1022 __enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr);
1024 #endif
1026 template<typename _Tp1, _Lock_policy _Lp1, typename _Alloc,
1027 typename... _Args>
1028 friend __shared_ptr<_Tp1, _Lp1>
1029 __allocate_shared(const _Alloc& __a, _Args&&... __args);
1031 private:
1032 void*
1033 _M_get_deleter(const std::type_info& __ti) const
1034 { return _M_refcount._M_get_deleter(__ti); }
1036 template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
1037 template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
1039 template<typename _Del, typename _Tp1, _Lock_policy _Lp1>
1040 friend _Del* get_deleter(const __shared_ptr<_Tp1, _Lp1>&);
1042 _Tp* _M_ptr; // Contained pointer.
1043 __shared_count<_Lp> _M_refcount; // Reference counter.
1047 // 20.8.13.2.7 shared_ptr comparisons
1048 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1049 inline bool
1050 operator==(const __shared_ptr<_Tp1, _Lp>& __a,
1051 const __shared_ptr<_Tp2, _Lp>& __b)
1052 { return __a.get() == __b.get(); }
1054 template<typename _Tp, _Lock_policy _Lp>
1055 inline bool
1056 operator==(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
1057 { return __a.get() == nullptr; }
1059 template<typename _Tp, _Lock_policy _Lp>
1060 inline bool
1061 operator==(nullptr_t, const __shared_ptr<_Tp, _Lp>& __b)
1062 { return nullptr == __b.get(); }
1064 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1065 inline bool
1066 operator!=(const __shared_ptr<_Tp1, _Lp>& __a,
1067 const __shared_ptr<_Tp2, _Lp>& __b)
1068 { return __a.get() != __b.get(); }
1070 template<typename _Tp, _Lock_policy _Lp>
1071 inline bool
1072 operator!=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
1073 { return __a.get() != nullptr; }
1075 template<typename _Tp, _Lock_policy _Lp>
1076 inline bool
1077 operator!=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __b)
1078 { return nullptr != __b.get(); }
1080 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1081 inline bool
1082 operator<(const __shared_ptr<_Tp1, _Lp>& __a,
1083 const __shared_ptr<_Tp2, _Lp>& __b)
1084 { return __a.get() < __b.get(); }
1086 template<typename _Sp>
1087 struct _Sp_less : public binary_function<_Sp, _Sp, bool>
1089 bool
1090 operator()(const _Sp& __lhs, const _Sp& __rhs) const
1092 typedef typename _Sp::element_type element_type;
1093 return std::less<element_type*>()(__lhs.get(), __rhs.get());
1097 template<typename _Tp, _Lock_policy _Lp>
1098 struct less<__shared_ptr<_Tp, _Lp>>
1099 : public _Sp_less<__shared_ptr<_Tp, _Lp>>
1100 { };
1102 // 2.2.3.8 shared_ptr specialized algorithms.
1103 template<typename _Tp, _Lock_policy _Lp>
1104 inline void
1105 swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b)
1106 { __a.swap(__b); }
1108 // 2.2.3.9 shared_ptr casts
1110 // The seemingly equivalent code:
1111 // shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get()))
1112 // will eventually result in undefined behaviour, attempting to
1113 // delete the same object twice.
1114 /// static_pointer_cast
1115 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
1116 inline __shared_ptr<_Tp, _Lp>
1117 static_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r)
1118 { return __shared_ptr<_Tp, _Lp>(__r, static_cast<_Tp*>(__r.get())); }
1120 // The seemingly equivalent code:
1121 // shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get()))
1122 // will eventually result in undefined behaviour, attempting to
1123 // delete the same object twice.
1124 /// const_pointer_cast
1125 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
1126 inline __shared_ptr<_Tp, _Lp>
1127 const_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r)
1128 { return __shared_ptr<_Tp, _Lp>(__r, const_cast<_Tp*>(__r.get())); }
1130 // The seemingly equivalent code:
1131 // shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))
1132 // will eventually result in undefined behaviour, attempting to
1133 // delete the same object twice.
1134 /// dynamic_pointer_cast
1135 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
1136 inline __shared_ptr<_Tp, _Lp>
1137 dynamic_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r)
1139 if (_Tp* __p = dynamic_cast<_Tp*>(__r.get()))
1140 return __shared_ptr<_Tp, _Lp>(__r, __p);
1141 return __shared_ptr<_Tp, _Lp>();
1145 template<typename _Tp, _Lock_policy _Lp>
1146 class __weak_ptr
1148 public:
1149 typedef _Tp element_type;
1151 constexpr __weak_ptr()
1152 : _M_ptr(0), _M_refcount() // never throws
1155 // Generated copy constructor, assignment, destructor are fine.
1157 // The "obvious" converting constructor implementation:
1159 // template<typename _Tp1>
1160 // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
1161 // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
1162 // { }
1164 // has a serious problem.
1166 // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
1167 // conversion may require access to *__r._M_ptr (virtual inheritance).
1169 // It is not possible to avoid spurious access violations since
1170 // in multithreaded programs __r._M_ptr may be invalidated at any point.
1171 template<typename _Tp1, typename = typename
1172 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
1173 __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
1174 : _M_refcount(__r._M_refcount) // never throws
1175 { _M_ptr = __r.lock().get(); }
1177 template<typename _Tp1, typename = typename
1178 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
1179 __weak_ptr(const __shared_ptr<_Tp1, _Lp>& __r)
1180 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
1183 template<typename _Tp1>
1184 __weak_ptr&
1185 operator=(const __weak_ptr<_Tp1, _Lp>& __r) // never throws
1187 _M_ptr = __r.lock().get();
1188 _M_refcount = __r._M_refcount;
1189 return *this;
1192 template<typename _Tp1>
1193 __weak_ptr&
1194 operator=(const __shared_ptr<_Tp1, _Lp>& __r) // never throws
1196 _M_ptr = __r._M_ptr;
1197 _M_refcount = __r._M_refcount;
1198 return *this;
1201 __shared_ptr<_Tp, _Lp>
1202 lock() const // never throws
1204 #ifdef __GTHREADS
1205 // Optimization: avoid throw overhead.
1206 if (expired())
1207 return __shared_ptr<element_type, _Lp>();
1209 __try
1211 return __shared_ptr<element_type, _Lp>(*this);
1213 __catch(const bad_weak_ptr&)
1215 // Q: How can we get here?
1216 // A: Another thread may have invalidated r after the
1217 // use_count test above.
1218 return __shared_ptr<element_type, _Lp>();
1221 #else
1222 // Optimization: avoid try/catch overhead when single threaded.
1223 return expired() ? __shared_ptr<element_type, _Lp>()
1224 : __shared_ptr<element_type, _Lp>(*this);
1226 #endif
1227 } // XXX MT
1229 long
1230 use_count() const // never throws
1231 { return _M_refcount._M_get_use_count(); }
1233 bool
1234 expired() const // never throws
1235 { return _M_refcount._M_get_use_count() == 0; }
1237 template<typename _Tp1>
1238 bool
1239 owner_before(const __shared_ptr<_Tp1, _Lp>& __rhs) const
1240 { return _M_refcount._M_less(__rhs._M_refcount); }
1242 template<typename _Tp1>
1243 bool
1244 owner_before(const __weak_ptr<_Tp1, _Lp>& __rhs) const
1245 { return _M_refcount._M_less(__rhs._M_refcount); }
1247 void
1248 reset() // never throws
1249 { __weak_ptr().swap(*this); }
1251 void
1252 swap(__weak_ptr& __s) // never throws
1254 std::swap(_M_ptr, __s._M_ptr);
1255 _M_refcount._M_swap(__s._M_refcount);
1258 private:
1259 // Used by __enable_shared_from_this.
1260 void
1261 _M_assign(_Tp* __ptr, const __shared_count<_Lp>& __refcount)
1263 _M_ptr = __ptr;
1264 _M_refcount = __refcount;
1267 template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
1268 template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
1269 friend class __enable_shared_from_this<_Tp, _Lp>;
1270 friend class enable_shared_from_this<_Tp>;
1272 _Tp* _M_ptr; // Contained pointer.
1273 __weak_count<_Lp> _M_refcount; // Reference counter.
1276 // 20.8.13.3.7 weak_ptr specialized algorithms.
1277 template<typename _Tp, _Lock_policy _Lp>
1278 inline void
1279 swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b)
1280 { __a.swap(__b); }
1282 template<typename _Tp, typename _Tp1>
1283 struct _Sp_owner_less : public binary_function<_Tp, _Tp, bool>
1285 bool
1286 operator()(const _Tp& __lhs, const _Tp& __rhs) const
1287 { return __lhs.owner_before(__rhs); }
1289 bool
1290 operator()(const _Tp& __lhs, const _Tp1& __rhs) const
1291 { return __lhs.owner_before(__rhs); }
1293 bool
1294 operator()(const _Tp1& __lhs, const _Tp& __rhs) const
1295 { return __lhs.owner_before(__rhs); }
1298 template<typename _Tp, _Lock_policy _Lp>
1299 struct owner_less<__shared_ptr<_Tp, _Lp>>
1300 : public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>>
1301 { };
1303 template<typename _Tp, _Lock_policy _Lp>
1304 struct owner_less<__weak_ptr<_Tp, _Lp>>
1305 : public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>>
1306 { };
1309 template<typename _Tp, _Lock_policy _Lp>
1310 class __enable_shared_from_this
1312 protected:
1313 constexpr __enable_shared_from_this() { }
1315 __enable_shared_from_this(const __enable_shared_from_this&) { }
1317 __enable_shared_from_this&
1318 operator=(const __enable_shared_from_this&)
1319 { return *this; }
1321 ~__enable_shared_from_this() { }
1323 public:
1324 __shared_ptr<_Tp, _Lp>
1325 shared_from_this()
1326 { return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
1328 __shared_ptr<const _Tp, _Lp>
1329 shared_from_this() const
1330 { return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
1332 private:
1333 template<typename _Tp1>
1334 void
1335 _M_weak_assign(_Tp1* __p, const __shared_count<_Lp>& __n) const
1336 { _M_weak_this._M_assign(__p, __n); }
1338 template<typename _Tp1>
1339 friend void
1340 __enable_shared_from_this_helper(const __shared_count<_Lp>& __pn,
1341 const __enable_shared_from_this* __pe,
1342 const _Tp1* __px)
1344 if (__pe != 0)
1345 __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
1348 mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
1352 template<typename _Tp, _Lock_policy _Lp, typename _Alloc, typename... _Args>
1353 inline __shared_ptr<_Tp, _Lp>
1354 __allocate_shared(const _Alloc& __a, _Args&&... __args)
1356 return __shared_ptr<_Tp, _Lp>(_Sp_make_shared_tag(), __a,
1357 std::forward<_Args>(__args)...);
1360 template<typename _Tp, _Lock_policy _Lp, typename... _Args>
1361 inline __shared_ptr<_Tp, _Lp>
1362 __make_shared(_Args&&... __args)
1364 typedef typename std::remove_const<_Tp>::type _Tp_nc;
1365 return __allocate_shared<_Tp, _Lp>(std::allocator<_Tp_nc>(),
1366 std::forward<_Args>(__args)...);
1369 /// std::hash specialization for __shared_ptr.
1370 template<typename _Tp, _Lock_policy _Lp>
1371 struct hash<__shared_ptr<_Tp, _Lp>>
1372 : public std::unary_function<__shared_ptr<_Tp, _Lp>, size_t>
1374 size_t
1375 operator()(const __shared_ptr<_Tp, _Lp>& __s) const
1376 { return std::hash<_Tp*>()(__s.get()); }
1379 _GLIBCXX_END_NAMESPACE
1381 #endif // _SHARED_PTR_BASE_H