iOS load方法调用机制解析

前言:git

上篇文章 iOS Category底层原理详细研究流程 中 , 咱们有说到编译时 dyld 解读 Mach-o 文件对应在 runtime 库的入口函数 _objc_init 中三种不一样时期对应的回调函数.github

那么一样 , 咱们探索 load 方法也是由此展开 . 也就是在 dyld 读取完成时会调用 load_images 方法 , load 方法也是在此时调用. 感兴趣的能够去解读一下上篇博客.数组

废话很少说 , 一样打开 objc4 的源码 ( git开源地址 )app

探索流程

1. runtime 库入口函数

void _objc_init(void)
{
    static bool initialized = false;
    if (initialized) return;
    initialized = true;
    
    // fixme defer initialization until an objc-using image is found?
    environ_init();
    tls_init();
    static_init();
    lock_init();
    exception_init();

    _dyld_objc_notify_register(&map_images, load_images, unmap_image);
}
复制代码

关于 map_images, load_images, unmap_image 这三个函数我上一篇博客有具体提到, 这里很少赘述了. 直接进去 load_images :函数

void load_images(const char *path __unused, const struct mach_header *mh) {
    // Return without taking locks if there are no +load methods here.
    if (!hasLoadMethods((const headerType *)mh)) return;

    recursive_mutex_locker_t lock(loadMethodLock);

    // Discover load methods
    {
        mutex_locker_t lock2(runtimeLock);
        /** 准备 */
        prepare_load_methods((const headerType *)mh);
    }

    // Call +load methods (without runtimeLock - re-entrant)
    call_load_methods();
}
复制代码

代码中看出这里分为了两步 , 先准备 , 而后调用 . --> GOpost

2. load 方法准备

void prepare_load_methods(const headerType *mhdr) {
    size_t count, i;

    runtimeLock.assertLocked();

    //从 Macho 文件加载类的列表
    classref_t *classlist = 
        _getObjc2NonlazyClassList(mhdr, &count);
    for (i = 0; i < count; i++) {
        //数组:[<cls,method>,<cls,method>,<cls,method>] 有顺序
        schedule_class_load(remapClass(classlist[i]));
    }

    //针对分类的操做!
    category_t **categorylist = _getObjc2NonlazyCategoryList(mhdr, &count);
    for (i = 0; i < count; i++) {
        category_t *cat = categorylist[i];
        Class cls = remapClass(cat->cls);
        if (!cls) continue;  // category for ignored weak-linked class
        realizeClass(cls);
        assert(cls->ISA()->isRealized());
        add_category_to_loadable_list(cat);
    }
}
复制代码

这里又有两步操做spa

2.1 类的 load 方法读取和排序

//递归调用
static void schedule_class_load(Class cls) {
    if (!cls) return;
    assert(cls->isRealized());  // _read_images should realize

    if (cls->data()->flags & RW_LOADED) return;

    // Ensure superclass-first ordering
    schedule_class_load(cls->superclass);
    add_class_to_loadable_list(cls);
    cls->setInfo(RW_LOADED); 
}
复制代码

其实就是从 Mach-o 中读取到全部的类的列表 . 而后ssr

  • 递归 按当前类的 superclass 指针进行排列. 以此来知足一个规则 :

一个类的 load 方法调用 , 必定是在其父类的 load 方法以后.指针

  • 当递归到 NSObject 后 , 再找父类为 nil , 跳出递归 . 结束排列 , 而且将每一个 load 方法的 clsmethod 存储到全局的 loadable_class 结构体中.

2.2 分类的 load 方法读取和排序

分类的 load 大致流程和类的基本相似 , 一样先从 Mach-o 读取全部的分类列表 , 而后直接遍历 , 添加分类的 load 方法的 clsmethod 存储到全局的 loadable_categories 结构体中.code

也就是说 , 不一样分类之间 load 方法的加载顺序是根据 Mach-o 文件的编译顺序有关.

3. load 调用

call_load_methods :

void call_load_methods(void) {
    static bool loading = NO;
    bool more_categories;

    loadMethodLock.assertLocked();

    // Re-entrant calls do nothing; the outermost call will finish the job.
    if (loading) return;
    loading = YES;

    void *pool = objc_autoreleasePoolPush();

    do {
        // 1. Repeatedly call class +loads until there aren't any more
        while (loadable_classes_used > 0) {
            //先调用类的 load 方法
            call_class_loads();
        }

        // 2. Call category +loads ONCE
        more_categories = call_category_loads();

        // 3. Run more +loads if there are classes OR more untried categories
    } while (loadable_classes_used > 0  ||  more_categories);

    objc_autoreleasePoolPop(pool);

    loading = NO;
}
复制代码

这里就比较简单了. 先遍历以前存储好的类的 load 方法列表. 依次调用. 而后遍历分类的列表 , 依次调用.

总结:

  • load 方法在 runtime 库开始运行时调用.
  • 一个类的 load 方法在全部的父类 load 方法调用以后.
  • 分类的 load 方法在类的 load 方法以后.
  • 不一样分类之间的 load 方法调用顺序和编译顺序有关.
相关文章
相关标签/搜索