isa 走位圖
在講 OC->Class
底層類結(jié)構(gòu)之前,先看下下面這張圖:
通過isa走位圖 得出的結(jié)論是:
1,類,父類,元類都包含了 isa
, superclass
2,對象isa指向類對象,類對象的isa指向了元類,元類的 isa
指向了根元類,根元類 isa
指向自己
3,類的 superclass
指向父類,父類的 superclass
指向的根類,根類的superclass
指向的nil
4,元類的 superclass
指向父元類,父元類 superclass
指向的根元類,根元類 superclass
指向根類,根類 superclass
指向nil
這下又復(fù)習(xí)了 isa
,superclass
走位;那么問題這些類,類對象,元類對象當(dāng)中的在底層展現(xiàn)的數(shù)據(jù)結(jié)構(gòu)是怎樣呢,這是我需要探索的,于是把源碼貼出來展開分析下:
struct objc_class
struct objc_class : objc_object {
// Class ISA;
Class superclass;
cache_t cache; // formerly cache pointer and vtable
class_data_bits_t bits;
class_rw_t *data() const {
return bits.data();
}
const class_ro_t *safe_ro() const {
return bits.safe_ro();
}
}
從源碼沒見 isa
屬性,其實它繼承了objc_object
,而 objc_object
中有個isa
,在運行時類圖生成中會產(chǎn)生一個isa
指向objc_object
這個類圖,而 superclass
指向它的父類;根據(jù)上面 isa
, superclass
走位圖就知道它的指向關(guān)系。
cache_t & class_data_bits_t
cache
方法緩存,這個作用將常調(diào)用的方法緩存下來;便于下次直接查找調(diào)用,提高查找效率。
它的結(jié)構(gòu):
struct cache_t {
struct bucket_t *buckets() const;//存儲方法的散列表
mask_t mask() const;//散列表緩存長度
mask_t occupied() const;//已緩存方法個數(shù)
}
struct class_data_bits_t {
class_rw_t* data() const;//類信息
}
bits
存儲具體類信息,它需要&FAST_DATA_MASK來計算得到類心所有信息,源碼如下:
FAST_DATA_MASK 掩碼值
bool has_rw_pointer() const {
#if FAST_IS_RW_POINTER
return (bool)(bits & FAST_IS_RW_POINTER);
#else
class_rw_t *maybe_rw = (class_rw_t *)(bits & FAST_DATA_MASK);
return maybe_rw && (bool)(maybe_rw->flags & RW_REALIZED);
#endif
}
通過源碼確實需要這種方式計算能得到類的存儲信息;那為什么要用這種方式去處理呢。
比如說我要得到存儲在 class_rw_t
類信息信息我只要通過 FAST_DATA_MASK
掩碼值就能得到它的地址信息,通過地址信息就能從內(nèi)存中拿到所有類的存儲信息。
那這樣我的FAST_DATA_MASK
掩碼值不一樣,我通過&
計算,得到的數(shù)據(jù)信息也就不一樣,不得不說蘋果工程師想的周到,而且這種方式不僅isa也是這樣,很多地方都用這種方式取值,大大提高訪問速度,數(shù)據(jù)提取效率。
class_rw_t ,class_ro_t,class_rw_ext_t
struct class_rw_t {
const class_ro_t *ro() const ;
const method_array_t methods() const ;//如果是類對象:放對象方法,元類:元類對象方法
const property_array_t properties() const;
const protocol_array_t protocols() const;
class_rw_ext_t *ext() const;
}
struct class_rw_ext_t {
method_array_t methods;
property_array_t properties;
protocol_array_t protocols;
uint32_t version;
}
可以看出類的信息具體就存儲在class_rw_t
,class_ro_t
,class_rw_ext_t
中,
剖析下class_rw_t
先看看method_array_t
,property_array_t
,protocol_array_t
源碼結(jié)構(gòu)
class property_array_t :
public list_array_tt<property_t, property_list_t, RawPtr>
{
typedef list_array_tt<property_t, property_list_t, RawPtr> Super;
public:
property_array_t() : Super() { }
property_array_t(property_list_t *l) : Super(l) { }
};
class protocol_array_t :
public list_array_tt<protocol_ref_t, protocol_list_t, RawPtr>
{
typedef list_array_tt<protocol_ref_t, protocol_list_t, RawPtr> Super;
public:
protocol_array_t() : Super() { }
protocol_array_t(protocol_list_t *l) : Super(l) { }
};
看完之后,他們都繼承list_array_tt
,那么 list_array_tt
是什么鬼,它數(shù)據(jù)結(jié)構(gòu)是怎樣的,這下在取找下它。源碼如下:
template <typename Element, typename List, template<typename> class Ptr>
class list_array_tt {
protected:
template <bool authenticated>
class iteratorImpl {
const Ptr<List> *lists;
const Ptr<List> *listsEnd;
}
using iterator = iteratorImpl<false>;
using signedIterator = iteratorImpl<true>;
public:
list_array_tt() : list(nullptr) { }
list_array_tt(List *l) : list(l) { }
list_array_tt(const list_array_tt &other) {
*this = other;
}
void attachLists(List* const * addedLists, uint32_t addedCount) {
if (addedCount == 0) return;
if (hasArray()) {
// many lists -> many lists
uint32_t oldCount = array()->count;
uint32_t newCount = oldCount + addedCount;
array_t *newArray =(array_t*)malloc(array_t::byteSize(newCount));
newArray->count = newCount;
array()->count = newCount;
for (int i = oldCount - 1; i >= 0; i--)
newArray->lists[i + addedCount] = array()->lists[i];
for (unsigned i = 0; i < addedCount; i++)
newArray->lists[i] = addedLists[i];
free(array());
setArray(newArray);
validate();
}
else if (!list && addedCount == 1) {
// 0 lists -> 1 list
list = addedLists[0];
validate();
}
else {
// 1 list -> many lists
Ptr<List> oldList = list;
uint32_t oldCount = oldList ? 1 : 0;
uint32_t newCount = oldCount + addedCount;
setArray((array_t *)malloc(array_t::byteSize(newCount)));
array()->count = newCount;
if (oldList) array()->lists[addedCount] = oldList;
for (unsigned i = 0; i < addedCount; i++)
array()->lists[i] = addedLists[i];
validate();
}
}
}
我把主要地方拿去出來,可以看到 attachLists
它的目的是將一個或多個列表(List
?類型)附加到某個?list_array_tt
對象中。這個對象可以包含零個、一個或多個列表,這些列表可以是單個指針,也可以是指針數(shù)組。函數(shù)的輸入?yún)?shù)是一個指向?List
?指針數(shù)組的指針?addedLists
?和一個無符號整數(shù)?addedCount
,表示要添加的列表數(shù)量。
由此我推斷它是一個數(shù)組,而且是一個二維數(shù)組存儲的,所有由此得出 class_rw_t
中methods
,properties
,protocols
這幾個屬性利用二維數(shù)組取存儲類的方法,協(xié)議等信息,而且是可讀可寫的屬性。
那它設(shè)計這種二維數(shù)組有什么好處呢?當(dāng)然有好處,它可以動態(tài)的給數(shù)組里面增加刪除方法,很方便我們分類方法的編寫完進(jìn)行存儲。
那搞清楚了 class_rw_t
幾個重要數(shù)據(jù)存儲信息,那 class_rw_t
它的作用是干什么的呢;
從class_rw_t
結(jié)構(gòu)體定義來看;它是在應(yīng)用運行時,將OC類,分類的信息直接寫入到class_rw_t
結(jié)構(gòu)的數(shù)據(jù)結(jié)構(gòu)中,在類的方法,協(xié)議進(jìn)行調(diào)用時,從里面去讀取,然后常調(diào)用的方法,又存儲在cache_t這個結(jié)構(gòu)體中,可想而知,蘋果對OC類的處理,煞費苦心。
struct class_ro_t
在 class_rw_t
結(jié)構(gòu)體中有個 class_ro_t
結(jié)構(gòu)體,在探索下這個東西做什么的,它的源碼如下:
struct class_ro_t {
WrappedPtr<method_list_t, method_list_t::Ptrauth> baseMethods;
protocol_list_t * baseProtocols;
const ivar_list_t * ivars;
property_list_t *baseProperties;
}
先說說 ivars
這個屬性修飾的結(jié)構(gòu)體源碼如下:
struct ivar_list_t : entsize_list_tt<ivar_t, ivar_list_t, 0> {
bool containsIvar(Ivar ivar) const {
return (ivar >= (Ivar)&*begin() && ivar < (Ivar)&*end());
}
};
這個貌似只有一個繼承 entsize_list_tt
,那在探索下源碼:
struct entsize_list_tt {
uint32_t entsizeAndFlags;
uint32_t count;
struct iteratorImpl {
uint32_t entsize;
uint32_t index; // keeping track of this saves a divide in operator-
using ElementPtr = std::conditional_t<authenticated, Element * __ptrauth(ptrauth_key_process_dependent_data, 1, 0xdead), Element *>;
ElementPtr element;
typedef std::random_access_iterator_tag iterator_category;
typedef Element value_type;
typedef ptrdiff_t difference_type;
typedef Element* pointer;
typedef Element& reference;
iteratorImpl() { }
iteratorImpl(const List& list, uint32_t start = 0)
: entsize(list.entsize())
, index(start)
, element(&list.getOrEnd(start))
{ }
}
}
可以看出這段代碼定義了一個結(jié)構(gòu)體?entsize_list_tt
,它內(nèi)部包含一個嵌套的結(jié)構(gòu)體?iteratorImpl
,用于實現(xiàn)一個迭代器。遍歷容器(如列表、數(shù)組等)的對象。
到此可以得出ivars
是一個 ivar_list_t
數(shù)組,它存儲了類的屬性變量信息,那protocol_list_t
結(jié)構(gòu)體內(nèi)部也是數(shù)組形式構(gòu)建的。
baseProtocols
,baseProperties
這兩個屬性對類的存儲信息只能讀取,不能寫入。
所以總結(jié)的是:從 class_ro_t
結(jié)構(gòu)體定義來看,它存儲類的變量,方法,協(xié)議信息,而且這個結(jié)構(gòu)體屬于類的只讀信息,它包含了類的初始信息。
class_rw_ext_t
這個結(jié)構(gòu)體不在過多敘述,簡單來說它是基于 class_rw_t
之后為了更好管理oc類的高級特性,比如關(guān)聯(lián)屬性等,衍生出來的一個結(jié)構(gòu)體,包括:method_array_t
,property_arrat_t
,protocol_array_t
等定義屬性類型
到這里類結(jié)構(gòu)及存儲所關(guān)聯(lián)的信息都在這里了;來一張他們關(guān)聯(lián)的結(jié)構(gòu)思維圖:
總結(jié):一開始編譯時,程序?qū)㈩惖某跏夹畔⒎旁?class_ro_t
中,當(dāng)程序運行時,將類的信息合并在一起的時候,它會將 class_ro_t
類的信息合并到 class_rw_t
結(jié)構(gòu)體中去。
struct method_t
為什么要說method_t,因為它不僅在 class_ro_t
有使用,在OC底層其他地方也有使用;比如如下源碼:
void method_exchangeImplementations(Method m1Signed, Method m2Signed)
{
if (!m1Signed || !m2Signed) return;
method_t *m1 = _method_auth(m1Signed);
method_t *m2 = _method_auth(m2Signed);
mutex_locker_t lock(runtimeLock);
IMP imp1 = m1->imp(false);
IMP imp2 = m2->imp(false);
SEL sel1 = m1->name();
SEL sel2 = m2->name();
m1->setImp(imp2);
m2->setImp(imp1);
// RR/AWZ updates are slow because class is unknown
// Cache updates are slow because class is unknown
// fixme build list of classes whose Methods are known externally?
flushCaches(nil, __func__, [sel1, sel2, imp1, imp2](Class c){
return c->cache.shouldFlush(sel1, imp1) || c->cache.shouldFlush(sel2, imp2);
});
adjustCustomFlagsForMethodChange(nil, m1);
adjustCustomFlagsForMethodChange(nil, m2);
}
static IMP
_method_setImplementation(Class cls, method_t *m, IMP imp)
{
lockdebug::assert_locked(&runtimeLock);
if (!m) return nil;
if (!imp) return nil;
IMP old = m->imp(false);
SEL sel = m->name();
m->setImp(imp);
// Cache updates are slow if cls is nil (i.e. unknown)
// RR/AWZ updates are slow if cls is nil (i.e. unknown)
// fixme build list of classes whose Methods are known externally?
flushCaches(cls, __func__, [sel, old](Class c){
return c->cache.shouldFlush(sel, old);
});
adjustCustomFlagsForMethodChange(cls, m);
return old;
}
方法交換,實現(xiàn)中底層都有用到,我們探索下,先看看 method_t
源碼:
struct method_t {
// The representation of a "big" method. This is the traditional
// representation of three pointers storing the selector, types
// and implementation.
struct big {
SEL name;
const char *types;
MethodListIMP imp;
};
// A "big" method, but name is signed. Used for method lists created at runtime.
struct bigSigned {
SEL __ptrauth_objc_sel name;
const char * ptrauth_method_list_types types;
MethodListIMP imp;
};
// ***HACK: This is a TEMPORARY HACK FOR EXCLAVEKIT. It MUST go away.
// rdar://96885136 (Disallow insecure un-signed big method lists for ExclaveKit)
#if TARGET_OS_EXCLAVEKIT
struct bigStripped {
SEL name;
const char *types;
MethodListIMP imp;
};
#endif
}
可以看到這結(jié)構(gòu)體中掐套了多個結(jié)構(gòu)體;在把它簡化下:
struct method_t {
SEL name;//方法名
const char *types;//包含函數(shù)具有參數(shù)編碼的字符串類型的返回值
MethodListIMP imp;//函數(shù)指針(指向函數(shù)地址的指針)
}
SEL
:函數(shù)名,沒特別的意義;
特點:
1,使用@selector()
,sel_registerName()
獲得
2,使用sel_getName()
,NSStringFromSelector()
轉(zhuǎn)成字符串
3,不同類中相同名字方法,對應(yīng)的方法選擇器是相同或相等的
底層代碼結(jié)構(gòu):
/// An opaque type that represents a method selector.
typedef struct objc_selector *SEL;
types
:包含了函數(shù)返回值、參數(shù)編碼的字符串
可以看到types在值:v16@0:8 ,可以看出name,types,IMP其實都在class_ro_t結(jié)構(gòu)體中,這樣確實證明了之前說的;class_ro_t結(jié)構(gòu)體在運行時存儲著類的初始狀態(tài)數(shù)據(jù)。
v16@0:8說明下:
v:方法返回類型,這里說void,
16:第一個參數(shù),
@:id類型第二個參數(shù),
0:第三個參數(shù)
: :selector類型
8:第四個參數(shù)
那這種types參數(shù)又是什么鬼東西,查下了資料這叫:Type Encoding(類型編碼)
怎么證明了,使用如下代碼:
蘋果官網(wǎng)types encoding表格:
IMP
其實就是指向函數(shù)的指針,感覺這個就沒有必要講了。
struct cache_t
cache_t
用于 class
的方法緩存,對class
常調(diào)用的方法緩存下來,提高查詢效率,這個上之前都已經(jīng)說過;接下來看看 bucket_t
。
struct bucket_t
struct bucket_t {
cache_key_t _key;//函數(shù)名
IMP _imp;//函數(shù)內(nèi)存地址
}
這種散列表的模型,其實在底層用一個數(shù)組展現(xiàn):
其實它的內(nèi)部就是一個一維數(shù)組,那可能問了,數(shù)組難道它是循環(huán)查找嗎,其實不然;在它元素超找時,它是拿到你的 函數(shù)名 & mask
,而這個 mask
就是 cache_t
結(jié)構(gòu)體中的 mask
值;計算得到函數(shù)在 散列表
存儲的索引值,在通過索引拿到函數(shù)地址,進(jìn)行執(zhí)行。
接下來看個事例:
int main(int argc, const char * argv[]) {
? ? @autoreleasepool {
? ? ? ? Student *stu=[Student new];
? ? ? ? [stu test];
? ? ? ? [stu test];
? ? ? ? [stu test];
? ? ? ? [stu test];
? ? }
? ? return 0;
}
如上方法:當(dāng)首次調(diào)用它會去類對象中查找,在方法執(zhí)行時,他會放入cache_t
緩存中,當(dāng)?shù)诙?,第三次,第四次時,它就去緩存中查找。
當(dāng)方法執(zhí)行后;我們看到 _mask
是:3,這個3代表了我類中定義了三個函數(shù);而——_occupied
是一個隨意的值;它其實代表了換存方法的個數(shù)。
那如何知道方法有緩存了,再繼續(xù)往下執(zhí)行:
這時候執(zhí)行完 test02, _mask
的值從 3
變成了 7
,說明散列表 bucket_t
做了擴(kuò)容操作。在這里bucket_t
元素需要 _mask
個元素,所以最終 bucket_t
從原有的3個元素進(jìn)行了 2倍
擴(kuò)容。
在看下方法是否進(jìn)行緩存:
可以看見當(dāng)執(zhí)行完 [stu test02] 時,數(shù)據(jù)做了擴(kuò)容,并且擴(kuò)容的數(shù)據(jù)使用(null)
進(jìn)行填充。
在看個事例:
在執(zhí)行 [stu test] 之前;其實bucket_t
就3個元素,并且存入了 init
方法;
當(dāng)執(zhí)行完 [stu test] 之后;就存入 test
方法。
但是注意的地方:它在擴(kuò)容時對之前的緩存進(jìn)行清除。
通過查看源碼,我們知道了它如何進(jìn)行清除操作,
當(dāng)執(zhí)行完 [stu test02]; ,[stu test03]; 之后,它先將緩存清空;這時候 init
, test
方法被清空,bucket_t
擴(kuò)容完在存儲:test02
,test03
方法。
那問題又來了,它是如何快速定位到方法的,然后執(zhí)行的?接下來看看代碼:
可以清楚看見,當(dāng)我使用 @selector(test03)&stu_cache._mask
就可以得到下標(biāo),然后再從 bucket_t
拿到方法。文章來源:http://www.zghlxwxcb.cn/news/detail-842952.html
到這里 class結(jié)構(gòu),類的方法緩存到此結(jié)束了,從上面也可以思考下:如果自己去實現(xiàn)散列表數(shù)組
,是不是思路就跟清晰了。文章來源地址http://www.zghlxwxcb.cn/news/detail-842952.html
謝謝大家!青山不改,綠水長流。后會有期!
到了這里,關(guān)于Objective-C之Class底層結(jié)構(gòu)探索的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!