一文理解Android系统中强指针的实现
作者:小道安全 发布时间:2022-03-11 11:37:02
标签: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表示友元函数,
来源:https://blog.csdn.net/c_kongfei/article/details/120631334?utm_medium=distribute.pc_category.none-task-blog-hot-10.nonecase&depth_1-utm_source=distribute.pc_category.none-task-blog-hot-10.nonecase


猜你喜欢
- java 在Jetty9中使用HttpSessionListener和FilterHttpSessionListener当Session创建
- 一、跨域认证的问题互联网服务离不开用户认证。一般流程是下面这样。1、用户向服务器发送用户名和密码。2、服务器验证通过后,在当前对话(sess
- 爱java 爱dota,突发奇想想用java开发dota操作最华丽的英雄之一的卡尔的技能,因为本人系小白,代码不足的地方还请包涵,有同样爱好
- 介绍fastjson 1.2.0之后的版本支持JSONPath。,可以在java框架中当作json对象查询语言(OQL)来使用。常用APIp
- 本篇超级详细案例截图教学 IDEA如何运行SpringBoot项目,图片点击可放大仔细看Java编译工具以及环境准备:IDEA JDK1.8
- 定义建造者模式(Builder Pattern),又叫生成器模式,是一种对象构建模式 它可以将复杂对象的建造过程抽象出来,使这个抽象过程的不
- SpringBoot使用Commons Logging进行所有内部日志记录,但保留底层日志实现。默认提供了Java Util Logging
- 本文实例为大家分享了android实现动态显示隐藏进度条的具体代码,供大家参考,具体内容如下调用ProgressUtil.startProg
- init.rc脚本包含5种类型,Action(动作),Commands(命令), Services(服务),Options(选项), Imp
- 这篇文章主要介绍了JAVA利用递归删除文件代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可
- Android Studio第一次启动的Fetching android sdk component information的问题1)进入刚
- Java 8的18个常用日期处理一、简介伴随lambda表达式、streams以及一系列小优化,Java 8 推出了全新的日期时间API。J
- /** * 循环录像,当内存卡容量少于300M时,自动删除视频列表里面的第一个文件 */private void xunhuanluxian
- 使用@Autowired注解有错误提示使用Spring boot +mybatis框架时,在service实现类中使用Mapper类,给Ma
- 前言:线程安全是并发编程中的重要关注点,造成线程安全问题的主要原因有两点,一是存在共享数据(也称临界资源),二是存在多条线程共同操作共享数据
- 1.后台参数校验Spring Validation验证框架对参数的验证机制提供了@Validated(Spring JSR-303规范,是标
- Android存储方式有很多种,在这里所用的存储方式是SharedPreferrences,其采用了Map数据结构来存储数据,以键值的方式存
- 本文实例为大家分享了Java实现多人聊天室的具体代码,供大家参考,具体内容如下先说,记录本人的学习过程,当笔记了多人聊天室分为1.服务器①.
- Java计算一段程序的运行时间介绍了两种方法,一种是毫秒级别的计算,另一种是更精确的纳秒级别的计算。毫秒级别计算时间  
- 本文实例讲述了Android编程实现获得内存剩余大小与总大小的方法。分享给大家供大家参考,具体如下:public class memInfo