From ba6f3867a801013a2e4c4bc50060ea86f4c1d930 Mon Sep 17 00:00:00 2001 From: theraven Date: Thu, 26 May 2011 13:24:13 +0000 Subject: [PATCH] More GC fixes. Most notably, mark the thread object as not collectable, since it's hidden away in TLS where the GC can't find it. GC now works well enough for LanguageKit to run. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@33123 72102866-910b-0410-8b05-ffd578937521 --- Headers/Foundation/NSNotification.h | 2 +- Source/Additions/GSLock.m | 4 ++++ Source/Additions/GSObjCRuntime.m | 2 +- Source/Additions/Unicode.m | 10 ++++++---- Source/GSString.m | 2 +- Source/NSThread.m | 11 +++++++++++ Source/NSZone.m | 5 +++++ 7 files changed, 29 insertions(+), 7 deletions(-) diff --git a/Headers/Foundation/NSNotification.h b/Headers/Foundation/NSNotification.h index ee4b1e633..a3905e9e3 100644 --- a/Headers/Foundation/NSNotification.h +++ b/Headers/Foundation/NSNotification.h @@ -65,7 +65,7 @@ extern "C" { { #if GS_EXPOSE(NSNotificationCenter) @private - void *_table; + __strong void *_table; #endif } diff --git a/Source/Additions/GSLock.m b/Source/Additions/GSLock.m index 14c996b1a..29ff8ac8b 100644 --- a/Source/Additions/GSLock.m +++ b/Source/Additions/GSLock.m @@ -72,11 +72,13 @@ locked = -1; } +#ifdef __GS_WITH_GC__ - (void) finalize { [[NSNotificationCenter defaultCenter] removeObserver: self]; [super finalize]; } +#endif - (id) init { @@ -214,11 +216,13 @@ counter = -1; } +#ifdef __GS_WITH_GC__ - (void) finalize { [[NSNotificationCenter defaultCenter] removeObserver: self]; [super finalize]; } +#endif - (id) init { diff --git a/Source/Additions/GSObjCRuntime.m b/Source/Additions/GSObjCRuntime.m index e992dac90..a88775a63 100644 --- a/Source/Additions/GSObjCRuntime.m +++ b/Source/Additions/GSObjCRuntime.m @@ -1928,7 +1928,7 @@ NSArray *GSObjCDirectSubclassesOfClass(Class cls) void * GSAutoreleasedBuffer(unsigned size) { -#if GS_WITH_GC +#if GS_WITH_GC || __OBJC_GC__ return NSAllocateCollectable(size, NSScannedOption); #else #ifdef ALIGN diff --git a/Source/Additions/Unicode.m b/Source/Additions/Unicode.m index 00cce04ec..457cca676 100644 --- a/Source/Additions/Unicode.m +++ b/Source/Additions/Unicode.m @@ -28,6 +28,7 @@ Boston, MA 02111 USA. */ + #import "common.h" #if !defined(NeXT_Foundation_LIBRARY) #import "Foundation/NSArray.h" @@ -304,7 +305,7 @@ static void GSSetupEncodingTable(void) } } } - encTable = NSZoneMalloc(NSDefaultMallocZone(), + encTable = malloc( (encTableSize+1)*sizeof(struct _strenc_ *)); memset(encTable, 0, (encTableSize+1)*sizeof(struct _strenc_ *)); @@ -331,13 +332,13 @@ static void GSSetupEncodingTable(void) * See if we can do a lossy conversion. */ l = strlen(entry->iconv); - lossy = NSZoneMalloc(NSDefaultMallocZone(), l + 11); + lossy = malloc(l + 11); strncpy(lossy, entry->iconv, l); strncpy(lossy + l, "//TRANSLIT", 11); c = iconv_open(UNICODE_ENC, entry->iconv); if (c == (iconv_t)-1) { - NSZoneFree(NSDefaultMallocZone(),lossy); + free(lossy); } else { @@ -371,6 +372,7 @@ EntryForEncoding(NSStringEncoding enc) while (i < sizeof(str_encoding_table) / sizeof(struct _strenc_)) { + NSLog(@"Checking encoding %s (%d == %d) %d", str_encoding_table[i].ename, str_encoding_table[i].enc, enc, str_encoding_table[i].supported); if (str_encoding_table[i].enc == enc) { entry = &str_encoding_table[i]; @@ -2520,7 +2522,7 @@ GSPrivateAvailableEncodings() * This is also the place where we determine the name we use * for iconv to support unicode. */ - encodings = NSZoneMalloc(NSDefaultMallocZone(),sizeof(NSStringEncoding) * (encTableSize+1)); + encodings = malloc(sizeof(NSStringEncoding) * (encTableSize+1)); pos = 0; for (i = 0; i < encTableSize+1; i++) { diff --git a/Source/GSString.m b/Source/GSString.m index 2d6a6c4d0..1e45b7f53 100644 --- a/Source/GSString.m +++ b/Source/GSString.m @@ -477,7 +477,7 @@ fixBOM(unsigned char **bytes, NSUInteger*length, BOOL *owned, */ if (original == bytes) { -#if GS_WITH_GC || __OBJC_GC__ +#if GS_WITH_GC chars = NSAllocateCollectable(length, 0); #else chars = NSZoneMalloc([self zone], length); diff --git a/Source/NSThread.m b/Source/NSThread.m index dd471b611..9b72397d5 100644 --- a/Source/NSThread.m +++ b/Source/NSThread.m @@ -72,6 +72,7 @@ #import "Foundation/NSConnection.h" #import "Foundation/NSInvocation.h" #import "Foundation/NSUserDefaults.h" +#import "Foundation/NSGarbageCollector.h" #import "GSPrivate.h" #import "GSRunLoopCtxt.h" @@ -79,6 +80,9 @@ #if GS_WITH_GC #include #endif +#if __OBJC_GC__ +#include +#endif // Some older BSD systems used a non-standard range of thread priorities. // Use these if they exist, otherwise define standard ones. @@ -415,6 +419,7 @@ gnustep_base_thread_callback(void) static void setThreadForCurrentThread(NSThread *t) { + [[NSGarbageCollector defaultCollector] disableCollectorForPointer: t]; pthread_setspecific(thread_object_key, t); gnustep_base_thread_callback(); } @@ -443,6 +448,8 @@ unregisterActiveThread(NSThread *thread) [(GSRunLoopThreadInfo*)thread->_runLoopInfo invalidate]; [thread release]; + + [[NSGarbageCollector defaultCollector] enableCollectorForPointer: thread]; pthread_setspecific(thread_object_key, nil); } } @@ -462,6 +469,7 @@ unregisterActiveThread(NSThread *thread) { t = [self new]; t->_active = YES; + [[NSGarbageCollector defaultCollector] disableCollectorForPointer: t]; pthread_setspecific(thread_object_key, t); GS_CONSUMED(t); return YES; @@ -748,6 +756,9 @@ static void *nsthreadLauncher(void* thread) { NSThread *t = (NSThread*)thread; setThreadForCurrentThread(t); +#if __OBJC_GC__ + objc_registerThreadWithCollector(); +#endif #if GS_WITH_GC && defined(HAVE_GC_REGISTER_MY_THREAD) { struct GC_stack_base base; diff --git a/Source/NSZone.m b/Source/NSZone.m index 117f90251..935a2c21b 100644 --- a/Source/NSZone.m +++ b/Source/NSZone.m @@ -250,6 +250,11 @@ NSReallocateCollectable(void *ptr, NSUInteger size, NSUInteger options) ((options & NSScannedOption) == NSScannedOption)); } +id NSMakeCollectable(id obj) +{ + objc_gc_release(obj); +} + NSZone* NSCreateZone (NSUInteger start, NSUInteger gran, BOOL canFree) {