shared_ptr源码剖析

shared_ptr的源码:

主要又下列这几个文件组成:main.cpp sp_config.h sp_counted_base.h sp_counted_impl.h 
shared_count.h shared_ptr.h

sp_config.h

#ifndef _SP_CONFIG_H
#define _SP_CONFIG_H

#define DISPLAY//配置:在构造和析构的时候是否打印对应的字符串

#endif

shared_ptr.h

#ifndef _SHREAD_PTR_H
#define _SHREAD_PTR_H

#include"shared_count.h"

template<class T>
class shared_ptr
{
public:
	shared_ptr():px(0), pn()
	{
#ifdef DISPLAY
		cout<<"Create shared_ptr object."<<endl;
#endif
	}
	shared_ptr(T *p) : px(p), pn(p)
	{
#ifdef DISPLAY
		cout<<"Create shared_ptr object."<<endl;
#endif
	}
	shared_ptr(const shared_ptr<T> &r) : px(r.px), pn(r.pn)
	{
		cout<<"Create shared_ptr object."<<endl;
	}
	shared_ptr<T>& operator=(const shared_ptr<T> &r)
	{
		if(this != &r)
		{
			px = r.px;
			pn = r.pn;
		}
		return *this;
	}
	~shared_ptr()
	{
#ifdef DISPLAY
		cout<<"Free shared_ptr object."<<endl;
#endif
	}
public:
	T& operator*()const
	{
		return *px;
	}
	T* operator->()const
	{
		return px;
	}
	T* get()const
	{return px;}
public:
	long use_count()const
	{
		return pn.use_count();
	}
private:
	T *px;
	shared_count pn;
};

#endif

shared_count.h

#ifndef _SHREAD_COUNT_H
#define _SHREAD_COUNT_H

#include"sp_counted_base.h"
#include"sp_counted_impl.h"

class shared_count
{
public:
	shared_count() : pi_(  0)
	{
#ifdef DISPLAY
		cout<<"Create shared_count object."<<endl;
#endif
	}
	template<class T>
	shared_count(T *p) : pi_(new sp_counted_impl<T>(p))
	{
#ifdef DISPLAY
		cout<<"Create shared_count object."<<endl;
#endif
	}
	shared_count(const shared_count &r) : pi_(r.pi_)
	{
		if(pi_ != 0)
			pi_ ->add_ref_copy();
	}
	shared_count & operator=(const shared_count &r)
	{
		if(this != &r)
		{
			pi_ =r.pi_;
			if(pi_ != 0)
				pi_ -> add_ref_copy();
		}
		return *this;
	}
	~shared_count()
	{
#ifdef DISPLAY
		cout<<"Free shared_count object."<<endl;
#endif
		if(pi_)
			pi_->release();
	}
public:
	long use_count()const
	{
		return pi_!=0 ? pi_->use_count() : 0;
	}
private:
	sp_counted_base * pi_; 
};

#endif

sp_counted_base.h

#ifndef _SP_COUNTED_BASE_H
#define _SP_COUNTED_BASE_H

#include"sp_config.h"


#include<iostream>
using namespace std;

class sp_counted_base
{
public:
	sp_counted_base() : use_count_(1)
	{
#ifdef DISPLAY
		cout<<"Create sp_counted_base object."<<endl;
#endif
	}
	virtual ~sp_counted_base()
	{
#ifdef DISPLAY
		cout<<"Free sp_counted_base object."<<endl;
#endif
	}
public:
	virtual void dispose() = 0;//要实现多态,所以才采用了父类的指针
public:
	long use_count()const
	{
		return use_count_;
	}
	void add_ref_copy()
	{
		++use_count_;
	}
	void release()
	{
		if(--use_count_ == 0)
		{
			dispose();
			delete this;
		}
	}
private:
	long use_count_;
};

#endif

sp_counted_impl.h

#ifndef _SP_COUNTED_IMPL_H
#define _SP_COUNTED_IMPL_H

#include"sp_counted_base.h"



template<class T>
class sp_counted_impl : public sp_counted_base
{
public:
	sp_counted_impl() : px_(0)
	{
#ifdef DISPLAY
		cout<<"Create sp_counted_impl object."<<endl;
#endif
	}
	sp_counted_impl(T *p) : px_(p)
	{
#ifdef DISPLAY
		cout<<"Create sp_counted_impl object."<<endl;
#endif
	}
	~sp_counted_impl() 
	{
#ifdef DISPLAY
		cout<<"Free sp_counted_impl object."<<endl;
#endif
		px_ = 0;
	}
public:
	void dispose()
	{
		delete px_;
	}

private:
	T *px_;
};

#endif

main.cpp

#include<iostream>
#include<vld.h>
#include"shared_ptr.h"
#include"sp_config.h"
//#include<string>
using namespace std;


class Test
{
public:
	void fun()
	{cout<<"Test::fun()."<<endl;}
};



void main()
{
	int *p = new int(10);
	/*
	要构造一个ps,需要先构造一个sp_counted_base,再构造一个sp_counted_impl,
	再构造一个shared_count
	*/
	shared_ptr<int> ps(p);
	
	cout<<"ps = "<<*ps<<endl;
	cout<<"ps use_count = "<<ps.use_count()<<endl;
	shared_ptr<int> ps1;
	ps1 = ps;
	cout<<"ps use_count = "<<ps.use_count()<<endl;
	shared_ptr<int>ps2 = ps;
	cout<<"ps use_count = "<<ps.use_count()<<endl;

}

下面的图可以帮助大家理解shared_ptr的内存模型。

  • 构造时候的内存模型:

shared_ptr构造

  1. 根据主函数中的代码,首先构造一个shared_ptr的对象ps,ps中有指向T类型的指针px和shared_count的对象pn
  2. 需要先构造pn,其构造函数将new sp_counted_impl< T > ( p ) 赋值给其成员pi_。pi_是指向sp_counted_base类型的指针
  3. 调用 sp_counted_impl的构造函数,其成员px_也是指向T类型的指针。
  4. 在构造sp_counted_impl对象的时候,需要先构造其父类sp_counted_base
  5. 在sp_counted_base中只有一个成员use_coun_(这就是最终用来计数的变量)

PS:父类的指针pi_指向了子类对象。

  • 拷贝或者赋值时候的逻辑

shared_ptr拷贝和赋值

  1. 拷贝的时候,让对象ps1的成员px指针指向ps的成员px所指的位置
  2. 拷贝构造ps1的pn对象,让ps1中的pn的pi_指针指向sp_counted_impl,并将use_count_++
  • 析构时候的逻辑:

shared_ptr析构
3. 析构的shared_ptr的 时候,首先会析构shared_count类型的对象pn
4. shared_count中的析构函数会调用 其成员pi_的release()函数将引用计数–。如果引用计数变成0的话,调用dispose()函数由于在此类中dispose函数定义的是虚方法,所以会调用子类的dispose()函数方法。
5. 子类的dispose()方法会先释放掉px_所指向的空间。
6. 最后在sp_count_base类中delete this,


版权声明:本文为qq_35353824原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。