01-为什么选择选择webpack
952 2023-04-03 03:04:04
1.符号断点
2.查看汇编调试
3.当无法确定调用了什么方法时但是至少知道alloc
juejin.cn/post/697132… 大家可以看看这位怎么做的。
LGPerson *p = [LGPerson alloc] ;
+ (id)alloc {return _objc_rootAlloc(self);}
_objc_rootAlloc(Class cls){ return callAlloc(cls, false/*checkNil*/, true/*allocWithZone*/);}
- 假如 x = 8- X+7 & ~7 -> 8 + 7 & ~ 7- 8 : 0000 1000 - 7 : 0000 0111- ~7 : 1111 1000- & 运算 相同为1 不同为0- 15 0000 1111- & 1111 1000---------------- 0000 1000 - 得到结果为 8 - 假如 X = 9 - 9 : 0000 1001 - 7 : 0000 0111- ~7 : 1111 1000- & 运算 相同为1 不同为0- 16 0001 0000- & 1111 1000---------------- 0001 0000 - 得到结果为 16 我认为可以理解为 X / 8 = 商 --余数- 当有余数时 为 (商+1)*8- 没有余数时 为 商 * 8 位运算 >> 3 右移 3位 即 / 1000 如 15 : 0000 1111 >> 3 = 0000 0001 111 << 3 左移 3位 即 * 1000 如 15 : 0000 1111 << 3 = 0111 1000 所以 15 右移 3位 后 再 左移 3 位 得到了 0000 1000 为 8
字节对齐 8 字节 64位 为 8字节
内存对齐 16 字节
通过isa 获取类 地址
isa & isa_mask
+ (id)alloc { return _objc_rootAlloc(self);}
_objc_rootAlloc(Class cls){ return callAlloc(cls, false/*checkNil*/, true/*allocWithZone*/);}
callAlloc(Class cls, bool checkNil, bool allocWithZone=false){#if __OBJC2__ //是否有可用的编译器优化//slowpath 大概率为假//fastpath 大概率为真//提升编译效率。if (slowpath(checkNil && !cls)) return nil; //判断一个类是否有自定义的 +allocWithZone 实现,没有则走到if里面的实现 if (fastpath(!cls->ISA()->hasCustomAWZ())) { return _objc_rootAllocWithZone(cls, nil); }#endif // No shortcuts available. 没有可用的快捷方式 if (allocWithZone) { return ((id(*)(id, SEL, struct _NSZone *))objc_msgSend)(cls, @selector(allocWithZone:), nil); } return ((id(*)(id, SEL))objc_msgSend)(cls, @selector(alloc));}
_objc_rootAllocWithZone(Class cls, malloc_zone_t *zone __unused){ // allocWithZone under __OBJC2__ ignores the zone parameter return _class_createInstanceFromZone(cls, 0, nil, OBJECT_CONSTRUCT_CALL_BADALLOC);}
_class_createInstanceFromZone(Class cls, size_t extraBytes, void *zone, int construct_flags = OBJECT_CONSTRUCT_NONE, bool cxxConstruct = true, size_t *outAllocatedSize = nil){ ASSERT(cls->isRealized()); // Read class's info bits all at once for performance bool hasCxxCtor = cxxConstruct && cls->hasCxxCtor(); bool hasCxxDtor = cls->hasCxxDtor(); bool fast = cls->canAllocNonpointer(); size_t size; size = cls->instanceSize(extraBytes); //计算内存大小 if (outAllocatedSize) *outAllocatedSize = size; id obj; //分配一块脏地址 if (zone) { obj = (id)malloc_zone_calloc((malloc_zone_t *)zone, 1, size); } else { obj = (id)calloc(1, size); //分配一块可以使用的内存空间 } if (slowpath(!obj)) { if (construct_flags & OBJECT_CONSTRUCT_CALL_BADALLOC) { return _objc_callBadAllocHandler(cls); } return nil; } if (!zone && fast) { //将这块可使用的地址isa与cls类相关联 obj->initInstanceIsa(cls, hasCxxDtor); } else { // Use raw pointer isa on the assumption that they might be // doing something weird with the zone or RR. obj->initIsa(cls); } if (fastpath(!hasCxxCtor)) { return obj; //返回对象 } construct_flags |= OBJECT_CONSTRUCT_FREE_ONFAILURE; return object_cxxConstructFromClass(obj, cls, construct_flags);}
//slowpath 大概率为假
//fastpath 大概率为真
//此处去掉slowpath和fastpath对代码逻辑没有丝毫影响,应该只是告诉编译器对代码优化,提升编译效率。