more GC fixed

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@27788 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Richard Frith-MacDonald 2009-02-04 21:26:43 +00:00
parent eabab217de
commit 5e132b3cf7
3 changed files with 117 additions and 12 deletions

View file

@ -4,6 +4,9 @@
nonretainedObjectValue to hold classes ... because the garbage
collector might collect the latter, and we really just need to
hold references to classes without sending messages to them.
* Headers/Additions/GNUstepBase/GSIArray.h: Alterations for noe
Apple GC API
* Source/NSIndexSet.m: Use scanned memory for arrays.
2009-02-04 15:00-EST Gregory John Casamento <greg_casamento@yahoo.com>

View file

@ -25,9 +25,10 @@
#if OS_API_VERSION(GS_API_NONE,GS_API_LATEST)
#include <Foundation/NSObject.h>
#include <Foundation/NSException.h>
#include <Foundation/NSZone.h>
#import <Foundation/NSObject.h>
#import <Foundation/NSException.h>
#import <Foundation/NSGarbageCollector.h>
#import <Foundation/NSZone.h>
/* To turn assertions on, define GSI_ARRAY_CHECKS */
#define GSI_ARRAY_CHECKS 1
@ -144,6 +145,9 @@ extern "C" {
#endif /* #ifndef GSIArrayItem */
static BOOL _GSIArraySetup = NO;
static NSGarbageCollector *_GSIArrayCollector = nil;
struct _GSIArray {
GSIArrayItem *ptr;
unsigned count;
@ -189,14 +193,29 @@ GSIArrayGrow(GSIArray array)
}
next = array->cap + array->old;
size = next*sizeof(GSIArrayItem);
tmp = NSZoneMalloc(array->zone, size);
if (_GSIArrayCollector == nil)
{
tmp = NSZoneMalloc(array->zone, size);
}
else
{
tmp = NSAllocateCollectable(size, array->zone ? NSScannedOption : 0);
}
memcpy(tmp, array->ptr, array->count * sizeof(GSIArrayItem));
}
else
{
next = array->cap + array->old;
size = next*sizeof(GSIArrayItem);
tmp = NSZoneRealloc(array->zone, array->ptr, size);
if (_GSIArrayCollector == nil)
{
tmp = NSZoneRealloc(array->zone, array->ptr, size);
}
else
{
tmp = NSReallocateCollectable(array->ptr, size,
array->zone ? NSScannedOption : 0);
}
}
if (tmp == 0)
@ -226,12 +245,27 @@ GSIArrayGrowTo(GSIArray array, unsigned next)
/*
* Statically initialised buffer ... copy into new heap buffer.
*/
tmp = NSZoneMalloc(array->zone, size);
if (_GSIArrayCollector == nil)
{
tmp = NSZoneMalloc(array->zone, size);
}
else
{
tmp = NSAllocateCollectable(size, array->zone ? NSScannedOption : 0);
}
memcpy(tmp, array->ptr, array->count * sizeof(GSIArrayItem));
}
else
{
tmp = NSZoneRealloc(array->zone, array->ptr, size);
if (_GSIArrayCollector == nil)
{
tmp = NSZoneRealloc(array->zone, array->ptr, size);
}
else
{
tmp = NSReallocateCollectable(array->ptr, size,
array->zone ? NSScannedOption : 0);
}
}
if (tmp == 0)
@ -551,14 +585,38 @@ GSIArrayInitWithZoneAndCapacity(GSIArray array, NSZone *zone, size_t capacity)
{
unsigned int size;
array->zone = zone;
if (_GSIArraySetup == NO)
{
_GSIArrayCollector = [NSGarbageCollector defaultCollector];
_GSIArraySetup = YES;
}
if (_GSIArrayCollector == nil)
{
array->zone = zone;
}
else if (zone == GSAtomicMallocZone())
{
array->zone = (NSZone*)0;
}
else
{
array->zone = (NSZone*)1;
}
array->count = 0;
if (capacity < 2)
capacity = 2;
array->cap = capacity;
array->old = capacity/2;
size = capacity*sizeof(GSIArrayItem);
array->ptr = (GSIArrayItem*)NSZoneMalloc(zone, size);
if (_GSIArrayCollector == nil)
{
array->ptr = (GSIArrayItem*)NSZoneMalloc(zone, size);
}
else
{
array->ptr = (GSIArrayItem*)NSAllocateCollectable(size,
array->zone ? NSScannedOption : 0);
}
return array;
}
@ -566,7 +624,23 @@ static INLINE GSIArray
GSIArrayInitWithZoneAndStaticCapacity(GSIArray array, NSZone *zone,
size_t capacity, GSIArrayItem *buffer)
{
array->zone = zone;
if (_GSIArraySetup == NO)
{
_GSIArrayCollector = [NSGarbageCollector defaultCollector];
_GSIArraySetup = YES;
}
if (_GSIArrayCollector == nil)
{
array->zone = zone;
}
else if (zone == GSAtomicMallocZone())
{
array->zone = (NSZone*)0;
}
else
{
array->zone = (NSZone*)1;
}
array->count = 0;
array->cap = capacity;
array->old = 0;
@ -579,8 +653,18 @@ GSIArrayCopyWithZone(GSIArray array, NSZone *zone)
{
unsigned int i;
GSIArray new;
new = NSZoneMalloc(zone, sizeof(GSIArray_t));
GSIArrayInitWithZoneAndCapacity(new, zone, array->count);
if (_GSIArrayCollector == nil)
{
new = NSZoneMalloc(zone, sizeof(GSIArray_t));
GSIArrayInitWithZoneAndCapacity(new, zone, array->count);
}
else
{
new = NSAllocateCollectable(sizeof(GSIArray_t), NSScannedOption);
GSIArrayInitWithZoneAndCapacity(new, (zone ? 0 : GSAtomicMallocZone()),
array->count);
}
for (i = 0; i < array->count; i++)
{

View file

@ -721,8 +721,14 @@ static unsigned posForIndex(GSIArray array, unsigned index)
}
else
{
#if GS_WITH_GC
_data = (GSIArray)NSAllocateCollectable(sizeof(GSIArray_t),
NSScannedOption);
GSIArrayInitWithZoneAndCapacity(_array, 0, 1);
#else
_data = (GSIArray)NSZoneMalloc([self zone], sizeof(GSIArray_t));
GSIArrayInitWithZoneAndCapacity(_array, [self zone], 1);
#endif
GSIArrayAddItem(_array, (GSIArrayItem)aRange);
}
}
@ -743,8 +749,14 @@ static unsigned posForIndex(GSIArray array, unsigned index)
{
unsigned i;
#if GS_WITH_GC
_data = (GSIArray)NSAllocateCollectable(sizeof(GSIArray_t),
NSScannedOption);
GSIArrayInitWithZoneAndCapacity(_array, 0, 1);
#else
_data = (GSIArray)NSZoneMalloc([self zone], sizeof(GSIArray_t));
GSIArrayInitWithZoneAndCapacity(_array, [self zone], count);
#endif
for (i = 0; i < count; i++)
{
GSIArrayAddItem(_array, GSIArrayItemAtIndex(_other, i));
@ -888,8 +900,14 @@ static unsigned posForIndex(GSIArray array, unsigned index)
}
if (_array == 0)
{
#if GS_WITH_GC
_data = (GSIArray)NSAllocateCollectable(sizeof(GSIArray_t),
NSScannedOption);
GSIArrayInitWithZoneAndCapacity(_array, 0, 1);
#else
_data = (GSIArray)NSZoneMalloc([self zone], sizeof(GSIArray_t));
GSIArrayInitWithZoneAndCapacity(_array, [self zone], 1);
#endif
}
pos = posForIndex(_array, aRange.location);