Zone stuff

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@3069 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
richard 1998-10-15 05:03:16 +00:00
parent 5e02efeb1d
commit 99129c437a
21 changed files with 1706 additions and 987 deletions

View file

@ -1,3 +1,34 @@
Thu Oct 15 06:15:00 1998 Richard Frith-Macdonald <richard@brainstorm.co.uk>
* src/BinaryCStream.m: Update system version number
* src/NSCalendarDate.m: Implemented ([-copyWithZone:])
* src/NSCountedSet.m: Rewrite ([-copyWithZone:])
* src/include/NSData.h: Added GNUstep specific method for more
efficient zone use.
* src/NSData.m: Modified throughout to use NSZone functions for
managing content memory.
* src/NSGArray.m: Modified to use NSZone functions for managing
content memory. Also tidied coding methods.
* src/include/NSGCString.h: Modified to use NSZone for contents.
* src/NSGCString.m: Modified to use NSZone for contents and fixed
bug in coding (couldn't cope with embedded nuls in string).
* src/NSGDictionary.m: Modified to use NSZone for contents.
* src/include/NSGString.h: Modified to use NSZone for contents.
* src/NSGString.m: Modified to use NSZone for contents.
* src/NSObject.m: Added fastZone() function.
* src/PortCoder.m: Updated system version number
* src/include/NSSet.h: Added ([+setWithSet:])
* src/NSSet.m: Added ([+setWithSet:]) and fixed ([-copyWithZone:])
* src/include/NSString.h: Added GNUstep specific methods for faster
use of zones.
* src/NSString.m: Modified to use NSZone functions for contents.
* src/include/NSZone.h: Removed non-existent methods and changed
structure layout to support NSZoneFromPointer()
* src/NSZone.m: Rewrite to support NSZoneFromPointer(),
NSRecycleZone() and fix a couple of bugs. Needs more work to
make simpler and more efficient.
* src/include/fast.x: Added fastZone()
Tue Oct 13 09:24:17 1998 Adam Fedor <fedor@doc.com> Tue Oct 13 09:24:17 1998 Adam Fedor <fedor@doc.com>
* src/include/NSNotification.h: Addd placeholder in * src/include/NSNotification.h: Addd placeholder in

View file

@ -94,6 +94,16 @@
+ (id) dataWithShmID: (int)anID length: (unsigned) length; + (id) dataWithShmID: (int)anID length: (unsigned) length;
+ (id) dataWithSharedBytes: (const void*)bytes length: (unsigned) length; + (id) dataWithSharedBytes: (const void*)bytes length: (unsigned) length;
+ (id) dataWithStaticBytes: (const void*)bytes length: (unsigned) length; + (id) dataWithStaticBytes: (const void*)bytes length: (unsigned) length;
/*
* -initWithBytesNoCopy:length:fromZone:
* The GNUstep designated initialiser for normal data objects - lets
* the class know what zone the data comes from, so we can avoid the
* overhead of an NSZoneFromPointer() call.
* A zone of zero denotes static memory rather than malloced memory.
*/
- (id) initWithBytesNoCopy: (void*)bytes
length: (unsigned)length
fromZone: (NSZone*)zone;
/* /*
* -relinquishAllocatedBytes * -relinquishAllocatedBytes
* For an NSData object with a malloced buffer, returns that buffer and * For an NSData object with a malloced buffer, returns that buffer and

View file

@ -38,7 +38,7 @@
{ {
char * _contents_chars; char * _contents_chars;
int _count; int _count;
BOOL _free_contents; NSZone *_zone;
unsigned _hash; unsigned _hash;
} }
@end @end
@ -47,7 +47,7 @@
{ {
char * _contents_chars; char * _contents_chars;
int _count; int _count;
BOOL _free_contents; NSZone *_zone;
unsigned _hash; unsigned _hash;
int _capacity; int _capacity;
} }

View file

@ -38,7 +38,7 @@
{ {
unichar * _contents_chars; unichar * _contents_chars;
int _count; int _count;
BOOL _free_contents; NSZone *_zone;
unsigned _hash; unsigned _hash;
} }
@end @end
@ -47,7 +47,7 @@
{ {
unichar * _contents_chars; unichar * _contents_chars;
int _count; int _count;
BOOL _free_contents; NSZone *_zone;
unsigned _hash; unsigned _hash;
int _capacity; int _capacity;
} }

View file

@ -34,6 +34,7 @@
+ setWithArray: (NSArray*)array; + setWithArray: (NSArray*)array;
+ setWithObject: anObject; + setWithObject: anObject;
+ setWithObjects: anObject, ...; + setWithObjects: anObject, ...;
+ setWithSet: (NSSet*)aSet;
- (id) initWithObjects: (id*)objects - (id) initWithObjects: (id*)objects
count: (unsigned)count; count: (unsigned)count;

View file

@ -264,8 +264,13 @@ enum {
#endif #endif
#ifndef NO_GNUSTEP #ifndef NO_GNUSTEP
- (const char *) cStringNoCopy;
- (NSString*) descriptionForPropertyList; - (NSString*) descriptionForPropertyList;
- (id) initWithCharactersNoCopy: (unichar*)chars
length: (unsigned int)length
fromZone: (NSZone*)zone;
- (id) initWithCStringNoCopy: (char*)byteString
length: (unsigned int)length
fromZone: (NSZone*)zone;
#endif /* NO_GNUSTEP */ #endif /* NO_GNUSTEP */
@end @end

View file

@ -52,10 +52,12 @@ struct _NSZone
void (*free)(struct _NSZone *zone, void *ptr); void (*free)(struct _NSZone *zone, void *ptr);
void (*recycle)(struct _NSZone *zone); void (*recycle)(struct _NSZone *zone);
BOOL (*check)(struct _NSZone *zone); BOOL (*check)(struct _NSZone *zone);
BOOL (*lookup)(struct _NSZone *zone, void *ptr);
struct NSZoneStats (*stats)(struct _NSZone *zone); struct NSZoneStats (*stats)(struct _NSZone *zone);
size_t gran; // Zone granularity size_t gran; // Zone granularity
NSString *name; // Name of zone (default is 'nil') NSString *name; // Name of zone (default is 'nil')
NSZone *next;
}; };
@ -97,16 +99,6 @@ extern inline NSString* NSZoneName (NSZone *zone)
{ if (!zone) zone = NSDefaultMallocZone(); { if (!zone) zone = NSDefaultMallocZone();
return zone->name; } return zone->name; }
/* Not in OpenStep */
extern void NSZoneRegisterRegion (NSZone *zone, void *low, void *high);
extern void NSDeregisterZone (NSZone *zone); // Not in OpenStep
/* Not in OpenStep */
extern void* NSZoneRegisterChunk (NSZone *zone, void *chunk);
extern size_t NSZoneChunkOverhead (void); // Not in OpenStep
extern inline BOOL NSZoneCheck (NSZone *zone) // Not in OpenStep extern inline BOOL NSZoneCheck (NSZone *zone) // Not in OpenStep
{ if (!zone) zone = NSDefaultMallocZone(); { if (!zone) zone = NSDefaultMallocZone();
return (zone->check)(zone); } return (zone->check)(zone); }

View file

@ -158,3 +158,15 @@ fastInstanceIsKindOfClass(NSObject *obj, Class c)
return fastClassIsKindOfClass(ic, c); return fastClassIsKindOfClass(ic, c);
} }
/*
* fastZone(NSObject *obj)
* This function gets the zone that would be returned by the
* [NSObject -zone] instance method. Using this could mess you up in
* the unlikely event that you had an object that had overridden the
* '-zone' method.
* This function DOES know about NXConstantString, so it's pretty safe
* for normal use.
*/
extern NSZone *fastZone(NSObject* obj);

View file

@ -39,7 +39,8 @@
#endif /* !__WIN32__ */ #endif /* !__WIN32__ */
#define PRE_SIZEOF_PREFIX_FORMAT_VERSION 0 #define PRE_SIZEOF_PREFIX_FORMAT_VERSION 0
#define CURRENT_FORMAT_VERSION 1 #define CURRENT_FORMAT_VERSION ((((GNUSTEP_BASE_MAJOR_VERSION * 100) + \
GNUSTEP_BASE_MINOR_VERSION) * 100) + GNUSTEP_BASE_SUBMINOR_VERSION)
#define DEFAULT_FORMAT_VERSION CURRENT_FORMAT_VERSION #define DEFAULT_FORMAT_VERSION CURRENT_FORMAT_VERSION
#define ROUND(V, A) \ #define ROUND(V, A) \

View file

@ -1,5 +1,5 @@
/* Implementation for NSCalendarDate for GNUstep /* Implementation for NSCalendarDate for GNUstep
Copyright (C) 1996 Free Software Foundation, Inc. Copyright (C) 1996, 1998 Free Software Foundation, Inc.
Author: Scott Christley <scottc@net-community.com> Author: Scott Christley <scottc@net-community.com>
Date: October 1996 Date: October 1996
@ -982,6 +982,23 @@ static id long_day[7] = {@"Sunday",
return [NSString stringWithCString: buf]; return [NSString stringWithCString: buf];
} }
- (id) copyWithZone:(NSZone*)zone
{
NSCalendarDate *newDate;
if (NSShouldRetainWithZone(self, zone)) {
newDate = [self retain];
}
else {
newDate = (NSCalendarDate*)NSCopyObject(self, 0, zone);
if (newDate) {
newDate->calendar_format = [calendar_format copyWithZone: zone];
}
}
return newDate;
}
- (NSString *)descriptionWithLocale:(NSDictionary *)locale - (NSString *)descriptionWithLocale:(NSDictionary *)locale
{ {
return [self descriptionWithCalendarFormat: calendar_format return [self descriptionWithCalendarFormat: calendar_format
@ -996,6 +1013,7 @@ static id long_day[7] = {@"Sunday",
- (void)setCalendarFormat:(NSString *)format - (void)setCalendarFormat:(NSString *)format
{ {
[calendar_format release];
calendar_format = [format copyWithZone: [self zone]]; calendar_format = [format copyWithZone: [self zone]];
} }

View file

@ -69,51 +69,38 @@ static Class NSCountedSet_concrete_class;
- copyWithZone: (NSZone*)z - copyWithZone: (NSZone*)z
{ {
/* a deep copy */ NSSet *newSet;
int count = [self count]; int count = [self count];
id objects[count]; id objects[count];
id enumerator = [self objectEnumerator]; id enumerator = [self objectEnumerator];
id o; id o;
NSSet *newSet;
int i; int i;
BOOL needCopy = [self isKindOfClass: [NSMutableSet class]];
if (NSShouldRetainWithZone(self, z) == NO)
needCopy = YES;
for (i = 0; (o = [enumerator nextObject]); i++) for (i = 0; (o = [enumerator nextObject]); i++)
{
objects[i] = [o copyWithZone:z]; objects[i] = [o copyWithZone:z];
if (objects[i] != o)
needCopy = YES;
}
if (needCopy)
{
int j;
newSet = [[[[self class] _concreteClass] allocWithZone: z] newSet = [[[self class] allocWithZone: z] initWithObjects: objects
initWithObjects:objects count:count]; count: count];
for (j = 0; j < i; j++) for (i = 0; (o = [enumerator nextObject]); i++) {
{ unsigned extra;
unsigned extra = [self countForObject: objects[j]];
if (extra > 1) extra = [self countForObject: o];
while (--extra)
[newSet addObject: objects[j]]; if (extra > 1) {
while (--extra) {
[newSet addObject: o];
} }
} }
else [o release];
newSet = [self retain]; }
for (i = 0; i < count; i++)
[objects[i] release];
return newSet; return newSet;
} }
- mutableCopyWithZone: (NSZone*)z - mutableCopyWithZone: (NSZone*)z
{ {
/* a shallow copy */ return [self copyWithZone: z];
return [[[[self class] _concreteClass] allocWithZone: z] initWithSet: self];
} }
- initWithCoder: aCoder - initWithCoder: aCoder

View file

@ -94,7 +94,7 @@
static BOOL static BOOL
readContentsOfFile(NSString* path, void** buf, unsigned* len) readContentsOfFile(NSString* path, void** buf, unsigned* len, NSZone* zone)
{ {
char thePath[BUFSIZ*2]; char thePath[BUFSIZ*2];
FILE *theFile = 0; FILE *theFile = 0;
@ -133,7 +133,7 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
goto failure; goto failure;
} }
tmp = objc_malloc(fileLength); tmp = NSZoneMalloc(zone, fileLength);
if (tmp == 0) if (tmp == 0)
{ {
NSLog(@"Malloc failed for file of length %d- %s", NSLog(@"Malloc failed for file of length %d- %s",
@ -165,7 +165,7 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
/* Just in case the failure action needs to be changed. */ /* Just in case the failure action needs to be changed. */
failure: failure:
if (tmp) if (tmp)
objc_free(tmp); NSZoneFree(zone, tmp);
if (theFile) if (theFile)
fclose(theFile); fclose(theFile);
return NO; return NO;
@ -176,6 +176,7 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
{ {
unsigned int length; unsigned int length;
void *bytes; void *bytes;
NSZone *zone;
} }
@end @end
@ -184,6 +185,7 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
unsigned int capacity; unsigned int capacity;
unsigned int length; unsigned int length;
void *bytes; void *bytes;
NSZone *zone;
} }
@end @end
@ -261,18 +263,29 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
length: [data length]] autorelease]; length: [data length]] autorelease];
} }
- (id) initWithBytes: (const void*)bytes - (id) init
length: (unsigned int)length {
return [self initWithBytesNoCopy: 0 length: 0];
}
- (id) initWithBytes: (const void*)aBuffer
length: (unsigned int)bufferSize
{ {
[self subclassResponsibility:_cmd]; [self subclassResponsibility:_cmd];
return nil; return nil;
} }
- (id) initWithBytesNoCopy: (void*)bytes - (id) initWithBytesNoCopy: (void*)aBuffer
length: (unsigned int)length length: (unsigned int)bufferSize
{ {
[self subclassResponsibility:_cmd]; if (aBuffer)
return nil; return [self initWithBytesNoCopy: aBuffer
length: bufferSize
fromZone: NSZoneFromPointer(aBuffer)];
else
return [self initWithBytesNoCopy: aBuffer
length: bufferSize
fromZone: [self zone]];
} }
- (id) initWithContentsOfFile: (NSString *)path - (id) initWithContentsOfFile: (NSString *)path
@ -308,11 +321,12 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
char *dest; char *dest;
int length = [self length]; int length = [self length];
int i,j; int i,j;
NSZone *z = [self zone];
#define num2char(num) ((num) < 0xa ? ((num)+'0') : ((num)+0x57)) #define num2char(num) ((num) < 0xa ? ((num)+'0') : ((num)+0x57))
/* we can just build a cString and convert it to an NSString */ /* we can just build a cString and convert it to an NSString */
dest = (char*) objc_malloc (2*length+length/4+3); dest = (char*) NSZoneMalloc(z, 2*length+length/4+3);
if (dest == 0) if (dest == 0)
[NSException raise:NSMallocException [NSException raise:NSMallocException
format:@"No memory for description of NSData object"]; format:@"No memory for description of NSData object"];
@ -327,8 +341,8 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
} }
dest[j++] = '>'; dest[j++] = '>';
dest[j] = '\0'; dest[j] = '\0';
str = [NSString stringWithCString: dest]; str = [[[NSString alloc] initWithCStringNoCopy: dest length: j fromZone: z]
objc_free(dest); autorelease];
return str; return str;
} }
@ -384,7 +398,7 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
format: @"Range: (%u, %u) Size: %d", format: @"Range: (%u, %u) Size: %d",
aRange.location, aRange.length, l]; aRange.location, aRange.length, l];
buffer = objc_malloc(aRange.length); buffer = NSZoneMalloc([self zone], aRange.length);
if (buffer == 0) if (buffer == 0)
[NSException raise:NSMallocException [NSException raise:NSMallocException
format:@"No memory for subdata of NSData object"]; format:@"No memory for subdata of NSData object"];
@ -554,7 +568,7 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
else { else {
unsigned len = (length+1)*sizeof(char); unsigned len = (length+1)*sizeof(char);
*(char**)data = (char*)objc_malloc(len); *(char**)data = (char*)NSZoneMalloc([self zone], len);
adr = [NSData dataWithBytesNoCopy: *(void**)data length: len]; adr = [NSData dataWithBytesNoCopy: *(void**)data length: len];
} }
@ -605,7 +619,7 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
unsigned len = objc_sizeof_type(++type); unsigned len = objc_sizeof_type(++type);
id adr; id adr;
*(char**)data = (char*)objc_malloc(len); *(char**)data = (char*)NSZoneMalloc([self zone], len);
adr = [NSData dataWithBytesNoCopy: *(void**)data length: len]; adr = [NSData dataWithBytesNoCopy: *(void**)data length: len];
[self deserializeDataAt:*(char**)data [self deserializeDataAt:*(char**)data
@ -780,9 +794,16 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
autorelease]; autorelease];
} }
- (id) initWithBytesNoCopy: (void*)bytes
length: (unsigned)length
fromZone: (NSZone*)zone
{
[self subclassResponsibility:_cmd];
}
- (void*) relinquishAllocatedBytes - (void*) relinquishAllocatedBytes
{ {
return 0; /* No data from objc_malloc - return nul pointer */ return 0; /* No data from NSZoneMalloc - return nul pointer */
} }
@end @end
@ -1171,7 +1192,7 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
{ {
if (bytes) if (bytes)
{ {
objc_free(bytes); NSZoneFree(zone, bytes);
bytes = 0; bytes = 0;
length = 0; length = 0;
} }
@ -1180,13 +1201,13 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
- (void) encodeWithCoder: (NSCoder*)aCoder - (void) encodeWithCoder: (NSCoder*)aCoder
{ {
[aCoder encodeValueOfObjCType:"I" at: &length]; [aCoder encodeValueOfObjCType: @encode(unsigned long) at: &length];
[aCoder encodeArrayOfObjCType:"C" count:length at: bytes]; [aCoder encodeArrayOfObjCType: @encode(unsigned char) count:length at: bytes];
} }
- (id) init - (id) init
{ {
return [self initWithBytesNoCopy: 0 length: 0]; return [self initWithBytesNoCopy: 0 length: 0 fromZone: [self zone]];
} }
- (id) initWithBytes: (const void*)aBuffer length: (unsigned int)bufferSize - (id) initWithBytes: (const void*)aBuffer length: (unsigned int)bufferSize
@ -1195,7 +1216,8 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
if (aBuffer != 0 && bufferSize > 0) if (aBuffer != 0 && bufferSize > 0)
{ {
tmp = objc_malloc(bufferSize); zone = [self zone];
tmp = NSZoneMalloc(zone, bufferSize);
if (tmp == 0) if (tmp == 0)
{ {
NSLog(@"[NSDataMalloc -initWithBytes:length:] unable to allocate %lu bytes", bufferSize); NSLog(@"[NSDataMalloc -initWithBytes:length:] unable to allocate %lu bytes", bufferSize);
@ -1207,23 +1229,37 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
memcpy(tmp, aBuffer, bufferSize); memcpy(tmp, aBuffer, bufferSize);
} }
} }
self = [self initWithBytesNoCopy:tmp length:bufferSize]; self = [self initWithBytesNoCopy:tmp length:bufferSize fromZone: zone];
return self; return self;
} }
- (id) initWithBytesNoCopy: (void*)aBuffer - (id) initWithBytesNoCopy: (void*)aBuffer
length: (unsigned int)bufferSize length: (unsigned int)bufferSize
{ {
self = [super init]; NSZone *z = NSZoneFromPointer(aBuffer);
if (self)
return [self initWithBytesNoCopy: aBuffer length: bufferSize fromZone: z];
}
- (id) initWithBytesNoCopy: (void*)aBuffer
length: (unsigned)bufferSize
fromZone: (NSZone*)aZone
{
if (aZone == 0)
{ {
NSData *data;
data = [[NSDataStatic alloc] initWithBytesNoCopy: aBuffer
length: bufferSize];
[self dealloc];
return data;
}
zone = aZone;
bytes = aBuffer; bytes = aBuffer;
if (bytes) if (bytes)
length = bufferSize; length = bufferSize;
}
else
if (aBuffer)
objc_free(aBuffer);
return self; return self;
} }
@ -1232,22 +1268,29 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
unsigned int l; unsigned int l;
void* b; void* b;
[aCoder decodeValueOfObjCType:"I" at: &l]; zone = [self zone];
b = objc_malloc(l);
[aCoder decodeValueOfObjCType: @encode(unsigned long) at: &l];
if (l)
{
b = NSZoneMalloc(zone, l);
if (b == 0) if (b == 0)
{ {
NSLog(@"[NSDataMalloc -initWithCode:] unable to allocate %lu bytes", l); NSLog(@"[NSDataMalloc -initWithCoder:] unable to get %lu bytes", l);
[self dealloc]; [self dealloc];
return nil; return nil;
} }
[aCoder decodeArrayOfObjCType:"C" count: l at: b]; [aCoder decodeArrayOfObjCType: @encode(unsigned char) count: l at: b];
return [self initWithBytesNoCopy: b length: l]; }
else
b = 0;
return [self initWithBytesNoCopy: b length: l fromZone: zone];
} }
- (id) initWithContentsOfFile: (NSString *)path - (id) initWithContentsOfFile: (NSString *)path
{ {
self = [super init]; zone = [self zone];
if (readContentsOfFile(path, &bytes, &length) == NO) if (readContentsOfFile(path, &bytes, &length, zone) == NO)
{ {
[self dealloc]; [self dealloc];
self = nil; self = nil;
@ -1269,7 +1312,7 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
- (id) initWithData: (NSData*)anObject - (id) initWithData: (NSData*)anObject
{ {
if (anObject == nil) if (anObject == nil)
return [self initWithBytesNoCopy: 0 length: 0]; return [self initWithBytesNoCopy: 0 length: 0 fromZone: [self zone]];
if ([anObject isKindOfClass:[NSData class]] == NO) if ([anObject isKindOfClass:[NSData class]] == NO)
{ {
@ -1404,9 +1447,6 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
{ {
struct shmid_ds buf; struct shmid_ds buf;
self = [super init];
if (self == nil)
return nil;
shmid = -1; shmid = -1;
if (aBuffer && bufferSize) if (aBuffer && bufferSize)
{ {
@ -1439,10 +1479,6 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
{ {
struct shmid_ds buf; struct shmid_ds buf;
self = [super init];
if (self == nil)
return nil;
shmid = anId; shmid = anId;
if (shmctl(shmid, IPC_STAT, &buf) < 0) if (shmctl(shmid, IPC_STAT, &buf) < 0)
{ {
@ -1492,9 +1528,12 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
} }
- (id) initWithBytesNoCopy: (void*)aBuffer - (id) initWithBytesNoCopy: (void*)aBuffer
length: (unsigned int)bufferSize length: (unsigned)bufferSize
fromZone: (NSZone*)aZone
{ {
return [super initWithBytesNoCopy: aBuffer length: bufferSize]; bytes = aBuffer;
length = bufferSize;
return self;
} }
@end @end
@ -1529,7 +1568,7 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
{ {
if (bytes) if (bytes)
{ {
objc_free(bytes); NSZoneFree(zone, bytes);
bytes = 0; bytes = 0;
length = 0; length = 0;
capacity = 0; capacity = 0;
@ -1539,8 +1578,8 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
- (void) encodeWithCoder: (NSCoder*)aCoder - (void) encodeWithCoder: (NSCoder*)aCoder
{ {
[aCoder encodeValueOfObjCType:"I" at: &length]; [aCoder encodeValueOfObjCType: @encode(unsigned long) at: &length];
[aCoder encodeArrayOfObjCType:"C" count:length at: bytes]; [aCoder encodeArrayOfObjCType: @encode(unsigned char) count:length at: bytes];
} }
- (id) init - (id) init
@ -1565,33 +1604,45 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
- (id) initWithBytesNoCopy: (void*)aBuffer - (id) initWithBytesNoCopy: (void*)aBuffer
length: (unsigned int)bufferSize length: (unsigned int)bufferSize
{ {
self = [self initWithCapacity: 0]; NSZone *aZone = NSZoneFromPointer(aBuffer);
if (self) return [self initWithBytesNoCopy: aBuffer length: bufferSize fromZone: aZone];
{
bytes = aBuffer;
if (bytes)
{
length = bufferSize;
capacity = bufferSize;
}
}
else
if (aBuffer)
objc_free(aBuffer);
return self;
} }
/* /*
* THIS IS THE DESIGNATED INITIALISER * THIS IS THE DESIGNATED INITIALISER
*/ */
- (id) initWithBytesNoCopy: (void*)aBuffer
length: (unsigned)bufferSize
fromZone: (NSZone*)aZone
{
if (aZone == 0)
{
self = [self initWithBytes: aBuffer length: bufferSize];
return self;
}
if (aBuffer == 0)
{
self = [self initWithCapacity: bufferSize];
if (self)
[self setLength: bufferSize];
return self;
}
zone = aZone;
bytes = aBuffer;
length = bufferSize;
capacity = bufferSize;
return self;
}
- (id) initWithCapacity: (unsigned)size - (id) initWithCapacity: (unsigned)size
{ {
self = [super init]; zone = [self zone];
if (self)
{
if (size) if (size)
{ {
bytes = objc_malloc(size); bytes = NSZoneMalloc(zone, size);
if (bytes == 0) if (bytes == 0)
{ {
NSLog(@"[NSMutableDataMalloc -initWithCapacity:] out of memory for %u bytes - %s", size, strerror(errno)); NSLog(@"[NSMutableDataMalloc -initWithCapacity:] out of memory for %u bytes - %s", size, strerror(errno));
@ -1601,7 +1652,7 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
} }
capacity = size; capacity = size;
length = 0; length = 0;
}
return self; return self;
} }
@ -1610,16 +1661,20 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
unsigned int l; unsigned int l;
void* b; void* b;
[aCoder decodeValueOfObjCType:"I" at: &l]; [aCoder decodeValueOfObjCType: @encode(unsigned long) at: &l];
b = objc_malloc(l); if (l)
if (b == 0)
{ {
NSLog(@"[NSMutableDataMalloc -initWithCode:] unable to allocate %lu bytes", l); [self initWithCapacity: l];
if (bytes == 0)
{
NSLog(@"[NSMutableDataMalloc -initWithCoder:] unable to allocate %lu bytes", l);
[self dealloc]; [self dealloc];
return nil; return nil;
} }
[aCoder decodeArrayOfObjCType:"C" count: l at: b]; [aCoder decodeArrayOfObjCType: @encode(unsigned char) count: l at: bytes];
return [self initWithBytesNoCopy: b length: l]; length = l;
}
return self;
} }
- (id) initWithLength: (unsigned)size - (id) initWithLength: (unsigned)size
@ -1636,7 +1691,7 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
- (id) initWithContentsOfFile: (NSString *)path - (id) initWithContentsOfFile: (NSString *)path
{ {
self = [self initWithCapacity: 0]; self = [self initWithCapacity: 0];
if (readContentsOfFile(path, &bytes, &length) == NO) if (readContentsOfFile(path, &bytes, &length, zone) == NO)
{ {
[self dealloc]; [self dealloc];
self = nil; self = nil;
@ -1681,9 +1736,9 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
void* tmp; void* tmp;
if (bytes) if (bytes)
tmp = objc_realloc(bytes, size); tmp = NSZoneRealloc(zone, bytes, size);
else else
tmp = objc_malloc(size); tmp = NSZoneMalloc(zone, size);
if (tmp == 0) if (tmp == 0)
[NSException raise:NSMallocException [NSException raise:NSMallocException
@ -1762,9 +1817,6 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
struct shmid_ds buf; struct shmid_ds buf;
int e; int e;
self = [super initWithCapacity: 0];
if (self)
{
shmid = shmget(IPC_PRIVATE, bufferSize, IPC_CREAT|VM_ACCESS); shmid = shmget(IPC_PRIVATE, bufferSize, IPC_CREAT|VM_ACCESS);
if (shmid == -1) /* Created memory? */ if (shmid == -1) /* Created memory? */
{ {
@ -1786,7 +1838,7 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
} }
length = 0; length = 0;
capacity = bufferSize; capacity = bufferSize;
}
return self; return self;
} }
@ -1794,9 +1846,6 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
{ {
struct shmid_ds buf; struct shmid_ds buf;
self = [super initWithCapacity: 0];
if (self)
{
shmid = anId; shmid = anId;
if (shmctl(shmid, IPC_STAT, &buf) < 0) if (shmctl(shmid, IPC_STAT, &buf) < 0)
{ {
@ -1820,7 +1869,7 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
} }
length = bufferSize; length = bufferSize;
capacity = length; capacity = length;
}
return self; return self;
} }

View file

@ -30,8 +30,6 @@
#include <Foundation/NSPortCoder.h> #include <Foundation/NSPortCoder.h>
#include <gnustep/base/Coding.h> #include <gnustep/base/Coding.h>
#define BADREALLOC 1
@interface NSGArray : NSArray @interface NSGArray : NSArray
{ {
id *_contents_array; id *_contents_array;
@ -60,6 +58,13 @@
} }
} }
+ (id) allocWithZone: (NSZone*)zone
{
NSGArray *array = NSAllocateObject(self, 0, zone);
return array;
}
- (void) dealloc - (void) dealloc
{ {
if (_contents_array) { if (_contents_array) {
@ -88,7 +93,7 @@
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
if ((_contents_array[i] = [objects[i] retain]) == nil) { if ((_contents_array[i] = [objects[i] retain]) == nil) {
_count = i; _count = i;
[self autorelease]; [self release];
[NSException raise: NSInvalidArgumentException [NSException raise: NSInvalidArgumentException
format: @"Tried to add nil"]; format: @"Tried to add nil"];
} }
@ -98,11 +103,6 @@
return self; return self;
} }
- (Class) classForCoder
{
return [NSArray class];
}
- (void) encodeWithCoder: (NSCoder*)aCoder - (void) encodeWithCoder: (NSCoder*)aCoder
{ {
unsigned i; unsigned i;
@ -121,15 +121,17 @@
{ {
unsigned count; unsigned count;
if ([aCoder systemVersion] == 0) { #if 0
unsigned dummy; unsigned dummy;
[(id<Decoding>)aCoder decodeValueOfCType: @encode(unsigned) [(id<Decoding>)aCoder decodeValueOfCType: @encode(unsigned)
at: &dummy at: &dummy
withName: NULL]; withName: NULL];
[(id<Decoding>)aCoder decodeValueOfCType: @encode(unsigned) [(id<Decoding>)aCoder decodeValueOfCType: @encode(unsigned)
at: &dummy at: &dummy
withName: NULL]; withName: NULL];
} #endif
[(id<Decoding>)aCoder decodeValueOfCType: @encode(unsigned) [(id<Decoding>)aCoder decodeValueOfCType: @encode(unsigned)
at: &count at: &count
withName: NULL]; withName: NULL];
@ -208,7 +210,7 @@
if (_count < e) { if (_count < e) {
e = _count; e = _count;
} }
for (i = aRange.location; i < _count; i++) { for (i = aRange.location; i < e; i++) {
aBuffer[j++] = _contents_array[i]; aBuffer[j++] = _contents_array[i];
} }
} }
@ -266,7 +268,7 @@
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
if ((_contents_array[i] = [objects[i] retain]) == nil) { if ((_contents_array[i] = [objects[i] retain]) == nil) {
_count = i; _count = i;
[self autorelease]; [self release];
[NSException raise: NSInvalidArgumentException [NSException raise: NSInvalidArgumentException
format: @"Tried to add nil"]; format: @"Tried to add nil"];
} }
@ -276,11 +278,6 @@
return self; return self;
} }
- (Class) classForCoder
{
return [NSMutableArray class];
}
- (void) insertObject: (id)anObject atIndex: (unsigned)index - (void) insertObject: (id)anObject atIndex: (unsigned)index
{ {
unsigned i; unsigned i;
@ -297,21 +294,11 @@
id *ptr; id *ptr;
size_t size = (_capacity + _grow_factor)*sizeof(id); size_t size = (_capacity + _grow_factor)*sizeof(id);
#if BADREALLOC
ptr = NSZoneMalloc([self zone], size);
#else
ptr = NSZoneRealloc([self zone], _contents_array, size); ptr = NSZoneRealloc([self zone], _contents_array, size);
#endif
if (ptr == 0) { if (ptr == 0) {
[NSException raise: NSMallocException [NSException raise: NSMallocException
format: @"Unable to grow"]; format: @"Unable to grow"];
} }
#if BADREALLOC
if (_contents_array) {
memcpy(ptr, _contents_array, _capacity*sizeof(id));
NSZoneFree([self zone], _contents_array);
}
#endif
_contents_array = ptr; _contents_array = ptr;
_capacity += _grow_factor; _capacity += _grow_factor;
_grow_factor = _capacity/2; _grow_factor = _capacity/2;
@ -339,21 +326,11 @@
id *ptr; id *ptr;
size_t size = (_capacity + _grow_factor)*sizeof(id); size_t size = (_capacity + _grow_factor)*sizeof(id);
#if BADREALLOC
ptr = NSZoneMalloc([self zone], size);
#else
ptr = NSZoneRealloc([self zone], _contents_array, size); ptr = NSZoneRealloc([self zone], _contents_array, size);
#endif
if (ptr == 0) { if (ptr == 0) {
[NSException raise: NSMallocException [NSException raise: NSMallocException
format: @"Unable to grow"]; format: @"Unable to grow"];
} }
#if BADREALLOC
if (_contents_array) {
memcpy(ptr, _contents_array, _capacity*sizeof(id));
NSZoneFree([self zone], _contents_array);
}
#endif
_contents_array = ptr; _contents_array = ptr;
_capacity += _grow_factor; _capacity += _grow_factor;
_grow_factor = _capacity/2; _grow_factor = _capacity/2;

View file

@ -1,8 +1,10 @@
/* Implementation for GNUStep of NSStrings with C-string backing /* Implementation for GNUStep of NSStrings with C-string backing
Copyright (C) 1993,1994, 1996, 1997 Free Software Foundation, Inc. Copyright (C) 1993,1994, 1996, 1997, 1998 Free Software Foundation, Inc.
Written by: Andrew Kachites McCallum <mccallum@gnu.ai.mit.edu> Written by: Andrew Kachites McCallum <mccallum@gnu.ai.mit.edu>
Date: March 1995 Date: March 1995
Optimised by: Richard frith-Macdoanld <richard@brainstorm.co.uk>
Date: October 1998
This file is part of the GNUstep Base Library. This file is part of the GNUstep Base Library.
@ -25,6 +27,7 @@
#include <gnustep/base/preface.h> #include <gnustep/base/preface.h>
#include <Foundation/NSString.h> #include <Foundation/NSString.h>
#include <Foundation/NSData.h> #include <Foundation/NSData.h>
#include <Foundation/NSCoder.h>
#include <gnustep/base/NSGString.h> #include <gnustep/base/NSGString.h>
#include <gnustep/base/NSGCString.h> #include <gnustep/base/NSGCString.h>
#include <gnustep/base/IndexedCollection.h> #include <gnustep/base/IndexedCollection.h>
@ -39,8 +42,10 @@
- (void)dealloc - (void)dealloc
{ {
if (_free_contents) if (_zone) {
OBJC_FREE(_contents_chars); NSZoneFree(_zone, (void*)_contents_chars);
_zone = 0;
}
[super dealloc]; [super dealloc];
} }
@ -51,24 +56,57 @@
return _hash; return _hash;
} }
/* This is the designated initializer for this class. */ /* This is the GNUstep designated initializer for this class. */
- (id) initWithCStringNoCopy: (char*)byteString
length: (unsigned int)length
fromZone: (NSZone*)zone
{
self = [super init];
if (self) {
_count = length;
_contents_chars = byteString;
_zone = byteString ? zone : 0;
}
return self;
}
/* This is the OpenStep designated initializer for this class. */
- (id) initWithCStringNoCopy: (char*)byteString - (id) initWithCStringNoCopy: (char*)byteString
length: (unsigned int)length length: (unsigned int)length
freeWhenDone: (BOOL)flag freeWhenDone: (BOOL)flag
{ {
/* assert(!flag); xxx need to make a subclass to handle this. */ self = [super init];
[super init]; if (self) {
_count = length; _count = length;
_contents_chars = byteString; _contents_chars = byteString;
_free_contents = flag; if (flag) {
_zone = NSZoneFromPointer(byteString);
}
else {
_zone = 0;
}
}
return self; return self;
} }
- (id) initWithCharactersNoCopy: (unichar*)chars
length: (unsigned int)length
fromZone: (NSZone*)zone
{
NSZone *z = zone ? zone : fastZone(self);
id a = [[NSGString allocWithZone: z] initWithCharactersNoCopy: chars
length: length
fromZone: z];
[self release];
return a;
}
- (id) initWithCharactersNoCopy: (unichar*)chars - (id) initWithCharactersNoCopy: (unichar*)chars
length: (unsigned int)length length: (unsigned int)length
freeWhenDone: (BOOL)flag freeWhenDone: (BOOL)flag
{ {
id a = [[NSGString alloc] initWithCharactersNoCopy: chars NSZone *z = fastZone(self);
id a = [[NSGString allocWithZone: z] initWithCharactersNoCopy: chars
length: length length: length
freeWhenDone: flag]; freeWhenDone: flag];
[self release]; [self release];
@ -77,7 +115,7 @@
- (id) init - (id) init
{ {
return [self initWithCString:""]; return [self initWithCStringNoCopy: 0 length: 0 fromZone: 0];
} }
- (void) _collectionReleaseContents - (void) _collectionReleaseContents
@ -87,21 +125,17 @@
- (void) _collectionDealloc - (void) _collectionDealloc
{ {
if (_free_contents) if (_zone) {
OBJC_FREE(_contents_chars); NSZoneFree(_zone, (void*)_contents_chars);
} _zone = 0;
}
- (Class) classForConnectedCoder: aRmc
{
/* Make sure that Connection's always send us bycopy,
i.e. as our own class, not a Proxy class. */
return [self class];
} }
- (Class) classForPortCoder - (Class) classForPortCoder
{ {
return [self class]; return [self class];
} }
- replacementObjectForPortCoder:(NSPortCoder*)aCoder - replacementObjectForPortCoder:(NSPortCoder*)aCoder
{ {
return self; return self;
@ -109,16 +143,34 @@
- (void) encodeWithCoder: aCoder - (void) encodeWithCoder: aCoder
{ {
[aCoder encodeValueOfObjCType:@encode(char*) at:&_contents_chars [aCoder encodeValueOfObjCType:@encode(unsigned) at:&_count
withName:@"Concrete String count"];
[aCoder encodeArrayOfObjCType:@encode(unsigned char)
count:_count
at:_contents_chars
withName:@"Concrete String content_chars"]; withName:@"Concrete String content_chars"];
} }
- initWithCoder: aCoder - initWithCoder: aCoder
{ {
#if 0
[aCoder decodeValueOfObjCType:@encode(char*) at:&_contents_chars [aCoder decodeValueOfObjCType:@encode(char*) at:&_contents_chars
withName:NULL]; withName:NULL];
_count = strlen(_contents_chars); _count = strlen(_contents_chars);
_free_contents = YES; _zone = NSZoneFromPointer(_contents_chars);
#else
[aCoder decodeValueOfObjCType:@encode(unsigned)
at:&_count
withName:NULL];
if (_count > 0) {
_zone = fastZone(self);
_contents_chars = NSZoneMalloc(_zone, _count);
[aCoder decodeArrayOfObjCType:@encode(unsigned char)
count:_count
at:_contents_chars
withName:NULL];
}
#endif
return self; return self;
} }
@ -133,11 +185,6 @@
return r; return r;
} }
- (const char *) cStringNoCopy
{
return _contents_chars;
}
- (void) getCString: (char*)buffer - (void) getCString: (char*)buffer
{ {
memcpy(buffer, _contents_chars, _count); memcpy(buffer, _contents_chars, _count);
@ -336,7 +383,12 @@
- (id) initWithString: (NSString*)string - (id) initWithString: (NSString*)string
{ {
return [self initWithCString:[string cString]]; NSZone *z = fastZone(self);
unsigned length = [string cStringLength];
char *buf = NSZoneMalloc(z, length+1); // getCString appends a nul.
[string getCString: buf];
return [self initWithCStringNoCopy: buf length: length fromZone: z];
} }
- (NSString*) descriptionForPropertyList - (NSString*) descriptionForPropertyList
@ -362,7 +414,7 @@
case '\f': case '\f':
case '\\': case '\\':
case '"' : case '"' :
length += 2; length += 1;
break; break;
default: default:
@ -370,14 +422,15 @@
needQuote = YES; needQuote = YES;
} }
else if (isprint(val) == 0) { else if (isprint(val) == 0) {
length += 4; length += 3;
} }
break; break;
} }
} }
if (needQuote || length != _count) { if (needQuote || length != _count) {
char *buf = objc_malloc(length+3); NSZone *z = fastZone(self);
char *buf = NSZoneMalloc(z, length+3);
char *ptr = buf; char *ptr = buf;
NSString *result; NSString *result;
@ -413,7 +466,7 @@
*ptr++ = '"'; *ptr++ = '"';
*ptr = '\0'; *ptr = '\0';
result = [[[_fastCls._NSGCString alloc] initWithCStringNoCopy: buf result = [[[_fastCls._NSGCString alloc] initWithCStringNoCopy: buf
length: length+2 freeWhenDone: YES] autorelease]; length: length+2 fromZone: z] autorelease];
return result; return result;
} }
return self; return self;
@ -477,18 +530,40 @@ stringDecrementCountAndFillHoleAt(NSGMutableCStringStruct *self,
} }
/* This is the designated initializer for this class */ /* This is the designated initializer for this class */
/* NB. capacity does not include the '\0' terminator */
- initWithCapacity: (unsigned)capacity - initWithCapacity: (unsigned)capacity
{ {
[super init]; [super init];
_count = 0; _count = 0;
_capacity = MAX(capacity, 3); _capacity = MAX(capacity, 3);
OBJC_MALLOC(_contents_chars, char, _capacity+1); _zone = fastZone(self);
_contents_chars[0] = '\0'; _contents_chars = NSZoneMalloc(_zone, _capacity);
_free_contents = YES;
return self; return self;
} }
- (id) initWithCharactersNoCopy: (unichar*)chars
length: (unsigned int)length
fromZone: (NSZone*)zone
{
NSZone *z = zone ? zone : fastZone(self);
id a = [[NSGMutableString allocWithZone: z] initWithCharactersNoCopy: chars
length: length
fromZone: z];
[self release];
return a;
}
- (id) initWithCharactersNoCopy: (unichar*)chars
length: (unsigned int)length
freeWhenDone: (BOOL)flag
{
NSZone *z = fastZone(self);
id a = [[NSGMutableString allocWithZone: z] initWithCharactersNoCopy: chars
length: length
freeWhenDone: flag];
[self release];
return a;
}
- (void) deleteCharactersInRange: (NSRange)range - (void) deleteCharactersInRange: (NSRange)range
{ {
stringDecrementCountAndFillHoleAt((NSGMutableCStringStruct*)self, stringDecrementCountAndFillHoleAt((NSGMutableCStringStruct*)self,
@ -506,72 +581,71 @@ stringDecrementCountAndFillHoleAt(NSGMutableCStringStruct *self,
- (void) insertString: (NSString*)aString atIndex:(unsigned)index - (void) insertString: (NSString*)aString atIndex:(unsigned)index
{ {
unsigned c = [aString cStringLength]; unsigned c = [aString cStringLength];
if (_count + c > _capacity) char save;
if (_count + c >= _capacity)
{ {
_capacity = MAX(_capacity*2, _count+c); _capacity = MAX(_capacity*2, _count+c+1);
OBJC_REALLOC(_contents_chars, char, _capacity+1); NSZoneRealloc(_zone, _contents_chars, _capacity);
} }
stringIncrementCountAndMakeHoleAt((NSGMutableCStringStruct*)self, index, c); stringIncrementCountAndMakeHoleAt((NSGMutableCStringStruct*)self, index, c);
save = _contents_chars[index+c]; // getCString will put a nul here.
[aString getCString: _contents_chars + index]; [aString getCString: _contents_chars + index];
_contents_chars[_count] = '\0'; _contents_chars[index+c] = save;
} }
- (void) appendString: (NSString*)aString - (void) appendString: (NSString*)aString
{ {
unsigned c = [aString cStringLength]; Class c;
if (_count + c > _capacity)
if (aString == nil || (c = fastClassOfInstance(aString)) == nil)
return;
if (c == _fastCls._NSGCString || c == _fastCls._NSGMutableCString)
{ {
_capacity = MAX(_capacity*2, _count+c); NSGMutableCString *other = (NSGMutableCString*)aString;
OBJC_REALLOC(_contents_chars, char, _capacity+1); unsigned l = other->_count;
if (_count + l > _capacity)
{
_capacity = MAX(_capacity*2, _count+l);
_contents_chars =
NSZoneRealloc(fastZone(self), _contents_chars, _capacity);
}
memcpy(_contents_chars + _count, other->_contents_chars, l);
_count += l;
_hash = 0;
}
else
{
unsigned l = [aString cStringLength];
if (_count + l >= _capacity)
{
_capacity = MAX(_capacity*2, _count+l+1);
_contents_chars =
NSZoneRealloc(fastZone(self), _contents_chars, _capacity);
} }
[aString getCString: _contents_chars + _count]; [aString getCString: _contents_chars + _count];
_count += c; _count += l;
_contents_chars[_count] = '\0';
_hash = 0; _hash = 0;
}
} }
- (void) setString: (NSString*)aString - (void) setString: (NSString*)aString
{ {
unsigned length = [aString cStringLength]; unsigned length = [aString cStringLength];
if (_capacity < length) if (_capacity <= length)
{ {
_capacity = length; _capacity = length+1;
OBJC_REALLOC(_contents_chars, char, _capacity+1); _contents_chars =
NSZoneRealloc(fastZone(self), _contents_chars, _capacity);
} }
[aString getCString: _contents_chars]; [aString getCString: _contents_chars];
_contents_chars[length] = '\0';
_count = length; _count = length;
_hash = 0; _hash = 0;
} }
/* Override NSString's designated initializer for CStrings. */
- (id) initWithCStringNoCopy: (char*)byteString
length: (unsigned int)length
freeWhenDone: (BOOL)flag
{
[super init];
_count = length;
_capacity = length;
_contents_chars = byteString;
_free_contents = flag;
return self;
}
/* Override NSString's designated initializer for Unicode Strings. */
- (id) initWithCharactersNoCopy: (unichar*)chars
length: (unsigned int)length
freeWhenDone: (BOOL)flag
{
id a = [[NSGMutableString alloc] initWithCharactersNoCopy: chars
length: length
freeWhenDone: flag];
[self release];
return a;
}
- (id) init - (id) init
{ {
return [self initWithCString:""]; return [self initWithCStringNoCopy: 0 length: 0 fromZone: 0];
}; };
/* For IndexedCollecting Protocol and other GNU libobjects conformity. */ /* For IndexedCollecting Protocol and other GNU libobjects conformity. */
@ -584,25 +658,17 @@ stringDecrementCountAndFillHoleAt(NSGMutableCStringStruct *self,
range.location, range.length); range.location, range.length);
} }
- (void) encodeWithCoder: aCoder
{
[aCoder encodeValueOfObjCType:@encode(unsigned) at:&_capacity
withName:@"String capacity"];
[aCoder encodeValueOfObjCType:@encode(char*) at:&_contents_chars
withName:@"String content_chars"];
}
- initWithCoder: aCoder - initWithCoder: aCoder
{ {
unsigned cap; unsigned cap;
[aCoder decodeValueOfObjCType:@encode(unsigned) at:&cap withName:NULL]; [aCoder decodeValueOfObjCType:@encode(unsigned) at:&cap withName:NULL];
[self initWithCapacity:cap]; [self initWithCapacity:cap];
[aCoder decodeValueOfObjCType:@encode(char*) at:&_contents_chars _count = cap;
[aCoder decodeArrayOfObjCType:@encode(unsigned char)
count:_count
at:_contents_chars
withName:NULL]; withName:NULL];
_count = strlen(_contents_chars);
_capacity = cap;
_free_contents = YES;
return self; return self;
} }
@ -624,18 +690,16 @@ stringDecrementCountAndFillHoleAt(NSGMutableCStringStruct *self,
if (_count >= _capacity) if (_count >= _capacity)
{ {
_capacity *= 2; _capacity *= 2;
OBJC_REALLOC(_contents_chars, char, _capacity+1); NSZoneRealloc(fastZone(self), _contents_chars, _capacity);
} }
stringIncrementCountAndMakeHoleAt((NSGMutableCStringStruct*)self, index, 1); stringIncrementCountAndMakeHoleAt((NSGMutableCStringStruct*)self, index, 1);
_contents_chars[index] = [newObject charValue]; _contents_chars[index] = [newObject charValue];
_contents_chars[_count] = '\0';
} }
- (void) removeObjectAtIndex: (unsigned)index - (void) removeObjectAtIndex: (unsigned)index
{ {
CHECK_INDEX_RANGE_ERROR(index, _count); CHECK_INDEX_RANGE_ERROR(index, _count);
stringDecrementCountAndFillHoleAt((NSGMutableCStringStruct*)self, index, 1); stringDecrementCountAndFillHoleAt((NSGMutableCStringStruct*)self, index, 1);
_contents_chars[_count] = '\0';
} }
@end @end

View file

@ -39,7 +39,7 @@ typedef struct {
Class *isa; Class *isa;
char *_contents_chars; char *_contents_chars;
int _count; int _count;
BOOL _free_when_done; NSZone *_zone;
unsigned _hash; unsigned _hash;
} *dictAccessToStringHack; } *dictAccessToStringHack;
@ -185,7 +185,7 @@ myEqual(NSObject *self, NSObject *other)
at: &count at: &count
withName: NULL]; withName: NULL];
FastMapInitWithZoneAndCapacity(&map, [self zone], count); FastMapInitWithZoneAndCapacity(&map, fastZone(self), count);
while (count-- > 0) { while (count-- > 0) {
[(id<Decoding>)aCoder decodeObjectAt: &key withName: NULL]; [(id<Decoding>)aCoder decodeObjectAt: &key withName: NULL];
[(id<Decoding>)aCoder decodeObjectAt: &value withName: NULL]; [(id<Decoding>)aCoder decodeObjectAt: &value withName: NULL];
@ -199,7 +199,7 @@ myEqual(NSObject *self, NSObject *other)
- (id) initWithObjects: (id*)objs forKeys: (NSObject**)keys count: (unsigned)c - (id) initWithObjects: (id*)objs forKeys: (NSObject**)keys count: (unsigned)c
{ {
int i; int i;
FastMapInitWithZoneAndCapacity(&map, [self zone], c); FastMapInitWithZoneAndCapacity(&map, fastZone(self), c);
for (i = 0; i < c; i++) { for (i = 0; i < c; i++) {
FastMapNode node = FastMapNodeForKey(&map, (FastMapItem)keys[i]); FastMapNode node = FastMapNodeForKey(&map, (FastMapItem)keys[i]);
@ -251,7 +251,7 @@ myEqual(NSObject *self, NSObject *other)
/* Designated initialiser */ /* Designated initialiser */
- (id) initWithCapacity: (unsigned)cap - (id) initWithCapacity: (unsigned)cap
{ {
FastMapInitWithZoneAndCapacity(&map, [self zone], cap); FastMapInitWithZoneAndCapacity(&map, fastZone(self), cap);
return self; return self;
} }

View file

@ -1,5 +1,5 @@
/* Implementation for GNUStep of NSStrings with Unicode-string backing /* Implementation for GNUStep of NSStrings with Unicode-string backing
Copyright (C) 1997 Free Software Foundation, Inc. Copyright (C) 1997,1998 Free Software Foundation, Inc.
Written by Stevo Crvenkovski <stevo@btinternet.com> Written by Stevo Crvenkovski <stevo@btinternet.com>
Date: February 1997 Date: February 1997
@ -9,6 +9,9 @@
<mccallum@gnu.ai.mit.edu> <mccallum@gnu.ai.mit.edu>
Date: March 1995 Date: March 1995
Optimised by Richard Frith-Macdonald <richard@brainstorm.co.uk>
Date: October 1998
This file is part of the GNUstep Base Library. This file is part of the GNUstep Base Library.
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
@ -46,10 +49,10 @@
- (void)dealloc - (void)dealloc
{ {
if (_free_contents) if (_zone)
{ {
OBJC_FREE(_contents_chars); NSZoneFree(_zone, _contents_chars);
_free_contents = NO; _zone = 0;
} }
[super dealloc]; [super dealloc];
} }
@ -95,43 +98,73 @@
// Initializing Newly Allocated Strings // Initializing Newly Allocated Strings
/* This is the designated initializer for this class. */ /* This is the GNUstep designated initializer for this class. */
- (id) initWithCharactersNoCopy: (unichar*)chars
length: (unsigned int)length
fromZone: (NSZone*)zone
{
self = [super init];
if (self) {
_count = length;
_contents_chars = chars;
_zone = chars ? zone : 0;
}
return self;
}
/* This is the OpenStep designated initializer for this class. */
- (id) initWithCharactersNoCopy: (unichar*)chars - (id) initWithCharactersNoCopy: (unichar*)chars
length: (unsigned int)length length: (unsigned int)length
freeWhenDone: (BOOL)flag freeWhenDone: (BOOL)flag
{ {
/* assert(!flag); xxx need to make a subclass to handle this. */ self = [super init];
[super init]; if (self) {
_count = length; _count = length;
_contents_chars = chars; _contents_chars = chars;
_free_contents = flag; if (flag) {
_zone = NSZoneFromPointer(chars);
}
else {
_zone = 0;
}
}
return self; return self;
} }
- (id) initWithCharacters: (const unichar*)chars - (id) initWithCharacters: (const unichar*)chars
length: (unsigned int)length length: (unsigned int)length
{ {
unichar *s; NSZone *z = fastZone(self);
OBJC_MALLOC(s, unichar, length); unichar *s = NSZoneMalloc(z, length*sizeof(unichar));
if (chars) if (chars)
memcpy(s, chars,2*length); memcpy(s, chars, sizeof(unichar)*length);
return [self initWithCharactersNoCopy:s length:length freeWhenDone:YES]; return [self initWithCharactersNoCopy:s length:length fromZone:z];
}
- (id) initWithCStringNoCopy: (char*)byteString
length: (unsigned int)length
fromZone: (NSZone*)zone
{
id a = [[NSGCString allocWithZone: zone]
initWithCStringNoCopy: byteString length: length fromZone: zone];
[self release];
return a;
} }
- (id) initWithCStringNoCopy: (char*)byteString - (id) initWithCStringNoCopy: (char*)byteString
length: (unsigned int)length length: (unsigned int)length
freeWhenDone: (BOOL)flag freeWhenDone: (BOOL)flag
{ {
id a = [[NSGCString alloc] initWithCStringNoCopy: byteString id a = [[NSGCString allocWithZone: fastZone(self)]
length: length initWithCStringNoCopy: byteString length: length freeWhenDone: flag];
freeWhenDone: flag];
[self release]; [self release];
return a; return a;
} }
- (id) init - (id) init
{ {
return [self initWithCharactersNoCopy:0 length:0 freeWhenDone: NO]; return [self initWithCharactersNoCopy:0 length:0 fromZone: fastZone(self)];
} }
// Getting a String's Length // Getting a String's Length
@ -217,23 +250,6 @@
return blen; return blen;
} }
// #ifndef NO_GNUSTEP
// xxx This is Not NoCopy
// copy of cString just for compatibility
// Is this realy needed ???
- (const char *) cStringNoCopy
{
char *r;
OBJC_MALLOC(r, char, _count+1);
ustrtostr(r,_contents_chars, _count);
r[_count] = '\0';
[NSData dataWithBytesNoCopy: r length: _count+1];
return r;
}
// #endif /* NO_GNUSTEP */
/* NSCoding Protocol */ /* NSCoding Protocol */
- (void) encodeWithCoder: aCoder - (void) encodeWithCoder: aCoder
@ -250,12 +266,12 @@
{ {
[aCoder decodeValueOfObjCType:@encode(int) at:&_count [aCoder decodeValueOfObjCType:@encode(int) at:&_count
withName:NULL]; withName:NULL];
OBJC_MALLOC(_contents_chars, unichar, _count+1); _zone = fastZone(self);
_contents_chars = NSZoneMalloc(_zone, sizeof(unichar)*_count);
[aCoder decodeArrayOfObjCType:@encode(unichar) [aCoder decodeArrayOfObjCType:@encode(unichar)
count:_count count:_count
at:_contents_chars at:_contents_chars
withName:NULL]; withName:NULL];
_free_contents = YES;
return self; return self;
} }
@ -270,21 +286,18 @@
- (void) _collectionDealloc - (void) _collectionDealloc
{ {
if (_free_contents) if (_zone)
OBJC_FREE(_contents_chars); {
} NSZoneFree(_zone, _contents_chars);
_zone = 0;
- (Class) classForConnectedCoder: aRmc }
{
/* Make sure that Connection's always send us bycopy,
i.e. as our own class, not a Proxy class. */
return [self class];
} }
- (Class) classForPortCoder - (Class) classForPortCoder
{ {
return [self class]; return [self class];
} }
- replacementObjectForPortCoder:(NSPortCoder*)aCoder - replacementObjectForPortCoder:(NSPortCoder*)aCoder
{ {
return self; return self;
@ -367,31 +380,74 @@ stringDecrementCountAndFillHoleAt(NSGMutableStringStruct *self,
return [self initWithCapacity: 0]; return [self initWithCapacity: 0];
} }
// This is the designated initializer for this class /* This is the GNUstep designated initializer for this class. */
- (id) initWithCharactersNoCopy: (unichar*)chars
length: (unsigned int)length
fromZone: (NSZone*)zone
{
self = [super init];
if (self) {
_count = length;
_capacity = length;
_contents_chars = chars;
_zone = zone;
}
return self;
}
- (id) initWithCharactersNoCopy: (unichar*)chars - (id) initWithCharactersNoCopy: (unichar*)chars
length: (unsigned int)length length: (unsigned int)length
freeWhenDone: (BOOL)flag freeWhenDone: (BOOL)flag
{ {
[super init]; self = [super init];
if (self) {
_count = length; _count = length;
_capacity = length; _capacity = length;
_contents_chars = chars; _contents_chars = chars;
_free_contents = flag; if (flag) {
_zone = NSZoneFromPointer(chars);
}
else {
_zone = 0;
}
}
return self; return self;
} }
// NB capacity does not include the '\0' terminator.
- initWithCapacity: (unsigned)capacity - initWithCapacity: (unsigned)capacity
{ {
unichar *tmp; self = [super init];
if (self) {
if (capacity < 2) if (capacity < 2)
capacity = 2; capacity = 2;
OBJC_MALLOC(tmp, unichar, capacity); _count = 0;
[self initWithCharactersNoCopy: tmp length: 0 freeWhenDone: YES];
_capacity = capacity; _capacity = capacity;
_zone = fastZone(self);
_contents_chars = NSZoneMalloc(_zone, sizeof(unichar)*capacity);
}
return self; return self;
} }
- (id) initWithCStringNoCopy: (char*)byteString
length: (unsigned int)length
fromZone: (NSZone*)zone
{
id a = [[NSGMutableCString allocWithZone: zone]
initWithCStringNoCopy: byteString length: length fromZone: zone];
[self release];
return a;
}
- (id) initWithCStringNoCopy: (char*)byteString
length: (unsigned int)length
freeWhenDone: (BOOL)flag
{
id a = [[NSGMutableCString allocWithZone: fastZone(self)]
initWithCStringNoCopy: byteString length: length freeWhenDone: flag];
[self release];
return a;
}
// Modify A String // Modify A String
- (void) deleteCharactersInRange: (NSRange)range - (void) deleteCharactersInRange: (NSRange)range
@ -419,7 +475,8 @@ stringDecrementCountAndFillHoleAt(NSGMutableStringStruct *self,
_capacity += stringLength - aRange.length; _capacity += stringLength - aRange.length;
if (_capacity < 2) if (_capacity < 2)
_capacity = 2; _capacity = 2;
OBJC_REALLOC(_contents_chars, unichar, _capacity); _contents_chars =
NSZoneRealloc(_zone, _contents_chars, sizeof(unichar)*_capacity);
} }
#ifdef HAVE_MEMMOVE #ifdef HAVE_MEMMOVE
@ -456,27 +513,14 @@ stringDecrementCountAndFillHoleAt(NSGMutableStringStruct *self,
_capacity = len; _capacity = len;
if (_capacity < 2) if (_capacity < 2)
_capacity = 2; _capacity = 2;
OBJC_REALLOC(_contents_chars, unichar, _capacity); _contents_chars =
NSZoneRealloc(_zone, _contents_chars, sizeof(unichar)*_capacity);
} }
[aString getCharacters: _contents_chars]; [aString getCharacters: _contents_chars];
_count = len; _count = len;
_hash = 0; _hash = 0;
} }
// ************ Stuff from NSGCString *********
/* Override NSString's designated initializer for CStrings. */
- (id) initWithCStringNoCopy: (char*)byteString
length: (unsigned int)length
freeWhenDone: (BOOL)flag
{
id a = [[NSGMutableCString alloc] initWithCStringNoCopy: byteString
length: length
freeWhenDone: flag];
[self release];
return a;
}
/* For IndexedCollecting Protocol and other GNU libobjects conformity. */ /* For IndexedCollecting Protocol and other GNU libobjects conformity. */
/* xxx This should be made to return void, but we need to change /* xxx This should be made to return void, but we need to change
@ -535,7 +579,8 @@ stringDecrementCountAndFillHoleAt(NSGMutableStringStruct *self,
_capacity = _count; _capacity = _count;
if (_capacity < 2) if (_capacity < 2)
_capacity = 2; _capacity = 2;
OBJC_REALLOC(_contents_chars, unichar, _capacity); _contents_chars =
NSZoneRealloc(_zone, _contents_chars, sizeof(unichar)*_capacity);
} }
stringIncrementCountAndMakeHoleAt((NSGMutableStringStruct*)self, index, 1); stringIncrementCountAndMakeHoleAt((NSGMutableStringStruct*)self, index, 1);
_contents_chars[index] = [newObject charValue]; _contents_chars[index] = [newObject charValue];

View file

@ -87,7 +87,7 @@ void _fastBuildCache()
*/ */
#define REFCNT_LOCAL 1 #define REFCNT_LOCAL 1
/* #define CACHE_ZONE 1 */ #define CACHE_ZONE 1
#if defined(REFCNT_LOCAL) || defined(CACHE_ZONE) #if defined(REFCNT_LOCAL) || defined(CACHE_ZONE)
@ -104,12 +104,14 @@ typedef struct obj_layout_unpadded {
NSZone *zone; NSZone *zone;
#endif #endif
} unp; } unp;
#define UNP sizeof(unp)
/* /*
* Now do the REAL version - using the other version to determine * Now do the REAL version - using the other version to determine
* what padding (if any) is required to get the alignment of the * what padding (if any) is required to get the alignment of the
* structure correct. * structure correct.
*/ */
#define ALIGN __alignof__(double)
struct obj_layout { struct obj_layout {
#if defined(REFCNT_LOCAL) #if defined(REFCNT_LOCAL)
unsigned retained; unsigned retained;
@ -117,7 +119,7 @@ struct obj_layout {
#if defined(CACHE_ZONE) #if defined(CACHE_ZONE)
NSZone *zone; NSZone *zone;
#endif #endif
char padding[__alignof(double) - sizeof(unp)%__alignof__(double)]; char padding[ALIGN - ((UNP % ALIGN) ? (UNP % ALIGN) : ALIGN)];
}; };
typedef struct obj_layout *obj; typedef struct obj_layout *obj;
@ -218,6 +220,28 @@ extraRefCount (id anObject)
*/ */
#if defined(REFCNT_LOCAL) || defined(CACHE_ZONE) #if defined(REFCNT_LOCAL) || defined(CACHE_ZONE)
#if defined(CACHE_ZONE)
inline NSZone *
fastZone(NSObject *object)
{
if (fastClass(object) == _fastCls._NXConstantString)
return NSDefaultMallocZone();
return ((obj)object)[-1].zone;
}
#else /* defined(CACHE_ZONE) */
inline NSZone *
fastZone(NSObject *object)
{
if (fastClass(object) == _fastCls._NXConstantString)
return NSDefaultMallocZone();
return NSZoneFromPointer(&((obj)object)[-1]);
}
#endif /* defined(CACHE_ZONE) */
NSObject *NSAllocateObject (Class aClass, unsigned extraBytes, NSZone *zone) NSObject *NSAllocateObject (Class aClass, unsigned extraBytes, NSZone *zone)
{ {
id new = nil; id new = nil;
@ -236,10 +260,10 @@ NSObject *NSAllocateObject (Class aClass, unsigned extraBytes, NSZone *zone)
#endif #endif
new = (id)&((obj)new)[1]; new = (id)&((obj)new)[1];
new->class_pointer = aClass; new->class_pointer = aClass;
}
#ifndef NDEBUG #ifndef NDEBUG
GSDebugAllocationAdd(aClass); GSDebugAllocationAdd(aClass);
#endif #endif
}
return new; return new;
} }
@ -247,16 +271,11 @@ void NSDeallocateObject(NSObject *anObject)
{ {
if ((anObject!=nil) && CLS_ISCLASS(((id)anObject)->class_pointer)) if ((anObject!=nil) && CLS_ISCLASS(((id)anObject)->class_pointer))
{ {
NSZone *z;
obj o = &((obj)anObject)[-1]; obj o = &((obj)anObject)[-1];
NSZone *z = fastZone(anObject);
#ifndef NDEBUG #ifndef NDEBUG
GSDebugAllocationRemove(((id)anObject)->class_pointer); GSDebugAllocationRemove(((id)anObject)->class_pointer);
#endif
#if defined(CACHE_ZONE)
z = o->zone;
#else
z = [anObject zone];
#endif #endif
((id)anObject)->class_pointer = (void*) 0xdeadface; ((id)anObject)->class_pointer = (void*) 0xdeadface;
NSZoneFree(z, o); NSZoneFree(z, o);
@ -266,6 +285,14 @@ void NSDeallocateObject(NSObject *anObject)
#else #else
inline NSZone *
fastZone(NSObject *object)
{
if (fastClass(object) == _fastCls._NXConstantString)
return NSDefaultMallocZone();
return NSZoneFromPointer(object);
}
NSObject *NSAllocateObject (Class aClass, unsigned extraBytes, NSZone *zone) NSObject *NSAllocateObject (Class aClass, unsigned extraBytes, NSZone *zone)
{ {
id new = nil; id new = nil;
@ -276,10 +303,10 @@ NSObject *NSAllocateObject (Class aClass, unsigned extraBytes, NSZone *zone)
{ {
memset (new, 0, size); memset (new, 0, size);
new->class_pointer = aClass; new->class_pointer = aClass;
}
#ifndef NDEBUG #ifndef NDEBUG
GSDebugAllocationAdd(aClass); GSDebugAllocationAdd(aClass);
#endif #endif
}
return new; return new;
} }
@ -300,21 +327,12 @@ void NSDeallocateObject(NSObject *anObject)
#endif /* defined(REFCNT_LOCAL) || defined(CACHE_ZONE) */ #endif /* defined(REFCNT_LOCAL) || defined(CACHE_ZONE) */
#if defined(CACHE_ZONE)
BOOL BOOL
NSShouldRetainWithZone (NSObject *anObject, NSZone *requestedZone) NSShouldRetainWithZone (NSObject *anObject, NSZone *requestedZone)
{ {
return (!requestedZone || requestedZone == NSDefaultMallocZone() return (!requestedZone || requestedZone == NSDefaultMallocZone()
|| ((obj)anObject)[-1].zone == requestedZone); || fastZone(anObject) == requestedZone);
} }
#else
BOOL
NSShouldRetainWithZone (NSObject *anObject, NSZone *requestedZone)
{
return (!requestedZone || requestedZone == NSDefaultMallocZone()
|| [anObject zone] == requestedZone);
}
#endif
@ -739,15 +757,7 @@ static BOOL double_release_check_enabled = NO;
- (NSZone *)zone - (NSZone *)zone
{ {
#if defined(REFCNT_LOCAL) || defined(CACHE_ZONE) return fastZone(self);
#if defined(CACHE_ZONE)
return ((obj)self)[-1].zone;
#else
return NSZoneFromPointer(&((obj)self)[-1]);
#endif
#else
return NSZoneFromPointer(self);
#endif
} }
- (void) encodeWithCoder: (NSCoder*)aCoder - (void) encodeWithCoder: (NSCoder*)aCoder

View file

@ -41,7 +41,8 @@
#define DEFAULT_SIZE 256 #define DEFAULT_SIZE 256
#define PORT_CODER_FORMAT_VERSION 0 #define PORT_CODER_FORMAT_VERSION ((((GNUSTEP_BASE_MAJOR_VERSION * 100) + \
GNUSTEP_BASE_MINOR_VERSION) * 100) + GNUSTEP_BASE_SUBMINOR_VERSION)
static BOOL debug_connected_coder = NO; static BOOL debug_connected_coder = NO;

View file

@ -105,6 +105,12 @@ static Class NSMutableSet_concrete_class;
return [self autorelease]; return [self autorelease];
} }
+ setWithSet: (NSSet*)aSet
{
return [[[self alloc] initWithSet: aSet]
autorelease];
}
+ allocWithZone: (NSZone*)z + allocWithZone: (NSZone*)z
{ {
return NSAllocateObject([self _concreteClass], 0, z); return NSAllocateObject([self _concreteClass], 0, z);
@ -160,16 +166,14 @@ static Class NSMutableSet_concrete_class;
if (NSShouldRetainWithZone(self, z) == NO) if (NSShouldRetainWithZone(self, z) == NO)
needCopy = YES; needCopy = YES;
for (i = 0; (o = [enumerator nextObject]); i++) for (i = 0; (o = [enumerator nextObject]); i++) {
{
objects[i] = [o copyWithZone:z]; objects[i] = [o copyWithZone:z];
if (objects[i] != o) if (objects[i] != o)
needCopy = YES; needCopy = YES;
} }
if (needCopy) if (needCopy)
newSet = [[[[self class] _concreteClass] allocWithZone: z] newSet = [[[[self class] _concreteClass] allocWithZone: z]
initWithObjects:objects initWithObjects:objects count:count];
count:count];
else else
newSet = [self retain]; newSet = [self retain];
for (i = 0; i < count; i++) for (i = 0; i < count; i++)

View file

@ -1,5 +1,5 @@
/* Implementation of GNUSTEP string class /* Implementation of GNUSTEP string class
Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc. Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
Written by: Andrew Kachites McCallum <mccallum@gnu.ai.mit.edu> Written by: Andrew Kachites McCallum <mccallum@gnu.ai.mit.edu>
Date: January 1995 Date: January 1995
@ -7,6 +7,9 @@
Unicode implementation by Stevo Crvenkovski <stevo@btinternet.com> Unicode implementation by Stevo Crvenkovski <stevo@btinternet.com>
Date: February 1997 Date: February 1997
Optimisations by Richard Frith-Macdonald <richard@brainstorm.co.uk>
Date: October 1998
This file is part of the GNUstep Base Library. This file is part of the GNUstep Base Library.
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
@ -316,27 +319,57 @@ handle_printf_atsign (FILE *stream,
/* This is the designated initializer for Unicode Strings. */ /* This is the designated initializer for Unicode Strings. */
- (id) initWithCharactersNoCopy: (unichar*)chars - (id) initWithCharactersNoCopy: (unichar*)chars
length: (unsigned int)length length: (unsigned int)length
freeWhenDone: (BOOL)flag fromZone: (NSZone*)zone
{ {
[self subclassResponsibility:_cmd]; [self subclassResponsibility:_cmd];
return self; return self;
} }
- (id) initWithCharactersNoCopy: (unichar*)chars
length: (unsigned int)length
freeWhenDone: (BOOL)flag
{
if (flag)
return [self initWithCharactersNoCopy: chars
length: length
fromZone: NSZoneFromPointer(chars)];
else
return [self initWithCharactersNoCopy: chars
length: length
fromZone: 0];
return self;
}
- (id) initWithCharacters: (const unichar*)chars - (id) initWithCharacters: (const unichar*)chars
length: (unsigned int)length length: (unsigned int)length
{ {
unichar *s; NSZone *z = [self zone];
OBJC_MALLOC(s, unichar, length+1); unichar *s = NSZoneMalloc(z, sizeof(unichar)*length);
if (chars) if (chars)
memcpy(s, chars,2*length); memcpy(s, chars, sizeof(unichar)*length);
s[length] = (unichar)0;
return [self initWithCharactersNoCopy:s length:length freeWhenDone:YES]; return [self initWithCharactersNoCopy:s length:length fromZone:z];
}
- (id) initWithCStringNoCopy: (char*)byteString
length: (unsigned int)length
freeWhenDone: (BOOL)flag
{
if (flag)
return [self initWithCStringNoCopy: byteString
length: length
fromZone: NSZoneFromPointer(byteString)];
else
return [self initWithCStringNoCopy: byteString
length: length
fromZone: 0];
} }
/* This is the designated initializer for CStrings. */ /* This is the designated initializer for CStrings. */
- (id) initWithCStringNoCopy: (char*)byteString - (id) initWithCStringNoCopy: (char*)byteString
length: (unsigned int)length length: (unsigned int)length
freeWhenDone: (BOOL)flag fromZone: (NSZone*)zone
{ {
[self subclassResponsibility:_cmd]; [self subclassResponsibility:_cmd];
return self; return self;
@ -344,12 +377,12 @@ handle_printf_atsign (FILE *stream,
- (id) initWithCString: (const char*)byteString length: (unsigned int)length - (id) initWithCString: (const char*)byteString length: (unsigned int)length
{ {
char *s; NSZone *z = [self zone];
OBJC_MALLOC(s, char, length+1); char *s = NSZoneMalloc(z, length);
if (byteString) if (byteString)
memcpy(s, byteString, length); memcpy(s, byteString, length);
s[length] = '\0'; return [self initWithCStringNoCopy:s length:length fromZone:z];
return [self initWithCStringNoCopy:s length:length freeWhenDone:YES];
} }
- (id) initWithCString: (const char*)byteString - (id) initWithCString: (const char*)byteString
@ -360,14 +393,14 @@ handle_printf_atsign (FILE *stream,
- (id) initWithString: (NSString*)string - (id) initWithString: (NSString*)string
{ {
unichar *s; NSZone *z = [self zone];
unsigned length = [string length]; unsigned length = [string length];
OBJC_MALLOC(s, unichar, length+1); unichar *s = NSZoneMalloc(z, sizeof(unichar)*length);
[string getCharacters:s]; [string getCharacters:s];
s[length] = (unichar)0;
return [self initWithCharactersNoCopy: s return [self initWithCharactersNoCopy: s
length: length length: length
freeWhenDone: YES]; fromZone: z];
} }
- (id) initWithFormat: (NSString*)format,... - (id) initWithFormat: (NSString*)format,...
@ -504,25 +537,23 @@ handle_printf_atsign (FILE *stream,
- (id) initWithData: (NSData*)data - (id) initWithData: (NSData*)data
encoding: (NSStringEncoding)encoding encoding: (NSStringEncoding)encoding
{ {
if((encoding==[NSString defaultCStringEncoding]) if ((encoding==[NSString defaultCStringEncoding])
|| (encoding==NSASCIIStringEncoding)) || (encoding==NSASCIIStringEncoding))
{ {
char *s; NSZone *z = fastZone(self);
int len=[data length]; int len=[data length];
OBJC_MALLOC(s, char, len+1); char *s = NSZoneMalloc(z, len+1);
[data getBytes:s]; [data getBytes:s];
s[len]=0; return [self initWithCStringNoCopy:s length:len fromZone:z];
return [self initWithCStringNoCopy:s length:len freeWhenDone:YES];
} }
else else
{ {
unichar *u; NSZone *z = fastZone(self);
int count;
int len=[data length]; int len=[data length];
unichar *u = NSZoneMalloc(z, sizeof(unichar)*(len+1));
int count;
const unsigned char *b=[data bytes]; const unsigned char *b=[data bytes];
OBJC_MALLOC(u, unichar, len+1);
if(encoding==NSUnicodeStringEncoding) if(encoding==NSUnicodeStringEncoding)
{ {
@ -537,8 +568,7 @@ handle_printf_atsign (FILE *stream,
else else
count = encode_strtoustr(u,b,len,encoding); count = encode_strtoustr(u,b,len,encoding);
u[count]=(unichar)0; return [self initWithCharactersNoCopy:u length:count fromZone:z];
return [self initWithCharactersNoCopy:u length:count freeWhenDone:YES];
} }
return self; return self;
} }
@ -614,17 +644,16 @@ handle_printf_atsign (FILE *stream,
- (NSString*) stringByAppendingString: (NSString*)aString - (NSString*) stringByAppendingString: (NSString*)aString
{ {
NSZone *z = fastZone(self);
unsigned len = [self length]; unsigned len = [self length];
unsigned otherLength = [aString length]; unsigned otherLength = [aString length];
unichar *s; unichar *s = NSZoneMalloc(z, (len+otherLength)*sizeof(unichar));
NSString *tmp; NSString *tmp;
OBJC_MALLOC(s, unichar, len+otherLength+1);
[self getCharacters:s]; [self getCharacters:s];
[aString getCharacters:s+len]; [aString getCharacters:s+len];
s[len + otherLength]=(unichar) 0; tmp = [[[self class] allocWithZone:z] initWithCharactersNoCopy: s
tmp = [[[self class] alloc] initWithCharactersNoCopy: s length: len+otherLength fromZone: z];
length: len+otherLength
freeWhenDone: YES];
return [tmp autorelease]; return [tmp autorelease];
} }
@ -667,6 +696,7 @@ handle_printf_atsign (FILE *stream,
- (NSString*) substringFromRange: (NSRange)aRange - (NSString*) substringFromRange: (NSRange)aRange
{ {
NSZone *z;
unichar *buf; unichar *buf;
id ret; id ret;
@ -676,11 +706,12 @@ handle_printf_atsign (FILE *stream,
[NSException raise: NSRangeException format:@"Invalid location+length."]; [NSException raise: NSRangeException format:@"Invalid location+length."];
if (aRange.length == 0) if (aRange.length == 0)
return @""; return @"";
OBJC_MALLOC(buf, unichar, aRange.length+1); z = fastZone(self);
buf = NSZoneMalloc(z, sizeof(unichar)*aRange.length);
[self getCharacters:buf range:aRange]; [self getCharacters:buf range:aRange];
ret = [[[self class] alloc] initWithCharactersNoCopy: buf ret = [[[self class] alloc] initWithCharactersNoCopy: buf
length: aRange.length length: aRange.length
freeWhenDone: YES]; fromZone: z];
return [ret autorelease]; return [ret autorelease];
} }
@ -1834,13 +1865,14 @@ else
// but this will work in most cases // but this will work in most cases
- (NSString*) capitalizedString - (NSString*) capitalizedString
{ {
NSZone *z = fastZone(self);
unichar *s; unichar *s;
int count=0; int count=0;
BOOL found=YES; BOOL found=YES;
int len=[self length]; int len=[self length];
id white = [NSCharacterSet whitespaceAndNewlineCharacterSet]; id white = [NSCharacterSet whitespaceAndNewlineCharacterSet];
OBJC_MALLOC(s, unichar,len +1); s = NSZoneMalloc(z, sizeof(unichar)*(len+1));
[self getCharacters:s]; [self getCharacters:s];
s[len] = (unichar)0; s[len] = (unichar)0;
while(count<len) while(count<len)
@ -1867,36 +1899,35 @@ else
}; };
found=NO; found=NO;
}; };
s[count] = (unichar)0; return [[[NSString alloc] initWithCharactersNoCopy:s length:len fromZone:z] autorelease];
return [[[NSString alloc] initWithCharactersNoCopy:s length:len freeWhenDone:YES] autorelease];
} }
- (NSString*) lowercaseString - (NSString*) lowercaseString
{ {
NSZone *z = fastZone(self);
unichar *s; unichar *s;
int count; int count;
int len=[self length]; int len=[self length];
OBJC_MALLOC(s, unichar,len +1); s = NSZoneMalloc(z, sizeof(unichar)*(len+1));
for(count=0;count<len;count++) for(count=0;count<len;count++)
s[count]=uni_tolower([self characterAtIndex:count]); s[count]=uni_tolower([self characterAtIndex:count]);
s[len] = (unichar)0;
return [[[[self class] alloc] initWithCharactersNoCopy: s return [[[[self class] alloc] initWithCharactersNoCopy: s
length: len length: len
freeWhenDone: YES] autorelease]; fromZone: z] autorelease];
} }
- (NSString*) uppercaseString; - (NSString*) uppercaseString;
{ {
NSZone *z = fastZone(self);
unichar *s; unichar *s;
int count; int count;
int len=[self length]; int len=[self length];
OBJC_MALLOC(s, unichar,len +1); s = NSZoneMalloc(z, sizeof(unichar)*(len+1));
for(count=0;count<len;count++) for(count=0;count<len;count++)
s[count]=uni_toupper([self characterAtIndex:count]); s[count]=uni_toupper([self characterAtIndex:count]);
s[len] = (unichar)0;
return [[[[self class] alloc] initWithCharactersNoCopy: s return [[[[self class] alloc] initWithCharactersNoCopy: s
length: len length: len
freeWhenDone: YES] autorelease]; fromZone: z] autorelease];
} }
// Storing the String // Storing the String
@ -2417,12 +2448,14 @@ else
{ {
#define MAXDEC 18 #define MAXDEC 18
NSZone *z = fastZone(self);
unichar *u, *upoint; unichar *u, *upoint;
NSRange r; NSRange r;
id seq,ret; id seq,ret;
int len = [self length]; int len = [self length];
int count = 0; int count = 0;
OBJC_MALLOC(u, unichar, len*MAXDEC+1);
u = NSZoneMalloc(z, sizeof(unichar)*(len*MAXDEC+1));
upoint = u; upoint = u;
while(count < len) while(count < len)
@ -2437,7 +2470,7 @@ else
ret = [[[[self class] alloc] initWithCharactersNoCopy: u ret = [[[[self class] alloc] initWithCharactersNoCopy: u
length: uslen(u) length: uslen(u)
freeWhenDone: YES] autorelease]; fromZone: z] autorelease];
return ret; return ret;
} }
@ -2533,12 +2566,6 @@ else
// #endif // #endif
// #ifndef NO_GNUSTEP // #ifndef NO_GNUSTEP
// This method should be removed
- (const char *) cStringNoCopy
{
[self subclassResponsibility: _cmd];
return NULL;
}
- (NSString*) descriptionForPropertyList - (NSString*) descriptionForPropertyList
{ {
@ -2637,10 +2664,6 @@ else
return [self retain]; return [self retain];
} }
/* xxx Temporarily put this NSObject-like implementation here, so
we don't get String's Collection implementation.
When we separate Core from NonCore methods, this problem will
go away. */
- mutableCopyWithZone: (NSZone*)zone - mutableCopyWithZone: (NSZone*)zone
{ {
return [[[[self class] _mutableConcreteClass] allocWithZone:zone] return [[[[self class] _mutableConcreteClass] allocWithZone:zone]

File diff suppressed because it is too large Load diff