configure.ac (target_tools): Remove target-groff.
[gcc.git] / libobjc / class.c
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.
5
6 Lock-free class table code designed and written from scratch by
7 Nicola Pero, 2001.
8
9 This file is part of GCC.
10
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.
14
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.
19
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.
23
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/>. */
28
29 /* The code in this file critically affects class method invocation
30 speed. This long preamble comment explains why, and the issues
31 involved.
32
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
36
37 array = [NSArray new];
38
39 this gets basically compiled into the equivalent of
40
41 array = [(objc_get_class ("NSArray")) new];
42
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.
46
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:
50
51 Class arrayClass = [NSArray class];
52
53 ... later on ...
54
55 array = [arrayClass new];
56 array = [arrayClass new];
57 array = [arrayClass new];
58
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.
63
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.
72
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.
79
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. */
87
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 */
96
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. */
103
104 /**
105 ** Class Table Internals
106 **/
107
108 /* A node holding a class */
109 typedef struct class_node
110 {
111 struct class_node *next; /* Pointer to next entry on the list.
112 NULL indicates end of list. */
113
114 const char *name; /* The class name string */
115 int length; /* The class name string length */
116 Class pointer; /* The Class pointer */
117
118 } *class_node_ptr;
119
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). */
123
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
135
136 static class_node_ptr class_table_array[CLASS_TABLE_SIZE];
137
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;
144
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.
147
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.
151
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. */
155
156 #define CLASS_TABLE_HASH(INDEX, HASH, CLASS_NAME) \
157 HASH = 0; \
158 for (INDEX = 0; CLASS_NAME[INDEX] != '\0'; INDEX++) \
159 { \
160 HASH = (HASH << 4) ^ (HASH >> 28) ^ CLASS_NAME[INDEX]; \
161 } \
162 \
163 HASH = (HASH ^ (HASH >> 10) ^ (HASH >> 20)) & CLASS_TABLE_MASK;
164
165 /* Setup the table. */
166 static void
167 class_table_setup (void)
168 {
169 /* Start - nothing in the table. */
170 memset (class_table_array, 0, sizeof (class_node_ptr) * CLASS_TABLE_SIZE);
171
172 /* The table writing mutex. */
173 __class_table_lock = objc_mutex_allocate ();
174 }
175
176
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)
181 {
182 int hash, length;
183 class_node_ptr new_node;
184
185 /* Find out the class name's hash and length. */
186 CLASS_TABLE_HASH (length, hash, class_name);
187
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;
193
194 /* Lock the table for modifications. */
195 objc_mutex_lock (__class_table_lock);
196
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;
201
202 objc_mutex_unlock (__class_table_lock);
203 }
204
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)
208 {
209 int hash;
210 class_node_ptr node;
211
212 objc_mutex_lock (__class_table_lock);
213
214 hash = 0;
215 node = class_table_array[hash];
216
217 while (hash < CLASS_TABLE_SIZE)
218 {
219 if (node == NULL)
220 {
221 hash++;
222 if (hash < CLASS_TABLE_SIZE)
223 node = class_table_array[hash];
224 }
225 else
226 {
227 Class class1 = node->pointer;
228
229 if (class1 == old_class_pointer)
230 node->pointer = new_class_pointer;
231
232 node = node->next;
233 }
234 }
235
236 objc_mutex_unlock (__class_table_lock);
237 }
238
239
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)
245 {
246 class_node_ptr node;
247 int length, hash;
248
249 /* Compute length and hash. */
250 CLASS_TABLE_HASH (length, hash, class_name);
251
252 node = class_table_array[hash];
253
254 if (node != NULL)
255 {
256 do
257 {
258 if (node->length == length)
259 {
260 /* Compare the class names. */
261 int i;
262
263 for (i = 0; i < length; i++)
264 {
265 if ((node->name)[i] != class_name[i])
266 break;
267 }
268
269 if (i == length)
270 {
271 /* They are equal! */
272 return node->pointer;
273 }
274 }
275 }
276 while ((node = node->next) != NULL);
277 }
278
279 return Nil;
280 }
281
282 /* Enumerate over the class table. */
283 struct class_table_enumerator
284 {
285 int hash;
286 class_node_ptr node;
287 };
288
289
290 static Class
291 class_table_next (struct class_table_enumerator **e)
292 {
293 struct class_table_enumerator *enumerator = *e;
294 class_node_ptr next;
295
296 if (enumerator == NULL)
297 {
298 *e = objc_malloc (sizeof (struct class_table_enumerator));
299 enumerator = *e;
300 enumerator->hash = 0;
301 enumerator->node = NULL;
302
303 next = class_table_array[enumerator->hash];
304 }
305 else
306 next = enumerator->node->next;
307
308 if (next != NULL)
309 {
310 enumerator->node = next;
311 return enumerator->node->pointer;
312 }
313 else
314 {
315 enumerator->hash++;
316
317 while (enumerator->hash < CLASS_TABLE_SIZE)
318 {
319 next = class_table_array[enumerator->hash];
320 if (next != NULL)
321 {
322 enumerator->node = next;
323 return enumerator->node->pointer;
324 }
325 enumerator->hash++;
326 }
327
328 /* Ok - table finished - done. */
329 objc_free (enumerator);
330 return Nil;
331 }
332 }
333
334 #if 0 /* DEBUGGING FUNCTIONS */
335 /* Debugging function - print the class table. */
336 void
337 class_table_print (void)
338 {
339 int i;
340
341 for (i = 0; i < CLASS_TABLE_SIZE; i++)
342 {
343 class_node_ptr node;
344
345 printf ("%d:\n", i);
346 node = class_table_array[i];
347
348 while (node != NULL)
349 {
350 printf ("\t%s\n", node->name);
351 node = node->next;
352 }
353 }
354 }
355
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)
361 {
362 int i, j;
363 int counter = 0;
364
365 for (i = 0; i < CLASS_TABLE_SIZE; i++)
366 {
367 class_node_ptr node;
368
369 node = class_table_array[i];
370
371 while (node != NULL)
372 {
373 counter++;
374 node = node->next;
375 }
376 if (((i + 1) % 50) == 0)
377 {
378 printf ("%4d:", i + 1);
379 for (j = 0; j < counter; j++)
380 printf ("X");
381
382 printf ("\n");
383 counter = 0;
384 }
385 }
386 printf ("%4d:", i + 1);
387 for (j = 0; j < counter; j++)
388 printf ("X");
389
390 printf ("\n");
391 }
392 #endif /* DEBUGGING FUNCTIONS */
393
394 /**
395 ** Objective-C runtime functions
396 **/
397
398 /* From now on, the only access to the class table data structure
399 should be via the class_table_* functions. */
400
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.
404
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.
409 */
410 Class (*_objc_lookup_class) (const char *name) = 0; /* !T:SAFE */
411
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;
417
418 objc_get_unknown_class_handler
419 objc_setGetUnknownClassHandler (objc_get_unknown_class_handler
420 new_handler)
421 {
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;
426 }
427
428
429 /* True when class links has been resolved. */
430 BOOL __objc_class_links_resolved = NO; /* !T:UNUSED */
431
432
433 void
434 __objc_init_class_tables (void)
435 {
436 /* Allocate the class hash table. */
437
438 if (__class_table_lock)
439 return;
440
441 objc_mutex_lock (__objc_runtime_mutex);
442
443 class_table_setup ();
444
445 objc_mutex_unlock (__objc_runtime_mutex);
446 }
447
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)
453 {
454 Class existing_class;
455
456 objc_mutex_lock (__objc_runtime_mutex);
457
458 /* Make sure the table is there. */
459 assert (__class_table_lock);
460
461 /* Make sure it's not a meta class. */
462 assert (CLS_ISCLASS (class));
463
464 /* Check to see if the class is already in the hash table. */
465 existing_class = class_table_get_safe (class->name);
466
467 if (existing_class)
468 {
469 objc_mutex_unlock (__objc_runtime_mutex);
470 return NO;
471 }
472 else
473 {
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;
477
478 CLS_SETNUMBER (class, class_number);
479 CLS_SETNUMBER (class->class_pointer, class_number);
480
481 ++class_number;
482 class_table_insert (class->name, class);
483
484 objc_mutex_unlock (__objc_runtime_mutex);
485 return YES;
486 }
487 }
488
489 Class
490 objc_getClass (const char *name)
491 {
492 Class class;
493
494 if (name == NULL)
495 return Nil;
496
497 class = class_table_get_safe (name);
498
499 if (class)
500 return class;
501
502 if (__objc_get_unknown_class_handler)
503 return (*__objc_get_unknown_class_handler) (name);
504
505 if (_objc_lookup_class)
506 return (*_objc_lookup_class) (name);
507
508 return Nil;
509 }
510
511 Class
512 objc_lookUpClass (const char *name)
513 {
514 if (name == NULL)
515 return Nil;
516 else
517 return class_table_get_safe (name);
518 }
519
520 Class
521 objc_getMetaClass (const char *name)
522 {
523 Class class = objc_getClass (name);
524
525 if (class)
526 return class->class_pointer;
527 else
528 return Nil;
529 }
530
531 Class
532 objc_getRequiredClass (const char *name)
533 {
534 Class class = objc_getClass (name);
535
536 if (class)
537 return class;
538 else
539 _objc_abort ("objc_getRequiredClass ('%s') failed: class not found\n", name);
540 }
541
542 int
543 objc_getClassList (Class *returnValue, int maxNumberOfClassesToReturn)
544 {
545 /* Iterate over all entries in the table. */
546 int hash, count = 0;
547
548 for (hash = 0; hash < CLASS_TABLE_SIZE; hash++)
549 {
550 class_node_ptr node = class_table_array[hash];
551
552 while (node != NULL)
553 {
554 if (returnValue)
555 {
556 if (count < maxNumberOfClassesToReturn)
557 returnValue[count] = node->pointer;
558 else
559 return count;
560 }
561 count++;
562 node = node->next;
563 }
564 }
565
566 return count;
567 }
568
569 Class
570 objc_allocateClassPair (Class super_class, const char *class_name, size_t extraBytes)
571 {
572 Class new_class;
573 Class new_meta_class;
574
575 if (class_name == NULL)
576 return Nil;
577
578 if (objc_getClass (class_name))
579 return Nil;
580
581 if (super_class)
582 {
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;
589 }
590
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);
598
599 /* We create an unresolved class, similar to one generated by the
600 compiler. It will be resolved later when we register it.
601
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;
606
607 if (super_class)
608 {
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;
615 }
616 else
617 {
618 new_class->super_class = (void *)0;
619 new_meta_class->super_class = (void *)0;
620 }
621
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;
625
626 new_class->version = 0;
627 new_meta_class->version = 0;
628
629 new_class->info = _CLS_CLASS | _CLS_IN_CONSTRUCTION;
630 new_meta_class->info = _CLS_META | _CLS_IN_CONSTRUCTION;
631
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);
637
638 return new_class;
639 }
640
641 void
642 objc_registerClassPair (Class class_)
643 {
644 if (class_ == Nil)
645 return;
646
647 if ((! CLS_ISCLASS (class_)) || (! CLS_IS_IN_CONSTRUCTION (class_)))
648 return;
649
650 if ((! CLS_ISMETA (class_->class_pointer)) || (! CLS_IS_IN_CONSTRUCTION (class_->class_pointer)))
651 return;
652
653 objc_mutex_lock (__objc_runtime_mutex);
654
655 if (objc_getClass (class_->name))
656 {
657 objc_mutex_unlock (__objc_runtime_mutex);
658 return;
659 }
660
661 CLS_SET_NOT_IN_CONSTRUCTION (class_);
662 CLS_SET_NOT_IN_CONSTRUCTION (class_->class_pointer);
663
664 __objc_init_class (class_);
665
666 /* Resolve class links immediately. No point in waiting. */
667 __objc_resolve_class_links ();
668
669 objc_mutex_unlock (__objc_runtime_mutex);
670 }
671
672 void
673 objc_disposeClassPair (Class class_)
674 {
675 if (class_ == Nil)
676 return;
677
678 if ((! CLS_ISCLASS (class_)) || (! CLS_IS_IN_CONSTRUCTION (class_)))
679 return;
680
681 if ((! CLS_ISMETA (class_->class_pointer)) || (! CLS_IS_IN_CONSTRUCTION (class_->class_pointer)))
682 return;
683
684 /* Undo any class_addIvar(). */
685 if (class_->ivars)
686 {
687 int i;
688 for (i = 0; i < class_->ivars->ivar_count; i++)
689 {
690 struct objc_ivar *ivar = &(class_->ivars->ivar_list[i]);
691
692 objc_free ((char *)ivar->ivar_name);
693 objc_free ((char *)ivar->ivar_type);
694 }
695
696 objc_free (class_->ivars);
697 }
698
699 /* Undo any class_addMethod(). */
700 if (class_->methods)
701 {
702 struct objc_method_list *list = class_->methods;
703 while (list)
704 {
705 int i;
706 struct objc_method_list *next = list->method_next;
707
708 for (i = 0; i < list->method_count; i++)
709 {
710 struct objc_method *method = &(list->method_list[i]);
711
712 objc_free ((char *)method->method_name);
713 objc_free ((char *)method->method_types);
714 }
715
716 objc_free (list);
717 list = next;
718 }
719 }
720
721 /* Undo any class_addProtocol(). */
722 if (class_->protocols)
723 {
724 struct objc_protocol_list *list = class_->protocols;
725 while (list)
726 {
727 struct objc_protocol_list *next = list->next;
728
729 objc_free (list);
730 list = next;
731 }
732 }
733
734 /* Undo any class_addMethod() on the meta-class. */
735 if (class_->class_pointer->methods)
736 {
737 struct objc_method_list *list = class_->class_pointer->methods;
738 while (list)
739 {
740 int i;
741 struct objc_method_list *next = list->method_next;
742
743 for (i = 0; i < list->method_count; i++)
744 {
745 struct objc_method *method = &(list->method_list[i]);
746
747 objc_free ((char *)method->method_name);
748 objc_free ((char *)method->method_types);
749 }
750
751 objc_free (list);
752 list = next;
753 }
754 }
755
756 /* Undo objc_allocateClassPair(). */
757 objc_free ((char *)(class_->name));
758 objc_free (class_->class_pointer);
759 objc_free (class_);
760 }
761
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)
768 {
769 return objc_getClass (name);
770 }
771
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)
782 {
783 Class class;
784
785 class = class_table_get_safe (name);
786
787 if (class)
788 return class;
789
790 if (__objc_get_unknown_class_handler)
791 class = (*__objc_get_unknown_class_handler) (name);
792
793 if ((!class) && _objc_lookup_class)
794 class = (*_objc_lookup_class) (name);
795
796 if (class)
797 return class;
798
799 _objc_abort ("objc runtime: cannot find class %s\n", name);
800
801 return 0;
802 }
803
804 MetaClass
805 objc_get_meta_class (const char *name)
806 {
807 return objc_get_class (name)->class_pointer;
808 }
809
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;
818 */
819 Class
820 objc_next_class (void **enum_state)
821 {
822 Class class;
823
824 objc_mutex_lock (__objc_runtime_mutex);
825
826 /* Make sure the table is there. */
827 assert (__class_table_lock);
828
829 class = class_table_next ((struct class_table_enumerator **) enum_state);
830
831 objc_mutex_unlock (__objc_runtime_mutex);
832
833 return class;
834 }
835
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)
843 {
844 int hash;
845
846 /* Iterate over all classes. */
847 for (hash = 0; hash < CLASS_TABLE_SIZE; hash++)
848 {
849 class_node_ptr node = class_table_array[hash];
850
851 while (node != NULL)
852 {
853 /* Iterate over all methods in the class. */
854 Class class = node->pointer;
855 struct objc_method_list * method_list = class->methods;
856
857 while (method_list)
858 {
859 int i;
860
861 for (i = 0; i < method_list->method_count; ++i)
862 {
863 struct objc_method *method = &method_list->method_list[i];
864
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);
871
872 if (method == method_b)
873 {
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);
878 }
879 }
880
881 method_list = method_list->method_next;
882 }
883 node = node->next;
884 }
885 }
886 }
887
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)
893 {
894 struct class_table_enumerator *es = NULL;
895 Class object_class = objc_get_class ("Object");
896 Class class1;
897
898 assert (object_class);
899
900 objc_mutex_lock (__objc_runtime_mutex);
901
902 /* Assign subclass links. */
903 while ((class1 = class_table_next (&es)))
904 {
905 /* Make sure we have what we think we have. */
906 assert (CLS_ISCLASS (class1));
907 assert (CLS_ISMETA (class1->class_pointer));
908
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;
912
913 if (! CLS_ISRESOLV (class1))
914 {
915 CLS_SETRESOLV (class1);
916 CLS_SETRESOLV (class1->class_pointer);
917
918 if (class1->super_class)
919 {
920 Class a_super_class
921 = objc_get_class ((char *) class1->super_class);
922
923 assert (a_super_class);
924
925 DEBUG_PRINTF ("making class connections for: %s\n",
926 class1->name);
927
928 /* Assign subclass links for superclass. */
929 class1->sibling_class = a_super_class->subclass_list;
930 a_super_class->subclass_list = class1;
931
932 /* Assign subclass links for meta class of superclass. */
933 if (a_super_class->class_pointer)
934 {
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;
939 }
940 }
941 else /* A root class, make its meta object be a subclass of
942 Object. */
943 {
944 class1->class_pointer->sibling_class
945 = object_class->subclass_list;
946 object_class->subclass_list = class1->class_pointer;
947 }
948 }
949 }
950
951 /* Assign superclass links. */
952 es = NULL;
953 while ((class1 = class_table_next (&es)))
954 {
955 Class sub_class;
956 for (sub_class = class1->subclass_list; sub_class;
957 sub_class = sub_class->sibling_class)
958 {
959 sub_class->super_class = class1;
960 if (CLS_ISCLASS (sub_class))
961 sub_class->class_pointer->super_class = class1->class_pointer;
962 }
963 }
964
965 objc_mutex_unlock (__objc_runtime_mutex);
966 }
967
968 const char *
969 class_getName (Class class_)
970 {
971 if (class_ == Nil)
972 return "nil";
973
974 return class_->name;
975 }
976
977 BOOL
978 class_isMetaClass (Class class_)
979 {
980 /* CLS_ISMETA includes the check for Nil class_. */
981 return CLS_ISMETA (class_);
982 }
983
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_)
991 {
992 if (class_ == Nil)
993 return Nil;
994
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;
999
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 ();
1006
1007 return class_->super_class;
1008 }
1009
1010 int
1011 class_getVersion (Class class_)
1012 {
1013 if (class_ == Nil)
1014 return 0;
1015
1016 return (int)(class_->version);
1017 }
1018
1019 void
1020 class_setVersion (Class class_, int version)
1021 {
1022 if (class_ == Nil)
1023 return;
1024
1025 class_->version = version;
1026 }
1027
1028 size_t
1029 class_getInstanceSize (Class class_)
1030 {
1031 if (class_ == Nil)
1032 return 0;
1033
1034 return class_->instance_size;
1035 }
1036
1037 #define CLASSOF(c) ((c)->class_pointer)
1038
1039 Class
1040 class_pose_as (Class impostor, Class super_class)
1041 {
1042 if (! CLS_ISRESOLV (impostor))
1043 __objc_resolve_class_links ();
1044
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);
1052
1053 {
1054 Class *subclass = &(super_class->subclass_list);
1055
1056 /* Move subclasses of super_class to impostor. */
1057 while (*subclass)
1058 {
1059 Class nextSub = (*subclass)->sibling_class;
1060
1061 if (*subclass != impostor)
1062 {
1063 Class sub = *subclass;
1064
1065 /* Classes */
1066 sub->sibling_class = impostor->subclass_list;
1067 sub->super_class = impostor;
1068 impostor->subclass_list = sub;
1069
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))
1076 {
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);
1082 }
1083 }
1084
1085 *subclass = nextSub;
1086 }
1087
1088 /* Set subclasses of superclass to be impostor only. */
1089 super_class->subclass_list = impostor;
1090 CLASSOF (super_class)->subclass_list = CLASSOF (impostor);
1091
1092 /* Set impostor to have no sibling classes. */
1093 impostor->sibling_class = 0;
1094 CLASSOF (impostor)->sibling_class = 0;
1095 }
1096
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));
1100
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. */
1104
1105 objc_mutex_lock (__objc_runtime_mutex);
1106
1107 class_table_replace (super_class, impostor);
1108
1109 objc_mutex_unlock (__objc_runtime_mutex);
1110
1111 /* Next, we update the dispatch tables... */
1112 __objc_update_dispatch_table_for_class (CLASSOF (impostor));
1113 __objc_update_dispatch_table_for_class (impostor);
1114
1115 return impostor;
1116 }