-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNSObject.mm
More file actions
2665 lines (2221 loc) · 83.5 KB
/
NSObject.mm
File metadata and controls
2665 lines (2221 loc) · 83.5 KB
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@
*/
#include "objc-private.h"
#include "NSObject.h"
#include "objc-weak.h"
#include "llvm-DenseMap.h"
#include "NSObject.h"
#include <malloc/malloc.h>
#include <stdint.h>
#include <stdbool.h>
#include <mach/mach.h>
#include <mach-o/dyld.h>
#include <mach-o/nlist.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <libkern/OSAtomic.h>
#include <Block.h>
#include <map>
#include <execinfo.h>
@interface NSInvocation
- (SEL)selector;
@end
#if TARGET_OS_MAC
// NSObject used to be in Foundation/CoreFoundation.
#define SYMBOL_ELSEWHERE_IN_3(sym, vers, n) \
OBJC_EXPORT const char elsewhere_ ##n __asm__("$ld$hide$os" #vers "$" #sym); const char elsewhere_ ##n = 0
#define SYMBOL_ELSEWHERE_IN_2(sym, vers, n) \
SYMBOL_ELSEWHERE_IN_3(sym, vers, n)
#define SYMBOL_ELSEWHERE_IN(sym, vers) \
SYMBOL_ELSEWHERE_IN_2(sym, vers, __COUNTER__)
#if __OBJC2__
# define NSOBJECT_ELSEWHERE_IN(vers) \
SYMBOL_ELSEWHERE_IN(_OBJC_CLASS_$_NSObject, vers); \
SYMBOL_ELSEWHERE_IN(_OBJC_METACLASS_$_NSObject, vers); \
SYMBOL_ELSEWHERE_IN(_OBJC_IVAR_$_NSObject.isa, vers)
#else
# define NSOBJECT_ELSEWHERE_IN(vers) \
SYMBOL_ELSEWHERE_IN(.objc_class_name_NSObject, vers)
#endif
#if TARGET_OS_IOS
NSOBJECT_ELSEWHERE_IN(5.1);
NSOBJECT_ELSEWHERE_IN(5.0);
NSOBJECT_ELSEWHERE_IN(4.3);
NSOBJECT_ELSEWHERE_IN(4.2);
NSOBJECT_ELSEWHERE_IN(4.1);
NSOBJECT_ELSEWHERE_IN(4.0);
NSOBJECT_ELSEWHERE_IN(3.2);
NSOBJECT_ELSEWHERE_IN(3.1);
NSOBJECT_ELSEWHERE_IN(3.0);
NSOBJECT_ELSEWHERE_IN(2.2);
NSOBJECT_ELSEWHERE_IN(2.1);
NSOBJECT_ELSEWHERE_IN(2.0);
#elif TARGET_OS_OSX
NSOBJECT_ELSEWHERE_IN(10.7);
NSOBJECT_ELSEWHERE_IN(10.6);
NSOBJECT_ELSEWHERE_IN(10.5);
NSOBJECT_ELSEWHERE_IN(10.4);
NSOBJECT_ELSEWHERE_IN(10.3);
NSOBJECT_ELSEWHERE_IN(10.2);
NSOBJECT_ELSEWHERE_IN(10.1);
NSOBJECT_ELSEWHERE_IN(10.0);
#else
// NSObject has always been in libobjc on these platforms.
#endif
// TARGET_OS_MAC
#endif
/***********************************************************************
* Weak ivar support
**********************************************************************/
static id defaultBadAllocHandler(Class cls)
{
_objc_fatal("attempt to allocate object of class '%s' failed",
cls->nameForLogging());
}
static id(*badAllocHandler)(Class) = &defaultBadAllocHandler;
static id callBadAllocHandler(Class cls)
{
// fixme add re-entrancy protection in case allocation fails inside handler
return (*badAllocHandler)(cls);
}
void _objc_setBadAllocHandler(id(*newHandler)(Class))
{
badAllocHandler = newHandler;
}
namespace {
// The order of these bits is important.
#define SIDE_TABLE_WEAKLY_REFERENCED (1UL<<0)
#define SIDE_TABLE_DEALLOCATING (1UL<<1) // MSB-ward of weak bit
#define SIDE_TABLE_RC_ONE (1UL<<2) // MSB-ward of deallocating bit
#define SIDE_TABLE_RC_PINNED (1UL<<(WORD_BITS-1))
#define SIDE_TABLE_RC_SHIFT 2
#define SIDE_TABLE_FLAG_MASK (SIDE_TABLE_RC_ONE-1)
//MARK: ⚠️RefcountMap 引用计数表底层结构
/*
引用计数Map,其实就是个以objc_object为key的hash表,value值对应的就是该对象的引用计数
三个参数对应的就是:key类型、value类型、是否需要在value=0时释放掉响应的hash节点,这里写的是true
*/
// RefcountMap disguises its pointers because we
// don't want the table to act as a root for `leaks`.
typedef objc::DenseMap<DisguisedPtr<objc_object>,size_t,true> RefcountMap;
/*
DenseMap 又是一个模板类 【从其他地方搬过来的 方便在当前页面查看】
1. ZeroValuesArePurgeable默认是false,但是RefcountMap引用计数表指定其初始化为true;意思就是是否可以使用值为0(引用计数为 1)的桶
因为空桶存的初始值就是 0, 所以值为 0 的桶和空桶没什么区别.如果允许使用值为 0 的桶, 查找桶时如果没有找到对象对应的桶, 也没有找到墓碑桶,
就会优先使用值为 0 的桶.
2. Buckets 指针管理一段连续内存空间, 也就是数组,元素类型是BucketT类型,BucketT类型其实就类似swift中的元素(对象地址,引用计数);在申请空间后会进行初始化
在所有位置上都放上空桶(桶的 key 为 EmptyKey 时是空桶),之后对引用计数的操作, 都要依赖于桶;这里苹果把BucketT叫桶,
实际上Buckets数组才叫桶,苹果把数组中的元素称为桶应该是为了形象一些
3.NumEntries 记录数组中已使用的非空的桶的个数
4.NumTombstones, Tombstone 直译为墓碑, 当一个对象的引用计数为0, 要从桶中取出时, 其所处的位置会被标记为 Tombstone. NumTombstones 就是数组中的墓碑的个数;
当一个对象A引用计数为0时,被释放掉后,就会将该位置(例如下标3)的桶标记为墓碑,下次如果有新的对象B加入,哈希算法后找到的下标就是墓碑所在的下标(下标3),会将这个位置(下标3)记录下来,
然后继续哈希算法查找位置,如果查找到空桶,就说明在桶数组中之前没有对象B,那么就将墓碑所在的下标(下标3)来存储对象B,这样就可以利用到已经释放的位置了
5.NumBuckets 桶的数量, 因为数组中始终都充满桶, 所以可以理解为数组大小
大概执行流程:
1.通过哈希函数计算出对象地址的哈希值(下标),然后通过哈希值(下标)从Sidetables哈希表中找到SideTable,哈希值重复的对象的引用计数存储在同一个 SideTable 里.
2.在SideTable中获取到引用计数标后,通过对象地址查找到对应的桶,然后对引用计数进行【加】或者【减】
*/
template<typename KeyT, typename ValueT,
bool ZeroValuesArePurgeable = false,
typename KeyInfoT = DenseMapInfo<KeyT> >
class DenseMap
: public DenseMapBase<DenseMap<KeyT, ValueT, ZeroValuesArePurgeable, KeyInfoT>,
KeyT, ValueT, KeyInfoT, ZeroValuesArePurgeable> {
// Lift some types from the dependent base class into this class for
// simplicity of referring to them.
typedef DenseMapBase<DenseMap, KeyT, ValueT, KeyInfoT, ZeroValuesArePurgeable> BaseT;
typedef typename BaseT::BucketT BucketT;
friend class DenseMapBase<DenseMap, KeyT, ValueT, KeyInfoT, ZeroValuesArePurgeable>;
BucketT *Buckets;
unsigned NumEntries;
unsigned NumTombstones;
unsigned NumBuckets;
......
template<typename InputIt>
DenseMap(const InputIt &I, const InputIt &E) {
init(NextPowerOf2(std::distance(I, E)));
this->insert(I, E);
}
// 这是对应 64 位的提供数组大小的方法, 需要为桶数组开辟空间时, 会由这个方法来决定数组大小
//⚠️简单理解就是桶数组的大小会是 2^n.为什么数组的大小是这个规律,是为了哈希出来的哈希值通过映射后,能够均匀的分布到数组中
inline uint64_t NextPowerOf2(uint64_t A) {
A |= (A >> 1);
A |= (A >> 2);
A |= (A >> 4);
A |= (A >> 8);
A |= (A >> 16);
A |= (A >> 32);
return A + 1;
}
}
// Template parameters.
enum HaveOld { DontHaveOld = false, DoHaveOld = true };
enum HaveNew { DontHaveNew = false, DoHaveNew = true };
/// WGRunTimeSourceCode 源码阅读
//⚠️MARK: SideTable底层结构
struct SideTable {
/*
自旋锁:忙等状态、比较消耗CPU资源、不能递归调用、如果短时间内可以获取到资源,则使用自旋锁比互斥锁效率要高,因为少了互斥锁中的线程调度等操作
自旋锁比较适用于锁使用者保持锁时间比较短的情况。正是由于自旋锁使用者一般保持锁时间非常短,因此选择自旋而不是睡眠是非常必要的,自旋锁的效率远高于互斥锁。
*/
spinlock_t slock; //自旋锁:用于上锁/解锁SideTable
RefcountMap refcnts; //OC对象引用计数Map(key为对象,value为引用计数)-引用计数表
weak_table_t weak_table; //OC对象弱引用Map-弱引用表
SideTable() { //构造函数
memset(&weak_table, 0, sizeof(weak_table));
}
~SideTable() { //析构函数
_objc_fatal("Do not delete SideTable.");
}
//锁操作 符合StripedMap对T的定义
void lock() { slock.lock(); }
void unlock() { slock.unlock(); }
void forceReset() { slock.forceReset(); }
// Address-ordered lock discipline for a pair of side tables.
template<HaveOld, HaveNew>
static void lockTwo(SideTable *lock1, SideTable *lock2);
template<HaveOld, HaveNew>
static void unlockTwo(SideTable *lock1, SideTable *lock2);
};
template<>
void SideTable::lockTwo<DoHaveOld, DoHaveNew>
(SideTable *lock1, SideTable *lock2)
{
spinlock_t::lockTwo(&lock1->slock, &lock2->slock);
}
template<>
void SideTable::lockTwo<DoHaveOld, DontHaveNew>
(SideTable *lock1, SideTable *)
{
lock1->lock();
}
template<>
void SideTable::lockTwo<DontHaveOld, DoHaveNew>
(SideTable *, SideTable *lock2)
{
lock2->lock();
}
template<>
void SideTable::unlockTwo<DoHaveOld, DoHaveNew>
(SideTable *lock1, SideTable *lock2)
{
spinlock_t::unlockTwo(&lock1->slock, &lock2->slock);
}
template<>
void SideTable::unlockTwo<DoHaveOld, DontHaveNew>
(SideTable *lock1, SideTable *)
{
lock1->unlock();
}
template<>
void SideTable::unlockTwo<DontHaveOld, DoHaveNew>
(SideTable *, SideTable *lock2)
{
lock2->unlock();
}
// We cannot use a C++ static initializer to initialize SideTables because
// libc calls us before our C++ initializers run. We also don't want a global
// pointer to this struct because of the extra indirection.
// Do it the hard way.
alignas(StripedMap<SideTable>) static uint8_t
SideTableBuf[sizeof(StripedMap<SideTable>)];
static void SideTableInit() {
new (SideTableBuf) StripedMap<SideTable>();
}
/// WGRunTimeSourceCode 源码阅读
//MARK:⚠️SideTables底层结构
/*
1. SideTables与iOS内存管理息息相关,其实可以理解成一个全局的hash数组
2. SideTables实际的类型是存储SideTable的StripedMap,StripedMap里面定义了可以存储SideTable的最大数量StripeCount,最大数量是64个
3. SideTables可以理解为全局的hash数组,里面存放的是SideTable元素,例如:数组[SideTable]
4. SideTables的hash键值是一个对象obj的地址,对应的Value值就是SideTable,即一个对象对应一个SideTable;但是SideTable个数最多只有64个,
所以一个SideTable中可以存放多个对象obj,即多个对象obj可以共用同一个SideTable
*/
static StripedMap<SideTable>& SideTables() {
return *reinterpret_cast<StripedMap<SideTable>*>(SideTableBuf);
}
// anonymous namespace
};
void SideTableLockAll() {
SideTables().lockAll();
}
void SideTableUnlockAll() {
SideTables().unlockAll();
}
void SideTableForceResetAll() {
SideTables().forceResetAll();
}
void SideTableDefineLockOrder() {
SideTables().defineLockOrder();
}
void SideTableLocksPrecedeLock(const void *newlock) {
SideTables().precedeLock(newlock);
}
void SideTableLocksSucceedLock(const void *oldlock) {
SideTables().succeedLock(oldlock);
}
void SideTableLocksPrecedeLocks(StripedMap<spinlock_t>& newlocks) {
int i = 0;
const void *newlock;
while ((newlock = newlocks.getLock(i++))) {
SideTables().precedeLock(newlock);
}
}
void SideTableLocksSucceedLocks(StripedMap<spinlock_t>& oldlocks) {
int i = 0;
const void *oldlock;
while ((oldlock = oldlocks.getLock(i++))) {
SideTables().succeedLock(oldlock);
}
}
//
// The -fobjc-arc flag causes the compiler to issue calls to objc_{retain/release/autorelease/retain_block}
//
id objc_retainBlock(id x) {
return (id)_Block_copy(x);
}
//
// The following SHOULD be called by the compiler directly, but the request hasn't been made yet :-)
//
BOOL objc_should_deallocate(id object) {
return YES;
}
id
objc_retain_autorelease(id obj)
{
return objc_autorelease(objc_retain(obj));
}
void
objc_storeStrong(id *location, id obj)
{
id prev = *location;
if (obj == prev) {
return;
}
objc_retain(obj);
*location = obj;
objc_release(prev);
}
// Update a weak variable.
// If HaveOld is true, the variable has an existing value
// that needs to be cleaned up. This value might be nil.
// If HaveNew is true, there is a new value that needs to be
// assigned into the variable. This value might be nil.
// If CrashIfDeallocating is true, the process is halted if newObj is
// deallocating or newObj's class does not support weak references.
// If CrashIfDeallocating is false, nil is stored instead.
enum CrashIfDeallocating {
DontCrashIfDeallocating = false, DoCrashIfDeallocating = true
};
template <HaveOld haveOld, HaveNew haveNew,
CrashIfDeallocating crashIfDeallocating>
//MARK: StoreWeak源码->存储weak指针
/*
⚠️如果weak指向的对象还没有初始化 ,先对其进行初始化,再把它存储到弱引用表中
storeWeak<DontHaveOld, DoHaveNew, DoCrashIfDeallocating>
(location, (objc_object*)newObj);
为什么搞两个sidetable(oldTable和newTable),主要是因为 weak 修饰的变量如果之前已经指向一个对象,然后其再次改变指向另一个对象,那么按理来说我们需要释放旧对象中该 weak 变量的记录,也就是要将旧记录删除,然后在新记录中添加。这里的新旧散列表就是这个作用。
1. 根据新旧变量的地址获取相应的 SideTable
2. 对两个表进行加锁操作,防止多线程竞争冲突
3. 进行线程冲突重处理判断
4. 判断其 isa 是否为空,为空则需要进行初始化
5. 如果存在旧值,调用 weak_unregister_no_lock 函数清除旧值
6. 调用 weak_register_no_lock 函数分配新值
7. 解锁两个表,并返回对象
*/
//MARK:⚠️weak指针存取第2⃣️步 更新指针指向,创建对应的弱引用表
static id
storeWeak(id *location, objc_object *newObj) { //location 是 weak 指针,newObj 是 weak 指针将要指向的对象
//模版函数 haveOld、haveNew由编译器传入参数,这里传递的是haveOld=false,haveNew=true
assert(haveOld || haveNew);
if (!haveNew) assert(newObj == nil);
Class previouslyInitializedClass = nil;
id oldObj;
SideTable *oldTable; //⚠️存储旧对象的weak相关的信息
SideTable *newTable; //⚠️存储新对象的weak相关的信息
// Acquire locks for old and new values.
// Order by lock address to prevent lock ordering problems.
// Retry if the old value changes underneath us.
retry:
if (haveOld) { //如果之前weak弱指针(即id *location)有指向,现在要改指向新的对象,那么需要先将旧的指向给删除了,才能重新指向新的对象
oldObj = *location; //⚠️通过弱引用指针获取旧对象
oldTable = &SideTables()[oldObj]; //⚠️通过旧对象获取到对应的SideTable
} else {
oldTable = nil;
}
if (haveNew) {
newTable = &SideTables()[newObj]; //⚠️获取新对象的SideTable
} else {
newTable = nil;
}
SideTable::lockTwo<haveOld, haveNew>(oldTable, newTable);
//进行线程冲突重处理判断
if (haveOld && *location != oldObj) { //如果有旧值,并且现在新的弱引用指向不指向旧对象
SideTable::unlockTwo<haveOld, haveNew>(oldTable, newTable);
goto retry;
}
// Prevent a deadlock between the weak reference machinery
// and the +initialize machinery by ensuring that no
// weakly-referenced object has an un-+initialized isa.
if (haveNew && newObj) { //⚠️有新值,并且新值不为nil,
Class cls = newObj->getIsa(); //⚠️获取新对象的isa指针
if (cls != previouslyInitializedClass &&
!((objc_class *)cls)->isInitialized()) {
SideTable::unlockTwo<haveOld, haveNew>(oldTable, newTable);
//如果cls没有初始化,先对其进行初始化在尝试设置weak
_class_initialize(_class_getNonMetaClass(cls, (id)newObj));
// If this class is finished with +initialize then we're good.
// If this class is still running +initialize on this thread
// (i.e. +initialize called storeWeak on an instance of itself)
// then we may proceed but it will appear initializing and
// not yet initialized to the check above.
// Instead set previouslyInitializedClass to recognize it on retry.
previouslyInitializedClass = cls;
goto retry;
}
}
// Clean up old value, if any.
//⚠️ 清除旧对象weak_table中的location
if (haveOld) { //旧对象所在的弱引用表、旧对象、弱引用指针
//如果 weak 指针有旧值, 则需要在 weak_table 中处理掉旧值
weak_unregister_no_lock(&oldTable->weak_table, oldObj, location);
}
// Assign new value, if any.
//⚠️ 保存location到新对象的weak_table种
if (haveNew) {
//如果 weak 指针将要指向新值(即非 location = nil 的情况), 在 weak_table 中处理赋值操作
newObj = (objc_object *)
weak_register_no_lock(&newTable->weak_table, (id)newObj, location,
crashIfDeallocating);
// weak_register_no_lock returns nil if weak store should be rejected
// Set is-weakly-referenced bit in refcount table.
if (newObj && !newObj->isTaggedPointer()) {
newObj->setWeaklyReferenced_nolock();
}
// Do not set *location anywhere else. That would introduce a race.
//⚠️设置location指针指向newObj
*location = (id)newObj;
}
else { //⚠️没有新值,则无需更改
// No new value. The storage is not changed.
}
SideTable::unlockTwo<haveOld, haveNew>(oldTable, newTable);
return (id)newObj;
}
/**
* This function stores a new value into a __weak variable. It would
* be used anywhere a __weak variable is the target of an assignment.
*
* @param location The address of the weak pointer itself
* @param newObj The new object this weak ptr should now point to
*
* @return \e newObj
*/
//MARK: weak变量的存储
id
objc_storeWeak(id *location, id newObj)
{
return storeWeak<DoHaveOld, DoHaveNew, DoCrashIfDeallocating>
(location, (objc_object *)newObj);
}
/**
* This function stores a new value into a __weak variable.
* If the new object is deallocating or the new object's class
* does not support weak references, stores nil instead.
*
* @param location The address of the weak pointer itself
* @param newObj The new object this weak ptr should now point to
*
* @return The value stored (either the new object or nil)
*/
id
objc_storeWeakOrNil(id *location, id newObj)
{
return storeWeak<DoHaveOld, DoHaveNew, DontCrashIfDeallocating>
(location, (objc_object *)newObj);
}
/**
* Initialize a fresh weak pointer to some object location.
* It would be used for code like:
*
* (The nil case)
* __weak id weakPtr;
* (The non-nil case)
* NSObject *o = ...;
* __weak id weakPtr = o;
*
* This function IS NOT thread-safe with respect to concurrent
* modifications to the weak variable. (Concurrent weak clear is safe.)
* 这个函数不是线程安全的
* @param location Address of __weak ptr.
* @param newObj Object ptr.
*/
//MARK: weak实现原理总结
/*
⚠️Runtime维护了一个全局的SideTables表,SideTables就是个哈希表,其实就是个数组,里面存放了SideTable结构,SideTables数组中元素的最大数量是64个,通过对象可以找到对应SideTable,进而可以找到对应的弱引用表、对应的引用计数表
⚠️SideTable表中存储了一个自旋锁、一个引用计数表RefcountMap、一个弱引用表weak_table_t
⚠️weak_table_t weak_table;弱引用表其实就是个哈希表,key是对象的地址,value是weak指针的地址(这个地址的值是所指向对象的地址)数组
⚠️weak弱引用指针的原理主要分为weak指针的存储和销毁
存储阶段:
1.在初始化阶段:Runtime会调用objc_initWeak方法,这个方法首先判断传入的对象是否为nil,若为nil,
则直接将弱引用的指针设置为nil,并直接返回nil;若不为nil,则会初始化一个新的weak指针指向对象的地址。
2.添加引用阶段:objc_initWeak方法会调用storeWeak方法,在storeWeak方法中,更新指针指向,创建对应的弱引用表,将弱引用指针添加到弱引用表中
3.释放时调用clearDeallocating函数。clearDeallocating函数首先根据对象地址获取所有weak指针地址的数组,然后遍历这个数组把其中的数据设为nil,最后把这个entry从weak表中删除,清理对象的记录
spinlock_t slock; //自旋锁:用于上锁/解锁SideTable
RefcountMap refcnts; //OC对象引用计数Map(key为对象,value为引用计数)
weak_table_t weak_table; //OC对象弱引用Map()
NSObject *obj = [[NSObject alloc] init];
weak的三种赋值情况
1.变量赋值
_weakObj = obj; 编译为:objc_storeWeak(&_weakObj, obj);
2.直接初始化,strong对象赋值
__weak NSObject *obj1 = obj; 编译为:objc_initWeak(&obj1, obj);
3.直接初始化,weak对象赋值
__weak NSObject *obj2 = _weakObj; 编译为:objc_copyWeak(&obj2, & _weakObj);
__weak NSObject *obj1 = obj;
第一个参数id *location: weak指针地址(obj1)
第二个参数id newObj:weak指针指向的对象(obj)
这个函数是weak弱引用的底层入口
*/
//MARK:⚠️weak指针存取第1⃣️步
id
objc_initWeak(id *location, id newObj) {
if (!newObj) { //如果newObj对象为nil,那么指向它的weak指针也要置为nil
*location = nil;
return nil;
}
//DontHaveOld: 没有旧对象 DoHaveNew: 有新对象 DoCrashIfDeallocating: 如果释放了就Crash提示
return storeWeak<DontHaveOld, DoHaveNew, DoCrashIfDeallocating>
(location, (objc_object*)newObj);
}
id
objc_initWeakOrNil(id *location, id newObj) {
if (!newObj) {
*location = nil;
return nil;
}
return storeWeak<DontHaveOld, DoHaveNew, DontCrashIfDeallocating>
(location, (objc_object*)newObj);
}
/**
* Destroys the relationship between a weak pointer
* and the object it is referencing in the internal weak
* table. If the weak pointer is not referencing anything,
* there is no need to edit the weak table.
*
* This function IS NOT thread-safe with respect to concurrent
* modifications to the weak variable. (Concurrent weak clear is safe.)
*
* @param location The weak pointer address.
*/
//MARK: weak变量的释放
void
objc_destroyWeak(id *location)
{
(void)storeWeak<DoHaveOld, DontHaveNew, DontCrashIfDeallocating>
(location, nil);
}
/*
Once upon a time we eagerly cleared *location if we saw the object
was deallocating. This confuses code like NSPointerFunctions which
tries to pre-flight the raw storage and assumes if the storage is
zero then the weak system is done interfering. That is false: the
weak system is still going to check and clear the storage later.
This can cause objc_weak_error complaints and crashes.
So we now don't touch the storage until deallocation completes.
*/
id
objc_loadWeakRetained(id *location)
{
id obj;
id result;
Class cls;
SideTable *table;
retry:
// fixme std::atomic this load
obj = *location;
if (!obj) return nil;
if (obj->isTaggedPointer()) return obj;
table = &SideTables()[obj];
table->lock();
if (*location != obj) {
table->unlock();
goto retry;
}
result = obj;
cls = obj->ISA();
if (! cls->hasCustomRR()) {
// Fast case. We know +initialize is complete because
// default-RR can never be set before then.
assert(cls->isInitialized());
if (! obj->rootTryRetain()) {
result = nil;
}
}
else {
// Slow case. We must check for +initialize and call it outside
// the lock if necessary in order to avoid deadlocks.
if (cls->isInitialized() || _thisThreadIsInitializingClass(cls)) {
BOOL (*tryRetain)(id, SEL) = (BOOL(*)(id, SEL))
class_getMethodImplementation(cls, SEL_retainWeakReference);
if ((IMP)tryRetain == _objc_msgForward) {
result = nil;
}
else if (! (*tryRetain)(obj, SEL_retainWeakReference)) {
result = nil;
}
}
else {
table->unlock();
_class_initialize(cls);
goto retry;
}
}
table->unlock();
return result;
}
/**
* This loads the object referenced by a weak pointer and returns it, after
* retaining and autoreleasing the object to ensure that it stays alive
* long enough for the caller to use it. This function would be used
* anywhere a __weak variable is used in an expression.
*
* @param location The weak pointer address
*
* @return The object pointed to by \e location, or \c nil if \e location is \c nil.
*/
id
objc_loadWeak(id *location)
{
if (!*location) return nil;
return objc_autorelease(objc_loadWeakRetained(location));
}
/**
* This function copies a weak pointer from one location to another,
* when the destination doesn't already contain a weak pointer. It
* would be used for code like:
*
* __weak id src = ...;
* __weak id dst = src;
*
* This function IS NOT thread-safe with respect to concurrent
* modifications to the destination variable. (Concurrent weak clear is safe.)
*
* @param dst The destination variable.
* @param src The source variable.
*/
void
objc_copyWeak(id *dst, id *src)
{
id obj = objc_loadWeakRetained(src);
objc_initWeak(dst, obj);
objc_release(obj);
}
/**
* Move a weak pointer from one location to another.
* Before the move, the destination must be uninitialized.
* After the move, the source is nil.
*
* This function IS NOT thread-safe with respect to concurrent
* modifications to either weak variable. (Concurrent weak clear is safe.)
*
*/
void
objc_moveWeak(id *dst, id *src)
{
objc_copyWeak(dst, src);
objc_destroyWeak(src);
*src = nil;
}
/***********************************************************************
Autorelease pool implementation
A thread's autorelease pool is a stack of pointers.
Each pointer is either an object to release, or POOL_BOUNDARY which is
an autorelease pool boundary.
A pool token is a pointer to the POOL_BOUNDARY for that pool. When
the pool is popped, every object hotter than the sentinel is released.
The stack is divided into a doubly-linked list of pages. Pages are added
and deleted as necessary.
Thread-local storage points to the hot page, where newly autoreleased
objects are stored.
**********************************************************************/
// Set this to 1 to mprotect() autorelease pool contents
#define PROTECT_AUTORELEASEPOOL 0
// Set this to 1 to validate the entire autorelease pool header all the time
// (i.e. use check() instead of fastcheck() everywhere)
#define CHECK_AUTORELEASEPOOL (DEBUG)
BREAKPOINT_FUNCTION(void objc_autoreleaseNoPool(id obj));
BREAKPOINT_FUNCTION(void objc_autoreleasePoolInvalid(const void *token));
namespace {
struct magic_t {
static const uint32_t M0 = 0xA1A1A1A1;
# define M1 "AUTORELEASE!"
static const size_t M1_len = 12;
uint32_t m[4];
magic_t() {
assert(M1_len == strlen(M1));
assert(M1_len == 3 * sizeof(m[1]));
m[0] = M0;
strncpy((char *)&m[1], M1, M1_len);
}
~magic_t() {
m[0] = m[1] = m[2] = m[3] = 0;
}
bool check() const {
return (m[0] == M0 && 0 == strncmp((char *)&m[1], M1, M1_len));
}
bool fastcheck() const {
#if CHECK_AUTORELEASEPOOL
return check();
#else
return (m[0] == M0);
#endif
}
# undef M1
};
/*
1. 每个AutoreleasePoolPage占用4096个字节,除了用来存放它内部的成员变量外,剩下的空间用来存放autorelease对象的地址
AutoreleasePoolPage内部成员变量有7个,成员变量占用56个字节,剩下的4040个字节用来存放autorelease对象,第一页最多存放【504个对象】,
从第二页开始最多存储【505个对象】
2. 所有的AutoreleasePoolPage对象都是通过双向链表的形式连接在一起
3. 一个AutoreleasePoolPage的空间被占满时,会新建一个新的AutoreleasePoolPage对象,连接链表,后来的autorelease对象加入到新的page
4. 调用push方法时,会先将一个POOL_BOUNDARY(哨兵对象/边界对象)入栈POOL_BOUNDARY,值为nil,作为边界,然后返回这个边界对象POOL_BOUNDARY
的内存地址;push就是压栈操作,先加入边界对象,然后再添加autorelease对象
5. 调用pop方法时传入一个POOL_BOUNDARY的内存地址,会从最后一个入栈的对象开始发送release消息,直到遇到这个POOL_BOUNDARY
6. autorelease对象在什么时候释放?
程序运行启动时,RunLoop会注册两个Observer来管理和维护AutoreleasePool,
6.1一个Observer用来检测进入RunLoop的状态(kCFRunLoopEntry),此时会调用objc_autoreleasePoolPush方法向当前的AutoreleasePoolPage
增加一个POOL_BOUNDARY标志创建自动释放池。
6.2一个Observer用来检测RunLoop即将进入休眠状态(kCFRunLoopBeforeWaiting)和退出状态(kCFRunLoopExit),
在检测到RunLoop即将进入休眠状态时,会调用objc_autoreleasePoolPop() 和objc_autoreleasePoolPush() 方法.
系统会根据情况从最新加入的对象一直往前清理直到遇到POOL_BOUNDARY标志
在检测到RunLoop退出状态时,会调用objc_autoreleasePoolPop() 方法释放自动释放池内对象
⚠️所以autorelease的释放时机取决于RunLoop的运行状态,在RunLoop即将进入休眠和退出状态时,会释放AutoreleasePool自动释放池中所有的autorelease对象
9. 每一页page里都存储了next指针,指向下次新添加的autoreleased对象的位置
10. 每一页都有一个深度标记,第一页深度值为0,后面的页面递增1
11. 自动释放池存储在栈区,page内部地址从低到高依次存储:AutoreleasePoolPage自身的成员、哨兵、自动释放的对象。
12. 哨兵作为对象指针的边界,在释放池里只会有一个,如果autoreleasepool嵌套,那么可能会有多个哨兵对象
13. 调用pop方法时传入一个POOL_BOUNDARY的内存地址,会从最后一个入栈的对象开始发送release消息,直到遇到这个POOL_BOUNDARY
高地址
| 自动释放的对象
| 哨兵
低地址 成员变量
*/
//MARK: AutoreleasePoolPage底层结构
class AutoreleasePoolPage
{
// EMPTY_POOL_PLACEHOLDER is stored in TLS when exactly one pool is
// pushed and it has never contained any objects. This saves memory
// when the top level (i.e. libdispatch) pushes and pops pools but
// never uses them.
# define EMPTY_POOL_PLACEHOLDER ((id*)1)
# define POOL_BOUNDARY nil //哨兵对象
static pthread_key_t const key = AUTORELEASE_POOL_KEY;
static uint8_t const SCRIBBLE = 0xA3; // 0xA3A3A3A3 after releasing
//每一个page的大小
static size_t const SIZE =
#if PROTECT_AUTORELEASEPOOL
PAGE_MAX_SIZE; // must be multiple of vm page size
#else
PAGE_MAX_SIZE; // size and alignment, power of 2
#endif
static size_t const COUNT = SIZE / sizeof(id);
// AutoreleasePoolPage对象内部有7个成员变量,每个占用8个字节
magic_t const magic; //用来校验 AutoreleasePoolPage 的结构是否完整 16字节
id *next; //指向了下一个能存放autorelease对象地址的区域,初始化时指向 begin() 8字节
pthread_t const thread; //指向当前线程,AutoreleasePool是和线程一一对应的, 8字节
AutoreleasePoolPage * const parent; //父节点,指向上一个AutoreleasePoolPage对象,第一个结点的parent值为nil 8字节
AutoreleasePoolPage *child; //子节点,指向下一个AutoreleasePoolPage对象,最后一个结点的 child 值为 nil 8字节
uint32_t const depth; //代表深度,从 0 开始,往后递增 1 4字节
uint32_t hiwat; //4字节
// SIZE-sizeof(*this) bytes of contents follow
static void * operator new(size_t size) {
return malloc_zone_memalign(malloc_default_zone(), SIZE, SIZE);
}
static void operator delete(void * p) {
return free(p);
}
inline void protect() {
#if PROTECT_AUTORELEASEPOOL
mprotect(this, SIZE, PROT_READ);
check();
#endif
}
inline void unprotect() {
#if PROTECT_AUTORELEASEPOOL
check();
mprotect(this, SIZE, PROT_READ | PROT_WRITE);
#endif
}
// 构造函数
AutoreleasePoolPage(AutoreleasePoolPage *newParent)
: magic(),
next(begin()), //开始存储的位置
thread(pthread_self()), //传的是当前线程,
parent(newParent), child(nil),
depth(parent ? 1+parent->depth : 0), //如果是第一页深度为0,往后是前一个的深度+1
hiwat(parent ? parent->hiwat : 0)
{
if (parent) {
parent->check();
assert(!parent->child);
parent->unprotect();
parent->child = this;
parent->protect();
}
protect();
}
//析构函数
~AutoreleasePoolPage()
{
check();
unprotect();
assert(empty());
// Not recursive: we don't want to blow out the stack
// if a thread accumulates a stupendous amount of garbage
assert(!child);
}
void busted(bool die = true)
{
magic_t right;
(die ? _objc_fatal : _objc_inform)
("autorelease pool page %p corrupted\n"
" magic 0x%08x 0x%08x 0x%08x 0x%08x\n"
" should be 0x%08x 0x%08x 0x%08x 0x%08x\n"
" pthread %p\n"
" should be %p\n",
this,
magic.m[0], magic.m[1], magic.m[2], magic.m[3],
right.m[0], right.m[1], right.m[2], right.m[3],
this->thread, pthread_self());
}
void check(bool die = true)
{
if (!magic.check() || !pthread_equal(thread, pthread_self())) {
busted(die);
}
}
void fastcheck(bool die = true)
{
#if CHECK_AUTORELEASEPOOL
check(die);
#else
if (! magic.fastcheck()) {
busted(die);
}
#endif
}
//页的开始位置
id * begin() {
return (id *) ((uint8_t *)this+sizeof(*this));
}
//页的结束位置
id * end() {
return (id *) ((uint8_t *)this+SIZE);
}
//⚠️当 next == begin() 时,表示 AutoreleasePoolPage 为空
bool empty() {
return next == begin();
}
//⚠️当 next == end() 时,表示 AutoreleasePoolPage 已满
bool full() {
return next == end();
}
//页的存储是否少于空间的一半
bool lessThanHalfFull() {
return (next - begin() < (end() - begin()) / 2);
}
// 释放所有对象 知道开始位置(哨兵对象所在的位置)
void releaseAll() {
releaseUntil(begin());
}
//释放到stop位置之前的所有对象
void releaseUntil(id *stop) {
// Not recursive: we don't want to blow out the stack
// if a thread accumulates a stupendous amount of garbage
//判断下一个对象是否等于stop,如果不等于,则进入while循环
while (this->next != stop) {
// Restart from hotPage() every time, in case -release
// autoreleased more objects
//获取当前操作页面,即hot页面
AutoreleasePoolPage *page = hotPage();