class.c 27.6 KB
Newer Older
1
/* GNU Objective C Runtime class related functions
2
   Copyright (C) 1993-2019 Free Software Foundation, Inc.
3 4
   Contributed by Kresten Krab Thorup and Dennis Glatting.

5 6 7
   Lock-free class table code designed and written from scratch by
   Nicola Pero, 2001.

8
This file is part of GCC.
9

10
GCC is free software; you can redistribute it and/or modify it under the
11
terms of the GNU General Public License as published by the Free Software
12
Foundation; either version 3, or (at your option) any later version.
13

14
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 16 17 18
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
details.

19 20 21
Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.
22

23 24 25 26
You should have received a copy of the GNU General Public License and
a copy of the GCC Runtime Library Exception along with this program;
see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
<http://www.gnu.org/licenses/>.  */
27

28
/* The code in this file critically affects class method invocation
29
  speed.  This long preamble comment explains why, and the issues
30
  involved.
31 32 33 34 35 36 37 38 39 40 41 42 43

  One of the traditional weaknesses of the GNU Objective-C runtime is
  that class method invocations are slow.  The reason is that when you
  write
  
  array = [NSArray new];
  
  this gets basically compiled into the equivalent of 
  
  array = [(objc_get_class ("NSArray")) new];
  
  objc_get_class returns the class pointer corresponding to the string
  `NSArray'; and because of the lookup, the operation is more
44
  complicated and slow than a simple instance method invocation.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
  
  Most high performance Objective-C code (using the GNU Objc runtime)
  I had the opportunity to read (or write) work around this problem by
  caching the class pointer:
  
  Class arrayClass = [NSArray class];
  
  ... later on ...
  
  array = [arrayClass new];
  array = [arrayClass new];
  array = [arrayClass new];
  
  In this case, you always perform a class lookup (the first one), but
  then all the [arrayClass new] methods run exactly as fast as an
  instance method invocation.  It helps if you have many class method
61
  invocations to the same class.
62 63 64 65 66 67 68 69
  
  The long-term solution to this problem would be to modify the
  compiler to output tables of class pointers corresponding to all the
  class method invocations, and to add code to the runtime to update
  these tables - that should in the end allow class method invocations
  to perform precisely as fast as instance method invocations, because
  no class lookup would be involved.  I think the Apple Objective-C
  runtime uses this technique.  Doing this involves synchronized
70
  modifications in the runtime and in the compiler.
71 72 73 74 75 76
  
  As a first medicine to the problem, I [NP] have redesigned and
  rewritten the way the runtime is performing class lookup.  This
  doesn't give as much speed as the other (definitive) approach, but
  at least a class method invocation now takes approximately 4.5 times
  an instance method invocation on my machine (it would take approx 12
77
  times before the rewriting), which is a lot better.
78 79 80 81 82 83 84 85 86

  One of the main reason the new class lookup is so faster is because
  I implemented it in a way that can safely run multithreaded without
  using locks - a so-called `lock-free' data structure.  The atomic
  operation is pointer assignment.  The reason why in this problem
  lock-free data structures work so well is that you never remove
  classes from the table - and the difficult thing with lock-free data
  structures is freeing data when is removed from the structures.  */

Nicola Pero committed
87
#include "objc-private/common.h"
Nicola Pero committed
88
#include "objc-private/error.h"
89
#include "objc/runtime.h"
90
#include "objc/thr.h"
91 92
#include "objc-private/module-abi-8.h"  /* For CLS_ISCLASS and similar.  */
#include "objc-private/runtime.h"       /* the kitchen sink */
93
#include "objc-private/sarray.h"        /* For sarray_put_at_safe.  */
94
#include "objc-private/selector.h"      /* For sarray_put_at_safe.  */
95
#include <string.h>                     /* For memset */
96 97

/* We use a table which maps a class name to the corresponding class
98 99 100 101 102
   pointer.  The first part of this file defines this table, and
   functions to do basic operations on the table.  The second part of
   the file implements some higher level Objective-C functionality for
   classes by using the functions provided in the first part to manage
   the table. */
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140

/**
 ** Class Table Internals
 **/

/* 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;

/* A table containing classes is a class_node_ptr (pointing to the
   first entry in the table - if it is NULL, then the table is
   empty). */

/* We have 1024 tables.  Each table contains all class names which
   have the same hash (which is a number between 0 and 1023).  To look
   up a class_name, we compute its hash, and get the corresponding
   table.  Once we have the table, we simply compare strings directly
   till we find the one which we want (using the length first).  The
   number of tables is quite big on purpose (a normal big application
   has less than 1000 classes), so that you shouldn't normally get any
   collisions, and get away with a single comparison (which we can't
   avoid since we need to know that you have got the right thing).  */
#define CLASS_TABLE_SIZE 1024
#define CLASS_TABLE_MASK 1023

static class_node_ptr class_table_array[CLASS_TABLE_SIZE];

/* The table writing mutex - we lock on writing to avoid conflicts
   between different writers, but we read without locks.  That is
   possible because we assume pointer assignment to be an atomic
141 142
   operation.  TODO: This is only true under certain circumstances,
   which should be clarified.  */
143 144 145
static objc_mutex_t __class_table_lock = NULL;

/* CLASS_TABLE_HASH is how we compute the hash of a class name.  It is
146
   a macro - *not* a function - arguments *are* modified directly.
147 148 149 150 151 152 153 154 155

   INDEX should be a variable holding an int;
   HASH should be a variable holding an int;
   CLASS_NAME should be a variable holding a (char *) to the class_name.  

   After the macro is executed, INDEX contains the length of the
   string, and HASH the computed hash of the string; CLASS_NAME is
   untouched.  */

156 157 158 159 160 161 162 163 164 165
#define CLASS_TABLE_HASH(INDEX, HASH, CLASS_NAME)			\
  do {									\
    HASH = 0;								\
    for (INDEX = 0; CLASS_NAME[INDEX] != '\0'; INDEX++)			\
      {									\
	HASH = (HASH << 4) ^ (HASH >> 28) ^ CLASS_NAME[INDEX];		\
      }									\
									\
    HASH = (HASH ^ (HASH >> 10) ^ (HASH >> 20)) & CLASS_TABLE_MASK;	\
  } while (0)
166 167 168

/* Setup the table.  */
static void
169
class_table_setup (void)
170 171
{
  /* Start - nothing in the table.  */
172
  memset (class_table_array, 0, sizeof (class_node_ptr) * CLASS_TABLE_SIZE);
173 174 175 176 177 178

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


179 180
/* Insert a class in the table (used when a new class is
   registered).  */
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
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);
}

/* Get a class from the table.  This does not need mutex protection.
   Currently, this function is called each time you call a static
   method, this is why it must be very fast.  */
static inline Class 
class_table_get_safe (const char *class_name)
{
  class_node_ptr node;  
  int length, hash;

  /* Compute length and hash.  */
  CLASS_TABLE_HASH (length, hash, class_name);
  
  node = class_table_array[hash];
  
  if (node != NULL)
    {
      do
        {
          if (node->length == length)
            {
              /* Compare the class names.  */
              int i;

              for (i = 0; i < length; i++)
                {
                  if ((node->name)[i] != class_name[i]) 
233
		    break;
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
                }
              
              if (i == length)
                {
                  /* They are equal!  */
                  return node->pointer;
                }
            }
        }
      while ((node = node->next) != NULL);
    }

  return Nil;
}

/* Enumerate over the class table.  */
struct class_table_enumerator
{
  int hash;
  class_node_ptr node;
};


static Class
class_table_next (struct class_table_enumerator **e)
{
  struct class_table_enumerator *enumerator = *e;
  class_node_ptr next;
  
  if (enumerator == NULL)
    {
       *e = objc_malloc (sizeof (struct class_table_enumerator));
      enumerator = *e;
      enumerator->hash = 0;
      enumerator->node = NULL;

      next = class_table_array[enumerator->hash];
    }
  else
273
    next = enumerator->node->next;
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
  
  if (next != NULL)
    {
      enumerator->node = next;
      return enumerator->node->pointer;
    }
  else 
    {
      enumerator->hash++;
     
      while (enumerator->hash < CLASS_TABLE_SIZE)
        {
          next = class_table_array[enumerator->hash];
          if (next != NULL)
            {
              enumerator->node = next;
              return enumerator->node->pointer;
            }
          enumerator->hash++;
        }
      
      /* Ok - table finished - done.  */
      objc_free (enumerator);
      return Nil;
    }
}

#if 0 /* DEBUGGING FUNCTIONS */
/* Debugging function - print the class table.  */
void
304
class_table_print (void)
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
{
  int i;
  
  for (i = 0; i < CLASS_TABLE_SIZE; i++)
    {
      class_node_ptr node;
      
      printf ("%d:\n", i);
      node = class_table_array[i];
      
      while (node != NULL)
        {
          printf ("\t%s\n", node->name);
          node = node->next;
        }
    }
}

/* Debugging function - print an histogram of number of classes in
   function of hash key values.  Useful to evaluate the hash function
   in real cases.  */
void
327
class_table_print_histogram (void)
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
{
  int i, j;
  int counter = 0;
  
  for (i = 0; i < CLASS_TABLE_SIZE; i++)
    {
      class_node_ptr node;
      
      node = class_table_array[i];
      
      while (node != NULL)
        {
          counter++;
          node = node->next;
        }
      if (((i + 1) % 50) == 0)
        {
          printf ("%4d:", i + 1);
          for (j = 0; j < counter; j++)
347 348
	    printf ("X");

349 350 351 352 353 354
          printf ("\n");
          counter = 0;
        }
    }
  printf ("%4d:", i + 1);
  for (j = 0; j < counter; j++)
355 356
    printf ("X");

357 358 359 360 361 362 363 364 365 366 367 368
  printf ("\n");
}
#endif /* DEBUGGING FUNCTIONS */

/**
 ** Objective-C runtime functions
 **/

/* From now on, the only access to the class table data structure
   should be via the class_table_* functions.  */

/* This is a hook which is called by objc_get_class and
369
   objc_lookup_class if the runtime is not able to find the class.
370 371 372 373 374 375 376
   This may e.g. try to load in the class using dynamic loading.

   This hook was a public, global variable in the Traditional GNU
   Objective-C Runtime API (objc/objc-api.h).  The modern GNU
   Objective-C Runtime API (objc/runtime.h) provides the
   objc_setGetUnknownClassHandler() function instead.
*/
377
Class (*_objc_lookup_class) (const char *name) = 0;      /* !T:SAFE */
378

379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
/* The handler currently in use.  PS: if both
   __obj_get_unknown_class_handler and _objc_lookup_class are defined,
   __objc_get_unknown_class_handler is called first.  */
static objc_get_unknown_class_handler
__objc_get_unknown_class_handler = NULL;

objc_get_unknown_class_handler
objc_setGetUnknownClassHandler (objc_get_unknown_class_handler 
				new_handler)
{
  objc_get_unknown_class_handler old_handler 
    = __objc_get_unknown_class_handler;
  __objc_get_unknown_class_handler = new_handler;
  return old_handler;
}

395

396
/* True when class links has been resolved.  */     
397 398 399
BOOL __objc_class_links_resolved = NO;                  /* !T:UNUSED */


400 401
void
__objc_init_class_tables (void)
402
{
403 404
  /* Allocate the class hash table.  */
  
405
  if (__class_table_lock)
406
    return;
407
  
408
  objc_mutex_lock (__objc_runtime_mutex);
409 410
  
  class_table_setup ();
411

412
  objc_mutex_unlock (__objc_runtime_mutex);
413 414
}  

415
/* This function adds a class to the class hash table, and assigns the
416 417 418
   class a number, unless it's already known.  Return 'YES' if the
   class was added.  Return 'NO' if the class was already known.  */
BOOL
419
__objc_add_class_to_hash (Class class)
420
{
421
  Class existing_class;
422

423
  objc_mutex_lock (__objc_runtime_mutex);
424

425
  /* Make sure the table is there.  */
426
  assert (__class_table_lock);
427

428
  /* Make sure it's not a meta class.  */
429
  assert (CLS_ISCLASS (class));
430 431

  /* Check to see if the class is already in the hash table.  */
432 433 434 435 436 437 438 439
  existing_class = class_table_get_safe (class->name);

  if (existing_class)
    {
      objc_mutex_unlock (__objc_runtime_mutex);
      return NO;      
    }
  else
440
    {
441 442
      /* The class isn't in the hash table.  Add the class and assign
         a class number.  */
443
      static unsigned int class_number = 1;
444
      
445 446
      CLS_SETNUMBER (class, class_number);
      CLS_SETNUMBER (class->class_pointer, class_number);
447 448

      ++class_number;
449
      class_table_insert (class->name, class);
450

451 452 453
      objc_mutex_unlock (__objc_runtime_mutex);
      return YES;
    }
454 455
}

456
Class
457
objc_getClass (const char *name)
458 459 460
{
  Class class;

461 462
  if (name == NULL)
    return Nil;
463

464 465
  class = class_table_get_safe (name);
  
466 467
  if (class)
    return class;
468

469 470
  if (__objc_get_unknown_class_handler)
    return (*__objc_get_unknown_class_handler) (name);
471 472

  if (_objc_lookup_class)
473
    return (*_objc_lookup_class) (name);
474 475 476 477 478

  return Nil;
}

Class
479
objc_lookUpClass (const char *name)
480 481 482 483 484 485 486 487 488 489 490 491 492 493
{
  if (name == NULL)
    return Nil;
  else
    return class_table_get_safe (name);
}

Class
objc_getMetaClass (const char *name)
{
  Class class = objc_getClass (name);

  if (class)
    return class->class_pointer;
494
  else
495
    return Nil;
496 497
}

498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
Class
objc_getRequiredClass (const char *name)
{
  Class class = objc_getClass (name);

  if (class)
    return class;
  else
    _objc_abort ("objc_getRequiredClass ('%s') failed: class not found\n", name);
}

int
objc_getClassList (Class *returnValue, int maxNumberOfClassesToReturn)
{
  /* Iterate over all entries in the table.  */
  int hash, count = 0;

  for (hash = 0; hash < CLASS_TABLE_SIZE; hash++)
    {
      class_node_ptr node = class_table_array[hash];
      
      while (node != NULL)
	{
	  if (returnValue)
	    {
	      if (count < maxNumberOfClassesToReturn)
		returnValue[count] = node->pointer;
	      else
526
		return count;
527 528 529 530 531 532 533 534 535
	    }
	  count++;
	  node = node->next;
	}
    }
  
  return count;
}

536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
Class
objc_allocateClassPair (Class super_class, const char *class_name, size_t extraBytes)
{
  Class new_class;
  Class new_meta_class;

  if (class_name == NULL)
    return Nil;

  if (objc_getClass (class_name))
    return Nil;

  if (super_class)
    {
      /* If you want to build a hierarchy of classes, you need to
	 build and register them one at a time.  The risk is that you
	 are able to cause confusion by registering a subclass before
	 the superclass or similar.  */
      if (CLS_IS_IN_CONSTRUCTION (super_class))
	return Nil;
    }

  /* Technically, we should create the metaclass first, then use
     class_createInstance() to create the class.  That complication
     would be relevant if we had class variables, but we don't, so we
     just ignore it and create everything directly and assume all
     classes have the same size.  */
  new_class = objc_calloc (1, sizeof (struct objc_class) + extraBytes);
  new_meta_class = objc_calloc (1, sizeof (struct objc_class) + extraBytes);

  /* We create an unresolved class, similar to one generated by the
     compiler.  It will be resolved later when we register it.

     Note how the metaclass details are not that important; when the
     class is resolved, the ones that matter will be fixed up.  */
  new_class->class_pointer = new_meta_class;
  new_meta_class->class_pointer = 0;

  if (super_class)
    {
      /* Force the name of the superclass in place of the link to the
	 actual superclass, which will be put there when the class is
	 resolved.  */
      const char *super_class_name = class_getName (super_class);
      new_class->super_class = (void *)super_class_name;
      new_meta_class->super_class = (void *)super_class_name;
    }
  else
    {
      new_class->super_class = (void *)0;
      new_meta_class->super_class = (void *)0;
    }

  new_class->name = objc_malloc (strlen (class_name) + 1);
  strcpy ((char*)new_class->name, class_name);
  new_meta_class->name = new_class->name;

  new_class->version = 0;
  new_meta_class->version = 0;

  new_class->info = _CLS_CLASS | _CLS_IN_CONSTRUCTION;
  new_meta_class->info = _CLS_META | _CLS_IN_CONSTRUCTION;

  if (super_class)
    new_class->instance_size = super_class->instance_size;
  else
    new_class->instance_size = 0;
  new_meta_class->instance_size = sizeof (struct objc_class);

  return new_class;
}

void
objc_registerClassPair (Class class_)
{
  if (class_ == Nil)
    return;

  if ((! CLS_ISCLASS (class_)) || (! CLS_IS_IN_CONSTRUCTION (class_)))
    return;

  if ((! CLS_ISMETA (class_->class_pointer)) || (! CLS_IS_IN_CONSTRUCTION (class_->class_pointer)))
    return;

  objc_mutex_lock (__objc_runtime_mutex);

  if (objc_getClass (class_->name))
    {
      objc_mutex_unlock (__objc_runtime_mutex);
      return;
    }

  CLS_SET_NOT_IN_CONSTRUCTION (class_);
  CLS_SET_NOT_IN_CONSTRUCTION (class_->class_pointer);

  __objc_init_class (class_);

  /* Resolve class links immediately.  No point in waiting.  */
  __objc_resolve_class_links ();

  objc_mutex_unlock (__objc_runtime_mutex);
}

void
objc_disposeClassPair (Class class_)
{
  if (class_ == Nil)
    return;

  if ((! CLS_ISCLASS (class_)) || (! CLS_IS_IN_CONSTRUCTION (class_)))
    return;

  if ((! CLS_ISMETA (class_->class_pointer)) || (! CLS_IS_IN_CONSTRUCTION (class_->class_pointer)))
    return;

  /* Undo any class_addIvar().  */
  if (class_->ivars)
    {
      int i;
      for (i = 0; i < class_->ivars->ivar_count; i++)
	{
	  struct objc_ivar *ivar = &(class_->ivars->ivar_list[i]);

	  objc_free ((char *)ivar->ivar_name);
	  objc_free ((char *)ivar->ivar_type);
	}
      
      objc_free (class_->ivars);
    }

  /* Undo any class_addMethod().  */
  if (class_->methods)
    {
      struct objc_method_list *list = class_->methods;
      while (list)
	{
	  int i;
	  struct objc_method_list *next = list->method_next;

	  for (i = 0; i < list->method_count; i++)
	    {
	      struct objc_method *method = &(list->method_list[i]);

	      objc_free ((char *)method->method_name);
	      objc_free ((char *)method->method_types);
	    }

	  objc_free (list);
	  list = next;
	}
    }

  /* Undo any class_addProtocol().  */
  if (class_->protocols)
    {
      struct objc_protocol_list *list = class_->protocols;
      while (list)
	{
	  struct objc_protocol_list *next = list->next;

	  objc_free (list);
	  list = next;
	}
    }
  
  /* Undo any class_addMethod() on the meta-class.  */
  if (class_->class_pointer->methods)
    {
      struct objc_method_list *list = class_->class_pointer->methods;
      while (list)
	{
	  int i;
	  struct objc_method_list *next = list->method_next;

	  for (i = 0; i < list->method_count; i++)
	    {
	      struct objc_method *method = &(list->method_list[i]);

	      objc_free ((char *)method->method_name);
	      objc_free ((char *)method->method_types);
	    }

	  objc_free (list);
	  list = next;
	}
    }

  /* Undo objc_allocateClassPair().  */
  objc_free ((char *)(class_->name));
  objc_free (class_->class_pointer);
  objc_free (class_);
}

729 730 731 732
/* Traditional GNU Objective-C Runtime API.  Important: this method is
   called automatically by the compiler while messaging (if using the
   traditional ABI), so it is worth keeping it fast; don't make it
   just a wrapper around objc_getClass().  */
733
/* Note that this is roughly equivalent to objc_getRequiredClass().  */
734 735
/* Get the class object for the class named NAME.  If NAME does not
   identify a known class, the hook _objc_lookup_class is called.  If
736
   this fails, an error message is issued and the system aborts.  */
737 738 739 740 741
Class
objc_get_class (const char *name)
{
  Class class;

742
  class = class_table_get_safe (name);
743 744 745 746

  if (class)
    return class;

747 748 749 750
  if (__objc_get_unknown_class_handler)
    class = (*__objc_get_unknown_class_handler) (name);

  if ((!class)  &&  _objc_lookup_class)
751
    class = (*_objc_lookup_class) (name);
752

753
  if (class)
754 755
    return class;
  
Nicola Pero committed
756 757
  _objc_abort ("objc runtime: cannot find class %s\n", name);

758 759 760
  return 0;
}

761
/* This is used by the compiler too.  */
762
Class
763
objc_get_meta_class (const char *name)
764
{
765
  return objc_get_class (name)->class_pointer;
766 767
}

768
/* This is not used by GCC, but the clang compiler seems to use it
Ondřej Bílka committed
769
   when targeting the GNU runtime.  That's wrong, but we have it to
770 771 772 773 774 775 776
   be compatible.  */
Class
objc_lookup_class (const char *name)
{
  return objc_getClass (name);
}

777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
/* This is used when the implementation of a method changes.  It goes
   through all classes, looking for the ones that have these methods
   (either method_a or method_b; method_b can be NULL), and reloads
   the implementation for these.  You should call this with the
   runtime mutex already locked.  */
void
__objc_update_classes_with_methods (struct objc_method *method_a, struct objc_method *method_b)
{
  int hash;

  /* Iterate over all classes.  */
  for (hash = 0; hash < CLASS_TABLE_SIZE; hash++)
    {
      class_node_ptr node = class_table_array[hash];
      
      while (node != NULL)
	{
794 795 796 797 798 799 800 801
	  /* We execute this loop twice: the first time, we iterate
	     over all methods in the class (instance methods), while
	     the second time we iterate over all methods in the meta
	     class (class methods).  */
	  Class class = Nil;
	  BOOL done = NO;

	  while (done == NO)
802
	    {
803
	      struct objc_method_list * method_list;
804

805 806 807 808 809 810
	      if (class == Nil)
		{
		  /* The first time, we work on the class.  */
		  class = node->pointer;
		}
	      else
811
		{
812 813 814 815
		  /* The second time, we work on the meta class.  */
		  class = class->class_pointer;
		  done = YES;
		}
816

817
	      method_list = class->methods;
818

819 820 821 822 823
	      while (method_list)
		{
		  int i;
		  
		  for (i = 0; i < method_list->method_count; ++i)
824
		    {
825 826 827 828 829
		      struct objc_method *method = &method_list->method_list[i];
		      
		      /* If the method is one of the ones we are
			 looking for, update the implementation.  */
		      if (method == method_a)
830
			sarray_at_put_safe (class->dtable,
831 832 833 834 835 836 837 838 839 840
					    (sidx) method_a->method_name->sel_id,
					    method_a->method_imp);
		      
		      if (method == method_b)
			{
			  if (method_b != NULL)
			    sarray_at_put_safe (class->dtable,
						(sidx) method_b->method_name->sel_id,
						method_b->method_imp);
			}
841
		    }
842 843
		  
		  method_list = method_list->method_next;
844 845 846 847 848 849 850
		}
	    }
	  node = node->next;
	}
    }
}

851 852 853
/* Resolve super/subclass links for all classes.  The only thing we
   can be sure of is that the class_pointer for class objects point to
   the right meta class objects.  */
854 855
void
__objc_resolve_class_links (void)
856
{
857
  struct class_table_enumerator *es = NULL;
858
  Class object_class = objc_get_class ("Object");
859
  Class class1;
860

861
  assert (object_class);
862

863
  objc_mutex_lock (__objc_runtime_mutex);
864

865 866
  /* Assign subclass links.  */
  while ((class1 = class_table_next (&es)))
867 868
    {
      /* Make sure we have what we think we have.  */
869 870
      assert (CLS_ISCLASS (class1));
      assert (CLS_ISMETA (class1->class_pointer));
871

872 873
      /* The class_pointer of all meta classes point to Object's meta
         class.  */
874 875
      class1->class_pointer->class_pointer = object_class->class_pointer;

876
      if (! CLS_ISRESOLV (class1))
877
        {
878 879
          CLS_SETRESOLV (class1);
          CLS_SETRESOLV (class1->class_pointer);
880
              
881
          if (class1->super_class)
882 883 884 885 886 887 888 889 890
            {   
              Class a_super_class 
                = objc_get_class ((char *) class1->super_class);
              
              assert (a_super_class);
              
              DEBUG_PRINTF ("making class connections for: %s\n",
                            class1->name);
              
891
              /* Assign subclass links for superclass.  */
892 893 894
              class1->sibling_class = a_super_class->subclass_list;
              a_super_class->subclass_list = class1;
              
895
              /* Assign subclass links for meta class of superclass.  */
896 897 898 899 900 901 902 903
              if (a_super_class->class_pointer)
                {
                  class1->class_pointer->sibling_class
                    = a_super_class->class_pointer->subclass_list;
                  a_super_class->class_pointer->subclass_list 
                    = class1->class_pointer;
                }
            }
904 905
          else /* A root class, make its meta object be a subclass of
                  Object.  */
906 907 908 909 910 911 912 913
            {
              class1->class_pointer->sibling_class 
                = object_class->subclass_list;
              object_class->subclass_list = class1->class_pointer;
            }
        }
    }

914 915 916
  /* Assign superclass links.  */
   es = NULL;
   while ((class1 = class_table_next (&es)))
917 918 919 920 921 922
    {
      Class sub_class;
      for (sub_class = class1->subclass_list; sub_class;
           sub_class = sub_class->sibling_class)
        {
          sub_class->super_class = class1;
923
          if (CLS_ISCLASS (sub_class))
924 925 926 927
            sub_class->class_pointer->super_class = class1->class_pointer;
        }
    }

928
  objc_mutex_unlock (__objc_runtime_mutex);
929 930
}

931 932 933 934 935
const char *
class_getName (Class class_)
{
  if (class_ == Nil)
    return "nil";
936

937 938
  return class_->name;
}
939

940 941 942 943 944 945 946
BOOL
class_isMetaClass (Class class_)
{
  /* CLS_ISMETA includes the check for Nil class_.  */
  return CLS_ISMETA (class_);
}

947 948 949 950 951
/* Even inside libobjc it may be worth using class_getSuperclass
   instead of accessing class_->super_class directly because it
   resolves the class links if needed.  If you access
   class_->super_class directly, make sure to deal with the situation
   where the class is not resolved yet!  */
952 953 954 955 956 957
Class
class_getSuperclass (Class class_)
{
  if (class_ == Nil)
    return Nil;

958 959
  /* Classes that are in construction are not resolved, and still have
     the class name (instead of a class pointer) in the
960
     class_->super_class field.  In that case we need to lookup the
961
     superclass name to return the superclass.  We cannot resolve the
962
     class until it is registered.  */
963
  if (CLS_IS_IN_CONSTRUCTION (class_))
964 965 966 967 968 969
    {
      if (CLS_ISMETA (class_))
	return object_getClass ((id)objc_lookUpClass ((const char *)(class_->super_class)));
      else
	return objc_lookUpClass ((const char *)(class_->super_class));
    }
970

971 972 973 974 975 976 977
  /* If the class is not resolved yet, super_class would point to a
     string (the name of the super class) as opposed to the actual
     super class.  In that case, we need to resolve the class links
     before we can return super_class.  */
  if (! CLS_ISRESOLV (class_))
    __objc_resolve_class_links ();
  
978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
  return class_->super_class;
}

int
class_getVersion (Class class_)
{
  if (class_ == Nil)
    return 0;

  return (int)(class_->version);
}

void
class_setVersion (Class class_, int version)
{
  if (class_ == Nil)
    return;

  class_->version = version;
}

size_t
class_getInstanceSize (Class class_)
{
  if (class_ == Nil)
    return 0;

  return class_->instance_size;
}