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
This commit is contained in:
theraven 2011-05-25 11:15:08 +00:00
parent cf144e76f1
commit 485d23c7c7
7 changed files with 20 additions and 26 deletions

View file

@ -308,14 +308,14 @@ enum {
* garbage collected itsself.<br /> * garbage collected itsself.<br />
* In any case the memory returned is zero'ed. * In any case the memory returned is zero'ed.
*/ */
GS_EXPORT void * GS_EXPORT __strong void *
NSAllocateCollectable(NSUInteger size, NSUInteger options); NSAllocateCollectable(NSUInteger size, NSUInteger options);
/** Reallocate memory to be of a different size and/or to have different /** Reallocate memory to be of a different size and/or to have different
* options settings. The behavior of options is as for * options settings. The behavior of options is as for
* the NSAllocateCollectable() function. * the NSAllocateCollectable() function.
*/ */
GS_EXPORT void * GS_EXPORT __strong void *
NSReallocateCollectable(void *ptr, NSUInteger size, NSUInteger options); NSReallocateCollectable(void *ptr, NSUInteger size, NSUInteger options);
#endif #endif

View file

@ -390,11 +390,7 @@ fixBOM(unsigned char **bytes, NSUInteger*length, BOOL *owned,
len -= sizeof(unichar); len -= sizeof(unichar);
memcpy(&u, from, sizeof(unichar)); memcpy(&u, from, sizeof(unichar));
from += sizeof(unichar); from += sizeof(unichar);
#if GS_WITH_GC
to = NSAllocateCollectable(len, 0); to = NSAllocateCollectable(len, 0);
#else
to = NSZoneMalloc(NSDefaultMallocZone(), len);
#endif
if (u == 0xFEFF) if (u == 0xFEFF)
{ {
// Native byte order // Native byte order
@ -443,11 +439,7 @@ fixBOM(unsigned char **bytes, NSUInteger*length, BOOL *owned,
// Got a byte order marker ... remove it. // Got a byte order marker ... remove it.
len -= 3; len -= 3;
from += 3; from += 3;
#if GS_WITH_GC
to = NSAllocateCollectable(len, 0); to = NSAllocateCollectable(len, 0);
#else
to = NSZoneMalloc(NSDefaultMallocZone(), len);
#endif
memcpy(to, from, len); memcpy(to, from, len);
if (*owned == YES) if (*owned == YES)
{ {
@ -485,7 +477,7 @@ fixBOM(unsigned char **bytes, NSUInteger*length, BOOL *owned,
*/ */
if (original == bytes) if (original == bytes)
{ {
#if GS_WITH_GC #if GS_WITH_GC || __OBJC_GC__
chars = NSAllocateCollectable(length, 0); chars = NSAllocateCollectable(length, 0);
#else #else
chars = NSZoneMalloc([self zone], length); chars = NSZoneMalloc([self zone], length);

View file

@ -575,11 +575,7 @@ failure:
format: @"[%@-initWithBytes:length:] called with " format: @"[%@-initWithBytes:length:] called with "
@"length but null bytes", NSStringFromClass([self class])]; @"length but null bytes", NSStringFromClass([self class])];
} }
#if GS_WITH_GC
ptr = NSAllocateCollectable(bufferSize, 0); ptr = NSAllocateCollectable(bufferSize, 0);
#else
ptr = NSZoneMalloc(NSDefaultMallocZone(), bufferSize);
#endif
if (ptr == 0) if (ptr == 0)
{ {
DESTROY(self); DESTROY(self);

View file

@ -443,7 +443,7 @@ next_arg(const char *typePtr, NSArgumentInfo *info, char *outTypes)
alen = ptr - args; alen = ptr - args;
rlen += sprintf(ret + rlen, "%d", (int)_argFrameLength); 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, ret, rlen);
strncpy(((char*)_methodTypes) + rlen, args, alen); strncpy(((char*)_methodTypes) + rlen, args, alen);
((char*)_methodTypes)[alen + rlen] = '\0'; ((char*)_methodTypes)[alen + rlen] = '\0';

View file

@ -142,7 +142,7 @@ struct NCTbl; /* Notification Center Table structure */
*/ */
typedef struct Obs { typedef struct Obs {
id observer; /* Object to receive message. */ __weak id observer; /* Object to receive message. */
SEL selector; /* Method selector. */ SEL selector; /* Method selector. */
IMP method; /* Method implementation. */ IMP method; /* Method implementation. */
struct Obs *next; /* Next item in linked list. */ struct Obs *next; /* Next item in linked list. */

View file

@ -494,7 +494,7 @@ NSDecrementExtraRefCountWasZero(id anObject)
inline NSUInteger inline NSUInteger
NSExtraRefCount(id anObject) NSExtraRefCount(id anObject)
{ {
#if GS_WITH_GC #if GS_WITH_GC || __OBJC_GC__
return UINT_MAX - 1; return UINT_MAX - 1;
#else /* GS_WITH_GC */ #else /* GS_WITH_GC */
return ((obj)anObject)[-1].retained; return ((obj)anObject)[-1].retained;
@ -1723,7 +1723,7 @@ objc_create_block_classes_as_subclasses_of(Class super);
*/ */
- (id) autorelease - (id) autorelease
{ {
#if GS_WITH_GC == 0 #if !GS_WITH_GC && !__OBJC_GC__
if (double_release_check_enabled) if (double_release_check_enabled)
{ {
NSUInteger release_count; NSUInteger release_count;
@ -1935,7 +1935,7 @@ objc_create_block_classes_as_subclasses_of(Class super);
*/ */
- (oneway void) release - (oneway void) release
{ {
#if GS_WITH_GC == 0 && !__OBJC_GC__ #if (GS_WITH_GC == 0) && !__OBJC_GC__
if (NSDecrementExtraRefCountWasZero(self)) if (NSDecrementExtraRefCountWasZero(self))
{ {
[self dealloc]; [self dealloc];
@ -1985,7 +1985,7 @@ objc_create_block_classes_as_subclasses_of(Class super);
*/ */
- (id) retain - (id) retain
{ {
#if GS_WITH_GC == 0 && !__OBJC_GC__ #if (GS_WITH_GC == 0) && !__OBJC_GC__
NSIncrementExtraRefCount(self); NSIncrementExtraRefCount(self);
#endif #endif
return self; return self;
@ -2012,7 +2012,7 @@ objc_create_block_classes_as_subclasses_of(Class super);
*/ */
- (NSUInteger) retainCount - (NSUInteger) retainCount
{ {
#if GS_WITH_GC #if GS_WITH_GC || __OBJC_GC__
return UINT_MAX; return UINT_MAX;
#else #else
return NSExtraRefCount(self) + 1; return NSExtraRefCount(self) + 1;
@ -2042,7 +2042,7 @@ objc_create_block_classes_as_subclasses_of(Class super);
*/ */
- (NSZone*) zone - (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. /* MacOS-X 10.5 seems to return the default malloc zone if GC is enabled.
*/ */
return NSDefaultMallocZone(); return NSDefaultMallocZone();

View file

@ -230,14 +230,20 @@ NSZoneName (NSZone *zone)
#if __OBJC_GC__ #if __OBJC_GC__
#include <objc/objc-auto.h> #include <objc/objc-auto.h>
void *
__strong void *
NSAllocateCollectable(NSUInteger size, NSUInteger options) NSAllocateCollectable(NSUInteger size, NSUInteger options)
{ {
return objc_gc_allocate_collectable(size, id obj = objc_gc_allocate_collectable(size,
((options & NSScannedOption) == NSScannedOption)); ((options & NSScannedOption) == NSScannedOption));
if ((options & NSCollectorDisabledOption) == NSCollectorDisabledOption)
{
obj = objc_gc_retain(obj);
}
return obj;
} }
void * __strong void *
NSReallocateCollectable(void *ptr, NSUInteger size, NSUInteger options) NSReallocateCollectable(void *ptr, NSUInteger size, NSUInteger options)
{ {
return objc_gc_reallocate_collectable(ptr, size, return objc_gc_reallocate_collectable(ptr, size,