Chinaunix首页 | 论坛 | 博客
  • 博客访问: 98224
  • 博文数量: 21
  • 博客积分: 451
  • 博客等级: 一等列兵
  • 技术积分: 215
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-03 20:46
个人简介

记录技术旅程

文章分类

全部博文(21)

文章存档

2014年(3)

2011年(18)

我的朋友

分类: LINUX

2011-06-29 16:02:27

历史

WebKit中的许多对象是引用计数的(reference counted),采用的模式就是类具有ref和deref成员函数增加和减少引用计数。每个ref调用必须有一个deref与之匹配。当在引用计数值为1的对象上调用deref方法时,对象删除。WebKit中的许多类通过继承RefCounted类模板应用该模式。

时间回溯到2005年,我们发现存在许多由于不正确调用ref和deref而引起的内存泄露,特别是HTML编辑的代码。

我们希望使用智能指针来减少这一问题。但是早期的试验表明智能指针会进行额外的引用计数处理而影响性能。例如,一个函数有一个智能指针的参数并返回该指针指针作为返回值,仅仅传入该参数并返回该值会进行2到4次的增加和减少引用计数值,因为对象从一个智能指针转移到另一个上。因此我们寻找一种方式来让我们使用智能指针同时避免引用计数跳变(churn).

我们从C++标准类模板auto_ptr获得灵感,这些对象实现了一种模型,赋值即是归属关系转移(transfer of ownership),当您从一个auto_ptr赋值到另一个,贡献值变成0.(When you assign from one auto_ptr to another, the donor becomes 0.)

Maciej Stachowiak设计了一对类模板,RefPtr和PassRefPtr,实现这一模式来解决WebKit中恼人的引用计数问题。

原始指针(Raw pointers)

当我们讨论诸如RefPtr类模板之类的智能指针时,通常使用原始指针(raw pointer)指代C++语言中内置的指针类型。下面是经典的使用原始指针(raw pointer)的设置函数:

  1. // example, not preferred style  
  2. class Document {  
  3.     ...  
  4.     Title* m_title;  
  5. }  
  6. Document::Document()  
  7.     : m_title(0)  
  8. {  
  9. }  
  10. Document::~Document()  
  11. {  
  12.     if (m_title)  
  13.         m_title->deref();  
  14. }  
  15. void Document::setTitle(Title* title)  
  16. {  
  17.     if (title)  
  18.         title->ref();  
  19.     if (m_title)  
  20.         m_title->deref();  
  21.     m_title = title;  
  22. }  
 
RefPtr

RefPtr是一个简单的智能指针类,它对来值(incoming value)调用ref,对去值(outgoing value)调用deref。RefPtr可用于任何有ref和deref成员函数的对象。下面是使用RefPtr写的设置函数实例:

  1. // example, not preferred style  
  2.    
  3. class Document {  
  4.     ...  
  5.     RefPtr m_title;  </span></li><li class="" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; list-style-type: decimal-leading-zero; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-width: initial; border-color: initial; list-style-image: initial; list-style-position: outside !important; border-left-width: 3px; border-left-color: rgb(108, 226, 108); background-color: rgb(248, 248, 248); color: rgb(92, 92, 92); line-height: 14px; "><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">}  </span></li><li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; list-style-type: decimal-leading-zero; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-width: initial; border-color: initial; list-style-image: initial; list-style-position: outside !important; border-left-width: 3px; border-left-color: rgb(108, 226, 108); background-color: rgb(255, 255, 255); color: inherit; line-height: 14px; "><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; "><span class="keyword" style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold; ">void</span><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; "> Document::setTitle(Title* title)  </span></span></li><li class="" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; list-style-type: decimal-leading-zero; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-width: initial; border-color: initial; list-style-image: initial; list-style-position: outside !important; border-left-width: 3px; border-left-color: rgb(108, 226, 108); background-color: rgb(248, 248, 248); color: rgb(92, 92, 92); line-height: 14px; "><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">{  </span></li><li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; list-style-type: decimal-leading-zero; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-width: initial; border-color: initial; list-style-image: initial; list-style-position: outside !important; border-left-width: 3px; border-left-color: rgb(108, 226, 108); background-color: rgb(255, 255, 255); color: inherit; line-height: 14px; "><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">    m_title = title;  </span></li><li class="" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; list-style-type: decimal-leading-zero; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-width: initial; border-color: initial; list-style-image: initial; list-style-position: outside !important; border-left-width: 3px; border-left-color: rgb(108, 226, 108); background-color: rgb(248, 248, 248); color: rgb(92, 92, 92); line-height: 14px; "><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">}  </span></li></ol></div><span class="Apple-style-span" style="font-family: verdana, sans-serif; line-height: 21px; "> </span>单独使用RefPtr可能会导致引用计数跳变(churn)。<p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "></p><div class="dp-highlighter" style="line-height: 21px; font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; background-color: rgb(231, 229, 220); width: 913px; overflow-x: auto; overflow-y: auto; margin-top: 18px !important; margin-right: 0px !important; margin-bottom: 18px !important; margin-left: 0px !important; padding-top: 1px; "><div class="bar" style="padding-left: 45px; "><div class="tools" style="padding-top: 3px; padding-right: 8px; padding-left: 10px; font: normal normal normal 9px/normal Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; background-color: rgb(248, 248, 248); padding-bottom: 10px; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(108, 226, 108); "><a href="http://blog.csdn.net/keensword007/archive/2010/09/04/5863367.aspx#" style="text-decoration: none; color: rgb(160, 160, 160); background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: inherit; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 10px; margin-bottom: 0px; margin-left: 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial; " target="_blank" target="_blank">view plain</a><a href="http://blog.csdn.net/keensword007/archive/2010/09/04/5863367.aspx#" style="text-decoration: none; color: rgb(160, 160, 160); background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: inherit; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 10px; margin-bottom: 0px; margin-left: 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial; " target="_blank" target="_blank">copy to clipboard</a><a href="http://blog.csdn.net/keensword007/archive/2010/09/04/5863367.aspx#" style="text-decoration: none; color: rgb(160, 160, 160); background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: inherit; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 10px; margin-bottom: 0px; margin-left: 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial; " target="_blank" target="_blank">print</a><a href="http://blog.csdn.net/keensword007/archive/2010/09/04/5863367.aspx#" style="text-decoration: none; color: rgb(160, 160, 160); background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: inherit; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 10px; margin-bottom: 0px; margin-left: 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial; " target="_blank" target="_blank">?</a></div></div><ol start="1" class="dp-cpp" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 1px !important; margin-left: 45px !important; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: decimal; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; list-style-position: initial; list-style-image: initial; background-color: rgb(255, 255, 255); color: rgb(92, 92, 92); "><li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; list-style-type: decimal-leading-zero; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-width: initial; border-color: initial; list-style-image: initial; list-style-position: outside !important; border-left-width: 3px; border-left-color: rgb(108, 226, 108); background-color: rgb(255, 255, 255); color: inherit; line-height: 14px; "><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; "><span class="comment" style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: rgb(0, 130, 0); background-color: inherit; ">// example, not preferred style; should use RefCounted and adoptRef (see below)</span><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">  </span></span></li><li class="" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; list-style-type: decimal-leading-zero; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-width: initial; border-color: initial; list-style-image: initial; list-style-position: outside !important; border-left-width: 3px; border-left-color: rgb(108, 226, 108); background-color: rgb(248, 248, 248); color: rgb(92, 92, 92); line-height: 14px; "><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">   </span></li><li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; list-style-type: decimal-leading-zero; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-width: initial; border-color: initial; list-style-image: initial; list-style-position: outside !important; border-left-width: 3px; border-left-color: rgb(108, 226, 108); background-color: rgb(255, 255, 255); color: inherit; line-height: 14px; "><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">RefPtr<Node> createSpecialNode()  </span></li><li class="" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; list-style-type: decimal-leading-zero; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-width: initial; border-color: initial; list-style-image: initial; list-style-position: outside !important; border-left-width: 3px; border-left-color: rgb(108, 226, 108); background-color: rgb(248, 248, 248); color: rgb(92, 92, 92); line-height: 14px; "><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">{  </span></li><li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; list-style-type: decimal-leading-zero; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-width: initial; border-color: initial; list-style-image: initial; list-style-position: outside !important; border-left-width: 3px; border-left-color: rgb(108, 226, 108); background-color: rgb(255, 255, 255); color: inherit; line-height: 14px; "><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">    RefPtr<Node> a = <span class="keyword" style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold; ">new</span><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; "> Node;  </span></span></li><li class="" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; list-style-type: decimal-leading-zero; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-width: initial; border-color: initial; list-style-image: initial; list-style-position: outside !important; border-left-width: 3px; border-left-color: rgb(108, 226, 108); background-color: rgb(248, 248, 248); color: rgb(92, 92, 92); line-height: 14px; "><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">    a->setSpecial(<span class="keyword" style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold; ">true</span><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">);  </span></span></li><li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; list-style-type: decimal-leading-zero; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-width: initial; border-color: initial; list-style-image: initial; list-style-position: outside !important; border-left-width: 3px; border-left-color: rgb(108, 226, 108); background-color: rgb(255, 255, 255); color: inherit; line-height: 14px; "><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">    <span class="keyword" style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold; ">return</span><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; "> a;  </span></span></li><li class="" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; list-style-type: decimal-leading-zero; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-width: initial; border-color: initial; list-style-image: initial; list-style-position: outside !important; border-left-width: 3px; border-left-color: rgb(108, 226, 108); background-color: rgb(248, 248, 248); color: rgb(92, 92, 92); line-height: 14px; "><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">}  </span></li><li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; list-style-type: decimal-leading-zero; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-width: initial; border-color: initial; list-style-image: initial; list-style-position: outside !important; border-left-width: 3px; border-left-color: rgb(108, 226, 108); background-color: rgb(255, 255, 255); color: inherit; line-height: 14px; "><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">RefPtr<Node> b = createSpecialNode();  </span></li></ol></div><span class="Apple-style-span" style="font-family: verdana, sans-serif; line-height: 21px; "> </span><p style="font-family: verdana, sans-serif; line-height: 21px; "></p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">出于讨论考虑,我们假设节点对象的引用计数起始值为0(稍后更多),当它赋值给a后,引用计数值增加到1。创建返回值后引用计数值又增加到2,接下来a销毁,引用计数值又减少到1。然后创建b后引用计数值增加到2,再下来createSpecialNode返回值销毁后,引用计数值减到1。</p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">(如果编译器实现了返回值优化(return value optimization),就会少一次引用计数增加和减少。)</p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">引用计数跳变(churn)在函数参数和返回值都涉及的情况下更严重,解决的方法就是PassRefPtr。</p><span class="Apple-style-span" style="font-family: verdana, sans-serif; line-height: 21px; ">PassRefPtr</span><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">PassRefPtr和RefPtr相似,但存在一处不同,当您拷贝一个PassRefPtr或者赋PassRefPtr值给RefPtr或另一个PassRefPtr时,原来的指针值设置为0,操作不改变引用计数。让我们来看一个新的版本的例子:</p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "></p><div class="dp-highlighter nogutter" style="line-height: 21px; font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; background-color: rgb(231, 229, 220); width: 913px; overflow-x: auto; overflow-y: auto; margin-top: 18px !important; margin-right: 0px !important; margin-bottom: 18px !important; margin-left: 0px !important; padding-top: 1px; "><div class="bar" style="padding-left: 0px; "></div><ol start="1" class="dp-cpp" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 1px !important; margin-left: 0px !important; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: none !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; list-style-position: initial !important; list-style-image: initial !important; background-color: rgb(255, 255, 255); color: rgb(92, 92, 92); "><li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; list-style-type: none !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-width: initial; border-color: initial; list-style-image: initial !important; list-style-position: initial !important; border-left-width: 0px; background-color: rgb(255, 255, 255); color: inherit; line-height: 14px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-style: initial; border-color: initial; "><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; "><span class="comment" style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: rgb(0, 130, 0); background-color: inherit; ">// example, not preferred style; should use RefCounted and adoptRef (see below)</span><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">  </span></span></li><li class="" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; list-style-type: none !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-width: initial; border-color: initial; list-style-image: initial !important; list-style-position: initial !important; border-left-width: 0px; background-color: rgb(248, 248, 248); color: rgb(92, 92, 92); line-height: 14px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-style: initial; border-color: initial; "><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">PassRefPtr<Node> createSpecialNode()  </span></li><li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; list-style-type: none !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-width: initial; border-color: initial; list-style-image: initial !important; list-style-position: initial !important; border-left-width: 0px; background-color: rgb(255, 255, 255); color: inherit; line-height: 14px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-style: initial; border-color: initial; "><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">{  </span></li><li class="" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; list-style-type: none !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-width: initial; border-color: initial; list-style-image: initial !important; list-style-position: initial !important; border-left-width: 0px; background-color: rgb(248, 248, 248); color: rgb(92, 92, 92); line-height: 14px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-style: initial; border-color: initial; "><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">    PassRefPtr<Node> a = <span class="keyword" style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold; ">new</span><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; "> Node;  </span></span></li><li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; list-style-type: none !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-width: initial; border-color: initial; list-style-image: initial !important; list-style-position: initial !important; border-left-width: 0px; background-color: rgb(255, 255, 255); color: inherit; line-height: 14px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-style: initial; border-color: initial; "><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">    a->setSpecial(<span class="keyword" style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold; ">true</span><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">);  </span></span></li><li class="" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; list-style-type: none !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-width: initial; border-color: initial; list-style-image: initial !important; list-style-position: initial !important; border-left-width: 0px; background-color: rgb(248, 248, 248); color: rgb(92, 92, 92); line-height: 14px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-style: initial; border-color: initial; "><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">    <span class="keyword" style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold; ">return</span><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; "> a;  </span></span></li><li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; list-style-type: none !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-width: initial; border-color: initial; list-style-image: initial !important; list-style-position: initial !important; border-left-width: 0px; background-color: rgb(255, 255, 255); color: inherit; line-height: 14px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-style: initial; border-color: initial; "><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">}  </span></li><li class="" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; list-style-type: none !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-width: initial; border-color: initial; list-style-image: initial !important; list-style-position: initial !important; border-left-width: 0px; background-color: rgb(248, 248, 248); color: rgb(92, 92, 92); line-height: 14px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-style: initial; border-color: initial; "><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">RefPtr<Node> b = createSpecialNode();  </span></li></ol></div><span class="Apple-style-span" style="font-family: verdana, sans-serif; line-height: 21px; "> 节点对象的初始引用计数值为0,当它赋值给a后,引用计数值增加到1。当返回值创建后,指针a被赋值为0。当b值创建后,返回值被赋值为0。然而,引用计数保持1不变。</span><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">     由于PassRefPtr类型指针赋值给另一个指针后本指针变成为0,所以使用PassRefPtr编程很容易容易导致错误。如下下列代码所示:</p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "></p><div id="codeText" class="codeText"><ol start="1" class="dp-css"><li><span><span style="color:#0000CC;">/</span><span style="color:#0000CC;">/</span> warning<span style="color:#0000CC;">,</span> will dereference a <span style="color:#0000FF;">null</span> pointer <span style="color:#0000FF;">and</span> will <span style="color:#0000FF;">not</span> work<br></span></li><li>  <br></li><li> static RefPtr<span style="color:#0000CC;"><</span>Ring<span style="color:#0000CC;">></span> g_oneRingToRuleThemAll<span style="color:#0000CC;">;</span><br></li><li> <br></li><li> void finish<span style="color:#0000CC;">(</span>PassRefPtr<span style="color:#0000CC;"><</span>Ring<span style="color:#0000CC;">></span> ring<span style="color:#0000CC;">)</span><br></li><li> <span style="color:#0000CC;">{</span><br></li><li>     g_oneRingToRuleThemAll <span style="color:#0000CC;">=</span> ring<span style="color:#0000CC;">;</span><br></li><li>     <span style="color:#0000CC;">.</span><span style="color:#0000CC;">.</span><span style="color:#0000CC;">.</span><br></li><li>     ring<span style="color:#0000CC;">-</span><span style="color:#0000CC;">></span>wear<span style="color:#0000CC;">(</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">;</span><br></li><li> <span style="color:#0000CC;">}</span></li></ol></div><p></p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">当wear方法被调用时,ring指针已经变为0了。为了避免这种情况发生,建议只是用PassRefPtr类型作为函数参数类型和返回值类型。并且,将参数类型为PassRefPtr的参数赋值给本地的RefPtr类型指针进行操作。如下:<br></p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "></p><div id="codeText" class="codeText"><ol start="1" class="dp-css"><li><span>static RefPtr<span style="color:#0000CC;"><</span>Ring<span style="color:#0000CC;">></span> g_oneRingToRuleThemAll<span style="color:#0000CC;">;</span><br></span></li><li> <br></li><li> void finish<span style="color:#0000CC;">(</span>PassRefPtr<span style="color:#0000CC;"><</span>Ring<span style="color:#0000CC;">></span> prpRing<span style="color:#0000CC;">)</span><br></li><li> <span style="color:#0000CC;">{</span><br></li><li>     RefPtr<span style="color:#0000CC;"><</span>Ring<span style="color:#0000CC;">></span> ring <span style="color:#0000CC;">=</span> prpRing<span style="color:#0000CC;">;</span><br></li><li>     g_oneRingToRuleThemAll <span style="color:#0000CC;">=</span> ring<span style="color:#0000CC;">;</span><br></li><li>     <span style="color:#0000CC;">.</span><span style="color:#0000CC;">.</span><span style="color:#0000CC;">.</span><br></li><li>     ring<span style="color:#0000CC;">-</span><span style="color:#0000CC;">></span>wear<span style="color:#0000CC;">(</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">;</span><br></li><li> <span style="color:#0000CC;">}</span></li></ol></div><p></p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "><b>Mixing RefPtr and PassRefPtr</b></p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">    除了函数传递参数和返回值时使用PassRefPtr类型外,建议使用RefPtr作为计数指针类型。但是,经常会需要RefPtr类型指针象PassRefPtr型指针一样传递指针的所有权。RefPtr类型有一个release成员函数,它通过设置原始的RefPtr指针为0,同时构建一个PassRefPtr类型指针,从而达到不改变指针引用计数的目的。示例代码如下:</p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "></p><div id="codeText" class="codeText"><ol start="1" class="dp-css"><li><span><span style="color:#0000CC;">/</span><span style="color:#0000CC;">/</span> example<span style="color:#0000CC;">,</span> <span style="color:#0000FF;">not</span> preferred style<span style="color:#0000CC;">;</span> should use RefCounted <span style="color:#0000FF;">and</span> adoptRef <span style="color:#0000CC;">(</span>see below<span style="color:#0000CC;">)</span><br></span></li><li>  <br></li><li> PassRefPtr<span style="color:#0000CC;"><</span>Node<span style="color:#0000CC;">></span> createSpecialNode<span style="color:#0000CC;">(</span><span style="color:#0000CC;">)</span><br></li><li> <span style="color:#0000CC;">{</span><br></li><li>     RefPtr<span style="color:#0000CC;"><</span>Node<span style="color:#0000CC;">></span> a <span style="color:#0000CC;">=</span> new Node<span style="color:#0000CC;">;</span><br></li><li>     a<span style="color:#0000CC;">-</span><span style="color:#0000CC;">></span>setCreated<span style="color:#0000CC;">(</span><span style="color:#0000FF;">true</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">;</span><br></li><li>     return a<span style="color:#0000CC;">.</span>release<span style="color:#0000CC;">(</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">;</span><br></li><li> <span style="color:#0000CC;">}</span><br></li><li> <br></li><li> RefPtr<span style="color:#0000CC;"><</span>Node<span style="color:#0000CC;">></span> b <span style="color:#0000CC;">=</span> createSpecialNode<span style="color:#0000CC;">(</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">;</span></li></ol></div><p></p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">上述代码与前面将a定义成PassRefPtr类型的示例代码具有相同的效果。这种用法,不仅保持了PassRefPtr类型指针的高效的特性,同时又避免了PassRefPtr容易出错的问题。</p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "><b>Mixing with raw pointers</b></p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">      当要获得RefPtr类型中的原始指针时,需要通过get函数来获得原始指针。</p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">下面示例打印原始指针的值:</p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "></p><div id="codeText" class="codeText"><ol start="1" class="dp-css"><li><span>printNode<span style="color:#0000CC;">(</span>stderr<span style="color:#0000CC;">,</span> a<span style="color:#0000CC;">.</span><span style="color:#0000FF;">get</span><span style="color:#0000CC;">(</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">;</span></span></li></ol></div>然而,RefPtr类型和PassRefPtr类型变量无需显性的调用get的函数就可以直接完成大多数的操作。<p></p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">示例如下:</p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "></p><div id="codeText" class="codeText"><ol start="1" class="dp-css"><li><span>RefPtr<span style="color:#0000CC;"><</span>Node<span style="color:#0000CC;">></span> a <span style="color:#0000CC;">=</span> createSpecialNode<span style="color:#0000CC;">(</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">;</span><br></span></li><li> Node<span style="color:#0000CC;">*</span> b <span style="color:#0000CC;">=</span> getOrdinaryNode<span style="color:#0000CC;">(</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">;</span><br></li><li> <br></li><li> <span style="color:#0000CC;">/</span><span style="color:#0000CC;">/</span> the <span style="color:#0000CC;">*</span> operator<br></li><li> <span style="color:#0000CC;">*</span>a <span style="color:#0000CC;">=</span> value<span style="color:#0000CC;">;</span><br></li><li> <br></li><li> <span style="color:#0000CC;">/</span><span style="color:#0000CC;">/</span> the <span style="color:#0000CC;">-</span><span style="color:#0000CC;">></span> operator<br></li><li> a<span style="color:#0000CC;">-</span><span style="color:#0000CC;">></span>clear<span style="color:#0000CC;">(</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">;</span><br></li><li> <br></li><li> <span style="color:#0000CC;">/</span><span style="color:#0000CC;">/</span> <span style="color:#0000FF;">null</span> check <span style="color:#0000FF;">in</span> an <span style="color:#0000FF;">if</span> statement<br></li><li> <span style="color:#0000FF;">if</span> <span style="color:#0000CC;">(</span>a<span style="color:#0000CC;">)</span><br></li><li>     <span style="color:#FF0000;">log</span><span style="color:#0000CC;">(</span><span style="color:#FF00FF;">"not empty"</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">;</span><br></li><li> <br></li><li> <span style="color:#0000CC;">/</span><span style="color:#0000CC;">/</span> the <span style="color:#0000CC;">!</span> operator<br></li><li> <span style="color:#0000FF;">if</span> <span style="color:#0000CC;">(</span><span style="color:#0000CC;">!</span>a<span style="color:#0000CC;">)</span><br></li><li>     <span style="color:#FF0000;">log</span><span style="color:#0000CC;">(</span><span style="color:#FF00FF;">"empty"</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">;</span><br></li><li> <br></li><li> <span style="color:#0000CC;">/</span><span style="color:#0000CC;">/</span> the <span style="color:#0000CC;">=</span><span style="color:#0000CC;">=</span> <span style="color:#0000FF;">and</span> <span style="color:#0000CC;">!</span><span style="color:#0000CC;">=</span> operators<span style="color:#0000CC;">,</span> mixing with raw pointers<br></li><li> <span style="color:#0000FF;">if</span> <span style="color:#0000CC;">(</span>a <span style="color:#0000CC;">=</span><span style="color:#0000CC;">=</span> b<span style="color:#0000CC;">)</span><br></li><li>     <span style="color:#FF0000;">log</span><span style="color:#0000CC;">(</span><span style="color:#FF00FF;">"equal"</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">;</span><br></li><li> <span style="color:#0000FF;">if</span> <span style="color:#0000CC;">(</span>a <span style="color:#0000CC;">!</span><span style="color:#0000CC;">=</span> b<span style="color:#0000CC;">)</span><br></li><li>     <span style="color:#FF0000;">log</span><span style="color:#0000CC;">(</span><span style="color:#FF00FF;">"not equal"</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">;</span><br></li><li> <br></li><li> <span style="color:#0000CC;">/</span><span style="color:#0000CC;">/</span> some type casts<br></li><li> RefPtr<span style="color:#0000CC;"><</span>DerivedNode<span style="color:#0000CC;">></span> d <span style="color:#0000CC;">=</span> static_pointer_cast<span style="color:#0000CC;"><</span>DerivedNode<span style="color:#0000CC;">></span><span style="color:#0000CC;">(</span>a<span style="color:#0000CC;">)</span><span style="color:#0000CC;">;</span></li></ol></div><p></p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">通常,RefPtr和PassRefPtr强制实施一个简单的规则,就是成对调用ref和deref,保证编程者不会漏掉对deref的调用。说的ref和deref就不能不说一下RefCounted类,这个类实现了指针的引用计数功能,提供了ref和deref方法。而RefPtr和PassRefPtr只是完成了对ref和deref的自动调用功能,因此这里所说的原始指针(raw pointer)通常是指继承自RefCounted类的指针,换句话说,是具有ref和deref功能的指针。这样的原始指针可以通过<font class="Apple-style-span" size="3"><span class="Apple-style-span" style="color: rgb(51, 51, 51); font-family: 'Lucida Grande', Verdana, Arial; line-height: 18px; background-color: rgb(255, 255, 255); ">adoptRef函数转换成</span><span class="Apple-style-span" style="color: rgb(51, 51, 51); font-family: 'Lucida Grande', Verdana, Arial; line-height: 18px; background-color: rgb(255, 255, 255); ">RefPtr类型。</span></font></p><div id="codeText" class="codeText"><ol start="1" class="dp-css"><li><span><span style="color:#0000CC;">/</span><span style="color:#0000CC;">/</span> warning<span style="color:#0000CC;">,</span> requires a pointer that already has a ref<br></span></li><li> RefPtr<span style="color:#0000CC;"><</span>Node<span style="color:#0000CC;">></span> node <span style="color:#0000CC;">=</span> adoptRef<span style="color:#0000CC;">(</span>rawNodePointer<span style="color:#0000CC;">)</span><span style="color:#0000CC;">;</span></li></ol></div><p></p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">从RefPtr类型转换成一个原始指针并且不改变引用计数,可以使用PassRefPtr提供的leakRef函数。</p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "></p><div id="codeText" class="codeText"><ol start="1" class="dp-css"><li><span><span style="color:#0000CC;">/</span><span style="color:#0000CC;">/</span> warning<span style="color:#0000CC;">,</span> results <span style="color:#0000FF;">in</span> a pointer that must <span style="color:#0000FF;">get</span> an <span style="color:#0000FF;">explicit</span> deref<br></span></li><li> RefPtr<span style="color:#0000CC;"><</span>Node<span style="color:#0000CC;">></span> node <span style="color:#0000CC;">=</span> createSpecialNode<span style="color:#0000CC;">(</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">;</span><br></li><li> Node<span style="color:#0000CC;">*</span> rawNodePointer <span style="color:#0000CC;">=</span> node<span style="color:#0000CC;">.</span>release<span style="color:#0000CC;">(</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">.</span>leakRef<span style="color:#0000CC;">(</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">;</span></li></ol></div><p></p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">由于leakRef很少被使用,因此只有PassRefPtr类具有这个方法。RefPtr类型的变量使用此方法使,需要先调用release方法,然后才能调用leakRef方法。</p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "><b>RefPtr and new object</b></p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">在以上讨论的例子中,对象的假设引用计数是从0开始的。这样做的目的为了讲解简单明了,但是RefCounted类的引用计数并不是从0开始的。在构建ReCounted类的构造函数中,引用计数被赋值为1。当deref被调用时,如果引用计数等于1,则删除这个对象。由于ref和deref是成对出现的,为了不遗失调用deref,推荐的用法是在创建对象时应该立即调用adoptRef。在WebCore中,使用create方法代替new方法去创建对象。示例如下:</p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "></p><div id="codeText" class="codeText"><ol start="1" class="dp-css"><li><span><span style="color:#0000CC;">/</span><span style="color:#0000CC;">/</span> preferred style<br></span></li><li>  <br></li><li> PassRefPtr<span style="color:#0000CC;"><</span>Node<span style="color:#0000CC;">></span> Node<span style="color:#0000CC;">:</span><span style="color:#0000CC;">:</span>create<span style="color:#0000CC;">(</span><span style="color:#0000CC;">)</span><br></li><li> <span style="color:#0000CC;">{</span><br></li><li>     return adoptRef<span style="color:#0000CC;">(</span>new Node<span style="color:#0000CC;">)</span><span style="color:#0000CC;">;</span><br></li><li> <span style="color:#0000CC;">}</span><br></li><li> <br></li><li> RefPtr<span style="color:#0000CC;"><</span>Node<span style="color:#0000CC;">></span> e <span style="color:#0000CC;">=</span> Node<span style="color:#0000CC;">:</span><span style="color:#0000CC;">:</span>create<span style="color:#0000CC;">(</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">;</span></li></ol></div><p></p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">因为adoptRef和PassRefPtr被应用,上述用法是非常高效的。以引用计数为1创建对象,而整个过程没有再操作引用计数。</p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "></p><div id="codeText" class="codeText"><ol start="1" class="dp-css"><li><span><span style="color:#0000CC;">/</span><span style="color:#0000CC;">/</span> preferred style<br></span></li><li>  <br></li><li> PassRefPtr<span style="color:#0000CC;"><</span>Node<span style="color:#0000CC;">></span> createSpecialNode<span style="color:#0000CC;">(</span><span style="color:#0000CC;">)</span><br></li><li> <span style="color:#0000CC;">{</span><br></li><li>     RefPtr<span style="color:#0000CC;"><</span>Node<span style="color:#0000CC;">></span> a <span style="color:#0000CC;">=</span> Node<span style="color:#0000CC;">:</span><span style="color:#0000CC;">:</span>create<span style="color:#0000CC;">(</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">;</span><br></li><li>     a<span style="color:#0000CC;">-</span><span style="color:#0000CC;">></span>setCreated<span style="color:#0000CC;">(</span><span style="color:#0000FF;">true</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">;</span><br></li><li>     return a<span style="color:#0000CC;">.</span>release<span style="color:#0000CC;">(</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">;</span><br></li><li> <span style="color:#0000CC;">}</span><br></li><li> <br></li><li> RefPtr<span style="color:#0000CC;"><</span>Node<span style="color:#0000CC;">></span> b <span style="color:#0000CC;">=</span> createSpecialNode<span style="color:#0000CC;">(</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">;</span></li></ol></div><p></p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">对象node通过create函数创建,赋值给a并release,最后赋值给b,整个过程没有触及引用计数。</p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">RefCounted类会进行运行时刻检查,如果创建一个对象,调用ref和deref方法而没有最先调用adoptRef函数,将会声明一个错误。</p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "><span class="Apple-style-span" style="color: rgb(51, 51, 51); font-family: 'Lucida Grande', Verdana, Arial; font-size: 12px; line-height: 18px; background-color: rgb(255, 255, 255); "><b>Guidelines</b></span></p><div>在WebKit编程中使用RefPtr和PassRefPtr需要遵循如下规则:</div><div><br></div><div><b>Local variables</b></div><p></p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">如果指针的所有权和生命周期可以被保证,一个本地变量可以是一个原始指针。</p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">如果代码需要获得指针的所有权或者确保指针的生命周期,本地变量应该是RefPtr类型。</p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">本地变量永远都不能是PassRefPtr类型。</p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "><b>Data members</b></p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">如果所有权和生命周期可以被保证,数据成员可以是一个原始指针。</p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">如果类需要获得数据成员指针所有权并保证其生命周期,则数据成员需要定义为RefPtr类型。</p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">数据成员永远都不能是PassRefPtr类型。</p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "><b>Function arguments</b></p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">如果一个函数不需要获得参数对象的所有权,参数应该使用原始指针。</p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">如果函数需要获得一个对象的所有权,参数应个是一个PassRefPtr类型。这包括大多数的setter函数。除非参数非常简单,在函数的开始PassRefPtr参数应用赋值给一个本地的RefPtr变量。</p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "><b>Function Results</b></p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "><font class="Apple-style-span" size="3">如果一个函数的返回值是一个对象,但是所有权没有被传递,这个返回结果应用是一个原始指针。</font></p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "><font class="Apple-style-span" size="3">如果函数的返回值是一个被创建的对象,或者对象的所有权被传递,返回值应该是PassRefPtr类型。由于本地类型通常是RefPtr,因此在返回时需要调用release方法将RefPtr类型转换成PassRefPtr类型。</font></p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "><b>New Object</b></p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">新创建的指针对象应该尽快放在RefPtr类型中,这样可以使智能指针自动完成所有的引用计数。</p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">对于RefCounted类型指针,上述的操作应该由adoptRef函数完成。</p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; ">最好的用法是使用私有的构造函数和公有的create函数,通过返回一个PassRefPtr指针从而实现对象的创建。</p><p style="font-family: verdana, sans-serif; line-height: 21px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "><br></p> </div> <!-- <div class="Blog_con3_1">管理员在2009年8月13日编辑了该文章文章。</div> --> <div class="Blog_con2_1 Blog_con3_2"> <div> <!--<img src="/image/default/tu_8.png">--> <!-- JiaThis Button BEGIN --> <div class="bdsharebuttonbox"><A class=bds_more href="#" data-cmd="more"></A><A class=bds_qzone title=分享到QQ空间 href="#" data-cmd="qzone"></A><A class=bds_tsina title=分享到新浪微博 href="#" data-cmd="tsina"></A><A class=bds_tqq title=分享到腾讯微博 href="#" data-cmd="tqq"></A><A class=bds_renren title=分享到人人网 href="#" data-cmd="renren"></A><A class=bds_weixin title=分享到微信 href="#" data-cmd="weixin"></A></div> <script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script> <!-- JiaThis Button END --> </div> 阅读(4497) | 评论(0) | 转发(1) | <div class="HT_line3"></div> </div> <div class="Blog_con3_3"> <div><span id='digg_num'>0</span><a href="javascript:void(0)" id='digg' bid='603837' url='/blog/digg.html' ></a></div> <p>上一篇:<a href="/uid-7448773-id-310170.html">在Android中,通过JNI实现C++与Java相互调用</a></p> <p>下一篇:<a href="/uid-7448773-id-2305168.html">Debug Android with GDB.</a></p> </div> </div> <!-- <div class="Blog_con3_4 Blog_con3_5"> <div class="Blog_tit2 Blog_tit7">热门推荐</div> <ul> <li><a href="" title="" target='blank' ></a></li> </ul> </div> --> </div> </div> <div class="Blog_right1_7" id='replyList'> <div class="Blog_tit3">给主人留下些什么吧!~~</div> <!--暂无内容--> <!-- 评论分页--> <div class="Blog_right1_6 Blog_right1_12"> </div> <!-- 评论分页--> <div class="Blog_right1_10" style="display:none"> <div class="Blog_tit3">评论热议</div> <!--未登录 --> <div class="Blog_right1_8"> <div class="nologin_con1"> 请登录后评论。 <p><a href="http://account.chinaunix.net/login" onclick="link(this)">登录</a> <a href="http://account.chinaunix.net/register?url=http%3a%2f%2fblog.chinaunix.net">注册</a></p> </div> </div> </div> <div style="text-align:center;margin-top:10px;"> <script type="text/javascript" smua="d=p&s=b&u=u3118759&w=960&h=90" src="//www.nkscdn.com/smu0/o.js"></script> </div> </div> </div> </div> <input type='hidden' id='report_url' value='/blog/ViewReport.html' /> <script type="text/javascript"> //测试字符串的长度 一个汉字算2个字节 function mb_strlen(str) { var len=str.length; var totalCount=0; for(var i=0;i<len;i++) { var c = str.charCodeAt(i); if ((c >= 0x0001 && c <= 0x007e) || (0xff60<=c && c<=0xff9f)) { totalCount++; }else{ totalCount+=2; } } return totalCount; } /* var Util = {}; Util.calWbText = function (text, max){ if(max === undefined) max = 500; var cLen=0; var matcher = text.match(/[^\x00-\xff]/g), wlen = (matcher && matcher.length) || 0; //匹配url链接正则 http://*** var pattern = /http:\/\/([\w-]+\.)+[\w-]+(\/*[\w-\.\/\?%&=][^\s^\u4e00-\u9fa5]*)?/gi; //匹配的数据存入数组 var arrPt = new Array(); var i = 0; while((result = pattern.exec(text)) != null){ arrPt[i] = result[0]; i++; } //替换掉原文本中的链接 for(var j = 0;j<arrPt.length;j++){ text = text.replace(arrPt[j],""); } //减掉链接所占的长度 return Math.floor((max*2 - text.length - wlen)/2 - 12*i); }; */ var allowComment = '0'; //举报弹出层 function showJuBao(url, cid){ $.cover(false); asyncbox.open({ id : 'report_thickbox', url : url, title : '举报违规', width : 378, height : 240, scroll : 'no', data : { 'cid' : cid, 'idtype' : 2 , 'blogurl' : window.location.href }, callback : function(action){ if(action == 'close'){ $.cover(false); } } }); } $(function(){ //创建管理员删除的弹出层 $('#admin_article_del').click(function(){ $.cover(false); asyncbox.open({ id : 'class_thickbox', html : '<div class="HT_layer3_1"><ul><li class="HT_li1">操作原因:<select class="HT_sel7" id="del_type" name="del_type"><option value="广告文章">广告文章</option><option value="违规内容">违规内容</option><option value="标题不明">标题不明</option><option value="文不对题">文不对题</option></select></li><li class="HT_li1" ><input class="HT_btn4" id="admin_delete" type="button" /></li></ul></div>', title : '选择类型', width : 300, height : 150, scroll : 'no', callback : function(action){ if(action == 'close'){ $.cover(false); } } }); }); $('#admin_delete').live('click' , function(){ ///blog/logicdel/id/3480184/url/%252Fblog%252Findex.html.html var type = $('#del_type').val(); var url = '/blog/logicdel/id/603837/url/%252Fuid%252F7448773.html.html'; window.location.href= url + '?type=' + type; }); //顶 js中暂未添加&过滤 $('#digg').live('click' , function(){ if(isOnLine == '' ) { //showErrorMsg('登录之后才能进行此操作' , '消息提示'); showErrorMsg('操作失败,您需要先登录!', '消息提示', 'http://account.chinaunix.net/login'); return false; } var bid = $('#digg').attr('bid'); var url = $('#digg').attr('url'); var digg_str = $.cookie('digg_id'); if(digg_str != null) { var arr= new Array(); //定义一数组 arr = digg_str.split(","); //字符分割 for( i=0 ; i < arr.length ; i++ ) { if(bid == arr[i]) { showErrorMsg('已经赞过该文章', '消息提示'); return false; } } } $.ajax({ type:"POST", url:url, data: { 'bid' : bid }, dataType: 'json', success:function(msg){ if(msg.error == 0) { var num = parseInt($('#digg_num').html(),10); num += 1; $('#digg_num').html(num); $('#digg').die(); if(digg_str == null) { $.cookie('digg_id', bid, {expires: 30 , path: '/'}); } else { $.cookie('digg_id', digg_str + ',' + bid, {expires: 30 , path: '/'}); } showSucceedMsg('谢谢' , '消息提示'); } else if(msg.error == 1) { //showErrorMsg(msg.error_content , '消息提示'); showErrorMsg('操作失败,您需要先登录!', '消息提示', 'http://account.chinaunix.net/login'); } else if(msg.error == 2) { showErrorMsg(msg.error_content , '消息提示'); } } }); }); //举报弹出层 /*$('.box_report').live('click' , function(){ if(isOnLine == '' ) { //showErrorMsg('登录之后才能进行此操作' , '消息提示'); showErrorMsg('操作失败,您需要先登录!', '消息提示', 'http://account.chinaunix.net/login'); return false; } var url = $('#report_url').val(); var cid = $(this).attr('cid'); $.cover(false); asyncbox.open({ id : 'report_thickbox', url : url, title : '举报违规', width : 378, height : 240, scroll : 'no', data : { 'cid' : cid, 'idtype' : 2 }, callback : function(action){ if(action == 'close'){ $.cover(false); } } }); });*/ //评论相关代码 //点击回复显示评论框 $('.Blog_a10').live('click' , function(){ if(isOnLine == '' ) { //showErrorMsg('登录之后才能进行此操作' , '消息提示'); showErrorMsg('操作失败,您需要先登录!', '消息提示', 'http://account.chinaunix.net/login'); return false; } if(allowComment == 1) { showErrorMsg('该博文不允许评论' , '消息提示'); return false; } var tid = $(this).attr('toid');//留言作者id var bid = $(this).attr('bid');//blogid var cid = $(this).attr('cid');//留言id var tname = $(this).attr('tname'); var tpl = '<div class="Blog_right1_9">'; tpl += '<div class="div2">'; tpl += '<textarea name="" cols="" rows="" class="Blog_ar1_1" id="rmsg">文明上网,理性发言...</textarea>'; tpl += '</div>'; tpl += '<div class="div3">'; tpl += '<div class="div3_2"><a href="javascript:void(0);" class="Blog_a11" id="quota_sbumit" url="/Comment/PostComment.html" tid="'+tid+'" bid="'+bid+'" cid="'+cid+'" tname="'+tname+'" ></a><a href="javascript:void(0)" id="qx_comment" class="Blog_a12"></a></div>'; tpl += '<div class="div3_1"><a href="javascript:void(0);" id="mface"><span></span>表情</a></div>'; tpl += '<div class="clear"></div>'; tpl += '</div>'; tpl += '</div>'; $('.z_move_comment').html(''); $(this).parents('.Blog_right1_8').find('.z_move_comment').html(tpl).show(); }); //引用的评论提交 $('#quota_sbumit').live('click' , function(){ if(isOnLine == '' ) { //showErrorMsg('登录之后才能进行此操作' , '消息提示'); showErrorMsg('操作失败,您需要先登录!', '消息提示', 'http://account.chinaunix.net/login'); return false; } var bid = $(this).attr('bid'); var tid = $(this).attr('tid');//被引用人的id var qid = $(this).attr('cid');//引用的id var url = $(this).attr('url'); var text = $('#rmsg').val(); var tname = $(this).attr('tname'); if(text == '' || text=='文明上网,理性发言...') { showErrorMsg('评论内容不能为空!' , '消息提示'); return false; } else { if(mb_strlen(text) > 1000){ showErrorMsg('评论内容不能超过500个汉字' , '消息提示'); return false; } } $.ajax({ type: "post", url: url , data: {'bid': bid , 'to' : tid , 'qid' : qid , 'text': text , 'tname' : tname }, dataType: 'json', success: function(data){ if(data.code == 1){ var tpl = '<div class="Blog_right1_8">'; tpl+= '<div class="Blog_right_img1"><a href="' +data.info.url+ '" >' + data.info.header + '</a></div>'; tpl+= '<div class="Blog_right_font1">'; tpl+= '<p class="Blog_p5"><span><a href="' +data.info.url+ '" >' + data.info.username + '</a></span>' + data.info.dateline + '</p>'; tpl+= '<p class="Blog_p7"><a href="' + data.info.quota.url + '">' + data.info.quota.username + '</a>:'+ data.info.quota.content + '</p>'; tpl+= '<p class="Blog_p8">' + data.info.content + '</p><span class="span_text1"><a href="javascript:void(0);" class="Blog_a10" toid=' + data.info.fuid + ' bid=' + data.info.bid + ' cid=' + data.info.cid + ' tname = ' + data.info.username + ' >回复</a> |  <a class="comment_del_mark" style="cursor:pointer" url="' + data.info.delurl + '" >删除</a> |  <a href="javascript:showJuBao(\'/blog/ViewReport.html\','+data.info.cid+')" class="box_report" cid="' + data.info.cid + '" >举报</a></span></div>'; tpl+= '<div class="z_move_comment" style="display:none"></div>'; tpl+= '<div class="Blog_line1"></div></div>'; $('#replyList .Blog_right1_8:first').before(tpl); $('.z_move_comment').html('').hide(); } else if(data.code == -1){ //showErrorMsg(data.info , '消息提示'); showErrorMsg('操作失败,您需要先登录!', '消息提示', 'http://account.chinaunix.net/login'); } }, error: function(){//请求出错处理 } }); }); //底部发表评论 $('#submitmsg').click(function(){ if(allowComment == 1) { showErrorMsg('该博文不允许评论' , '消息提示'); return false; } var bid = $(this).attr('bid'); var toid = $(this).attr('toid'); var text = $('#reply').val(); var url = $(this).attr('url'); if(text == '' || text=='文明上网,理性发言...') { showErrorMsg('评论内容不能为空!' , '消息提示'); return false; } else { if(mb_strlen(text) > 1000){ showErrorMsg('评论内容不能超过500个汉字' , '消息提示'); return false; } } $.ajax({ type: "post", url: url , data: {'bid': bid , 'to' : toid ,'text': text}, dataType: 'json', success: function(data){ if(data.code == 1) { var tpl = '<div class="Blog_right1_8">'; tpl += '<div class="Blog_right_img1"><a href="' +data.info.url+ '" >' + data.info.header + '</a></div>'; tpl += '<div class="Blog_right_font1">'; tpl += '<p class="Blog_p5"><span><a href="' +data.info.url+ '" >' + data.info.username + '</a></span>' + data.info.dateline + '</p>'; tpl += '<p class="Blog_p6">' + data.info.content + '</p>'; tpl += '<div class="div1"><a href="javascript:void(0);" class="Blog_a10" toid=' + data.info.fuid + ' bid=' + data.info.bid + ' cid=' + data.info.cid + '>回复</a> |  <a class="comment_del_mark" style="cursor:pointer" url="' + data.info.delurl + '">删除</a> |  <a href="javascript:showJuBao(\'/blog/ViewReport.html\','+data.info.cid+')" class="box_report" cid="' + data.info.cid + '">举报</a></div>'; tpl += '<div class="z_move_comment" style="display:none"></div>'; tpl += '</div><div class="Blog_line1"></div></div>'; $('.Blog_tit3:first').after(tpl); $('#reply').val('文明上网,理性发言...'); } else if(data.code == -1) { showErrorMsg(data.info , '消息提示'); } }, error: function(){//请求出错处理 } }); }); //底部评论重置 $('#reset_comment').click(function(){ $('#reply').val('文明上网,理性发言...'); }); //取消回复 $('#qx_comment').live('click' , function(){ $('.z_move_comment').html('').hide(); }); $('#rmsg, #reply').live({ focus:function(){ if($(this).val() == '文明上网,理性发言...'){ $(this).val(''); } }, blur:function(){ if($(this).val() == ''){ $(this).val('文明上网,理性发言...'); } } }); //删除留言确认 $('.comment_del_mark').live('click' , function(){ var url = $(this).attr('url'); asyncbox.confirm('删除留言','确认', function(action){ if(action == 'ok') { location.href = url; } }); }); //删除时间确认 $('.del_article_id').click(function(){ var delurl = $(this).attr('delurl'); asyncbox.confirm('删除文章','确认', function(action){ if(action == 'ok') { location.href = delurl; } }); }); /* //字数限制 $('#rmsg, #reply').live('keyup', function(){ var id = $(this).attr('id'); var left = Util.calWbText($(this).val(), 500); var eid = '#errmsg'; if(id == 'reply') eid = '#rerrmsg'; if (left >= 0) $(eid).html('您还可以输入<span>' + left + '</span>字'); else $(eid).html('<font color="red">您已超出<span>' + Math.abs(left) + '</span>字 </font>'); }); */ //加载表情 $('#face').qqFace({id : 'facebox1', assign : 'reply', path : '/image/qqface/'}); $('#mface').qqFace({id : 'facebox', assign : 'rmsg', path:'/image/qqface/'}); /* $('#class_one_id').change(function(){ alert(123213); var id = parseInt($(this).val() , 10); if(id == 0) return false; $('.hidden_son_class span').each(function( index , dom ){ if( dom.attr('cid') == id ) { } }); }); */ //转载文章 var turn_url = "/blog/viewClassPart.html"; $('#repost_bar').click(function(){ if(isOnLine == '' ) { //showErrorMsg('登录之后才能进行此操作' , '消息提示'); showErrorMsg('操作失败,您需要先登录!', '消息提示', 'http://account.chinaunix.net/login'); return false; } var id = $(this).attr('bid'); asyncbox.open({ id : 'turn_class_thickbox', url : turn_url, title : '转载文章', width : 330, height : 131, scroll : 'no', data : { 'id' : id }, callback : function(action){ if(action == 'close'){ $.cover(false); } } }); }); /* //转发文章 $('#repost_bar').live('click' , function(){ if(isOnLine == '' ) { //showErrorMsg('登录之后才能进行此操作' , '消息提示'); showErrorMsg('操作失败,您需要先登录!', '消息提示', 'http://account.chinaunix.net/login'); return false; } var bid = $(this).attr('bid'); var url = $(this).attr('url'); asyncbox.confirm('转载文章','确认', function(action){ if(action == 'ok'){ $.ajax({ type:"POST", url:url, data: { 'bid' : bid }, dataType: 'json', success:function(msg){ if(msg.error == 0){ showSucceedMsg('转发成功!', '消息提示'); }else if(msg.error == 1){ //location.href = '/index.php?r=site/login'; showErrorMsg('操作失败,您需要先登录!', '消息提示', 'http://account.chinaunix.net/login'); }else{ showErrorMsg(msg.error_content, '消息提示'); } } }); } }); }); */ }); </script> <!--该部分应该放在输出代码块的后面才起作用 --> <script type="text/javascript"> SyntaxHighlighter.autoloader( 'applescript /highlight/scripts/shBrushAppleScript.js', 'actionscript3 as3 /highlight/scripts/shBrushAS3.js', 'bash shell /highlight/scripts/shBrushBash.js', 'coldfusion cf /highlight/scripts/shBrushColdFusion.js', 'cpp c /highlight/scripts/shBrushCpp.js', 'c# c-sharp csharp /highlight/scripts/shBrushCSharp.js', 'css /highlight/scripts/shBrushCss.js', 'delphi pascal /highlight/scripts/shBrushDelphi.js', 'diff patch pas /highlight/scripts/shBrushDiff.js', 'erl erlang /highlight/scripts/shBrushErlang.js', 'groovy /highlight/scripts/shBrushGroovy.js', 'java /highlight/scripts/shBrushJava.js', 'jfx javafx /highlight/scripts/shBrushJavaFX.js', 'js jscript javascript /highlight/scripts/shBrushJScript.js', 'perl pl /highlight/scripts/shBrushPerl.js', 'php /highlight/scripts/shBrushPhp.js', 'text plain /highlight/scripts/shBrushPlain.js', 'py python /highlight/scripts/shBrushPython.js', 'ruby rails ror rb /highlight/scripts/shBrushRuby.js', 'scala /highlight/scripts/shBrushScala.js', 'sql /highlight/scripts/shBrushSql.js', 'vb vbnet /highlight/scripts/shBrushVb.js', 'xml xhtml xslt html /highlight/scripts/shBrushXml.js' ); SyntaxHighlighter.all(); function code_hide(id){ var code = document.getElementById(id).style.display; if(code == 'none'){ document.getElementById(id).style.display = 'block'; }else{ document.getElementById(id).style.display = 'none'; } } </script> <!--回顶部js2011.12.30--> <script language="javascript"> lastScrollY=0; function heartBeat(){ var diffY; if (document.documentElement && document.documentElement.scrollTop) diffY = document.documentElement.scrollTop; else if (document.body) diffY = document.body.scrollTop else {/*Netscape stuff*/} percent=.1*(diffY-lastScrollY); if(percent>0)percent=Math.ceil(percent); else percent=Math.floor(percent); document.getElementById("full").style.top=parseInt(document.getElementById("full").style.top)+percent+"px"; lastScrollY=lastScrollY+percent; if(lastScrollY<200) { document.getElementById("full").style.display="none"; } else { document.getElementById("full").style.display="block"; } } var gkuan=document.body.clientWidth; var ks=(gkuan-960)/2-30; suspendcode="<div id=\"full\" style='right:-30px;POSITION:absolute;TOP:500px;z-index:100;width:26px; height:86px;cursor:pointer;'><a href=\"javascript:void(0)\" onclick=\"window.scrollTo(0,0);\"><img src=\"\/image\/top.png\" /></a></div>" document.write(suspendcode); window.setInterval("heartBeat()",1); </script> <!-- footer --> <div class="Blog_footer" style='clear:both'> <div><a href="http://www.chinaunix.net/about/index.shtml" target="_blank" rel="nofollow">关于我们</a> | <a href="http://www.it168.com/bottomfile/it168.shtml" target="_blank" rel="nofollow">关于IT168</a> | <a href="http://www.chinaunix.net/about/connect.html" target="_blank" rel="nofollow">联系方式</a> | <a href="http://www.chinaunix.net/about/service.html" target="_blank" rel="nofollow">广告合作</a> | <a href="http://www.it168.com//bottomfile/flgw/fl.htm" target="_blank" rel="nofollow">法律声明</a> | <a href="http://account.chinaunix.net/register?url=http%3a%2f%2fblog.chinaunix.net" target="_blank" rel="nofollow">免费注册</a> <p>Copyright 2001-2010 ChinaUnix.net All Rights Reserved 北京皓辰网域网络信息技术有限公司. 版权所有 </p> <div>感谢所有关心和支持过ChinaUnix的朋友们 <p><a href="http://beian.miit.gov.cn/">16024965号-6 </a></p> </div> </div> </div> </div> <script language="javascript"> //全局错误提示弹出框 function showErrorMsg(content, title, url){ var url = url || ''; var title = title || '消息提示'; var html = ''; html += '<div class="HT_layer3_1 HT_layer3_2"><ul><li><p><span class="login_span1"></span>' + content + '</p></li>'; if(url == '' || url.length == 0){ html += '<li class="HT_li1"><input type="button" class="HT_btn2" onclick=\'close_windows("error_msg")\'></li>'; } else { html += '<li class="HT_li1"><input type="button" class="login_btn1" onclick="location.href=\'' + url + '\'"></li>'; } html += '</ul></div>'; $.cover(true); asyncbox.open({ id: 'error_msg', title : title, html : html, 'callback' : function(action){ if(action == 'close'){ $.cover(false); } } }); } //全局正确提示 function showSucceedMsg(content, title , url ){ var url = url || ''; var title = title || '消息提示'; var html = ''; html += '<div class="HT_layer3_1 HT_layer3_2"><ul><li><p><span class="login_span2"></span>' + content + '</p></li>'; if(url == '' || url.length == 0){ html += '<li class="HT_li1"><input type="button" class="HT_btn2" onclick=\'close_windows("error_msg")\'></li>'; } else { html += '<li class="HT_li1"><input type="button" class="HT_btn2" onclick="location.href=\'' + url + '\'"></li>'; } html += '</ul></div>'; $.cover(true); asyncbox.open({ id: 'error_msg', title : title, html : html, 'callback' : function(action){ if(action == 'close'){ $.cover(false); } } }); } //关闭指定id的窗口 function close_windows(id){ $.cover(false); $.close(id); } //公告 var tID; var tn; // 高度 var nStopTime = 5000; // 不同行间滚动时间隔的时间,值越小,移动越快 var nSpeed = 50; // 滚动时,向上移动一像素间隔的时间,值越小,移动越快 var isMove = true; var nHeight = 25; var nS = 0; var nNewsCount = 3; /** * n 用于表示是否为第一次运行 **/ function moveT(n) { clearTimeout(tID) var noticev2 = document.getElementById("noticev2") nS = nSpeed; // 只在第一次调用时运行,初始化环境(有没有参数) if (n) { // 设置行高 noticev2.style.lineHeight = nHeight + "px"; // 初始化显示位置 tn = 0; // 刚进入时在第一行停止时间 nS = nStopTime; } // 判断鼠标是否指向层 if (isMove) { // 向上移动一像素 tn--; // 如果移动到最下面一行了,则移到顶行 if (Math.abs(tn) == nNewsCount * nHeight) { tn = 0; } // 设置位置 noticev2.style.marginTop = tn + "px"; // 完整显示一行时,停止一段时间 if (tn % nHeight == 0) { nS = nStopTime; } } tID = setTimeout("moveT()", nS); } moveT(1); // 此处可以传入任何参数 </script> <script type="text/javascript"> // var _gaq = _gaq || []; // _gaq.push(['_setAccount', 'UA-20237423-2']); // _gaq.push(['_setDomainName', '.chinaunix.net']); // _gaq.push(['_trackPageview']); // // (function() { // var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; // ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; // var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); // })(); </script> <script type="text/javascript"> var _bdhmProtocol = (("https:" == document.location.protocol) ? " https://" : " http://"); document.write(unescape("%3Cscript src='" + _bdhmProtocol + "hm.baidu.com/h.js%3F0ee5e8cdc4d43389b3d1bfd76e83216b' type='text/javascript'%3E%3C/script%3E")); function link(t){ var href= $(t).attr('href'); href+="?url="+encodeURIComponent(location.href); $(t).attr('href',href); //setCookie("returnOutUrl", location.href, 60, "/"); } </script> <script type="text/javascript" src="/js/jquery.qqFace.js"></script> </body> </html>