From 4489de0cc0251cd9951d22cc3c8ff6873aead069 Mon Sep 17 00:00:00 2001 From: Richard Frith-MacDonald Date: Fri, 2 Jul 1999 13:26:37 +0000 Subject: [PATCH] Tidy memory allocation for empty strings. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@4507 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 7 +- Source/NSGCString.m | 110 ++++++++++++------ Source/NSGString.m | 127 ++++++++++++--------- Source/NSString.m | 265 ++++++++++++++++++++++++++++---------------- 4 files changed, 323 insertions(+), 186 deletions(-) diff --git a/ChangeLog b/ChangeLog index 30eee5533..347119aff 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,9 +1,12 @@ -Fre Jul 2 12:10:00 1999 Richard Frith-Macdonald +Fri Jul 2 14:20:00 1999 Richard Frith-Macdonald * Source/GSeq.h: Fix boundary error when searching for string in string. + * Source/NSString.m: Don't allocate contents for string with 0 length. + * Source/NSGString.m: ditto + * Source/NSGCString.m: ditto -Fre Jul 2 8:45:00 1999 Richard Frith-Macdonald +Fri Jul 2 8:45:00 1999 Richard Frith-Macdonald * Source/NSUnarchiver.m: In designated initialiser, catch exceptions and deallocate self before re-throwing in order to prevent memory diff --git a/Source/NSGCString.m b/Source/NSGCString.m index ca36d3f5a..dcef3734c 100644 --- a/Source/NSGCString.m +++ b/Source/NSGCString.m @@ -92,14 +92,14 @@ static IMP msInitImp; /* designated initialiser for mutable */ } } -+ allocWithZone: (NSZone*)z ++ (id) allocWithZone: (NSZone*)z { - return NSAllocateObject (self, 0, z); + return NSAllocateObject(self, 0, z); } -+ alloc ++ (id) alloc { - return NSAllocateObject (self, 0, NSDefaultMallocZone()); + return NSAllocateObject(self, 0, NSDefaultMallocZone()); } - (void)dealloc @@ -144,7 +144,7 @@ static IMP msInitImp; /* designated initialiser for mutable */ { NSZone *z; - if (flag) + if (flag && byteString) { z = NSZoneFromPointer(byteString); } @@ -198,7 +198,7 @@ static IMP msInitImp; /* designated initialiser for mutable */ } } -- (void) encodeWithCoder: aCoder +- (void) encodeWithCoder: (NSCoder*)aCoder { [aCoder encodeValueOfObjCType:@encode(unsigned) at:&_count]; if (_count > 0) @@ -209,7 +209,7 @@ static IMP msInitImp; /* designated initialiser for mutable */ } } -- initWithCoder: aCoder +- (id) initWithCoder: (NSCoder*)aCoder { [aCoder decodeValueOfObjCType:@encode(unsigned) at:&_count]; @@ -224,7 +224,7 @@ static IMP msInitImp; /* designated initialiser for mutable */ return self; } -- copy +- (id) copy { NSZone *z = NSDefaultMallocZone(); @@ -234,8 +234,16 @@ static IMP msInitImp; /* designated initialiser for mutable */ char *tmp; obj = (NSGCString*)NSAllocateObject(_fastCls._NSGCString, 0, z); - tmp = NSZoneMalloc(z, _count); - memcpy(tmp, _contents_chars, _count); + if (_count) + { + tmp = NSZoneMalloc(z, _count); + memcpy(tmp, _contents_chars, _count); + } + else + { + tmp = 0; + z = 0; + } obj = (*csInitImp)(obj, csInitSel, tmp, _count, z); if (_hash && obj) { @@ -249,7 +257,7 @@ static IMP msInitImp; /* designated initialiser for mutable */ } } -- copyWithZone: (NSZone*)z +- (id) copyWithZone: (NSZone*)z { if (NSShouldRetainWithZone(self, z) == NO) { @@ -257,8 +265,16 @@ static IMP msInitImp; /* designated initialiser for mutable */ char *tmp; obj = (NSGCString*)NSAllocateObject(_fastCls._NSGCString, 0, z); - tmp = NSZoneMalloc(z, _count); - memcpy(tmp, _contents_chars, _count); + if (_count) + { + tmp = NSZoneMalloc(z, _count); + memcpy(tmp, _contents_chars, _count); + } + else + { + tmp = 0; + z = 0; + } obj = (*csInitImp)(obj, csInitSel, tmp, _count, z); if (_hash && obj) { @@ -272,7 +288,7 @@ static IMP msInitImp; /* designated initialiser for mutable */ } } -- mutableCopy +- (id) mutableCopy { NSGMutableCString *obj; @@ -293,7 +309,7 @@ static IMP msInitImp; /* designated initialiser for mutable */ return obj; } -- mutableCopyWithZone: (NSZone*)z +- (id) mutableCopyWithZone: (NSZone*)z { NSGMutableCString *obj; @@ -525,12 +541,22 @@ static IMP msInitImp; /* designated initialiser for mutable */ - (id) initWithString: (NSString*)string { - NSZone *z = fastZone(self); - unsigned length = [string cStringLength]; - char *buf = NSZoneMalloc(z, length+1); // getCString appends a nul. + unsigned length = [string cStringLength]; + NSZone *z; + char *buf; - [string getCString: buf]; - return [self initWithCStringNoCopy: buf length: length fromZone: z]; + if (length > 0) + { + z = fastZone(self); + buf = NSZoneMalloc(z, length+1); // getCString appends a nul. + [string getCString: buf]; + } + else + { + z = 0; + buf = 0; + } + return [self initWithCStringNoCopy: buf length: length fromZone: z]; } - (void) descriptionTo: (id)output @@ -747,14 +773,14 @@ static IMP msInitImp; /* designated initialiser for mutable */ @implementation NSGMutableCString -+ allocWithZone: (NSZone*)z ++ (id) allocWithZone: (NSZone*)z { - return NSAllocateObject (self, 0, z); + return NSAllocateObject(self, 0, z); } -+ alloc ++ (id) alloc { - return NSAllocateObject (self, 0, NSDefaultMallocZone()); + return NSAllocateObject(self, 0, NSDefaultMallocZone()); } + (void) initialize @@ -822,7 +848,7 @@ stringDecrementCountAndFillHoleAt(NSGMutableCStringStruct *self, } /* This is the designated initializer for this class */ -- initWithCapacity: (unsigned)capacity +- (id) initWithCapacity: (unsigned)capacity { _count = 0; _capacity = capacity; @@ -873,15 +899,23 @@ stringDecrementCountAndFillHoleAt(NSGMutableCStringStruct *self, return a; } -- copy +- (id) copy { char *tmp; NSGCString *obj; NSZone *z = NSDefaultMallocZone(); obj = (NSGCString*)NSAllocateObject(_fastCls._NSGCString, 0, z); - tmp = NSZoneMalloc(z, _count); - memcpy(tmp, _contents_chars, _count); + if (_count) + { + tmp = NSZoneMalloc(z, _count); + memcpy(tmp, _contents_chars, _count); + } + else + { + tmp = 0; + z = 0; + } obj = (*csInitImp)(obj, csInitSel, tmp, _count, z); if (_hash && obj) { @@ -892,14 +926,22 @@ stringDecrementCountAndFillHoleAt(NSGMutableCStringStruct *self, return obj; } -- copyWithZone: (NSZone*)z +- (id) copyWithZone: (NSZone*)z { char *tmp; NSGCString *obj; obj = (NSGCString*)NSAllocateObject(_fastCls._NSGCString, 0, z); - tmp = NSZoneMalloc(z, _count); - memcpy(tmp, _contents_chars, _count); + if (_count) + { + tmp = NSZoneMalloc(z, _count); + memcpy(tmp, _contents_chars, _count); + } + else + { + tmp = 0; + z = 0; + } obj = (*csInitImp)(obj, csInitSel, tmp, _count, z); if (_hash && obj) { @@ -910,7 +952,7 @@ stringDecrementCountAndFillHoleAt(NSGMutableCStringStruct *self, return obj; } -- mutableCopy +- (id) mutableCopy { NSGMutableCString *obj; @@ -955,7 +997,7 @@ stringDecrementCountAndFillHoleAt(NSGMutableCStringStruct *self, // xxx This should be primitive method - (void) replaceCharactersInRange: (NSRange)range - withString: (NSString*)aString + withString: (NSString*)aString { [self deleteCharactersInRange:range]; [self insertString:aString atIndex:range.location]; @@ -1030,7 +1072,7 @@ stringDecrementCountAndFillHoleAt(NSGMutableCStringStruct *self, range.location, range.length); } -- initWithCoder: aCoder +- (id) initWithCoder: (NSCoder*)aCoder { unsigned cap; diff --git a/Source/NSGString.m b/Source/NSGString.m index eb33caac5..0b1842f63 100644 --- a/Source/NSGString.m +++ b/Source/NSGString.m @@ -162,13 +162,14 @@ length: (unsigned int)length fromZone: (NSZone*)zone { - self = [super init]; - if (self) { - _count = length; - _contents_chars = chars; - _zone = chars ? zone : 0; + self = [super init]; + if (self) + { + _count = length; + _contents_chars = chars; + _zone = chars ? zone : 0; } - return self; + return self; } /* This is the OpenStep designated initializer for this class. */ @@ -176,29 +177,40 @@ length: (unsigned int)length freeWhenDone: (BOOL)flag { - self = [super init]; - if (self) { - _count = length; - _contents_chars = chars; - if (flag) { - _zone = NSZoneFromPointer(chars); + self = [super init]; + if (self) + { + _count = length; + _contents_chars = chars; + if (flag && chars) + { + _zone = NSZoneFromPointer(chars); } - else { - _zone = 0; + else + { + _zone = 0; } } - return self; + return self; } - (id) initWithCharacters: (const unichar*)chars length: (unsigned int)length { - NSZone *z = fastZone(self); - unichar *s = NSZoneMalloc(z, length*sizeof(unichar)); + NSZone *z = fastZone(self); + unichar *s; - if (chars) + if (length) + { + s = NSZoneMalloc(z, length*sizeof(unichar)); + if (chars) memcpy(s, chars, sizeof(unichar)*length); - return [self initWithCharactersNoCopy:s length:length fromZone:z]; + } + else + { + s = 0; + } + return [self initWithCharactersNoCopy:s length:length fromZone:z]; } - (id) initWithCStringNoCopy: (char*)byteString @@ -304,7 +316,7 @@ /* NSCoding Protocol */ -- (void) encodeWithCoder: aCoder +- (void) encodeWithCoder: (NSCoder*)aCoder { [aCoder encodeValueOfObjCType: @encode(unsigned) at: &_count]; if (_count > 0) @@ -315,7 +327,7 @@ } } -- initWithCoder: aCoder +- (id) initWithCoder: (NSCoder*)aCoder { [aCoder decodeValueOfObjCType: @encode(unsigned) at: &_count]; if (_count) @@ -467,14 +479,14 @@ // @protocol NSMutableString -+ allocWithZone: (NSZone*)z ++ (id) allocWithZone: (NSZone*)z { - return NSAllocateObject (self, 0, z); + return NSAllocateObject(self, 0, z); } -+ alloc ++ (id) alloc { - return NSAllocateObject (self, 0, NSDefaultMallocZone()); + return NSAllocateObject(self, 0, NSDefaultMallocZone()); } + (void) initialize @@ -540,47 +552,54 @@ stringDecrementCountAndFillHoleAt(NSGMutableStringStruct *self, length: (unsigned int)length fromZone: (NSZone*)zone { - self = [super init]; - if (self) { - _count = length; - _capacity = length; - _contents_chars = chars; - _zone = zone; + self = [super init]; + if (self) + { + _count = length; + _capacity = length; + _contents_chars = chars; + _zone = zone; } - return self; + return self; } - (id) initWithCharactersNoCopy: (unichar*)chars length: (unsigned int)length freeWhenDone: (BOOL)flag { - self = [super init]; - if (self) { - _count = length; - _capacity = length; - _contents_chars = chars; - if (flag) { - _zone = NSZoneFromPointer(chars); + self = [super init]; + if (self) + { + _count = length; + _capacity = length; + _contents_chars = chars; + if (flag && chars) + { + _zone = NSZoneFromPointer(chars); } - else { - _zone = 0; + else + { + _zone = 0; } } - return self; + return self; } -- initWithCapacity: (unsigned)capacity +- (id) initWithCapacity: (unsigned)capacity { - self = [super init]; - if (self) { - if (capacity < 2) - capacity = 2; - _count = 0; - _capacity = capacity; - _zone = fastZone(self); - _contents_chars = NSZoneMalloc(_zone, sizeof(unichar)*capacity); + self = [super init]; + if (self) + { + if (capacity < 2) + { + capacity = 2; + } + _count = 0; + _capacity = capacity; + _zone = fastZone(self); + _contents_chars = NSZoneMalloc(_zone, sizeof(unichar)*capacity); } - return self; + return self; } - (id) initWithCStringNoCopy: (char*)byteString @@ -612,7 +631,7 @@ stringDecrementCountAndFillHoleAt(NSGMutableStringStruct *self, } - (void) replaceCharactersInRange: (NSRange)aRange - withString: (NSString*)aString + withString: (NSString*)aString { int offset; unsigned stringLength; @@ -683,7 +702,7 @@ stringDecrementCountAndFillHoleAt(NSGMutableStringStruct *self, range.location, range.length); } -- initWithCoder: aCoder // *** changed to unichar +- (id) initWithCoder: (NSCoder*)aCoder { unsigned cap; diff --git a/Source/NSString.m b/Source/NSString.m index a5d1491fc..80413974d 100644 --- a/Source/NSString.m +++ b/Source/NSString.m @@ -384,13 +384,23 @@ handle_printf_atsign (FILE *stream, - (id) initWithCharacters: (const unichar*)chars length: (unsigned)length { - NSZone *z = [self zone]; - unichar *s = NSZoneMalloc(z, sizeof(unichar)*length); + NSZone *z; + unichar *s; - if (chars) + if (length > 0) + { + z = [self zone]; + s = NSZoneMalloc(z, sizeof(unichar)*length); + if (chars) memcpy(s, chars, sizeof(unichar)*length); + } + else + { + s = 0; + z = 0; + } - return [self initWithCharactersNoCopy: s length: length fromZone: z]; + return [self initWithCharactersNoCopy: s length: length fromZone: z]; } - (id) initWithCStringNoCopy: (char*)byteString @@ -400,7 +410,7 @@ handle_printf_atsign (FILE *stream, if (flag) return [self initWithCStringNoCopy: byteString length: length - fromZone: NSZoneFromPointer(byteString)]; + fromZone: length?NSZoneFromPointer(byteString):0]; else return [self initWithCStringNoCopy: byteString length: length @@ -418,12 +428,25 @@ handle_printf_atsign (FILE *stream, - (id) initWithCString: (const char*)byteString length: (unsigned)length { - NSZone *z = [self zone]; - char *s = NSZoneMalloc(z, length); + NSZone *z; + char *s; - if (byteString) - memcpy(s, byteString, length); - return [self initWithCStringNoCopy: s length: length fromZone: z]; + if (length > 0) + { + z = [self zone]; + s = NSZoneMalloc(z, length); + if (byteString) + { + memcpy(s, byteString, length); + } + } + else + { + s = 0; + z = 0; + } + + return [self initWithCStringNoCopy: s length: length fromZone: z]; } - (id) initWithCString: (const char*)byteString @@ -434,14 +457,24 @@ handle_printf_atsign (FILE *stream, - (id) initWithString: (NSString*)string { - NSZone *z = [self zone]; - unsigned length = [string length]; - unichar *s = NSZoneMalloc(z, sizeof(unichar)*length); + unsigned length = [string length]; + NSZone *z; + unichar *s; - [string getCharacters: s]; - return [self initWithCharactersNoCopy: s - length: length - fromZone: z]; + if (length > 0) + { + z = [self zone]; + s = NSZoneMalloc(z, sizeof(unichar)*length); + [string getCharacters: s]; + } + else + { + s = 0; + z = 0; + } + return [self initWithCharactersNoCopy: s + length: length + fromZone: z]; } - (id) initWithFormat: (NSString*)format,... @@ -568,34 +601,50 @@ handle_printf_atsign (FILE *stream, } - (id) initWithFormat: (NSString*)format - locale: (NSDictionary*)dictionary - arguments: (va_list)argList + locale: (NSDictionary*)dictionary + arguments: (va_list)argList { [self notImplemented: _cmd]; return self; } - (id) initWithData: (NSData*)data - encoding: (NSStringEncoding)encoding + encoding: (NSStringEncoding)encoding { if ((encoding==[NSString defaultCStringEncoding]) || (encoding==NSASCIIStringEncoding)) { - NSZone *z = fastZone(self); - int len=[data length]; - char *s = NSZoneMalloc(z, len+1); + unsigned len=[data length]; + NSZone *z; + char *s; - [data getBytes: s]; + if (len > 0) + { + z = fastZone(self); + s = NSZoneMalloc(z, len); + [data getBytes: s]; + } + else + { + s = 0; + z = 0; + } return [self initWithCStringNoCopy: s length: len fromZone: z]; } else { - NSZone *z = fastZone(self); - int len=[data length]; - unichar *u = NSZoneMalloc(z, sizeof(unichar)*(len+1)); - int count; - const unsigned char *b=[data bytes]; + unsigned len = [data length]; + NSZone *z; + unichar *u; + unsigned count; + const unsigned char *b; + if (len < 2) + return [self initWithCStringNoCopy: 0 length: 0 fromZone: z]; + + z = fastZone(self); + b=[data bytes]; + u = NSZoneMalloc(z, sizeof(unichar)*(len+1)); if (encoding==NSUnicodeStringEncoding) { if ((b[0]==0xFE)&(b[1]==0xFF)) @@ -617,10 +666,14 @@ handle_printf_atsign (FILE *stream, - (id) initWithContentsOfFile: (NSString*)path { NSStringEncoding enc; - id d = [NSData dataWithContentsOfFile: path]; - const unsigned char *test=[d bytes]; + id d = [NSData dataWithContentsOfFile: path]; + const unsigned char *test; - if (d == nil) return nil; + if (d == nil) + return nil; + if ([d length] < 2) + return @""; + test = [d bytes]; if (test && (((test[0]==0xFF) && (test[1]==0xFE)) || ((test[1]==0xFF) && (test[0]==0xFE)))) enc = NSUnicodeStringEncoding; else @@ -630,8 +683,8 @@ handle_printf_atsign (FILE *stream, - (id) init { - self = [super init]; - return self; + self = [super init]; + return self; } // Getting a String's Length @@ -912,19 +965,23 @@ handle_printf_atsign (FILE *stream, - (BOOL) isEqual: (id)anObject { - if (anObject == self) { - return YES; + if (anObject == self) + { + return YES; } - if (anObject != nil) { - Class c = fastClassOfInstance(anObject); + if (anObject != nil) + { + Class c = fastClassOfInstance(anObject); - if (c != nil) { - if (fastClassIsKindOfClass(c, NSString_class)) { - return [self isEqualToString: anObject]; + if (c != nil) + { + if (fastClassIsKindOfClass(c, NSString_class)) + { + return [self isEqualToString: anObject]; } } } - return NO; + return NO; } - (BOOL) isEqualToString: (NSString*)aString @@ -1244,54 +1301,63 @@ handle_printf_atsign (FILE *stream, // but this will work in most cases - (NSString*) capitalizedString { - NSZone *z = fastZone(self); - unichar *s; - int count=0; - BOOL found=YES; - int len=[self length]; + NSZone *z; + unichar *s; + unsigned count = 0; + BOOL found = YES; + unsigned len = [self length]; + if (len == 0) + return self; if (whitespce == nil) setupWhitespce(); - s = NSZoneMalloc(z, sizeof(unichar)*(len+1)); + z = fastZone(self); + s = NSZoneMalloc(z, sizeof(unichar)*len); [self getCharacters: s]; - s[len] = (unichar)0; while (countlength = aRange.length - maxLength; } } - count=0; - while (count