Objective-C中的对象的内存布局

2021-02-02  本文已影响0人  lieon

Objective-C的本质

一个OC对象在内存中如何布局的?

NSObject对象的底层实现

    NSObject *obj = [[NSObject alloc] init];
    // 16个字节
    
    // 获得NSObject实例对象的成员变量所占用的大小 >> 8
    NSLog(@"%zd", class_getInstanceSize([NSObject class]));
    
    // 获得obj指针所指向内存的大小 >> 16
    // 编译器分配了16个字节,实际用到8个字节
    NSLog(@"%zd", malloc_size((__bridge const void *)obj));
     + (id)allocWithZone:(struct _NSZone *)zone {
         return _objc_rootAllocWithZone(self, (malloc_zone_t *)zone);
     }
 
     id
     _objc_rootAllocWithZone(Class cls, malloc_zone_t *zone)
     {
         id obj;

     #if __OBJC2__
         // allocWithZone under __OBJC2__ ignores the zone parameter
         (void)zone;
         obj = class_createInstance(cls, 0);
     #else
         if (!zone) {
             obj = class_createInstance(cls, 0);
         }
         else {
             obj = class_createInstanceFromZone(cls, 0, zone);
         }
     #endif

         if (slowpath(!obj)) obj = callBadAllocHandler(cls);
         return obj;
     }
    
     id class_createInstance(Class cls, size_t extraBytes)
     {
         return _class_createInstanceFromZone(cls, extraBytes, nil);
     }
 
     id
     _class_createInstanceFromZone(Class cls, size_t extraBytes, void *zone,
                                   bool cxxConstruct = true,
                                   size_t *outAllocatedSize = nil)
     {
         if (!cls) return nil;

         assert(cls->isRealized());

         // Read class's info bits all at once for performance
         bool hasCxxCtor = cls->hasCxxCtor();
         bool hasCxxDtor = cls->hasCxxDtor();
         bool fast = cls->canAllocNonpointer();
        // 获取到实例的大小
         size_t size = cls->instanceSize(extraBytes);
         if (outAllocatedSize) *outAllocatedSize = size;

         id obj;
         if (!zone  &&  fast) {
             obj = (id)calloc(1, size);
             if (!obj) return nil;
             obj->initInstanceIsa(cls, hasCxxDtor);
         }
         else {
             if (zone) {
                 obj = (id)malloc_zone_calloc ((malloc_zone_t *)zone, 1, size);
             } else {
                 obj = (id)calloc(1, size);
             }
             if (!obj) return nil;

             // Use raw pointer isa on the assumption that they might be
             // doing something weird with the zone or RR.
             obj->initIsa(cls);
         }

         if (cxxConstruct && hasCxxCtor) {
             obj = _objc_constructOrFree(obj, cls);
         }

         return obj;
     }
 
     size_t instanceSize(size_t extraBytes) {
         size_t size = alignedInstanceSize() + extraBytes;
         // CF requires all objects be at least 16 bytes.
        // 保证最小大小为16个字节
         if (size < 16) size = 16;
         return size;
     }

继承NSObject的类的对象内存布局

     @interface Student : NSObject
     {
         @public
         int _no;
         int _age;
     }
    @end
    struct Student_IMPL {
        struct NSObject_IMPL NSObject_IVARS;
        int _no;
        int _age;
    };

    struct NSObject_IMPL {
        Class isa;
    };
    struct Student_IMPL {
        Class isa;
        int _no;
        int _age;
    };
    Student *stu = [[Student alloc] init];
    stu->_no = 4;
    stu->_age = 5;
    // 16
    NSLog(@"%zd", class_getInstanceSize([Student class]));
    // 16
    NSLog(@"%zd", malloc_size((__bridge const void *)stu));
image.png
@interface Person : NSObject
{
    @public
    int _age;
}
@end

@interface Student : Person
{
    @public
    int _no;
}
@end

@interface Graduate : Student
{
    @public
    int grade;
}
@end

@implementation Graduate

@end
struct NSObject_IMPL {
    Class isa;
};

struct Person_IMPL {
    struct NSObject_IMPL NSObject_IVARS; // 8
    int _age; // 4
};

struct Student_IMPL {
    struct Person_IMPL Person_IVARS; 
    int _no; 
}; 


 Student *stu = [[Student alloc] init];
 stu->_age = 2;
 stu->_no = 3;
 NSLog(@"stu - %zd", class_getInstanceSize([Student class]));  // 16
 NSLog(@"stu - %zd", malloc_size((__bridge const void *)stu)); // 16

 Graduate *grade = [[Graduate alloc] init];
grade->_age = 7;
grade->_no = 8;
grade->grade = 9;
NSLog(@"grade - %zd", class_getInstanceSize([Graduate class])); // 24
NSLog(@"grade - %zd", malloc_size((__bridge const void *)grade)); // 32
Student内存数据.png Student内存布局.png Grade内存数据.png

总结

上一篇下一篇

猜你喜欢

热点阅读