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

智能指针来源

引发指针错误情况表现常常有如下几个表现情况:
1.申请了内存空间,但是忘记释放指针所指向的对象占用的内存空间。
2.使用了无效的指针。
因此在android的C++代码部分采用了智能指针的技术。智能指针通过一种能够自动危害对象引用计数的技术。来解决C++中指针存在的缺陷问题。
在android系统中提供了三种类型的C++智能指针,分别为:轻量级智能指针、强指针、弱指针。
下面主要通过进行分析轻量级指针的实现原理。

轻量级指针

轻量级指针就是通过利用简单的引用计数计数类维护对象的生命周期,如果一个类的对象支持使用轻量级指针,那么它就必须要从LightRefBase类进行继承,因为这个LightRefBase类提供了一个简单的引用计数器。

LightRefBase类定义

下面的源码主要依据android5.0的源码进行分析的。LightRefBase类的定义在android系统中的\frameworks\rs\cpp\util\RefBase.h 这个文件中。
LightRefBase类也是一个模板类。模板参数T表示对象的实际类型,它必须进行对LightRefBase这个类继承。

  1. //模板类
  2. template <class T>
  3. class LightRefBase
  4. {
  5. public:
  6. //公共的内联函数
  7. inline LightRefBase() : mCount(0) { }
  8. inline void incStrong(__attribute__((unused)) const void* id) const {
  9. __sync_fetch_and_add(&mCount, 1);
  10. }
  11. inline void decStrong(__attribute__((unused)) const void* id) const {
  12. if (__sync_fetch_and_sub(&mCount, 1) == 1) {
  13. delete static_cast<const T*>(this);
  14. }
  15. }
  16. //! DEBUGGING ONLY: Get current strong ref count.
  17. inline int32_t getStrongCount() const {
  18. return mCount;
  19. }
  20.  
  21. typedef LightRefBase<T> basetype;
  22.  
  23. protected:
  24. //内联的虚构函数
  25. inline ~LightRefBase() { }
  26.  
  27. private:
  28. friend class ReferenceMover;
  29. inline static void moveReferences(void*, void const*, size_t,
  30. const ReferenceConverterBase&) { }
  31.  
  32. private:
  33. mutable volatile int32_t mCount;
  34. };
  35.  
  36.  
  37.  

上面LightRefBase类定义涉及到几个知识点:

  • 1、C++的三个访问权限类型:public、protected、private。

public:可以被任意实体访问。
protected:只允许子类及本类的成员函数访问。
private:只允许本类的成员函数访问。

  • 2、C++中的inline内联函数

在函数第一部分如果包含有inline关键字的函数,那么这个函数就表示为内联函数。内联函数主要为了解决一些频繁调用的小函数大量消耗栈空间(栈内存)的问题。
inline的使用是有所限制的,inline只适合涵数体内代码简单的涵数使用,不能包含复杂的结构控制语句例如while、switch,并且不能内联函数本身不能是直接递归函数(递归函数:自己内部还调用自己的函数)。

  • 3、C++中的friend友元函数

C++中的友元机制允许类的非公有成员被一个类或者函数访问,友元按类型分为三种:普通非类成员函数作为友元,类的成员函数作为友元,类作为友元。
友元函数是可以直接访问类的私有成员的非成员函数。它是定义在类外的普通函数,它不属于任何类,但需要在类的定义中加以声明,声明时只需在友元的名称前加上关键字friend。

  • 4、C++中的mutable关键字

mutable是为了突破const的限制而设置的。被mutable修饰的变量,将永远处于可变的状态,即使在一个const函数中。

  • 5、C++中的template类模板

一个类模板(也称为类属类或类生成类)同意用户为类定义一种模式。使得类中的某些数据成员、默写成员函数的參数、某些成员函数的返回值,能够取随意类型(包含系统提前定义的和用户自己定义的)。
类模板的应用场景:多个类有着共同操作,但是数据类型不同。

LightRefBase的实现类

轻量级LightRefBase类的实现类为wp类,它也是在\frameworks\rs\cpp\util\RefBase.h 这个文件中。wp也是一个模板类,模板参数T表示的是对象的实际类型,它必须继承LightRefBase类。由于wp类也是强指针的实现类,因此我们只需要分析下该wp类中关于轻量级指针类的实现部分就可以了。

  1. //模板类
  2. template <typename T>
  3. class wp
  4. {
  5. public:
  6. typedef typename RefBase::weakref_type weakref_type;
  7. //轻量级指针需要用到的构造函数
  8. inline wp() : m_ptr(0) { }
  9.  
  10. wp(T* other);
  11. wp(const wp<T>& other);
  12. wp(const sp<T>& other);
  13. template<typename U> wp(U* other);
  14. template<typename U> wp(const sp<U>& other);
  15. template<typename U> wp(const wp<U>& other);
  16. //轻量级指针需要用到的虚构函数
  17. ~wp();
  18.  
  19. // Assignment
  20.  
  21. wp& operator = (T* other);
  22. wp& operator = (const wp<T>& other);
  23. wp& operator = (const sp<T>& other);
  24.  
  25. template<typename U> wp& operator = (U* other);
  26. template<typename U> wp& operator = (const wp<U>& other);
  27. template<typename U> wp& operator = (const sp<U>& other);
  28.  
  29. void set_object_and_refs(T* other, weakref_type* refs);
  30.  
  31. // promotion to sp
  32.  
  33. sp<T> promote() const;
  34.  
  35. // Reset
  36.  
  37. void clear();
  38.  
  39. // Accessors
  40.  
  41. inline weakref_type* get_refs() const { return m_refs; }
  42.  
  43. inline T* unsafe_get() const { return m_ptr; }
  44.  
  45. // Operators
  46.  
  47. COMPARE_WEAK(==)
  48. COMPARE_WEAK(!=)
  49. COMPARE_WEAK(>)
  50. COMPARE_WEAK(<)
  51. COMPARE_WEAK(<=)
  52. COMPARE_WEAK(>=)
  53.  
  54. inline bool operator == (const wp<T>& o) const {
  55. return (m_ptr == o.m_ptr) && (m_refs == o.m_refs);
  56. }
  57. template<typename U>
  58. inline bool operator == (const wp<U>& o) const {
  59. return m_ptr == o.m_ptr;
  60. }
  61.  
  62. inline bool operator > (const wp<T>& o) const {
  63. return (m_ptr == o.m_ptr) ? (m_refs > o.m_refs) : (m_ptr > o.m_ptr);
  64. }
  65. template<typename U>
  66. inline bool operator > (const wp<U>& o) const {
  67. return (m_ptr == o.m_ptr) ? (m_refs > o.m_refs) : (m_ptr > o.m_ptr);
  68. }
  69.  
  70. inline bool operator < (const wp<T>& o) const {
  71. return (m_ptr == o.m_ptr) ? (m_refs < o.m_refs) : (m_ptr < o.m_ptr);
  72. }
  73. template<typename U>
  74. inline bool operator < (const wp<U>& o) const {
  75. return (m_ptr == o.m_ptr) ? (m_refs < o.m_refs) : (m_ptr < o.m_ptr);
  76. }
  77. inline bool operator != (const wp<T>& o) const { return m_refs != o.m_refs; }
  78. template<typename U> inline bool operator != (const wp<U>& o) const { return !operator == (o); }
  79. inline bool operator <= (const wp<T>& o) const { return !operator > (o); }
  80. template<typename U> inline bool operator <= (const wp<U>& o) const { return !operator > (o); }
  81. inline bool operator >= (const wp<T>& o) const { return !operator < (o); }
  82. template<typename U> inline bool operator >= (const wp<U>& o) const { return !operator < (o); }
  83.  
  84. private:
  85. template<typename Y> friend class sp;
  86. template<typename Y> friend class wp;
  87. //轻量级指针会用到的变量m_ptr
  88. T* m_ptr;
  89. weakref_type* m_refs;
  90. };
  91.  

总结

通过前面的概念和系统源码实现功能原理分析,我们如果要使用轻量级指针的话,那么要包含头文件并采用继承LightRefBase类的方式,那么就可以使用轻量级指针的功能了。

到此这篇关于理解关于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号