diff --git a/ChangeLog b/ChangeLog index dfbf47599..f690a35b3 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +2001-12-04 Richard Frith-Macdonald + + * Source/NSValue.m: polish last set of changes - + Optimisation ... cut memory allocation/deallocation to a minimum. + Versioning ... set NSValue version number and attempt to decode + old format objects. + Simplification ... remove redundant code in placeholder class. + Fixes ... allocate new objects in correct memory zone, I think a few + others I forgot. + * Source/NSNumberFormatter.m (-initWithCoder:): Avoid unnecessary + autorelease/retain sequences. (-init): Fix illegal re-initialisation + of initialised values and avoid unnecessary use of autorelease. + 2001-12-03 Laurent Julliard * Source/NSNumberFormatter.m (-initWithCoder:): decoded objects diff --git a/Source/NSNumberFormatter.m b/Source/NSNumberFormatter.m index a6308badd..77cdce399 100644 --- a/Source/NSNumberFormatter.m +++ b/Source/NSNumberFormatter.m @@ -155,13 +155,17 @@ - (id) init { + id o; + _allowsFloats = YES; _decimalSeparator = '.'; _thousandSeparator = ','; - [self setAttributedStringForNil: AUTORELEASE([[NSAttributedString new] - initWithString: @""])]; - [self setAttributedStringForNotANumber: AUTORELEASE([[NSAttributedString new] - initWithString: @"NaN"])]; + o = [[NSAttributedString alloc] initWithString: @""]; + [self setAttributedStringForNil: o]; + RELEASE(o); + o = [[NSAttributedString alloc] initWithString: @"NaN"]; + [self setAttributedStringForNotANumber: o]; + RELEASE(o); return self; } @@ -174,16 +178,19 @@ [decoder decodeValueOfObjCType: @encode(unichar) at: &_thousandSeparator]; [decoder decodeValueOfObjCType: @encode(unichar) at: &_decimalSeparator]; - _roundingBehavior = RETAIN([decoder decodeObject]); - _maximum = RETAIN([decoder decodeObject]); - _minimum = RETAIN([decoder decodeObject]); - _attributedStringForNil = RETAIN([decoder decodeObject]); - _attributedStringForNotANumber = RETAIN([decoder decodeObject]); - _attributedStringForZero = RETAIN([decoder decodeObject]); - _negativeFormat = RETAIN([decoder decodeObject]); - _positiveFormat = RETAIN([decoder decodeObject]); - _attributesForPositiveValues = RETAIN([decoder decodeObject]); - _attributesForNegativeValues = RETAIN([decoder decodeObject]); + [decoder decodeValueOfObjCType: @encode(id) at: &_roundingBehavior]; + [decoder decodeValueOfObjCType: @encode(id) at: &_maximum]; + [decoder decodeValueOfObjCType: @encode(id) at: &_minimum]; + [decoder decodeValueOfObjCType: @encode(id) at: &_attributedStringForNil]; + [decoder decodeValueOfObjCType: @encode(id) + at: &_attributedStringForNotANumber]; + [decoder decodeValueOfObjCType: @encode(id) at: &_attributedStringForZero]; + [decoder decodeValueOfObjCType: @encode(id) at: &_negativeFormat]; + [decoder decodeValueOfObjCType: @encode(id) at: &_positiveFormat]; + [decoder decodeValueOfObjCType: @encode(id) + at: &_attributesForPositiveValues]; + [decoder decodeValueOfObjCType: @encode(id) + at: &_attributesForNegativeValues]; return self; } diff --git a/Source/NSValue.m b/Source/NSValue.m index 2188360e7..5f4fec396 100644 --- a/Source/NSValue.m +++ b/Source/NSValue.m @@ -68,6 +68,7 @@ static NSLock *placeholderLock; if (self == [NSValue class]) { abstractClass = self; + [abstractClass setVersion: 1]; // Version 1 concreteClass = [GSValue class]; nonretainedObjectValueClass = [GSNonretainedObjectValue class]; pointValueClass = [GSPointValue class]; @@ -381,33 +382,118 @@ static NSLock *placeholderLock; - (id) initWithCoder: (NSCoder *)coder { + char type[64]; const char *objctype; Class c; id o; unsigned size; - NSData *d; - char *data; - unsigned cursor = 0; + int ver; [coder decodeValueOfObjCType: @encode(unsigned) at: &size]; - objctype = (void*)NSZoneMalloc(NSDefaultMallocZone(), size); + /* + * For almost all type encodings, we can use space on the stack, + * but to handle exceptionally large ones (possibly some huge structs) + * we have a strategy of allocating and deallocating heap space too. + */ + if (size <= 64) + { + objctype = type; + } + else + { + objctype = (void*)NSZoneMalloc(NSDefaultMallocZone(), size); + } [coder decodeArrayOfObjCType: @encode(signed char) count: size at: (void*)objctype]; c = [abstractClass valueClassWithObjCType: objctype]; - o = [c alloc]; + o = [c allocWithZone: [coder objectZone]]; - size = objc_sizeof_type(objctype); - data = (void *)NSZoneMalloc(NSDefaultMallocZone(), size); - [coder decodeValueOfObjCType: @encode(id) at: &d]; - [d deserializeDataAt: data - ofObjCType: objctype - atCursor: &cursor - context: nil]; - o = [o initWithBytes: data objCType: objctype]; - RELEASE(d); - NSZoneFree(NSDefaultMallocZone(), data); - NSZoneFree(NSDefaultMallocZone(), (void*)objctype); + ver = [coder versionForClassName: @"NSValue"]; + if (ver < 1) + { + if (c == pointValueClass) + { + NSPoint v; + + [coder decodeValueOfObjCType: @encode(NSPoint) at: &v]; + o = [o initWithBytes: &v objCType: @encode(NSPoint)]; + } + else if (c == sizeValueClass) + { + NSSize v; + + [coder decodeValueOfObjCType: @encode(NSSize) at: &v]; + o = [o initWithBytes: &v objCType: @encode(NSSize)]; + } + else if (c == rangeValueClass) + { + NSRange v; + + [coder decodeValueOfObjCType: @encode(NSRange) at: &v]; + o = [o initWithBytes: &v objCType: @encode(NSRange)]; + } + else if (c == rectValueClass) + { + NSRect v; + + [coder decodeValueOfObjCType: @encode(NSRect) at: &v]; + o = [o initWithBytes: &v objCType: @encode(NSRect)]; + } + else + { + unsigned char *data; + + [coder decodeValueOfObjCType: @encode(unsigned) at: &size]; + data = (void *)NSZoneMalloc(NSDefaultMallocZone(), size); + [coder decodeArrayOfObjCType: @encode(unsigned char) + count: size + at: (void*)data]; + o = [o initWithBytes: data objCType: objctype]; + NSZoneFree(NSDefaultMallocZone(), data); + } + } + else + { + NSData *d; + unsigned cursor = 0; + + /* + * For performance, decode small values directly onto the stack, + * For larger values we allocate and deallocate heap space. + */ + size = objc_sizeof_type(objctype); + if (size <= 64) + { + unsigned char data[size]; + + [coder decodeValueOfObjCType: @encode(id) at: &d]; + [d deserializeDataAt: data + ofObjCType: objctype + atCursor: &cursor + context: nil]; + o = [o initWithBytes: data objCType: objctype]; + RELEASE(d); + } + else + { + unsigned char *data; + + data = (void *)NSZoneMalloc(NSDefaultMallocZone(), size); + [coder decodeValueOfObjCType: @encode(id) at: &d]; + [d deserializeDataAt: data + ofObjCType: objctype + atCursor: &cursor + context: nil]; + o = [o initWithBytes: data objCType: objctype]; + RELEASE(d); + NSZoneFree(NSDefaultMallocZone(), data); + } + } + if (objctype != type) + { + NSZoneFree(NSDefaultMallocZone(), (void*)objctype); + } RELEASE(self); self = o; return self; @@ -436,39 +522,6 @@ static NSLock *placeholderLock; format: @"attempt to use uninitialised value"]; } -- (id) initWithCoder: (NSCoder *)coder -{ - const char *objctype; - Class c; - id o; - unsigned size; - NSData *d; - char *data; - unsigned cursor = 0; - - [coder decodeValueOfObjCType: @encode(unsigned) at: &size]; - objctype = (void*)NSZoneMalloc(NSDefaultMallocZone(), size); - [coder decodeArrayOfObjCType: @encode(signed char) - count: size - at: (void*)objctype]; - c = [abstractClass valueClassWithObjCType: objctype]; - o = [c alloc]; - - size = objc_sizeof_type(objctype); - data = (void *)NSZoneMalloc(NSDefaultMallocZone(), size); - [coder decodeValueOfObjCType: @encode(id) at: &d]; - [d deserializeDataAt: data - ofObjCType: objctype - atCursor: &cursor - context: nil]; - o = [o initWithBytes: data objCType: objctype]; - RELEASE(d); - NSZoneFree(NSDefaultMallocZone(), data); - NSZoneFree(NSDefaultMallocZone(), (void*)objctype); - self = o; - return self; -} - - (id) initWithBytes: (const void*)data objCType: (const char*)type { Class c = [abstractClass valueClassWithObjCType: type];