More GC updates

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@4959 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Richard Frith-MacDonald 1999-09-29 20:15:17 +00:00
parent 78e6f68250
commit f233886308
6 changed files with 222 additions and 104 deletions

View file

@ -1,9 +1,13 @@
Wed Sep 29 15:33:00 1999 Richard Frith-Macdonald <richard@brainstorm.co.uk>
Wed Sep 29 21:34:00 1999 Richard Frith-Macdonald <richard@brainstorm.co.uk>
* Source/NSData.m: More GC updates (atomic data buffer)
* Source/NSGString.m: ditto
* Source/NSGCString.m: ditto
* Source/NSObject.m: More GC updates
* Source/NSZone.m: ditto
* Source/mframe.m: ditto
* Source/objc-gnu2next.m: ditto
* Headers/Foundation/GSIMap.h: ditto
Tue Sep 28 20:54:00 1999 Richard Frith-Macdonald <richard@brainstorm.co.uk>

View file

@ -341,6 +341,16 @@ GSIMapMoreNodes(GSIMapTable map)
GSIMapNode *newArray;
size_t arraySize = (map->chunkCount+1)*sizeof(GSIMapNode);
#if GS_WITH_GC == 1
/*
* Our nodes may be allocated from the atomic zone - but we don't want
* them freed - so we must keep the array of pointers to memory chunks in
* the default zone
*/
if (map->zone == GSAtomicMallocZone())
newArray = (GSIMapNode*)NSZoneMalloc(NSDefaultMallocZone(), arraySize);
else
#endif
newArray = (GSIMapNode*)NSZoneMalloc(map->zone, arraySize);
if (newArray)
{
@ -507,20 +517,18 @@ GSIMapResize(GSIMapTable map, size_t new_capacity)
* around powers of two - we don't want lots of keys falling into a single
* bucket.
*/
if (size == 8) size++;
if (size == 8)
size++;
/*
* Make a new set of buckets for this map
*/
new_buckets = (GSIMapBucket)NSZoneCalloc(map->zone, size,
sizeof(GSIMapBucket_t));
sizeof(GSIMapBucket_t));
if (new_buckets != 0)
{
GSIMapRemangleBuckets(map,
map->buckets,
map->bucketCount,
new_buckets,
size);
GSIMapRemangleBuckets(map, map->buckets, map->bucketCount, new_buckets,
size);
if (map->buckets != 0)
{

View file

@ -158,7 +158,11 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len, NSZone* zone)
goto failure;
}
#if GS_WITH_GC == 1
tmp = NSZoneMalloc(GSAtomicMallocZone(), fileLength);
#else
tmp = NSZoneMalloc(zone, fileLength);
#endif
if (tmp == 0)
{
NSLog(@"Malloc failed for file of length %d- %s",
@ -397,7 +401,11 @@ failure:
#define num2char(num) ((num) < 0xa ? ((num)+'0') : ((num)+0x57))
/* we can just build a cString and convert it to an NSString */
#if GS_WITH_GC
dest = (char*) NSZoneMalloc(GSAtomicMallocZone(), 2*length+length/4+3);
#else
dest = (char*) NSZoneMalloc(z, 2*length+length/4+3);
#endif
if (dest == 0)
[NSException raise: NSMallocException
format: @"No memory for description of NSData object"];
@ -412,9 +420,14 @@ failure:
}
dest[j++] = '>';
dest[j] = '\0';
#if GS_WITH_GC
str = [[NSString allocWithZone: z]
initWithCStringNoCopy: dest length: j fromZone: GSAtomicMallocZone()];
#else
str = [[NSString allocWithZone: z] initWithCStringNoCopy: dest
length: j
fromZone: z];
#endif
return AUTORELEASE(str);
}
@ -450,7 +463,11 @@ failure:
GS_RANGE_CHECK(aRange, l);
#if GS_WITH_GC
buffer = NSZoneMalloc(GSAtomicMallocZone(), aRange.length);
#else
buffer = NSZoneMalloc([self zone], aRange.length);
#endif
if (buffer == 0)
[NSException raise: NSMallocException
format: @"No memory for subdata of NSData object"];
@ -2090,7 +2107,11 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
if (aBuffer != 0 && bufferSize > 0)
{
#if GS_WITH_GC
zone = GSAtomicMallocZone();
#else
zone = [self zone];
#endif
tmp = NSZoneMalloc(zone, bufferSize);
if (tmp == 0)
{
@ -2133,7 +2154,11 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
return data;
}
#if GS_WITH_GC
zone = GSAtomicMallocZone();
#else
zone = aZone;
#endif
bytes = aBuffer;
if (bytes)
{
@ -2147,7 +2172,11 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
unsigned l;
void* b;
#if GS_WITH_GC
zone = GSAtomicMallocZone();
#else
zone = [self zone];
#endif
[aCoder decodeValueOfObjCType: @encode(unsigned long) at: &l];
if (l)
@ -2170,7 +2199,11 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
- (id) initWithContentsOfFile: (NSString *)path
{
#if GS_WITH_GC
zone = GSAtomicMallocZone();
#else
zone = [self zone];
#endif
if (readContentsOfFile(path, &bytes, &length, zone) == NO)
{
RELEASE(self);
@ -2401,8 +2434,9 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
@implementation NSMutableDataMalloc
+ (void) initialize
{
if ([self class] == [NSMutableDataMalloc class]) {
behavior_class_add_class(self, [NSDataMalloc class]);
if (self == [NSMutableDataMalloc class])
{
behavior_class_add_class(self, [NSDataMalloc class]);
}
}
@ -2413,17 +2447,17 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
- (Class) classForArchiver
{
return mutableDataMalloc;
return mutableDataMalloc;
}
- (Class) classForCoder
{
return mutableDataMalloc;
return mutableDataMalloc;
}
- (Class) classForPortCoder
{
return mutableDataMalloc;
return mutableDataMalloc;
}
- (id) copy
@ -2435,7 +2469,7 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
- (id) copyWithZone: (NSZone*)z
{
return [[dataMalloc allocWithZone: z]
initWithBytes: bytes length: length];
initWithBytes: bytes length: length];
}
- (id) initWithBytes: (const void*)aBuffer length: (unsigned)bufferSize
@ -2463,30 +2497,39 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
length: (unsigned)bufferSize
fromZone: (NSZone*)aZone
{
if (aZone == 0) {
self = [self initWithBytes: aBuffer length: bufferSize];
return self;
if (aZone == 0)
{
self = [self initWithBytes: aBuffer length: bufferSize];
return self;
}
if (aBuffer == 0) {
self = [self initWithCapacity: bufferSize];
if (self) {
[self setLength: bufferSize];
if (aBuffer == 0)
{
self = [self initWithCapacity: bufferSize];
if (self)
{
[self setLength: bufferSize];
}
return self;
return self;
}
self = [self initWithCapacity: 0];
if (self) {
zone = aZone;
bytes = aBuffer;
length = bufferSize;
capacity = bufferSize;
growth = capacity/2;
if (growth == 0) {
growth = 1;
self = [self initWithCapacity: 0];
if (self)
{
#if GS_WITH_GC
zone = GSAtomicMallocZone();
#else
zone = aZone;
#endif
bytes = aBuffer;
length = bufferSize;
capacity = bufferSize;
growth = capacity/2;
if (growth == 0)
{
growth = 1;
}
}
return self;
return self;
}
/*
@ -2494,23 +2537,30 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
*/
- (id) initWithCapacity: (unsigned)size
{
zone = [self zone];
if (size) {
bytes = NSZoneMalloc(zone, size);
if (bytes == 0) {
NSLog(@"[NSMutableDataMalloc -initWithCapacity:] out of memory for %u bytes - %s", size, strerror(errno));
RELEASE(self);
return nil;
#if GS_WITH_GC
zone = GSAtomicMallocZone();
#else
zone = [self zone];
#endif
if (size)
{
bytes = NSZoneMalloc(zone, size);
if (bytes == 0)
{
NSLog(@"[NSMutableDataMalloc -initWithCapacity:] out of memory for %u bytes - %s", size, strerror(errno));
RELEASE(self);
return nil;
}
}
capacity = size;
growth = capacity/2;
if (growth == 0) {
growth = 1;
capacity = size;
growth = capacity/2;
if (growth == 0)
{
growth = 1;
}
length = 0;
length = 0;
return self;
return self;
}
- (id) initWithCoder: (NSCoder*)aCoder
@ -2537,56 +2587,62 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
- (id) initWithLength: (unsigned)size
{
self = [self initWithCapacity: size];
if (self) {
memset(bytes, '\0', size);
length = size;
self = [self initWithCapacity: size];
if (self)
{
memset(bytes, '\0', size);
length = size;
}
return self;
return self;
}
- (id) initWithContentsOfFile: (NSString *)path
{
self = [self initWithCapacity: 0];
if (readContentsOfFile(path, &bytes, &length, zone) == NO) {
RELEASE(self);
self = nil;
self = [self initWithCapacity: 0];
if (readContentsOfFile(path, &bytes, &length, zone) == NO)
{
RELEASE(self);
self = nil;
}
else {
capacity = length;
else
{
capacity = length;
}
return self;
return self;
}
- (id) initWithContentsOfMappedFile: (NSString *)path
{
return [self initWithContentsOfFile: path];
return [self initWithContentsOfFile: path];
}
- (id) initWithData: (NSData*)anObject
{
if (anObject == nil) {
return [self initWithCapacity: 0];
if (anObject == nil)
{
return [self initWithCapacity: 0];
}
if ([anObject isKindOfClass: [NSData class]] == NO) {
NSLog(@"-initWithData: passed a non-data object");
RELEASE(self);
return nil;
if ([anObject isKindOfClass: [NSData class]] == NO)
{
NSLog(@"-initWithData: passed a non-data object");
RELEASE(self);
return nil;
}
return [self initWithBytes: [anObject bytes] length: [anObject length]];
return [self initWithBytes: [anObject bytes] length: [anObject length]];
}
- (void) appendBytes: (const void*)aBuffer
length: (unsigned)bufferSize
{
unsigned oldLength = length;
unsigned minimum = length + bufferSize;
unsigned oldLength = length;
unsigned minimum = length + bufferSize;
if (minimum > capacity) {
[self _grow: minimum];
if (minimum > capacity)
{
[self _grow: minimum];
}
memcpy(bytes + oldLength, aBuffer, bufferSize);
length = minimum;
memcpy(bytes + oldLength, aBuffer, bufferSize);
length = minimum;
}
- (unsigned) capacity
@ -2596,34 +2652,38 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
- (void) _grow: (unsigned)minimum
{
if (minimum > capacity) {
unsigned nextCapacity = capacity + growth;
unsigned nextGrowth = capacity ? capacity : 1;
if (minimum > capacity)
{
unsigned nextCapacity = capacity + growth;
unsigned nextGrowth = capacity ? capacity : 1;
while (nextCapacity < minimum) {
unsigned tmp = nextCapacity + nextGrowth;
nextGrowth = nextCapacity;
nextCapacity = tmp;
while (nextCapacity < minimum)
{
unsigned tmp = nextCapacity + nextGrowth;
nextGrowth = nextCapacity;
nextCapacity = tmp;
}
[self setCapacity: nextCapacity];
growth = nextGrowth;
[self setCapacity: nextCapacity];
growth = nextGrowth;
}
}
- (void*) mutableBytes
{
return bytes;
return bytes;
}
- (void*) relinquishAllocatedBytesFromZone: (NSZone*)aZone
{
void *ptr = [super relinquishAllocatedBytesFromZone: aZone];
void *ptr = [super relinquishAllocatedBytesFromZone: aZone];
if (ptr != 0) {
capacity = 0;
growth = 1;
if (ptr != 0)
{
capacity = 0;
growth = 1;
}
return ptr;
return ptr;
}
- (void) replaceBytesInRange: (NSRange)aRange
@ -2909,30 +2969,36 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
- (id) setCapacity: (unsigned)size
{
if (size != capacity) {
void* tmp;
if (size != capacity)
{
void* tmp;
if (bytes) {
tmp = NSZoneRealloc(zone, bytes, size);
if (bytes)
{
tmp = NSZoneRealloc(zone, bytes, size);
}
else {
tmp = NSZoneMalloc(zone, size);
else
{
tmp = NSZoneMalloc(zone, size);
}
if (tmp == 0) {
[NSException raise: NSMallocException
format: @"Unable to set data capacity to '%d'", size];
if (tmp == 0)
{
[NSException raise: NSMallocException
format: @"Unable to set data capacity to '%d'", size];
}
bytes = tmp;
capacity = size;
growth = capacity/2;
if (growth == 0) {
growth = 1;
bytes = tmp;
capacity = size;
growth = capacity/2;
if (growth == 0)
{
growth = 1;
}
}
if (size < length) {
length = size;
if (size < length)
{
length = size;
}
return self;
return self;
}
- (void) setLength: (unsigned)size

View file

@ -133,7 +133,11 @@ static IMP msInitImp; /* designated initialiser for mutable */
{
_count = length;
_contents_chars = (unsigned char*)byteString;
#if GS_WITH_GC
_zone = byteString ? GSAtomicMallocZone() : 0;
#else
_zone = byteString ? zone : 0;
#endif
return self;
}
@ -215,7 +219,11 @@ static IMP msInitImp; /* designated initialiser for mutable */
at: &_count];
if (_count > 0)
{
#if GS_WITH_GC
_zone = GSAtomicMallocZone();
#else
_zone = fastZone(self);
#endif
_contents_chars = NSZoneMalloc(_zone, _count);
[aCoder decodeArrayOfObjCType: @encode(unsigned char)
count: _count
@ -883,7 +891,11 @@ stringDecrementCountAndFillHoleAt(NSGMutableCStringStruct *self,
_capacity = capacity;
if (capacity)
{
#if GS_WITH_GC
_zone = GSAtomicMallocZone();
#else
_zone = fastZone(self);
#endif
_contents_chars = NSZoneMalloc(_zone, _capacity);
}
return self;
@ -899,7 +911,11 @@ stringDecrementCountAndFillHoleAt(NSGMutableCStringStruct *self,
_count = length;
_capacity = length;
_contents_chars = (unsigned char*)byteString;
#if GS_WITH_GC
_zone = byteString ? GSAtomicMallocZone() : 0;
#else
_zone = byteString ? zone : 0;
#endif
}
return self;
}

View file

@ -185,7 +185,11 @@
{
_count = length;
_contents_chars = chars;
#if GS_WITH_GC
_zone = chars ? GSAtomicMallocZone() : 0;
#else
_zone = chars ? zone : 0;
#endif
}
return self;
}
@ -202,7 +206,11 @@
_contents_chars = chars;
if (flag && chars)
{
#if GS_WITH_GC
_zone = GSAtomicMallocZone();
#else
_zone = NSZoneFromPointer(chars);
#endif
}
else
{
@ -350,7 +358,11 @@
[aCoder decodeValueOfObjCType: @encode(unsigned) at: &_count];
if (_count)
{
#if GS_WITH_GC
_zone = GSAtomicMallocZone();
#else
_zone = fastZone(self);
#endif
_contents_chars = NSZoneMalloc(_zone, sizeof(unichar)*_count);
[aCoder decodeArrayOfObjCType: @encode(unichar)
count: _count
@ -585,7 +597,11 @@ stringDecrementCountAndFillHoleAt(NSGMutableStringStruct *self,
_count = length;
_capacity = length;
_contents_chars = chars;
#if GS_WITH_GC
_zone = _zone ? GSAtomicMallocZone() : 0;
#else
_zone = zone;
#endif
}
return self;
}
@ -602,7 +618,11 @@ stringDecrementCountAndFillHoleAt(NSGMutableStringStruct *self,
_contents_chars = chars;
if (flag && chars)
{
#if GS_WITH_GC
_zone = GSAtomicMallocZone();
#else
_zone = NSZoneFromPointer(chars);
#endif
}
else
{
@ -623,7 +643,11 @@ stringDecrementCountAndFillHoleAt(NSGMutableStringStruct *self,
}
_count = 0;
_capacity = capacity;
#if GS_WITH_GC
_zone = GSAtomicMallocZone();
#else
_zone = fastZone(self);
#endif
_contents_chars = NSZoneMalloc(_zone, sizeof(unichar)*capacity);
}
return self;

View file

@ -304,7 +304,7 @@ NSAllocateObject(Class aClass, unsigned extraBytes, NSZone *zone)
}
else
{
GC_descr gc_type = aClass->gc_object_type;
GC_descr gc_type = (GC_descr)aClass->gc_object_type;
if (gc_type == 0)
{