Android系统中强指针是如何实现的
发表于:2025-02-12 作者:千家信息网编辑
千家信息网最后更新 2025年02月12日,本篇内容主要讲解"Android系统中强指针是如何实现的",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"Android系统中强指针是如何实现的"吧!强指针和
千家信息网最后更新 2025年02月12日Android系统中强指针是如何实现的
本篇内容主要讲解"Android系统中强指针是如何实现的",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"Android系统中强指针是如何实现的"吧!
强指针和弱指针基础
android中的智能指针包括:轻量级指针、强指针、弱指针。
强指针:它主要是通过强引用计数来进行维护对象的生命周期。
弱指针:它主要是通过弱引用计数来进行维护所指向对象的生命周期。
如果在一个类中使用了强指针或者弱指针的技术,那么这个类就必须从RefBase这个类进行做继承,因为强指针和弱指针是通过RefBase这个类来提供实现的引用计数器。
强指针和弱指针关系相对于轻量级指针来说更加亲密,因此他们一般是相互配合使用的。
强指针原理分析
以下针对源码的分析都是来源于android5.0系统源码
强指针的定义实现主要在\frameworks\rs\cpp\util\RefBase.h文件中
class RefBase{public: //定义了成员变量用于维护强引用对象的引用计数 void incStrong(const void* id) const; //定义了成员变量用于维护强引用对象的引用计数 void decStrong(const void* id) const; void forceIncStrong(const void* id) const; //获取强指针计数的数量. int32_t getStrongCount() const; //这个类主要实现计数器的 class weakref_type { public: RefBase* refBase() const; void incWeak(const void* id); void decWeak(const void* id); // acquires a strong reference if there is already one. bool attemptIncStrong(const void* id); // acquires a weak reference if there is already one. // This is not always safe. see ProcessState.cpp and BpBinder.cpp // for proper use. bool attemptIncWeak(const void* id); //! DEBUGGING ONLY: Get current weak ref count. int32_t getWeakCount() const; //! DEBUGGING ONLY: Print references held on object. void printRefs() const; //! DEBUGGING ONLY: Enable tracking for this object. // enable -- enable/disable tracking // retain -- when tracking is enable, if true, then we save a stack trace // for each reference and dereference; when retain == false, we // match up references and dereferences and keep only the // outstanding ones. void trackMe(bool enable, bool retain); }; weakref_type* createWeak(const void* id) const; weakref_type* getWeakRefs() const; //! DEBUGGING ONLY: Print references held on object. inline void printRefs() const { getWeakRefs()->printRefs(); } //! DEBUGGING ONLY: Enable tracking of object. inline void trackMe(bool enable, bool retain) { getWeakRefs()->trackMe(enable, retain); } typedef RefBase basetype;protected: RefBase(); virtual ~RefBase(); //! Flags for extendObjectLifetime() enum { OBJECT_LIFETIME_STRONG = 0x0000, OBJECT_LIFETIME_WEAK = 0x0001, OBJECT_LIFETIME_MASK = 0x0001 }; void extendObjectLifetime(int32_t mode); //! Flags for onIncStrongAttempted() enum { FIRST_INC_STRONG = 0x0001 }; virtual void onFirstRef(); virtual void onLastStrongRef(const void* id); virtual bool onIncStrongAttempted(uint32_t flags, const void* id); virtual void onLastWeakRef(const void* id);private: friend class ReferenceMover; static void moveReferences(void* d, void const* s, size_t n, const ReferenceConverterBase& caster);private: friend class weakref_type; //通过类对象来获取计数器数据。 class weakref_impl; RefBase(const RefBase& o); RefBase& operator=(const RefBase& o); weakref_impl* const mRefs;};
通过以上类定义可以看到 RefBase类里面嵌套着weakref_type类,这个weakref_type类也的对象mRefs来描述对象的引用计数。也就是说每一个RefBase对象都包含一个weakref_type对象。
virtual表示的是虚函数,friend表示友元函数,
到此,相信大家对"Android系统中强指针是如何实现的"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
指针
对象
系统
中强
计数器
内容
函数
变量
周期
成员
源码
生命
轻量
轻量级
数来
分析
学习
亲密
实用
更深
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
福建网络技术开发口碑推荐
光网络技术大二层
电脑软件开发什么意思
电力 数据库
计算机网络安全技术浅析论文
储存文档使用什么数据库
数据库技术在电气专业中的应用
列举生活中网络安全问题
台达服务器机柜
sql数据库sa用户
大学班级网络安全教育活动方案
神通数据库 oracle
云服务器外国
手机卡服务器节点
移动网络安全宣传标语
软件开发的组织管理
我的世界服务器pvp管理
参加网络安全大赛获奖会怎样
软件开发项目管理表
软件开发知识竞赛题库可下载
移动端数据库 源码下载
网络安全法与个人信息保护
wpress数据库
数据库技术与应用知识结构图
json插入数据库字符串太长
2002年机房服务器
怎么构建sql数据库
数据库页内扫描的原理
软件开发方案如何设计
北京金融用哪些软件开发