]> git.ipfire.org Git - thirdparty/gcc.git/blob - libobjc/class.c
In libobjc/: 2010-12-19 Nicola Pero <nicola.pero@meta-innovation.com>
[thirdparty/gcc.git] / libobjc / class.c
1 /* GNU Objective C Runtime class related functions
2 Copyright (C) 1993, 1995, 1996, 1997, 2001, 2002, 2009
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. */
450 void
451 __objc_add_class_to_hash (Class class)
452 {
453 Class h_class;
454
455 objc_mutex_lock (__objc_runtime_mutex);
456
457 /* Make sure the table is there. */
458 assert (__class_table_lock);
459
460 /* Make sure it's not a meta class. */
461 assert (CLS_ISCLASS (class));
462
463 /* Check to see if the class is already in the hash table. */
464 h_class = class_table_get_safe (class->name);
465 if (! h_class)
466 {
467 /* The class isn't in the hash table. Add the class and assign a class
468 number. */
469 static unsigned int class_number = 1;
470
471 CLS_SETNUMBER (class, class_number);
472 CLS_SETNUMBER (class->class_pointer, class_number);
473
474 ++class_number;
475 class_table_insert (class->name, class);
476 }
477
478 objc_mutex_unlock (__objc_runtime_mutex);
479 }
480
481 Class
482 objc_getClass (const char *name)
483 {
484 Class class;
485
486 if (name == NULL)
487 return Nil;
488
489 class = class_table_get_safe (name);
490
491 if (class)
492 return class;
493
494 if (__objc_get_unknown_class_handler)
495 return (*__objc_get_unknown_class_handler) (name);
496
497 if (_objc_lookup_class)
498 return (*_objc_lookup_class) (name);
499
500 return Nil;
501 }
502
503 Class
504 objc_lookupClass (const char *name)
505 {
506 if (name == NULL)
507 return Nil;
508 else
509 return class_table_get_safe (name);
510 }
511
512 Class
513 objc_getMetaClass (const char *name)
514 {
515 Class class = objc_getClass (name);
516
517 if (class)
518 return class->class_pointer;
519 else
520 return Nil;
521 }
522
523 Class
524 objc_getRequiredClass (const char *name)
525 {
526 Class class = objc_getClass (name);
527
528 if (class)
529 return class;
530 else
531 _objc_abort ("objc_getRequiredClass ('%s') failed: class not found\n", name);
532 }
533
534 int
535 objc_getClassList (Class *returnValue, int maxNumberOfClassesToReturn)
536 {
537 /* Iterate over all entries in the table. */
538 int hash, count = 0;
539
540 for (hash = 0; hash < CLASS_TABLE_SIZE; hash++)
541 {
542 class_node_ptr node = class_table_array[hash];
543
544 while (node != NULL)
545 {
546 if (returnValue)
547 {
548 if (count < maxNumberOfClassesToReturn)
549 returnValue[count] = node->pointer;
550 else
551 return count;
552 }
553 count++;
554 node = node->next;
555 }
556 }
557
558 return count;
559 }
560
561 Class
562 objc_allocateClassPair (Class super_class, const char *class_name, size_t extraBytes)
563 {
564 Class new_class;
565 Class new_meta_class;
566
567 if (class_name == NULL)
568 return Nil;
569
570 if (objc_getClass (class_name))
571 return Nil;
572
573 if (super_class)
574 {
575 /* If you want to build a hierarchy of classes, you need to
576 build and register them one at a time. The risk is that you
577 are able to cause confusion by registering a subclass before
578 the superclass or similar. */
579 if (CLS_IS_IN_CONSTRUCTION (super_class))
580 return Nil;
581 }
582
583 /* Technically, we should create the metaclass first, then use
584 class_createInstance() to create the class. That complication
585 would be relevant if we had class variables, but we don't, so we
586 just ignore it and create everything directly and assume all
587 classes have the same size. */
588 new_class = objc_calloc (1, sizeof (struct objc_class) + extraBytes);
589 new_meta_class = objc_calloc (1, sizeof (struct objc_class) + extraBytes);
590
591 /* We create an unresolved class, similar to one generated by the
592 compiler. It will be resolved later when we register it.
593
594 Note how the metaclass details are not that important; when the
595 class is resolved, the ones that matter will be fixed up. */
596 new_class->class_pointer = new_meta_class;
597 new_meta_class->class_pointer = 0;
598
599 if (super_class)
600 {
601 /* Force the name of the superclass in place of the link to the
602 actual superclass, which will be put there when the class is
603 resolved. */
604 const char *super_class_name = class_getName (super_class);
605 new_class->super_class = (void *)super_class_name;
606 new_meta_class->super_class = (void *)super_class_name;
607 }
608 else
609 {
610 new_class->super_class = (void *)0;
611 new_meta_class->super_class = (void *)0;
612 }
613
614 new_class->name = objc_malloc (strlen (class_name) + 1);
615 strcpy ((char*)new_class->name, class_name);
616 new_meta_class->name = new_class->name;
617
618 new_class->version = 0;
619 new_meta_class->version = 0;
620
621 new_class->info = _CLS_CLASS | _CLS_IN_CONSTRUCTION;
622 new_meta_class->info = _CLS_META | _CLS_IN_CONSTRUCTION;
623
624 if (super_class)
625 new_class->instance_size = super_class->instance_size;
626 else
627 new_class->instance_size = 0;
628 new_meta_class->instance_size = sizeof (struct objc_class);
629
630 return new_class;
631 }
632
633 void
634 objc_registerClassPair (Class class_)
635 {
636 if (class_ == Nil)
637 return;
638
639 if ((! CLS_ISCLASS (class_)) || (! CLS_IS_IN_CONSTRUCTION (class_)))
640 return;
641
642 if ((! CLS_ISMETA (class_->class_pointer)) || (! CLS_IS_IN_CONSTRUCTION (class_->class_pointer)))
643 return;
644
645 objc_mutex_lock (__objc_runtime_mutex);
646
647 if (objc_getClass (class_->name))
648 {
649 objc_mutex_unlock (__objc_runtime_mutex);
650 return;
651 }
652
653 CLS_SET_NOT_IN_CONSTRUCTION (class_);
654 CLS_SET_NOT_IN_CONSTRUCTION (class_->class_pointer);
655
656 __objc_init_class (class_);
657
658 /* Resolve class links immediately. No point in waiting. */
659 __objc_resolve_class_links ();
660
661 objc_mutex_unlock (__objc_runtime_mutex);
662 }
663
664 void
665 objc_disposeClassPair (Class class_)
666 {
667 if (class_ == Nil)
668 return;
669
670 if ((! CLS_ISCLASS (class_)) || (! CLS_IS_IN_CONSTRUCTION (class_)))
671 return;
672
673 if ((! CLS_ISMETA (class_->class_pointer)) || (! CLS_IS_IN_CONSTRUCTION (class_->class_pointer)))
674 return;
675
676 /* Undo any class_addIvar(). */
677 if (class_->ivars)
678 {
679 int i;
680 for (i = 0; i < class_->ivars->ivar_count; i++)
681 {
682 struct objc_ivar *ivar = &(class_->ivars->ivar_list[i]);
683
684 objc_free ((char *)ivar->ivar_name);
685 objc_free ((char *)ivar->ivar_type);
686 }
687
688 objc_free (class_->ivars);
689 }
690
691 /* Undo any class_addMethod(). */
692 if (class_->methods)
693 {
694 struct objc_method_list *list = class_->methods;
695 while (list)
696 {
697 int i;
698 struct objc_method_list *next = list->method_next;
699
700 for (i = 0; i < list->method_count; i++)
701 {
702 struct objc_method *method = &(list->method_list[i]);
703
704 objc_free ((char *)method->method_name);
705 objc_free ((char *)method->method_types);
706 }
707
708 objc_free (list);
709 list = next;
710 }
711 }
712
713 /* Undo any class_addProtocol(). */
714 if (class_->protocols)
715 {
716 struct objc_protocol_list *list = class_->protocols;
717 while (list)
718 {
719 struct objc_protocol_list *next = list->next;
720
721 objc_free (list);
722 list = next;
723 }
724 }
725
726 /* Undo any class_addMethod() on the meta-class. */
727 if (class_->class_pointer->methods)
728 {
729 struct objc_method_list *list = class_->class_pointer->methods;
730 while (list)
731 {
732 int i;
733 struct objc_method_list *next = list->method_next;
734
735 for (i = 0; i < list->method_count; i++)
736 {
737 struct objc_method *method = &(list->method_list[i]);
738
739 objc_free ((char *)method->method_name);
740 objc_free ((char *)method->method_types);
741 }
742
743 objc_free (list);
744 list = next;
745 }
746 }
747
748 /* Undo objc_allocateClassPair(). */
749 objc_free ((char *)(class_->name));
750 objc_free (class_->class_pointer);
751 objc_free (class_);
752 }
753
754 /* Traditional GNU Objective-C Runtime API. */
755 /* Get the class object for the class named NAME. If NAME does not
756 identify a known class, the hook _objc_lookup_class is called. If
757 this fails, nil is returned. */
758 Class
759 objc_lookup_class (const char *name)
760 {
761 return objc_getClass (name);
762 }
763
764 /* Traditional GNU Objective-C Runtime API. Important: this method is
765 called automatically by the compiler while messaging (if using the
766 traditional ABI), so it is worth keeping it fast; don't make it
767 just a wrapper around objc_getClass(). */
768 /* Note that this is roughly equivalent to objc_getRequiredClass(). */
769 /* Get the class object for the class named NAME. If NAME does not
770 identify a known class, the hook _objc_lookup_class is called. If
771 this fails, an error message is issued and the system aborts. */
772 Class
773 objc_get_class (const char *name)
774 {
775 Class class;
776
777 class = class_table_get_safe (name);
778
779 if (class)
780 return class;
781
782 if (__objc_get_unknown_class_handler)
783 class = (*__objc_get_unknown_class_handler) (name);
784
785 if ((!class) && _objc_lookup_class)
786 class = (*_objc_lookup_class) (name);
787
788 if (class)
789 return class;
790
791 _objc_abort ("objc runtime: cannot find class %s\n", name);
792
793 return 0;
794 }
795
796 MetaClass
797 objc_get_meta_class (const char *name)
798 {
799 return objc_get_class (name)->class_pointer;
800 }
801
802 /* This function provides a way to enumerate all the classes in the
803 executable. Pass *ENUM_STATE == NULL to start the enumeration. The
804 function will return 0 when there are no more classes.
805 For example:
806 id class;
807 void *es = NULL;
808 while ((class = objc_next_class (&es)))
809 ... do something with class;
810 */
811 Class
812 objc_next_class (void **enum_state)
813 {
814 Class class;
815
816 objc_mutex_lock (__objc_runtime_mutex);
817
818 /* Make sure the table is there. */
819 assert (__class_table_lock);
820
821 class = class_table_next ((struct class_table_enumerator **) enum_state);
822
823 objc_mutex_unlock (__objc_runtime_mutex);
824
825 return class;
826 }
827
828 /* This is used when the implementation of a method changes. It goes
829 through all classes, looking for the ones that have these methods
830 (either method_a or method_b; method_b can be NULL), and reloads
831 the implementation for these. You should call this with the
832 runtime mutex already locked. */
833 void
834 __objc_update_classes_with_methods (struct objc_method *method_a, struct objc_method *method_b)
835 {
836 int hash;
837
838 /* Iterate over all classes. */
839 for (hash = 0; hash < CLASS_TABLE_SIZE; hash++)
840 {
841 class_node_ptr node = class_table_array[hash];
842
843 while (node != NULL)
844 {
845 /* Iterate over all methods in the class. */
846 Class class = node->pointer;
847 struct objc_method_list * method_list = class->methods;
848
849 while (method_list)
850 {
851 int i;
852
853 for (i = 0; i < method_list->method_count; ++i)
854 {
855 struct objc_method *method = &method_list->method_list[i];
856
857 /* If the method is one of the ones we are looking
858 for, update the implementation. */
859 if (method == method_a)
860 sarray_at_put_safe (class->dtable,
861 (sidx) method_a->method_name->sel_id,
862 method_a->method_imp);
863
864 if (method == method_b)
865 {
866 if (method_b != NULL)
867 sarray_at_put_safe (class->dtable,
868 (sidx) method_b->method_name->sel_id,
869 method_b->method_imp);
870 }
871 }
872
873 method_list = method_list->method_next;
874 }
875 node = node->next;
876 }
877 }
878 }
879
880 /* Resolve super/subclass links for all classes. The only thing we
881 can be sure of is that the class_pointer for class objects point to
882 the right meta class objects. */
883 void
884 __objc_resolve_class_links (void)
885 {
886 struct class_table_enumerator *es = NULL;
887 Class object_class = objc_get_class ("Object");
888 Class class1;
889
890 assert (object_class);
891
892 objc_mutex_lock (__objc_runtime_mutex);
893
894 /* Assign subclass links. */
895 while ((class1 = class_table_next (&es)))
896 {
897 /* Make sure we have what we think we have. */
898 assert (CLS_ISCLASS (class1));
899 assert (CLS_ISMETA (class1->class_pointer));
900
901 /* The class_pointer of all meta classes point to Object's meta
902 class. */
903 class1->class_pointer->class_pointer = object_class->class_pointer;
904
905 if (! CLS_ISRESOLV (class1))
906 {
907 CLS_SETRESOLV (class1);
908 CLS_SETRESOLV (class1->class_pointer);
909
910 if (class1->super_class)
911 {
912 Class a_super_class
913 = objc_get_class ((char *) class1->super_class);
914
915 assert (a_super_class);
916
917 DEBUG_PRINTF ("making class connections for: %s\n",
918 class1->name);
919
920 /* Assign subclass links for superclass. */
921 class1->sibling_class = a_super_class->subclass_list;
922 a_super_class->subclass_list = class1;
923
924 /* Assign subclass links for meta class of superclass. */
925 if (a_super_class->class_pointer)
926 {
927 class1->class_pointer->sibling_class
928 = a_super_class->class_pointer->subclass_list;
929 a_super_class->class_pointer->subclass_list
930 = class1->class_pointer;
931 }
932 }
933 else /* A root class, make its meta object be a subclass of
934 Object. */
935 {
936 class1->class_pointer->sibling_class
937 = object_class->subclass_list;
938 object_class->subclass_list = class1->class_pointer;
939 }
940 }
941 }
942
943 /* Assign superclass links. */
944 es = NULL;
945 while ((class1 = class_table_next (&es)))
946 {
947 Class sub_class;
948 for (sub_class = class1->subclass_list; sub_class;
949 sub_class = sub_class->sibling_class)
950 {
951 sub_class->super_class = class1;
952 if (CLS_ISCLASS (sub_class))
953 sub_class->class_pointer->super_class = class1->class_pointer;
954 }
955 }
956
957 objc_mutex_unlock (__objc_runtime_mutex);
958 }
959
960 const char *
961 class_getName (Class class_)
962 {
963 if (class_ == Nil)
964 return "nil";
965
966 return class_->name;
967 }
968
969 BOOL
970 class_isMetaClass (Class class_)
971 {
972 /* CLS_ISMETA includes the check for Nil class_. */
973 return CLS_ISMETA (class_);
974 }
975
976 /* Even inside libobjc it may be worth using class_getSuperclass
977 instead of accessing class_->super_class directly because it
978 resolves the class links if needed. If you access
979 class_->super_class directly, make sure to deal with the situation
980 where the class is not resolved yet! */
981 Class
982 class_getSuperclass (Class class_)
983 {
984 if (class_ == Nil)
985 return Nil;
986
987 /* Classes that are in construction are not resolved and can not be
988 resolved! */
989 if (CLS_IS_IN_CONSTRUCTION (class_))
990 return Nil;
991
992 /* If the class is not resolved yet, super_class would point to a
993 string (the name of the super class) as opposed to the actual
994 super class. In that case, we need to resolve the class links
995 before we can return super_class. */
996 if (! CLS_ISRESOLV (class_))
997 __objc_resolve_class_links ();
998
999 return class_->super_class;
1000 }
1001
1002 int
1003 class_getVersion (Class class_)
1004 {
1005 if (class_ == Nil)
1006 return 0;
1007
1008 return (int)(class_->version);
1009 }
1010
1011 void
1012 class_setVersion (Class class_, int version)
1013 {
1014 if (class_ == Nil)
1015 return;
1016
1017 class_->version = version;
1018 }
1019
1020 size_t
1021 class_getInstanceSize (Class class_)
1022 {
1023 if (class_ == Nil)
1024 return 0;
1025
1026 return class_->instance_size;
1027 }
1028
1029 #define CLASSOF(c) ((c)->class_pointer)
1030
1031 Class
1032 class_pose_as (Class impostor, Class super_class)
1033 {
1034 if (! CLS_ISRESOLV (impostor))
1035 __objc_resolve_class_links ();
1036
1037 /* Preconditions */
1038 assert (impostor);
1039 assert (super_class);
1040 assert (impostor->super_class == super_class);
1041 assert (CLS_ISCLASS (impostor));
1042 assert (CLS_ISCLASS (super_class));
1043 assert (impostor->instance_size == super_class->instance_size);
1044
1045 {
1046 Class *subclass = &(super_class->subclass_list);
1047
1048 /* Move subclasses of super_class to impostor. */
1049 while (*subclass)
1050 {
1051 Class nextSub = (*subclass)->sibling_class;
1052
1053 if (*subclass != impostor)
1054 {
1055 Class sub = *subclass;
1056
1057 /* Classes */
1058 sub->sibling_class = impostor->subclass_list;
1059 sub->super_class = impostor;
1060 impostor->subclass_list = sub;
1061
1062 /* It will happen that SUB is not a class object if it is
1063 the top of the meta class hierarchy chain (root
1064 meta-class objects inherit their class object). If
1065 that is the case... don't mess with the meta-meta
1066 class. */
1067 if (CLS_ISCLASS (sub))
1068 {
1069 /* Meta classes */
1070 CLASSOF (sub)->sibling_class =
1071 CLASSOF (impostor)->subclass_list;
1072 CLASSOF (sub)->super_class = CLASSOF (impostor);
1073 CLASSOF (impostor)->subclass_list = CLASSOF (sub);
1074 }
1075 }
1076
1077 *subclass = nextSub;
1078 }
1079
1080 /* Set subclasses of superclass to be impostor only. */
1081 super_class->subclass_list = impostor;
1082 CLASSOF (super_class)->subclass_list = CLASSOF (impostor);
1083
1084 /* Set impostor to have no sibling classes. */
1085 impostor->sibling_class = 0;
1086 CLASSOF (impostor)->sibling_class = 0;
1087 }
1088
1089 /* Check relationship of impostor and super_class is kept. */
1090 assert (impostor->super_class == super_class);
1091 assert (CLASSOF (impostor)->super_class == CLASSOF (super_class));
1092
1093 /* This is how to update the lookup table. Regardless of what the
1094 keys of the hashtable is, change all values that are superclass
1095 into impostor. */
1096
1097 objc_mutex_lock (__objc_runtime_mutex);
1098
1099 class_table_replace (super_class, impostor);
1100
1101 objc_mutex_unlock (__objc_runtime_mutex);
1102
1103 /* Next, we update the dispatch tables... */
1104 __objc_update_dispatch_table_for_class (CLASSOF (impostor));
1105 __objc_update_dispatch_table_for_class (impostor);
1106
1107 return impostor;
1108 }