目录
知识点
1. hasCustomAWZ()
在类完成+initialize方法后,会调用_finishInitializing方法里的cls->setInitialized()来进行标记这个类完成了+initialized
objc_class::setInitialized()
{
Class metacls;
Class cls;
ASSERT(!isMetaClass());
cls = (Class)this;
metacls = cls->ISA();
mutex_locker_t lock(runtimeLock);
// Special cases:
// - NSObject AWZ class methods are default.
// - NSObject RR class and instance methods are default.
// - NSObject Core class and instance methods are default.
// adjustCustomFlagsForMethodChange() also knows these special cases.
// attachMethodLists() also knows these special cases.
// AWZ methods: +alloc / +allocWithZone:
objc::AWZScanner::scanInitializedClass(cls, metacls);
// Retain/Release methods that are extremely rarely overridden
//
// retain/release/autorelease/retainCount/
// _tryRetain/_isDeallocating/retainWeakReference/allowsWeakReference
objc::RRScanner::scanInitializedClass(cls, metacls);
// Core NSObject methods that are extremely rarely overridden
//
// +new, ±class, ±self, ±isKindOfClass:, ±respondsToSelector
objc::CoreScanner::scanInitializedClass(cls, metacls);
// Update the +initialize flags.
// Do this last.
metacls->changeInfo(RW_INITIALIZED, RW_INITIALIZING);
}
bool hasCustomAWZ() const {
return !cache.getBit(FAST_CACHE_HAS_DEFAULT_AWZ);
}
void setHasDefaultAWZ() {
cache.setBit(FAST_CACHE_HAS_DEFAULT_AWZ);
}
从注释中可以得知NSObject是default AWZ,也可以跟scanInitializedClass源码得知, 而从setHasDefaultAWZ中得知值存在类的cache里的_flags中占第14位,cache.getBit中_flags & FAST_CACHE_HAS_DEFAULT_AWZ为真, 所以取非后hasCustomAWZ为false, 所以fastpath(!cls->ISA()->hasCustomAWZ())为真。
2. instanceSize
instanceSize用来计算应该开辟的空间大小
size_t instanceSize(size_t extraBytes) const {
if (fastpath(cache.hasFastInstanceSize(extraBytes))) {
return cache.fastInstanceSize(extraBytes);
}
size_t size = alignedInstanceSize() + extraBytes;
// CF requires all objects be at least 16 bytes.
if (size < 16) size = 16;
return size;
}
bool hasFastInstanceSize(size_t extra) const
{
if (