经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
一文理解Android系统中强指针的实现
来源:jb51  时间:2021/10/8 17:42:30  对本文有异议

强指针和弱指针基础

android中的智能指针包括:轻量级指针、强指针、弱指针。
强指针:它主要是通过强引用计数来进行维护对象的生命周期。
弱指针:它主要是通过弱引用计数来进行维护所指向对象的生命周期。
如果在一个类中使用了强指针或者弱指针的技术,那么这个类就必须从RefBase这个类进行做继承,因为强指针和弱指针是通过RefBase这个类来提供实现的引用计数器。
强指针和弱指针关系相对于轻量级指针来说更加亲密,因此他们一般是相互配合使用的。

强指针原理分析

以下针对源码的分析都是来源于android5.0系统源码
强指针的定义实现主要在\frameworks\rs\cpp\util\RefBase.h文件中

  1. class RefBase
  2. {
  3. public:
  4. //定义了成员变量用于维护强引用对象的引用计数
  5. void incStrong(const void* id) const;
  6. //定义了成员变量用于维护强引用对象的引用计数
  7. void decStrong(const void* id) const;
  8. void forceIncStrong(const void* id) const;
  9.  
  10. //获取强指针计数的数量.
  11. int32_t getStrongCount() const;
  12. //这个类主要实现计数器的
  13. class weakref_type
  14. {
  15. public:
  16. RefBase* refBase() const;
  17.  
  18. void incWeak(const void* id);
  19. void decWeak(const void* id);
  20.  
  21. // acquires a strong reference if there is already one.
  22. bool attemptIncStrong(const void* id);
  23.  
  24. // acquires a weak reference if there is already one.
  25. // This is not always safe. see ProcessState.cpp and BpBinder.cpp
  26. // for proper use.
  27. bool attemptIncWeak(const void* id);
  28.  
  29. //! DEBUGGING ONLY: Get current weak ref count.
  30. int32_t getWeakCount() const;
  31.  
  32. //! DEBUGGING ONLY: Print references held on object.
  33. void printRefs() const;
  34.  
  35. //! DEBUGGING ONLY: Enable tracking for this object.
  36. // enable -- enable/disable tracking
  37. // retain -- when tracking is enable, if true, then we save a stack trace
  38. // for each reference and dereference; when retain == false, we
  39. // match up references and dereferences and keep only the
  40. // outstanding ones.
  41.  
  42. void trackMe(bool enable, bool retain);
  43. };
  44.  
  45. weakref_type* createWeak(const void* id) const;
  46.  
  47. weakref_type* getWeakRefs() const;
  48.  
  49. //! DEBUGGING ONLY: Print references held on object.
  50. inline void printRefs() const { getWeakRefs()->printRefs(); }
  51.  
  52. //! DEBUGGING ONLY: Enable tracking of object.
  53. inline void trackMe(bool enable, bool retain)
  54. {
  55. getWeakRefs()->trackMe(enable, retain);
  56. }
  57.  
  58. typedef RefBase basetype;
  59.  
  60. protected:
  61. RefBase();
  62. virtual ~RefBase();
  63.  
  64. //! Flags for extendObjectLifetime()
  65. enum {
  66. OBJECT_LIFETIME_STRONG = 0x0000,
  67. OBJECT_LIFETIME_WEAK = 0x0001,
  68. OBJECT_LIFETIME_MASK = 0x0001
  69. };
  70.  
  71. void extendObjectLifetime(int32_t mode);
  72.  
  73. //! Flags for onIncStrongAttempted()
  74. enum {
  75. FIRST_INC_STRONG = 0x0001
  76. };
  77.  
  78. virtual void onFirstRef();
  79. virtual void onLastStrongRef(const void* id);
  80. virtual bool onIncStrongAttempted(uint32_t flags, const void* id);
  81. virtual void onLastWeakRef(const void* id);
  82.  
  83. private:
  84. friend class ReferenceMover;
  85. static void moveReferences(void* d, void const* s, size_t n,
  86. const ReferenceConverterBase& caster);
  87.  
  88. private:
  89. friend class weakref_type;
  90. //通过类对象来获取计数器数据。
  91. class weakref_impl;
  92.  
  93. RefBase(const RefBase& o);
  94. RefBase& operator=(const RefBase& o);
  95.  
  96. weakref_impl* const mRefs;
  97. };

通过以上类定义可以看到 RefBase类里面嵌套着weakref_type类,这个weakref_type类也的对象mRefs来描述对象的引用计数。也就是说每一个RefBase对象都包含一个weakref_type对象。
virtual表示的是虚函数,friend表示友元函数,

总结

如果一个对象的生命周期控制标志值被设置为0的情况下,只要它的强引用计数值也为0,那么系统就会自动释放这个对象。

到此这篇关于一文理解Android系统中强指针的实现的文章就介绍到这了,更多相关Android 强指针内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号