iOS底层原理 01 : alloc&init

2020-09-07  本文已影响0人  SmoothV

alloc的底层调用

1. alloc 会调用_objc_rootAlloc
+ (id)alloc {
  return _objc_rootAlloc(self);
}
2._objc_rootAlloc会调用callAlloc(cls, false, true);
id _objc_rootAlloc(Class cls)
{
  return callAlloc(cls, false/*checkNil*/, true/*allocWithZone*/);
}

3.预编译阶段,fastpath() 告诉系统大概率会执行下面的过程,即:_objc_rootAllocWithZone(cls, nil)

static ALWAYS_INLINE id callAlloc(Class cls, bool checkNil, bool allocWithZone=false)
{
#if __OBJC2__
  if (slowpath(checkNil && !cls)) return nil;
  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));
}

4.接下来执行_class_createInstanceFromZone(cls, 0, nil,OBJECT_CONSTRUCT_CALL_BADALLOC)
id _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);
}
5._class_createInstanceFromZone这里是具体干的事情

1.需要开辟多少空间 size = cls->instanceSize(extraBytes);
2.申请内存空间 obj = (id)calloc(1, size)
3.将这段内存空间与isa关联 obj->initInstanceIsa(cls, hasCxxDtor);
4.返回指向该段内存地址的指针 if (fastpath(!hasCxxCtor)) {return obj;}

static ALWAYS_INLINE id _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;
  // 1:要开辟多少内存
  size = cls->instanceSize(extraBytes);
  if (outAllocatedSize) *outAllocatedSize = size;

  id obj;
  if (zone) {
      obj = (id)malloc_zone_calloc((malloc_zone_t *)zone, 1, size);
  } else {
      // 2;怎么去申请内存
      obj = (id)calloc(1, size);
  }
  if (slowpath(!obj)) {
      if (construct_flags & OBJECT_CONSTRUCT_CALL_BADALLOC) {
          return _objc_callBadAllocHandler(cls);
      }
      return nil;
  }

  // 3: 将这段内存空间与isa关联 
  if (!zone && fast) {
      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);
  }
  // 4: 返回指向该段内存地址的指针
  if (fastpath(!hasCxxCtor)) {
      return obj;
  }

  construct_flags |= OBJECT_CONSTRUCT_FREE_ONFAILURE;
  return object_cxxConstructFromClass(obj, cls, construct_flags);
}

alloc流程图

alloc流程图

init底层调用

返回self

+ (id)init {
    return (id)self;
}

new的底层调用

1.[callAlloc(self, false) init]其实就是 [[XXX alloc]init]
2. 但是一般开发中并不建议使用new,主要是因为有时会重写init方法做一些自定义的操作,用new初始化无法走到重写init的自定义的部分,因为new是直接调用底层的C函数去实现的。

+ (id)new {
    return [callAlloc(self, false/*checkNil*/) init];
}

面试题

1.分析以下代打印的结果
LGPerson *p1 = [LGPerson alloc];
LGPerson *p2 = [p1 init];
LGPerson *p3 = [p1 init];
LGNSLog(@"%@ - %p - %p",p1,p1,&p1);
NSLog(@"%@ - %p - %p",p2,p2,&p2);
NSLog(@"%@ - %p - %p",p3,p3,&p3);

打印的结果:


结果

分析:
1.首先p1=p2=p3 ,是同一个对象,占据同一段内存空间,所依打印出来的地址相同
2. &p1 &p2 &p3 但是指向同一段内存空间的指针是不同的,所以 &p1 &p2 &p3不同

分析图.png
上一篇下一篇

猜你喜欢

热点阅读