Home > front end >  What kind of information is stored in an instance object beside an isa pointer?
What kind of information is stored in an instance object beside an isa pointer?

Time:10-28

All classes in objective-C inherited from objc_class, which has superClass, cache_t and class_data_bits containing all the methods, properties and protocols.

A instance object stores its superClass, methods, properties and protools informations in its class object, and class object stores its information in metaClass's class_data_bits.

So what else an instance object stores besides an isa pointer since all the class information has been stored in its class object?

CodePudding user response:

The object's data.

ObjC objects are laid out as a header, followed by instance variables (first the root class, then each subclass down to the object's direct instance variables). You won't see this anywhere directly in objc_object. But you'll see it in _class_createInstancesFromZone:

size_t size = cls->instanceSize(extraBytes);

num_allocated = 
    malloc_zone_batch_malloc((malloc_zone_t *)(zone ? zone : malloc_default_zone()), 
                             size, (void**)results, num_requested);

Note the call to cls->instanceSize(extraBytes). This is the full size of the instance, including its ivars (plus "extra"). It allocates much more than just an isa pointer. The Ivar table has offsets into that additional allocation.

The "extra" is not used very often, but it allows you to allocate extra memory for whatever purpose you like. For example, NSString allocates extra inline storage for its data rather than creating an extra pointer indirection. (I assume it still does this; it used to, but I haven't checked the code in a while.)

  • Related