-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
175 lines (146 loc) · 5.04 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<title>Lincwee Test WebPage by lincwee</title>
<link rel="stylesheet" href="stylesheets/styles.css">
<link rel="stylesheet" href="stylesheets/github-light.css">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<div class="wrapper">
<header>
<h1>Lincwee Test WebPage</h1>
<p>lincwee's blog</p>
<p class="view"><a href="https://github.com/lincwee">View My GitHub Profile</a></p>
</header>
<section>
<h1>
<a id="lincweegithubio" class="anchor" href="#lincweegithubio" aria-hidden="true"><span class="octicon octicon-link"></span></a>lincwee.github.io</h1>
<h2>
<a id="lincwees-blog" class="anchor" href="#lincwees-blog" aria-hidden="true"><span class="octicon octicon-link"></span></a>lincwee's blog</h2>
<h2>
<a id="ios内存管理二-实现篇" class="anchor" href="#ios%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86%E4%BA%8C-%E5%AE%9E%E7%8E%B0%E7%AF%87" aria-hidden="true"><span class="octicon octicon-link"></span></a>iOS内存管理(二) 实现篇</h2>
<p>首先需要强调的是,现在iOS中的NSObject类的Foundation框架并没有公开。不过Foundation框架使用的Core Foundation框架的源代码,以及通过NSObject类进行的内存管理这一块儿的源代码是公开的。本次使用的是GNUstep来设计具体说明,从NSObejct的源代码到内存管理的源代码。</p>
<table>
<thead>
<tr>
<th align="left">对象操作</th>
<th align="left">Objective-C方法</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">生成并持有对象</td>
<td align="left">alloc/new/copy/mutableCopy</td>
</tr>
<tr>
<td align="left">持有对象</td>
<td align="left">retain</td>
</tr>
<tr>
<td align="left">释放对象</td>
<td align="left">release</td>
</tr>
<tr>
<td align="left">废弃对象</td>
<td align="left">delloc</td>
</tr>
</tbody>
</table>
<blockquote>
<p>基本用法</p>
</blockquote>
<pre><code>//取得非自己生成并持有的对象
id obj1 = [NSMutableArray array];
//取得并生成自己持有的对象
id obj2 = [[NSObject alloc] init];
//取得对象存在,并且自己能够持有
[obj1 retain];
//释放对象1
[obj1 release];
//释放对象2
[obj2 release];
//[obj delloc];
//嘿嘿,如果我再加一个release会怎么样
[obj1 release]
</code></pre>
<h3>
<a id="alloc" class="anchor" href="#alloc" aria-hidden="true"><span class="octicon octicon-link"></span></a>alloc</h3>
<blockquote>
<p>alloc实现代码</p>
</blockquote>
<pre><code>+(id) alloc
{
return [self allocWithZone:NSDefaultMallocZone()];
}
+(id)allocWithZone:(NSZone *)z
{
return NSAllocateObject(self, 0, z);
}
</code></pre>
<blockquote>
<p>通过allocWithZone:<strong>类</strong>方法调用NSAllocateObject函数分配了对象。</p>
</blockquote>
<pre><code>struct obj_layout{
NSUInterger retained;
}
inline id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone){
int size = 计算容纳对象所需内存大小的空间
//分配内存对象所需空间
id new = NSZoneMalloc(zone, size);
//将内存控件置0
memset(new, 0, size);
//得到并返回对象使用的指针,为什么是[1],因为,是要返回除了retained以外的部分,本身的计数器并不需要得到,所以就要是alloc指针向下移动一位。
new = (id)&((struct obj_layout *) new)[1];
}
</code></pre>
<h3>
<a id="retaincount" class="anchor" href="#retaincount" aria-hidden="true"><span class="octicon octicon-link"></span></a>retainCount</h3>
<blockquote>
<p>ratainCount代码实现</p>
</blockquote>
<pre><code>//获得当前对象的引用计数,可以使用会实例方法获得
id obj = [[NSObject alloc] init];
//此时应该输出的是1
NSLog(@"%d",[obj retainCount]);
-(NSUinteger) retainCount
{
//self用法
return NSExtraRefCount(self) + 1;
}
inline NSUInteger NSExtraRefCount(id anObject)
{
//这个[-1]的意思代表把alloc指针向上移动一位去的retained的内容
return ((struct obj_layout *) anobject)[-1].retained;
}
</code></pre>
<h3>
<a id="retain" class="anchor" href="#retain" aria-hidden="true"><span class="octicon octicon-link"></span></a>retain</h3>
<blockquote>
<p>retain和release基本相同</p>
</blockquote>
<pre><code>-(id) retain
{
NSIncrementExtraRefCount(self);
return self;
}
inline void NSIncrementExtraRefCount(id anObject)
{
if(((struct obj_layout *)anObject)[-1].retained == UINT_MAX - 1)
[NSException raise:NSInternalInconsistencyException format:@"NSIncrementExtraRefCount() asked to increment too far"];
((struct obj_layout *)anObject)[-1].retained++;
}
</code></pre>
</section>
<footer>
<p><small>Hosted on GitHub Pages — Theme by <a href="https://github.com/orderedlist">orderedlist</a></small></p>
</footer>
</div>
<script src="javascripts/scale.fix.js"></script>
</body>
</html>