2 * Copyright (c) 2010-2012 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
24 #include "objc-private.h"
27 #include "objc-weak.h"
28 #include "llvm-DenseMap.h"
31 #include <malloc/malloc.h>
34 #include <mach/mach.h>
35 #include <mach-o/dyld.h>
36 #include <mach-o/nlist.h>
37 #include <sys/types.h>
39 #include <libkern/OSAtomic.h>
44 @interface NSInvocation
51 // NSObject used to be in Foundation/CoreFoundation.
53 #define SYMBOL_ELSEWHERE_IN_3(sym, vers, n) \
54 OBJC_EXPORT const char elsewhere_ ##n __asm__("$ld$hide$os" #vers "$" #sym); const char elsewhere_ ##n = 0
55 #define SYMBOL_ELSEWHERE_IN_2(sym, vers, n) \
56 SYMBOL_ELSEWHERE_IN_3(sym, vers, n)
57 #define SYMBOL_ELSEWHERE_IN(sym, vers) \
58 SYMBOL_ELSEWHERE_IN_2(sym, vers, __COUNTER__)
61 # define NSOBJECT_ELSEWHERE_IN(vers) \
62 SYMBOL_ELSEWHERE_IN(_OBJC_CLASS_$_NSObject, vers); \
63 SYMBOL_ELSEWHERE_IN(_OBJC_METACLASS_$_NSObject, vers); \
64 SYMBOL_ELSEWHERE_IN(_OBJC_IVAR_$_NSObject.isa, vers)
66 # define NSOBJECT_ELSEWHERE_IN(vers) \
67 SYMBOL_ELSEWHERE_IN(.objc_class_name_NSObject, vers)
71 NSOBJECT_ELSEWHERE_IN(5.1);
72 NSOBJECT_ELSEWHERE_IN(5.0);
73 NSOBJECT_ELSEWHERE_IN(4.3);
74 NSOBJECT_ELSEWHERE_IN(4.2);
75 NSOBJECT_ELSEWHERE_IN(4.1);
76 NSOBJECT_ELSEWHERE_IN(4.0);
77 NSOBJECT_ELSEWHERE_IN(3.2);
78 NSOBJECT_ELSEWHERE_IN(3.1);
79 NSOBJECT_ELSEWHERE_IN(3.0);
80 NSOBJECT_ELSEWHERE_IN(2.2);
81 NSOBJECT_ELSEWHERE_IN(2.1);
82 NSOBJECT_ELSEWHERE_IN(2.0);
83 #elif TARGET_OS_MAC && !TARGET_OS_IPHONE
84 NSOBJECT_ELSEWHERE_IN(10.7);
85 NSOBJECT_ELSEWHERE_IN(10.6);
86 NSOBJECT_ELSEWHERE_IN(10.5);
87 NSOBJECT_ELSEWHERE_IN(10.4);
88 NSOBJECT_ELSEWHERE_IN(10.3);
89 NSOBJECT_ELSEWHERE_IN(10.2);
90 NSOBJECT_ELSEWHERE_IN(10.1);
91 NSOBJECT_ELSEWHERE_IN(10.0);
93 // NSObject has always been in libobjc on these platforms.
100 /***********************************************************************
102 **********************************************************************/
104 static id defaultBadAllocHandler(Class cls)
106 _objc_fatal("attempt to allocate object of class '%s' failed",
107 cls->nameForLogging());
110 static id(*badAllocHandler)(Class) = &defaultBadAllocHandler;
112 static id callBadAllocHandler(Class cls)
114 // fixme add re-entrancy protection in case allocation fails inside handler
115 return (*badAllocHandler)(cls);
118 void _objc_setBadAllocHandler(id(*newHandler)(Class))
120 badAllocHandler = newHandler;
126 // The order of these bits is important.
127 #define SIDE_TABLE_WEAKLY_REFERENCED (1UL<<0)
128 #define SIDE_TABLE_DEALLOCATING (1UL<<1) // MSB-ward of weak bit
129 #define SIDE_TABLE_RC_ONE (1UL<<2) // MSB-ward of deallocating bit
130 #define SIDE_TABLE_RC_PINNED (1UL<<(WORD_BITS-1))
132 #define SIDE_TABLE_RC_SHIFT 2
133 #define SIDE_TABLE_FLAG_MASK (SIDE_TABLE_RC_ONE-1)
135 // RefcountMap disguises its pointers because we
136 // don't want the table to act as a root for `leaks`.
137 typedef objc::DenseMap<DisguisedPtr<objc_object>,size_t,true> RefcountMap;
142 weak_table_t weak_table;
145 memset(&weak_table, 0, sizeof(weak_table));
149 _objc_fatal("Do not delete SideTable.");
152 void lock() { slock.lock(); }
153 void unlock() { slock.unlock(); }
154 bool trylock() { return slock.trylock(); }
156 // Address-ordered lock discipline for a pair of side tables.
158 template<bool HaveOld, bool HaveNew>
159 static void lockTwo(SideTable *lock1, SideTable *lock2);
160 template<bool HaveOld, bool HaveNew>
161 static void unlockTwo(SideTable *lock1, SideTable *lock2);
166 void SideTable::lockTwo<true, true>(SideTable *lock1, SideTable *lock2) {
167 spinlock_t::lockTwo(&lock1->slock, &lock2->slock);
171 void SideTable::lockTwo<true, false>(SideTable *lock1, SideTable *) {
176 void SideTable::lockTwo<false, true>(SideTable *, SideTable *lock2) {
181 void SideTable::unlockTwo<true, true>(SideTable *lock1, SideTable *lock2) {
182 spinlock_t::unlockTwo(&lock1->slock, &lock2->slock);
186 void SideTable::unlockTwo<true, false>(SideTable *lock1, SideTable *) {
191 void SideTable::unlockTwo<false, true>(SideTable *, SideTable *lock2) {
197 // We cannot use a C++ static initializer to initialize SideTables because
198 // libc calls us before our C++ initializers run. We also don't want a global
199 // pointer to this struct because of the extra indirection.
200 // Do it the hard way.
201 alignas(StripedMap<SideTable>) static uint8_t
202 SideTableBuf[sizeof(StripedMap<SideTable>)];
204 static void SideTableInit() {
205 new (SideTableBuf) StripedMap<SideTable>();
208 static StripedMap<SideTable>& SideTables() {
209 return *reinterpret_cast<StripedMap<SideTable>*>(SideTableBuf);
212 // anonymous namespace
217 // The -fobjc-arc flag causes the compiler to issue calls to objc_{retain/release/autorelease/retain_block}
220 id objc_retainBlock(id x) {
221 return (id)_Block_copy(x);
225 // The following SHOULD be called by the compiler directly, but the request hasn't been made yet :-)
228 BOOL objc_should_deallocate(id object) {
233 objc_retain_autorelease(id obj)
235 return objc_autorelease(objc_retain(obj));
240 objc_storeStrong(id *location, id obj)
252 // Update a weak variable.
253 // If HaveOld is true, the variable has an existing value
254 // that needs to be cleaned up. This value might be nil.
255 // If HaveNew is true, there is a new value that needs to be
256 // assigned into the variable. This value might be nil.
257 // If CrashIfDeallocating is true, the process is halted if newObj is
258 // deallocating or newObj's class does not support weak references.
259 // If CrashIfDeallocating is false, nil is stored instead.
260 template <bool HaveOld, bool HaveNew, bool CrashIfDeallocating>
262 storeWeak(id *location, objc_object *newObj)
264 assert(HaveOld || HaveNew);
265 if (!HaveNew) assert(newObj == nil);
267 Class previouslyInitializedClass = nil;
272 // Acquire locks for old and new values.
273 // Order by lock address to prevent lock ordering problems.
274 // Retry if the old value changes underneath us.
278 oldTable = &SideTables()[oldObj];
283 newTable = &SideTables()[newObj];
288 SideTable::lockTwo<HaveOld, HaveNew>(oldTable, newTable);
290 if (HaveOld && *location != oldObj) {
291 SideTable::unlockTwo<HaveOld, HaveNew>(oldTable, newTable);
295 // Prevent a deadlock between the weak reference machinery
296 // and the +initialize machinery by ensuring that no
297 // weakly-referenced object has an un-+initialized isa.
298 if (HaveNew && newObj) {
299 Class cls = newObj->getIsa();
300 if (cls != previouslyInitializedClass &&
301 !((objc_class *)cls)->isInitialized())
303 SideTable::unlockTwo<HaveOld, HaveNew>(oldTable, newTable);
304 _class_initialize(_class_getNonMetaClass(cls, (id)newObj));
306 // If this class is finished with +initialize then we're good.
307 // If this class is still running +initialize on this thread
308 // (i.e. +initialize called storeWeak on an instance of itself)
309 // then we may proceed but it will appear initializing and
310 // not yet initialized to the check above.
311 // Instead set previouslyInitializedClass to recognize it on retry.
312 previouslyInitializedClass = cls;
318 // Clean up old value, if any.
320 weak_unregister_no_lock(&oldTable->weak_table, oldObj, location);
323 // Assign new value, if any.
325 newObj = (objc_object *)weak_register_no_lock(&newTable->weak_table,
326 (id)newObj, location,
327 CrashIfDeallocating);
328 // weak_register_no_lock returns nil if weak store should be rejected
330 // Set is-weakly-referenced bit in refcount table.
331 if (newObj && !newObj->isTaggedPointer()) {
332 newObj->setWeaklyReferenced_nolock();
335 // Do not set *location anywhere else. That would introduce a race.
336 *location = (id)newObj;
339 // No new value. The storage is not changed.
342 SideTable::unlockTwo<HaveOld, HaveNew>(oldTable, newTable);
349 * This function stores a new value into a __weak variable. It would
350 * be used anywhere a __weak variable is the target of an assignment.
352 * @param location The address of the weak pointer itself
353 * @param newObj The new object this weak ptr should now point to
358 objc_storeWeak(id *location, id newObj)
360 return storeWeak<true/*old*/, true/*new*/, true/*crash*/>
361 (location, (objc_object *)newObj);
366 * This function stores a new value into a __weak variable.
367 * If the new object is deallocating or the new object's class
368 * does not support weak references, stores nil instead.
370 * @param location The address of the weak pointer itself
371 * @param newObj The new object this weak ptr should now point to
373 * @return The value stored (either the new object or nil)
376 objc_storeWeakOrNil(id *location, id newObj)
378 return storeWeak<true/*old*/, true/*new*/, false/*crash*/>
379 (location, (objc_object *)newObj);
384 * Initialize a fresh weak pointer to some object location.
385 * It would be used for code like:
391 * __weak id weakPtr = o;
393 * This function IS NOT thread-safe with respect to concurrent
394 * modifications to the weak variable. (Concurrent weak clear is safe.)
396 * @param location Address of __weak ptr.
397 * @param newObj Object ptr.
400 objc_initWeak(id *location, id newObj)
407 return storeWeak<false/*old*/, true/*new*/, true/*crash*/>
408 (location, (objc_object*)newObj);
412 objc_initWeakOrNil(id *location, id newObj)
419 return storeWeak<false/*old*/, true/*new*/, false/*crash*/>
420 (location, (objc_object*)newObj);
425 * Destroys the relationship between a weak pointer
426 * and the object it is referencing in the internal weak
427 * table. If the weak pointer is not referencing anything,
428 * there is no need to edit the weak table.
430 * This function IS NOT thread-safe with respect to concurrent
431 * modifications to the weak variable. (Concurrent weak clear is safe.)
433 * @param location The weak pointer address.
436 objc_destroyWeak(id *location)
438 (void)storeWeak<true/*old*/, false/*new*/, false/*crash*/>
444 objc_loadWeakRetained(id *location)
452 if (!result) return nil;
454 table = &SideTables()[result];
457 if (*location != result) {
462 result = weak_read_no_lock(&table->weak_table, location);
469 * This loads the object referenced by a weak pointer and returns it, after
470 * retaining and autoreleasing the object to ensure that it stays alive
471 * long enough for the caller to use it. This function would be used
472 * anywhere a __weak variable is used in an expression.
474 * @param location The weak pointer address
476 * @return The object pointed to by \e location, or \c nil if \e location is \c nil.
479 objc_loadWeak(id *location)
481 if (!*location) return nil;
482 return objc_autorelease(objc_loadWeakRetained(location));
487 * This function copies a weak pointer from one location to another,
488 * when the destination doesn't already contain a weak pointer. It
489 * would be used for code like:
491 * __weak id src = ...;
492 * __weak id dst = src;
494 * This function IS NOT thread-safe with respect to concurrent
495 * modifications to the destination variable. (Concurrent weak clear is safe.)
497 * @param dst The destination variable.
498 * @param src The source variable.
501 objc_copyWeak(id *dst, id *src)
503 id obj = objc_loadWeakRetained(src);
504 objc_initWeak(dst, obj);
509 * Move a weak pointer from one location to another.
510 * Before the move, the destination must be uninitialized.
511 * After the move, the source is nil.
513 * This function IS NOT thread-safe with respect to concurrent
514 * modifications to either weak variable. (Concurrent weak clear is safe.)
518 objc_moveWeak(id *dst, id *src)
520 objc_copyWeak(dst, src);
521 objc_destroyWeak(src);
526 /***********************************************************************
527 Autorelease pool implementation
529 A thread's autorelease pool is a stack of pointers.
530 Each pointer is either an object to release, or POOL_SENTINEL which is
531 an autorelease pool boundary.
532 A pool token is a pointer to the POOL_SENTINEL for that pool. When
533 the pool is popped, every object hotter than the sentinel is released.
534 The stack is divided into a doubly-linked list of pages. Pages are added
535 and deleted as necessary.
536 Thread-local storage points to the hot page, where newly autoreleased
538 **********************************************************************/
540 BREAKPOINT_FUNCTION(void objc_autoreleaseNoPool(id obj));
545 static const uint32_t M0 = 0xA1A1A1A1;
546 # define M1 "AUTORELEASE!"
547 static const size_t M1_len = 12;
551 assert(M1_len == strlen(M1));
552 assert(M1_len == 3 * sizeof(m[1]));
555 strncpy((char *)&m[1], M1, M1_len);
559 m[0] = m[1] = m[2] = m[3] = 0;
563 return (m[0] == M0 && 0 == strncmp((char *)&m[1], M1, M1_len));
566 bool fastcheck() const {
578 // Set this to 1 to mprotect() autorelease pool contents
579 #define PROTECT_AUTORELEASEPOOL 0
581 class AutoreleasePoolPage
584 #define POOL_SENTINEL nil
585 static pthread_key_t const key = AUTORELEASE_POOL_KEY;
586 static uint8_t const SCRIBBLE = 0xA3; // 0xA3A3A3A3 after releasing
587 static size_t const SIZE =
588 #if PROTECT_AUTORELEASEPOOL
589 PAGE_MAX_SIZE; // must be multiple of vm page size
591 PAGE_MAX_SIZE; // size and alignment, power of 2
593 static size_t const COUNT = SIZE / sizeof(id);
597 pthread_t const thread;
598 AutoreleasePoolPage * const parent;
599 AutoreleasePoolPage *child;
600 uint32_t const depth;
603 // SIZE-sizeof(*this) bytes of contents follow
605 static void * operator new(size_t size) {
606 return malloc_zone_memalign(malloc_default_zone(), SIZE, SIZE);
608 static void operator delete(void * p) {
612 inline void protect() {
613 #if PROTECT_AUTORELEASEPOOL
614 mprotect(this, SIZE, PROT_READ);
619 inline void unprotect() {
620 #if PROTECT_AUTORELEASEPOOL
622 mprotect(this, SIZE, PROT_READ | PROT_WRITE);
626 AutoreleasePoolPage(AutoreleasePoolPage *newParent)
627 : magic(), next(begin()), thread(pthread_self()),
628 parent(newParent), child(nil),
629 depth(parent ? 1+parent->depth : 0),
630 hiwat(parent ? parent->hiwat : 0)
634 assert(!parent->child);
636 parent->child = this;
642 ~AutoreleasePoolPage()
648 // Not recursive: we don't want to blow out the stack
649 // if a thread accumulates a stupendous amount of garbage
654 void busted(bool die = true)
657 (die ? _objc_fatal : _objc_inform)
658 ("autorelease pool page %p corrupted\n"
659 " magic 0x%08x 0x%08x 0x%08x 0x%08x\n"
660 " should be 0x%08x 0x%08x 0x%08x 0x%08x\n"
664 magic.m[0], magic.m[1], magic.m[2], magic.m[3],
665 right.m[0], right.m[1], right.m[2], right.m[3],
666 this->thread, pthread_self());
669 void check(bool die = true)
671 if (!magic.check() || !pthread_equal(thread, pthread_self())) {
676 void fastcheck(bool die = true)
678 if (! magic.fastcheck()) {
685 return (id *) ((uint8_t *)this+sizeof(*this));
689 return (id *) ((uint8_t *)this+SIZE);
693 return next == begin();
697 return next == end();
700 bool lessThanHalfFull() {
701 return (next - begin() < (end() - begin()) / 2);
708 id *ret = next; // faster than `return next-1` because of aliasing
716 releaseUntil(begin());
719 void releaseUntil(id *stop)
721 // Not recursive: we don't want to blow out the stack
722 // if a thread accumulates a stupendous amount of garbage
724 while (this->next != stop) {
725 // Restart from hotPage() every time, in case -release
726 // autoreleased more objects
727 AutoreleasePoolPage *page = hotPage();
729 // fixme I think this `while` can be `if`, but I can't prove it
730 while (page->empty()) {
736 id obj = *--page->next;
737 memset((void*)page->next, SCRIBBLE, sizeof(*page->next));
740 if (obj != POOL_SENTINEL) {
748 // we expect any children to be completely empty
749 for (AutoreleasePoolPage *page = child; page; page = page->child) {
750 assert(page->empty());
757 // Not recursive: we don't want to blow out the stack
758 // if a thread accumulates a stupendous amount of garbage
759 AutoreleasePoolPage *page = this;
760 while (page->child) page = page->child;
762 AutoreleasePoolPage *deathptr;
772 } while (deathptr != this);
775 static void tls_dealloc(void *p)
777 // reinstate TLS value while we work
778 setHotPage((AutoreleasePoolPage *)p);
780 if (AutoreleasePoolPage *page = coldPage()) {
781 if (!page->empty()) pop(page->begin()); // pop all of the pools
782 if (DebugMissingPools || DebugPoolAllocation) {
783 // pop() killed the pages already
785 page->kill(); // free all of the pages
789 // clear TLS value so TLS destruction doesn't loop
793 static AutoreleasePoolPage *pageForPointer(const void *p)
795 return pageForPointer((uintptr_t)p);
798 static AutoreleasePoolPage *pageForPointer(uintptr_t p)
800 AutoreleasePoolPage *result;
801 uintptr_t offset = p % SIZE;
803 assert(offset >= sizeof(AutoreleasePoolPage));
805 result = (AutoreleasePoolPage *)(p - offset);
812 static inline AutoreleasePoolPage *hotPage()
814 AutoreleasePoolPage *result = (AutoreleasePoolPage *)
816 if (result) result->fastcheck();
820 static inline void setHotPage(AutoreleasePoolPage *page)
822 if (page) page->fastcheck();
823 tls_set_direct(key, (void *)page);
826 static inline AutoreleasePoolPage *coldPage()
828 AutoreleasePoolPage *result = hotPage();
830 while (result->parent) {
831 result = result->parent;
839 static inline id *autoreleaseFast(id obj)
841 AutoreleasePoolPage *page = hotPage();
842 if (page && !page->full()) {
843 return page->add(obj);
845 return autoreleaseFullPage(obj, page);
847 return autoreleaseNoPage(obj);
851 static __attribute__((noinline))
852 id *autoreleaseFullPage(id obj, AutoreleasePoolPage *page)
854 // The hot page is full.
855 // Step to the next non-full page, adding a new page if necessary.
856 // Then add the object to that page.
857 assert(page == hotPage());
858 assert(page->full() || DebugPoolAllocation);
861 if (page->child) page = page->child;
862 else page = new AutoreleasePoolPage(page);
863 } while (page->full());
866 return page->add(obj);
869 static __attribute__((noinline))
870 id *autoreleaseNoPage(id obj)
875 if (obj != POOL_SENTINEL && DebugMissingPools) {
876 // We are pushing an object with no pool in place,
877 // and no-pool debugging was requested by environment.
878 _objc_inform("MISSING POOLS: Object %p of class %s "
879 "autoreleased with no pool in place - "
880 "just leaking - break on "
881 "objc_autoreleaseNoPool() to debug",
882 (void*)obj, object_getClassName(obj));
883 objc_autoreleaseNoPool(obj);
887 // Install the first page.
888 AutoreleasePoolPage *page = new AutoreleasePoolPage(nil);
891 // Push an autorelease pool boundary if it wasn't already requested.
892 if (obj != POOL_SENTINEL) {
893 page->add(POOL_SENTINEL);
896 // Push the requested object.
897 return page->add(obj);
901 static __attribute__((noinline))
902 id *autoreleaseNewPage(id obj)
904 AutoreleasePoolPage *page = hotPage();
905 if (page) return autoreleaseFullPage(obj, page);
906 else return autoreleaseNoPage(obj);
910 static inline id autorelease(id obj)
913 assert(!obj->isTaggedPointer());
914 id *dest __unused = autoreleaseFast(obj);
915 assert(!dest || *dest == obj);
920 static inline void *push()
923 if (DebugPoolAllocation) {
924 // Each autorelease pool starts on a new pool page.
925 dest = autoreleaseNewPage(POOL_SENTINEL);
927 dest = autoreleaseFast(POOL_SENTINEL);
929 assert(*dest == POOL_SENTINEL);
933 static inline void pop(void *token)
935 AutoreleasePoolPage *page;
938 page = pageForPointer(token);
940 if (DebugPoolAllocation && *stop != POOL_SENTINEL) {
941 // This check is not valid with DebugPoolAllocation off
942 // after an autorelease with a pool page but no pool in place.
943 _objc_fatal("invalid or prematurely-freed autorelease pool %p; ",
947 if (PrintPoolHiwat) printHiwat();
949 page->releaseUntil(stop);
951 // memory: delete empty children
952 if (DebugPoolAllocation && page->empty()) {
953 // special case: delete everything during page-per-pool debugging
954 AutoreleasePoolPage *parent = page->parent;
957 } else if (DebugMissingPools && page->empty() && !page->parent) {
958 // special case: delete everything for pop(top)
959 // when debugging missing autorelease pools
963 else if (page->child) {
964 // hysteresis: keep one empty child if page is more than half full
965 if (page->lessThanHalfFull()) {
968 else if (page->child->child) {
969 page->child->child->kill();
976 int r __unused = pthread_key_init_np(AutoreleasePoolPage::key,
977 AutoreleasePoolPage::tls_dealloc);
983 _objc_inform("[%p] ................ PAGE %s %s %s", this,
984 full() ? "(full)" : "",
985 this == hotPage() ? "(hot)" : "",
986 this == coldPage() ? "(cold)" : "");
988 for (id *p = begin(); p < next; p++) {
989 if (*p == POOL_SENTINEL) {
990 _objc_inform("[%p] ################ POOL %p", p, p);
992 _objc_inform("[%p] %#16lx %s",
993 p, (unsigned long)*p, object_getClassName(*p));
998 static void printAll()
1000 _objc_inform("##############");
1001 _objc_inform("AUTORELEASE POOLS for thread %p", pthread_self());
1003 AutoreleasePoolPage *page;
1004 ptrdiff_t objects = 0;
1005 for (page = coldPage(); page; page = page->child) {
1006 objects += page->next - page->begin();
1008 _objc_inform("%llu releases pending.", (unsigned long long)objects);
1010 for (page = coldPage(); page; page = page->child) {
1014 _objc_inform("##############");
1017 static void printHiwat()
1019 // Check and propagate high water mark
1020 // Ignore high water marks under 256 to suppress noise.
1021 AutoreleasePoolPage *p = hotPage();
1022 uint32_t mark = p->depth*COUNT + (uint32_t)(p->next - p->begin());
1023 if (mark > p->hiwat && mark > 256) {
1024 for( ; p; p = p->parent) {
1030 _objc_inform("POOL HIGHWATER: new high water mark of %u "
1031 "pending autoreleases for thread %p:",
1032 mark, pthread_self());
1035 int count = backtrace(stack, sizeof(stack)/sizeof(stack[0]));
1036 char **sym = backtrace_symbols(stack, count);
1037 for (int i = 0; i < count; i++) {
1038 _objc_inform("POOL HIGHWATER: %s", sym[i]);
1044 #undef POOL_SENTINEL
1047 // anonymous namespace
1051 /***********************************************************************
1052 * Slow paths for inline control
1053 **********************************************************************/
1055 #if SUPPORT_NONPOINTER_ISA
1058 objc_object::rootRetain_overflow(bool tryRetain)
1060 return rootRetain(tryRetain, true);
1065 objc_object::rootRelease_underflow(bool performDealloc)
1067 return rootRelease(performDealloc, true);
1071 // Slow path of clearDeallocating()
1072 // for objects with indexed isa
1073 // that were ever weakly referenced
1074 // or whose retain count ever overflowed to the side table.
1076 objc_object::clearDeallocating_slow()
1078 assert(isa.indexed && (isa.weakly_referenced || isa.has_sidetable_rc));
1080 SideTable& table = SideTables()[this];
1082 if (isa.weakly_referenced) {
1083 weak_clear_no_lock(&table.weak_table, (id)this);
1085 if (isa.has_sidetable_rc) {
1086 table.refcnts.erase(this);
1093 __attribute__((noinline,used))
1095 objc_object::rootAutorelease2()
1097 assert(!isTaggedPointer());
1098 return AutoreleasePoolPage::autorelease((id)this);
1102 BREAKPOINT_FUNCTION(
1103 void objc_overrelease_during_dealloc_error(void)
1109 objc_object::overrelease_error()
1111 _objc_inform_now_and_on_crash("%s object %p overreleased while already deallocating; break on objc_overrelease_during_dealloc_error to debug", object_getClassName((id)this), this);
1112 objc_overrelease_during_dealloc_error();
1113 return false; // allow rootRelease() to tail-call this
1117 /***********************************************************************
1118 * Retain count operations for side table.
1119 **********************************************************************/
1123 // Used to assert that an object is not present in the side table.
1125 objc_object::sidetable_present()
1127 bool result = false;
1128 SideTable& table = SideTables()[this];
1132 RefcountMap::iterator it = table.refcnts.find(this);
1133 if (it != table.refcnts.end()) result = true;
1135 if (weak_is_registered_no_lock(&table.weak_table, (id)this)) result = true;
1143 #if SUPPORT_NONPOINTER_ISA
1146 objc_object::sidetable_lock()
1148 SideTable& table = SideTables()[this];
1153 objc_object::sidetable_unlock()
1155 SideTable& table = SideTables()[this];
1160 // Move the entire retain count to the side table,
1161 // as well as isDeallocating and weaklyReferenced.
1163 objc_object::sidetable_moveExtraRC_nolock(size_t extra_rc,
1164 bool isDeallocating,
1165 bool weaklyReferenced)
1167 assert(!isa.indexed); // should already be changed to not-indexed
1168 SideTable& table = SideTables()[this];
1170 size_t& refcntStorage = table.refcnts[this];
1171 size_t oldRefcnt = refcntStorage;
1172 // not deallocating - that was in the isa
1173 assert((oldRefcnt & SIDE_TABLE_DEALLOCATING) == 0);
1174 assert((oldRefcnt & SIDE_TABLE_WEAKLY_REFERENCED) == 0);
1177 size_t refcnt = addc(oldRefcnt, extra_rc << SIDE_TABLE_RC_SHIFT, 0, &carry);
1178 if (carry) refcnt = SIDE_TABLE_RC_PINNED;
1179 if (isDeallocating) refcnt |= SIDE_TABLE_DEALLOCATING;
1180 if (weaklyReferenced) refcnt |= SIDE_TABLE_WEAKLY_REFERENCED;
1182 refcntStorage = refcnt;
1186 // Move some retain counts to the side table from the isa field.
1187 // Returns true if the object is now pinned.
1189 objc_object::sidetable_addExtraRC_nolock(size_t delta_rc)
1191 assert(isa.indexed);
1192 SideTable& table = SideTables()[this];
1194 size_t& refcntStorage = table.refcnts[this];
1195 size_t oldRefcnt = refcntStorage;
1196 // isa-side bits should not be set here
1197 assert((oldRefcnt & SIDE_TABLE_DEALLOCATING) == 0);
1198 assert((oldRefcnt & SIDE_TABLE_WEAKLY_REFERENCED) == 0);
1200 if (oldRefcnt & SIDE_TABLE_RC_PINNED) return true;
1204 addc(oldRefcnt, delta_rc << SIDE_TABLE_RC_SHIFT, 0, &carry);
1207 SIDE_TABLE_RC_PINNED | (oldRefcnt & SIDE_TABLE_FLAG_MASK);
1211 refcntStorage = newRefcnt;
1217 // Move some retain counts from the side table to the isa field.
1218 // Returns the actual count subtracted, which may be less than the request.
1220 objc_object::sidetable_subExtraRC_nolock(size_t delta_rc)
1222 assert(isa.indexed);
1223 SideTable& table = SideTables()[this];
1225 RefcountMap::iterator it = table.refcnts.find(this);
1226 if (it == table.refcnts.end() || it->second == 0) {
1227 // Side table retain count is zero. Can't borrow.
1230 size_t oldRefcnt = it->second;
1232 // isa-side bits should not be set here
1233 assert((oldRefcnt & SIDE_TABLE_DEALLOCATING) == 0);
1234 assert((oldRefcnt & SIDE_TABLE_WEAKLY_REFERENCED) == 0);
1236 size_t newRefcnt = oldRefcnt - (delta_rc << SIDE_TABLE_RC_SHIFT);
1237 assert(oldRefcnt > newRefcnt); // shouldn't underflow
1238 it->second = newRefcnt;
1244 objc_object::sidetable_getExtraRC_nolock()
1246 assert(isa.indexed);
1247 SideTable& table = SideTables()[this];
1248 RefcountMap::iterator it = table.refcnts.find(this);
1249 if (it == table.refcnts.end()) return 0;
1250 else return it->second >> SIDE_TABLE_RC_SHIFT;
1254 // SUPPORT_NONPOINTER_ISA
1258 __attribute__((used,noinline,nothrow))
1260 objc_object::sidetable_retain_slow(SideTable& table)
1262 #if SUPPORT_NONPOINTER_ISA
1263 assert(!isa.indexed);
1267 size_t& refcntStorage = table.refcnts[this];
1268 if (! (refcntStorage & SIDE_TABLE_RC_PINNED)) {
1269 refcntStorage += SIDE_TABLE_RC_ONE;
1278 objc_object::sidetable_retain()
1280 #if SUPPORT_NONPOINTER_ISA
1281 assert(!isa.indexed);
1283 SideTable& table = SideTables()[this];
1285 if (table.trylock()) {
1286 size_t& refcntStorage = table.refcnts[this];
1287 if (! (refcntStorage & SIDE_TABLE_RC_PINNED)) {
1288 refcntStorage += SIDE_TABLE_RC_ONE;
1293 return sidetable_retain_slow(table);
1298 objc_object::sidetable_tryRetain()
1300 #if SUPPORT_NONPOINTER_ISA
1301 assert(!isa.indexed);
1303 SideTable& table = SideTables()[this];
1306 // _objc_rootTryRetain() is called exclusively by _objc_loadWeak(),
1307 // which already acquired the lock on our behalf.
1309 // fixme can't do this efficiently with os_lock_handoff_s
1310 // if (table.slock == 0) {
1311 // _objc_fatal("Do not call -_tryRetain.");
1315 RefcountMap::iterator it = table.refcnts.find(this);
1316 if (it == table.refcnts.end()) {
1317 table.refcnts[this] = SIDE_TABLE_RC_ONE;
1318 } else if (it->second & SIDE_TABLE_DEALLOCATING) {
1320 } else if (! (it->second & SIDE_TABLE_RC_PINNED)) {
1321 it->second += SIDE_TABLE_RC_ONE;
1329 objc_object::sidetable_retainCount()
1331 SideTable& table = SideTables()[this];
1333 size_t refcnt_result = 1;
1336 RefcountMap::iterator it = table.refcnts.find(this);
1337 if (it != table.refcnts.end()) {
1338 // this is valid for SIDE_TABLE_RC_PINNED too
1339 refcnt_result += it->second >> SIDE_TABLE_RC_SHIFT;
1342 return refcnt_result;
1347 objc_object::sidetable_isDeallocating()
1349 SideTable& table = SideTables()[this];
1352 // _objc_rootIsDeallocating() is called exclusively by _objc_storeWeak(),
1353 // which already acquired the lock on our behalf.
1356 // fixme can't do this efficiently with os_lock_handoff_s
1357 // if (table.slock == 0) {
1358 // _objc_fatal("Do not call -_isDeallocating.");
1361 RefcountMap::iterator it = table.refcnts.find(this);
1362 return (it != table.refcnts.end()) && (it->second & SIDE_TABLE_DEALLOCATING);
1367 objc_object::sidetable_isWeaklyReferenced()
1369 bool result = false;
1371 SideTable& table = SideTables()[this];
1374 RefcountMap::iterator it = table.refcnts.find(this);
1375 if (it != table.refcnts.end()) {
1376 result = it->second & SIDE_TABLE_WEAKLY_REFERENCED;
1386 objc_object::sidetable_setWeaklyReferenced_nolock()
1388 #if SUPPORT_NONPOINTER_ISA
1389 assert(!isa.indexed);
1392 SideTable& table = SideTables()[this];
1394 table.refcnts[this] |= SIDE_TABLE_WEAKLY_REFERENCED;
1399 // return uintptr_t instead of bool so that the various raw-isa
1400 // -release paths all return zero in eax
1401 __attribute__((used,noinline,nothrow))
1403 objc_object::sidetable_release_slow(SideTable& table, bool performDealloc)
1405 #if SUPPORT_NONPOINTER_ISA
1406 assert(!isa.indexed);
1408 bool do_dealloc = false;
1411 RefcountMap::iterator it = table.refcnts.find(this);
1412 if (it == table.refcnts.end()) {
1414 table.refcnts[this] = SIDE_TABLE_DEALLOCATING;
1415 } else if (it->second < SIDE_TABLE_DEALLOCATING) {
1416 // SIDE_TABLE_WEAKLY_REFERENCED may be set. Don't change it.
1418 it->second |= SIDE_TABLE_DEALLOCATING;
1419 } else if (! (it->second & SIDE_TABLE_RC_PINNED)) {
1420 it->second -= SIDE_TABLE_RC_ONE;
1423 if (do_dealloc && performDealloc) {
1424 ((void(*)(objc_object *, SEL))objc_msgSend)(this, SEL_dealloc);
1431 // return uintptr_t instead of bool so that the various raw-isa
1432 // -release paths all return zero in eax
1434 objc_object::sidetable_release(bool performDealloc)
1436 #if SUPPORT_NONPOINTER_ISA
1437 assert(!isa.indexed);
1439 SideTable& table = SideTables()[this];
1441 bool do_dealloc = false;
1443 if (table.trylock()) {
1444 RefcountMap::iterator it = table.refcnts.find(this);
1445 if (it == table.refcnts.end()) {
1447 table.refcnts[this] = SIDE_TABLE_DEALLOCATING;
1448 } else if (it->second < SIDE_TABLE_DEALLOCATING) {
1449 // SIDE_TABLE_WEAKLY_REFERENCED may be set. Don't change it.
1451 it->second |= SIDE_TABLE_DEALLOCATING;
1452 } else if (! (it->second & SIDE_TABLE_RC_PINNED)) {
1453 it->second -= SIDE_TABLE_RC_ONE;
1456 if (do_dealloc && performDealloc) {
1457 ((void(*)(objc_object *, SEL))objc_msgSend)(this, SEL_dealloc);
1462 return sidetable_release_slow(table, performDealloc);
1467 objc_object::sidetable_clearDeallocating()
1469 SideTable& table = SideTables()[this];
1471 // clear any weak table items
1472 // clear extra retain count and deallocating bit
1473 // (fixme warn or abort if extra retain count == 0 ?)
1475 RefcountMap::iterator it = table.refcnts.find(this);
1476 if (it != table.refcnts.end()) {
1477 if (it->second & SIDE_TABLE_WEAKLY_REFERENCED) {
1478 weak_clear_no_lock(&table.weak_table, (id)this);
1480 table.refcnts.erase(it);
1486 /***********************************************************************
1487 * Optimized retain/release/autorelease entrypoints
1488 **********************************************************************/
1493 __attribute__((aligned(16)))
1497 if (!obj) return obj;
1498 if (obj->isTaggedPointer()) return obj;
1499 return obj->retain();
1503 __attribute__((aligned(16)))
1505 objc_release(id obj)
1508 if (obj->isTaggedPointer()) return;
1509 return obj->release();
1513 __attribute__((aligned(16)))
1515 objc_autorelease(id obj)
1517 if (!obj) return obj;
1518 if (obj->isTaggedPointer()) return obj;
1519 return obj->autorelease();
1528 id objc_retain(id obj) { return [obj retain]; }
1529 void objc_release(id obj) { [obj release]; }
1530 id objc_autorelease(id obj) { return [obj autorelease]; }
1536 /***********************************************************************
1537 * Basic operations for root class implementations a.k.a. _objc_root*()
1538 **********************************************************************/
1541 _objc_rootTryRetain(id obj)
1545 return obj->rootTryRetain();
1549 _objc_rootIsDeallocating(id obj)
1553 return obj->rootIsDeallocating();
1558 objc_clear_deallocating(id obj)
1563 if (obj->isTaggedPointer()) return;
1564 obj->clearDeallocating();
1569 _objc_rootReleaseWasZero(id obj)
1573 return obj->rootReleaseShouldDealloc();
1578 _objc_rootAutorelease(id obj)
1582 if (UseGC) return obj; // fixme CF calls this when GC is on
1584 return obj->rootAutorelease();
1588 _objc_rootRetainCount(id obj)
1592 return obj->rootRetainCount();
1597 _objc_rootRetain(id obj)
1601 return obj->rootRetain();
1605 _objc_rootRelease(id obj)
1614 _objc_rootAllocWithZone(Class cls, malloc_zone_t *zone)
1619 // allocWithZone under __OBJC2__ ignores the zone parameter
1621 obj = class_createInstance(cls, 0);
1623 if (!zone || UseGC) {
1624 obj = class_createInstance(cls, 0);
1627 obj = class_createInstanceFromZone(cls, 0, zone);
1631 if (!obj) obj = callBadAllocHandler(cls);
1636 // Call [cls alloc] or [cls allocWithZone:nil], with appropriate
1637 // shortcutting optimizations.
1638 static ALWAYS_INLINE id
1639 callAlloc(Class cls, bool checkNil, bool allocWithZone=false)
1641 if (checkNil && !cls) return nil;
1644 if (! cls->ISA()->hasCustomAWZ()) {
1645 // No alloc/allocWithZone implementation. Go straight to the allocator.
1646 // fixme store hasCustomAWZ in the non-meta class and
1647 // add it to canAllocFast's summary
1648 if (cls->canAllocFast()) {
1649 // No ctors, raw isa, etc. Go straight to the metal.
1650 bool dtor = cls->hasCxxDtor();
1651 id obj = (id)calloc(1, cls->bits.fastInstanceSize());
1652 if (!obj) return callBadAllocHandler(cls);
1653 obj->initInstanceIsa(cls, dtor);
1657 // Has ctor or raw isa or something. Use the slower path.
1658 id obj = class_createInstance(cls, 0);
1659 if (!obj) return callBadAllocHandler(cls);
1665 // No shortcuts available.
1666 if (allocWithZone) return [cls allocWithZone:nil];
1671 // Base class implementation of +alloc. cls is not nil.
1672 // Calls [cls allocWithZone:nil].
1674 _objc_rootAlloc(Class cls)
1676 return callAlloc(cls, false/*checkNil*/, true/*allocWithZone*/);
1679 // Calls [cls alloc].
1681 objc_alloc(Class cls)
1683 return callAlloc(cls, true/*checkNil*/, false/*allocWithZone*/);
1686 // Calls [cls allocWithZone:nil].
1688 objc_allocWithZone(Class cls)
1690 return callAlloc(cls, true/*checkNil*/, true/*allocWithZone*/);
1695 _objc_rootDealloc(id obj)
1703 _objc_rootFinalize(id obj __unused)
1711 _objc_fatal("_objc_rootFinalize called with garbage collection off");
1716 _objc_rootInit(id obj)
1718 // In practice, it will be hard to rely on this function.
1719 // Many classes do not properly chain -init calls.
1725 _objc_rootZone(id obj)
1732 // allocWithZone under __OBJC2__ ignores the zone parameter
1733 return malloc_default_zone();
1735 malloc_zone_t *rval = malloc_zone_from_ptr(obj);
1736 return rval ? rval : malloc_default_zone();
1741 _objc_rootHash(id obj)
1744 return _object_getExternalHash(obj);
1746 return (uintptr_t)obj;
1750 objc_autoreleasePoolPush(void)
1752 if (UseGC) return nil;
1753 return AutoreleasePoolPage::push();
1757 objc_autoreleasePoolPop(void *ctxt)
1760 AutoreleasePoolPage::pop(ctxt);
1765 _objc_autoreleasePoolPush(void)
1767 return objc_autoreleasePoolPush();
1771 _objc_autoreleasePoolPop(void *ctxt)
1773 objc_autoreleasePoolPop(ctxt);
1777 _objc_autoreleasePoolPrint(void)
1780 AutoreleasePoolPage::printAll();
1784 // Same as objc_release but suitable for tail-calling
1785 // if you need the value back and don't want to push a frame before this point.
1786 __attribute__((noinline))
1788 objc_releaseAndReturn(id obj)
1794 // Same as objc_retainAutorelease but suitable for tail-calling
1795 // if you don't want to push a frame before this point.
1796 __attribute__((noinline))
1798 objc_retainAutoreleaseAndReturn(id obj)
1800 return objc_retainAutorelease(obj);
1804 // Prepare a value at +1 for return through a +0 autoreleasing convention.
1806 objc_autoreleaseReturnValue(id obj)
1808 if (prepareOptimizedReturn(ReturnAtPlus1)) return obj;
1810 return objc_autorelease(obj);
1813 // Prepare a value at +0 for return through a +0 autoreleasing convention.
1815 objc_retainAutoreleaseReturnValue(id obj)
1817 if (prepareOptimizedReturn(ReturnAtPlus0)) return obj;
1819 // not objc_autoreleaseReturnValue(objc_retain(obj))
1820 // because we don't need another optimization attempt
1821 return objc_retainAutoreleaseAndReturn(obj);
1824 // Accept a value returned through a +0 autoreleasing convention for use at +1.
1826 objc_retainAutoreleasedReturnValue(id obj)
1828 if (acceptOptimizedReturn() == ReturnAtPlus1) return obj;
1830 return objc_retain(obj);
1833 // Accept a value returned through a +0 autoreleasing convention for use at +0.
1835 objc_unsafeClaimAutoreleasedReturnValue(id obj)
1837 if (acceptOptimizedReturn() == ReturnAtPlus0) return obj;
1839 return objc_releaseAndReturn(obj);
1843 objc_retainAutorelease(id obj)
1845 return objc_autorelease(objc_retain(obj));
1849 _objc_deallocOnMainThreadHelper(void *context)
1851 id obj = (id)context;
1855 #undef objc_retainedObject
1856 #undef objc_unretainedObject
1857 #undef objc_unretainedPointer
1859 // convert objc_objectptr_t to id, callee must take ownership.
1860 id objc_retainedObject(objc_objectptr_t pointer) { return (id)pointer; }
1862 // convert objc_objectptr_t to id, without ownership transfer.
1863 id objc_unretainedObject(objc_objectptr_t pointer) { return (id)pointer; }
1865 // convert id to objc_objectptr_t, no ownership transfer.
1866 objc_objectptr_t objc_unretainedPointer(id object) { return object; }
1871 AutoreleasePoolPage::init();
1875 @implementation NSObject
1878 if (UseGC) gc_init2();
1881 + (void)initialize {
1897 return object_getClass(self);
1900 + (Class)superclass {
1901 return self->superclass;
1904 - (Class)superclass {
1905 return [self class]->superclass;
1908 + (BOOL)isMemberOfClass:(Class)cls {
1909 return object_getClass((id)self) == cls;
1912 - (BOOL)isMemberOfClass:(Class)cls {
1913 return [self class] == cls;
1916 + (BOOL)isKindOfClass:(Class)cls {
1917 for (Class tcls = object_getClass((id)self); tcls; tcls = tcls->superclass) {
1918 if (tcls == cls) return YES;
1923 - (BOOL)isKindOfClass:(Class)cls {
1924 for (Class tcls = [self class]; tcls; tcls = tcls->superclass) {
1925 if (tcls == cls) return YES;
1930 + (BOOL)isSubclassOfClass:(Class)cls {
1931 for (Class tcls = self; tcls; tcls = tcls->superclass) {
1932 if (tcls == cls) return YES;
1937 + (BOOL)isAncestorOfObject:(NSObject *)obj {
1938 for (Class tcls = [obj class]; tcls; tcls = tcls->superclass) {
1939 if (tcls == self) return YES;
1944 + (BOOL)instancesRespondToSelector:(SEL)sel {
1945 if (!sel) return NO;
1946 return class_respondsToSelector(self, sel);
1949 + (BOOL)respondsToSelector:(SEL)sel {
1950 if (!sel) return NO;
1951 return class_respondsToSelector_inst(object_getClass(self), sel, self);
1954 - (BOOL)respondsToSelector:(SEL)sel {
1955 if (!sel) return NO;
1956 return class_respondsToSelector_inst([self class], sel, self);
1959 + (BOOL)conformsToProtocol:(Protocol *)protocol {
1960 if (!protocol) return NO;
1961 for (Class tcls = self; tcls; tcls = tcls->superclass) {
1962 if (class_conformsToProtocol(tcls, protocol)) return YES;
1967 - (BOOL)conformsToProtocol:(Protocol *)protocol {
1968 if (!protocol) return NO;
1969 for (Class tcls = [self class]; tcls; tcls = tcls->superclass) {
1970 if (class_conformsToProtocol(tcls, protocol)) return YES;
1975 + (NSUInteger)hash {
1976 return _objc_rootHash(self);
1979 - (NSUInteger)hash {
1980 return _objc_rootHash(self);
1983 + (BOOL)isEqual:(id)obj {
1984 return obj == (id)self;
1987 - (BOOL)isEqual:(id)obj {
2009 + (IMP)instanceMethodForSelector:(SEL)sel {
2010 if (!sel) [self doesNotRecognizeSelector:sel];
2011 return class_getMethodImplementation(self, sel);
2014 + (IMP)methodForSelector:(SEL)sel {
2015 if (!sel) [self doesNotRecognizeSelector:sel];
2016 return object_getMethodImplementation((id)self, sel);
2019 - (IMP)methodForSelector:(SEL)sel {
2020 if (!sel) [self doesNotRecognizeSelector:sel];
2021 return object_getMethodImplementation(self, sel);
2024 + (BOOL)resolveClassMethod:(SEL)sel {
2028 + (BOOL)resolveInstanceMethod:(SEL)sel {
2032 // Replaced by CF (throws an NSException)
2033 + (void)doesNotRecognizeSelector:(SEL)sel {
2034 _objc_fatal("+[%s %s]: unrecognized selector sent to instance %p",
2035 class_getName(self), sel_getName(sel), self);
2038 // Replaced by CF (throws an NSException)
2039 - (void)doesNotRecognizeSelector:(SEL)sel {
2040 _objc_fatal("-[%s %s]: unrecognized selector sent to instance %p",
2041 object_getClassName(self), sel_getName(sel), self);
2045 + (id)performSelector:(SEL)sel {
2046 if (!sel) [self doesNotRecognizeSelector:sel];
2047 return ((id(*)(id, SEL))objc_msgSend)((id)self, sel);
2050 + (id)performSelector:(SEL)sel withObject:(id)obj {
2051 if (!sel) [self doesNotRecognizeSelector:sel];
2052 return ((id(*)(id, SEL, id))objc_msgSend)((id)self, sel, obj);
2055 + (id)performSelector:(SEL)sel withObject:(id)obj1 withObject:(id)obj2 {
2056 if (!sel) [self doesNotRecognizeSelector:sel];
2057 return ((id(*)(id, SEL, id, id))objc_msgSend)((id)self, sel, obj1, obj2);
2060 - (id)performSelector:(SEL)sel {
2061 if (!sel) [self doesNotRecognizeSelector:sel];
2062 return ((id(*)(id, SEL))objc_msgSend)(self, sel);
2065 - (id)performSelector:(SEL)sel withObject:(id)obj {
2066 if (!sel) [self doesNotRecognizeSelector:sel];
2067 return ((id(*)(id, SEL, id))objc_msgSend)(self, sel, obj);
2070 - (id)performSelector:(SEL)sel withObject:(id)obj1 withObject:(id)obj2 {
2071 if (!sel) [self doesNotRecognizeSelector:sel];
2072 return ((id(*)(id, SEL, id, id))objc_msgSend)(self, sel, obj1, obj2);
2076 // Replaced by CF (returns an NSMethodSignature)
2077 + (NSMethodSignature *)instanceMethodSignatureForSelector:(SEL)sel {
2078 _objc_fatal("+[NSObject instanceMethodSignatureForSelector:] "
2079 "not available without CoreFoundation");
2082 // Replaced by CF (returns an NSMethodSignature)
2083 + (NSMethodSignature *)methodSignatureForSelector:(SEL)sel {
2084 _objc_fatal("+[NSObject methodSignatureForSelector:] "
2085 "not available without CoreFoundation");
2088 // Replaced by CF (returns an NSMethodSignature)
2089 - (NSMethodSignature *)methodSignatureForSelector:(SEL)sel {
2090 _objc_fatal("-[NSObject methodSignatureForSelector:] "
2091 "not available without CoreFoundation");
2094 + (void)forwardInvocation:(NSInvocation *)invocation {
2095 [self doesNotRecognizeSelector:(invocation ? [invocation selector] : 0)];
2098 - (void)forwardInvocation:(NSInvocation *)invocation {
2099 [self doesNotRecognizeSelector:(invocation ? [invocation selector] : 0)];
2102 + (id)forwardingTargetForSelector:(SEL)sel {
2106 - (id)forwardingTargetForSelector:(SEL)sel {
2111 // Replaced by CF (returns an NSString)
2112 + (NSString *)description {
2116 // Replaced by CF (returns an NSString)
2117 - (NSString *)description {
2121 + (NSString *)debugDescription {
2122 return [self description];
2125 - (NSString *)debugDescription {
2126 return [self description];
2131 return [callAlloc(self, false/*checkNil*/) init];
2138 // Replaced by ObjectAlloc
2140 return ((id)self)->rootRetain();
2144 + (BOOL)_tryRetain {
2148 // Replaced by ObjectAlloc
2149 - (BOOL)_tryRetain {
2150 return ((id)self)->rootTryRetain();
2153 + (BOOL)_isDeallocating {
2157 - (BOOL)_isDeallocating {
2158 return ((id)self)->rootIsDeallocating();
2161 + (BOOL)allowsWeakReference {
2165 + (BOOL)retainWeakReference {
2169 - (BOOL)allowsWeakReference {
2170 return ! [self _isDeallocating];
2173 - (BOOL)retainWeakReference {
2174 return [self _tryRetain];
2177 + (oneway void)release {
2180 // Replaced by ObjectAlloc
2181 - (oneway void)release {
2182 ((id)self)->rootRelease();
2189 // Replaced by ObjectAlloc
2191 return ((id)self)->rootAutorelease();
2194 + (NSUInteger)retainCount {
2198 - (NSUInteger)retainCount {
2199 return ((id)self)->rootRetainCount();
2203 return _objc_rootAlloc(self);
2206 // Replaced by ObjectAlloc
2207 + (id)allocWithZone:(struct _NSZone *)zone {
2208 return _objc_rootAllocWithZone(self, (malloc_zone_t *)zone);
2211 // Replaced by CF (throws an NSException)
2217 return _objc_rootInit(self);
2220 // Replaced by CF (throws an NSException)
2225 // Replaced by NSZombies
2227 _objc_rootDealloc(self);
2230 // Replaced by CF (throws an NSException)
2235 _objc_rootFinalize(self);
2238 + (struct _NSZone *)zone {
2239 return (struct _NSZone *)_objc_rootZone(self);
2242 - (struct _NSZone *)zone {
2243 return (struct _NSZone *)_objc_rootZone(self);
2250 + (id)copyWithZone:(struct _NSZone *)zone {
2255 return [(id)self copyWithZone:nil];
2262 + (id)mutableCopyWithZone:(struct _NSZone *)zone {
2267 return [(id)self mutableCopyWithZone:nil];