From 485d23c7c7a15e7c3a02c2c30b3036da16113aac Mon Sep 17 00:00:00 2001 From: theraven Date: Wed, 25 May 2011 11:15:08 +0000 Subject: [PATCH] More tweaks for garbage collection mode, including making NSNotificationCenter use weak pointers (things are never removed if it uses strong pointers because they remove themselves in the -dealloc method, which is never called, and can't remove themselves in the -finalize method because the -finalize method would not be called until after they have been removed - this is consistent with Apple behaviour). Gorm now works correctly when built with GC enabled. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@33109 72102866-910b-0410-8b05-ffd578937521 --- Headers/Foundation/NSZone.h | 4 ++-- Source/GSString.m | 10 +--------- Source/NSData.m | 4 ---- Source/NSMethodSignature.m | 2 +- Source/NSNotificationCenter.m | 2 +- Source/NSObject.m | 12 ++++++------ Source/NSZone.m | 12 +++++++++--- 7 files changed, 20 insertions(+), 26 deletions(-) diff --git a/Headers/Foundation/NSZone.h b/Headers/Foundation/NSZone.h index 14d317f42..7fbe3e5d9 100644 --- a/Headers/Foundation/NSZone.h +++ b/Headers/Foundation/NSZone.h @@ -308,14 +308,14 @@ enum { * garbage collected itsself.
* In any case the memory returned is zero'ed. */ -GS_EXPORT void * +GS_EXPORT __strong void * NSAllocateCollectable(NSUInteger size, NSUInteger options); /** Reallocate memory to be of a different size and/or to have different * options settings. The behavior of options is as for * the NSAllocateCollectable() function. */ -GS_EXPORT void * +GS_EXPORT __strong void * NSReallocateCollectable(void *ptr, NSUInteger size, NSUInteger options); #endif diff --git a/Source/GSString.m b/Source/GSString.m index f125e17cc..2d6a6c4d0 100644 --- a/Source/GSString.m +++ b/Source/GSString.m @@ -390,11 +390,7 @@ fixBOM(unsigned char **bytes, NSUInteger*length, BOOL *owned, len -= sizeof(unichar); memcpy(&u, from, sizeof(unichar)); from += sizeof(unichar); -#if GS_WITH_GC to = NSAllocateCollectable(len, 0); -#else - to = NSZoneMalloc(NSDefaultMallocZone(), len); -#endif if (u == 0xFEFF) { // Native byte order @@ -443,11 +439,7 @@ fixBOM(unsigned char **bytes, NSUInteger*length, BOOL *owned, // Got a byte order marker ... remove it. len -= 3; from += 3; -#if GS_WITH_GC to = NSAllocateCollectable(len, 0); -#else - to = NSZoneMalloc(NSDefaultMallocZone(), len); -#endif memcpy(to, from, len); if (*owned == YES) { @@ -485,7 +477,7 @@ fixBOM(unsigned char **bytes, NSUInteger*length, BOOL *owned, */ if (original == bytes) { -#if GS_WITH_GC +#if GS_WITH_GC || __OBJC_GC__ chars = NSAllocateCollectable(length, 0); #else chars = NSZoneMalloc([self zone], length); diff --git a/Source/NSData.m b/Source/NSData.m index 368f255f6..fdbff085f 100644 --- a/Source/NSData.m +++ b/Source/NSData.m @@ -575,11 +575,7 @@ failure: format: @"[%@-initWithBytes:length:] called with " @"length but null bytes", NSStringFromClass([self class])]; } -#if GS_WITH_GC ptr = NSAllocateCollectable(bufferSize, 0); -#else - ptr = NSZoneMalloc(NSDefaultMallocZone(), bufferSize); -#endif if (ptr == 0) { DESTROY(self); diff --git a/Source/NSMethodSignature.m b/Source/NSMethodSignature.m index 71d3946d1..0cd1adeac 100644 --- a/Source/NSMethodSignature.m +++ b/Source/NSMethodSignature.m @@ -443,7 +443,7 @@ next_arg(const char *typePtr, NSArgumentInfo *info, char *outTypes) alen = ptr - args; rlen += sprintf(ret + rlen, "%d", (int)_argFrameLength); - _methodTypes = NSZoneMalloc(NSDefaultMallocZone(), alen + rlen + 1); + _methodTypes = NSAllocateCollectable(alen + rlen + 1, 0); strncpy((char*)_methodTypes, ret, rlen); strncpy(((char*)_methodTypes) + rlen, args, alen); ((char*)_methodTypes)[alen + rlen] = '\0'; diff --git a/Source/NSNotificationCenter.m b/Source/NSNotificationCenter.m index ea4fe2fe1..15718b762 100644 --- a/Source/NSNotificationCenter.m +++ b/Source/NSNotificationCenter.m @@ -142,7 +142,7 @@ struct NCTbl; /* Notification Center Table structure */ */ typedef struct Obs { - id observer; /* Object to receive message. */ + __weak id observer; /* Object to receive message. */ SEL selector; /* Method selector. */ IMP method; /* Method implementation. */ struct Obs *next; /* Next item in linked list. */ diff --git a/Source/NSObject.m b/Source/NSObject.m index 60b5fb0ed..6124e5006 100644 --- a/Source/NSObject.m +++ b/Source/NSObject.m @@ -494,7 +494,7 @@ NSDecrementExtraRefCountWasZero(id anObject) inline NSUInteger NSExtraRefCount(id anObject) { -#if GS_WITH_GC +#if GS_WITH_GC || __OBJC_GC__ return UINT_MAX - 1; #else /* GS_WITH_GC */ return ((obj)anObject)[-1].retained; @@ -1723,7 +1723,7 @@ objc_create_block_classes_as_subclasses_of(Class super); */ - (id) autorelease { -#if GS_WITH_GC == 0 +#if !GS_WITH_GC && !__OBJC_GC__ if (double_release_check_enabled) { NSUInteger release_count; @@ -1935,7 +1935,7 @@ objc_create_block_classes_as_subclasses_of(Class super); */ - (oneway void) release { -#if GS_WITH_GC == 0 && !__OBJC_GC__ +#if (GS_WITH_GC == 0) && !__OBJC_GC__ if (NSDecrementExtraRefCountWasZero(self)) { [self dealloc]; @@ -1985,7 +1985,7 @@ objc_create_block_classes_as_subclasses_of(Class super); */ - (id) retain { -#if GS_WITH_GC == 0 && !__OBJC_GC__ +#if (GS_WITH_GC == 0) && !__OBJC_GC__ NSIncrementExtraRefCount(self); #endif return self; @@ -2012,7 +2012,7 @@ objc_create_block_classes_as_subclasses_of(Class super); */ - (NSUInteger) retainCount { -#if GS_WITH_GC +#if GS_WITH_GC || __OBJC_GC__ return UINT_MAX; #else return NSExtraRefCount(self) + 1; @@ -2042,7 +2042,7 @@ objc_create_block_classes_as_subclasses_of(Class super); */ - (NSZone*) zone { -#if GS_WITH_GC +#if GS_WITH_GC || __OBJC_GC__ /* MacOS-X 10.5 seems to return the default malloc zone if GC is enabled. */ return NSDefaultMallocZone(); diff --git a/Source/NSZone.m b/Source/NSZone.m index f9aee896f..117f90251 100644 --- a/Source/NSZone.m +++ b/Source/NSZone.m @@ -230,14 +230,20 @@ NSZoneName (NSZone *zone) #if __OBJC_GC__ #include -void * + +__strong void * NSAllocateCollectable(NSUInteger size, NSUInteger options) { - return objc_gc_allocate_collectable(size, + id obj = objc_gc_allocate_collectable(size, ((options & NSScannedOption) == NSScannedOption)); + if ((options & NSCollectorDisabledOption) == NSCollectorDisabledOption) + { + obj = objc_gc_retain(obj); + } + return obj; } -void * +__strong void * NSReallocateCollectable(void *ptr, NSUInteger size, NSUInteger options) { return objc_gc_reallocate_collectable(ptr, size,