消息流程2. lookUpImpOrForward
2021-05-25 本文已影响0人
东旭39
objc_msgSend
是使用汇编编写的,首先 在缓存中查找方法的imp,如果没有查找到方法,则会调用lookUpImpOrForward
开启慢速查找。
lookUpImpOrForward
IMP lookUpImpOrForward(id inst, SEL sel, Class cls, int behavior)
{
//1._objc_msgForward_impcache 复制一个默认的imp
const IMP forward_imp = (IMP)_objc_msgForward_impcache;
IMP imp = nil;
Class curClass;
runtimeLock.assertUnlocked();
if (slowpath(!cls->isInitialized())) {
behavior |= LOOKUP_NOCACHE;
}
runtimeLock.lock();
checkIsKnownClass(cls);
//如果这个类没有实现则实现,OC中实现了load的方法的类会在启动时优先实现,启动时未实现的类会在这里实现
cls = realizeAndInitializeIfNeeded_locked(inst, cls, behavior & LOOKUP_INITIALIZE);
//2. curClass赋值
curClass = cls;
//3. 循环遍历从当前类再到父类缓存查找
for (unsigned attempts = unreasonableClassCount();;) {
if (curClass->cache.isConstantOptimizedCache(/* strict */true)) {
#if CONFIG_USE_PREOPT_CACHES
imp = cache_getImp(curClass, sel);
if (imp) goto done_unlock;
curClass = curClass->cache.preoptFallbackClass();
#endif
} else {
// curClass method list.
//3.1二分法从methodlist查找imp
Method meth = getMethodNoSuper_nolock(curClass, sel);
//3.2 有就到done
if (meth) {
imp = meth->imp(false);
goto done;
}
//3.3 当前类中没有,curClass设置其父类,如果父类为nil,imp设置forward_imp,跳出循环
if (slowpath((curClass = curClass->getSuperclass()) == nil)) {
// No implementation found, and method resolver didn't help.
// Use forwarding.
imp = forward_imp;
break;
}
}
// Halt if there is a cycle in the superclass chain.
if (slowpath(--attempts == 0)) {
_objc_fatal("Memory corruption in class list.");
}
// Superclass cache.
//3.4 父类的缓存查找
imp = cache_getImp(curClass, sel);
//如果imp等于forward_imp,表示父类中也没有
if (slowpath(imp == forward_imp)) {
// Found a forward:: entry in a superclass.
// Stop searching, but don't cache yet; call method
// resolver for this class first.
break;
}
if (fastpath(imp)) {
// Found the method in a superclass. Cache it in this class.
goto done;
}
}
// No implementation found. Try method resolver once.
//4.如果没有执行,去resolveMethod_locked去查找
if (slowpath(behavior & LOOKUP_RESOLVER)) {
behavior ^= LOOKUP_RESOLVER;
return resolveMethod_locked(inst, sel, cls, behavior);
}
done:
if (fastpath((behavior & LOOKUP_NOCACHE) == 0)) {
#if CONFIG_USE_PREOPT_CACHES
while (cls->cache.isConstantOptimizedCache(/* strict */true)) {
cls = cls->cache.preoptFallbackClass();
}
#endif
//5.将方法插入缓存
log_and_fill_cache(cls, imp, sel, inst, curClass);
}
done_unlock:
runtimeLock.unlock();
if (slowpath((behavior & LOOKUP_NIL) && imp == forward_imp)) {
return nil;
}
return imp;
}
- 获取一个默认的imp,
_objc_msgForward_impcache
- 做前期的容错检查
- 循环遍历从当前类methodList再到父类缓存查找
3.1 二分法从methodlist查找imp
3.2 有就到6
3.3 当前类中没有,curClass设置为class的父类,如果父类为nil,imp设置forward_imp
,跳出循环
3.4 父类的缓存cache_getImp
查找,找到了就到6 - 如果父类缓存也没有,继续for循环调转到3,从父类的methodlist中查找,还没有再去父类的父类
cache_getImp
查找,递归循环查找,直到没有父类为止。 - 如果父类中也没有执行
resolveMethod_locked
去查找 - 将方法插入缓存
_objc_msgForward_impcache
的源码如下
__attribute__((noreturn, cold)) void
objc_defaultForwardHandler(id self, SEL sel)
{
_objc_fatal("%c[%s %s]: unrecognized selector sent to instance %p "
"(no message forward handler is installed)",
class_isMetaClass(object_getClass(self)) ? '+' : '-',
object_getClassName(self), sel_getName(sel), self);
}
getMethodNoSuper_nolock
getMethodNoSuper_nolock
二分法查找imp
getMethodNoSuper_nolock
->search_method_list_inline
->findMethodInSortedMethodList
其中findMethodInSortedMethodList
会判断当前的method是否为small或者big
template<class getNameFunc>
ALWAYS_INLINE static method_t *
findMethodInSortedMethodList(SEL key, const method_list_t *list, const getNameFunc &getName)
{
ASSERT(list);
//1.获取获取list的第一个值
auto first = list->begin();
auto base = first;
//基准位置
decltype(first) probe;
uintptr_t keyValue = (uintptr_t)key;
uint32_t count;
for (count = list->count; count != 0; count >>= 1) {
//cout右移1位,即probe移动到中间位置
probe = base + (count >> 1);
//获取中间位置的值
uintptr_t probeValue = (uintptr_t)getName(probe);
//如果中间位置与sel相等
if (keyValue == probeValue) {
// `probe` is a match.
// Rewind looking for the *first* occurrence of this value.
// This is required for correct category overrides.
//排除分类重名方法,加载的时候先存储类方法,再存储分类---按照栈格式存储,分类方法最先出,要取的是类方法,先排除分类方法,如果是两个分类,就看谁先进行加载
while (probe > first && keyValue == (uintptr_t)getName((probe - 1))) {
probe--;
}
return &*probe;
}
//如果没有找到,开始从右侧查找继续循环
if (keyValue > probeValue) {
base = probe + 1;
count--;
}
}
return nil;
}
1.在methodlist中,存储类的方法存储在前面,分类的方法存储在后后面,
cache_getImp
其中cache_getImp
也是一段汇编代码,调用CacheLookup
,调用了CacheLookup
第三个参数为LGetImpMissDynamic
会返回NULL,不会像objc_msgSend
样在缓存中没有查找,而执行lookUpImpOrForward
。
STATIC_ENTRY _cache_getImp
//获取父类的isa指针
GetClassFromIsa_p16 p0, 0
//缓存查找,第三个参数是缓存miss后执行的函数
CacheLookup GETIMP, _cache_getImp, LGetImpMissDynamic, LGetImpMissConstant
//缓存没有找到返回NULL
LGetImpMissDynamic:
mov p0, #0
ret
LGetImpMissConstant:
mov p0, p2
ret
END_ENTRY _cache_getImp