1 // <tr1/memory> -*- C++ -*-
3 // Copyright (C) 2005 Free Software Foundation, Inc.
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 2, or (at your option)
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 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 // boost/checked_delete.hpp
31 // Copyright (c) 2002, 2003 Peter Dimov
32 // Copyright (c) 2003 Daniel Frey
33 // Copyright (c) 2003 Howard Hinnant
35 // boost/shared_count.hpp
36 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
39 // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
40 // Copyright (c) 2001, 2002, 2003 Peter Dimov
43 // Copyright (c) 2001, 2002, 2003 Peter Dimov
45 // enable_shared_from_this.hpp
46 // Copyright (c) 2002 Peter Dimov
52 #include <functional> // std::less
53 #include <exception> // std::exception
54 #include <new> // std::bad_alloc
55 #include <typeinfo> // std::type_info in get_deleter
56 #include <cstddef> // std::size_t
57 #include <algorithm> // for std::swap
58 #include <iosfwd> // for std::basic_ostream
59 #include <cstdlib> // for std::abort
61 #include <bits/gthr.h>
62 #include <bits/atomicity.h>
63 #include <bits/functexcept.h>
64 #include <debug/debug.h>
72 class bad_weak_ptr : public std::exception
76 virtual char const* what() const throw()
78 return "tr1::bad_weak_ptr";
82 // Helper for exception objects in <tr1/memory>
83 // TODO this should be defined in a different file.
85 __throw_bad_weak_ptr()
95 // verify that types are complete for increased safety
97 template <typename _Tp>
99 checked_delete(_Tp * __x)
101 // intentionally complex - simplification causes regressions
102 typedef char type_must_be_complete[ sizeof(_Tp)? 1: -1 ];
103 (void) sizeof(type_must_be_complete);
107 template <typename _Tp>
108 struct checked_deleter
110 typedef void result_type;
111 typedef _Tp* argument_type;
114 operator()(_Tp * x) const
115 { std::tr1::checked_delete(x); }
119 class _Sp_counted_base
124 : _M_use_count(1), _M_weak_count(1)
128 ~_Sp_counted_base() // nothrow
131 // dispose() is called when _M_use_count drops to zero, to release
132 // the resources managed by *this.
134 dispose() = 0; // nothrow
136 // destroy() is called when _M_weak_count drops to zero.
144 get_deleter(const std::type_info&) = 0;
149 __gnu_cxx::__atomic_add(&_M_use_count, 1);
155 if (_M_use_count <= 0) // TODO not yet MT safe XXX
157 __throw_bad_weak_ptr();
159 __gnu_cxx::__atomic_add(&_M_use_count, 1);
165 if (__gnu_cxx::__exchange_and_add(&_M_use_count, -1) <= 1)
173 weak_add_ref() // nothrow
175 __gnu_cxx::__atomic_add(&_M_weak_count, 1);
179 weak_release() // nothrow
181 if (__gnu_cxx::__exchange_and_add(&_M_weak_count, -1) <= 1)
188 use_count() const // nothrow
190 return _M_use_count; // XXX is this MT safe?
195 _Sp_counted_base(_Sp_counted_base const&);
196 _Sp_counted_base& operator= (_Sp_counted_base const&);
198 _Atomic_word _M_use_count; // #shared
199 _Atomic_word _M_weak_count; // #weak + (#shared != 0)
202 template <typename _Ptr, typename _Deleter>
203 class _Sp_counted_base_impl : public _Sp_counted_base
209 * @pre d(p) must not throw.
211 _Sp_counted_base_impl(_Ptr __p, _Deleter __d)
212 : _M_ptr(__p), _M_del(__d)
222 get_deleter(const std::type_info& __ti)
224 return __ti == typeid(_Deleter) ? &_M_del : 0;
228 _Sp_counted_base_impl(const _Sp_counted_base_impl&);
229 _Sp_counted_base_impl& operator=(const _Sp_counted_base_impl&);
231 _Ptr _M_ptr; // copy constructor must not throw
232 _Deleter _M_del; // copy constructor must not throw
241 _Sp_counted_base* _M_pi;
243 friend class weak_count;
248 : _M_pi(0) // nothrow
251 template <typename _Ptr, typename _Deleter>
252 shared_count(_Ptr __p, _Deleter __d)
257 _M_pi = new _Sp_counted_base_impl<_Ptr, _Deleter>(__p, __d);
261 __d(__p); // delete __p
262 __throw_exception_again;
266 // auto_ptr<_Tp> is special cased to provide the strong guarantee
268 template <typename _Tp>
269 explicit shared_count(std::auto_ptr<_Tp>& __r)
270 : _M_pi(new _Sp_counted_base_impl<_Tp*,checked_deleter<_Tp> >(
271 __r.get(), checked_deleter<_Tp>()
275 // throws bad_weak_ptr when __r.use_count() == 0
276 explicit shared_count(const weak_count& __r);
278 ~shared_count() // nothrow
284 shared_count(const shared_count& __r)
285 : _M_pi(__r._M_pi) // nothrow
288 _M_pi->add_ref_copy();
292 operator=(const shared_count& __r) // nothrow
294 _Sp_counted_base* __tmp = __r._M_pi;
299 __tmp->add_ref_copy();
307 void swap(shared_count& __r) // nothrow
309 _Sp_counted_base* __tmp = __r._M_pi;
315 use_count() const // nothrow
316 { return _M_pi != 0 ? _M_pi->use_count() : 0; }
319 unique() const // nothrow
320 { return this->use_count() == 1; }
323 operator==(const shared_count& __a, const shared_count& __b)
324 { return __a._M_pi == __b._M_pi; }
327 operator<(const shared_count& __a, const shared_count& __b)
328 { return std::less<_Sp_counted_base*>()(__a._M_pi, __b._M_pi); }
331 get_deleter(const std::type_info& __ti) const
332 { return _M_pi ? _M_pi->get_deleter(__ti) : 0; }
340 _Sp_counted_base * _M_pi;
342 friend class shared_count;
347 : _M_pi(0) // nothrow
350 weak_count(const shared_count& __r)
351 : _M_pi(__r._M_pi) // nothrow
354 _M_pi->weak_add_ref();
357 weak_count(const weak_count& __r)
358 : _M_pi(__r._M_pi) // nothrow
361 _M_pi->weak_add_ref();
364 ~weak_count() // nothrow
367 _M_pi->weak_release();
371 operator=(const shared_count& __r) // nothrow
373 _Sp_counted_base* __tmp = __r._M_pi;
375 __tmp->weak_add_ref();
377 _M_pi->weak_release();
384 operator=(const weak_count& __r) // nothrow
386 _Sp_counted_base * __tmp = __r._M_pi;
388 __tmp->weak_add_ref();
390 _M_pi->weak_release();
397 swap(weak_count& __r) // nothrow
399 _Sp_counted_base * __tmp = __r._M_pi;
405 use_count() const // nothrow
406 { return _M_pi != 0 ? _M_pi->use_count() : 0; }
409 operator==(const weak_count& __a, const weak_count& __b)
410 { return __a._M_pi == __b._M_pi; }
413 operator<(const weak_count& __a, const weak_count& __b)
414 { return std::less<_Sp_counted_base*>()(__a._M_pi, __b._M_pi); }
418 shared_count::shared_count(const weak_count& __r)
423 _M_pi->add_ref_lock();
427 __throw_bad_weak_ptr();
432 template <typename _Tp> class weak_ptr;
433 template <typename _Tp> class enable_shared_from_this;
435 struct __static_cast_tag {};
436 struct __const_cast_tag {};
437 struct __dynamic_cast_tag {};
438 struct __polymorphic_cast_tag {};
440 template<class _Tp> struct shared_ptr_traits
442 typedef _Tp & reference;
445 template<> struct shared_ptr_traits<void>
447 typedef void reference;
450 template<> struct shared_ptr_traits<void const>
452 typedef void reference;
455 template<> struct shared_ptr_traits<void volatile>
457 typedef void reference;
460 template<> struct shared_ptr_traits<void const volatile>
462 typedef void reference;
466 // enable_shared_from_this support
468 // friend of enable_shared_from_this
469 template <typename _Tp1, typename _Tp2>
471 __enable_shared_from_this( const shared_count& __pn,
472 const enable_shared_from_this<_Tp1>* __pe,
476 __enable_shared_from_this(const shared_count&, ...)
480 * @class shared_ptr <tr1/memory>
482 * A smart pointer with reference-counted copy semantics.
483 * The object pointed to is deleted when the last shared_ptr pointing to it
484 * is destroyed or reset.
487 template <typename _Tp>
490 typedef typename shared_ptr_traits<_Tp>::reference _Reference;
494 typedef _Tp element_type;
496 /** @brief Construct an empty %shared_ptr.
497 * @post use_count()==0 && get()==0
499 shared_ptr() : _M_ptr(0), _M_refcount() // never throws
502 /** @brief Construct a %shared_ptr that owns the pointer @a p.
503 * @param p A pointer that is convertible to element_type*.
504 * @post use_count()==1 && get()==p
505 * @throw std::bad_alloc, in which case @c delete @a p is called.
507 template <typename _Tp1>
508 explicit shared_ptr(_Tp1* __p)
509 : _M_ptr(__p), _M_refcount(__p, checked_deleter<_Tp1>())
511 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
512 // __glibcxx_function_requires(_CompleteConcept<_Tp1*>)
514 __enable_shared_from_this( _M_refcount, __p, __p );
518 // Requirements: D's copy constructor and destructor must not throw
520 // shared_ptr will release p by calling d(p)
522 /** @brief Construct a %shared_ptr that owns the pointer @a p
523 * and the deleter @a d.
524 * @param p A pointer.
525 * @param d A deleter.
526 * @post use_count()==1 && get()==p
527 * @throw std::bad_alloc, in which case @a d(p) is called.
529 template <typename _Tp1, typename _Deleter>
530 shared_ptr(_Tp1* __p, _Deleter __d)
531 : _M_ptr(__p), _M_refcount(__p, __d)
533 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
534 // TODO requires D is CopyConstructible and d(p) well-formed
536 __enable_shared_from_this( _M_refcount, __p, __p );
539 // generated copy constructor, assignment, destructor are fine.
541 /** @brief If @a r is empty, constructs an empty %shared_ptr; otherwise
542 * construct a %shared_ptr that shares ownership with @a r.
543 * @param r A %shared_ptr.
544 * @post get()==r.get() && use_count()==r.use_count()
545 * @throw std::bad_alloc, in which case
547 template <typename _Tp1>
548 shared_ptr(const shared_ptr<_Tp1>& __r)
549 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
551 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
554 /** @brief Constructs a %shared_ptr that shares ownership with @a r
555 * and stores a copy of the pointer stored in @a r.
556 * @param r A weak_ptr.
557 * @post use_count()==r.use_count()
558 * @throw bad_weak_ptr when r.expired(),
559 * in which case the constructor has no effect.
561 template <typename _Tp1>
562 explicit shared_ptr(const weak_ptr<_Tp1>& __r)
563 : _M_refcount(__r._M_refcount) // may throw
565 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
566 // it is now safe to copy r__._M_ptr, as _M_refcount(__r._M_refcount)
572 * @post use_count()==1 and r.get()==0
574 template <typename _Tp1>
575 explicit shared_ptr(std::auto_ptr<_Tp1>& __r)
576 : _M_ptr(__r.get()), _M_refcount()
578 // TODO requires r.release() convertible to _Tp*, Tp1 is complete,
579 // delete r.release() well-formed
580 _Tp1 * __tmp = __r.get();
581 _M_refcount = shared_count(__r);
583 __enable_shared_from_this( _M_refcount, __tmp, __tmp );
586 template <typename _Tp1>
587 shared_ptr(const shared_ptr<_Tp1>& __r, __static_cast_tag)
588 : _M_ptr(static_cast<element_type*>(__r._M_ptr))
589 , _M_refcount(__r._M_refcount)
592 template <typename _Tp1>
593 shared_ptr(const shared_ptr<_Tp1>& __r, __const_cast_tag)
594 : _M_ptr(const_cast<element_type*>(__r._M_ptr))
595 , _M_refcount(__r._M_refcount)
598 template <typename _Tp1>
599 shared_ptr(const shared_ptr<_Tp1>& __r, __dynamic_cast_tag)
600 : _M_ptr(dynamic_cast<element_type*>(__r._M_ptr))
601 , _M_refcount(__r._M_refcount)
603 if (_M_ptr == 0) // need to allocate new counter -- the cast failed
605 _M_refcount = shared_count();
609 template <typename _Tp1>
611 operator=(const shared_ptr<_Tp1>& __r) // never throws
614 _M_refcount = __r._M_refcount; // shared_count::op= doesn't throw
618 template <typename _Tp1>
620 operator=(std::auto_ptr<_Tp1>& __r)
622 shared_ptr(__r).swap(*this);
627 reset() // never throws
628 { shared_ptr().swap(*this); }
630 template <typename _Tp1>
632 reset(_Tp1* __p) // _Tp1 must be complete
634 _GLIBCXX_DEBUG_ASSERT(__p == 0 || __p != _M_ptr); // catch self-reset errors
635 shared_ptr(__p).swap(*this);
638 template <typename _Tp1, typename _Deleter>
640 reset(_Tp1 * __p, _Deleter __d)
641 { shared_ptr(__p, __d).swap(*this); }
643 // error to instantiate if _Tp is [cv-qual] void
645 operator*() const // never throws
647 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
652 operator->() const // never throws
654 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
659 get() const // never throws
662 // implicit conversion to "bool"
664 typedef _Tp* shared_ptr::*__unspecified_bool_type;
667 operator __unspecified_bool_type() const // never throws
668 { return _M_ptr == 0 ? 0 : &shared_ptr::_M_ptr; }
671 unique() const // never throws
672 { return _M_refcount.unique(); }
675 use_count() const // never throws
676 { return _M_refcount.use_count(); }
679 swap(shared_ptr<_Tp>& __other) // never throws
681 std::swap(_M_ptr, __other._M_ptr);
682 _M_refcount.swap(__other._M_refcount);
686 template <typename _Tp1>
688 _M_less(const shared_ptr<_Tp1>& __rhs) const
689 { return _M_refcount < __rhs._M_refcount; }
692 _M_get_deleter(const std::type_info& __ti) const
693 { return _M_refcount.get_deleter(__ti); }
695 template <typename _Tp1> friend class shared_ptr;
696 template <typename _Tp1> friend class weak_ptr;
698 // friends injected into enclosing namespace and found by ADL:
700 // get_deleter (experimental)
701 template <typename _Del>
703 get_deleter(const shared_ptr& __p)
704 { return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del))); }
706 template <typename _Tp1>
708 operator==(const shared_ptr& __a, const shared_ptr<_Tp1>& __b)
709 { return __a.get() == __b.get(); }
711 template <typename _Tp1>
713 operator!=(const shared_ptr& __a, const shared_ptr<_Tp1>& __b)
714 { return __a.get() != __b.get(); }
716 template <typename _Tp1>
718 operator<(const shared_ptr& __a, const shared_ptr<_Tp1>& __b)
719 { return __a._M_less(__b); }
721 _Tp* _M_ptr; // contained pointer
722 shared_count _M_refcount; // reference counter
725 // 2.2.3.9 shared_ptr casts
727 /** @warning The seemingly equivalent
728 * <code>shared_ptr<T>(static_cast<T*>(r.get()))</code>
729 * will eventually result in undefined behaviour,
730 * attempting to delete the same object twice.
732 template <typename _Tp, typename _Tp1>
734 static_pointer_cast(const shared_ptr<_Tp1>& __r)
736 return shared_ptr<_Tp>(__r, __static_cast_tag());
739 /** @warning The seemingly equivalent
740 * <code>shared_ptr<T>(const_cast<T*>(r.get()))</code>
741 * will eventually result in undefined behaviour,
742 * attempting to delete the same object twice.
744 template <typename _Tp, typename _Tp1>
746 const_pointer_cast(const shared_ptr<_Tp1>& __r)
748 return shared_ptr<_Tp>(__r, __const_cast_tag());
751 /** @warning The seemingly equivalent
752 * <code>shared_ptr<T>(dynamic_cast<T*>(r.get()))</code>
753 * will eventually result in undefined behaviour,
754 * attempting to delete the same object twice.
756 template <typename _Tp, typename _Tp1>
758 dynamic_pointer_cast(const shared_ptr<_Tp1>& __r)
760 return shared_ptr<_Tp>(__r, __dynamic_cast_tag());
764 template <typename _Ch, typename _Tr, typename _Tp>
765 std::basic_ostream<_Ch,_Tr>&
766 operator<<(std::basic_ostream<_Ch,_Tr>& __os, const shared_ptr<_Tp>& __p)
773 template <typename _Tp>
778 typedef _Tp element_type;
781 : _M_ptr(0), _M_refcount() // never throws
784 // generated copy constructor, assignment, destructor are fine
787 // The "obvious" converting constructor implementation:
790 // weak_ptr(weak_ptr<Y> const & r): _M_ptr(r._M_ptr), _M_refcount(r._M_refcount) // never throws
794 // has a serious problem.
796 // r._M_ptr may already have been invalidated. The _M_ptr(r._M_ptr)
797 // conversion may require access to *r._M_ptr (virtual inheritance).
799 // It is not possible to avoid spurious access violations since
800 // in multithreaded programs r._M_ptr may be invalidated at any point.
803 template <typename _Tp1>
804 weak_ptr(const weak_ptr<_Tp1>& r)
805 : _M_refcount(r._M_refcount) // never throws
807 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
808 _M_ptr = r.lock().get();
811 template <typename _Tp1>
812 weak_ptr(const shared_ptr<_Tp1>& r)
813 : _M_ptr(r._M_ptr), _M_refcount(r._M_refcount) // never throws
815 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
818 template <typename _Tp1>
820 operator=(const weak_ptr<_Tp1>& r) // never throws
822 _M_ptr = r.lock().get();
823 _M_refcount = r._M_refcount;
827 template <typename _Tp1>
829 operator=(const shared_ptr<_Tp1>& r) // never throws
832 _M_refcount = r._M_refcount;
837 lock() const // never throws
841 // optimization: avoid throw overhead
844 return shared_ptr<element_type>();
849 return shared_ptr<element_type>(*this);
851 catch (const bad_weak_ptr&)
853 // Q: how can we get here?
854 // A: another thread may have invalidated r after the use_count test above.
855 return shared_ptr<element_type>();
860 // optimization: avoid try/catch overhead when single threaded
861 return expired() ? shared_ptr<element_type>() : shared_ptr<element_type>(*this);
868 use_count() const // never throws
869 { return _M_refcount.use_count(); }
872 expired() const // never throws
873 { return _M_refcount.use_count() == 0; }
876 reset() // never throws
877 { weak_ptr().swap(*this); }
880 swap(weak_ptr& __s) // never throws
882 std::swap(_M_ptr, __s._M_ptr);
883 _M_refcount.swap(__s._M_refcount);
888 template <typename _Tp1>
890 _M_less(const weak_ptr<_Tp1>& __rhs) const
891 { return _M_refcount < __rhs._M_refcount; }
893 // used by __enable_shared_from_this
895 _M_assign(_Tp* __ptr, const shared_count& __refcount)
898 _M_refcount = __refcount;
901 // friend injected into namespace and found by ADL
903 template <typename _Tp1>
905 operator<(const weak_ptr& __lhs, const weak_ptr<_Tp1>& __rhs)
906 { return __lhs._M_less(__rhs); }
908 template <typename _Tp1> friend class weak_ptr;
909 template <typename _Tp1> friend class shared_ptr;
910 friend class enable_shared_from_this<_Tp>;
912 _Tp* _M_ptr; // contained pointer
913 weak_count _M_refcount; // reference counter
919 template <typename _Tp>
920 class enable_shared_from_this
924 enable_shared_from_this()
927 enable_shared_from_this(const enable_shared_from_this&)
930 enable_shared_from_this&
931 operator=(const enable_shared_from_this&)
934 ~enable_shared_from_this()
942 shared_ptr<_Tp> p(this->_M_weak_this);
946 shared_ptr<const _Tp>
947 shared_from_this() const
949 shared_ptr<const _Tp> p(this->_M_weak_this);
954 template <typename _Tp1>
956 _M_weak_assign(_Tp1* __p, const shared_count& __n) const
957 { _M_weak_this._M_assign(__p, __n); }
959 template <typename _Tp1>
961 __enable_shared_from_this( const shared_count& __pn, const enable_shared_from_this* __pe, const _Tp1* __px)
964 __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
967 mutable weak_ptr<_Tp> _M_weak_this;
973 * @brief std::swap() specialisation for shared_ptr.
974 * @relates shared_ptr.
976 template <typename _Tp>
978 swap(tr1::shared_ptr<_Tp>& __a, tr1::shared_ptr<_Tp>& __b)
984 * @brief std::swap() specialisation for weak_ptr.
987 template <typename _Tp>
989 swap(tr1::weak_ptr<_Tp>& __a, tr1::weak_ptr<_Tp>& __b)