Classes, Instances and Objects

Although they are said frequently, what they are exactly is  not usually known. They are conveniently explained like this.
Classes are like a template to instances of classes.

What it means is that the real object of a class is an instance, and a class is not tangible.
Well, then how a class can exist?

So, now let’s find out what the “template” really means by investigating source codes for the Objective-C implementation by the GNU. The reason it is chosen is that it is rather simple implementation for objects.

First let’s take a look at the Object class of the GNU Obj-C.

@implementation Object

+ initialize
{
  return self;
}

- init
{
  return self;
}

+ new
{
  return [[self alloc] init];
}

+ alloc
{
  return class_create_instance(self); => How is a class instantiated?
}

- free
{
  return object_dispose(self);
}
@endif

There are two interesting things in the code above.

  1. +initialize is for a class, -init is for an instance of the class. However the both return self.
  2. In alloc, class_create_instance(self) is used to allocate the instance.

Actually, the +initialize set up things once for the all instances of the classes. So, I guess the self of the +initialize can be also for the instance. (Before seeing the real source code for the Obj-C, I though the self for the +initialize was self pointer to the class.)

Now, let’s take a look at how class_create_instance is implemented.

id
class_create_instance (Class class)
{
  id new = nil;

#if OBJC_WITH_GC
  if (CLS_ISCLASS (class))
    new = (id) GC_malloc_explicitly_typed (class->instance_size,
					   class->gc_object_type);
#else
  if (CLS_ISCLASS (class))
    new = (*_objc_object_alloc) (class);
#endif

  if (new != nil)
    {
      memset (new, 0, class->instance_size);
      new->class_pointer = class;
    }
  return new;
}

id __objc_object_alloc (Class class)
{
  return (id) objc_malloc (class->instance_size);
}

So, memory is allocated in the class_create_instance using instance_size and gc_object_type depending on whether garbage collection is used or not.
So, here we can confirm that information in class type is used to allocate memory.What is the Class type and how class lookup table is constructed are shown below.

/* A node holding a class */
typedef struct class_node
{
  struct class_node *next;      /* Pointer to next entry on the list.
                                   NULL indicates end of list. */

  const char *name;             /* The class name string */
  int length;                   /* The class name string length */
  Class pointer;                /* The Class pointer */

} *class_node_ptr;

static class_node_ptr class_table_array[CLASS_TABLE_SIZE];

/* Setup the table.  */
static void
class_table_setup (void)
{
  /* Start - nothing in the table.  */
  memset (class_table_array, 0, sizeof (class_node_ptr) * CLASS_TABLE_SIZE);

  /* The table writing mutex.  */
  __class_table_lock = objc_mutex_allocate ();
}

/* Insert a class in the table (used when a new class is registered).  */
static void
class_table_insert (const char *class_name, Class class_pointer)
{
  int hash, length;
  class_node_ptr new_node;

  /* Find out the class name's hash and length.  */
  CLASS_TABLE_HASH (length, hash, class_name);

  /* Prepare the new node holding the class.  */
  new_node = objc_malloc (sizeof (struct class_node));
  new_node->name = class_name;
  new_node->length = length;
  new_node->pointer = class_pointer;

  /* Lock the table for modifications.  */
  objc_mutex_lock (__class_table_lock);

  /* Insert the new node in the table at the beginning of the table at
     class_table_array[hash].  */
  new_node->next = class_table_array[hash];
  class_table_array[hash] = new_node;

  objc_mutex_unlock (__class_table_lock);
}

typedef struct objc_class *MetaClass;
typedef struct objc_class *Class;
struct objc_class {
  MetaClass           class_pointer;          /* Pointer to the class's
                                                meta class. */
  struct objc_class*  super_class;            /* Pointer to the super
                                                class. NULL for class
                                                Object. */
  const char*         name;                   /* Name of the class. */
  long                version;                /* Unknown. */
  unsigned long       info;                   /* Bit mask.  See class masks
                                                defined above. */
  long                instance_size;          /* Size in bytes of the class.
                                                The sum of the class
						definition and all super
						class definitions. */
  struct objc_ivar_list* ivars;               /* Pointer to a structure that
                                                describes the instance
                                                variables in the class
                                                definition.  NULL indicates
                                                no instance variables.  Does
                                                not include super class
                                                variables. */
  struct objc_method_list*  methods;          /* Linked list of instance
                                                methods defined for the
                                                class. */
  struct sarray *    dtable;                  /* Pointer to instance
					         method dispatch table. */
  struct objc_class* subclass_list;           /* Subclasses */
  struct objc_class* sibling_class;

  struct objc_protocol_list *protocols;	      /* Protocols conformed to */
  void* gc_object_type;
};

So, it is now clear how class is used in the compiler and what the instance of a class is.
Class contains information for object, and when actual memory space is allocated using information for a class, it is called an instance of the class.Then what is the object? It is different from a textbook to a textbook.
“Object” means an instance of a class sometimes, or it is used to mean an entity like instance object, class object. So, the term object is positioned somewhat differently from the other two’s.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: