-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjc-object.h
1305 lines (1040 loc) · 38.4 KB
/
objc-object.h
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2010-2012 Apple Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
/***********************************************************************
* Inlineable parts of NSObject / objc_object implementation
**********************************************************************/
#ifndef _OBJC_OBJCOBJECT_H_
#define _OBJC_OBJCOBJECT_H_
#include "objc-private.h"
enum ReturnDisposition : bool {
ReturnAtPlus0 = false, ReturnAtPlus1 = true
};
static ALWAYS_INLINE
bool prepareOptimizedReturn(ReturnDisposition disposition);
#if SUPPORT_TAGGED_POINTERS
extern "C" {
extern Class objc_debug_taggedpointer_classes[_OBJC_TAG_SLOT_COUNT*2];
extern Class objc_debug_taggedpointer_ext_classes[_OBJC_TAG_EXT_SLOT_COUNT];
}
#define objc_tag_classes objc_debug_taggedpointer_classes
#define objc_tag_ext_classes objc_debug_taggedpointer_ext_classes
#endif
#if SUPPORT_INDEXED_ISA
ALWAYS_INLINE Class &
classForIndex(uintptr_t index) {
assert(index > 0);
assert(index < (uintptr_t)objc_indexed_classes_count);
return objc_indexed_classes[index];
}
#endif
inline bool
objc_object::isClass()
{
if (isTaggedPointer()) return false;
return ISA()->isMetaClass();
}
#if SUPPORT_TAGGED_POINTERS
inline Class
objc_object::getIsa()
{
if (!isTaggedPointer()) return ISA();
uintptr_t ptr = (uintptr_t)this;
if (isExtTaggedPointer()) {
uintptr_t slot =
(ptr >> _OBJC_TAG_EXT_SLOT_SHIFT) & _OBJC_TAG_EXT_SLOT_MASK;
return objc_tag_ext_classes[slot];
} else {
uintptr_t slot =
(ptr >> _OBJC_TAG_SLOT_SHIFT) & _OBJC_TAG_SLOT_MASK;
return objc_tag_classes[slot];
}
}
inline bool
objc_object::isTaggedPointer()
{
return _objc_isTaggedPointer(this);
}
inline bool
objc_object::isBasicTaggedPointer()
{
return isTaggedPointer() && !isExtTaggedPointer();
}
inline bool
objc_object::isExtTaggedPointer()
{
uintptr_t ptr = _objc_decodeTaggedPointer(this);
return (ptr & _OBJC_TAG_EXT_MASK) == _OBJC_TAG_EXT_MASK;
}
// SUPPORT_TAGGED_POINTERS
#else
// not SUPPORT_TAGGED_POINTERS
inline Class
objc_object::getIsa()
{
return ISA();
}
inline bool
objc_object::isTaggedPointer()
{
return false;
}
inline bool
objc_object::isBasicTaggedPointer()
{
return false;
}
inline bool
objc_object::isExtTaggedPointer()
{
return false;
}
// not SUPPORT_TAGGED_POINTERS
#endif
#if SUPPORT_NONPOINTER_ISA
inline Class
objc_object::ISA()
{
assert(!isTaggedPointer());
#if SUPPORT_INDEXED_ISA /// iWatch
if (isa.nonpointer) {
uintptr_t slot = isa.indexcls;
return classForIndex((unsigned)slot);
}
return (Class)isa.bits;
#else
/// 获取class地址
return (Class)(isa.bits & ISA_MASK);
/// arm64平台 ISA_MASK的值 用于获取isa_t结构中存储的class的地址(33位有效位)
/// 0x 0 0 0 0 0 0 0 f f f f f f f f 8 ULL
/// b 0000 0000 0000 0000 0000 0000 0000 1111 1111 1111 1111 1111 1111 1111 1111 1000
#endif
}
inline bool
objc_object::hasNonpointerIsa()
{
return isa.nonpointer;
}
inline void
objc_object::initIsa(Class cls)
{
initIsa(cls, false, false);
}
inline void
objc_object::initClassIsa(Class cls)
{
if (DisableNonpointerIsa || cls->instancesRequireRawIsa()) {
initIsa(cls, false/*not nonpointer*/, false);
} else {
initIsa(cls, true/*nonpointer*/, false);
}
}
inline void
objc_object::initProtocolIsa(Class cls)
{
return initClassIsa(cls);
}
inline void
objc_object::initInstanceIsa(Class cls, bool hasCxxDtor)
{
assert(!cls->instancesRequireRawIsa());
assert(hasCxxDtor == cls->hasCxxDtor());
initIsa(cls, true, hasCxxDtor);
}
// cls参数表示对象的类,nonpointer表示是否构建非指针类型isa,hasCxxDtor表示对象是否存在cxx语系析构函数。
inline void
objc_object::initIsa(Class cls, bool nonpointer, bool hasCxxDtor)
{
assert(!isTaggedPointer());
if (!nonpointer) { /// 非优化的isa,直接存储cls地址
isa.cls = cls;
} else {
assert(!DisableNonpointerIsa);
assert(!cls->instancesRequireRawIsa());
isa_t newisa(0); /// 构造函数,初始化为0
#if SUPPORT_INDEXED_ISA /// iWatch
assert(cls->classArrayIndex() > 0);
newisa.bits = ISA_INDEX_MAGIC_VALUE;
// isa.magic is part of ISA_MAGIC_VALUE
// isa.nonpointer is part of ISA_MAGIC_VALUE
newisa.has_cxx_dtor = hasCxxDtor;
newisa.indexcls = (uintptr_t)cls->classArrayIndex();
#else
// 当前主流机型一般会运行到这个逻辑分支
// ISA_MAGIC_VALUE 0x000001a000000001ULL(arm64) 初始化了 nonpointer 和 magic两个标志位
newisa.bits = ISA_MAGIC_VALUE; /// // magic设置为0xA1,nonpointer设置为1
/// 初始化是否有C++析构函数 如果没有析构器就会快速释放内存
newisa.has_cxx_dtor = hasCxxDtor;
// shiftcls位域保存对象的类的地址,注意最低3位不需要保存,因为必定是全0
newisa.shiftcls = (uintptr_t)cls >> 3;
/**
将当前地址右移三位的主要原因是用于将 Class 指针中无用的后三位清除减小内存的消耗,因为类的指针要按照字节(8 bits)对齐内存,其指针后三位都是没有意义的 0。
Using an entire pointer-sized piece of memory for the isa pointer is a bit wasteful, especially on 64-bit CPUs which don’t use all 64 bits of a pointer. ARM64 running iOS currently uses only 33 bits of a pointer, leaving 31 bits for other purposes.
Class pointers are also aligned, meaning that a class pointer is guaranteed to be divisible by 8, which frees up another three bits, leaving 34 bits of the isa available for other uses. Apple’s ARM64 runtime takes advantage of this for some great performance improvements.
*/
#endif
// This write must be performed in a single store in some cases
// (for example when realizing a class because other threads
// may simultaneously try to use the class).
// fixme use atomics here to guarantee single-store and to
// guarantee memory order w.r.t. the class index table
// ...but not too atomic because we don't want to hurt instantiation
isa = newisa;
}
}
inline Class
objc_object::changeIsa(Class newCls)
{
// This is almost always true but there are
// enough edge cases that we can't assert it.
// assert(newCls->isFuture() ||
// newCls->isInitializing() || newCls->isInitialized());
assert(!isTaggedPointer());
isa_t oldisa;
isa_t newisa;
bool sideTableLocked = false;
bool transcribeToSideTable = false;
do {
transcribeToSideTable = false;
oldisa = LoadExclusive(&isa.bits);
if ((oldisa.bits == 0 || oldisa.nonpointer) &&
!newCls->isFuture() && newCls->canAllocNonpointer())
{
// 0 -> nonpointer
// nonpointer -> nonpointer
#if SUPPORT_INDEXED_ISA
if (oldisa.bits == 0) newisa.bits = ISA_INDEX_MAGIC_VALUE;
else newisa = oldisa;
// isa.magic is part of ISA_MAGIC_VALUE
// isa.nonpointer is part of ISA_MAGIC_VALUE
newisa.has_cxx_dtor = newCls->hasCxxDtor();
assert(newCls->classArrayIndex() > 0);
newisa.indexcls = (uintptr_t)newCls->classArrayIndex();
#else
if (oldisa.bits == 0) newisa.bits = ISA_MAGIC_VALUE;
else newisa = oldisa;
// isa.magic is part of ISA_MAGIC_VALUE
// isa.nonpointer is part of ISA_MAGIC_VALUE
newisa.has_cxx_dtor = newCls->hasCxxDtor();
newisa.shiftcls = (uintptr_t)newCls >> 3;
#endif
}
else if (oldisa.nonpointer) {
// nonpointer -> raw pointer
// Need to copy retain count et al to side table.
// Acquire side table lock before setting isa to
// prevent races such as concurrent -release.
if (!sideTableLocked) sidetable_lock();
sideTableLocked = true;
transcribeToSideTable = true;
newisa.cls = newCls;
}
else {
// raw pointer -> raw pointer
newisa.cls = newCls;
}
} while (!StoreExclusive(&isa.bits, oldisa.bits, newisa.bits));
if (transcribeToSideTable) {
// Copy oldisa's retain count et al to side table.
// oldisa.has_assoc: nothing to do
// oldisa.has_cxx_dtor: nothing to do
sidetable_moveExtraRC_nolock(oldisa.extra_rc,
oldisa.deallocating,
oldisa.weakly_referenced);
}
if (sideTableLocked) sidetable_unlock();
if (oldisa.nonpointer) {
#if SUPPORT_INDEXED_ISA
return classForIndex(oldisa.indexcls);
#else
return (Class)((uintptr_t)oldisa.shiftcls << 3);
#endif
}
else {
return oldisa.cls;
}
}
inline bool
objc_object::hasAssociatedObjects()
{
if (isTaggedPointer()) return true;
if (isa.nonpointer) return isa.has_assoc;
return true;
}
inline void
objc_object::setHasAssociatedObjects()
{
if (isTaggedPointer()) return;
retry:
isa_t oldisa = LoadExclusive(&isa.bits);
isa_t newisa = oldisa;
if (!newisa.nonpointer || newisa.has_assoc) {
ClearExclusive(&isa.bits);
return;
}
newisa.has_assoc = true;
if (!StoreExclusive(&isa.bits, oldisa.bits, newisa.bits)) goto retry;
}
inline bool
objc_object::isWeaklyReferenced()
{
/// isWeaklyReferenced()用于查询对象是否被弱引用。当对象isa为非指针类型时,直接返回isa.weakly_referenced,
/// 否则需要调用sidetable_isWeaklyReferenced ()从 side table 中查询结果;
assert(!isTaggedPointer());
if (isa.nonpointer) return isa.weakly_referenced;
else return sidetable_isWeaklyReferenced();
}
inline void
objc_object::setWeaklyReferenced_nolock()
{
/// 当对象isa为非指针类型时,仅需将weakly_referenced位置为1,否则需要调用sidetable_setWeaklyReferenced_nolock()从 side table 中查询结果并写入。
retry:
isa_t oldisa = LoadExclusive(&isa.bits);
isa_t newisa = oldisa;
if (slowpath(!newisa.nonpointer)) {
ClearExclusive(&isa.bits);
// 则调用sidetable_setWeaklyReferenced_nolock()方法,只是简单设置了SideTable的refcnts中该对象的引用��数信息的弱引用标记位为1
sidetable_setWeaklyReferenced_nolock();
return;
}
if (newisa.weakly_referenced) {
ClearExclusive(&isa.bits);
return;
}
newisa.weakly_referenced = true; /// 标记对象被弱引用
if (!StoreExclusive(&isa.bits, oldisa.bits, newisa.bits)) goto retry;
}
inline bool
objc_object::hasCxxDtor()
{
assert(!isTaggedPointer());
if (isa.nonpointer) return isa.has_cxx_dtor;
else return isa.cls->hasCxxDtor();
}
inline bool
objc_object::rootIsDeallocating()
{
if (isTaggedPointer()) return false;
if (isa.nonpointer) return isa.deallocating;
return sidetable_isDeallocating();
}
inline void
objc_object::clearDeallocating()
{
if (slowpath(!isa.nonpointer)) {
// Slow path for raw pointer isa.
sidetable_clearDeallocating();
}
else if (slowpath(isa.weakly_referenced || isa.has_sidetable_rc)) {
// Slow path for non-pointer isa with weak refs and/or side table data.
clearDeallocating_slow();
}
assert(!sidetable_present());
}
/**
对象的析构
对象的析构调用对象的rootDealloc()方法,源代码虽然不很多但是整个过程经过了几个函数。总结对象析构所需要的操作如下:
释放对关联对象的引用;
清空 side table 中保存的该对象弱引用地址、对象引用计数等内存管理数据;
释放对象占用的内存;
*/
inline void
objc_object::rootDealloc()
{
if (isTaggedPointer()) return; // fixme necessary?
/// 优化的isa:没有弱引用 没有关联对象没有C++析构 没有sidetable辅助存储引用计数则可以快速释放对象
if (fastpath(isa.nonpointer &&
!isa.weakly_referenced &&
!isa.has_assoc &&
!isa.has_cxx_dtor &&
!isa.has_sidetable_rc))
{
assert(!sidetable_present());
free(this); /// 释放对象占用内存
}
else {
/// 内部会执行C++析构、移除关联对象、该对象所在sidetable中引用计数和弱引用相关清理工作
object_dispose((id)this);
}
}
// Equivalent to calling [this retain], with shortcuts if there is no override
inline id
objc_object::retain()
{
assert(!isTaggedPointer()); /// 不是TaggedPointer
/**
class or superclass has default retain/release/autorelease/retainCount/_tryRetain/_isDeallocating/retainWeakReference/allowsWeakReference
hasCustomRR() 用于判定对象是否有自定义的retain函数指针
**/
if (fastpath(!ISA()->hasCustomRR())) { /// 大概率执行此处的代码
return rootRetain();
}
return ((id(*)(objc_object *, SEL))objc_msgSend)(this, SEL_retain);
}
// Base retain implementation, ignoring overrides.
// This does not check isa.fast_rr; if there is an RR override then
// it was already called and it chose to call [super retain].
//
// tryRetain=true is the -_tryRetain path.
// handleOverflow=false is the frameless fast path.
// handleOverflow=true is the framed slow path including overflow to side table
// The code is structured this way to prevent duplication.
ALWAYS_INLINE id
objc_object::rootRetain()
{
return rootRetain(false, false);
}
ALWAYS_INLINE bool
objc_object::rootTryRetain()
{
return rootRetain(true, false) ? true : false;
}
ALWAYS_INLINE id
objc_object::rootRetain(bool tryRetain, bool handleOverflow)
{
/// 如果是tagged pointer,直接返回this,因为tagged pointer不用记录引用次数
if (isTaggedPointer()) return (id)this;
bool sideTableLocked = false;
// transcribeToSideTable用于表示extra_rc是否溢出,默认为false transcribe:转录
bool transcribeToSideTable = false;
isa_t oldisa;
isa_t newisa;
do {
transcribeToSideTable = false;
/**
LoadExclusive(uintptr_t *src)直接返回*src,即src指针指向的内容。
代码中oldisa = LoadExclusive(&isa.bits)看起来是oldisa加载了isa.bits,但是由于isa.bits数据实际上就是isa的数据,
因此oldisa实质上是加载了isa,写成oldisa = (isa_t)LoadExclusive(&isa.bits)可以更好理解
*/
oldisa = LoadExclusive(&isa.bits); /// 将isa_t提取出来
newisa = oldisa;
if (slowpath(!newisa.nonpointer)) { /// 小概率执行此处代码: 是未经优化的isa概率很小
/// 如果是未经过优化的isa那么直接使用sidetable记录引用计数
ClearExclusive(&isa.bits);
if (!tryRetain && sideTableLocked) sidetable_unlock(); /// sideTable解锁
if (tryRetain) return sidetable_tryRetain() ? (id)this : nil;
else return sidetable_retain();
}
// don't check newisa.fast_rr; we already called any RR overrides
if (slowpath(tryRetain && newisa.deallocating)) { /// 小概率执行此处代码
ClearExclusive(&isa.bits);
if (!tryRetain && sideTableLocked) sidetable_unlock();
// 对于标记为已析构的对象,调用retain不做关于对象引用计数的任何操作。
return nil;
}
// 采用了isa优化,做extra_rc++,同时检查是否extra_rc溢出,若溢出,则extra_rc减半,并将另一半转存至sidetable
uintptr_t carry;
/**
addc(uintptr_t lhs, uintptr_t rhs, uintptr_t carryin, uintptr_t *carryout)
实际调用了__builtin_addcl(lhs, rhs, carryin, carryout)函数,
实现功能为:计算lhs + rhs + carryin,若64bit上溢出则在输出指针carryout中写入1,
例如:
uintptr_t result = __builtin_addcl((1ULL<<63), (1ULL<<62), ((1ULL<<62) + 2), &carry),结果为result = 2,carry = 1,结果上溢出;
__builtin_addcl((1ULL<<63), (1ULL<<62), (1ULL<<61), &carry),输出carry为0,结果不上溢出;
*/
/// # define RC_ONE (1ULL<<45) isa_t结构中的第45位到63位(从0位开始)记录引用计数的值
newisa.bits = addc(newisa.bits, RC_ONE, 0, &carry); // extra_rc++
if (slowpath(carry)) { /// 有carry值,表示extra_rc 溢出
// newisa.extra_rc++ overflowed
if (!handleOverflow) {
// 如果不处理溢出情况,则在这里会递归调用一次,再进来的时候,
// handleOverflow会被rootRetain_overflow设置为true,从而进入到下面的溢出处理流程
ClearExclusive(&isa.bits);
return rootRetain_overflow(tryRetain);
}
// Leave half of the retain counts inline and
// prepare to copy the other half to the side table.
if (!tryRetain && !sideTableLocked) sidetable_lock();
// 进行溢出处理:逻辑很简单,先在extra_rc中引用计数减半,同时把has_sidetable_rc设置为true,表明借用了sidetable。
// 然后把另一半放到sidetable中
sideTableLocked = true;
transcribeToSideTable = true;
newisa.extra_rc = RC_HALF; /// define RC_HALF (1ULL<<18) 保留一半值在extra_rc(19bit)中,其他的一半值需要转录到sidetable中
newisa.has_sidetable_rc = true;
/**
在iOS中,extra_rc占有19位,也就是最大能够表示2^19-1, 用二进制表示就是19个1。当extra_rc等于2^19时,溢出,此时的二进制位是一个1后面跟19个0, 即10000…00。将会溢出的值2^19除以2,相当于将10000…00向右移动一位。也就等于RC_HALF(1ULL<<18),即一个1后面跟18个0
*/
}
/**
StoreExclusive(uintptr_t *dst, uintptr_t oldvalue, uintptr_t value)实际调用了__sync_bool_compare_and_swap((void **)dst, (void *)oldvalue, (void *)value)
实现功能为:比较oldvalue和dst指针指向的值,若两���相等则将value写入dst指针的内容且返回true,否则不写入且返回false;
*/
/// 将oldisa 替换为 newisa,并赋值给isa.bits(更新isa_t), 如果不成功,do while再试一遍
} while (slowpath(!StoreExclusive(&isa.bits, oldisa.bits, newisa.bits)));
if (slowpath(transcribeToSideTable)) { /// 小概率
///isa的extra_rc溢出,将一半的refer count值放到sidetable中
// Copy the other half of the retain counts to the side table.
sidetable_addExtraRC_nolock(RC_HALF);
}
if (slowpath(!tryRetain && sideTableLocked)) sidetable_unlock();
return (id)this;
}
// Equivalent to calling [this release], with shortcuts if there is no override
inline void
objc_object::release()
{
assert(!isTaggedPointer());
if (fastpath(!ISA()->hasCustomRR())) { /// 大概率执行此处代码
rootRelease();
return;
}
((void(*)(objc_object *, SEL))objc_msgSend)(this, SEL_release);
}
// Base release implementation, ignoring overrides.
// Does not call -dealloc.
// Returns true if the object should now be deallocated.
// This does not check isa.fast_rr; if there is an RR override then
// it was already called and it chose to call [super release].
//
// handleUnderflow=false is the frameless fast path.
// handleUnderflow=true is the framed slow path including side table borrow
// The code is structured this way to prevent duplication.
ALWAYS_INLINE bool
objc_object::rootRelease()
{
return rootRelease(true, false);
}
ALWAYS_INLINE bool
objc_object::rootReleaseShouldDealloc()
{
return rootRelease(false, false);
}
ALWAYS_INLINE bool
objc_object::rootRelease(bool performDealloc, bool handleUnderflow)
{
if (isTaggedPointer()) return false;
bool sideTableLocked = false;
isa_t oldisa;
isa_t newisa;
retry:
do {
oldisa = LoadExclusive(&isa.bits);
newisa = oldisa;
if (slowpath(!newisa.nonpointer)) { /// 未经优化的isa。引用计数全部存放在sidetable中
ClearExclusive(&isa.bits);
if (sideTableLocked) sidetable_unlock();
return sidetable_release(performDealloc); /// sidetabel中计数操作和是否执行析构
}
// don't check newisa.fast_rr; we already called any RR overrides
/// isa和sidetable中共同存储计数
uintptr_t carry;
newisa.bits = subc(newisa.bits, RC_ONE, 0, &carry); // extra_rc--
if (slowpath(carry)) { /// isa中extra_rc计数位发生下溢。
// don't ClearExclusive()
goto underflow;
}
} while (slowpath(!StoreReleaseExclusive(&isa.bits,
oldisa.bits, newisa.bits)));
if (slowpath(sideTableLocked)) sidetable_unlock();
return false; /// extra_rc递减没溢出 返回false说明不需要销毁
underflow:
// newisa.extra_rc-- underflowed: borrow from side table or deallocate 需要向sidetable借或执行销毁操作
// abandon newisa to undo the decrement
newisa = oldisa; /// newisa.extra_rc--发生下溢,将newisa重置
if (slowpath(newisa.has_sidetable_rc)) { /// 有has_sidetable_rc辅助计数
if (!handleUnderflow) {
ClearExclusive(&isa.bits);
/// 不处理下溢时,最终会递归调用rootRelease来处理下溢。注意这时newisa已经重置了
return rootRelease_underflow(performDealloc);
}
// Transfer retain count from side table to inline storage. 向sidetable借
if (!sideTableLocked) { /// 确保操作前加锁
ClearExclusive(&isa.bits);
sidetable_lock();
sideTableLocked = true;
// Need to start over to avoid a race against
// the nonpointer -> raw pointer transition.
goto retry;
}
// Try to remove some retain counts from the side table.
size_t borrowed = sidetable_subExtraRC_nolock(RC_HALF); /// 借define RC_HALF (1ULL<<18)
// To avoid races, has_sidetable_rc must remain set
// even if the side table count is now zero.
if (borrowed > 0) {
// Side table retain count decreased.
// Try to add them to the inline count.
// 为什么是newisa.extra_rc = borrowed - 1,这是因为前面newisa = oldisa将newisa恢复到递减前的状态,因此要对其作重新减1处理。
newisa.extra_rc = borrowed - 1; // redo the original decrement too 计数减一
bool stored = StoreReleaseExclusive(&isa.bits,
oldisa.bits, newisa.bits);
if (!stored) { /// 修改失败,再尝试一次
// Inline update failed.
// Try it again right now. This prevents livelock on LL/SC
// architectures where the side table access itself may have
// dropped the reservation.
isa_t oldisa2 = LoadExclusive(&isa.bits);
isa_t newisa2 = oldisa2;
if (newisa2.nonpointer) {
uintptr_t overflow;
newisa2.bits =
addc(newisa2.bits, RC_ONE * (borrowed-1), 0, &overflow);
if (!overflow) {
stored = StoreReleaseExclusive(&isa.bits, oldisa2.bits,
newisa2.bits);
}
}
}
if (!stored) { /// 再次失败,将sidetable恢复。重新开始尝试
// Inline update failed.
// Put the retains back in the side table.
sidetable_addExtraRC_nolock(borrowed);
goto retry;
}
// Decrement successful after borrowing from side table.
// This decrement cannot be the deallocating decrement - the side
// table lock and has_sidetable_rc bit ensure that if everyone
// else tried to -release while we worked, the last one would block.
sidetable_unlock();
return false;
}
else { /// sidetable为空或计数值为0,走析构流程
// Side table is empty after all. Fall-through to the dealloc path.
}
}
// Really deallocate.
if (slowpath(newisa.deallocating)) { // 若isa.deallocating为1表示对象已释放抛出 over release 异常,
ClearExclusive(&isa.bits);
if (sideTableLocked) sidetable_unlock();
return overrelease_error();
// does not actually return
}
newisa.deallocating = true; /// 将isa.bits.deallocating置为1,
if (!StoreExclusive(&isa.bits, oldisa.bits, newisa.bits)) goto retry;
if (slowpath(sideTableLocked)) sidetable_unlock();
__sync_synchronize();
if (performDealloc) {
// 向对象发送析构消息
((void(*)(objc_object *, SEL))objc_msgSend)(this, SEL_dealloc);
}
return true;
}
// Equivalent to [this autorelease], with shortcuts if there is no override
inline id
objc_object::autorelease()
{
if (isTaggedPointer()) return (id)this;
if (fastpath(!ISA()->hasCustomRR())) return rootAutorelease();
return ((id(*)(objc_object *, SEL))objc_msgSend)(this, SEL_autorelease);
}
// Base autorelease implementation, ignoring overrides.
inline id
objc_object::rootAutorelease()
{
if (isTaggedPointer()) return (id)this;
if (prepareOptimizedReturn(ReturnAtPlus1)) return (id)this;
return rootAutorelease2();
}
inline uintptr_t
objc_object::rootRetainCount()
{
if (isTaggedPointer()) return (uintptr_t)this;
sidetable_lock();
isa_t bits = LoadExclusive(&isa.bits);
ClearExclusive(&isa.bits);
if (bits.nonpointer) { /// 优化的isa
uintptr_t rc = 1 + bits.extra_rc;
if (bits.has_sidetable_rc) {
rc += sidetable_getExtraRC_nolock();
}
sidetable_unlock();
return rc;
}
sidetable_unlock();
return sidetable_retainCount(); ///未优化的isa,引用计数全部存放在sidetable中
}
// SUPPORT_NONPOINTER_ISA
#else
// not SUPPORT_NONPOINTER_ISA
inline Class
objc_object::ISA()
{
assert(!isTaggedPointer());
return isa.cls;
}
inline bool
objc_object::hasNonpointerIsa()
{
return false;
}
inline void
objc_object::initIsa(Class cls)
{
assert(!isTaggedPointer());
isa = (uintptr_t)cls;
}
inline void
objc_object::initClassIsa(Class cls)
{
initIsa(cls);
}
inline void
objc_object::initProtocolIsa(Class cls)
{
initIsa(cls);
}
inline void
objc_object::initInstanceIsa(Class cls, bool)
{
initIsa(cls);
}
inline void
objc_object::initIsa(Class cls, bool, bool)
{
initIsa(cls);
}
inline Class
objc_object::changeIsa(Class cls)
{
// This is almost always rue but there are
// enough edge cases that we can't assert it.
// assert(cls->isFuture() ||
// cls->isInitializing() || cls->isInitialized());
assert(!isTaggedPointer());
isa_t oldisa, newisa;
newisa.cls = cls;
do {
oldisa = LoadExclusive(&isa.bits);
} while (!StoreExclusive(&isa.bits, oldisa.bits, newisa.bits));
if (oldisa.cls && oldisa.cls->instancesHaveAssociatedObjects()) {
cls->setInstancesHaveAssociatedObjects();
}
return oldisa.cls;
}
inline bool
objc_object::hasAssociatedObjects()
{
return getIsa()->instancesHaveAssociatedObjects();
}
inline void
objc_object::setHasAssociatedObjects()
{
getIsa()->setInstancesHaveAssociatedObjects();
}
inline bool
objc_object::isWeaklyReferenced()
{
assert(!isTaggedPointer());
return sidetable_isWeaklyReferenced();
}
inline void
objc_object::setWeaklyReferenced_nolock()
{
assert(!isTaggedPointer());
sidetable_setWeaklyReferenced_nolock();
}
inline bool
objc_object::hasCxxDtor()
{
assert(!isTaggedPointer());
return isa.cls->hasCxxDtor();
}
inline bool
objc_object::rootIsDeallocating()
{
if (isTaggedPointer()) return false;
return sidetable_isDeallocating();
}
inline void
objc_object::clearDeallocating()
{
sidetable_clearDeallocating();
}
inline void
objc_object::rootDealloc()
{
if (isTaggedPointer()) return;
object_dispose((id)this);
}
// Equivalent to calling [this retain], with shortcuts if there is no override
inline id
objc_object::retain()
{
assert(!isTaggedPointer());
if (fastpath(!ISA()->hasCustomRR())) {
return sidetable_retain();
}
return ((id(*)(objc_object *, SEL))objc_msgSend)(this, SEL_retain);
}
// Base retain implementation, ignoring overrides.
// This does not check isa.fast_rr; if there is an RR override then
// it was already called and it chose to call [super retain].
inline id
objc_object::rootRetain()
{
if (isTaggedPointer()) return (id)this;
return sidetable_retain();
}
// Equivalent to calling [this release], with shortcuts if there is no override
inline void
objc_object::release()
{
assert(!isTaggedPointer());
if (fastpath(!ISA()->hasCustomRR())) {
sidetable_release();
return;
}
((void(*)(objc_object *, SEL))objc_msgSend)(this, SEL_release);
}