平時開發中我們經常會用到weak,但是它的實現原理也許不是很清楚,今天就從源碼來探究一下(weak實現原理源碼鏈接)。
weak指針的建立
weak修飾對象不增加其引用計數,系統通過一個hash表來實現對象的弱引用。
在Xcode下編寫如下代碼:
__weak obj1 = obj;
編譯器編譯之后變成類似如下的代碼:
objc_initWeak(&obj1,obj);
翻開runtime源碼,NSObject.mm,找到objc_initWeak函數,實現如下:
// jack.deng weak實現原理
id
objc_initWeak(id *location, id newObj)
{
if (!newObj) {
*location = nil;
return nil;
}
return storeWeak<DontHaveOld, DoHaveNew, DoCrashIfDeallocating>
(location, (objc_object*)newObj);
}
apple對參數已有說明,location是weak指針的地址,newObj是被指向的對象的地址。接下來尋找storeWeak函數,先看apple對storeWeak函數的注釋:
// 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.
如果weak指針有指向的對象,那么清除這個對象,然后weak指針指向新的對象;如果weak指針指向新的對象,那么就將新的weak引用存起來;如果weak指針指向的對象被釋放了,那么就不會存儲這個weak引用,直接返回nil。下面一行行看代碼:
weak實現原理關鍵方法
打開NSObject.mm
查看storeWeak(id *location, objc_object *newObj)
方法實現。
// jack.deng weak實現原理關鍵方法 //[我已經配了中英文雙語解釋了]
// HaveOld: true - 變量有值
// false - 需要被及時清理,當前值可能為 nil
// HaveNew: true - 需要被分配的新值,當前值可能為 nil
// false - 不需要分配新值
// CrashIfDeallocating: true - 說明 newObj 已經釋放或者 newObj 不支持弱引用,該過程需要暫停
// false - 用 nil 替代存儲
// 大家可能看不懂template,我簡單解釋一下,這是c++的模板函數:
// 模板(Template)指C++程序設計設計語言中采用類型作為參數的程序設計,支持通用程序設計。簡單來說就是支持多種類型。
// 我們這里可以認為這個storeWeak函數有5個參數就可以了
template <HaveOld haveOld, HaveNew haveNew,
CrashIfDeallocating crashIfDeallocating>
static id
storeWeak(id *location, objc_object *newObj)
{
assert(haveOld || haveNew);
if (!haveNew) assert(newObj == nil);
// 該過程用來更新弱引用指針的指向
// 初始化 previouslyInitializedClass 指針
Class previouslyInitializedClass = nil;
id oldObj;
// 聲明兩個 SideTable
SideTable *oldTable;
SideTable *newTable;
// 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) {
// 更改指針,獲得以 oldObj 為索引所存儲的值地址
oldObj = *location;
oldTable = &SideTables()[oldObj];
} else {
oldTable = nil;
}
if (haveNew) {
// 更改新值指針,獲得以 newObj 為索引所存儲的值地址
newTable = &SideTables()[newObj];
} else {
newTable = nil;
}
// 加鎖操作,防止多線程中競爭沖突
SideTable::lockTwo<haveOld, haveNew>(oldTable, newTable);
// 避免線程沖突重處理
// location 應該與 oldObj 保持一致,如果不同,說明當前的 location 已經處理過 oldObj 可是又被其他線程所修改
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.
// 防止弱引用間死鎖
// 并且通過 +initialize 初始化構造器保證所有弱引用的 isa 非空指向
if (haveNew && newObj) {
// 獲得新對象的 isa 指針
Class cls = newObj->getIsa();
// 判斷 isa 非空且已經初始化
if (cls != previouslyInitializedClass &&
!((objc_class *)cls)->isInitialized())
{
// 解鎖
SideTable::unlockTwo<haveOld, haveNew>(oldTable, newTable);
// 對其 isa 指針進行初始化
_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.
// 如果該類已經完成執行 +initialize 方法是最理想情況
// 如果該類 +initialize 在線程中
// 例如 +initialize 正在調用 storeWeak 方法
// 需要手動對其增加保護策略,并設置 previouslyInitializedClass 指針進行標記
previouslyInitializedClass = cls;
goto retry;
}
}
// Clean up old value, if any.
// 清除舊值
if (haveOld) {
weak_unregister_no_lock(&oldTable->weak_table, oldObj, location);
}
// Assign new value, if any.
// 分配新值
if (haveNew) {
newObj = (objc_object *)
weak_register_no_lock(&newTable->weak_table, (id)newObj, location,
crashIfDeallocating);
// 如果弱引用被釋放 weak_register_no_lock 方法返回 nil
// 在引用計數表中設置若引用標記位
// weak_register_no_lock returns nil if weak store should be rejected
// Set is-weakly-referenced bit in refcount table.
if (newObj && !newObj->isTaggedPointer()) {
// 弱引用位初始化操作
// 引用計數那張散列表的weak引用對象的引用計數中標識為weak引用
newObj->setWeaklyReferenced_nolock();
}
// Do not set *location anywhere else. That would introduce a race.
// 之前不要設置 location 對象,這里需要更改指針指向
*location = (id)newObj;
}
else {
// No new value. The storage is not changed.
// 沒有新值,則無需更改
}
SideTable::unlockTwo<haveOld, haveNew>(oldTable, newTable);
return (id)newObj;
}
這里面用到了SideTable,我們看看它到底是啥
SideTable
打開NSObject.mm查看struct SideTable {
struct SideTable {
spinlock_t slock; // 防止競爭的自旋鎖
RefcountMap refcnts; // 引用計數的 hash 表
weak_table_t weak_table; // weak 引用全局 hash 表
SideTable() {
memset(&weak_table, 0, sizeof(weak_table));
}
~SideTable() {
_objc_fatal("Do not delete SideTable.");
}
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);
};
對于 slock 和 refcnts 兩個成員不用多說,第一個是為了防止競爭選擇的自旋鎖,第二個是協助對象的 isa 指針的 extra_rc 共同引用計數的變量。這里主要看 weak 全局 hash 表的結構與作用。
weak表
weak表是一個弱引用表,實現為一個weak_table_t結構體,存儲了某個對象相關的的所有的弱引用信息。其定義如下(見objc-weak.h
中的struct weak_table_t {
定義)
struct weak_table_t {
// 保存了所有指向指定對象的 weak 指針
weak_entry_t *weak_entries;
// 存儲空間
size_t num_entries;
// 參與判斷引用計數輔助量
uintptr_t mask;
// hash key 最大偏移值
uintptr_t max_hash_displacement;
};
這是一個全局弱引用hash表。使用不定類型對象的地址作為 key ,用 weak_entry_t
類型結構體對象作為 value 。其中的 weak_entries
成員,從字面意思上看,即為弱引用表入口。其實現也是這樣的。
其中weak_entry_t是存儲在弱引用表中的一個內部結構體,它負責維護和存儲指向一個對象的所有弱引用hash表。其定義如下:
#define WEAK_INLINE_COUNT 4
#define REFERRERS_OUT_OF_LINE 2
struct weak_entry_t {
DisguisedPtr<objc_object> referent;
union {
struct {
weak_referrer_t *referrers;
uintptr_t out_of_line_ness : 2;
uintptr_t num_refs : PTR_MINUS_2;
uintptr_t mask;
uintptr_t max_hash_displacement;
};
struct {
// out_of_line_ness field is low bits of inline_referrers[1]
weak_referrer_t inline_referrers[WEAK_INLINE_COUNT];
};
};
bool out_of_line() {
return (out_of_line_ness == REFERRERS_OUT_OF_LINE);
}
weak_entry_t& operator=(const weak_entry_t& other) {
memcpy(this, &other, sizeof(other));
return *this;
}
weak_entry_t(objc_object *newReferent, objc_object **newReferrer)
: referent(newReferent)
{
inline_referrers[0] = newReferrer;
for (int i = 1; i < WEAK_INLINE_COUNT; i++) {
inline_referrers[i] = nil;
}
}
};
在 weak_entry_t 的結構中,DisguisedPtr referent 是對泛型對象的指針做了一個封裝,通過這個泛型類來解決內存泄漏的問題。從注釋中寫 out_of_line 成員為最低有效位,當其為0的時候, weak_referrer_t 成員將擴展為多行靜態 hash table。其實其中的 weak_referrer_t 是二維 objc_object 的別名,通過一個二維指針地址偏移,用下標作為 hash 的 key,做成了一個弱引用散列。
那么在有效位未生效的時候,out_of_line 、 num_refs、 mask 、 max_hash_displacement 有什么作用?以下是筆者自身的猜測:
out_of_line:最低有效位,也是標志位。當標志位 0 時,增加引用表指針緯度。
num_refs:引用數值。這里記錄弱引用表中引用有效數字,因為弱引用表使用的是靜態 hash 結構,所以需要使用變量來記錄數目。
mask:計數輔助量。
max_hash_displacement:hash 元素上限閥值。
其實 out_of_line 的值通常情況下是等于零的,所以弱引用表總是一個 objc_objective 指針二維數組。一維 objc_objective 指針可構成一張弱引用散列表,通過第三緯度實現了多張散列表,并且表數量為 WEAK_INLINE_COUNT 。
總結一下 StripedMap[] : StripedMap 是一個模板類,在這個類中有一個 array 成員,用來存儲 PaddedT 對象,并且其中對于 [] 符的重載定義中,會返回這個 PaddedT 的 value 成員,這個 value 就是我們傳入的 T 泛型成員,也就是 SideTable 對象。在 array 的下標中,這里使用了 indexForPointer 方法通過位運算計算下標,實現了靜態的 Hash Table。而在 weak_table 中,其成員 weak_entry 會將傳入對象的地址加以封裝起來,并且其中也有訪問全局弱引用表的入口。
下面再說說2個主要方法
舊對象解除注冊操作 weak_unregister_no_lock
該方法主要作用是將舊對象在 weak_table 中解除 weak 指針的對應綁定。根據函數名,稱之為解除注冊操作。從源碼中,可以知道其功能就是從 weak_table 中解除 weak 指針的綁定。而其中的遍歷查詢,就是針對于 weak_entry 中的多張弱引用散列表。(見obcj-weak.mm中的weak_unregister_no_lock)
void
weak_unregister_no_lock(weak_table_t *weak_table, id referent_id,
id *referrer_id)
{
objc_object *referent = (objc_object *)referent_id;
objc_object **referrer = (objc_object **)referrer_id;
weak_entry_t *entry;
if (!referent) return;
if ((entry = weak_entry_for_referent(weak_table, referent))) {
remove_referrer(entry, referrer);
bool empty = true;
if (entry->out_of_line() && entry->num_refs != 0) {
empty = false;
}
else {
for (size_t i = 0; i < WEAK_INLINE_COUNT; i++) {
if (entry->inline_referrers[i]) {
empty = false;
break;
}
}
}
if (empty) {
weak_entry_remove(weak_table, entry);
}
}
// Do not set *referrer = nil. objc_storeWeak() requires that the
// value not change.
}
新對象添加注冊操作 weak_register_no_lock
這一步與上一步相反,通過 weak_register_no_lock 函數把心的對象進行注冊操作,完成與對應的弱引用表進行綁定操作。(見obcj-weak.mm中的weak_register_no_lock)
id
weak_register_no_lock(weak_table_t *weak_table, id referent_id,
id *referrer_id, bool crashIfDeallocating)
{
objc_object *referent = (objc_object *)referent_id;
objc_object **referrer = (objc_object **)referrer_id;
if (!referent || referent->isTaggedPointer()) return referent_id;
// ensure that the referenced object is viable
bool deallocating;
if (!referent->ISA()->hasCustomRR()) {
deallocating = referent->rootIsDeallocating();
}
else {
BOOL (*allowsWeakReference)(objc_object *, SEL) =
(BOOL(*)(objc_object *, SEL))
object_getMethodImplementation((id)referent,
SEL_allowsWeakReference);
if ((IMP)allowsWeakReference == _objc_msgForward) {
return nil;
}
deallocating =
! (*allowsWeakReference)(referent, SEL_allowsWeakReference);
}
if (deallocating) {
if (crashIfDeallocating) {
_objc_fatal("Cannot form weak reference to instance (%p) of "
"class %s. It is possible that this object was "
"over-released, or is in the process of deallocation.",
(void*)referent, object_getClassName((id)referent));
} else {
return nil;
}
}
// now remember it and where it is being stored
weak_entry_t *entry;
if ((entry = weak_entry_for_referent(weak_table, referent))) {
append_referrer(entry, referrer);
}
else {
weak_entry_t new_entry(referent, referrer);
weak_grow_maybe(weak_table);
weak_entry_insert(weak_table, &new_entry);
}
// Do not set *referrer. objc_storeWeak() requires that the
// value not change.
return referent_id;
}
初始化弱引用對象流程一覽
弱引用的初始化,從上文的分析中可以看出,主要的操作部分就在弱引用表的取鍵、查詢散列、創建弱引用表等操作,可以總結出如下的流程圖:
這個圖中省略了很多情況的判斷,但是當聲明一個 __weak 會調用上圖中的這些方法。當然, storeWeak 方法不僅僅用在 __weak 的聲明中,在 class 內部的操作中也會常常通過該方法來對 weak 對象進行操作。
weak指針的銷毀
weak指針的建立說完了,我們再來看看它的銷毀。
再來看weak是在什么時候銷毀并被置為空的。找到dealloc函數的實現,其中dealloc主要調用了objc_destructInstance函數:
// dealloc調用_objc_rootDealloc
- (void)dealloc {
_objc_rootDealloc(self);
}
// _objc_rootDealloc調用obj->rootDealloc()
void
_objc_rootDealloc(id obj)
{
assert(obj);
obj->rootDealloc();
}
inline void
objc_object::rootDealloc()
{
if (isTaggedPointer()) return; // fixme necessary?
if (fastpath(isa.nonpointer &&
!isa.weakly_referenced &&
!isa.has_assoc &&
!isa.has_cxx_dtor &&
!isa.has_sidetable_rc))
{
assert(!sidetable_present());
free(this);
}
else {
object_dispose((id)this);
}
}
id
object_dispose(id obj)
{
if (!obj) return nil;
objc_destructInstance(obj);
free(obj);
return nil;
}
void *objc_destructInstance(id obj)
{
if (obj) {
// Read all of the flags at once for performance.
bool cxx = obj->hasCxxDtor();
bool assoc = obj->hasAssociatedObjects();
// This order is important.
// 尋找一個名為 ._cxx_destruct 的函數,這一步主要是由編譯器插入代碼實現,這里銷毀了對象的實例變量,并且調用了父類的dealloc(ARC環境下)
if (cxx) object_cxxDestruct(obj);
// 調用_object_remove_assocations函數清除對象的所有關聯對象。
if (assoc) _object_remove_assocations(obj);
// 調用obj->clearDeallocating()函數清除所有的weak指針被將其置為nil。
obj->clearDeallocating();
}
return obj;
}
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());
}
void
objc_object::sidetable_clearDeallocating()
{
SideTable& table = SideTables()[this];
// clear any weak table items
// clear extra retain count and deallocating bit
// (fixme warn or abort if extra retain count == 0 ?)
table.lock();
RefcountMap::iterator it = table.refcnts.find(this);
if (it != table.refcnts.end()) {
if (it->second & SIDE_TABLE_WEAKLY_REFERENCED) {
weak_clear_no_lock(&table.weak_table, (id)this);
}
table.refcnts.erase(it);
}
table.unlock();
}
NEVER_INLINE void
objc_object::clearDeallocating_slow()
{
assert(isa.nonpointer && (isa.weakly_referenced || isa.has_sidetable_rc));
SideTable& table = SideTables()[this];
table.lock();
if (isa.weakly_referenced) {
weak_clear_no_lock(&table.weak_table, (id)this);
}
if (isa.has_sidetable_rc) {
table.refcnts.erase(this);
}
table.unlock();
}
從上面的調用順序可以看出
- dealloc調用_objc_rootDealloc調用objc_object::rootDealloc()
- objc_object::rootDealloc()調用object_dispose
- object_dispose調用objc_destructInstance
- objc_destructInstance調用obj->clearDeallocating()
- clearDeallocating里面有分支sidetable_clearDeallocating()和clearDeallocating_slow(),但是這2個函數里面其實都是調用了weak_clear_no_lock。 所以我們來看看weak_clear_no_lock
核心方法weak_clear_no_lock
void
weak_clear_no_lock(weak_table_t *weak_table, id referent_id)
{
//1、拿到被銷毀對象的指針
objc_object *referent = (objc_object *)referent_id;
//2、通過 指針 在weak_table中查找出對應的entry
weak_entry_t *entry = weak_entry_for_referent(weak_table, referent);
if (entry == nil) {
/// XXX shouldn't happen, but does with mismatched CF/objc
//printf("XXX no entry for clear deallocating %p\n", referent);
return;
}
//3、將所有的引用設置成nil
// zero out references
weak_referrer_t *referrers;
size_t count;
if (entry->out_of_line()) {
//3.1、如果弱引用超過4個則將referrers數組內的弱引用都置成nil。
referrers = entry->referrers;
count = TABLE_SIZE(entry);
}
else {
//3.2、不超過4個則將inline_referrers數組內的弱引用都置成nil
referrers = entry->inline_referrers;
count = WEAK_INLINE_COUNT;
}
//循環設置所有的引用為nil
for (size_t i = 0; i < count; ++i) {
objc_object **referrer = referrers[i];
if (referrer) {
if (*referrer == referent) {
*referrer = nil;
}
else if (*referrer) {
_objc_inform("__weak variable at %p holds %p instead of %p. "
"This is probably incorrect use of "
"objc_storeWeak() and objc_loadWeak(). "
"Break on objc_weak_error to debug.\n",
referrer, (void*)*referrer, (void*)referent);
objc_weak_error();
}
}
}
//4、從weak_table中移除entry
weak_entry_remove(weak_table, entry);
}
該函數的做了如下事情:
- 從weak表中獲取廢棄對象的地址為鍵值的記錄
- 將包含在記錄中的所有附有 weak修飾符變量的地址,賦值為nil
- 將weak表中該記錄刪除
- 從引用計數表中刪除廢棄對象的地址為鍵值的記錄
總結
嫌上面的內容太多,那就看簡化版的
runtime維護了一個weak表,用于存儲指向某個對象的所有weak指針。weak表其實是一個hash(哈希)表,key是所指對象的地址,Value是weak指針的地址(這個地址的值是所指對象指針的地址)數組。
稍微詳細版
- 初始化時:runtime會調用objc_initWeak函數,初始化一個新的weak指針指向對象的地址。
- 添加引用時:objc_initWeak函數會調用 storeWeak() 函數, storeWeak() 的作用是更新指針指向,創建對應的弱引用表。
- 釋放時,調用clearDeallocating函數。clearDeallocating函數首先根據對象地址獲取所有weak指針地址的數組,然后遍歷這個數組把其中的數據設為nil,最后把這個entry從weak表中刪除,最后清理對象的記錄。
小尾巴
大家可能看了這篇文章還是暈乎乎的,主要是對里面的數據結構不是很清楚,我又找到2篇很好的文章,看完這個,保管完全不會暈乎乎了。
iOS管理對象內存的數據結構以及操作算法--SideTables、RefcountMap、weak_table_t-一
iOS管理對象內存的數據結構以及操作算法--SideTables、RefcountMap、weak_table_t-二