Simplify memory management/initialisation for strings and data.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@7621 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Richard Frith-MacDonald 2000-09-27 15:26:16 +00:00
parent 2f6bd9b5f3
commit b2b79c362d
12 changed files with 248 additions and 608 deletions

View file

@ -1,5 +1,13 @@
2000-09-27 Richard Frith-Macdonald <rfm@gnu.org>
* Source/GSCompatibility.m: Use OpenStep standard string initialisation.
* Source/NSSerializer.m: Use OpenStep stanrd initializers.
* Source/NSUnarchiver.m: ditto
* Source/NSData.m: Simplify by removing memory management extensions.
* Source/NSGCString.m: ditto
* Source/NSGString.m: ditto
* Source/NSObject.m: ditto
* Source/NSString.m: ditto
* Source/externs.m: Removed unused NXConstantString assignments.
2000-09-26 Lyndon Tremblay <humasect@home.com>

View file

@ -185,35 +185,6 @@
- (void) deserializeTypeTag: (unsigned char*)tag
andCrossRef: (unsigned int*)xref
atCursor: (unsigned*)cursor;
/*
* -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
* For an NSData object with a malloced buffer, returns that buffer and
* removes it from the NSData object, otherwise returns a nul pointer.
* Use with care, preferably when no-one else has retained the NSData
* object - or they will find it's buffer disappearing unexpectedly.
* Once you have used this method, you own the malloced data and are
* responsible for freeing it.
* NB. While this buffer is guaranteed to be freeable by NSZoneFree(),
* it's not necessarily safe to pass it to free()/objc_free() and
* friends. If you wish to pass the buffer to code that might use
* free() or realloc(), you should use the
* -relinquishAllocatedBytesFromZone: method instead - this method
* will only relinquich the buffer if it was allocated from the
* specified zone (a zone of 0 disables this checking).
*/
- (void*) relinquishAllocatedBytes;
- (void*) relinquishAllocatedBytesFromZone: (NSZone*)aZone;
@end
#endif

View file

@ -274,12 +274,6 @@ enum {
#ifndef NO_GNUSTEP
- (BOOL) boolValue;
- (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 */
@end

View file

@ -168,9 +168,7 @@ encodeBase64(NSData *source)
}
return [[NSGCString allocWithZone: NSDefaultMallocZone()]
initWithCStringNoCopy: dBuf
length: destlen-1
fromZone: NSDefaultMallocZone()];
initWithCStringNoCopy: dBuf length: destlen-1];
}
static NSString*

View file

@ -286,7 +286,6 @@ failure:
@interface NSDataMalloc : NSDataStatic
{
NSZone *zone;
}
@end
@ -425,7 +424,7 @@ failure:
NSData *d;
d = [dataMalloc allocWithZone: NSDefaultMallocZone()];
d = [d initWithBytesNoCopy: 0 length: 0 fromZone: NSDefaultMallocZone()];
d = [d initWithBytesNoCopy: 0 length: 0];
return d;
}
@ -437,44 +436,72 @@ failure:
- (id) initWithBytes: (const void*)aBuffer
length: (unsigned)bufferSize
{
[self subclassResponsibility: _cmd];
return nil;
void *ptr = NSZoneMalloc(NSDefaultMallocZone(), bufferSize);
memcpy(ptr, aBuffer, bufferSize);
return [self initWithBytesNoCopy: ptr length: bufferSize];
}
- (id) initWithBytesNoCopy: (void*)aBuffer
length: (unsigned)bufferSize
{
if (aBuffer)
return [self initWithBytesNoCopy: aBuffer
length: bufferSize
fromZone: NSZoneFromPointer(aBuffer)];
else
return [self initWithBytesNoCopy: aBuffer
length: bufferSize
fromZone: [self zone]];
[self subclassResponsibility: _cmd];
return nil;
}
- (id) initWithContentsOfFile: (NSString *)path
{
[self subclassResponsibility: _cmd];
return nil;
void *fileBytes;
unsigned fileLength;
NSZone *zone;
#if GS_WITH_GC
zone = GSAtomicMallocZone();
#else
zone = fastZone(self);
#endif
if (readContentsOfFile(path, &fileBytes, &fileLength, zone) == NO)
{
RELEASE(self);
self = nil;
}
else
{
self = [self initWithBytesNoCopy: fileBytes length: fileLength];
}
return self;
}
- (id) initWithContentsOfMappedFile: (NSString *)path;
{
[self subclassResponsibility: _cmd];
return nil;
#if HAVE_MMAP
RELEASE(self);
self = [NSDataMappedFile allocWithZone: fastZone(self)];
return [self initWithContentsOfMappedFile: path];
#else
return [self initWithContentsOfFile: path];
#endif
}
- (id) initWithContentsOfURL: (NSURL*)url
{
NSData *data = [url resourceDataUsingCache: YES];
return [self initWithBytes: [data bytes] length: [data length]];
return [self initWithData: data];
}
- (id) initWithData: (NSData*)data
{
if (data == nil)
{
return [self initWithBytesNoCopy: 0 length: 0];
}
if ([data isKindOfClass: [NSData class]] == NO)
{
NSLog(@"-initWithData: passed a non-data object");
RELEASE(self);
return nil;
}
return [self initWithBytes: [data bytes] length: [data length]];
}
@ -518,14 +545,9 @@ failure:
}
dest[j++] = '>';
dest[j] = '\0';
#if GS_WITH_GC
str = [[NSString allocWithZone: z]
initWithCStringNoCopy: dest length: j fromZone: GSAtomicMallocZone()];
#else
str = [[NSString allocWithZone: z] initWithCStringNoCopy: dest
length: j
fromZone: z];
#endif
freeWhenDone: YES];
return AUTORELEASE(str);
}
@ -1227,14 +1249,6 @@ failure:
return AUTORELEASE(d);
}
- (id) initWithBytesNoCopy: (void*)bytes
length: (unsigned)length
fromZone: (NSZone*)zone
{
[self subclassResponsibility: _cmd];
return nil;
}
- (void) deserializeTypeTag: (unsigned char*)tag
andCrossRef: (unsigned int*)ref
atCursor: (unsigned*)cursor
@ -1288,15 +1302,6 @@ failure:
}
}
- (void*) relinquishAllocatedBytes
{
return [self relinquishAllocatedBytesFromZone: 0];
}
- (void*) relinquishAllocatedBytesFromZone: (NSZone*)aZone;
{
return 0; /* No data from NSZoneMalloc - return nul pointer */
}
@end
@ -1436,7 +1441,6 @@ failure:
- (id) initWithCoder: (NSCoder*)aCoder
{
unsigned l;
void *b;
NSZone *zone;
#if GS_WITH_GC
@ -1448,7 +1452,8 @@ failure:
[aCoder decodeValueOfObjCType: @encode(unsigned long) at: &l];
if (l)
{
b = NSZoneMalloc(zone, l);
void *b = NSZoneMalloc(zone, l);
if (b == 0)
{
NSLog(@"[NSDataMalloc -initWithCoder:] unable to get %lu bytes", l);
@ -1456,12 +1461,13 @@ failure:
return nil;
}
[aCoder decodeArrayOfObjCType: @encode(unsigned char) count: l at: b];
self = [self initWithBytesNoCopy: b length: l];
}
else
{
b = 0;
self = [self initWithBytesNoCopy: 0 length: 0];
}
return [self initWithBytesNoCopy: b length: l fromZone: zone];
return self;
}
- (id) initWithLength: (unsigned)length
@ -1895,16 +1901,8 @@ failure:
[super dealloc];
}
- (id) init
{
return [self initWithBytesNoCopy: 0
length: 0
fromZone: [self zone]];
}
- (id) initWithBytesNoCopy: (void*)aBuffer
length: (unsigned)bufferSize
fromZone: (NSZone*)aZone
{
bytes = aBuffer;
length = bufferSize;
@ -2309,136 +2307,22 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
- (void) dealloc
{
if (bytes)
if (bytes != 0)
{
NSZoneFree(zone, bytes);
NSZoneFree(NSZoneFromPointer(bytes), bytes);
bytes = 0;
}
[super dealloc];
}
- (id) initWithBytes: (const void*)aBuffer length: (unsigned)bufferSize
{
void* tmp = 0;
if (aBuffer != 0 && bufferSize > 0)
{
#if GS_WITH_GC
zone = GSAtomicMallocZone();
#else
zone = [self zone];
#endif
tmp = NSZoneMalloc(zone, bufferSize);
if (tmp == 0)
{
NSLog(@"[NSDataMalloc -initWithBytes:length:] unable to allocate %lu bytes", bufferSize);
RELEASE(self);
return nil;
}
else
{
memcpy(tmp, aBuffer, bufferSize);
}
}
self = [self initWithBytesNoCopy: tmp length: bufferSize fromZone: zone];
return self;
}
- (id) initWithBytesNoCopy: (void*)aBuffer
length: (unsigned)bufferSize
{
NSZone *z = NSZoneFromPointer(aBuffer);
return [self initWithBytesNoCopy: aBuffer length: bufferSize fromZone: z];
}
- (id) initWithBytesNoCopy: (void*)aBuffer
length: (unsigned)bufferSize
fromZone: (NSZone*)aZone
{
/*
* If the zone is zero, the data we have been given does not belong
* to use so we must create an NSDataStatic object to contain it.
*/
if (aZone == 0)
{
NSData *data;
data = [[NSDataStatic allocWithZone: NSDefaultMallocZone()]
initWithBytesNoCopy: aBuffer length: bufferSize];
RELEASE(self);
return data;
}
#if GS_WITH_GC
zone = GSAtomicMallocZone();
#else
zone = aZone;
#endif
bytes = aBuffer;
if (bytes)
{
length = bufferSize;
}
length = bufferSize;
return self;
}
- (id) initWithContentsOfFile: (NSString *)path
{
#if GS_WITH_GC
zone = GSAtomicMallocZone();
#else
zone = [self zone];
#endif
if (readContentsOfFile(path, &bytes, &length, zone) == NO)
{
RELEASE(self);
self = nil;
}
return self;
}
- (id) initWithContentsOfMappedFile: (NSString *)path
{
#if HAVE_MMAP
NSZone *z = [self zone];
RELEASE(self);
self = [NSDataMappedFile allocWithZone: z];
return [self initWithContentsOfMappedFile: path];
#else
return [self initWithContentsOfFile: path];
#endif
}
- (id) initWithData: (NSData*)anObject
{
if (anObject == nil)
{
return [self initWithBytesNoCopy: 0 length: 0 fromZone: [self zone]];
}
if ([anObject isKindOfClass: [NSData class]] == NO)
{
NSLog(@"-initWithData: passed a non-data object");
RELEASE(self);
return nil;
}
return [self initWithBytes: [anObject bytes] length: [anObject length]];
}
- (void*) relinquishAllocatedBytesFromZone: (NSZone*)aZone
{
if (aZone == zone || aZone == 0)
{
void *buf = bytes;
bytes = 0;
length = 0;
return buf;
}
return 0;
}
@end
#if HAVE_MMAP
@ -2450,7 +2334,7 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
- (void) dealloc
{
if (bytes)
if (bytes != 0)
{
munmap(bytes, length);
bytes = 0;
@ -2506,10 +2390,6 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
return self;
}
- (void*) relinquishAllocatedBytesFromZone: (NSZone*)aZone
{
return 0;
}
@end
#endif /* HAVE_MMAP */
@ -2522,7 +2402,7 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
- (void) dealloc
{
if (bytes)
if (bytes != 0)
{
struct shmid_ds buf;
@ -2603,11 +2483,6 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
return self;
}
- (void*) relinquishAllocatedBytesFromZone: (NSZone*)aZone
{
return 0;
}
- (int) shmID
{
return shmid;
@ -2675,24 +2550,10 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
- (id) initWithBytesNoCopy: (void*)aBuffer
length: (unsigned)bufferSize
{
NSZone *aZone = NSZoneFromPointer(aBuffer);
return [self initWithBytesNoCopy: aBuffer length: bufferSize fromZone: aZone];
}
- (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)
if (self != nil)
{
[self setLength: bufferSize];
}
@ -2704,7 +2565,7 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
#if GS_WITH_GC
zone = GSAtomicMallocZone();
#else
zone = aZone;
zone = NSZoneFromPointer(aBuffer);
#endif
bytes = aBuffer;
length = bufferSize;
@ -2726,7 +2587,7 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
#if GS_WITH_GC
zone = GSAtomicMallocZone();
#else
zone = [self zone];
zone = fastZone(self);
#endif
if (size)
{
@ -2780,21 +2641,6 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
return [self initWithContentsOfFile: path];
}
- (id) initWithData: (NSData*)anObject
{
if (anObject == nil)
{
return [self initWithCapacity: 0];
}
if ([anObject isKindOfClass: [NSData class]] == NO)
{
NSLog(@"-initWithData: passed a non-data object");
RELEASE(self);
return nil;
}
return [self initWithBytes: [anObject bytes] length: [anObject length]];
}
- (void) appendBytes: (const void*)aBuffer
length: (unsigned)bufferSize
{
@ -2838,18 +2684,6 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
return bytes;
}
- (void*) relinquishAllocatedBytesFromZone: (NSZone*)aZone
{
void *ptr = [super relinquishAllocatedBytesFromZone: aZone];
if (ptr != 0)
{
capacity = 0;
growth = 1;
}
return ptr;
}
- (void) replaceBytesInRange: (NSRange)aRange
withBytes: (const void*)moreBytes
{
@ -3331,11 +3165,6 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
return self;
}
- (void*) relinquishAllocatedBytesFromZone: (NSZone*)aZone
{
return 0;
}
- (int) shmID
{
return shmid;

View file

@ -69,10 +69,10 @@
#define GSPLUNI 0
#include "propList.h"
static SEL csInitSel = @selector(initWithCStringNoCopy: length: fromZone:);
static SEL msInitSel = @selector(initWithCapacity:);
static IMP csInitImp; /* designated initialiser for cString */
static IMP msInitImp; /* designated initialiser for mutable */
static SEL csInitSel = @selector(initWithCStringNoCopy:length:freeWhenDone:);
static SEL msInitSel = @selector(initWithCapacity:);
static IMP csInitImp; /* designated initialiser for cString */
static IMP msInitImp; /* designated initialiser for mutable */
@interface NSGMutableCString (GNUDescription)
- (unsigned char*) _extendBy: (unsigned)len;
@ -118,54 +118,26 @@ static IMP msInitImp; /* designated initialiser for mutable */
return _hash;
}
/*
* This is the GNUstep designated initializer for this class.
* NB. this does NOT change the '_hash' instance variable, so the copy
* methods can safely allocate a new object, copy the _hash into place,
* and then invoke this method to complete the copy operation.
*/
- (id) initWithCStringNoCopy: (char*)byteString
length: (unsigned int)length
fromZone: (NSZone*)zone
{
_count = length;
_contents_chars = (unsigned char*)byteString;
#if GS_WITH_GC
_zone = byteString ? GSAtomicMallocZone() : 0;
#else
_zone = byteString ? zone : 0;
#endif
return self;
}
/* This is the OpenStep designated initializer for this class. */
- (id) initWithCStringNoCopy: (char*)byteString
length: (unsigned int)length
freeWhenDone: (BOOL)flag
{
NSZone *z;
if (flag && byteString)
_count = length;
_contents_chars = (unsigned char*)byteString;
if (flag == NO)
{
z = NSZoneFromPointer(byteString);
_zone = 0;
}
else
{
z = 0;
#if GS_WITH_GC
_zone = byteString ? GSAtomicMallocZone() : 0;
#else
_zone = byteString ? NSZoneFromPointer(byteString) : 0;
#endif
}
return (*csInitImp)(self, csInitSel, byteString, length, z);
}
- (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];
RELEASE(self);
return a;
return self;
}
- (id) initWithCharactersNoCopy: (unichar*)chars
@ -182,21 +154,7 @@ static IMP msInitImp; /* designated initialiser for mutable */
- (id) init
{
return [self initWithCStringNoCopy: 0 length: 0 fromZone: 0];
}
- (void) _collectionReleaseContents
{
return;
}
- (void) _collectionDealloc
{
if (_zone)
{
NSZoneFree(_zone, (void*)_contents_chars);
_zone = 0;
}
return [self initWithCStringNoCopy: 0 length: 0 freeWhenDone: NO];
}
- (void) encodeWithCoder: (NSCoder*)aCoder
@ -236,20 +194,19 @@ static IMP msInitImp; /* designated initialiser for mutable */
if (NSShouldRetainWithZone(self, z) == NO)
{
NSGCString *obj;
unsigned char *tmp;
obj = (NSGCString*)NSAllocateObject(_fastCls._NSGCString, 0, z);
if (_count)
if (_count > 0)
{
tmp = NSZoneMalloc(z, _count);
unsigned char *tmp = NSZoneMalloc(z, _count);
memcpy(tmp, _contents_chars, _count);
obj = (*csInitImp)(obj, csInitSel, tmp, _count, YES);
}
else
{
tmp = 0;
z = 0;
obj = (*csInitImp)(obj, csInitSel, 0, 0, NO);
}
obj = (*csInitImp)(obj, csInitSel, tmp, _count, z);
if (_hash && obj)
{
obj->_hash = _hash;
@ -267,20 +224,19 @@ static IMP msInitImp; /* designated initialiser for mutable */
if (NSShouldRetainWithZone(self, z) == NO)
{
NSGCString *obj;
unsigned char *tmp;
obj = (NSGCString*)NSAllocateObject(_fastCls._NSGCString, 0, z);
if (_count)
{
tmp = NSZoneMalloc(z, _count);
unsigned char *tmp = NSZoneMalloc(z, _count);
memcpy(tmp, _contents_chars, _count);
obj = (*csInitImp)(obj, csInitSel, tmp, _count, YES);
}
else
{
tmp = 0;
z = 0;
obj = (*csInitImp)(obj, csInitSel, 0, 0, NO);
}
obj = (*csInitImp)(obj, csInitSel, tmp, _count, z);
if (_hash && obj)
{
obj->_hash = _hash;
@ -619,21 +575,20 @@ static IMP msInitImp; /* designated initialiser for mutable */
- (id) initWithString: (NSString*)string
{
unsigned length = [string cStringLength];
NSZone *z;
unsigned char *buf;
if (length > 0)
{
z = fastZone(self);
buf = NSZoneMalloc(z, length+1); // getCString appends a nul.
unsigned char *buf = NSZoneMalloc(fastZone(self), length+1);
// getCString appends a nul.
[string getCString: buf];
self = [self initWithCStringNoCopy: buf length: length freeWhenDone: YES];
}
else
{
z = 0;
buf = 0;
return [self initWithCStringNoCopy: 0 length: 0 freeWhenDone: NO];
}
return [self initWithCStringNoCopy: buf length: length fromZone: z];
return self;
}
- (void) descriptionWithLocale: (NSDictionary*)aLocale
@ -745,7 +700,7 @@ static IMP msInitImp; /* designated initialiser for mutable */
NSString *result;
result = [[_fastCls._NSGCString allocWithZone: z]
initWithCStringNoCopy: buf length: length fromZone: z];
initWithCStringNoCopy: buf length: length freeWhenDone: YES];
[output appendString: result];
RELEASE(result);
}
@ -1029,35 +984,30 @@ stringDecrementCountAndFillHoleAt(NSGMutableCStringStruct *self,
- (id) initWithCStringNoCopy: (char*)byteString
length: (unsigned int)length
fromZone: (NSZone*)zone
freeWhenDone: (BOOL)flag
{
self = (*msInitImp)(self, msInitSel, 0);
if (self)
if (self != nil)
{
_count = length;
_capacity = length;
_contents_chars = (unsigned char*)byteString;
if (flag == YES)
{
#if GS_WITH_GC
_zone = byteString ? GSAtomicMallocZone() : 0;
_zone = (byteString != 0) ? GSAtomicMallocZone() : 0;
#else
_zone = byteString ? zone : 0;
_zone = (byteString != 0) ? NSZoneFromPointer(byteString) : 0;
#endif
}
else
{
_zone = 0;
}
}
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];
RELEASE(self);
return a;
}
- (id) initWithCharactersNoCopy: (unichar*)chars
length: (unsigned int)length
freeWhenDone: (BOOL)flag
@ -1072,22 +1022,21 @@ stringDecrementCountAndFillHoleAt(NSGMutableCStringStruct *self,
- (id) copy
{
unsigned char *tmp;
NSGCString *obj;
NSZone *z = NSDefaultMallocZone();
obj = (NSGCString*)NSAllocateObject(_fastCls._NSGCString, 0, z);
if (_count)
{
tmp = NSZoneMalloc(z, _count);
unsigned char *tmp = NSZoneMalloc(z, _count);
memcpy(tmp, _contents_chars, _count);
obj = (*csInitImp)(obj, csInitSel, tmp, _count, YES);
}
else
{
tmp = 0;
z = 0;
obj = (*csInitImp)(obj, csInitSel, 0, 0, NO);
}
obj = (*csInitImp)(obj, csInitSel, tmp, _count, z);
if (_hash && obj)
{
NSGMutableCString *tmp = (NSGMutableCString*)obj; // Same ivar layout
@ -1099,21 +1048,20 @@ stringDecrementCountAndFillHoleAt(NSGMutableCStringStruct *self,
- (id) copyWithZone: (NSZone*)z
{
unsigned char *tmp;
NSGCString *obj;
obj = (NSGCString*)NSAllocateObject(_fastCls._NSGCString, 0, z);
if (_count)
{
tmp = NSZoneMalloc(z, _count);
unsigned char *tmp = NSZoneMalloc(z, _count);
memcpy(tmp, _contents_chars, _count);
obj = (*csInitImp)(obj, csInitSel, tmp, _count, YES);
}
else
{
tmp = 0;
z = 0;
obj = (*csInitImp)(obj, csInitSel, 0, 0, NO);
}
obj = (*csInitImp)(obj, csInitSel, tmp, _count, z);
if (_hash && obj)
{
NSGMutableCString *tmp = (NSGMutableCString*)obj; // Same ivar layout
@ -1279,7 +1227,7 @@ stringDecrementCountAndFillHoleAt(NSGMutableCStringStruct *self,
- (id) init
{
return [self initWithCStringNoCopy: 0 length: 0 fromZone: 0];
return [self initWithCStringNoCopy: 0 length: 0 freeWhenDone: NO];
}
- (id) initWithCoder: (NSCoder*)aCoder
@ -1330,7 +1278,7 @@ stringDecrementCountAndFillHoleAt(NSGMutableCStringStruct *self,
- (id) initWithCStringNoCopy: (char*)byteString
length: (unsigned int)length
fromZone: (NSZone*)zone
freeWhenDone: (BOOL)flag
{
[NSException raise: NSGenericException
format: @"Attempt to init an NXConstantString"];

View file

@ -174,32 +174,13 @@
// Initializing Newly Allocated Strings
/* 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;
#if GS_WITH_GC
_zone = chars ? GSAtomicMallocZone() : 0;
#else
_zone = chars ? zone : 0;
#endif
}
return self;
}
/* This is the OpenStep designated initializer for this class. */
- (id) initWithCharactersNoCopy: (unichar*)chars
length: (unsigned int)length
freeWhenDone: (BOOL)flag
{
self = [super init];
if (self)
if (self != nil)
{
_count = length;
_contents_chars = chars;
@ -222,30 +203,21 @@
- (id) initWithCharacters: (const unichar*)chars
length: (unsigned int)length
{
NSZone *z = fastZone(self);
unichar *s;
if (length)
if (length > 0)
{
s = NSZoneMalloc(z, length*sizeof(unichar));
if (chars)
unichar *s = NSZoneMalloc(fastZone(self), length*sizeof(unichar));
if (chars != 0)
memcpy(s, chars, sizeof(unichar)*length);
self = [self initWithCharactersNoCopy: s
length: length
freeWhenDone: YES];
}
else
{
s = 0;
self = [self initWithCharactersNoCopy: 0 length: 0 freeWhenDone: NO];
}
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;
return self;
}
- (id) initWithCStringNoCopy: (char*)byteString
@ -254,13 +226,13 @@
{
id a = [[NSGCString allocWithZone: fastZone(self)]
initWithCStringNoCopy: byteString length: length freeWhenDone: flag];
[self release];
RELEASE(self);
return a;
}
- (id) init
{
return [self initWithCharactersNoCopy:0 length:0 fromZone: fastZone(self)];
return [self initWithCharactersNoCopy:0 length:0 freeWhenDone: 0];
}
// Getting a String's Length
@ -412,23 +384,6 @@
}
// ******* Stuff from NSGCString *********
// Do we need this ???
- (void) _collectionReleaseContents
{
return;
}
- (void) _collectionDealloc
{
if (_zone)
{
NSZoneFree(_zone, _contents_chars);
_zone = 0;
}
}
- (id) propertyList
{
id result;
@ -704,32 +659,12 @@ stringDecrementCountAndFillHoleAt(NSGMutableStringStruct *self,
return [self initWithCapacity: 0];
}
/* 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;
#if GS_WITH_GC
_zone = _zone ? GSAtomicMallocZone() : 0;
#else
_zone = zone;
#endif
}
return self;
}
- (id) initWithCharactersNoCopy: (unichar*)chars
length: (unsigned int)length
freeWhenDone: (BOOL)flag
{
self = [super init];
if (self)
if (self != nil)
{
_count = length;
_capacity = length;
@ -771,23 +706,13 @@ stringDecrementCountAndFillHoleAt(NSGMutableStringStruct *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];
RELEASE(self);
return a;
}

View file

@ -111,8 +111,8 @@ void _fastBuildCache()
static objc_mutex_t retain_counts_gate = NULL;
#if GS_WITH_GC == 0
#define REFCNT_LOCAL 1
#define CACHE_ZONE 1
#define REFCNT_LOCAL 0
#define CACHE_ZONE 0
#endif
#if defined(REFCNT_LOCAL) || defined(CACHE_ZONE)

View file

@ -371,9 +371,9 @@ typedef struct {
static SEL debSel = @selector(deserializeBytes:length:atCursor:);
static SEL deiSel = @selector(deserializeIntAtCursor:);
static SEL csInitSel = @selector(initWithCStringNoCopy:length:fromZone:);
static SEL usInitSel = @selector(initWithCharactersNoCopy:length:fromZone:);
static SEL dInitSel = @selector(initWithBytesNoCopy:length:fromZone:);
static SEL csInitSel = @selector(initWithCStringNoCopy:length:freeWhenDone:);
static SEL usInitSel = @selector(initWithCharactersNoCopy:length:freeWhenDone:);
static SEL dInitSel = @selector(initWithBytesNoCopy:length:);
static SEL iaInitSel = @selector(initWithObjects:count:);
static SEL maInitSel = @selector(initWithObjects:count:);
static SEL idInitSel = @selector(initWithObjects:forKeys:count:);
@ -429,7 +429,7 @@ deserializeFromInfo(_NSDeserializerInfo* info)
(*info->debImp)(info->data, debSel, b, size, info->cursor);
s = (NSGCString*)NSAllocateObject(CSCls, 0, NSDefaultMallocZone());
s = (*csInitImp)(s, csInitSel, b, size-1, NSDefaultMallocZone());
s = (*csInitImp)(s, csInitSel, b, size-1, YES);
/*
* If we are supposed to be doing uniquing of strings, handle it.
@ -453,7 +453,7 @@ deserializeFromInfo(_NSDeserializerInfo* info)
(*info->debImp)(info->data, debSel, b, size*2, info->cursor);
s = (NSGString*)NSAllocateObject(USCls, 0, NSDefaultMallocZone());
s = (*usInitImp)(s, usInitSel, b, size, NSDefaultMallocZone());
s = (*usInitImp)(s, usInitSel, b, size, YES);
/*
* If we are supposed to be doing uniquing of strings, handle it.
@ -570,11 +570,19 @@ deserializeFromInfo(_NSDeserializerInfo* info)
case ST_DATA:
{
NSData *d;
void *b = NSZoneMalloc(NSDefaultMallocZone(), size);
(*info->debImp)(info->data, debSel, b, size, info->cursor);
d = (NSData*)NSAllocateObject(DCls, 0, NSDefaultMallocZone());
d = (*dInitImp)(d, dInitSel, b, size, NSDefaultMallocZone());
if (size > 0)
{
void *b = NSZoneMalloc(NSDefaultMallocZone(), size);
(*info->debImp)(info->data, debSel, b, size, info->cursor);
d = (*dInitImp)(d, dInitSel, b, size);
}
else
{
d = (*dInitImp)(d, dInitSel, 0, 0);
}
return d;
}

View file

@ -353,69 +353,41 @@ handle_printf_atsign (FILE *stream,
// Initializing Newly Allocated Strings
/* This is the designated initializer for Unicode Strings. */
- (id) initWithCharactersNoCopy: (unichar*)chars
length: (unsigned)length
fromZone: (NSZone*)zone
{
[self subclassResponsibility: _cmd];
return self;
}
- (id) initWithCharactersNoCopy: (unichar*)chars
length: (unsigned)length
freeWhenDone: (BOOL)flag
{
if (flag)
return [self initWithCharactersNoCopy: chars
length: length
fromZone: NSZoneFromPointer(chars)];
else
return [self initWithCharactersNoCopy: chars
length: length
fromZone: 0];
[self subclassResponsibility: _cmd];
return self;
}
- (id) initWithCharacters: (const unichar*)chars
length: (unsigned)length
{
NSZone *z;
unichar *s;
if (length > 0)
{
z = [self zone];
s = NSZoneMalloc(z, sizeof(unichar)*length);
if (chars)
memcpy(s, chars, sizeof(unichar)*length);
unichar *s = NSZoneMalloc(fastZone(self), sizeof(unichar)*length);
if (chars != 0)
{
memcpy(s, chars, sizeof(unichar)*length);
}
self = [self initWithCharactersNoCopy: s
length: length
freeWhenDone: YES];
}
else
{
s = 0;
z = 0;
self = [self initWithCharactersNoCopy: 0 length: 0 freeWhenDone: NO];
}
return [self initWithCharactersNoCopy: s length: length fromZone: z];
}
- (id) initWithCStringNoCopy: (char*)byteString
length: (unsigned)length
freeWhenDone: (BOOL)flag
{
if (flag)
return [self initWithCStringNoCopy: byteString
length: length
fromZone: length?NSZoneFromPointer(byteString):0];
else
return [self initWithCStringNoCopy: byteString
length: length
fromZone: 0];
return self;
}
/* This is the designated initializer for CStrings. */
- (id) initWithCStringNoCopy: (char*)byteString
length: (unsigned)length
fromZone: (NSZone*)zone
freeWhenDone: (BOOL)flag
{
[self subclassResponsibility: _cmd];
return self;
@ -423,75 +395,72 @@ handle_printf_atsign (FILE *stream,
- (id) initWithCString: (const char*)byteString length: (unsigned)length
{
NSZone *z;
char *s;
if (length > 0)
{
z = [self zone];
s = NSZoneMalloc(z, length);
if (byteString)
char *s = NSZoneMalloc(fastZone(self), length);
if (byteString != 0)
{
memcpy(s, byteString, length);
}
self = [self initWithCStringNoCopy: s length: length freeWhenDone: YES];
}
else
{
s = 0;
z = 0;
self = [self initWithCStringNoCopy: 0 length: 0 freeWhenDone: NO];
}
return [self initWithCStringNoCopy: s length: length fromZone: z];
return self;
}
- (id) initWithCString: (const char*)byteString
{
return [self initWithCString: byteString
length: (byteString ? strlen(byteString) : 0)];
length: (byteString ? strlen(byteString) : 0)];
}
- (id) initWithString: (NSString*)string
{
unsigned length = [string length];
NSZone *z;
unichar *s;
if (length > 0)
{
z = [self zone];
s = NSZoneMalloc(z, sizeof(unichar)*length);
unichar *s = NSZoneMalloc(fastZone(self), sizeof(unichar)*length);
[string getCharacters: s];
self = [self initWithCharactersNoCopy: s
length: length
freeWhenDone: YES];
}
else
{
s = 0;
z = 0;
self = [self initWithCharactersNoCopy: 0
length: 0
freeWhenDone: NO];
}
return [self initWithCharactersNoCopy: s
length: length
fromZone: z];
return self;
}
- (id) initWithUTF8String:(const char *)bytes
{
unsigned length = strlen(bytes);
NSZone *z;
unichar *s;
if (length > 0)
{
z = [self zone];
s = NSZoneMalloc(z, sizeof(unichar)*length);
unichar *s = NSZoneMalloc(fastZone(self), sizeof(unichar)*length);
length = encode_strtoustr(s, bytes, length+1, NSUTF8StringEncoding);
self = [self initWithCharactersNoCopy: s
length: length
freeWhenDone: YES];
}
else
{
s = 0;
z = 0;
self = [self initWithCharactersNoCopy: 0
length: 0
freeWhenDone: NO];
}
return [self initWithCharactersNoCopy: s
length: length
fromZone: z];
return self;
}
- (id) initWithFormat: (NSString*)format,...
@ -812,54 +781,60 @@ handle_printf_atsign (FILE *stream,
- (id) initWithData: (NSData*)data
encoding: (NSStringEncoding)encoding
{
if ((encoding==[NSString defaultCStringEncoding])
|| (encoding==NSASCIIStringEncoding))
if ((encoding == [NSString defaultCStringEncoding])
|| (encoding == NSASCIIStringEncoding))
{
unsigned len=[data length];
NSZone *z;
char *s;
unsigned len = [data length];
if (len > 0)
{
z = fastZone(self);
s = NSZoneMalloc(z, len);
char *s = NSZoneMalloc(fastZone(self), len);
[data getBytes: s];
self = [self initWithCStringNoCopy: s length: len freeWhenDone: YES];
}
else
{
s = 0;
z = 0;
self = [self initWithCStringNoCopy: 0 length: 0 freeWhenDone: NO];
}
return [self initWithCStringNoCopy: s length: len fromZone: z];
return self;
}
else
{
unsigned len = [data length];
NSZone *z;
unichar *u;
unsigned count;
const unsigned char *b;
z = fastZone(self);
if (len < 2)
return [self initWithCStringNoCopy: 0 length: 0 fromZone: z];
return [self initWithCStringNoCopy: 0 length: 0 freeWhenDone: NO];
b=[data bytes];
u = NSZoneMalloc(z, sizeof(unichar)*(len+1));
if (encoding==NSUnicodeStringEncoding)
b = [data bytes];
u = NSZoneMalloc(fastZone(self), sizeof(unichar)*(len+1));
if (encoding == NSUnicodeStringEncoding)
{
if ((b[0]==0xFE)&(b[1]==0xFF))
for(count=2;count<(len-1);count+=2)
u[count/2 - 1]=256*b[count]+b[count+1];
if ((b[0]==0xFE) & (b[1]==0xFF))
{
for (count = 2; count < (len - 1); count += 2)
{
u[count/2 - 1] = 256*b[count] + b[count + 1];
}
}
else
for(count=2;count<(len-1);count+=2)
u[count/2 -1]=256*b[count+1]+b[count];
count = count/2 -1;
{
for (count = 2; count < (len - 1); count += 2)
{
u[count/2 - 1] = 256*b[count + 1] + b[count];
}
}
count = count/2 - 1;
}
else
count = encode_strtoustr(u,b,len,encoding);
{
count = encode_strtoustr(u, b, len, encoding);
}
return [self initWithCharactersNoCopy: u length: count fromZone: z];
self = [self initWithCharactersNoCopy: u length: count freeWhenDone: YES];
}
return self;
}
@ -875,7 +850,9 @@ handle_printf_atsign (FILE *stream,
if ([d length] < 2)
return @"";
test = [d bytes];
if (test && (((test[0]==0xFF) && (test[1]==0xFE)) || ((test[1]==0xFF) && (test[0]==0xFE))))
if (test != 0 &&
(((test[0]==0xFF) && (test[1]==0xFE))
|| ((test[1]==0xFF) && (test[0]==0xFE))))
enc = NSUnicodeStringEncoding;
else
enc = [NSString defaultCStringEncoding];
@ -893,7 +870,9 @@ handle_printf_atsign (FILE *stream,
if ([d length] < 2)
return @"";
test = [d bytes];
if (test && (((test[0]==0xFF) && (test[1]==0xFE)) || ((test[1]==0xFF) && (test[0]==0xFE))))
if (test != 0
&& (((test[0]==0xFF) && (test[1]==0xFE))
|| ((test[1]==0xFF) && (test[0]==0xFE))))
enc = NSUnicodeStringEncoding;
else
enc = [NSString defaultCStringEncoding];
@ -961,16 +940,16 @@ handle_printf_atsign (FILE *stream,
- (NSString*) stringByAppendingString: (NSString*)aString
{
NSZone *z = fastZone(self);
unsigned len = [self length];
unsigned otherLength = [aString length];
unichar *s = NSZoneMalloc(z, (len+otherLength)*sizeof(unichar));
NSString *tmp;
unsigned len = [self length];
unsigned otherLength = [aString length];
NSZone *z = fastZone(self);
unichar *s = NSZoneMalloc(z, (len+otherLength)*sizeof(unichar));
NSString *tmp;
[self getCharacters: s];
[aString getCharacters: s+len];
[aString getCharacters: s + len];
tmp = [[NSString_concrete_class allocWithZone: z] initWithCharactersNoCopy: s
length: len+otherLength fromZone: z];
length: len + otherLength freeWhenDone: YES];
return AUTORELEASE(tmp);
}
@ -1023,7 +1002,6 @@ handle_printf_atsign (FILE *stream,
- (NSString*) substringWithRange: (NSRange)aRange
{
NSZone *z;
unichar *buf;
id ret;
unsigned len = [self length];
@ -1032,11 +1010,10 @@ handle_printf_atsign (FILE *stream,
if (aRange.length == 0)
return @"";
z = fastZone(self);
buf = NSZoneMalloc(z, sizeof(unichar)*aRange.length);
buf = NSZoneMalloc(fastZone(self), sizeof(unichar)*aRange.length);
[self getCharacters: buf range: aRange];
ret = [[NSString_concrete_class allocWithZone: NSDefaultMallocZone()]
initWithCharactersNoCopy: buf length: aRange.length fromZone: z];
initWithCharactersNoCopy: buf length: aRange.length freeWhenDone: YES];
return AUTORELEASE(ret);
}
@ -1542,7 +1519,6 @@ handle_printf_atsign (FILE *stream,
// but this will work in most cases
- (NSString*) capitalizedString
{
NSZone *z;
unichar *s;
unsigned count = 0;
BOOL found = YES;
@ -1553,8 +1529,7 @@ handle_printf_atsign (FILE *stream,
if (whitespce == nil)
setupWhitespce();
z = fastZone(self);
s = NSZoneMalloc(z, sizeof(unichar)*len);
s = NSZoneMalloc(fastZone(self), sizeof(unichar)*len);
[self getCharacters: s];
while (count < len)
{
@ -1588,12 +1563,11 @@ handle_printf_atsign (FILE *stream,
found = NO;
}
return AUTORELEASE([[NSString allocWithZone: NSDefaultMallocZone()]
initWithCharactersNoCopy: s length: len fromZone: z]);
initWithCharactersNoCopy: s length: len freeWhenDone: YES]);
}
- (NSString*) lowercaseString
{
NSZone *z;
unichar *s;
unsigned count;
unsigned len = [self length];
@ -1603,8 +1577,7 @@ handle_printf_atsign (FILE *stream,
{
return self;
}
z = fastZone(self);
s = NSZoneMalloc(z, sizeof(unichar)*len);
s = NSZoneMalloc(fastZone(self), sizeof(unichar)*len);
caiImp = (unichar (*)())[self methodForSelector: caiSel];
for (count = 0; count < len; count++)
{
@ -1612,12 +1585,11 @@ handle_printf_atsign (FILE *stream,
}
return AUTORELEASE([[NSString_concrete_class
allocWithZone: NSDefaultMallocZone()]
initWithCharactersNoCopy: s length: len fromZone: z]);
initWithCharactersNoCopy: s length: len freeWhenDone: YES]);
}
- (NSString*) uppercaseString;
{
NSZone *z;
unichar *s;
unsigned count;
unsigned len = [self length];
@ -1627,8 +1599,7 @@ handle_printf_atsign (FILE *stream,
{
return self;
}
z = fastZone(self);
s = NSZoneMalloc(z, sizeof(unichar)*len);
s = NSZoneMalloc(fastZone(self), sizeof(unichar)*len);
caiImp = (unichar (*)())[self methodForSelector: caiSel];
for (count = 0; count < len; count++)
{
@ -1636,7 +1607,7 @@ handle_printf_atsign (FILE *stream,
}
return AUTORELEASE([[NSString_concrete_class
allocWithZone: NSDefaultMallocZone()]
initWithCharactersNoCopy: s length: len fromZone: z]);
initWithCharactersNoCopy: s length: len freeWhenDone: YES]);
}
// Storing the String

View file

@ -975,8 +975,7 @@ mapClassName(NSUnarchiverObjectInfo *info)
count: l
at: b];
d = [[NSData allocWithZone: zone] initWithBytesNoCopy: b
length: l
fromZone: z];
length: l];
IF_NO_GC(AUTORELEASE(d));
return d;
}

View file

@ -1,11 +1,6 @@
#include <Foundation/Foundation.h>
#include <stdio.h>
#define _(X) \
[[NSBundle mainBundle] localizedStringForKey: @#X value: nil table: nil]
#define $(X) \
[[NSBundle mainBundle] localizedStringForKey: X value: nil table: nil]
#if 0
int main ()
@ -19,18 +14,12 @@ int main (int argc, char **argv)
{
NSString *string;
id pool = [NSAutoreleasePool new];
NSURL *url = [NSURL fileURLWithPath: @"/tmp/a"];
NSData *data = [url resourceDataUsingCache: YES];
NSLog(@"%@", data);
url = [NSURL fileURLWithPath: @"/tmp/z"];
[url setResourceData: data];
NSLog(@"%@", _(Testing));
NSLog(@"%@", $(@"Testing"));
string = [NSString stringWithCString:argv[1]];
NSProcessInfo *info = [NSProcessInfo processInfo];
NSUserDefaults *defaults;
[info setProcessName: @"TestProcess"];
defaults = [NSUserDefaults standardUserDefaults];
NSLog(@"%@", [defaults dictionaryRepresentation]);
return 0;
}
#endif