女人荫蒂被添全过程13种图片,亚洲+欧美+在线,欧洲精品无码一区二区三区 ,在厨房拨开内裤进入毛片

您好,歡迎來電子發(fā)燒友網(wǎng)! ,新用戶?[免費(fèi)注冊]

您的位置:電子發(fā)燒友網(wǎng)>源碼下載>通訊/手機(jī)編程>

objc源碼中NSObject如何進(jìn)行初始化

大小:0.2 MB 人氣: 2017-09-26 需要積分:1

  + alloc 和 - init 這一對我們在 iOS 開發(fā)中每天都要用到的初始化方法一直困擾著我, 于是筆者仔細(xì)研究了一下 objc 源碼中 NSObject 如何進(jìn)行初始化。

  在具體分析對象的初始化過程之前,我想先放出結(jié)論,以免文章中的細(xì)枝末節(jié)對讀者的理解有所影響;整個對象的初始化過程其實(shí)只是 為一個分配內(nèi)存空間,并且初始化 isa_t 結(jié)構(gòu)體的過程。

  alloc 方法分析

  先來看一下 + alloc 方法的調(diào)用棧(在調(diào)用棧中省略了很多不必要的方法的調(diào)用):

  id _objc_rootAlloc(Class cls)

  └── static id callAlloc(Class cls, bool checkNil, bool allocWithZone=false)

  └── id class_createInstance(Class cls, size_t extraBytes)

  └── id _class_createInstanceFromZone(Class cls, size_t extraBytes, void *zone, bool cxxConstruct, size_t *outAllocatedSize)

  ├── size_t instanceSize(size_t extraBytes)

  ├── void *calloc(size_t, size_t) └── inline void objc_object::initInstanceIsa(Class cls, bool hasCxxDtor)

  這個調(diào)用棧中的方法涉及了多個文件中的代碼,在下面的章節(jié)中會對調(diào)用的方法逐步進(jìn)行分析,如果這個調(diào)用棧讓你覺得很頭疼,也不是什么問題。

  alloc 的實(shí)現(xiàn)

  + (id)alloc {

  return _objc_rootAlloc(self);

  }

  alloc 方法的實(shí)現(xiàn)真的是非常的簡單, 它直接調(diào)用了另一個私有方法 id _objc_rootAlloc(Class cls)

  id _objc_rootAlloc(Class cls) {

  return callAlloc(cls, false/*checkNil*/, true/*allocWithZone*/);

  }

  這就是上帝類 NSObject 對 callAlloc 的實(shí)現(xiàn),我們省略了非常多的代碼,展示了最常見的執(zhí)行路徑:

  static id callAlloc(Class cls, bool checkNil, bool allocWithZone=false) {

  id obj = class_createInstance(cls, 0);

  return obj;

  }

  id class_createInstance(Class cls, size_t extraBytes) {

  return _class_createInstanceFromZone(cls, extraBytes, nil);

  }

  對象初始化中最重要的操作都在 _class_createInstanceFromZone 方法中執(zhí)行:

  static id _class_createInstanceFromZone(Class cls, size_t extraBytes, void *zone, bool cxxConstruct = true, size_t *outAllocatedSize = nil) {

  size_t size = cls-》instanceSize(extraBytes);

  id obj = (id)calloc(1, size);

  if (!obj) return nil;

  obj-》initInstanceIsa(cls, hasCxxDtor);

  return obj;

  }

  對象的大小

  在使用 calloc 為對象分配一塊內(nèi)存空間之前,我們要先獲取對象在內(nèi)存的大小:

  size_t instanceSize(size_t extraBytes) {

  size_t size = alignedInstanceSize() + extraBytes;

  if (size 《 16) size = 16;

  return size;

  }

  uint32_t alignedInstanceSize() {

  return word_align(unalignedInstanceSize());

  }

  uint32_t unalignedInstanceSize() {

  assert(isRealized());

  return data()-》ro-》instanceSize;

  }

  實(shí)例大小 instanceSize 會存儲在類的 isa_t 結(jié)構(gòu)體中,然后經(jīng)過對齊最后返回。

  Core Foundation 需要所有的對象的大小都必須大于或等于 16 字節(jié)。

  在獲取對象大小之后,直接調(diào)用 calloc 函數(shù)就可以為對象分配內(nèi)存空間了。

  isa 的初始化

  在對象的初始化過程中除了使用 calloc 來分配內(nèi)存之外,還需要根據(jù)類初始化 isa_t 結(jié)構(gòu)體:

  inline void objc_object::initIsa(Class cls, bool indexed, bool hasCxxDtor) {

  if (!indexed) {

  isa.cls = cls;

  } else {

  isa.bits = ISA_MAGIC_VALUE;

  isa.has_cxx_dtor = hasCxxDtor;

  isa.shiftcls = (uintptr_t)cls 》》 3;

  }

  }

  上面的代碼只是對 isa_t 結(jié)構(gòu)體進(jìn)行初始化而已:

  union isa_t { isa_t() { }

  isa_t(uintptr_t value) : bits(value) { }

  Class cls;

  uintptr_t bits;

  struct {

  uintptr_t indexed : 1;

  uintptr_t has_assoc : 1;

  uintptr_t has_cxx_dtor : 1;

  uintptr_t shiftcls : 44;

  uintptr_t magic : 6;

  uintptr_t weakly_referenced : 1;

  uintptr_t deallocating : 1;

  uintptr_t has_sidetable_rc : 1;

  uintptr_t extra_rc : 8;

  };

  };

  init 方法

  NSObject 的 - init 方法只是調(diào)用了 _objc_rootInit 并返回了當(dāng)前對象:

  - (id)init {

  return _objc_rootInit(self);

  }

  id _objc_rootInit(id obj) {

  return obj;

  }

非常好我支持^.^

(0) 0%

不好我反對

(0) 0%

      發(fā)表評論

      用戶評論
      評價:好評中評差評

      發(fā)表評論,獲取積分! 請遵守相關(guān)規(guī)定!

      ?
      主站蜘蛛池模板: 屯门区| 房山区| 彭水| 菏泽市| 德昌县| 海门市| 莎车县| 鸡泽县| 新沂市| 安岳县| 库尔勒市| 台安县| 金山区| 阿荣旗| 集安市| 三明市| 汪清县| 呼玛县| 秭归县| 兰坪| 河津市| 长葛市| 沈阳市| 会昌县| 巢湖市| 扬中市| 英吉沙县| 静安区| 札达县| 泸定县| 阳新县| 苍梧县| 清涧县| 靖远县| 上饶县| 安泽县| 福贡县| 和林格尔县| 徐水县| 桃源县| 琼海市|