问题: 讲一下 Category 的实现原理?
注: 本问题回答基于
objc4-818.2
版本.
Category在运行时将 实例方法/类方法 动态的注入到 类对象/元类对象 中.
Category 在编译过程中会形成如下所示的 _category_t
结构体. 主要包含一下内容.
_method_list_t
类型的instance_methods
;_method_list_t
类型的class_methods
;_protocol_list_t
类型的protocols
;_prop_list_t
类型的properties
;
struct _category_t {
const char *name;
sturct _class_t *cls;
const sturct _method_list_t *instance_methods;
const sturct _method_list_t *class_methods;
const struct _protocol_list_t *protocols;
const struct _prop_list_t *properties;
};
Category在运行时的加载过程,其通过源码 attachCategories
函数,不管是实例方法/类方法/协议列表/属性列表都是先根据编译顺序,构建 method_list_t
/ protocol_list_t
/ prop_list_t
对应的临时数组.然后通过 list_array_tt
的 attachLists
方法添加到各个对应的 起始 位置上.
注: list_array_tt 是 method_array_t、protocol_array_t、property_array_t 的父类.
注: 不同Category的加载顺序是由项目配置项
Compile Sources
的文件顺序决定,也可以说是由编译顺序决定.
static void
attachCategories(Class cls, const locstamped_category_t *cats_list, uint32_t cats_count,
int flags)
{
if (slowpath(PrintReplacedMethods)) {
printReplacements(cls, cats_list, cats_count);
}
if (slowpath(PrintConnecting)) {
_objc_inform("CLASS: attaching %d categories to%s class '%s'%s",
cats_count, (flags & ATTACH_EXISTING) ? " existing" : "",
cls->nameForLogging(), (flags & ATTACH_METACLASS) ? " (meta)" : "");
}
/*
* Only a few classes have more than 64 categories during launch.
* This uses a little stack, and avoids malloc.
*
* Categories must be added in the proper order, which is back
* to front. To do that with the chunking, we iterate cats_list
* from front to back, build up the local buffers backwards,
* and call attachLists on the chunks. attachLists prepends the
* lists, so the final result is in the expected order.
*/
constexpr uint32_t ATTACH_BUFSIZ = 64;
method_list_t *mlists[TT];
property_list_t *proplists[ATTACH_BUFSIZ];
protocol_list_t *protolists[ATTACH_BUFSIZ];
uint32_t mcount = 0;
uint32_t propcount = 0;
uint32_t protocount = 0;
bool fromBundle = NO;
bool isMeta = (flags & ATTACH_METACLASS);
auto rwe = cls->data()->extAllocIfNeeded();
for (uint32_t i = 0; i < cats_count; i++) {
auto& entry = cats_list[i];
method_list_t *mlist = entry.cat->methodsForMeta(isMeta);
if (mlist) {
if (mcount == ATTACH_BUFSIZ) {
prepareMethodLists(cls, mlists, mcount, NO, fromBundle, __func__);
rwe->methods.attachLists(mlists, mcount);
mcount = 0;
}
mlists[ATTACH_BUFSIZ - ++mcount] = mlist;
fromBundle |= entry.hi->isBundle();
}
property_list_t *proplist =
entry.cat->propertiesForMeta(isMeta, entry.hi);
if (proplist) {
if (propcount == ATTACH_BUFSIZ) {
rwe->properties.attachLists(proplists, propcount);
propcount = 0;
}
proplists[ATTACH_BUFSIZ - ++propcount] = proplist;
}
protocol_list_t *protolist = entry.cat->protocolsForMeta(isMeta);
if (protolist) {
if (protocount == ATTACH_BUFSIZ) {
rwe->protocols.attachLists(protolists, protocount);
protocount = 0;
}
protolists[ATTACH_BUFSIZ - ++protocount] = protolist;
}
}
if (mcount > 0) {
prepareMethodLists(cls, mlists + ATTACH_BUFSIZ - mcount, mcount,
NO, fromBundle, __func__);
rwe->methods.attachLists(mlists + ATTACH_BUFSIZ - mcount, mcount);
if (flags & ATTACH_EXISTING) {
flushCaches(cls, __func__, [](Class c){
// constant caches have been dealt with in prepareMethodLists
// if the class still is constant here, it's fine to keep
return !c->cache.isConstantOptimizedCache();
});
}
}
rwe->properties.attachLists(proplists + ATTACH_BUFSIZ - propcount, propcount);
rwe->protocols.attachLists(protolists + ATTACH_BUFSIZ - protocount, protocount);
}
list_array_t 结构如下所示.
struct list_array_t {
...
prublic:
void attachLists(List * addedLists, uint32_t addedCount) {
if (addedCount == 0) return;
if (hasArray()) {
// 当前已经存在多个列表
uint32_t oldCount = array()->count;
unit32_ newCount = oldCount + addedCount;
// 开辟内存空间
arrat_t *newArray = (array_t *)malloc(array::byteSize(newCount));
newArray->count = newCount;
array()->count = newCount;
for (int i = oldCount - 1; i >= 0; i--)
newArray->lists[i + addedCount] = array()->list[i];
for (unsiged i = 0; i < addedCount; i++)
newArray->lists[i] = addedLists[i];
free(array());
setArray(newArray);
validate();
}
else if (!list && addCount = 1) {
// 当前没有列表
list = addLists[0];
validate();
}
else {
// 当前只有一个列表
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()->list[addedCount] = oldList;
for (unsiged i = 0; i < addedCount; i++)
array()->list[i] = addedLists[i];
validate();
}
}
}
问题: 讲一下 Category 和 Extension 的区别?
类的 Extension 在编译时相关数据就已经包含在类信息中了.
类的 Category 在编译时时形成 category_t
的结构体, 在运行时将 方法 属性 协议 动态注入到类对象或者元类对象中.
Comments | 0 条评论