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 Frith-MacDonald 1998-10-15 05:03:16 +00:00
parent e3def6fa5b
commit b56f5ae12e
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>
* src/include/NSNotification.h: Addd placeholder in

View file

@ -94,6 +94,16 @@
+ (id) dataWithShmID: (int)anID length: (unsigned) length;
+ (id) dataWithSharedBytes: (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
* For an NSData object with a malloced buffer, returns that buffer and

View file

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

View file

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

View file

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

View file

@ -264,8 +264,13 @@ enum {
#endif
#ifndef NO_GNUSTEP
- (const char *) cStringNoCopy;
- (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 */
@end

View file

@ -52,10 +52,12 @@ struct _NSZone
void (*free)(struct _NSZone *zone, void *ptr);
void (*recycle)(struct _NSZone *zone);
BOOL (*check)(struct _NSZone *zone);
BOOL (*lookup)(struct _NSZone *zone, void *ptr);
struct NSZoneStats (*stats)(struct _NSZone *zone);
size_t gran; // Zone granularity
NSString *name; // Name of zone (default is 'nil')
NSZone *next;
};
@ -97,16 +99,6 @@ extern inline NSString* NSZoneName (NSZone *zone)
{ if (!zone) zone = NSDefaultMallocZone();
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
{ if (!zone) zone = NSDefaultMallocZone();
return (zone->check)(zone); }

View file

@ -158,3 +158,15 @@ fastInstanceIsKindOfClass(NSObject *obj, Class 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__ */
#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 ROUND(V, A) \

View file

@ -1,5 +1,5 @@
/* 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>
Date: October 1996
@ -982,6 +982,23 @@ static id long_day[7] = {@"Sunday",
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
{
return [self descriptionWithCalendarFormat: calendar_format
@ -996,6 +1013,7 @@ static id long_day[7] = {@"Sunday",
- (void)setCalendarFormat:(NSString *)format
{
[calendar_format release];
calendar_format = [format copyWithZone: [self zone]];
}

View file

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

View file

@ -94,7 +94,7 @@
static BOOL
readContentsOfFile(NSString* path, void** buf, unsigned* len)
readContentsOfFile(NSString* path, void** buf, unsigned* len, NSZone* zone)
{
char thePath[BUFSIZ*2];
FILE *theFile = 0;
@ -133,7 +133,7 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
goto failure;
}
tmp = objc_malloc(fileLength);
tmp = NSZoneMalloc(zone, fileLength);
if (tmp == 0)
{
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. */
failure:
if (tmp)
objc_free(tmp);
NSZoneFree(zone, tmp);
if (theFile)
fclose(theFile);
return NO;
@ -176,6 +176,7 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
{
unsigned int length;
void *bytes;
NSZone *zone;
}
@end
@ -184,6 +185,7 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
unsigned int capacity;
unsigned int length;
void *bytes;
NSZone *zone;
}
@end
@ -261,18 +263,29 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
length: [data length]] autorelease];
}
- (id) initWithBytes: (const void*)bytes
length: (unsigned int)length
- (id) init
{
return [self initWithBytesNoCopy: 0 length: 0];
}
- (id) initWithBytes: (const void*)aBuffer
length: (unsigned int)bufferSize
{
[self subclassResponsibility:_cmd];
return nil;
}
- (id) initWithBytesNoCopy: (void*)bytes
length: (unsigned int)length
- (id) initWithBytesNoCopy: (void*)aBuffer
length: (unsigned int)bufferSize
{
[self subclassResponsibility:_cmd];
return nil;
if (aBuffer)
return [self initWithBytesNoCopy: aBuffer
length: bufferSize
fromZone: NSZoneFromPointer(aBuffer)];
else
return [self initWithBytesNoCopy: aBuffer
length: bufferSize
fromZone: [self zone]];
}
- (id) initWithContentsOfFile: (NSString *)path
@ -308,11 +321,12 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
char *dest;
int length = [self length];
int i,j;
NSZone *z = [self zone];
#define num2char(num) ((num) < 0xa ? ((num)+'0') : ((num)+0x57))
/* 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)
[NSException raise:NSMallocException
format:@"No memory for description of NSData object"];
@ -327,8 +341,8 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
}
dest[j++] = '>';
dest[j] = '\0';
str = [NSString stringWithCString: dest];
objc_free(dest);
str = [[[NSString alloc] initWithCStringNoCopy: dest length: j fromZone: z]
autorelease];
return str;
}
@ -384,7 +398,7 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
format: @"Range: (%u, %u) Size: %d",
aRange.location, aRange.length, l];
buffer = objc_malloc(aRange.length);
buffer = NSZoneMalloc([self zone], aRange.length);
if (buffer == 0)
[NSException raise:NSMallocException
format:@"No memory for subdata of NSData object"];
@ -554,7 +568,7 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
else {
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];
}
@ -605,7 +619,7 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
unsigned len = objc_sizeof_type(++type);
id adr;
*(char**)data = (char*)objc_malloc(len);
*(char**)data = (char*)NSZoneMalloc([self zone], len);
adr = [NSData dataWithBytesNoCopy: *(void**)data length: len];
[self deserializeDataAt:*(char**)data
@ -780,9 +794,16 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
autorelease];
}
- (id) initWithBytesNoCopy: (void*)bytes
length: (unsigned)length
fromZone: (NSZone*)zone
{
[self subclassResponsibility:_cmd];
}
- (void*) relinquishAllocatedBytes
{
return 0; /* No data from objc_malloc - return nul pointer */
return 0; /* No data from NSZoneMalloc - return nul pointer */
}
@end
@ -1171,7 +1192,7 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
{
if (bytes)
{
objc_free(bytes);
NSZoneFree(zone, bytes);
bytes = 0;
length = 0;
}
@ -1180,13 +1201,13 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
- (void) encodeWithCoder: (NSCoder*)aCoder
{
[aCoder encodeValueOfObjCType:"I" at: &length];
[aCoder encodeArrayOfObjCType:"C" count:length at: bytes];
[aCoder encodeValueOfObjCType: @encode(unsigned long) at: &length];
[aCoder encodeArrayOfObjCType: @encode(unsigned char) count:length at: bytes];
}
- (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
@ -1195,7 +1216,8 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
if (aBuffer != 0 && bufferSize > 0)
{
tmp = objc_malloc(bufferSize);
zone = [self zone];
tmp = NSZoneMalloc(zone, bufferSize);
if (tmp == 0)
{
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);
}
}
self = [self initWithBytesNoCopy:tmp length:bufferSize];
self = [self initWithBytesNoCopy:tmp length:bufferSize fromZone: zone];
return self;
}
- (id) initWithBytesNoCopy: (void*)aBuffer
length: (unsigned int)bufferSize
{
self = [super init];
if (self)
NSZone *z = NSZoneFromPointer(aBuffer);
return [self initWithBytesNoCopy: aBuffer length: bufferSize fromZone: z];
}
- (id) initWithBytesNoCopy: (void*)aBuffer
length: (unsigned)bufferSize
fromZone: (NSZone*)aZone
{
if (aZone == 0)
{
bytes = aBuffer;
if (bytes)
length = bufferSize;
NSData *data;
data = [[NSDataStatic alloc] initWithBytesNoCopy: aBuffer
length: bufferSize];
[self dealloc];
return data;
}
else
if (aBuffer)
objc_free(aBuffer);
zone = aZone;
bytes = aBuffer;
if (bytes)
length = bufferSize;
return self;
}
@ -1232,22 +1268,29 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
unsigned int l;
void* b;
[aCoder decodeValueOfObjCType:"I" at: &l];
b = objc_malloc(l);
if (b == 0)
zone = [self zone];
[aCoder decodeValueOfObjCType: @encode(unsigned long) at: &l];
if (l)
{
NSLog(@"[NSDataMalloc -initWithCode:] unable to allocate %lu bytes", l);
[self dealloc];
return nil;
b = NSZoneMalloc(zone, l);
if (b == 0)
{
NSLog(@"[NSDataMalloc -initWithCoder:] unable to get %lu bytes", l);
[self dealloc];
return nil;
}
[aCoder decodeArrayOfObjCType: @encode(unsigned char) count: l at: b];
}
[aCoder decodeArrayOfObjCType:"C" 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
{
self = [super init];
if (readContentsOfFile(path, &bytes, &length) == NO)
zone = [self zone];
if (readContentsOfFile(path, &bytes, &length, zone) == NO)
{
[self dealloc];
self = nil;
@ -1269,7 +1312,7 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
- (id) initWithData: (NSData*)anObject
{
if (anObject == nil)
return [self initWithBytesNoCopy: 0 length: 0];
return [self initWithBytesNoCopy: 0 length: 0 fromZone: [self zone]];
if ([anObject isKindOfClass:[NSData class]] == NO)
{
@ -1404,9 +1447,6 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
{
struct shmid_ds buf;
self = [super init];
if (self == nil)
return nil;
shmid = -1;
if (aBuffer && bufferSize)
{
@ -1439,10 +1479,6 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
{
struct shmid_ds buf;
self = [super init];
if (self == nil)
return nil;
shmid = anId;
if (shmctl(shmid, IPC_STAT, &buf) < 0)
{
@ -1492,9 +1528,12 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
}
- (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
@ -1529,7 +1568,7 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
{
if (bytes)
{
objc_free(bytes);
NSZoneFree(zone, bytes);
bytes = 0;
length = 0;
capacity = 0;
@ -1539,8 +1578,8 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
- (void) encodeWithCoder: (NSCoder*)aCoder
{
[aCoder encodeValueOfObjCType:"I" at: &length];
[aCoder encodeArrayOfObjCType:"C" count:length at: bytes];
[aCoder encodeValueOfObjCType: @encode(unsigned long) at: &length];
[aCoder encodeArrayOfObjCType: @encode(unsigned char) count:length at: bytes];
}
- (id) init
@ -1565,43 +1604,55 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
- (id) initWithBytesNoCopy: (void*)aBuffer
length: (unsigned int)bufferSize
{
self = [self initWithCapacity: 0];
if (self)
{
bytes = aBuffer;
if (bytes)
{
length = bufferSize;
capacity = bufferSize;
}
}
else
if (aBuffer)
objc_free(aBuffer);
return self;
NSZone *aZone = NSZoneFromPointer(aBuffer);
return [self initWithBytesNoCopy: aBuffer length: bufferSize fromZone: aZone];
}
/*
* 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
{
self = [super init];
if (self)
zone = [self zone];
if (size)
{
if (size)
{
bytes = objc_malloc(size);
if (bytes == 0)
{
NSLog(@"[NSMutableDataMalloc -initWithCapacity:] out of memory for %u bytes - %s", size, strerror(errno));
[self dealloc];
return nil;
}
}
capacity = size;
length = 0;
bytes = NSZoneMalloc(zone, size);
if (bytes == 0)
{
NSLog(@"[NSMutableDataMalloc -initWithCapacity:] out of memory for %u bytes - %s", size, strerror(errno));
[self dealloc];
return nil;
}
}
capacity = size;
length = 0;
return self;
}
@ -1610,16 +1661,20 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
unsigned int l;
void* b;
[aCoder decodeValueOfObjCType:"I" at: &l];
b = objc_malloc(l);
if (b == 0)
[aCoder decodeValueOfObjCType: @encode(unsigned long) at: &l];
if (l)
{
NSLog(@"[NSMutableDataMalloc -initWithCode:] unable to allocate %lu bytes", l);
[self dealloc];
return nil;
[self initWithCapacity: l];
if (bytes == 0)
{
NSLog(@"[NSMutableDataMalloc -initWithCoder:] unable to allocate %lu bytes", l);
[self dealloc];
return nil;
}
[aCoder decodeArrayOfObjCType: @encode(unsigned char) count: l at: bytes];
length = l;
}
[aCoder decodeArrayOfObjCType:"C" count: l at: b];
return [self initWithBytesNoCopy: b length: l];
return self;
}
- (id) initWithLength: (unsigned)size
@ -1636,7 +1691,7 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
- (id) initWithContentsOfFile: (NSString *)path
{
self = [self initWithCapacity: 0];
if (readContentsOfFile(path, &bytes, &length) == NO)
if (readContentsOfFile(path, &bytes, &length, zone) == NO)
{
[self dealloc];
self = nil;
@ -1681,9 +1736,9 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
void* tmp;
if (bytes)
tmp = objc_realloc(bytes, size);
tmp = NSZoneRealloc(zone, bytes, size);
else
tmp = objc_malloc(size);
tmp = NSZoneMalloc(zone, size);
if (tmp == 0)
[NSException raise:NSMallocException
@ -1762,31 +1817,28 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
struct shmid_ds buf;
int e;
self = [super initWithCapacity: 0];
if (self)
shmid = shmget(IPC_PRIVATE, bufferSize, IPC_CREAT|VM_ACCESS);
if (shmid == -1) /* Created memory? */
{
shmid = shmget(IPC_PRIVATE, bufferSize, IPC_CREAT|VM_ACCESS);
if (shmid == -1) /* Created memory? */
{
NSLog(@"[NSMutableDataShared -initWithCapacity:] shared memory get failed for %u - %s", bufferSize, strerror(errno));
[self dealloc];
self = [NSMutableDataMalloc alloc];
return [self initWithCapacity: bufferSize];
}
bytes = shmat(shmid, 0, 0);
e = errno;
if (bytes == (void*)-1)
{
NSLog(@"[NSMutableDataShared -initWithCapacity:] shared memory attach failed for %u - %s", bufferSize, strerror(e));
bytes = 0;
[self dealloc];
self = [NSMutableDataMalloc alloc];
return [self initWithCapacity: bufferSize];
}
length = 0;
capacity = bufferSize;
NSLog(@"[NSMutableDataShared -initWithCapacity:] shared memory get failed for %u - %s", bufferSize, strerror(errno));
[self dealloc];
self = [NSMutableDataMalloc alloc];
return [self initWithCapacity: bufferSize];
}
bytes = shmat(shmid, 0, 0);
e = errno;
if (bytes == (void*)-1)
{
NSLog(@"[NSMutableDataShared -initWithCapacity:] shared memory attach failed for %u - %s", bufferSize, strerror(e));
bytes = 0;
[self dealloc];
self = [NSMutableDataMalloc alloc];
return [self initWithCapacity: bufferSize];
}
length = 0;
capacity = bufferSize;
return self;
}
@ -1794,33 +1846,30 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len)
{
struct shmid_ds buf;
self = [super initWithCapacity: 0];
if (self)
shmid = anId;
if (shmctl(shmid, IPC_STAT, &buf) < 0)
{
shmid = anId;
if (shmctl(shmid, IPC_STAT, &buf) < 0)
{
NSLog(@"[NSMutableDataShared -initWithShmID:length:] shared memory control failed - %s", strerror(errno));
[self dealloc]; /* Unable to access memory. */
return nil;
}
if (buf.shm_segsz < bufferSize)
{
NSLog(@"[NSMutableDataShared -initWithShmID:length:] shared memory segment too small");
[self dealloc]; /* Memory segment too small. */
return nil;
}
bytes = shmat(shmid, 0, 0);
if (bytes == (void*)-1)
{
NSLog(@"[NSMutableDataShared -initWithShmID:length:] shared memory attach failed - %s", strerror(errno));
bytes = 0;
[self dealloc]; /* Unable to attach to memory. */
return nil;
}
length = bufferSize;
capacity = length;
NSLog(@"[NSMutableDataShared -initWithShmID:length:] shared memory control failed - %s", strerror(errno));
[self dealloc]; /* Unable to access memory. */
return nil;
}
if (buf.shm_segsz < bufferSize)
{
NSLog(@"[NSMutableDataShared -initWithShmID:length:] shared memory segment too small");
[self dealloc]; /* Memory segment too small. */
return nil;
}
bytes = shmat(shmid, 0, 0);
if (bytes == (void*)-1)
{
NSLog(@"[NSMutableDataShared -initWithShmID:length:] shared memory attach failed - %s", strerror(errno));
bytes = 0;
[self dealloc]; /* Unable to attach to memory. */
return nil;
}
length = bufferSize;
capacity = length;
return self;
}

View file

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

View file

@ -1,8 +1,10 @@
/* 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>
Date: March 1995
Optimised by: Richard frith-Macdoanld <richard@brainstorm.co.uk>
Date: October 1998
This file is part of the GNUstep Base Library.
@ -25,6 +27,7 @@
#include <gnustep/base/preface.h>
#include <Foundation/NSString.h>
#include <Foundation/NSData.h>
#include <Foundation/NSCoder.h>
#include <gnustep/base/NSGString.h>
#include <gnustep/base/NSGCString.h>
#include <gnustep/base/IndexedCollection.h>
@ -39,9 +42,11 @@
- (void)dealloc
{
if (_free_contents)
OBJC_FREE(_contents_chars);
[super dealloc];
if (_zone) {
NSZoneFree(_zone, (void*)_contents_chars);
_zone = 0;
}
[super dealloc];
}
- (unsigned) hash
@ -51,33 +56,66 @@
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
freeWhenDone: (BOOL)flag
length: (unsigned int)length
fromZone: (NSZone*)zone
{
/* assert(!flag); xxx need to make a subclass to handle this. */
[super init];
_count = length;
_contents_chars = byteString;
_free_contents = flag;
return self;
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
length: (unsigned int)length
freeWhenDone: (BOOL)flag
{
self = [super init];
if (self) {
_count = length;
_contents_chars = byteString;
if (flag) {
_zone = NSZoneFromPointer(byteString);
}
else {
_zone = 0;
}
}
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
length: (unsigned int)length
freeWhenDone: (BOOL)flag
{
id a = [[NSGString alloc] initWithCharactersNoCopy: chars
length: length
freeWhenDone: flag];
[self release];
return a;
NSZone *z = fastZone(self);
id a = [[NSGString allocWithZone: z] initWithCharactersNoCopy: chars
length: length
freeWhenDone: flag];
[self release];
return a;
}
- (id) init
{
return [self initWithCString:""];
return [self initWithCStringNoCopy: 0 length: 0 fromZone: 0];
}
- (void) _collectionReleaseContents
@ -87,21 +125,17 @@
- (void) _collectionDealloc
{
if (_free_contents)
OBJC_FREE(_contents_chars);
}
- (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];
if (_zone) {
NSZoneFree(_zone, (void*)_contents_chars);
_zone = 0;
}
}
- (Class) classForPortCoder
{
return [self class];
}
- replacementObjectForPortCoder:(NSPortCoder*)aCoder
{
return self;
@ -109,17 +143,35 @@
- (void) encodeWithCoder: aCoder
{
[aCoder encodeValueOfObjCType:@encode(char*) at:&_contents_chars
withName:@"Concrete String content_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"];
}
- initWithCoder: aCoder
{
[aCoder decodeValueOfObjCType:@encode(char*) at:&_contents_chars
withName:NULL];
_count = strlen(_contents_chars);
_free_contents = YES;
return self;
#if 0
[aCoder decodeValueOfObjCType:@encode(char*) at:&_contents_chars
withName:NULL];
_count = strlen(_contents_chars);
_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;
}
- (const char *) cString
@ -133,11 +185,6 @@
return r;
}
- (const char *) cStringNoCopy
{
return _contents_chars;
}
- (void) getCString: (char*)buffer
{
memcpy(buffer, _contents_chars, _count);
@ -145,7 +192,7 @@
}
- (void) getCString: (char*)buffer
maxLength: (unsigned int)maxLength
maxLength: (unsigned int)maxLength
{
if (maxLength > _count)
maxLength = _count;
@ -336,7 +383,12 @@
- (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
@ -362,7 +414,7 @@
case '\f':
case '\\':
case '"' :
length += 2;
length += 1;
break;
default:
@ -370,14 +422,15 @@
needQuote = YES;
}
else if (isprint(val) == 0) {
length += 4;
length += 3;
}
break;
}
}
if (needQuote || length != _count) {
char *buf = objc_malloc(length+3);
NSZone *z = fastZone(self);
char *buf = NSZoneMalloc(z, length+3);
char *ptr = buf;
NSString *result;
@ -413,7 +466,7 @@
*ptr++ = '"';
*ptr = '\0';
result = [[[_fastCls._NSGCString alloc] initWithCStringNoCopy: buf
length: length+2 freeWhenDone: YES] autorelease];
length: length+2 fromZone: z] autorelease];
return result;
}
return self;
@ -477,18 +530,40 @@ stringDecrementCountAndFillHoleAt(NSGMutableCStringStruct *self,
}
/* This is the designated initializer for this class */
/* NB. capacity does not include the '\0' terminator */
- initWithCapacity: (unsigned)capacity
{
[super init];
_count = 0;
_capacity = MAX(capacity, 3);
OBJC_MALLOC(_contents_chars, char, _capacity+1);
_contents_chars[0] = '\0';
_free_contents = YES;
_zone = fastZone(self);
_contents_chars = NSZoneMalloc(_zone, _capacity);
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
{
stringDecrementCountAndFillHoleAt((NSGMutableCStringStruct*)self,
@ -506,72 +581,71 @@ stringDecrementCountAndFillHoleAt(NSGMutableCStringStruct *self,
- (void) insertString: (NSString*)aString atIndex:(unsigned)index
{
unsigned c = [aString cStringLength];
if (_count + c > _capacity)
char save;
if (_count + c >= _capacity)
{
_capacity = MAX(_capacity*2, _count+c);
OBJC_REALLOC(_contents_chars, char, _capacity+1);
_capacity = MAX(_capacity*2, _count+c+1);
NSZoneRealloc(_zone, _contents_chars, _capacity);
}
stringIncrementCountAndMakeHoleAt((NSGMutableCStringStruct*)self, index, c);
save = _contents_chars[index+c]; // getCString will put a nul here.
[aString getCString: _contents_chars + index];
_contents_chars[_count] = '\0';
_contents_chars[index+c] = save;
}
- (void) appendString: (NSString*)aString
{
unsigned c = [aString cStringLength];
if (_count + c > _capacity)
Class c;
if (aString == nil || (c = fastClassOfInstance(aString)) == nil)
return;
if (c == _fastCls._NSGCString || c == _fastCls._NSGMutableCString)
{
_capacity = MAX(_capacity*2, _count+c);
OBJC_REALLOC(_contents_chars, char, _capacity+1);
NSGMutableCString *other = (NSGMutableCString*)aString;
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];
_count += l;
_hash = 0;
}
[aString getCString: _contents_chars + _count];
_count += c;
_contents_chars[_count] = '\0';
_hash = 0;
}
- (void) setString: (NSString*)aString
{
unsigned length = [aString cStringLength];
if (_capacity < length)
if (_capacity <= length)
{
_capacity = length;
OBJC_REALLOC(_contents_chars, char, _capacity+1);
_capacity = length+1;
_contents_chars =
NSZoneRealloc(fastZone(self), _contents_chars, _capacity);
}
[aString getCString: _contents_chars];
_contents_chars[length] = '\0';
_count = length;
_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
{
return [self initWithCString:""];
return [self initWithCStringNoCopy: 0 length: 0 fromZone: 0];
};
/* For IndexedCollecting Protocol and other GNU libobjects conformity. */
@ -584,25 +658,17 @@ stringDecrementCountAndFillHoleAt(NSGMutableCStringStruct *self,
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
{
unsigned cap;
[aCoder decodeValueOfObjCType:@encode(unsigned) at:&cap withName:NULL];
[self initWithCapacity:cap];
[aCoder decodeValueOfObjCType:@encode(char*) at:&_contents_chars
withName:NULL];
_count = strlen(_contents_chars);
_capacity = cap;
_free_contents = YES;
_count = cap;
[aCoder decodeArrayOfObjCType:@encode(unsigned char)
count:_count
at:_contents_chars
withName:NULL];
return self;
}
@ -624,18 +690,16 @@ stringDecrementCountAndFillHoleAt(NSGMutableCStringStruct *self,
if (_count >= _capacity)
{
_capacity *= 2;
OBJC_REALLOC(_contents_chars, char, _capacity+1);
NSZoneRealloc(fastZone(self), _contents_chars, _capacity);
}
stringIncrementCountAndMakeHoleAt((NSGMutableCStringStruct*)self, index, 1);
_contents_chars[index] = [newObject charValue];
_contents_chars[_count] = '\0';
}
- (void) removeObjectAtIndex: (unsigned)index
{
CHECK_INDEX_RANGE_ERROR(index, _count);
stringDecrementCountAndFillHoleAt((NSGMutableCStringStruct*)self, index, 1);
_contents_chars[_count] = '\0';
}
@end

View file

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

View file

@ -1,5 +1,5 @@
/* 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>
Date: February 1997
@ -9,6 +9,9 @@
<mccallum@gnu.ai.mit.edu>
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 library is free software; you can redistribute it and/or
@ -46,10 +49,10 @@
- (void)dealloc
{
if (_free_contents)
if (_zone)
{
OBJC_FREE(_contents_chars);
_free_contents = NO;
NSZoneFree(_zone, _contents_chars);
_zone = 0;
}
[super dealloc];
}
@ -95,43 +98,73 @@
// 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
freeWhenDone: (BOOL)flag
length: (unsigned int)length
fromZone: (NSZone*)zone
{
/* assert(!flag); xxx need to make a subclass to handle this. */
[super init];
_count = length;
_contents_chars = chars;
_free_contents = flag;
return self;
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
length: (unsigned int)length
freeWhenDone: (BOOL)flag
{
self = [super init];
if (self) {
_count = length;
_contents_chars = chars;
if (flag) {
_zone = NSZoneFromPointer(chars);
}
else {
_zone = 0;
}
}
return self;
}
- (id) initWithCharacters: (const unichar*)chars
length: (unsigned int)length
length: (unsigned int)length
{
unichar *s;
OBJC_MALLOC(s, unichar, length);
if (chars)
memcpy(s, chars,2*length);
return [self initWithCharactersNoCopy:s length:length freeWhenDone:YES];
NSZone *z = fastZone(self);
unichar *s = NSZoneMalloc(z, length*sizeof(unichar));
if (chars)
memcpy(s, chars, sizeof(unichar)*length);
return [self initWithCharactersNoCopy:s length:length fromZone:z];
}
- (id) initWithCStringNoCopy: (char*)byteString
length: (unsigned int)length
freeWhenDone: (BOOL)flag
length: (unsigned int)length
fromZone: (NSZone*)zone
{
id a = [[NSGCString alloc] initWithCStringNoCopy: byteString
length: length
freeWhenDone: flag];
id a = [[NSGCString 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 = [[NSGCString allocWithZone: fastZone(self)]
initWithCStringNoCopy: byteString length: length freeWhenDone: flag];
[self release];
return a;
}
- (id) init
{
return [self initWithCharactersNoCopy:0 length:0 freeWhenDone: NO];
return [self initWithCharactersNoCopy:0 length:0 fromZone: fastZone(self)];
}
// Getting a String's Length
@ -217,23 +250,6 @@
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 */
- (void) encodeWithCoder: aCoder
@ -248,15 +264,15 @@
- initWithCoder: aCoder
{
[aCoder decodeValueOfObjCType:@encode(int) at:&_count
withName:NULL];
OBJC_MALLOC(_contents_chars, unichar, _count+1);
[aCoder decodeArrayOfObjCType:@encode(unichar)
[aCoder decodeValueOfObjCType:@encode(int) at:&_count
withName:NULL];
_zone = fastZone(self);
_contents_chars = NSZoneMalloc(_zone, sizeof(unichar)*_count);
[aCoder decodeArrayOfObjCType:@encode(unichar)
count:_count
at:_contents_chars
withName:NULL];
_free_contents = YES;
return self;
return self;
}
@ -270,21 +286,18 @@
- (void) _collectionDealloc
{
if (_free_contents)
OBJC_FREE(_contents_chars);
}
- (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];
if (_zone)
{
NSZoneFree(_zone, _contents_chars);
_zone = 0;
}
}
- (Class) classForPortCoder
{
return [self class];
}
- replacementObjectForPortCoder:(NSPortCoder*)aCoder
{
return self;
@ -367,29 +380,72 @@ stringDecrementCountAndFillHoleAt(NSGMutableStringStruct *self,
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
length: (unsigned int)length
freeWhenDone: (BOOL)flag
{
[super init];
_count = length;
_capacity = length;
_contents_chars = chars;
_free_contents = flag;
return self;
self = [super init];
if (self) {
_count = length;
_capacity = length;
_contents_chars = chars;
if (flag) {
_zone = NSZoneFromPointer(chars);
}
else {
_zone = 0;
}
}
return self;
}
// NB capacity does not include the '\0' terminator.
- initWithCapacity: (unsigned)capacity
{
unichar *tmp;
if (capacity < 2)
capacity = 2;
OBJC_MALLOC(tmp, unichar, capacity);
[self initWithCharactersNoCopy: tmp length: 0 freeWhenDone: YES];
_capacity = capacity;
return self;
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;
}
- (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
@ -419,7 +475,8 @@ stringDecrementCountAndFillHoleAt(NSGMutableStringStruct *self,
_capacity += stringLength - aRange.length;
if (_capacity < 2)
_capacity = 2;
OBJC_REALLOC(_contents_chars, unichar, _capacity);
_contents_chars =
NSZoneRealloc(_zone, _contents_chars, sizeof(unichar)*_capacity);
}
#ifdef HAVE_MEMMOVE
@ -456,27 +513,14 @@ stringDecrementCountAndFillHoleAt(NSGMutableStringStruct *self,
_capacity = len;
if (_capacity < 2)
_capacity = 2;
OBJC_REALLOC(_contents_chars, unichar, _capacity);
_contents_chars =
NSZoneRealloc(_zone, _contents_chars, sizeof(unichar)*_capacity);
}
[aString getCharacters: _contents_chars];
_count = len;
_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. */
/* xxx This should be made to return void, but we need to change
@ -535,7 +579,8 @@ stringDecrementCountAndFillHoleAt(NSGMutableStringStruct *self,
_capacity = _count;
if (_capacity < 2)
_capacity = 2;
OBJC_REALLOC(_contents_chars, unichar, _capacity);
_contents_chars =
NSZoneRealloc(_zone, _contents_chars, sizeof(unichar)*_capacity);
}
stringIncrementCountAndMakeHoleAt((NSGMutableStringStruct*)self, index, 1);
_contents_chars[index] = [newObject charValue];

View file

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

View file

@ -41,7 +41,8 @@
#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;

View file

@ -105,6 +105,12 @@ static Class NSMutableSet_concrete_class;
return [self autorelease];
}
+ setWithSet: (NSSet*)aSet
{
return [[[self alloc] initWithSet: aSet]
autorelease];
}
+ allocWithZone: (NSZone*)z
{
return NSAllocateObject([self _concreteClass], 0, z);
@ -148,33 +154,31 @@ static Class NSMutableSet_concrete_class;
- copyWithZone: (NSZone*)z
{
/* a deep copy */
int count = [self count];
id objects[count];
id enumerator = [self objectEnumerator];
id o;
NSSet *newSet;
int i;
BOOL needCopy = [self isKindOfClass: [NSMutableSet class]];
/* a deep copy */
int count = [self count];
id objects[count];
id enumerator = [self objectEnumerator];
id o;
NSSet *newSet;
int i;
BOOL needCopy = [self isKindOfClass: [NSMutableSet class]];
if (NSShouldRetainWithZone(self, z) == NO)
needCopy = YES;
if (NSShouldRetainWithZone(self, z) == NO)
needCopy = YES;
for (i = 0; (o = [enumerator nextObject]); i++)
{
objects[i] = [o copyWithZone:z];
if (objects[i] != o)
needCopy = YES;
for (i = 0; (o = [enumerator nextObject]); i++) {
objects[i] = [o copyWithZone:z];
if (objects[i] != o)
needCopy = YES;
}
if (needCopy)
newSet = [[[[self class] _concreteClass] allocWithZone: z]
initWithObjects:objects
count:count];
else
newSet = [self retain];
for (i = 0; i < count; i++)
[objects[i] release];
return newSet;
if (needCopy)
newSet = [[[[self class] _concreteClass] allocWithZone: z]
initWithObjects:objects count:count];
else
newSet = [self retain];
for (i = 0; i < count; i++)
[objects[i] release];
return newSet;
}
- mutableCopyWithZone: (NSZone*)z

View file

@ -1,5 +1,5 @@
/* 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>
Date: January 1995
@ -7,6 +7,9 @@
Unicode implementation by Stevo Crvenkovski <stevo@btinternet.com>
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 library is free software; you can redistribute it and/or
@ -315,28 +318,58 @@ handle_printf_atsign (FILE *stream,
/* This is the designated initializer for Unicode Strings. */
- (id) initWithCharactersNoCopy: (unichar*)chars
length: (unsigned int)length
freeWhenDone: (BOOL)flag
length: (unsigned int)length
fromZone: (NSZone*)zone
{
[self subclassResponsibility:_cmd];
return self;
}
- (id) initWithCharacters: (const unichar*)chars
- (id) initWithCharactersNoCopy: (unichar*)chars
length: (unsigned int)length
freeWhenDone: (BOOL)flag
{
unichar *s;
OBJC_MALLOC(s, unichar, length+1);
if (chars)
memcpy(s, chars,2*length);
s[length] = (unichar)0;
return [self initWithCharactersNoCopy:s length:length freeWhenDone:YES];
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
length: (unsigned int)length
{
NSZone *z = [self zone];
unichar *s = NSZoneMalloc(z, sizeof(unichar)*length);
if (chars)
memcpy(s, chars, sizeof(unichar)*length);
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. */
- (id) initWithCStringNoCopy: (char*)byteString
length: (unsigned int)length
freeWhenDone: (BOOL)flag
length: (unsigned int)length
fromZone: (NSZone*)zone
{
[self subclassResponsibility:_cmd];
return self;
@ -344,12 +377,12 @@ handle_printf_atsign (FILE *stream,
- (id) initWithCString: (const char*)byteString length: (unsigned int)length
{
char *s;
OBJC_MALLOC(s, char, length+1);
if (byteString)
memcpy(s, byteString, length);
s[length] = '\0';
return [self initWithCStringNoCopy:s length:length freeWhenDone:YES];
NSZone *z = [self zone];
char *s = NSZoneMalloc(z, length);
if (byteString)
memcpy(s, byteString, length);
return [self initWithCStringNoCopy:s length:length fromZone:z];
}
- (id) initWithCString: (const char*)byteString
@ -360,14 +393,14 @@ handle_printf_atsign (FILE *stream,
- (id) initWithString: (NSString*)string
{
unichar *s;
unsigned length = [string length];
OBJC_MALLOC(s, unichar, length+1);
[string getCharacters:s];
s[length] = (unichar)0;
return [self initWithCharactersNoCopy: s
length: length
freeWhenDone: YES];
NSZone *z = [self zone];
unsigned length = [string length];
unichar *s = NSZoneMalloc(z, sizeof(unichar)*length);
[string getCharacters:s];
return [self initWithCharactersNoCopy: s
length: length
fromZone: z];
}
- (id) initWithFormat: (NSString*)format,...
@ -504,42 +537,39 @@ handle_printf_atsign (FILE *stream,
- (id) initWithData: (NSData*)data
encoding: (NSStringEncoding)encoding
{
if((encoding==[NSString defaultCStringEncoding])
|| (encoding==NSASCIIStringEncoding))
{
char *s;
if ((encoding==[NSString defaultCStringEncoding])
|| (encoding==NSASCIIStringEncoding))
{
NSZone *z = fastZone(self);
int len=[data length];
char *s = NSZoneMalloc(z, len+1);
int len=[data length];
OBJC_MALLOC(s, char, len+1);
[data getBytes:s];
s[len]=0;
return [self initWithCStringNoCopy:s length:len freeWhenDone:YES];
[data getBytes:s];
return [self initWithCStringNoCopy:s length:len fromZone:z];
}
else
{
unichar *u;
int count;
int len=[data length];
const unsigned char *b=[data bytes];
OBJC_MALLOC(u, 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];
else
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);
NSZone *z = fastZone(self);
int len=[data length];
unichar *u = NSZoneMalloc(z, sizeof(unichar)*(len+1));
int count;
const unsigned char *b=[data bytes];
u[count]=(unichar)0;
return [self initWithCharactersNoCopy:u length:count freeWhenDone:YES];
}
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];
else
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);
return [self initWithCharactersNoCopy:u length:count fromZone:z];
}
return self;
}
@ -614,17 +644,16 @@ handle_printf_atsign (FILE *stream,
- (NSString*) stringByAppendingString: (NSString*)aString
{
NSZone *z = fastZone(self);
unsigned len = [self length];
unsigned otherLength = [aString length];
unichar *s;
unichar *s = NSZoneMalloc(z, (len+otherLength)*sizeof(unichar));
NSString *tmp;
OBJC_MALLOC(s, unichar, len+otherLength+1);
[self getCharacters:s];
[aString getCharacters:s+len];
s[len + otherLength]=(unichar) 0;
tmp = [[[self class] alloc] initWithCharactersNoCopy: s
length: len+otherLength
freeWhenDone: YES];
tmp = [[[self class] allocWithZone:z] initWithCharactersNoCopy: s
length: len+otherLength fromZone: z];
return [tmp autorelease];
}
@ -667,6 +696,7 @@ handle_printf_atsign (FILE *stream,
- (NSString*) substringFromRange: (NSRange)aRange
{
NSZone *z;
unichar *buf;
id ret;
@ -676,11 +706,12 @@ handle_printf_atsign (FILE *stream,
[NSException raise: NSRangeException format:@"Invalid location+length."];
if (aRange.length == 0)
return @"";
OBJC_MALLOC(buf, unichar, aRange.length+1);
z = fastZone(self);
buf = NSZoneMalloc(z, sizeof(unichar)*aRange.length);
[self getCharacters:buf range:aRange];
ret = [[[self class] alloc] initWithCharactersNoCopy: buf
length: aRange.length
freeWhenDone: YES];
fromZone: z];
return [ret autorelease];
}
@ -1834,13 +1865,14 @@ else
// 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];
id white = [NSCharacterSet whitespaceAndNewlineCharacterSet];
OBJC_MALLOC(s, unichar,len +1);
s = NSZoneMalloc(z, sizeof(unichar)*(len+1));
[self getCharacters:s];
s[len] = (unichar)0;
while(count<len)
@ -1867,36 +1899,35 @@ else
};
found=NO;
};
s[count] = (unichar)0;
return [[[NSString alloc] initWithCharactersNoCopy:s length:len freeWhenDone:YES] autorelease];
return [[[NSString alloc] initWithCharactersNoCopy:s length:len fromZone:z] autorelease];
}
- (NSString*) lowercaseString
{
NSZone *z = fastZone(self);
unichar *s;
int count;
int len=[self length];
OBJC_MALLOC(s, unichar,len +1);
s = NSZoneMalloc(z, sizeof(unichar)*(len+1));
for(count=0;count<len;count++)
s[count]=uni_tolower([self characterAtIndex:count]);
s[len] = (unichar)0;
return [[[[self class] alloc] initWithCharactersNoCopy: s
length: len
freeWhenDone: YES] autorelease];
fromZone: z] autorelease];
}
- (NSString*) uppercaseString;
{
NSZone *z = fastZone(self);
unichar *s;
int count;
int len=[self length];
OBJC_MALLOC(s, unichar,len +1);
s = NSZoneMalloc(z, sizeof(unichar)*(len+1));
for(count=0;count<len;count++)
s[count]=uni_toupper([self characterAtIndex:count]);
s[len] = (unichar)0;
return [[[[self class] alloc] initWithCharactersNoCopy: s
length: len
freeWhenDone: YES] autorelease];
fromZone: z] autorelease];
}
// Storing the String
@ -2417,12 +2448,14 @@ else
{
#define MAXDEC 18
NSZone *z = fastZone(self);
unichar *u, *upoint;
NSRange r;
id seq,ret;
int len = [self length];
int count = 0;
OBJC_MALLOC(u, unichar, len*MAXDEC+1);
u = NSZoneMalloc(z, sizeof(unichar)*(len*MAXDEC+1));
upoint = u;
while(count < len)
@ -2437,7 +2470,7 @@ else
ret = [[[[self class] alloc] initWithCharactersNoCopy: u
length: uslen(u)
freeWhenDone: YES] autorelease];
fromZone: z] autorelease];
return ret;
}
@ -2533,12 +2566,6 @@ else
// #endif
// #ifndef NO_GNUSTEP
// This method should be removed
- (const char *) cStringNoCopy
{
[self subclassResponsibility: _cmd];
return NULL;
}
- (NSString*) descriptionForPropertyList
{
@ -2637,10 +2664,6 @@ else
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
{
return [[[[self class] _mutableConcreteClass] allocWithZone:zone]

File diff suppressed because it is too large Load diff