Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

retain、release 的实现机制是怎样的? #14

Open
tbfungeek opened this issue Jan 5, 2020 · 0 comments
Open

retain、release 的实现机制是怎样的? #14

tbfungeek opened this issue Jan 5, 2020 · 0 comments

Comments

@tbfungeek
Copy link
Owner

建议查看我的博客网站上的关于《iOS 内存管理之对象创建,销毁,持有,弱引用源码剖析》具体的源码分析。http://coderlin.coding.me/

其实考察的还是 NONEPOINT_ISA 和 SideTables 结构

  • retain的实现机制。
SideTable& table = SideTables()[This];
size_t& refcntStorage = table.refcnts[This];
refcntStorage += SIZE_TABLE_RC_ONE;
  • release的实现机制。
SideTable& table = SideTables()[This];
size_t& refcntStorage = table.refcnts[This];
refcntStorage -= SIZE_TABLE_RC_ONE;

二者的实现机制类似,概括讲就是通过第一层 hash 算法,找到 指针变量 所对应的 sideTable。然后再通过一层 hash 算法,找到存储 引用计数 的 size_t,然后对其进行增减操作。retainCount 不是固定的 1,SIZE_TABLE_RC_ONE 是一个宏定义,实际上是一个值为 4 的偏移量。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant