]> git.saurik.com Git - apple/objc4.git/blob - runtime/objc-class-old.m
objc4-437.1.tar.gz
[apple/objc4.git] / runtime / objc-class-old.m
1 /*
2 * Copyright (c) 1999-2007 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 /***********************************************************************
25 * objc-class-old.m
26 * Support for old-ABI classes, methods, and categories.
27 **********************************************************************/
28
29 #if !__OBJC2__
30
31 #define OLD 1
32 #include "objc-private.h"
33
34 // Freed objects have their isa set to point to this dummy class.
35 // This avoids the need to check for Nil classes in the messenger.
36 static const struct old_class freedObjectClass =
37 {
38 Nil, // isa
39 Nil, // super_class
40 "FREED(id)", // name
41 0, // version
42 0, // info
43 0, // instance_size
44 NULL, // ivars
45 NULL, // methodLists
46 (Cache) &_objc_empty_cache, // cache
47 NULL // protocols
48 };
49
50 static const struct old_class nonexistentObjectClass =
51 {
52 Nil, // isa
53 Nil, // super_class
54 "NONEXISTENT(id)", // name
55 0, // version
56 0, // info
57 0, // instance_size
58 NULL, // ivars
59 NULL, // methodLists
60 (Cache) &_objc_empty_cache, // cache
61 NULL // protocols
62 };
63
64
65 /***********************************************************************
66 * _class_getFreedObjectClass. Return a pointer to the dummy freed
67 * object class. Freed objects get their isa pointers replaced with
68 * a pointer to the freedObjectClass, so that we can catch usages of
69 * the freed object.
70 **********************************************************************/
71 static Class _class_getFreedObjectClass(void)
72 {
73 return (Class)&freedObjectClass;
74 }
75
76
77 /***********************************************************************
78 * _class_getNonexistentClass. Return a pointer to the dummy nonexistent
79 * object class. This is used when, for example, mapping the class
80 * refs for an image, and the class can not be found, so that we can
81 * catch later uses of the non-existent class.
82 **********************************************************************/
83 __private_extern__ Class _class_getNonexistentObjectClass(void)
84 {
85 return (Class)&nonexistentObjectClass;
86 }
87
88
89 /***********************************************************************
90 * _objc_getFreedObjectClass. Return a pointer to the dummy freed
91 * object class. Freed objects get their isa pointers replaced with
92 * a pointer to the freedObjectClass, so that we can catch usages of
93 * the freed object.
94 **********************************************************************/
95 Class _objc_getFreedObjectClass(void)
96 {
97 return _class_getFreedObjectClass();
98 }
99
100
101 static void allocateExt(struct old_class *cls)
102 {
103 if (! (cls->info & CLS_EXT)) {
104 _objc_inform("class '%s' needs to be recompiled", cls->name);
105 return;
106 }
107 if (!cls->ext) {
108 uint32_t size = (uint32_t)sizeof(struct old_class_ext);
109 cls->ext = _calloc_internal(size, 1);
110 cls->ext->size = size;
111 }
112 }
113
114
115 static inline struct old_method *_findNamedMethodInList(struct old_method_list * mlist, const char *meth_name) {
116 int i;
117 if (!mlist) return NULL;
118 for (i = 0; i < mlist->method_count; i++) {
119 struct old_method *m = &mlist->method_list[i];
120 if (m->method_name == (SEL)kRTAddress_ignoredSelector) continue;
121 if (*((const char *)m->method_name) == *meth_name && 0 == strcmp((const char *)(m->method_name), meth_name)) {
122 return m;
123 }
124 }
125 return NULL;
126 }
127
128
129 /***********************************************************************
130 * Method list fixup markers.
131 * mlist->obsolete == fixed_up_method_list marks method lists with real SELs
132 * versus method lists with un-uniqued char*.
133 * PREOPTIMIZED VERSION:
134 * Fixed-up method lists get mlist->obsolete == OBJC_FIXED_UP
135 * dyld shared cache sets this for method lists it preoptimizes.
136 * UN-PREOPTIMIZED VERSION
137 * Fixed-up method lists get mlist->obsolete == OBJC_FIXED_UP_outside_dyld
138 * dyld shared cache uses OBJC_FIXED_UP, but those aren't trusted.
139 **********************************************************************/
140 #define OBJC_FIXED_UP ((void *)1771)
141 #define OBJC_FIXED_UP_outside_dyld ((void *)1773)
142 static void *fixed_up_method_list = OBJC_FIXED_UP;
143
144 // sel_init() decided that selectors in the dyld shared cache are untrustworthy
145 __private_extern__ void disableSelectorPreoptimization(void)
146 {
147 fixed_up_method_list = OBJC_FIXED_UP_outside_dyld;
148 }
149
150 /***********************************************************************
151 * fixupSelectorsInMethodList
152 * Uniques selectors in the given method list.
153 * Also replaces imps for GC-ignored selectors
154 * The given method list must be non-NULL and not already fixed-up.
155 * If the class was loaded from a bundle:
156 * fixes up the given list in place with heap-allocated selector strings
157 * If the class was not from a bundle:
158 * allocates a copy of the method list, fixes up the copy, and returns
159 * the copy. The given list is unmodified.
160 *
161 * If cls is already in use, methodListLock must be held by the caller.
162 **********************************************************************/
163 static struct old_method_list *fixupSelectorsInMethodList(struct old_class *cls, struct old_method_list *mlist)
164 {
165 int i;
166 size_t size;
167 struct old_method *method;
168 struct old_method_list *old_mlist;
169
170 if ( ! mlist ) return NULL;
171 if ( mlist->obsolete == fixed_up_method_list ) {
172 // method list OK
173 } else {
174 BOOL isBundle = (cls->info & CLS_FROM_BUNDLE) ? YES : NO;
175 if (!isBundle) {
176 old_mlist = mlist;
177 size = sizeof(struct old_method_list) - sizeof(struct old_method) + old_mlist->method_count * sizeof(struct old_method);
178 mlist = _malloc_internal(size);
179 memmove(mlist, old_mlist, size);
180 } else {
181 // Mach-O bundles are fixed up in place.
182 // This prevents leaks when a bundle is unloaded.
183 }
184 sel_lock();
185 for ( i = 0; i < mlist->method_count; i += 1 ) {
186 method = &mlist->method_list[i];
187 method->method_name =
188 sel_registerNameNoLock((const char *)method->method_name, isBundle); // Always copy selector data from bundles.
189
190 #ifndef NO_GC
191 if (method->method_name == (SEL)kRTAddress_ignoredSelector) {
192 method->method_imp = (IMP)&_objc_ignored_method;
193 }
194 #endif
195 }
196 sel_unlock();
197 mlist->obsolete = fixed_up_method_list;
198 }
199 return mlist;
200 }
201
202
203 /***********************************************************************
204 * nextMethodList
205 * Returns successive method lists from the given class.
206 * Method lists are returned in method search order (i.e. highest-priority
207 * implementations first).
208 * All necessary method list fixups are performed, so the
209 * returned method list is fully-constructed.
210 *
211 * If cls is already in use, methodListLock must be held by the caller.
212 * For full thread-safety, methodListLock must be continuously held by the
213 * caller across all calls to nextMethodList(). If the lock is released,
214 * the bad results listed in class_nextMethodList() may occur.
215 *
216 * void *iterator = NULL;
217 * struct old_method_list *mlist;
218 * mutex_lock(&methodListLock);
219 * while ((mlist = nextMethodList(cls, &iterator))) {
220 * // do something with mlist
221 * }
222 * mutex_unlock(&methodListLock);
223 **********************************************************************/
224 static struct old_method_list *nextMethodList(struct old_class *cls,
225 void **it)
226 {
227 uintptr_t index = *(uintptr_t *)it;
228 struct old_method_list **resultp;
229
230 if (index == 0) {
231 // First call to nextMethodList.
232 if (!cls->methodLists) {
233 resultp = NULL;
234 } else if (cls->info & CLS_NO_METHOD_ARRAY) {
235 resultp = (struct old_method_list **)&cls->methodLists;
236 } else {
237 resultp = &cls->methodLists[0];
238 if (!*resultp || *resultp == END_OF_METHODS_LIST) {
239 resultp = NULL;
240 }
241 }
242 } else {
243 // Subsequent call to nextMethodList.
244 if (!cls->methodLists) {
245 resultp = NULL;
246 } else if (cls->info & CLS_NO_METHOD_ARRAY) {
247 resultp = NULL;
248 } else {
249 resultp = &cls->methodLists[index];
250 if (!*resultp || *resultp == END_OF_METHODS_LIST) {
251 resultp = NULL;
252 }
253 }
254 }
255
256 // resultp now is NULL, meaning there are no more method lists,
257 // OR the address of the method list pointer to fix up and return.
258
259 if (resultp) {
260 if (*resultp) {
261 *resultp = fixupSelectorsInMethodList(cls, *resultp);
262 }
263 *it = (void *)(index + 1);
264 return *resultp;
265 } else {
266 *it = 0;
267 return NULL;
268 }
269 }
270
271
272 /* These next three functions are the heart of ObjC method lookup.
273 * If the class is currently in use, methodListLock must be held by the caller.
274 */
275 static inline struct old_method *_findMethodInList(struct old_method_list * mlist, SEL sel) {
276 int i;
277 if (!mlist) return NULL;
278 for (i = 0; i < mlist->method_count; i++) {
279 struct old_method *m = &mlist->method_list[i];
280 if (m->method_name == sel) {
281 return m;
282 }
283 }
284 return NULL;
285 }
286
287 static inline struct old_method * _findMethodInClass(struct old_class *cls, SEL sel) __attribute__((always_inline));
288 static inline struct old_method * _findMethodInClass(struct old_class *cls, SEL sel) {
289 // Flattened version of nextMethodList(). The optimizer doesn't
290 // do a good job with hoisting the conditionals out of the loop.
291 // Conceptually, this looks like:
292 // while ((mlist = nextMethodList(cls, &iterator))) {
293 // struct old_method *m = _findMethodInList(mlist, sel);
294 // if (m) return m;
295 // }
296
297 if (!cls->methodLists) {
298 // No method lists.
299 return NULL;
300 }
301 else if (cls->info & CLS_NO_METHOD_ARRAY) {
302 // One method list.
303 struct old_method_list **mlistp;
304 mlistp = (struct old_method_list **)&cls->methodLists;
305 *mlistp = fixupSelectorsInMethodList(cls, *mlistp);
306 return _findMethodInList(*mlistp, sel);
307 }
308 else {
309 // Multiple method lists.
310 struct old_method_list **mlistp;
311 for (mlistp = cls->methodLists;
312 *mlistp != NULL && *mlistp != END_OF_METHODS_LIST;
313 mlistp++)
314 {
315 struct old_method *m;
316 *mlistp = fixupSelectorsInMethodList(cls, *mlistp);
317 m = _findMethodInList(*mlistp, sel);
318 if (m) return m;
319 }
320 return NULL;
321 }
322 }
323
324 static inline struct old_method * _getMethod(struct old_class *cls, SEL sel) {
325 for (; cls; cls = cls->super_class) {
326 struct old_method *m;
327 m = _findMethodInClass(cls, sel);
328 if (m) return m;
329 }
330 return NULL;
331 }
332
333
334 // fixme for gc debugging temporary use
335 __private_extern__ IMP findIMPInClass(struct old_class *cls, SEL sel)
336 {
337 struct old_method *m = _findMethodInClass(cls, sel);
338 if (m) return m->method_imp;
339 else return NULL;
340 }
341
342
343 /***********************************************************************
344 * _freedHandler.
345 **********************************************************************/
346 static void _freedHandler(id obj, SEL sel)
347 {
348 __objc_error (obj, "message %s sent to freed object=%p",
349 sel_getName(sel), obj);
350 }
351
352 /***********************************************************************
353 * _nonexistentHandler.
354 **********************************************************************/
355 static void _nonexistentHandler(id obj, SEL sel)
356 {
357 __objc_error (obj, "message %s sent to non-existent object=%p",
358 sel_getName(sel), obj);
359 }
360
361
362 /***********************************************************************
363 * ABI-specific lookUpMethod helpers.
364 **********************************************************************/
365 __private_extern__ void lockForMethodLookup(void)
366 {
367 mutex_lock(&methodListLock);
368 }
369 __private_extern__ void unlockForMethodLookup(void)
370 {
371 mutex_unlock(&methodListLock);
372 }
373 __private_extern__ IMP prepareForMethodLookup(Class cls, SEL sel, BOOL init)
374 {
375 mutex_assert_unlocked(&methodListLock);
376
377 // Check for freed class
378 if (cls == _class_getFreedObjectClass())
379 return (IMP) _freedHandler;
380
381 // Check for nonexistent class
382 if (cls == _class_getNonexistentObjectClass())
383 return (IMP) _nonexistentHandler;
384
385 if (init && !_class_isInitialized(cls)) {
386 _class_initialize (cls);
387 // If sel == initialize, _class_initialize will send +initialize and
388 // then the messenger will send +initialize again after this
389 // procedure finishes. Of course, if this is not being called
390 // from the messenger then it won't happen. 2778172
391 }
392
393 return NULL;
394 }
395
396
397 /***********************************************************************
398 * class_getVariable. Return the named instance variable.
399 **********************************************************************/
400 __private_extern__
401 Ivar _class_getVariable(Class cls_gen, const char *name)
402 {
403 struct old_class *cls = _class_asOld(cls_gen);
404
405 for (; cls != Nil; cls = cls->super_class) {
406 int i;
407
408 // Skip class having no ivars
409 if (!cls->ivars) continue;
410
411 for (i = 0; i < cls->ivars->ivar_count; i++) {
412 // Check this ivar's name. Be careful because the
413 // compiler generates ivar entries with NULL ivar_name
414 // (e.g. for anonymous bit fields).
415 struct old_ivar *ivar = &cls->ivars->ivar_list[i];
416 if (ivar->ivar_name && 0 == strcmp(name, ivar->ivar_name)) {
417 return (Ivar)ivar;
418 }
419 }
420 }
421
422 // Not found
423 return NULL;
424 }
425
426
427 /***********************************************************************
428 * class_getPropertyList. Return the class's property list
429 * Locking: classLock must be held by the caller
430 **********************************************************************/
431 static struct objc_property_list *
432 nextPropertyList(struct old_class *cls, uintptr_t *indexp)
433 {
434 struct objc_property_list *result = NULL;
435
436 mutex_assert_locked(&classLock);
437 if (! ((cls->info & CLS_EXT) && cls->ext)) {
438 // No class ext
439 result = NULL;
440 } else if (!cls->ext->propertyLists) {
441 // No property lists
442 result = NULL;
443 } else if (cls->info & CLS_NO_PROPERTY_ARRAY) {
444 // Only one property list
445 if (*indexp == 0) {
446 result = (struct objc_property_list *)cls->ext->propertyLists;
447 } else {
448 result = NULL;
449 }
450 } else {
451 // More than one property list
452 result = cls->ext->propertyLists[*indexp];
453 }
454
455 if (result) {
456 ++*indexp;
457 return result;
458 } else {
459 *indexp = 0;
460 return NULL;
461 }
462 }
463
464
465 /***********************************************************************
466 * class_getIvarLayout
467 * NULL means all-scanned. "" means non-scanned.
468 **********************************************************************/
469 const char *
470 class_getIvarLayout(Class cls_gen)
471 {
472 struct old_class *cls = _class_asOld(cls_gen);
473 if (cls && (cls->info & CLS_EXT)) {
474 return cls->ivar_layout;
475 } else {
476 return NULL; // conservative scan
477 }
478 }
479
480
481 /***********************************************************************
482 * class_getWeakIvarLayout
483 * NULL means no weak ivars.
484 **********************************************************************/
485 const char *
486 class_getWeakIvarLayout(Class cls_gen)
487 {
488 struct old_class *cls = _class_asOld(cls_gen);
489 if (cls && (cls->info & CLS_EXT) && cls->ext) {
490 return cls->ext->weak_ivar_layout;
491 } else {
492 return NULL; // no weak ivars
493 }
494 }
495
496
497 /***********************************************************************
498 * class_setIvarLayout
499 * NULL means all-scanned. "" means non-scanned.
500 **********************************************************************/
501 void class_setIvarLayout(Class cls_gen, const char *layout)
502 {
503 struct old_class *cls = _class_asOld(cls_gen);
504 if (!cls) return;
505
506 if (! (cls->info & CLS_EXT)) {
507 _objc_inform("class '%s' needs to be recompiled", cls->name);
508 return;
509 }
510
511 // fixme leak
512 cls->ivar_layout = layout ? _strdup_internal(layout) : NULL;
513 }
514
515
516 /***********************************************************************
517 * class_setWeakIvarLayout
518 * NULL means no weak ivars.
519 **********************************************************************/
520 void class_setWeakIvarLayout(Class cls_gen, const char *layout)
521 {
522 struct old_class *cls = _class_asOld(cls_gen);
523 if (!cls) return;
524
525 mutex_lock(&classLock);
526
527 allocateExt(cls);
528
529 // fixme leak
530 cls->ext->weak_ivar_layout = layout ? _strdup_internal(layout) : NULL;
531
532 mutex_unlock(&classLock);
533 }
534
535
536 /***********************************************************************
537 * _class_changeInfo
538 * Atomically sets and clears some bits in cls's info field.
539 * set and clear must not overlap.
540 **********************************************************************/
541 __private_extern__ void _class_changeInfo(Class cls, long set, long clear)
542 {
543 struct old_class *old = _class_asOld(cls);
544 long newinfo;
545 long oldinfo;
546 do {
547 oldinfo = old->info;
548 newinfo = (oldinfo | set) & ~clear;
549 } while (! OSAtomicCompareAndSwapLong(oldinfo, newinfo, &old->info));
550 }
551
552
553 /***********************************************************************
554 * _class_getInfo
555 * Returns YES iff all set bits in get are also set in cls's info field.
556 **********************************************************************/
557 __private_extern__ BOOL _class_getInfo(Class cls, int get)
558 {
559 struct old_class *old = _class_asOld(cls);
560 return ((old->info & get) == get) ? YES : NO;
561 }
562
563
564 /***********************************************************************
565 * _class_setInfo
566 * Atomically sets some bits in cls's info field.
567 **********************************************************************/
568 __private_extern__ void _class_setInfo(Class cls, long set)
569 {
570 _class_changeInfo(cls, set, 0);
571 }
572
573
574 /***********************************************************************
575 * _class_clearInfo
576 * Atomically clears some bits in cls's info field.
577 **********************************************************************/
578 __private_extern__ void _class_clearInfo(Class cls, long clear)
579 {
580 _class_changeInfo(cls, 0, clear);
581 }
582
583
584 /***********************************************************************
585 * isInitializing
586 * Return YES if cls is currently being initialized.
587 * The initializing bit is stored in the metaclass only.
588 **********************************************************************/
589 __private_extern__ BOOL _class_isInitializing(Class cls)
590 {
591 return _class_getInfo(_class_getMeta(cls), CLS_INITIALIZING);
592 }
593
594
595 /***********************************************************************
596 * isInitialized
597 * Return YES if cls is already initialized.
598 * The initialized bit is stored in the metaclass only.
599 **********************************************************************/
600 __private_extern__ BOOL _class_isInitialized(Class cls)
601 {
602 return _class_getInfo(_class_getMeta(cls), CLS_INITIALIZED);
603 }
604
605
606 /***********************************************************************
607 * setInitializing
608 * Mark cls as initialization in progress.
609 **********************************************************************/
610 __private_extern__ void _class_setInitializing(Class cls)
611 {
612 _class_setInfo(_class_getMeta(cls), CLS_INITIALIZING);
613 }
614
615
616 /***********************************************************************
617 * setInitialized
618 * Atomically mark cls as initialized and not initializing.
619 **********************************************************************/
620 __private_extern__ void _class_setInitialized(Class cls)
621 {
622 _class_changeInfo(_class_getMeta(cls), CLS_INITIALIZED, CLS_INITIALIZING);
623 }
624
625
626 /***********************************************************************
627 * class_setVersion. Record the specified version with the class.
628 **********************************************************************/
629 void class_setVersion(Class cls, int version)
630 {
631 if (!cls) return;
632 cls->version = version;
633 }
634
635 /***********************************************************************
636 * class_getVersion. Return the version recorded with the class.
637 **********************************************************************/
638 int class_getVersion(Class cls)
639 {
640 if (!cls) return 0;
641 return (int)cls->version;
642 }
643
644
645 __private_extern__ Class _class_getMeta(Class cls)
646 {
647 if (_class_getInfo(cls, CLS_META)) return cls;
648 else return ((id)cls)->isa;
649 }
650
651 __private_extern__ BOOL _class_isMetaClass(Class cls)
652 {
653 if (!cls) return NO;
654 return _class_getInfo(cls, CLS_META);
655 }
656
657
658 /***********************************************************************
659 * _class_getNonMetaClass.
660 * Return the ordinary class for this class or metaclass.
661 * Used by +initialize.
662 **********************************************************************/
663 __private_extern__ Class _class_getNonMetaClass(Class cls)
664 {
665 // fixme ick
666 if (_class_isMetaClass(cls)) {
667 if (strncmp(_class_getName(cls), "_%", 2) == 0) {
668 // Posee's meta's name is smashed and isn't in the class_hash,
669 // so objc_getClass doesn't work.
670 const char *baseName = strchr(_class_getName(cls), '%'); // get posee's real name
671 cls = (Class)objc_getClass(baseName);
672 } else {
673 cls = (Class)objc_getClass(_class_getName(cls));
674 }
675 assert(cls);
676 }
677
678 return cls;
679 }
680
681
682 __private_extern__ Class _class_getSuperclass(Class cls)
683 {
684 if (!cls) return nil;
685 return (Class)cls->super_class;
686 }
687
688
689 __private_extern__ Cache _class_getCache(Class cls)
690 {
691 return cls->cache;
692 }
693
694 __private_extern__ void _class_setCache(Class cls, Cache cache)
695 {
696 cls->cache = cache;
697 }
698
699 __private_extern__ size_t _class_getInstanceSize(Class cls)
700 {
701 if (!cls) return 0;
702 return cls->instance_size;
703 }
704
705 __private_extern__ const char * _class_getName(Class cls)
706 {
707 if (!cls) return "nil";
708 return cls->name;
709 }
710
711
712
713 __private_extern__ const char *_category_getName(Category cat)
714 {
715 return _category_asOld(cat)->category_name;
716 }
717
718 __private_extern__ const char *_category_getClassName(Category cat)
719 {
720 return _category_asOld(cat)->class_name;
721 }
722
723 __private_extern__ Class _category_getClass(Category cat)
724 {
725 return (Class)objc_getClass(_category_asOld(cat)->class_name);
726 }
727
728 __private_extern__ IMP _category_getLoadMethod(Category cat)
729 {
730 struct old_method_list *mlist = _category_asOld(cat)->class_methods;
731 if (mlist) {
732 return lookupNamedMethodInMethodList(mlist, "load");
733 } else {
734 return NULL;
735 }
736 }
737
738
739
740 /***********************************************************************
741 * class_nextMethodList.
742 * External version of nextMethodList().
743 *
744 * This function is not fully thread-safe. A series of calls to
745 * class_nextMethodList() may fail if methods are added to or removed
746 * from the class between calls.
747 * If methods are added between calls to class_nextMethodList(), it may
748 * return previously-returned method lists again, and may fail to return
749 * newly-added lists.
750 * If methods are removed between calls to class_nextMethodList(), it may
751 * omit surviving method lists or simply crash.
752 **********************************************************************/
753 OBJC_EXPORT struct objc_method_list *class_nextMethodList(Class cls, void **it)
754 {
755 struct old_method_list *result;
756
757 OBJC_WARN_DEPRECATED;
758
759 mutex_lock(&methodListLock);
760 result = nextMethodList(_class_asOld(cls), it);
761 mutex_unlock(&methodListLock);
762 return (struct objc_method_list *)result;
763 }
764
765
766 /***********************************************************************
767 * class_addMethods.
768 *
769 * Formerly class_addInstanceMethods ()
770 **********************************************************************/
771 OBJC_EXPORT void class_addMethods(Class cls, struct objc_method_list *meths)
772 {
773 OBJC_WARN_DEPRECATED;
774
775 // Add the methods.
776 mutex_lock(&methodListLock);
777 _objc_insertMethods(_class_asOld(cls), (struct old_method_list *)meths, NULL);
778 mutex_unlock(&methodListLock);
779
780 // Must flush when dynamically adding methods. No need to flush
781 // all the class method caches. If cls is a meta class, though,
782 // this will still flush it and any of its sub-meta classes.
783 flush_caches (cls, NO);
784 }
785
786
787 /***********************************************************************
788 * class_removeMethods.
789 **********************************************************************/
790 OBJC_EXPORT void class_removeMethods(Class cls, struct objc_method_list *meths)
791 {
792 OBJC_WARN_DEPRECATED;
793
794 // Remove the methods
795 mutex_lock(&methodListLock);
796 _objc_removeMethods(_class_asOld(cls), (struct old_method_list *)meths);
797 mutex_unlock(&methodListLock);
798
799 // Must flush when dynamically removing methods. No need to flush
800 // all the class method caches. If cls is a meta class, though,
801 // this will still flush it and any of its sub-meta classes.
802 flush_caches (cls, NO);
803 }
804
805 /***********************************************************************
806 * lookupNamedMethodInMethodList
807 * Only called to find +load/-.cxx_construct/-.cxx_destruct methods,
808 * without fixing up the entire method list.
809 * The class is not yet in use, so methodListLock is not taken.
810 **********************************************************************/
811 __private_extern__ IMP lookupNamedMethodInMethodList(struct old_method_list *mlist, const char *meth_name)
812 {
813 struct old_method *m;
814 m = meth_name ? _findNamedMethodInList(mlist, meth_name) : NULL;
815 return (m ? m->method_imp : NULL);
816 }
817
818 __private_extern__ Method _class_getMethod(Class cls, SEL sel)
819 {
820 Method result;
821
822 mutex_lock(&methodListLock);
823 result = (Method)_getMethod(_class_asOld(cls), sel);
824 mutex_unlock(&methodListLock);
825
826 return result;
827 }
828
829 __private_extern__ Method _class_getMethodNoSuper(Class cls, SEL sel)
830 {
831 Method result;
832
833 mutex_lock(&methodListLock);
834 result = (Method)_findMethodInClass(_class_asOld(cls), sel);
835 mutex_unlock(&methodListLock);
836
837 return result;
838 }
839
840 __private_extern__ Method _class_getMethodNoSuper_nolock(Class cls, SEL sel)
841 {
842 mutex_assert_locked(&methodListLock);
843 return (Method)_findMethodInClass(_class_asOld(cls), sel);
844 }
845
846
847 BOOL class_conformsToProtocol(Class cls_gen, Protocol *proto_gen)
848 {
849 struct old_class *cls = oldcls(cls_gen);
850 struct old_protocol *proto = oldprotocol(proto_gen);
851
852 if (!cls_gen) return NO;
853 if (!proto) return NO;
854
855 if (cls->isa->version >= 3) {
856 struct old_protocol_list *list;
857 for (list = cls->protocols; list != NULL; list = list->next) {
858 int i;
859 for (i = 0; i < list->count; i++) {
860 if (list->list[i] == proto) return YES;
861 if (protocol_conformsToProtocol((Protocol *)list->list[i], proto_gen)) return YES;
862 }
863 if (cls->isa->version <= 4) break;
864 }
865 }
866 return NO;
867 }
868
869
870 static NXMapTable * posed_class_hash = NULL;
871
872 /***********************************************************************
873 * objc_getOrigClass.
874 **********************************************************************/
875 __private_extern__ Class _objc_getOrigClass(const char *name)
876 {
877 Class ret;
878
879 // Look for class among the posers
880 ret = Nil;
881 mutex_lock(&classLock);
882 if (posed_class_hash)
883 ret = (Class) NXMapGet (posed_class_hash, name);
884 mutex_unlock(&classLock);
885 if (ret)
886 return ret;
887
888 // Not a poser. Do a normal lookup.
889 ret = (Class)objc_getClass (name);
890 if (!ret)
891 _objc_inform ("class `%s' not linked into application", name);
892
893 return ret;
894 }
895
896 Class objc_getOrigClass(const char *name)
897 {
898 OBJC_WARN_DEPRECATED;
899 return _objc_getOrigClass(name);
900 }
901
902 /***********************************************************************
903 * _objc_addOrigClass. This function is only used from class_poseAs.
904 * Registers the original class names, before they get obscured by
905 * posing, so that [super ..] will work correctly from categories
906 * in posing classes and in categories in classes being posed for.
907 **********************************************************************/
908 static void _objc_addOrigClass (struct old_class *origClass)
909 {
910 mutex_lock(&classLock);
911
912 // Create the poser's hash table on first use
913 if (!posed_class_hash)
914 {
915 posed_class_hash = NXCreateMapTableFromZone (NXStrValueMapPrototype,
916 8,
917 _objc_internal_zone ());
918 }
919
920 // Add the named class iff it is not already there (or collides?)
921 if (NXMapGet (posed_class_hash, origClass->name) == 0)
922 NXMapInsert (posed_class_hash, origClass->name, origClass);
923
924 mutex_unlock(&classLock);
925 }
926
927
928 /***********************************************************************
929 * change_class_references
930 * Change classrefs and superclass pointers from original to imposter
931 * But if copy!=nil, don't change copy->super_class.
932 * If changeSuperRefs==YES, also change [super message] classrefs.
933 * Used by class_poseAs and objc_setFutureClass
934 * classLock must be locked.
935 **********************************************************************/
936 __private_extern__
937 void change_class_references(struct old_class *imposter,
938 struct old_class *original,
939 struct old_class *copy,
940 BOOL changeSuperRefs)
941 {
942 header_info *hInfo;
943 struct old_class *clsObject;
944 NXHashState state;
945
946 // Change all subclasses of the original to point to the imposter.
947 state = NXInitHashState (class_hash);
948 while (NXNextHashState (class_hash, &state, (void **) &clsObject))
949 {
950 while ((clsObject) && (clsObject != imposter) &&
951 (clsObject != copy))
952 {
953 if (clsObject->super_class == original)
954 {
955 clsObject->super_class = imposter;
956 clsObject->isa->super_class = imposter->isa;
957 // We must flush caches here!
958 break;
959 }
960
961 clsObject = clsObject->super_class;
962 }
963 }
964
965 // Replace the original with the imposter in all class refs
966 // Major loop - process all headers
967 for (hInfo = FirstHeader; hInfo != NULL; hInfo = hInfo->next)
968 {
969 struct old_class **cls_refs;
970 size_t refCount;
971 unsigned int index;
972
973 // Fix class refs associated with this header
974 cls_refs = _getObjcClassRefs(hInfo, &refCount);
975 if (cls_refs) {
976 for (index = 0; index < refCount; index += 1) {
977 if (cls_refs[index] == original) {
978 cls_refs[index] = imposter;
979 }
980 }
981 }
982 }
983 }
984
985
986 /***********************************************************************
987 * class_poseAs.
988 *
989 * !!! class_poseAs () does not currently flush any caches.
990 **********************************************************************/
991 Class class_poseAs(Class imposter_gen, Class original_gen)
992 {
993 struct old_class *imposter = _class_asOld(imposter_gen);
994 struct old_class *original = _class_asOld(original_gen);
995 char * imposterNamePtr;
996 struct old_class * copy;
997
998 OBJC_WARN_DEPRECATED;
999
1000 // Trivial case is easy
1001 if (imposter_gen == original_gen)
1002 return imposter_gen;
1003
1004 // Imposter must be an immediate subclass of the original
1005 if (imposter->super_class != original) {
1006 __objc_error((id)imposter_gen,
1007 "[%s poseAs:%s]: target not immediate superclass",
1008 imposter->name, original->name);
1009 }
1010
1011 // Can't pose when you have instance variables (how could it work?)
1012 if (imposter->ivars) {
1013 __objc_error((id)imposter_gen,
1014 "[%s poseAs:%s]: %s defines new instance variables",
1015 imposter->name, original->name, imposter->name);
1016 }
1017
1018 // Build a string to use to replace the name of the original class.
1019 #if TARGET_OS_WIN32
1020 # define imposterNamePrefix "_%"
1021 imposterNamePtr = _malloc_internal(strlen(original->name) + strlen(imposterNamePrefix) + 1);
1022 strcpy(imposterNamePtr, imposterNamePrefix);
1023 strcat(imposterNamePtr, original->name);
1024 # undef imposterNamePrefix
1025 #else
1026 asprintf(&imposterNamePtr, "_%%%s", original->name);
1027 #endif
1028
1029 // We lock the class hashtable, so we are thread safe with respect to
1030 // calls to objc_getClass (). However, the class names are not
1031 // changed atomically, nor are all of the subclasses updated
1032 // atomically. I have ordered the operations so that you will
1033 // never crash, but you may get inconsistent results....
1034
1035 // Register the original class so that [super ..] knows
1036 // exactly which classes are the "original" classes.
1037 _objc_addOrigClass (original);
1038 _objc_addOrigClass (imposter);
1039
1040 // Copy the imposter, so that the imposter can continue
1041 // its normal life in addition to changing the behavior of
1042 // the original. As a hack we don't bother to copy the metaclass.
1043 // For some reason we modify the original rather than the copy.
1044 copy = (struct old_class *)_malloc_internal(sizeof(struct old_class));
1045 memmove(copy, imposter, sizeof(struct old_class));
1046
1047 mutex_lock(&classLock);
1048
1049 // Remove both the imposter and the original class.
1050 NXHashRemove (class_hash, imposter);
1051 NXHashRemove (class_hash, original);
1052
1053 NXHashInsert (class_hash, copy);
1054 objc_addRegisteredClass((Class)copy); // imposter & original will rejoin later, just track the new guy
1055
1056 // Mark the imposter as such
1057 _class_setInfo((Class)imposter, CLS_POSING);
1058 _class_setInfo((Class)imposter->isa, CLS_POSING);
1059
1060 // Change the name of the imposter to that of the original class.
1061 imposter->name = original->name;
1062 imposter->isa->name = original->isa->name;
1063
1064 // Also copy the version field to avoid archiving problems.
1065 imposter->version = original->version;
1066
1067 // Change classrefs and superclass pointers
1068 // Don't change copy->super_class
1069 // Don't change [super ...] messages
1070 change_class_references(imposter, original, copy, NO);
1071
1072 // Change the name of the original class.
1073 original->name = imposterNamePtr + 1;
1074 original->isa->name = imposterNamePtr;
1075
1076 // Restore the imposter and the original class with their new names.
1077 NXHashInsert (class_hash, imposter);
1078 NXHashInsert (class_hash, original);
1079
1080 mutex_unlock(&classLock);
1081
1082 return imposter_gen;
1083 }
1084
1085
1086 /***********************************************************************
1087 * flush_caches. Flush the instance and optionally class method caches
1088 * of cls and all its subclasses.
1089 *
1090 * Specifying Nil for the class "all classes."
1091 **********************************************************************/
1092 __private_extern__ void flush_caches(Class target_gen, BOOL flush_meta)
1093 {
1094 NXHashState state;
1095 struct old_class *target = _class_asOld(target_gen);
1096 struct old_class *clsObject;
1097 #ifdef OBJC_INSTRUMENTED
1098 unsigned int classesVisited;
1099 unsigned int subclassCount;
1100 #endif
1101
1102 mutex_lock(&classLock);
1103 mutex_lock(&cacheUpdateLock);
1104
1105 // Leaf classes are fastest because there are no subclass caches to flush.
1106 // fixme instrument
1107 if (target && (target->info & CLS_LEAF)) {
1108 _cache_flush ((Class)target);
1109
1110 if (!flush_meta) {
1111 mutex_unlock(&cacheUpdateLock);
1112 mutex_unlock(&classLock);
1113 return; // done
1114 } else if (target->isa && (target->isa->info & CLS_LEAF)) {
1115 _cache_flush ((Class)target->isa);
1116 mutex_unlock(&cacheUpdateLock);
1117 mutex_unlock(&classLock);
1118 return; // done
1119 } else {
1120 // Reset target and handle it by one of the methods below.
1121 target = target->isa;
1122 flush_meta = NO;
1123 // NOT done
1124 }
1125 }
1126
1127 state = NXInitHashState(class_hash);
1128
1129 // Handle nil and root instance class specially: flush all
1130 // instance and class method caches. Nice that this
1131 // loop is linear vs the N-squared loop just below.
1132 if (!target || !target->super_class)
1133 {
1134 #ifdef OBJC_INSTRUMENTED
1135 LinearFlushCachesCount += 1;
1136 classesVisited = 0;
1137 subclassCount = 0;
1138 #endif
1139 // Traverse all classes in the hash table
1140 while (NXNextHashState(class_hash, &state, (void**)&clsObject))
1141 {
1142 struct old_class *metaClsObject;
1143 #ifdef OBJC_INSTRUMENTED
1144 classesVisited += 1;
1145 #endif
1146
1147 // Skip class that is known not to be a subclass of this root
1148 // (the isa pointer of any meta class points to the meta class
1149 // of the root).
1150 // NOTE: When is an isa pointer of a hash tabled class ever nil?
1151 metaClsObject = clsObject->isa;
1152 if (target && metaClsObject && target->isa != metaClsObject->isa) {
1153 continue;
1154 }
1155
1156 #ifdef OBJC_INSTRUMENTED
1157 subclassCount += 1;
1158 #endif
1159
1160 _cache_flush ((Class)clsObject);
1161 if (flush_meta && metaClsObject != NULL) {
1162 _cache_flush ((Class)metaClsObject);
1163 }
1164 }
1165 #ifdef OBJC_INSTRUMENTED
1166 LinearFlushCachesVisitedCount += classesVisited;
1167 if (classesVisited > MaxLinearFlushCachesVisitedCount)
1168 MaxLinearFlushCachesVisitedCount = classesVisited;
1169 IdealFlushCachesCount += subclassCount;
1170 if (subclassCount > MaxIdealFlushCachesCount)
1171 MaxIdealFlushCachesCount = subclassCount;
1172 #endif
1173
1174 mutex_unlock(&cacheUpdateLock);
1175 mutex_unlock(&classLock);
1176 return;
1177 }
1178
1179 // Outer loop - flush any cache that could now get a method from
1180 // cls (i.e. the cache associated with cls and any of its subclasses).
1181 #ifdef OBJC_INSTRUMENTED
1182 NonlinearFlushCachesCount += 1;
1183 classesVisited = 0;
1184 subclassCount = 0;
1185 #endif
1186 while (NXNextHashState(class_hash, &state, (void**)&clsObject))
1187 {
1188 struct old_class *clsIter;
1189
1190 #ifdef OBJC_INSTRUMENTED
1191 NonlinearFlushCachesClassCount += 1;
1192 #endif
1193
1194 // Inner loop - Process a given class
1195 clsIter = clsObject;
1196 while (clsIter)
1197 {
1198
1199 #ifdef OBJC_INSTRUMENTED
1200 classesVisited += 1;
1201 #endif
1202 // Flush clsObject instance method cache if
1203 // clsObject is a subclass of cls, or is cls itself
1204 // Flush the class method cache if that was asked for
1205 if (clsIter == target)
1206 {
1207 #ifdef OBJC_INSTRUMENTED
1208 subclassCount += 1;
1209 #endif
1210 _cache_flush ((Class)clsObject);
1211 if (flush_meta)
1212 _cache_flush ((Class)clsObject->isa);
1213
1214 break;
1215
1216 }
1217
1218 // Flush clsObject class method cache if cls is
1219 // the meta class of clsObject or of one
1220 // of clsObject's superclasses
1221 else if (clsIter->isa == target)
1222 {
1223 #ifdef OBJC_INSTRUMENTED
1224 subclassCount += 1;
1225 #endif
1226 _cache_flush ((Class)clsObject->isa);
1227 break;
1228 }
1229
1230 // Move up superclass chain
1231 // else if (_class_isInitialized(clsIter))
1232 clsIter = clsIter->super_class;
1233
1234 // clsIter is not initialized, so its cache
1235 // must be empty. This happens only when
1236 // clsIter == clsObject, because
1237 // superclasses are initialized before
1238 // subclasses, and this loop traverses
1239 // from sub- to super- classes.
1240 // else
1241 // break;
1242 }
1243 }
1244 #ifdef OBJC_INSTRUMENTED
1245 NonlinearFlushCachesVisitedCount += classesVisited;
1246 if (classesVisited > MaxNonlinearFlushCachesVisitedCount)
1247 MaxNonlinearFlushCachesVisitedCount = classesVisited;
1248 IdealFlushCachesCount += subclassCount;
1249 if (subclassCount > MaxIdealFlushCachesCount)
1250 MaxIdealFlushCachesCount = subclassCount;
1251 #endif
1252
1253 mutex_unlock(&cacheUpdateLock);
1254 mutex_unlock(&classLock);
1255 }
1256
1257
1258 /***********************************************************************
1259 * flush_marked_caches. Flush the method cache of any class marked
1260 * CLS_FLUSH_CACHE (and all subclasses thereof)
1261 * fixme instrument
1262 **********************************************************************/
1263 __private_extern__ void flush_marked_caches(void)
1264 {
1265 struct old_class *cls;
1266 struct old_class *supercls;
1267 NXHashState state;
1268
1269 mutex_lock(&classLock);
1270 mutex_lock(&cacheUpdateLock);
1271
1272 state = NXInitHashState(class_hash);
1273 while (NXNextHashState(class_hash, &state, (void**)&cls)) {
1274 for (supercls = cls; supercls; supercls = supercls->super_class) {
1275 if (supercls->info & CLS_FLUSH_CACHE) {
1276 _cache_flush((Class)cls);
1277 break;
1278 }
1279 }
1280
1281 for (supercls = cls->isa; supercls; supercls = supercls->super_class) {
1282 if (supercls->info & CLS_FLUSH_CACHE) {
1283 _cache_flush((Class)cls->isa);
1284 break;
1285 }
1286 }
1287 }
1288
1289 state = NXInitHashState(class_hash);
1290 while (NXNextHashState(class_hash, &state, (void**)&cls)) {
1291 if (cls->info & CLS_FLUSH_CACHE) {
1292 _class_clearInfo((Class)cls, CLS_FLUSH_CACHE);
1293 }
1294 if (cls->isa->info & CLS_FLUSH_CACHE) {
1295 _class_clearInfo((Class)cls->isa, CLS_FLUSH_CACHE);
1296 }
1297 }
1298
1299 mutex_unlock(&cacheUpdateLock);
1300 mutex_unlock(&classLock);
1301 }
1302
1303
1304 /***********************************************************************
1305 * get_base_method_list
1306 * Returns the method list containing the class's own methods,
1307 * ignoring any method lists added by categories or class_addMethods.
1308 * Called only by add_class_to_loadable_list.
1309 * Does not hold methodListLock because add_class_to_loadable_list
1310 * does not manipulate in-use classes.
1311 **********************************************************************/
1312 static struct old_method_list *get_base_method_list(struct old_class *cls)
1313 {
1314 struct old_method_list **ptr;
1315
1316 if (!cls->methodLists) return NULL;
1317 if (cls->info & CLS_NO_METHOD_ARRAY) return (struct old_method_list *)cls->methodLists;
1318 ptr = cls->methodLists;
1319 if (!*ptr || *ptr == END_OF_METHODS_LIST) return NULL;
1320 while ( *ptr != 0 && *ptr != END_OF_METHODS_LIST ) { ptr++; }
1321 --ptr;
1322 return *ptr;
1323 }
1324
1325
1326 static IMP _class_getLoadMethod_nocheck(struct old_class *cls)
1327 {
1328 struct old_method_list *mlist;
1329 mlist = get_base_method_list(cls->isa);
1330 if (mlist) {
1331 return lookupNamedMethodInMethodList (mlist, "load");
1332 }
1333 return NULL;
1334 }
1335
1336
1337 __private_extern__ BOOL _class_hasLoadMethod(Class cls)
1338 {
1339 if (oldcls(cls)->isa->info & CLS_HAS_LOAD_METHOD) return YES;
1340 return (_class_getLoadMethod_nocheck(oldcls(cls)) ? YES : NO);
1341 }
1342
1343
1344 /***********************************************************************
1345 * _class_getLoadMethod
1346 * Returns cls's +load implementation, or NULL if it doesn't have one.
1347 **********************************************************************/
1348 __private_extern__ IMP _class_getLoadMethod(Class cls_gen)
1349 {
1350 struct old_class *cls = _class_asOld(cls_gen);
1351 if (cls->isa->info & CLS_HAS_LOAD_METHOD) {
1352 return _class_getLoadMethod_nocheck(cls);
1353 }
1354 return NULL;
1355 }
1356
1357
1358 __private_extern__ BOOL _class_shouldGrowCache(Class cls)
1359 {
1360 return _class_getInfo(cls, CLS_GROW_CACHE);
1361 }
1362
1363 __private_extern__ void _class_setGrowCache(Class cls, BOOL grow)
1364 {
1365 if (grow) _class_setInfo(cls, CLS_GROW_CACHE);
1366 else _class_clearInfo(cls, CLS_GROW_CACHE);
1367 }
1368
1369 __private_extern__ BOOL _class_hasCxxStructorsNoSuper(Class cls)
1370 {
1371 return _class_getInfo(cls, CLS_HAS_CXX_STRUCTORS);
1372 }
1373
1374 __private_extern__ BOOL _class_shouldFinalizeOnMainThread(Class cls) {
1375 return _class_getInfo(cls, CLS_FINALIZE_ON_MAIN_THREAD);
1376 }
1377
1378 __private_extern__ void _class_setFinalizeOnMainThread(Class cls) {
1379 _class_setInfo(cls, CLS_FINALIZE_ON_MAIN_THREAD);
1380 }
1381
1382 __private_extern__ BOOL _class_instancesHaveAssociatedObjects(Class cls) {
1383 return _class_getInfo(cls, CLS_INSTANCES_HAVE_ASSOCIATED_OBJECTS);
1384 }
1385
1386 __private_extern__ void _class_assertInstancesHaveAssociatedObjects(Class cls) {
1387 _class_setInfo(cls, CLS_INSTANCES_HAVE_ASSOCIATED_OBJECTS);
1388 }
1389
1390 __private_extern__ struct old_ivar *_ivar_asOld(Ivar ivar)
1391 {
1392 return (struct old_ivar *)ivar;
1393 }
1394
1395 ptrdiff_t ivar_getOffset(Ivar ivar)
1396 {
1397 return _ivar_asOld(ivar)->ivar_offset;
1398 }
1399
1400 const char *ivar_getName(Ivar ivar)
1401 {
1402 return _ivar_asOld(ivar)->ivar_name;
1403 }
1404
1405 const char *ivar_getTypeEncoding(Ivar ivar)
1406 {
1407 return _ivar_asOld(ivar)->ivar_type;
1408 }
1409
1410
1411 IMP method_getImplementation(Method m)
1412 {
1413 if (!m) return NULL;
1414 return _method_asOld(m)->method_imp;
1415 }
1416
1417 SEL method_getName(Method m)
1418 {
1419 if (!m) return NULL;
1420 return _method_asOld(m)->method_name;
1421 }
1422
1423 const char *method_getTypeEncoding(Method m)
1424 {
1425 if (!m) return NULL;
1426 return _method_asOld(m)->method_types;
1427 }
1428
1429 static OSSpinLock impLock = OS_SPINLOCK_INIT;
1430
1431 IMP method_setImplementation(Method m_gen, IMP imp)
1432 {
1433 IMP old;
1434 struct old_method *m = _method_asOld(m_gen);
1435 if (!m) return NULL;
1436 if (!imp) return NULL;
1437
1438 if (m->method_name == (SEL)kIgnore) {
1439 // Ignored methods stay ignored
1440 return m->method_imp;
1441 }
1442
1443 OSSpinLockLock(&impLock);
1444 old = m->method_imp;
1445 m->method_imp = imp;
1446 OSSpinLockUnlock(&impLock);
1447 return old;
1448 }
1449
1450
1451 void method_exchangeImplementations(Method m1_gen, Method m2_gen)
1452 {
1453 IMP m1_imp;
1454 struct old_method *m1 = _method_asOld(m1_gen);
1455 struct old_method *m2 = _method_asOld(m2_gen);
1456 if (!m1 || !m2) return;
1457
1458 if (m1->method_name == (SEL)kIgnore || m2->method_name == (SEL)kIgnore) {
1459 // Ignored methods stay ignored. Now they're both ignored.
1460 m1->method_imp = (IMP)&_objc_ignored_method;
1461 m2->method_imp = (IMP)&_objc_ignored_method;
1462 return;
1463 }
1464
1465 OSSpinLockLock(&impLock);
1466 m1_imp = m1->method_imp;
1467 m1->method_imp = m2->method_imp;
1468 m2->method_imp = m1_imp;
1469 OSSpinLockUnlock(&impLock);
1470 }
1471
1472
1473 struct objc_method_description * method_getDescription(Method m)
1474 {
1475 if (!m) return NULL;
1476 return (struct objc_method_description *)oldmethod(m);
1477 }
1478
1479
1480 /***********************************************************************
1481 * class_addMethod
1482 **********************************************************************/
1483 static IMP _class_addMethod(Class cls_gen, SEL name, IMP imp,
1484 const char *types, BOOL replace)
1485 {
1486 struct old_class *cls = oldcls(cls_gen);
1487 struct old_method *m;
1488 IMP result = NULL;
1489
1490 if (!types) types = "";
1491
1492 mutex_lock(&methodListLock);
1493
1494 if ((m = _findMethodInClass(cls, name))) {
1495 // already exists
1496 // fixme atomic
1497 result = method_getImplementation((Method)m);
1498 if (replace) {
1499 method_setImplementation((Method)m, imp);
1500 }
1501 } else {
1502 // fixme could be faster
1503 struct old_method_list *mlist =
1504 _calloc_internal(sizeof(struct old_method_list), 1);
1505 mlist->obsolete = fixed_up_method_list;
1506 mlist->method_count = 1;
1507 mlist->method_list[0].method_name = name;
1508 mlist->method_list[0].method_types = _strdup_internal(types);
1509 if (name != (SEL)kIgnore) {
1510 mlist->method_list[0].method_imp = imp;
1511 } else {
1512 mlist->method_list[0].method_imp = (IMP)&_objc_ignored_method;
1513 }
1514
1515 _objc_insertMethods(cls, mlist, NULL);
1516 if (!(cls->info & CLS_CONSTRUCTING)) {
1517 flush_caches((Class)cls, NO);
1518 } else {
1519 // in-construction class has no subclasses
1520 flush_cache((Class)cls);
1521 }
1522 result = NULL;
1523 }
1524
1525 mutex_unlock(&methodListLock);
1526
1527 return result;
1528 }
1529
1530
1531 /***********************************************************************
1532 * class_addMethod
1533 **********************************************************************/
1534 BOOL class_addMethod(Class cls, SEL name, IMP imp, const char *types)
1535 {
1536 IMP old;
1537 if (!cls) return NO;
1538
1539 old = _class_addMethod(cls, name, imp, types, NO);
1540 return old ? NO : YES;
1541 }
1542
1543
1544 /***********************************************************************
1545 * class_replaceMethod
1546 **********************************************************************/
1547 IMP class_replaceMethod(Class cls, SEL name, IMP imp, const char *types)
1548 {
1549 if (!cls) return NULL;
1550
1551 return _class_addMethod(cls, name, imp, types, YES);
1552 }
1553
1554
1555 /***********************************************************************
1556 * class_addIvar
1557 **********************************************************************/
1558 BOOL class_addIvar(Class cls_gen, const char *name, size_t size,
1559 uint8_t alignment, const char *type)
1560 {
1561 struct old_class *cls = oldcls(cls_gen);
1562 BOOL result = YES;
1563
1564 if (!cls) return NO;
1565 if (ISMETA(cls)) return NO;
1566 if (!(cls->info & CLS_CONSTRUCTING)) return NO;
1567
1568 if (!type) type = "";
1569 if (name && 0 == strcmp(name, "")) name = NULL;
1570
1571 mutex_lock(&classLock);
1572
1573 // Check for existing ivar with this name
1574 // fixme check superclasses?
1575 if (cls->ivars) {
1576 int i;
1577 for (i = 0; i < cls->ivars->ivar_count; i++) {
1578 if (0 == strcmp(cls->ivars->ivar_list[i].ivar_name, name)) {
1579 result = NO;
1580 break;
1581 }
1582 }
1583 }
1584
1585 if (result) {
1586 struct old_ivar_list *old = cls->ivars;
1587 size_t oldSize;
1588 int newCount;
1589 struct old_ivar *ivar;
1590 size_t alignBytes;
1591 size_t misalign;
1592
1593 if (old) {
1594 oldSize = sizeof(struct old_ivar_list) +
1595 (old->ivar_count - 1) * sizeof(struct old_ivar);
1596 newCount = 1 + old->ivar_count;
1597 } else {
1598 oldSize = sizeof(struct old_ivar_list) - sizeof(struct old_ivar);
1599 newCount = 1;
1600 }
1601
1602 // allocate new ivar list
1603 cls->ivars = _calloc_internal(oldSize + sizeof(struct old_ivar), 1);
1604 if (old) memcpy(cls->ivars, old, oldSize);
1605 if (old && malloc_size(old)) free(old);
1606 cls->ivars->ivar_count = newCount;
1607 ivar = &cls->ivars->ivar_list[newCount-1];
1608
1609 // set ivar name and type
1610 ivar->ivar_name = _strdup_internal(name);
1611 ivar->ivar_type = _strdup_internal(type);
1612
1613 // align if necessary
1614 alignBytes = 1 << alignment;
1615 misalign = cls->instance_size % alignBytes;
1616 if (misalign) cls->instance_size += (long)(alignBytes - misalign);
1617
1618 // set ivar offset and increase instance size
1619 ivar->ivar_offset = (int)cls->instance_size;
1620 cls->instance_size += (long)size;
1621 }
1622
1623 mutex_unlock(&classLock);
1624
1625 return result;
1626 }
1627
1628
1629 /***********************************************************************
1630 * class_addProtocol
1631 **********************************************************************/
1632 BOOL class_addProtocol(Class cls_gen, Protocol *protocol_gen)
1633 {
1634 struct old_class *cls = oldcls(cls_gen);
1635 struct old_protocol *protocol = oldprotocol(protocol_gen);
1636 struct old_protocol_list *plist;
1637
1638 if (!cls) return NO;
1639 if (class_conformsToProtocol(cls_gen, protocol_gen)) return NO;
1640
1641 mutex_lock(&classLock);
1642
1643 // fixme optimize - protocol list doesn't escape?
1644 plist = _calloc_internal(sizeof(struct old_protocol_list), 1);
1645 plist->count = 1;
1646 plist->list[0] = protocol;
1647 plist->next = cls->protocols;
1648 cls->protocols = plist;
1649
1650 // fixme metaclass?
1651
1652 mutex_unlock(&classLock);
1653
1654 return YES;
1655 }
1656
1657
1658 /***********************************************************************
1659 * class_addProperty
1660 **********************************************************************/
1661 __private_extern__ void
1662 _class_addProperties(struct old_class *cls,
1663 struct objc_property_list *additions)
1664 {
1665 struct objc_property_list *newlist;
1666
1667 if (!(cls->info & CLS_EXT)) return;
1668
1669 newlist =
1670 _memdup_internal(additions, sizeof(*newlist) - sizeof(newlist->first)
1671 + (additions->entsize * additions->count));
1672
1673 mutex_lock(&classLock);
1674
1675 allocateExt(cls);
1676 if (!cls->ext->propertyLists) {
1677 // cls has no properties - simply use this list
1678 cls->ext->propertyLists = (struct objc_property_list **)newlist;
1679 _class_setInfo((Class)cls, CLS_NO_PROPERTY_ARRAY);
1680 }
1681 else if (cls->info & CLS_NO_PROPERTY_ARRAY) {
1682 // cls has one property list - make a new array
1683 struct objc_property_list **newarray =
1684 _malloc_internal(3 * sizeof(*newarray));
1685 newarray[0] = newlist;
1686 newarray[1] = (struct objc_property_list *)cls->ext->propertyLists;
1687 newarray[2] = NULL;
1688 cls->ext->propertyLists = newarray;
1689 _class_clearInfo((Class)cls, CLS_NO_PROPERTY_ARRAY);
1690 }
1691 else {
1692 // cls has a property array - make a bigger one
1693 struct objc_property_list **newarray;
1694 int count = 0;
1695 while (cls->ext->propertyLists[count]) count++;
1696 newarray = _malloc_internal((count+2) * sizeof(*newarray));
1697 newarray[0] = newlist;
1698 memcpy(&newarray[1], &cls->ext->propertyLists[0],
1699 count * sizeof(*newarray));
1700 newarray[count+1] = NULL;
1701 free(cls->ext->propertyLists);
1702 cls->ext->propertyLists = newarray;
1703 }
1704
1705 mutex_unlock(&classLock);
1706 }
1707
1708
1709 /***********************************************************************
1710 * class_copyProtocolList. Returns a heap block containing the
1711 * protocols implemented by the class, or NULL if the class
1712 * implements no protocols. Caller must free the block.
1713 * Does not copy any superclass's protocols.
1714 **********************************************************************/
1715 Protocol **class_copyProtocolList(Class cls_gen, unsigned int *outCount)
1716 {
1717 struct old_class *cls = oldcls(cls_gen);
1718 struct old_protocol_list *plist;
1719 Protocol **result = NULL;
1720 unsigned int count = 0;
1721 unsigned int p;
1722
1723 if (!cls) {
1724 if (outCount) *outCount = 0;
1725 return NULL;
1726 }
1727
1728 mutex_lock(&classLock);
1729
1730 for (plist = cls->protocols; plist != NULL; plist = plist->next) {
1731 count += (int)plist->count;
1732 }
1733
1734 if (count > 0) {
1735 result = malloc((count+1) * sizeof(Protocol *));
1736
1737 for (p = 0, plist = cls->protocols;
1738 plist != NULL;
1739 plist = plist->next)
1740 {
1741 int i;
1742 for (i = 0; i < plist->count; i++) {
1743 result[p++] = (Protocol *)plist->list[i];
1744 }
1745 }
1746 result[p] = NULL;
1747 }
1748
1749 mutex_unlock(&classLock);
1750
1751 if (outCount) *outCount = count;
1752 return result;
1753 }
1754
1755
1756 /***********************************************************************
1757 * class_getProperty. Return the named property.
1758 **********************************************************************/
1759 Property class_getProperty(Class cls_gen, const char *name)
1760 {
1761 Property result;
1762 struct old_class *cls = _class_asOld(cls_gen);
1763 if (!cls || !name) return NULL;
1764
1765 mutex_lock(&classLock);
1766
1767 for (result = NULL; cls && !result; cls = cls->super_class) {
1768 uintptr_t iterator = 0;
1769 struct objc_property_list *plist;
1770 while ((plist = nextPropertyList(cls, &iterator))) {
1771 uint32_t i;
1772 for (i = 0; i < plist->count; i++) {
1773 Property p = property_list_nth(plist, i);
1774 if (0 == strcmp(name, p->name)) {
1775 result = p;
1776 goto done;
1777 }
1778 }
1779 }
1780 }
1781
1782 done:
1783 mutex_unlock(&classLock);
1784
1785 return result;
1786 }
1787
1788
1789 /***********************************************************************
1790 * class_copyPropertyList. Returns a heap block containing the
1791 * properties declared in the class, or NULL if the class
1792 * declares no properties. Caller must free the block.
1793 * Does not copy any superclass's properties.
1794 **********************************************************************/
1795 Property *class_copyPropertyList(Class cls_gen, unsigned int *outCount)
1796 {
1797 struct old_class *cls = oldcls(cls_gen);
1798 struct objc_property_list *plist;
1799 uintptr_t iterator = 0;
1800 Property *result = NULL;
1801 unsigned int count = 0;
1802 unsigned int p, i;
1803
1804 if (!cls) {
1805 if (outCount) *outCount = 0;
1806 return NULL;
1807 }
1808
1809 mutex_lock(&classLock);
1810
1811 iterator = 0;
1812 while ((plist = nextPropertyList(cls, &iterator))) {
1813 count += plist->count;
1814 }
1815
1816 if (count > 0) {
1817 result = malloc((count+1) * sizeof(Property));
1818
1819 p = 0;
1820 iterator = 0;
1821 while ((plist = nextPropertyList(cls, &iterator))) {
1822 for (i = 0; i < plist->count; i++) {
1823 result[p++] = property_list_nth(plist, i);
1824 }
1825 }
1826 result[p] = NULL;
1827 }
1828
1829 mutex_unlock(&classLock);
1830
1831 if (outCount) *outCount = count;
1832 return result;
1833 }
1834
1835
1836 /***********************************************************************
1837 * class_copyMethodList. Returns a heap block containing the
1838 * methods implemented by the class, or NULL if the class
1839 * implements no methods. Caller must free the block.
1840 * Does not copy any superclass's methods.
1841 **********************************************************************/
1842 Method *class_copyMethodList(Class cls_gen, unsigned int *outCount)
1843 {
1844 struct old_class *cls = oldcls(cls_gen);
1845 struct old_method_list *mlist;
1846 void *iterator = NULL;
1847 Method *result = NULL;
1848 unsigned int count = 0;
1849 unsigned int m;
1850
1851 if (!cls) {
1852 if (outCount) *outCount = 0;
1853 return NULL;
1854 }
1855
1856 mutex_lock(&methodListLock);
1857
1858 iterator = NULL;
1859 while ((mlist = nextMethodList(cls, &iterator))) {
1860 count += mlist->method_count;
1861 }
1862
1863 if (count > 0) {
1864 result = malloc((count+1) * sizeof(Method));
1865
1866 m = 0;
1867 iterator = NULL;
1868 while ((mlist = nextMethodList(cls, &iterator))) {
1869 int i;
1870 for (i = 0; i < mlist->method_count; i++) {
1871 Method aMethod = (Method)&mlist->method_list[i];
1872 if (method_getName(aMethod) == (SEL)kIgnore) {
1873 count--;
1874 continue;
1875 }
1876 result[m++] = aMethod;
1877 }
1878 }
1879 result[m] = NULL;
1880 }
1881
1882 mutex_unlock(&methodListLock);
1883
1884 if (outCount) *outCount = count;
1885 return result;
1886 }
1887
1888
1889 /***********************************************************************
1890 * class_copyIvarList. Returns a heap block containing the
1891 * ivars declared in the class, or NULL if the class
1892 * declares no ivars. Caller must free the block.
1893 * Does not copy any superclass's ivars.
1894 **********************************************************************/
1895 Ivar *class_copyIvarList(Class cls_gen, unsigned int *outCount)
1896 {
1897 struct old_class *cls = oldcls(cls_gen);
1898 Ivar *result = NULL;
1899 unsigned int count = 0;
1900 int i;
1901
1902 if (!cls) {
1903 if (outCount) *outCount = 0;
1904 return NULL;
1905 }
1906
1907 if (cls->ivars) {
1908 count = cls->ivars->ivar_count;
1909 }
1910
1911 if (count > 0) {
1912 result = malloc((count+1) * sizeof(Ivar));
1913
1914 for (i = 0; i < cls->ivars->ivar_count; i++) {
1915 result[i] = (Ivar)&cls->ivars->ivar_list[i];
1916 }
1917 result[i] = NULL;
1918 }
1919
1920 if (outCount) *outCount = count;
1921 return result;
1922 }
1923
1924
1925 /***********************************************************************
1926 * objc_allocateClass.
1927 **********************************************************************/
1928 __private_extern__
1929 void set_superclass(struct old_class *cls, struct old_class *supercls)
1930 {
1931 struct old_class *meta = cls->isa;
1932
1933 if (supercls) {
1934 cls->super_class = supercls;
1935 meta->super_class = supercls->isa;
1936 meta->isa = supercls->isa->isa;
1937
1938 // Superclass is no longer a leaf for cache flushing
1939 if (supercls->info & CLS_LEAF) {
1940 _class_clearInfo((Class)supercls, CLS_LEAF);
1941 _class_clearInfo((Class)supercls->isa, CLS_LEAF);
1942 }
1943 } else {
1944 cls->super_class = Nil; // superclass of root class is nil
1945 meta->super_class = cls; // superclass of root metaclass is root class
1946 meta->isa = meta; // metaclass of root metaclass is root metaclass
1947
1948 // Root class is never a leaf for cache flushing, because the
1949 // root metaclass is a subclass. (This could be optimized, but
1950 // is too uncommon to bother.)
1951 _class_clearInfo((Class)cls, CLS_LEAF);
1952 _class_clearInfo((Class)meta, CLS_LEAF);
1953 }
1954 }
1955
1956 void objc_initializeClassPair(Class superclass_gen, const char *name, Class cls_gen, Class meta_gen)
1957 {
1958 struct old_class *supercls = oldcls(superclass_gen);
1959 struct old_class *cls = oldcls(cls_gen);
1960 struct old_class *meta = oldcls(meta_gen);
1961
1962 // Connect to superclasses and metaclasses
1963 cls->isa = meta;
1964 set_superclass(cls, supercls);
1965
1966 // Set basic info
1967 cls->name = _strdup_internal(name);
1968 meta->name = _strdup_internal(name);
1969 cls->version = 0;
1970 meta->version = 7;
1971 cls->info = CLS_CLASS | CLS_CONSTRUCTING | CLS_EXT | CLS_LEAF;
1972 meta->info = CLS_META | CLS_CONSTRUCTING | CLS_EXT | CLS_LEAF;
1973
1974 // Set instance size based on superclass.
1975 if (supercls) {
1976 cls->instance_size = supercls->instance_size;
1977 meta->instance_size = supercls->isa->instance_size;
1978 } else {
1979 cls->instance_size = sizeof(struct old_class *); // just an isa
1980 meta->instance_size = sizeof(struct old_class);
1981 }
1982
1983 // No ivars. No methods. Empty cache. No protocols. No layout. No ext.
1984 cls->ivars = NULL;
1985 cls->methodLists = NULL;
1986 cls->cache = (Cache)&_objc_empty_cache;
1987 cls->protocols = NULL;
1988 cls->ext = NULL;
1989
1990 meta->ivars = NULL;
1991 meta->methodLists = NULL;
1992 meta->cache = (Cache)&_objc_empty_cache;
1993 meta->protocols = NULL;
1994 cls->ext = NULL;
1995 }
1996
1997 Class objc_allocateClassPair(Class superclass_gen, const char *name,
1998 size_t extraBytes)
1999 {
2000 struct old_class *supercls = oldcls(superclass_gen);
2001 Class cls, meta;
2002
2003 if (objc_getClass(name)) return NO;
2004 // fixme reserve class name against simultaneous allocation
2005
2006 if (supercls && (supercls->info & CLS_CONSTRUCTING)) {
2007 // Can't make subclass of an in-construction class
2008 return NO;
2009 }
2010
2011 // Allocate new classes.
2012 if (supercls) {
2013 cls = _calloc_class(supercls->isa->instance_size + extraBytes);
2014 meta = _calloc_class(supercls->isa->isa->instance_size + extraBytes);
2015 } else {
2016 cls = _calloc_class(sizeof(struct old_class) + extraBytes);
2017 meta = _calloc_class(sizeof(struct old_class) + extraBytes);
2018 }
2019
2020
2021 objc_initializeClassPair(superclass_gen, name, cls, meta);
2022
2023 return (Class)cls;
2024 }
2025
2026
2027 void objc_registerClassPair(Class cls_gen)
2028 {
2029 struct old_class *cls = oldcls(cls_gen);
2030
2031 if ((cls->info & CLS_CONSTRUCTED) ||
2032 (cls->isa->info & CLS_CONSTRUCTED))
2033 {
2034 _objc_inform("objc_registerClassPair: class '%s' was already "
2035 "registered!", cls->name);
2036 return;
2037 }
2038
2039 if (!(cls->info & CLS_CONSTRUCTING) ||
2040 !(cls->isa->info & CLS_CONSTRUCTING))
2041 {
2042 _objc_inform("objc_registerClassPair: class '%s' was not "
2043 "allocated with objc_allocateClassPair!", cls->name);
2044 return;
2045 }
2046
2047 if (ISMETA(cls)) {
2048 _objc_inform("objc_registerClassPair: class '%s' is a metaclass, "
2049 "not a class!", cls->name);
2050 return;
2051 }
2052
2053 mutex_lock(&classLock);
2054
2055 // Build ivar layouts
2056 if (UseGC) {
2057 if (cls->ivar_layout) {
2058 // Class builder already called class_setIvarLayout.
2059 }
2060 else if (!cls->super_class) {
2061 // Root class. Scan conservatively (should be isa ivar only).
2062 // ivar_layout is already NULL.
2063 }
2064 else if (cls->ivars == NULL) {
2065 // No local ivars. Use superclass's layout.
2066 cls->ivar_layout =
2067 _strdup_internal(cls->super_class->ivar_layout);
2068 }
2069 else {
2070 // Has local ivars. Build layout based on superclass.
2071 struct old_class *supercls = cls->super_class;
2072 unsigned char *superlayout =
2073 (unsigned char *) class_getIvarLayout((Class)supercls);
2074 layout_bitmap bitmap =
2075 layout_bitmap_create(superlayout, supercls->instance_size,
2076 cls->instance_size, NO);
2077 int i;
2078 for (i = 0; i < cls->ivars->ivar_count; i++) {
2079 struct old_ivar *iv = &cls->ivars->ivar_list[i];
2080 layout_bitmap_set_ivar(bitmap, iv->ivar_type, iv->ivar_offset);
2081 }
2082 cls->ivar_layout = (char *)layout_string_create(bitmap);
2083 layout_bitmap_free(bitmap);
2084 }
2085
2086 if (cls->ext && cls->ext->weak_ivar_layout) {
2087 // Class builder already called class_setWeakIvarLayout.
2088 }
2089 else if (!cls->super_class) {
2090 // Root class. No weak ivars (should be isa ivar only)
2091 // weak_ivar_layout is already NULL.
2092 }
2093 else if (cls->ivars == NULL) {
2094 // No local ivars. Use superclass's layout.
2095 const char *weak =
2096 class_getWeakIvarLayout((Class)cls->super_class);
2097 if (weak) {
2098 allocateExt(cls);
2099 cls->ext->weak_ivar_layout = _strdup_internal(weak);
2100 }
2101 }
2102 else {
2103 // Has local ivars. Build layout based on superclass.
2104 // No way to add weak ivars yet.
2105 const char *weak =
2106 class_getWeakIvarLayout((Class)cls->super_class);
2107 if (weak) {
2108 allocateExt(cls);
2109 cls->ext->weak_ivar_layout = _strdup_internal(weak);
2110 }
2111 }
2112 }
2113
2114 // Clear "under construction" bit, set "done constructing" bit
2115 cls->info &= ~CLS_CONSTRUCTING;
2116 cls->isa->info &= ~CLS_CONSTRUCTING;
2117 cls->info |= CLS_CONSTRUCTED;
2118 cls->isa->info |= CLS_CONSTRUCTED;
2119
2120 NXHashInsertIfAbsent(class_hash, cls);
2121 objc_addRegisteredClass((Class)cls);
2122 //objc_addRegisteredClass(cls->isa); if we ever allocate classes from GC
2123
2124 mutex_unlock(&classLock);
2125 }
2126
2127
2128 Class objc_duplicateClass(Class orig_gen, const char *name, size_t extraBytes)
2129 {
2130 unsigned int count, i;
2131 struct old_method **originalMethods;
2132 struct old_method_list *duplicateMethods;
2133 struct old_class *original = oldcls(orig_gen);
2134 // Don't use sizeof(struct objc_class) here because
2135 // instance_size has historically contained two extra words,
2136 // and instance_size is what objc_getIndexedIvars() actually uses.
2137 struct old_class *duplicate = (struct old_class *)
2138 _calloc_class(original->isa->instance_size + extraBytes);
2139
2140 duplicate->isa = original->isa;
2141 duplicate->super_class = original->super_class;
2142 duplicate->name = strdup(name);
2143 duplicate->version = original->version;
2144 duplicate->info = original->info & (CLS_CLASS|CLS_META|CLS_INITIALIZED|CLS_JAVA_HYBRID|CLS_JAVA_CLASS|CLS_HAS_CXX_STRUCTORS|CLS_HAS_LOAD_METHOD);
2145 duplicate->instance_size = original->instance_size;
2146 duplicate->ivars = original->ivars;
2147 // methodLists handled below
2148 duplicate->cache = (Cache)&_objc_empty_cache;
2149 duplicate->protocols = original->protocols;
2150 if (original->info & CLS_EXT) {
2151 duplicate->info |= original->info & (CLS_EXT|CLS_NO_PROPERTY_ARRAY);
2152 duplicate->ivar_layout = original->ivar_layout;
2153 if (original->ext) {
2154 duplicate->ext = _malloc_internal(original->ext->size);
2155 memcpy(duplicate->ext, original->ext, original->ext->size);
2156 } else {
2157 duplicate->ext = NULL;
2158 }
2159 }
2160
2161 // Method lists are deep-copied so they can be stomped.
2162 originalMethods = (struct old_method **)
2163 class_copyMethodList(orig_gen, &count);
2164 if (originalMethods) {
2165 duplicateMethods = (struct old_method_list *)
2166 calloc(sizeof(struct old_method_list) +
2167 (count-1)*sizeof(struct old_method), 1);
2168 duplicateMethods->obsolete = fixed_up_method_list;
2169 duplicateMethods->method_count = count;
2170 for (i = 0; i < count; i++) {
2171 duplicateMethods->method_list[i] = *(originalMethods[i]);
2172 }
2173 duplicate->methodLists = (struct old_method_list **)duplicateMethods;
2174 duplicate->info |= CLS_NO_METHOD_ARRAY;
2175 free(originalMethods);
2176 }
2177
2178 mutex_lock(&classLock);
2179 NXHashInsert(class_hash, duplicate);
2180 objc_addRegisteredClass((Class)duplicate);
2181 mutex_unlock(&classLock);
2182
2183 return (Class)duplicate;
2184 }
2185
2186
2187 void objc_disposeClassPair(Class cls_gen)
2188 {
2189 struct old_class *cls = oldcls(cls_gen);
2190
2191 if (!(cls->info & (CLS_CONSTRUCTED|CLS_CONSTRUCTING)) ||
2192 !(cls->isa->info & (CLS_CONSTRUCTED|CLS_CONSTRUCTING)))
2193 {
2194 // class not allocated with objc_allocateClassPair
2195 // disposing still-unregistered class is OK!
2196 _objc_inform("objc_disposeClassPair: class '%s' was not "
2197 "allocated with objc_allocateClassPair!", cls->name);
2198 return;
2199 }
2200
2201 if (ISMETA(cls)) {
2202 _objc_inform("objc_disposeClassPair: class '%s' is a metaclass, "
2203 "not a class!", cls->name);
2204 return;
2205 }
2206
2207 mutex_lock(&classLock);
2208 NXHashRemove(class_hash, cls);
2209 objc_removeRegisteredClass((Class)cls);
2210 unload_class(cls->isa);
2211 unload_class(cls);
2212 mutex_unlock(&classLock);
2213 }
2214
2215
2216 /***********************************************************************
2217 * _internal_class_createInstance. Allocate an instance of the specified
2218 * class with the specified number of bytes for indexed variables, in
2219 * the default zone, using _internal_class_createInstanceFromZone.
2220 **********************************************************************/
2221 static id _internal_class_createInstance(Class cls, size_t extraBytes)
2222 {
2223 return _internal_class_createInstanceFromZone (cls, extraBytes, NULL);
2224 }
2225
2226
2227 static id _internal_object_copyFromZone(id oldObj, size_t extraBytes, void *zone)
2228 {
2229 id obj;
2230 size_t size;
2231
2232 if (!oldObj) return nil;
2233
2234 obj = (*_zoneAlloc)(oldObj->isa, extraBytes, zone);
2235 size = _class_getInstanceSize(oldObj->isa) + extraBytes;
2236
2237 // fixme need C++ copy constructor
2238 objc_memmove_collectable(obj, oldObj, size);
2239
2240 #if !defined(NO_GC)
2241 if (UseGC) gc_fixup_weakreferences(obj, oldObj);
2242 #endif
2243
2244 return obj;
2245 }
2246
2247 static id _internal_object_copy(id oldObj, size_t extraBytes)
2248 {
2249 void *z = malloc_zone_from_ptr(oldObj);
2250 return _internal_object_copyFromZone(oldObj, extraBytes,
2251 z ? z : malloc_default_zone());
2252 }
2253
2254 static id _internal_object_reallocFromZone(id anObject, size_t nBytes,
2255 void *zone)
2256 {
2257 id newObject;
2258 Class tmp;
2259
2260 if (anObject == nil)
2261 __objc_error(nil, "reallocating nil object");
2262
2263 if (anObject->isa == _objc_getFreedObjectClass ())
2264 __objc_error(anObject, "reallocating freed object");
2265
2266 if (nBytes < _class_getInstanceSize(anObject->isa))
2267 __objc_error(anObject, "(%s, %zu) requested size too small",
2268 object_getClassName(anObject), nBytes);
2269
2270 // fixme need C++ copy constructor
2271 // fixme GC copy
2272 // Make sure not to modify space that has been declared free
2273 tmp = anObject->isa;
2274 anObject->isa = _objc_getFreedObjectClass ();
2275 newObject = (id)malloc_zone_realloc(zone, anObject, nBytes);
2276 if (newObject) {
2277 newObject->isa = tmp;
2278 } else {
2279 // realloc failed, anObject is still alive
2280 anObject->isa = tmp;
2281 }
2282 return newObject;
2283 }
2284
2285
2286 static id _internal_object_realloc(id anObject, size_t nBytes)
2287 {
2288 void *z = malloc_zone_from_ptr(anObject);
2289 return _internal_object_reallocFromZone(anObject,
2290 nBytes,
2291 z ? z : malloc_default_zone());
2292 }
2293
2294 id (*_alloc)(Class, size_t) = _internal_class_createInstance;
2295 id (*_copy)(id, size_t) = _internal_object_copy;
2296 id (*_realloc)(id, size_t) = _internal_object_realloc;
2297 id (*_dealloc)(id) = _internal_object_dispose;
2298 id (*_zoneAlloc)(Class, size_t, void *) = _internal_class_createInstanceFromZone;
2299 id (*_zoneCopy)(id, size_t, void *) = _internal_object_copyFromZone;
2300 id (*_zoneRealloc)(id, size_t, void *) = _internal_object_reallocFromZone;
2301 void (*_error)(id, const char *, va_list) = _objc_error;
2302
2303
2304 id class_createInstance(Class cls, size_t extraBytes)
2305 {
2306 if (UseGC) {
2307 return _internal_class_createInstance(cls, extraBytes);
2308 } else {
2309 return (*_alloc)(cls, extraBytes);
2310 }
2311 }
2312
2313 id class_createInstanceFromZone(Class cls, size_t extraBytes, void *z)
2314 {
2315 OBJC_WARN_DEPRECATED;
2316 if (UseGC) {
2317 return _internal_class_createInstanceFromZone(cls, extraBytes, z);
2318 } else {
2319 return (*_zoneAlloc)(cls, extraBytes, z);
2320 }
2321 }
2322
2323 id object_copy(id obj, size_t extraBytes)
2324 {
2325 if (UseGC) return _internal_object_copy(obj, extraBytes);
2326 else return (*_copy)(obj, extraBytes);
2327 }
2328
2329 id object_copyFromZone(id obj, size_t extraBytes, void *z)
2330 {
2331 OBJC_WARN_DEPRECATED;
2332 if (UseGC) return _internal_object_copyFromZone(obj, extraBytes, z);
2333 else return (*_zoneCopy)(obj, extraBytes, z);
2334 }
2335
2336 id object_dispose(id obj)
2337 {
2338 if (UseGC) return _internal_object_dispose(obj);
2339 else return (*_dealloc)(obj);
2340 }
2341
2342 id object_realloc(id obj, size_t nBytes)
2343 {
2344 OBJC_WARN_DEPRECATED;
2345 if (UseGC) return _internal_object_realloc(obj, nBytes);
2346 else return (*_realloc)(obj, nBytes);
2347 }
2348
2349 id object_reallocFromZone(id obj, size_t nBytes, void *z)
2350 {
2351 OBJC_WARN_DEPRECATED;
2352 if (UseGC) return _internal_object_reallocFromZone(obj, nBytes, z);
2353 else return (*_zoneRealloc)(obj, nBytes, z);
2354 }
2355
2356
2357 // ProKit SPI
2358 Class class_setSuperclass(Class cls, Class newSuper)
2359 {
2360 Class oldSuper = cls->super_class;
2361 set_superclass(oldcls(cls), oldcls(newSuper));
2362 flush_caches(cls, YES);
2363 return oldSuper;
2364 }
2365 #endif