2011-03-24 Paolo Bonzini <bonzini@gnu.org>
[official-gcc.git] / libobjc / class.c
blob61d6fdf2f464c0c624437745de573420d5a857c5
1 /* GNU Objective C Runtime class related functions
2 Copyright (C) 1993, 1995, 1996, 1997, 2001, 2002, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Kresten Krab Thorup and Dennis Glatting.
6 Lock-free class table code designed and written from scratch by
7 Nicola Pero, 2001.
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under the
12 terms of the GNU General Public License as published by the Free Software
13 Foundation; either version 3, or (at your option) any later version.
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 details.
20 Under Section 7 of GPL version 3, you are granted additional
21 permissions described in the GCC Runtime Library Exception, version
22 3.1, as published by the Free Software Foundation.
24 You should have received a copy of the GNU General Public License and
25 a copy of the GCC Runtime Library Exception along with this program;
26 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
27 <http://www.gnu.org/licenses/>. */
29 /* The code in this file critically affects class method invocation
30 speed. This long preamble comment explains why, and the issues
31 involved.
33 One of the traditional weaknesses of the GNU Objective-C runtime is
34 that class method invocations are slow. The reason is that when you
35 write
37 array = [NSArray new];
39 this gets basically compiled into the equivalent of
41 array = [(objc_get_class ("NSArray")) new];
43 objc_get_class returns the class pointer corresponding to the string
44 `NSArray'; and because of the lookup, the operation is more
45 complicated and slow than a simple instance method invocation.
47 Most high performance Objective-C code (using the GNU Objc runtime)
48 I had the opportunity to read (or write) work around this problem by
49 caching the class pointer:
51 Class arrayClass = [NSArray class];
53 ... later on ...
55 array = [arrayClass new];
56 array = [arrayClass new];
57 array = [arrayClass new];
59 In this case, you always perform a class lookup (the first one), but
60 then all the [arrayClass new] methods run exactly as fast as an
61 instance method invocation. It helps if you have many class method
62 invocations to the same class.
64 The long-term solution to this problem would be to modify the
65 compiler to output tables of class pointers corresponding to all the
66 class method invocations, and to add code to the runtime to update
67 these tables - that should in the end allow class method invocations
68 to perform precisely as fast as instance method invocations, because
69 no class lookup would be involved. I think the Apple Objective-C
70 runtime uses this technique. Doing this involves synchronized
71 modifications in the runtime and in the compiler.
73 As a first medicine to the problem, I [NP] have redesigned and
74 rewritten the way the runtime is performing class lookup. This
75 doesn't give as much speed as the other (definitive) approach, but
76 at least a class method invocation now takes approximately 4.5 times
77 an instance method invocation on my machine (it would take approx 12
78 times before the rewriting), which is a lot better.
80 One of the main reason the new class lookup is so faster is because
81 I implemented it in a way that can safely run multithreaded without
82 using locks - a so-called `lock-free' data structure. The atomic
83 operation is pointer assignment. The reason why in this problem
84 lock-free data structures work so well is that you never remove
85 classes from the table - and the difficult thing with lock-free data
86 structures is freeing data when is removed from the structures. */
88 #include "objc-private/common.h"
89 #include "objc-private/error.h"
90 #include "objc/runtime.h"
91 #include "objc/thr.h"
92 #include "objc-private/module-abi-8.h" /* For CLS_ISCLASS and similar. */
93 #include "objc-private/runtime.h" /* the kitchen sink */
94 #include "objc-private/sarray.h" /* For sarray_put_at_safe. */
95 #include <string.h> /* For memset */
97 /* We use a table which maps a class name to the corresponding class
98 pointer. The first part of this file defines this table, and
99 functions to do basic operations on the table. The second part of
100 the file implements some higher level Objective-C functionality for
101 classes by using the functions provided in the first part to manage
102 the table. */
105 ** Class Table Internals
108 /* A node holding a class */
109 typedef struct class_node
111 struct class_node *next; /* Pointer to next entry on the list.
112 NULL indicates end of list. */
114 const char *name; /* The class name string */
115 int length; /* The class name string length */
116 Class pointer; /* The Class pointer */
118 } *class_node_ptr;
120 /* A table containing classes is a class_node_ptr (pointing to the
121 first entry in the table - if it is NULL, then the table is
122 empty). */
124 /* We have 1024 tables. Each table contains all class names which
125 have the same hash (which is a number between 0 and 1023). To look
126 up a class_name, we compute its hash, and get the corresponding
127 table. Once we have the table, we simply compare strings directly
128 till we find the one which we want (using the length first). The
129 number of tables is quite big on purpose (a normal big application
130 has less than 1000 classes), so that you shouldn't normally get any
131 collisions, and get away with a single comparison (which we can't
132 avoid since we need to know that you have got the right thing). */
133 #define CLASS_TABLE_SIZE 1024
134 #define CLASS_TABLE_MASK 1023
136 static class_node_ptr class_table_array[CLASS_TABLE_SIZE];
138 /* The table writing mutex - we lock on writing to avoid conflicts
139 between different writers, but we read without locks. That is
140 possible because we assume pointer assignment to be an atomic
141 operation. TODO: This is only true under certain circumstances,
142 which should be clarified. */
143 static objc_mutex_t __class_table_lock = NULL;
145 /* 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.
148 INDEX should be a variable holding an int;
149 HASH should be a variable holding an int;
150 CLASS_NAME should be a variable holding a (char *) to the class_name.
152 After the macro is executed, INDEX contains the length of the
153 string, and HASH the computed hash of the string; CLASS_NAME is
154 untouched. */
156 #define CLASS_TABLE_HASH(INDEX, HASH, CLASS_NAME) \
157 HASH = 0; \
158 for (INDEX = 0; CLASS_NAME[INDEX] != '\0'; INDEX++) \
160 HASH = (HASH << 4) ^ (HASH >> 28) ^ CLASS_NAME[INDEX]; \
163 HASH = (HASH ^ (HASH >> 10) ^ (HASH >> 20)) & CLASS_TABLE_MASK;
165 /* Setup the table. */
166 static void
167 class_table_setup (void)
169 /* Start - nothing in the table. */
170 memset (class_table_array, 0, sizeof (class_node_ptr) * CLASS_TABLE_SIZE);
172 /* The table writing mutex. */
173 __class_table_lock = objc_mutex_allocate ();
177 /* Insert a class in the table (used when a new class is
178 registered). */
179 static void
180 class_table_insert (const char *class_name, Class class_pointer)
182 int hash, length;
183 class_node_ptr new_node;
185 /* Find out the class name's hash and length. */
186 CLASS_TABLE_HASH (length, hash, class_name);
188 /* Prepare the new node holding the class. */
189 new_node = objc_malloc (sizeof (struct class_node));
190 new_node->name = class_name;
191 new_node->length = length;
192 new_node->pointer = class_pointer;
194 /* Lock the table for modifications. */
195 objc_mutex_lock (__class_table_lock);
197 /* Insert the new node in the table at the beginning of the table at
198 class_table_array[hash]. */
199 new_node->next = class_table_array[hash];
200 class_table_array[hash] = new_node;
202 objc_mutex_unlock (__class_table_lock);
205 /* Replace a class in the table (used only by poseAs:). */
206 static void
207 class_table_replace (Class old_class_pointer, Class new_class_pointer)
209 int hash;
210 class_node_ptr node;
212 objc_mutex_lock (__class_table_lock);
214 hash = 0;
215 node = class_table_array[hash];
217 while (hash < CLASS_TABLE_SIZE)
219 if (node == NULL)
221 hash++;
222 if (hash < CLASS_TABLE_SIZE)
223 node = class_table_array[hash];
225 else
227 Class class1 = node->pointer;
229 if (class1 == old_class_pointer)
230 node->pointer = new_class_pointer;
232 node = node->next;
236 objc_mutex_unlock (__class_table_lock);
240 /* Get a class from the table. This does not need mutex protection.
241 Currently, this function is called each time you call a static
242 method, this is why it must be very fast. */
243 static inline Class
244 class_table_get_safe (const char *class_name)
246 class_node_ptr node;
247 int length, hash;
249 /* Compute length and hash. */
250 CLASS_TABLE_HASH (length, hash, class_name);
252 node = class_table_array[hash];
254 if (node != NULL)
258 if (node->length == length)
260 /* Compare the class names. */
261 int i;
263 for (i = 0; i < length; i++)
265 if ((node->name)[i] != class_name[i])
266 break;
269 if (i == length)
271 /* They are equal! */
272 return node->pointer;
276 while ((node = node->next) != NULL);
279 return Nil;
282 /* Enumerate over the class table. */
283 struct class_table_enumerator
285 int hash;
286 class_node_ptr node;
290 static Class
291 class_table_next (struct class_table_enumerator **e)
293 struct class_table_enumerator *enumerator = *e;
294 class_node_ptr next;
296 if (enumerator == NULL)
298 *e = objc_malloc (sizeof (struct class_table_enumerator));
299 enumerator = *e;
300 enumerator->hash = 0;
301 enumerator->node = NULL;
303 next = class_table_array[enumerator->hash];
305 else
306 next = enumerator->node->next;
308 if (next != NULL)
310 enumerator->node = next;
311 return enumerator->node->pointer;
313 else
315 enumerator->hash++;
317 while (enumerator->hash < CLASS_TABLE_SIZE)
319 next = class_table_array[enumerator->hash];
320 if (next != NULL)
322 enumerator->node = next;
323 return enumerator->node->pointer;
325 enumerator->hash++;
328 /* Ok - table finished - done. */
329 objc_free (enumerator);
330 return Nil;
334 #if 0 /* DEBUGGING FUNCTIONS */
335 /* Debugging function - print the class table. */
336 void
337 class_table_print (void)
339 int i;
341 for (i = 0; i < CLASS_TABLE_SIZE; i++)
343 class_node_ptr node;
345 printf ("%d:\n", i);
346 node = class_table_array[i];
348 while (node != NULL)
350 printf ("\t%s\n", node->name);
351 node = node->next;
356 /* Debugging function - print an histogram of number of classes in
357 function of hash key values. Useful to evaluate the hash function
358 in real cases. */
359 void
360 class_table_print_histogram (void)
362 int i, j;
363 int counter = 0;
365 for (i = 0; i < CLASS_TABLE_SIZE; i++)
367 class_node_ptr node;
369 node = class_table_array[i];
371 while (node != NULL)
373 counter++;
374 node = node->next;
376 if (((i + 1) % 50) == 0)
378 printf ("%4d:", i + 1);
379 for (j = 0; j < counter; j++)
380 printf ("X");
382 printf ("\n");
383 counter = 0;
386 printf ("%4d:", i + 1);
387 for (j = 0; j < counter; j++)
388 printf ("X");
390 printf ("\n");
392 #endif /* DEBUGGING FUNCTIONS */
395 ** Objective-C runtime functions
398 /* From now on, the only access to the class table data structure
399 should be via the class_table_* functions. */
401 /* This is a hook which is called by objc_get_class and
402 objc_lookup_class if the runtime is not able to find the class.
403 This may e.g. try to load in the class using dynamic loading.
405 This hook was a public, global variable in the Traditional GNU
406 Objective-C Runtime API (objc/objc-api.h). The modern GNU
407 Objective-C Runtime API (objc/runtime.h) provides the
408 objc_setGetUnknownClassHandler() function instead.
410 Class (*_objc_lookup_class) (const char *name) = 0; /* !T:SAFE */
412 /* The handler currently in use. PS: if both
413 __obj_get_unknown_class_handler and _objc_lookup_class are defined,
414 __objc_get_unknown_class_handler is called first. */
415 static objc_get_unknown_class_handler
416 __objc_get_unknown_class_handler = NULL;
418 objc_get_unknown_class_handler
419 objc_setGetUnknownClassHandler (objc_get_unknown_class_handler
420 new_handler)
422 objc_get_unknown_class_handler old_handler
423 = __objc_get_unknown_class_handler;
424 __objc_get_unknown_class_handler = new_handler;
425 return old_handler;
429 /* True when class links has been resolved. */
430 BOOL __objc_class_links_resolved = NO; /* !T:UNUSED */
433 void
434 __objc_init_class_tables (void)
436 /* Allocate the class hash table. */
438 if (__class_table_lock)
439 return;
441 objc_mutex_lock (__objc_runtime_mutex);
443 class_table_setup ();
445 objc_mutex_unlock (__objc_runtime_mutex);
448 /* This function adds a class to the class hash table, and assigns the
449 class a number, unless it's already known. Return 'YES' if the
450 class was added. Return 'NO' if the class was already known. */
451 BOOL
452 __objc_add_class_to_hash (Class class)
454 Class existing_class;
456 objc_mutex_lock (__objc_runtime_mutex);
458 /* Make sure the table is there. */
459 assert (__class_table_lock);
461 /* Make sure it's not a meta class. */
462 assert (CLS_ISCLASS (class));
464 /* Check to see if the class is already in the hash table. */
465 existing_class = class_table_get_safe (class->name);
467 if (existing_class)
469 objc_mutex_unlock (__objc_runtime_mutex);
470 return NO;
472 else
474 /* The class isn't in the hash table. Add the class and assign
475 a class number. */
476 static unsigned int class_number = 1;
478 CLS_SETNUMBER (class, class_number);
479 CLS_SETNUMBER (class->class_pointer, class_number);
481 ++class_number;
482 class_table_insert (class->name, class);
484 objc_mutex_unlock (__objc_runtime_mutex);
485 return YES;
489 Class
490 objc_getClass (const char *name)
492 Class class;
494 if (name == NULL)
495 return Nil;
497 class = class_table_get_safe (name);
499 if (class)
500 return class;
502 if (__objc_get_unknown_class_handler)
503 return (*__objc_get_unknown_class_handler) (name);
505 if (_objc_lookup_class)
506 return (*_objc_lookup_class) (name);
508 return Nil;
511 Class
512 objc_lookUpClass (const char *name)
514 if (name == NULL)
515 return Nil;
516 else
517 return class_table_get_safe (name);
520 Class
521 objc_getMetaClass (const char *name)
523 Class class = objc_getClass (name);
525 if (class)
526 return class->class_pointer;
527 else
528 return Nil;
531 Class
532 objc_getRequiredClass (const char *name)
534 Class class = objc_getClass (name);
536 if (class)
537 return class;
538 else
539 _objc_abort ("objc_getRequiredClass ('%s') failed: class not found\n", name);
543 objc_getClassList (Class *returnValue, int maxNumberOfClassesToReturn)
545 /* Iterate over all entries in the table. */
546 int hash, count = 0;
548 for (hash = 0; hash < CLASS_TABLE_SIZE; hash++)
550 class_node_ptr node = class_table_array[hash];
552 while (node != NULL)
554 if (returnValue)
556 if (count < maxNumberOfClassesToReturn)
557 returnValue[count] = node->pointer;
558 else
559 return count;
561 count++;
562 node = node->next;
566 return count;
569 Class
570 objc_allocateClassPair (Class super_class, const char *class_name, size_t extraBytes)
572 Class new_class;
573 Class new_meta_class;
575 if (class_name == NULL)
576 return Nil;
578 if (objc_getClass (class_name))
579 return Nil;
581 if (super_class)
583 /* If you want to build a hierarchy of classes, you need to
584 build and register them one at a time. The risk is that you
585 are able to cause confusion by registering a subclass before
586 the superclass or similar. */
587 if (CLS_IS_IN_CONSTRUCTION (super_class))
588 return Nil;
591 /* Technically, we should create the metaclass first, then use
592 class_createInstance() to create the class. That complication
593 would be relevant if we had class variables, but we don't, so we
594 just ignore it and create everything directly and assume all
595 classes have the same size. */
596 new_class = objc_calloc (1, sizeof (struct objc_class) + extraBytes);
597 new_meta_class = objc_calloc (1, sizeof (struct objc_class) + extraBytes);
599 /* We create an unresolved class, similar to one generated by the
600 compiler. It will be resolved later when we register it.
602 Note how the metaclass details are not that important; when the
603 class is resolved, the ones that matter will be fixed up. */
604 new_class->class_pointer = new_meta_class;
605 new_meta_class->class_pointer = 0;
607 if (super_class)
609 /* Force the name of the superclass in place of the link to the
610 actual superclass, which will be put there when the class is
611 resolved. */
612 const char *super_class_name = class_getName (super_class);
613 new_class->super_class = (void *)super_class_name;
614 new_meta_class->super_class = (void *)super_class_name;
616 else
618 new_class->super_class = (void *)0;
619 new_meta_class->super_class = (void *)0;
622 new_class->name = objc_malloc (strlen (class_name) + 1);
623 strcpy ((char*)new_class->name, class_name);
624 new_meta_class->name = new_class->name;
626 new_class->version = 0;
627 new_meta_class->version = 0;
629 new_class->info = _CLS_CLASS | _CLS_IN_CONSTRUCTION;
630 new_meta_class->info = _CLS_META | _CLS_IN_CONSTRUCTION;
632 if (super_class)
633 new_class->instance_size = super_class->instance_size;
634 else
635 new_class->instance_size = 0;
636 new_meta_class->instance_size = sizeof (struct objc_class);
638 return new_class;
641 void
642 objc_registerClassPair (Class class_)
644 if (class_ == Nil)
645 return;
647 if ((! CLS_ISCLASS (class_)) || (! CLS_IS_IN_CONSTRUCTION (class_)))
648 return;
650 if ((! CLS_ISMETA (class_->class_pointer)) || (! CLS_IS_IN_CONSTRUCTION (class_->class_pointer)))
651 return;
653 objc_mutex_lock (__objc_runtime_mutex);
655 if (objc_getClass (class_->name))
657 objc_mutex_unlock (__objc_runtime_mutex);
658 return;
661 CLS_SET_NOT_IN_CONSTRUCTION (class_);
662 CLS_SET_NOT_IN_CONSTRUCTION (class_->class_pointer);
664 __objc_init_class (class_);
666 /* Resolve class links immediately. No point in waiting. */
667 __objc_resolve_class_links ();
669 objc_mutex_unlock (__objc_runtime_mutex);
672 void
673 objc_disposeClassPair (Class class_)
675 if (class_ == Nil)
676 return;
678 if ((! CLS_ISCLASS (class_)) || (! CLS_IS_IN_CONSTRUCTION (class_)))
679 return;
681 if ((! CLS_ISMETA (class_->class_pointer)) || (! CLS_IS_IN_CONSTRUCTION (class_->class_pointer)))
682 return;
684 /* Undo any class_addIvar(). */
685 if (class_->ivars)
687 int i;
688 for (i = 0; i < class_->ivars->ivar_count; i++)
690 struct objc_ivar *ivar = &(class_->ivars->ivar_list[i]);
692 objc_free ((char *)ivar->ivar_name);
693 objc_free ((char *)ivar->ivar_type);
696 objc_free (class_->ivars);
699 /* Undo any class_addMethod(). */
700 if (class_->methods)
702 struct objc_method_list *list = class_->methods;
703 while (list)
705 int i;
706 struct objc_method_list *next = list->method_next;
708 for (i = 0; i < list->method_count; i++)
710 struct objc_method *method = &(list->method_list[i]);
712 objc_free ((char *)method->method_name);
713 objc_free ((char *)method->method_types);
716 objc_free (list);
717 list = next;
721 /* Undo any class_addProtocol(). */
722 if (class_->protocols)
724 struct objc_protocol_list *list = class_->protocols;
725 while (list)
727 struct objc_protocol_list *next = list->next;
729 objc_free (list);
730 list = next;
734 /* Undo any class_addMethod() on the meta-class. */
735 if (class_->class_pointer->methods)
737 struct objc_method_list *list = class_->class_pointer->methods;
738 while (list)
740 int i;
741 struct objc_method_list *next = list->method_next;
743 for (i = 0; i < list->method_count; i++)
745 struct objc_method *method = &(list->method_list[i]);
747 objc_free ((char *)method->method_name);
748 objc_free ((char *)method->method_types);
751 objc_free (list);
752 list = next;
756 /* Undo objc_allocateClassPair(). */
757 objc_free ((char *)(class_->name));
758 objc_free (class_->class_pointer);
759 objc_free (class_);
762 /* Traditional GNU Objective-C Runtime API. */
763 /* Get the class object for the class named NAME. If NAME does not
764 identify a known class, the hook _objc_lookup_class is called. If
765 this fails, nil is returned. */
766 Class
767 objc_lookup_class (const char *name)
769 return objc_getClass (name);
772 /* Traditional GNU Objective-C Runtime API. Important: this method is
773 called automatically by the compiler while messaging (if using the
774 traditional ABI), so it is worth keeping it fast; don't make it
775 just a wrapper around objc_getClass(). */
776 /* Note that this is roughly equivalent to objc_getRequiredClass(). */
777 /* Get the class object for the class named NAME. If NAME does not
778 identify a known class, the hook _objc_lookup_class is called. If
779 this fails, an error message is issued and the system aborts. */
780 Class
781 objc_get_class (const char *name)
783 Class class;
785 class = class_table_get_safe (name);
787 if (class)
788 return class;
790 if (__objc_get_unknown_class_handler)
791 class = (*__objc_get_unknown_class_handler) (name);
793 if ((!class) && _objc_lookup_class)
794 class = (*_objc_lookup_class) (name);
796 if (class)
797 return class;
799 _objc_abort ("objc runtime: cannot find class %s\n", name);
801 return 0;
804 MetaClass
805 objc_get_meta_class (const char *name)
807 return objc_get_class (name)->class_pointer;
810 /* This function provides a way to enumerate all the classes in the
811 executable. Pass *ENUM_STATE == NULL to start the enumeration. The
812 function will return 0 when there are no more classes.
813 For example:
814 id class;
815 void *es = NULL;
816 while ((class = objc_next_class (&es)))
817 ... do something with class;
819 Class
820 objc_next_class (void **enum_state)
822 Class class;
824 objc_mutex_lock (__objc_runtime_mutex);
826 /* Make sure the table is there. */
827 assert (__class_table_lock);
829 class = class_table_next ((struct class_table_enumerator **) enum_state);
831 objc_mutex_unlock (__objc_runtime_mutex);
833 return class;
836 /* This is used when the implementation of a method changes. It goes
837 through all classes, looking for the ones that have these methods
838 (either method_a or method_b; method_b can be NULL), and reloads
839 the implementation for these. You should call this with the
840 runtime mutex already locked. */
841 void
842 __objc_update_classes_with_methods (struct objc_method *method_a, struct objc_method *method_b)
844 int hash;
846 /* Iterate over all classes. */
847 for (hash = 0; hash < CLASS_TABLE_SIZE; hash++)
849 class_node_ptr node = class_table_array[hash];
851 while (node != NULL)
853 /* Iterate over all methods in the class. */
854 Class class = node->pointer;
855 struct objc_method_list * method_list = class->methods;
857 while (method_list)
859 int i;
861 for (i = 0; i < method_list->method_count; ++i)
863 struct objc_method *method = &method_list->method_list[i];
865 /* If the method is one of the ones we are looking
866 for, update the implementation. */
867 if (method == method_a)
868 sarray_at_put_safe (class->dtable,
869 (sidx) method_a->method_name->sel_id,
870 method_a->method_imp);
872 if (method == method_b)
874 if (method_b != NULL)
875 sarray_at_put_safe (class->dtable,
876 (sidx) method_b->method_name->sel_id,
877 method_b->method_imp);
881 method_list = method_list->method_next;
883 node = node->next;
888 /* Resolve super/subclass links for all classes. The only thing we
889 can be sure of is that the class_pointer for class objects point to
890 the right meta class objects. */
891 void
892 __objc_resolve_class_links (void)
894 struct class_table_enumerator *es = NULL;
895 Class object_class = objc_get_class ("Object");
896 Class class1;
898 assert (object_class);
900 objc_mutex_lock (__objc_runtime_mutex);
902 /* Assign subclass links. */
903 while ((class1 = class_table_next (&es)))
905 /* Make sure we have what we think we have. */
906 assert (CLS_ISCLASS (class1));
907 assert (CLS_ISMETA (class1->class_pointer));
909 /* The class_pointer of all meta classes point to Object's meta
910 class. */
911 class1->class_pointer->class_pointer = object_class->class_pointer;
913 if (! CLS_ISRESOLV (class1))
915 CLS_SETRESOLV (class1);
916 CLS_SETRESOLV (class1->class_pointer);
918 if (class1->super_class)
920 Class a_super_class
921 = objc_get_class ((char *) class1->super_class);
923 assert (a_super_class);
925 DEBUG_PRINTF ("making class connections for: %s\n",
926 class1->name);
928 /* Assign subclass links for superclass. */
929 class1->sibling_class = a_super_class->subclass_list;
930 a_super_class->subclass_list = class1;
932 /* Assign subclass links for meta class of superclass. */
933 if (a_super_class->class_pointer)
935 class1->class_pointer->sibling_class
936 = a_super_class->class_pointer->subclass_list;
937 a_super_class->class_pointer->subclass_list
938 = class1->class_pointer;
941 else /* A root class, make its meta object be a subclass of
942 Object. */
944 class1->class_pointer->sibling_class
945 = object_class->subclass_list;
946 object_class->subclass_list = class1->class_pointer;
951 /* Assign superclass links. */
952 es = NULL;
953 while ((class1 = class_table_next (&es)))
955 Class sub_class;
956 for (sub_class = class1->subclass_list; sub_class;
957 sub_class = sub_class->sibling_class)
959 sub_class->super_class = class1;
960 if (CLS_ISCLASS (sub_class))
961 sub_class->class_pointer->super_class = class1->class_pointer;
965 objc_mutex_unlock (__objc_runtime_mutex);
968 const char *
969 class_getName (Class class_)
971 if (class_ == Nil)
972 return "nil";
974 return class_->name;
977 BOOL
978 class_isMetaClass (Class class_)
980 /* CLS_ISMETA includes the check for Nil class_. */
981 return CLS_ISMETA (class_);
984 /* Even inside libobjc it may be worth using class_getSuperclass
985 instead of accessing class_->super_class directly because it
986 resolves the class links if needed. If you access
987 class_->super_class directly, make sure to deal with the situation
988 where the class is not resolved yet! */
989 Class
990 class_getSuperclass (Class class_)
992 if (class_ == Nil)
993 return Nil;
995 /* Classes that are in construction are not resolved and can not be
996 resolved! */
997 if (CLS_IS_IN_CONSTRUCTION (class_))
998 return Nil;
1000 /* If the class is not resolved yet, super_class would point to a
1001 string (the name of the super class) as opposed to the actual
1002 super class. In that case, we need to resolve the class links
1003 before we can return super_class. */
1004 if (! CLS_ISRESOLV (class_))
1005 __objc_resolve_class_links ();
1007 return class_->super_class;
1011 class_getVersion (Class class_)
1013 if (class_ == Nil)
1014 return 0;
1016 return (int)(class_->version);
1019 void
1020 class_setVersion (Class class_, int version)
1022 if (class_ == Nil)
1023 return;
1025 class_->version = version;
1028 size_t
1029 class_getInstanceSize (Class class_)
1031 if (class_ == Nil)
1032 return 0;
1034 return class_->instance_size;
1037 #define CLASSOF(c) ((c)->class_pointer)
1039 Class
1040 class_pose_as (Class impostor, Class super_class)
1042 if (! CLS_ISRESOLV (impostor))
1043 __objc_resolve_class_links ();
1045 /* Preconditions */
1046 assert (impostor);
1047 assert (super_class);
1048 assert (impostor->super_class == super_class);
1049 assert (CLS_ISCLASS (impostor));
1050 assert (CLS_ISCLASS (super_class));
1051 assert (impostor->instance_size == super_class->instance_size);
1054 Class *subclass = &(super_class->subclass_list);
1056 /* Move subclasses of super_class to impostor. */
1057 while (*subclass)
1059 Class nextSub = (*subclass)->sibling_class;
1061 if (*subclass != impostor)
1063 Class sub = *subclass;
1065 /* Classes */
1066 sub->sibling_class = impostor->subclass_list;
1067 sub->super_class = impostor;
1068 impostor->subclass_list = sub;
1070 /* It will happen that SUB is not a class object if it is
1071 the top of the meta class hierarchy chain (root
1072 meta-class objects inherit their class object). If
1073 that is the case... don't mess with the meta-meta
1074 class. */
1075 if (CLS_ISCLASS (sub))
1077 /* Meta classes */
1078 CLASSOF (sub)->sibling_class =
1079 CLASSOF (impostor)->subclass_list;
1080 CLASSOF (sub)->super_class = CLASSOF (impostor);
1081 CLASSOF (impostor)->subclass_list = CLASSOF (sub);
1085 *subclass = nextSub;
1088 /* Set subclasses of superclass to be impostor only. */
1089 super_class->subclass_list = impostor;
1090 CLASSOF (super_class)->subclass_list = CLASSOF (impostor);
1092 /* Set impostor to have no sibling classes. */
1093 impostor->sibling_class = 0;
1094 CLASSOF (impostor)->sibling_class = 0;
1097 /* Check relationship of impostor and super_class is kept. */
1098 assert (impostor->super_class == super_class);
1099 assert (CLASSOF (impostor)->super_class == CLASSOF (super_class));
1101 /* This is how to update the lookup table. Regardless of what the
1102 keys of the hashtable is, change all values that are superclass
1103 into impostor. */
1105 objc_mutex_lock (__objc_runtime_mutex);
1107 class_table_replace (super_class, impostor);
1109 objc_mutex_unlock (__objc_runtime_mutex);
1111 /* Next, we update the dispatch tables... */
1112 __objc_update_dispatch_table_for_class (CLASSOF (impostor));
1113 __objc_update_dispatch_table_for_class (impostor);
1115 return impostor;