Fix initialising mutable array.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@15107 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
CaS 2002-11-26 09:15:11 +00:00
parent 72ae68e2d5
commit 302f8afb33
2 changed files with 36 additions and 37 deletions

View file

@ -2,6 +2,7 @@
* Source/Additions/GCObject.m: Attempt to make garbage collecting * Source/Additions/GCObject.m: Attempt to make garbage collecting
thread-safe. thread-safe.
* Source/Additions/GCArray.m: Fix count initialising mutable array.
2002-11-25 Richard Frith-Macdonald <rfm@gnu.org> 2002-11-25 Richard Frith-Macdonald <rfm@gnu.org>

View file

@ -128,15 +128,13 @@ static Class gcClass = 0;
- (id) initWithObjects: (id*)objects count: (unsigned int)count - (id) initWithObjects: (id*)objects count: (unsigned int)count
{ {
unsigned int i;
_contents = NSZoneMalloc([self zone], count * (sizeof(id) + sizeof(BOOL))); _contents = NSZoneMalloc([self zone], count * (sizeof(id) + sizeof(BOOL)));
_isGCObject = (BOOL*)&_contents[count]; _isGCObject = (BOOL*)&_contents[count];
_count = count; _count = 0;
for (i = 0; i < count; i++) while (_count < count)
{ {
_contents[i] = [objects[i] retain]; _contents[_count] = [objects[_count] retain];
if (_contents[i] == nil) if (_contents[_count] == nil)
{ {
[self release]; [self release];
[NSException raise: NSInvalidArgumentException [NSException raise: NSInvalidArgumentException
@ -144,24 +142,25 @@ static Class gcClass = 0;
} }
else else
{ {
_isGCObject[i] = [objects[i] isKindOfClass: gcClass]; _isGCObject[_count] = [objects[_count] isKindOfClass: gcClass];
} }
_count++;
} }
return self; return self;
} }
- (id) initWithArray: (NSArray*)anotherArray - (id) initWithArray: (NSArray*)anotherArray
{ {
unsigned int i;
unsigned int count = [anotherArray count]; unsigned int count = [anotherArray count];
_contents = NSZoneMalloc([self zone], count * (sizeof(id) + sizeof(BOOL))); _contents = NSZoneMalloc([self zone], count * (sizeof(id) + sizeof(BOOL)));
_isGCObject = (BOOL*)&_contents[count]; _isGCObject = (BOOL*)&_contents[count];
_count = count; _count = 0;
for (i = 0; i < _count; i++) while (_count < count)
{ {
_contents[i] = [[anotherArray objectAtIndex: i] retain]; _contents[_count] = [[anotherArray objectAtIndex: _count] retain];
_isGCObject[i] = [_contents[i] isKindOfClass: gcClass]; _isGCObject[_count] = [_contents[_count] isKindOfClass: gcClass];
_count++;
} }
return self; return self;
} }
@ -236,13 +235,31 @@ static Class gcClass = 0;
return [self initWithCapacity: 1]; return [self initWithCapacity: 1];
} }
- (id) initWithArray: (NSArray*)anotherArray
{
unsigned int count = [anotherArray count];
self = [self initWithCapacity: count];
if (self != nil)
{
while (_count < count)
{
_contents[_count] = [[anotherArray objectAtIndex: _count] retain];
_isGCObject[_count] = [_contents[_count] isKindOfClass: gcClass];
_count++;
}
}
return self;
}
- (id) initWithCapacity: (unsigned int)aNumItems - (id) initWithCapacity: (unsigned int)aNumItems
{ {
if (aNumItems < 1) if (aNumItems < 1)
{ {
aNumItems = 1; aNumItems = 1;
} }
_contents = NSZoneMalloc([self zone], aNumItems * (sizeof(id) + sizeof(BOOL))); _contents = NSZoneMalloc([self zone],
aNumItems * (sizeof(id) + sizeof(BOOL)));
_isGCObject = (BOOL*)&_contents[aNumItems]; _isGCObject = (BOOL*)&_contents[aNumItems];
_maxCount = aNumItems; _maxCount = aNumItems;
_count = 0; _count = 0;
@ -254,12 +271,10 @@ static Class gcClass = 0;
self = [self initWithCapacity: count]; self = [self initWithCapacity: count];
if (self != nil) if (self != nil)
{ {
unsigned int i; while (_count < count)
for (i = 0; i < count; i++)
{ {
_contents[i] = [objects[i] retain]; _contents[_count] = [objects[_count] retain];
if (_contents[i] == nil) if (_contents[_count] == nil)
{ {
[self release]; [self release];
[NSException raise: NSInvalidArgumentException [NSException raise: NSInvalidArgumentException
@ -267,26 +282,9 @@ static Class gcClass = 0;
} }
else else
{ {
_isGCObject[i] = [objects[i] isKindOfClass: gcClass]; _isGCObject[_count] = [objects[_count] isKindOfClass: gcClass];
} }
} _count++;
}
return self;
}
- (id) initWithArray: (NSArray*)anotherArray
{
unsigned int count = [anotherArray count];
self = [self initWithCapacity: count];
if (self != nil)
{
unsigned int i;
for (i = 0; i < _count; i++)
{
_contents[i] = [[anotherArray objectAtIndex: i] retain];
_isGCObject[i] = [_contents[i] isKindOfClass: gcClass];
} }
} }
return self; return self;