shared_ptr源码分析后续

本文详细解析了shared_ptr和weak_ptr的工作原理,重点介绍了shared_count类如何管理引用计数,以及sp_counted_base类如何实现资源释放。通过源码分析展示了不同删除器的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上次剖析了shared_ptr类的源码,本来肯定也是要说shared_count的,不过由于篇幅,shared_count在这篇博客分析。

shared_ptr类自身有两个成员,一个就是T类型指针,另一个就是shared_count对象了。shared_ptr把所有的计数任务都交给了该成员,最终指针的销毁也是由该对象去执行的(底层实际还有sp_counted_base)。这是一种解耦的思想。

源码分析:

class weak_count;

class shared_count
{
private:

    sp_counted_base * pi_;

    friend class weak_count;

public:

    shared_count(): pi_(0) // nothrow
    {
    }

    template<class Y> explicit shared_count( Y * p ): pi_( 0 )
    {
        pi_ = new sp_counted_impl_p<Y>( p );  //new一个impl派生类

        if( pi_ == 0 )  //如果失败,就摧毁,这是在定义了BOOST_NO_EXCEPTION情况下,new失败返回值为0。宏定义被我略去。
        {
            boost::checked_delete( p );
            boost::throw_exception( std::bad_alloc() );
        }
    }

    //删除器版本
    template<class P, class D> shared_count( P p, D d ): pi_(0)
    {
        pi_ = new sp_counted_impl_pd<P, D>(p, d);

        if(pi_ == 0)
        {
            d(p); // delete p
            boost::throw_exception(std::bad_alloc());
        }

    }

    //分配器版本
    template<class P, class D, class A> shared_count( P p, D d, A a ): pi_( 0 )
    {
        typedef sp_counted_impl_pda<P, D, A> impl_type;
        typedef typename A::template rebind< impl_type >::other A2;

        A2 a2( a );

        pi_ = a2.allocate( 1, static_cast< impl_type* >( 0 ) );

        if( pi_ != 0 )
        {
            new( static_cast< void* >( pi_ ) ) impl_type( p, d, a );
        }
        else
        {
            d( p );    //失败要执行d(),销毁操作。
            boost::throw_exception( std::bad_alloc() );
        }
    }

#ifndef BOOST_NO_AUTO_PTR
    // auto_ptr<Y> is special cased to provide the strong guarantee
    //auto_ptr版本,不过C++11已经弃用auto_ptr了
    template<class Y>
    explicit shared_count( std::auto_ptr<Y> & r ): pi_( new sp_counted_impl_p<Y>( r.get() ) )
    {
        r.release();    //在这里release的,呵呵
    }
#endif 

#if !defined( BOOST_NO_CXX11_SMART_PTR )
    //C++11的unique_ptr版本。
    template<class Y, class D>
    explicit shared_count( std::unique_ptr<Y, D> & r ): pi_( 0 )
    {
        typedef typename sp_convert_reference<D>::type D2;

        D2 d2( r.get_deleter() );
        pi_ = new sp_counted_impl_pd< typename std::unique_ptr<Y, D>::pointer, D2 >( r.get(), d2 );

        r.release();
    }
#endif

    ~shared_count() // nothrow
    {
        
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值