iOS开发之 runtime(30) :none-lazy classes

logo

本系列博客是本人的源码阅读笔记,如果有 iOS 开发者在看 runtime 的,欢迎大家多多交流。

但大家心中的问题可能也接踵而至:

  • 方法 _getObjc2NonlazyClassList 作用是什么?
  • 什么是 remapped_class_map ?
  • 什么时候 创建 remapped_class_map ?以及什么时候 addRemappedClass ?

分析

对于第一个问题,相信大家稍微 Google 一下就会有结果:

NonlazyClass is all about a class implementing or not a +load method.
All the classes implemented in a given image file have a reference in a list stored in the "__DATA, __objc_classlist, regular, no_dead_strip" binary's section. This list allows the runtime system to keep track of all the classes stored in such file. However, not all of the classes need to be realized when the program starts up. That's why when a class implements a +load method, it also has a reference in a list stored in the "__DATA, __objc_nlclslist, regular, no_dead_strip" section.
So, _getObjc2NonlazyClassList retrieves the list of classes that do implement a +load method and are so called non-lazy. _getObjc2ClassList retrieves a list of all the classes in a image file, including the classes that don't have a +load method (and are called lazy) and the non-lazy ones. Non-lazy classes must be realized when the program starts up. Lazy classes, on the other hand, don't need to be realized immediately. This may be delayed until the class receives a message for the first time, for example (that's the reason for them to be considered "lazy").
The same is true for categories, by the way.

以上引用来自:Objective-C: What is a lazy class?

也就是说,如果一个类实现了 +load 方法,那么它就是个 NonlazyClass。笔者为了证实这一点,做了个验证:

// Realize non-lazy classes (for +load methods and static instances)
for (EACH_HEADER) {
    classref_t *classlist =
    _getObjc2NonlazyClassList(hi, &count);
    for (i = 0; i < count; i++) {
        Class cls = remapClass(classlist[i]);
//这里加了代码用于测试
        if (0 == strcmp(cls->mangledName(), "TestObject")) {
                printf("TestObject is non-lazy Class\n");
                fflush(stdout);
        }
        if (!cls) continue;
        realizeClass(cls);
    }
}

其中,TestObject 的定义如下:

@implementation TestObject
+(void) load {
    
}

int MyWeakLinkedFunction () {
    return 1;
}
@end

那么,另外一个问题来了:
有多少 non-lazy class ?同样的,笔者写了如下代码,打印出所有的 non-lazy class :

// Realize non-lazy classes (for +load methods and static instances)
for (EACH_HEADER) {
    classref_t *classlist =
    _getObjc2NonlazyClassList(hi, &count);
    for (i = 0; i < count; i++) {
        Class cls = remapClass(classlist[i]);
        //这里加了代码用于测试
        printf("non-lazy Class:%s\n",cls->mangledName());
        fflush(stdout);
        if (!cls) continue;
        realizeClass(cls);
    }
}

打印的结果总结到笔者的GitHub了:non-lazy-classes.txt

可以看到,大概有60个 non-lazy-classes。

什么时候创建 remapped_class_map ?

其实函数

static NXMapTable *remappedClasses(bool create)

已经告诉我们了。
参数 create 告诉是否需要创建 hash map,在 _read_images 方法中传进来的参数都是 NO,也就是说不会主动创建 hash map。我们全局搜索一下 remappedClasses,可以发现确实有地方调用的时候 传入的参数是 YES:

static void addRemappedClass(Class oldcls, Class newcls) {
...
    old = NXMapInsert(remappedClasses(YES), oldcls, newcls);
...
}

可以发现确实创建了,也就是我们前文所说的 addRemappedClass 方法。那

什么时候调用 addRemappedClass 方法?

但是并不是所有情况下都会调用到,只要查看一下函数 readClass 的实现就知道:

本文暂不做介绍了,后面的文章中会给大家介绍。

总结

本文终于又讲完一个 hashmap:remapped_class_map。其实说白了很简单,remapped_class_map 就是存储了 non-lazy classes 的

你可能感兴趣的:(iOS开发之 runtime(30) :none-lazy classes)