webkit 智能指针分析

    首先说明,智能指针的目的就是解决帮助程序员解决内存泄露问题。智能指针比较典型的应用有两种,一种是针对指针拥有值的,例如auto_ptr,他控制只针对拥有权只有一个。另一种是资源共享,例如shared_ptr。智能指针在实现上也有两种凡是,一种是侵入式,通过继承一个带有引用计数的类来实现。另一种是非侵入式,使用类模版来实现,类模板里保存原始指针,和引用计数。

   webkit里实现采用的是非侵入式智能指针。其代码如下:

  template <class T> class SharedPtr
{
public:
    SharedPtr() : m_ptr(0) {}
    SharedPtr(T *ptr) : m_ptr(ptr) { if (m_ptr) m_ptr->ref(); }
    SharedPtr(const SharedPtr &o) : m_ptr(o.m_ptr) { if (m_ptr) m_ptr->ref(); }
    ~SharedPtr() { if (m_ptr) m_ptr->deref(); }

    template <class U> SharedPtr(const SharedPtr<U> &o) : m_ptr(o.get()) { if (T *ptr = m_ptr) ptr->ref(); }

    bool isNull() const { return m_ptr == 0; }
    bool notNull() const { return m_ptr != 0; }

    void reset() { if (m_ptr) m_ptr->deref(); m_ptr = 0; }
    void reset(T *o) { if (o) o->ref(); if (m_ptr) m_ptr->deref(); m_ptr = o; }
   
    T * get() const { return m_ptr; }
    T &operator*() const { return *m_ptr; }
    T *operator->() const { return m_ptr; }

    bool operator!() const { return m_ptr == 0; }
    operator bool() const { return m_ptr != NULL; }

    inline friend bool operator==(const SharedPtr &a, const SharedPtr &b) { return a.m_ptr == b.m_ptr; }
    inline friend bool operator==(const SharedPtr &a, const T *b) { return a.m_ptr == b; }
    inline friend bool operator==(const T *a, const SharedPtr &b) { return a == b.m_ptr; }

    SharedPtr &operator=(const SharedPtr &);
    SharedPtr &operator=(T *);

private:
    T* m_ptr;
    operator int() const;

// 故意不实现的目的是为了使用者看到了bool的重载,而想到使用重载的int。
};

 

 

类模板有三种构造函数,默认构造函数,拷贝构造函数,自定义构造函数,模板拷贝构造函数。模板拷贝构造函数和拷贝构造函数的区别可以用一段代码来解析

SharedPtr<int> p = new int;

SharedPtr<int> sp(p);

此段代码使用的是拷贝构造函数,因为模板实例化后的类型都是int类型。

SharedPtr<int> p = new int;

SharedPtr<char> sp(p);

此段代码使用的是模板拷贝构造函数,因为构造函数使用的不是同一种类型。

,值的注意的是指针的初始值,在webkit里面是0,而不是常见的NULL。

模板里有reset(T *o)函数的作用是重设置内存为o,而reset()的作用是清除掉内存。剩下的函数都是重载一些操作符。

 

 

还有一些地方不明白的是,析构的时候调用的是deref()函数而不是delete操作符。个人以为webki里为了能够坚持内存泄露,额外提供个一个类,

template<class type> class Shared
OOM_MODIFIED
{
public:
    Shared() { _ref=0; /*counter++;*/ }
    ~Shared() { /*counter--;*/ }

    void ref() { _ref++;  }
    void deref() {
    if(_ref) _ref--;
    if(!_ref)
        delete static_cast<type *>(this);
    }
    bool hasOneRef() { //kdDebug(300) << "ref=" << _ref << endl;
        return _ref==1; }

    int refCount() const { return _ref; }
//    static int counter;
protected:
    unsigned int _ref;

private:
    Shared(const Shared &);
    Shared &operator=(const Shared &);
};

 

这个类,也是个模板类,首先看最后两个函数,明显可以看到出来是防止对象间的拷贝,因为如果允许拷贝的话,shared_ptr就失去了存在的价值了(sharted_ptr控制不了内存的操作)。仔细看deref()函数可以观察得出,它只是删除了指针。对于文件句柄,等非内存型指针无法管理。当然用户可以发挥自己的编程里,加入一个回调函数,deref()的时候可以自动释放文件句柄。

还有一个没有提到的类模板是

template<class type> class TreeShared
OOM_MODIFIED
{
public:
    TreeShared() { _ref = 0; m_parent = 0; /*counter++;*/ }
    TreeShared( type *parent ) { _ref=0; m_parent = parent; /*counter++;*/ }
    ~TreeShared() { /*counter--;*/ }

    void ref() { _ref++;  }
    void deref() {
    if(_ref) _ref--;
    if(!_ref && !m_parent) {
        delete static_cast<type *>(this);
    }
    }
    bool hasOneRef() { //kdDebug(300) << "ref=" << _ref << endl;
        return _ref==1; }

    int refCount() const { return _ref; }
//    static int counter;

    void setParent(type *parent) { m_parent = parent; }
    type *parent() const { return m_parent; }
private:
    unsigned int _ref;
protected:
    type *m_parent;

private:
    TreeShared(const TreeShared &);
    TreeShared &operator=(const TreeShared &);
};

 

它只是share模板的一个特殊应用,用在树节点上。

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值