Minor extensions and bug fixes

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@3087 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
richard 1998-10-20 14:40:05 +00:00
parent dcf4ac31ea
commit 43fb5c926a
3 changed files with 332 additions and 251 deletions

View file

@ -107,8 +107,12 @@ extern fastImp _fastImp; /* Populated by _fastBuildCache() */
*/ */
extern void _fastBuildCache(); extern void _fastBuildCache();
/* /*
* Fast access to class info - DON'T pass nil to these! * Fast access to class info - DON'T pass nil to these!
* These should really do different things conditional upon the objc
* runtime in use, but we will probably only ever want to support the
* latest GNU runtime, so I haven't bothered about that.
*/ */
static INLINE BOOL static INLINE BOOL
@ -158,6 +162,29 @@ fastInstanceIsKindOfClass(NSObject *obj, Class c)
return fastClassIsKindOfClass(ic, c); return fastClassIsKindOfClass(ic, c);
} }
static INLINE const char*
fastClassName(Class c)
{
return c->name;
}
static INLINE int
fastClassVersion(Class c)
{
return c->version;
}
static INLINE const char*
fastSelectorName(SEL s)
{
return sel_get_name(s);
}
static INLINE const char*
fastSelectorTypes(SEL s)
{
return sel_get_type(s);
}
/* /*
* fastZone(NSObject *obj) * fastZone(NSObject *obj)

View file

@ -93,9 +93,15 @@
typedef union { typedef union {
NSObject *o; NSObject *o;
long int i; Class c;
int i;
unsigned I;
long l;
unsigned long L;
void *p; void *p;
unsigned u; const void *P;
char *s;
const char *S;
} FastMapItem; } FastMapItem;
typedef struct _FastMapTable FastMapTable_t; typedef struct _FastMapTable FastMapTable_t;

View file

@ -92,6 +92,8 @@
@class NSDataStatic; @class NSDataStatic;
@class NSMutableDataMalloc; @class NSMutableDataMalloc;
static Class dataMallocClass = 0;
static Class mutableDataMallocClass = 0;
static BOOL static BOOL
readContentsOfFile(NSString* path, void** buf, unsigned* len, NSZone* zone) readContentsOfFile(NSString* path, void** buf, unsigned* len, NSZone* zone)
@ -215,9 +217,18 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len, NSZone* zone)
@implementation NSData @implementation NSData
+ (void) initialize
{
if ([self class] == [NSData class]) {
dataMallocClass = [NSDataMalloc class];
mutableDataMallocClass = [NSMutableDataMalloc class];
}
}
+ (NSData*) allocWithZone: (NSZone*)z + (NSData*) allocWithZone: (NSZone*)z
{ {
return (NSData*)NSAllocateObject([NSDataMalloc class], 0, z); return (NSData*)NSAllocateObject(dataMallocClass, 0, z);
} }
+ (id) data + (id) data
@ -229,20 +240,20 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len, NSZone* zone)
+ (id) dataWithBytes: (const void*)bytes + (id) dataWithBytes: (const void*)bytes
length: (unsigned int)length length: (unsigned int)length
{ {
return [[[NSDataMalloc alloc] initWithBytes:bytes length:length] return [[[dataMallocClass alloc] initWithBytes: bytes length: length]
autorelease]; autorelease];
} }
+ (id) dataWithBytesNoCopy: (void*)bytes + (id) dataWithBytesNoCopy: (void*)bytes
length: (unsigned int)length length: (unsigned int)length
{ {
return [[[NSDataMalloc alloc] initWithBytesNoCopy:bytes length:length] return [[[dataMallocClass alloc] initWithBytesNoCopy: bytes length: length]
autorelease]; autorelease];
} }
+ (id) dataWithContentsOfFile: (NSString*)path + (id) dataWithContentsOfFile: (NSString*)path
{ {
return [[[NSDataMalloc alloc] initWithContentsOfFile:path] return [[[dataMallocClass alloc] initWithContentsOfFile: path]
autorelease]; autorelease];
} }
@ -252,14 +263,14 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len, NSZone* zone)
return [[[NSDataMappedFile alloc] initWithContentsOfMappedFile: path] return [[[NSDataMappedFile alloc] initWithContentsOfMappedFile: path]
autorelease]; autorelease];
#else #else
return [[[NSDataMalloc alloc] initWithContentsOfMappedFile:path] return [[[dataMallocClass alloc] initWithContentsOfMappedFile: path]
autorelease]; autorelease];
#endif #endif
} }
+ (id) dataWithData: (NSData*)data + (id) dataWithData: (NSData*)data
{ {
return [[[NSDataMalloc alloc] initWithBytes: [data bytes] return [[[dataMallocClass alloc] initWithBytes: [data bytes]
length: [data length]] autorelease]; length: [data length]] autorelease];
} }
@ -539,6 +550,7 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len, NSZone* zone)
atCursor: (unsigned int*)cursor atCursor: (unsigned int*)cursor
{ {
NSRange range = { *cursor, bytes }; NSRange range = { *cursor, bytes };
[self getBytes: buffer range: range]; [self getBytes: buffer range: range];
*cursor += bytes; *cursor += bytes;
} }
@ -559,7 +571,6 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len, NSZone* zone)
} }
case _C_CHARPTR: { case _C_CHARPTR: {
int length = [self deserializeIntAtCursor: cursor]; int length = [self deserializeIntAtCursor: cursor];
id adr = nil;
if (length == -1) { if (length == -1) {
*(const char**)data = NULL; *(const char**)data = NULL;
@ -567,39 +578,46 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len, NSZone* zone)
} }
else { else {
unsigned len = (length+1)*sizeof(char); unsigned len = (length+1)*sizeof(char);
NSZone *z = [self zone];
*(char**)data = (char*)NSZoneMalloc([self zone], len); *(char**)data = (char*)NSZoneMalloc(z, len);
adr = [NSData dataWithBytesNoCopy: *(void**)data length: len]; [[[dataMallocClass allocWithZone: z]
initWithBytesNoCopy: *(void**)data
length: len
fromZone: z] autorelease];
} }
[self deserializeBytes:*(char**)data length:length atCursor:cursor]; [self deserializeBytes: *(char**)data
length: length
atCursor: cursor];
(*(char**)data)[length] = '\0'; (*(char**)data)[length] = '\0';
[adr retain];
break; break;
} }
case _C_ARY_B: { case _C_ARY_B: {
int i, count, offset, itemSize; unsigned offset = 0;
const char* itemType; unsigned size;
unsigned count = atoi(++type);
unsigned i;
count = atoi(type + 1); while (isdigit(*type)) {
itemType = type; type++;
while(isdigit(*++itemType)); }
itemSize = objc_sizeof_type(itemType); size = objc_sizeof_type(type);
for(i = offset = 0; i < count; i++, offset += itemSize) for (i = 0; i < count; i++) {
[self deserializeDataAt: (char*)data + offset [self deserializeDataAt: (char*)data + offset
ofObjCType:itemType ofObjCType: type
atCursor: cursor atCursor: cursor
context: callback]; context: callback];
break; offset += size;
}
return;
} }
case _C_STRUCT_B: { case _C_STRUCT_B: {
int offset = 0; int offset = 0;
int align, rem;
while (*type != _C_STRUCT_E && *type++ != '='); /* skip "<name>=" */ while (*type != _C_STRUCT_E && *type++ != '='); /* skip "<name>=" */
while(1) { for (;;) {
[self deserializeDataAt: ((char*)data) + offset [self deserializeDataAt: ((char*)data) + offset
ofObjCType: type ofObjCType: type
atCursor: cursor atCursor: cursor
@ -607,28 +625,30 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len, NSZone* zone)
offset += objc_sizeof_type(type); offset += objc_sizeof_type(type);
type = objc_skip_typespec(type); type = objc_skip_typespec(type);
if (*type != _C_STRUCT_E) { if (*type != _C_STRUCT_E) {
align = objc_alignof_type(type); int align = objc_alignof_type(type);
if((rem = offset % align)) int rem = offset % align;
if (rem != 0) {
offset += align - rem; offset += align - rem;
} }
}
else break; else break;
} }
break; break;
} }
case _C_PTR: { case _C_PTR: {
unsigned len = objc_sizeof_type(++type); unsigned len = objc_sizeof_type(++type);
id adr; NSZone *z = [self zone];
*(char**)data = (char*)NSZoneMalloc([self zone], len);
adr = [NSData dataWithBytesNoCopy: *(void**)data length: len];
*(char**)data = (char*)NSZoneMalloc(z, len);
[[[dataMallocClass allocWithZone: z]
initWithBytesNoCopy: *(void**)data
length: len
fromZone: z] autorelease];
[self deserializeDataAt: *(char**)data [self deserializeDataAt: *(char**)data
ofObjCType: type ofObjCType: type
atCursor: cursor atCursor: cursor
context: callback]; context: callback];
[adr retain];
break; break;
} }
case _C_CHR: case _C_CHR:
@ -660,7 +680,7 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len, NSZone* zone)
} }
case _C_LNG: case _C_LNG:
case _C_ULNG: { case _C_ULNG: {
unsigned int nl; unsigned long nl;
[self deserializeBytes: &nl [self deserializeBytes: &nl
length: sizeof(unsigned long) length: sizeof(unsigned long)
@ -668,6 +688,18 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len, NSZone* zone)
*(unsigned long*)data = NSSwapBigLongToHost(nl); *(unsigned long*)data = NSSwapBigLongToHost(nl);
break; break;
} }
#ifdef _C_LNG_LNG
case _C_LNG_LNG:
case _C_ULNG_LNG: {
unsigned long long nl;
[self deserializeBytes: &nl
length: sizeof(unsigned long long)
atCursor: cursor];
*(unsigned long long*)data = NSSwapBigLongLongToHost(nl);
break;
}
#endif
case _C_FLT: { case _C_FLT: {
NSSwappedFloat nf; NSSwappedFloat nf;
@ -732,9 +764,10 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len, NSZone* zone)
[self deserializeBytes: &intBuffer [self deserializeBytes: &intBuffer
length: numInts * sizeof(int) length: numInts * sizeof(int)
atCursor: &index]; atCursor: &index];
for (i = 0; i < numInts; i++) for (i = 0; i < numInts; i++) {
intBuffer[i] = NSSwapBigIntToHost(intBuffer[i]); intBuffer[i] = NSSwapBigIntToHost(intBuffer[i]);
} }
}
- (id) copyWithZone: (NSZone*)zone - (id) copyWithZone: (NSZone*)zone
{ {
@ -742,7 +775,7 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len, NSZone* zone)
[self isKindOfClass: [NSMutableData class]] == NO) [self isKindOfClass: [NSMutableData class]] == NO)
return [self retain]; return [self retain];
else else
return [[NSDataMalloc allocWithZone: zone] return [[dataMallocClass allocWithZone: zone]
initWithBytes: [self bytes] length: [self length]]; initWithBytes: [self bytes] length: [self length]];
} }
@ -783,7 +816,7 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len, NSZone* zone)
return [[[NSDataShared alloc] initWithBytes: bytes length: length] return [[[NSDataShared alloc] initWithBytes: bytes length: length]
autorelease]; autorelease];
#else #else
return [[[NSDataMalloc alloc] initWithBytes:bytes length:length] return [[[dataMallocClass alloc] initWithBytes: bytes length: length]
autorelease]; autorelease];
#endif #endif
} }
@ -988,12 +1021,12 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len, NSZone* zone)
return; return;
switch (*type) { switch (*type) {
case _C_ID: { case _C_ID:
[callback serializeObjectAt: (id*)data [callback serializeObjectAt: (id*)data
ofObjCType: type ofObjCType: type
intoData: self]; intoData: self];
break; return;
}
case _C_CHARPTR: { case _C_CHARPTR: {
int len; int len;
@ -1004,29 +1037,33 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len, NSZone* zone)
len = strlen(*(void**)data); len = strlen(*(void**)data);
[self serializeInt: len]; [self serializeInt: len];
[self appendBytes: *(void**)data length: len]; [self appendBytes: *(void**)data length: len];
return;
break;
} }
case _C_ARY_B: { case _C_ARY_B: {
int i, offset, itemSize, count = atoi(type + 1); unsigned offset = 0;
const char* itemType = type; unsigned size;
unsigned count = atoi(++type);
unsigned i;
while(isdigit(*++itemType)); while (isdigit(*type)) {
itemSize = objc_sizeof_type(itemType); type++;
}
size = objc_sizeof_type(type);
for(i = offset = 0; i < count; i++, offset += itemSize) for (i = 0; i < count; i++) {
[self serializeDataAt: (char*)data + offset [self serializeDataAt: (char*)data + offset
ofObjCType:itemType ofObjCType: type
context: callback]; context: callback];
offset += size;
break; }
return;
} }
case _C_STRUCT_B: { case _C_STRUCT_B: {
int offset = 0; int offset = 0;
int align, rem; int align, rem;
while (*type != _C_STRUCT_E && *type++ != '='); /* skip "<name>=" */ while (*type != _C_STRUCT_E && *type++ != '='); /* skip "<name>=" */
while(1) { for (;;) {
[self serializeDataAt: ((char*)data) + offset [self serializeDataAt: ((char*)data) + offset
ofObjCType: type ofObjCType: type
context: callback]; context: callback];
@ -1039,12 +1076,13 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len, NSZone* zone)
} }
else break; else break;
} }
break; return;
} }
case _C_PTR: case _C_PTR:
[self serializeDataAt: *(char**)data [self serializeDataAt: *(char**)data
ofObjCType:++type context:callback]; ofObjCType: ++type
break; context: callback];
return;
case _C_CHR: case _C_CHR:
case _C_UCHR: case _C_UCHR:
[self appendBytes: data length: sizeof(unsigned char)]; [self appendBytes: data length: sizeof(unsigned char)];
@ -1067,6 +1105,16 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len, NSZone* zone)
[self appendBytes: &nl length: sizeof(unsigned long)]; [self appendBytes: &nl length: sizeof(unsigned long)];
break; break;
} }
#ifdef _C_LNG_LNG
case _C_LNG_LNG:
case _C_ULNG_LNG: {
unsigned long long nl;
nl = NSSwapHostLongLongToBig(*(unsigned long long*)data);
[self appendBytes: &nl length: sizeof(unsigned long long)];
break;
}
#endif
case _C_FLT: { case _C_FLT: {
NSSwappedFloat nf = NSSwapHostFloatToBig(*(float*)data); NSSwappedFloat nf = NSSwapHostFloatToBig(*(float*)data);
[self appendBytes: &nf length: sizeof(NSSwappedFloat)]; [self appendBytes: &nf length: sizeof(NSSwappedFloat)];
@ -1165,7 +1213,7 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len, NSZone* zone)
@implementation NSDataMalloc @implementation NSDataMalloc
+ (NSData*) allocWithZone: (NSZone*)z + (NSData*) allocWithZone: (NSZone*)z
{ {
return (NSData*)NSAllocateObject([NSDataMalloc class], 0, z); return (NSData*)NSAllocateObject(dataMallocClass, 0, z);
} }
- (const void*) bytes - (const void*) bytes
@ -1175,17 +1223,17 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len, NSZone* zone)
- (Class) classForArchiver - (Class) classForArchiver
{ {
return [NSDataMalloc class]; return dataMallocClass;
} }
- (Class) classForCoder - (Class) classForCoder
{ {
return [NSDataMalloc class]; return dataMallocClass;
} }
- (Class) classForPortCoder - (Class) classForPortCoder
{ {
return [NSDataMalloc class]; return dataMallocClass;
} }
- (void) dealloc - (void) dealloc
@ -1403,7 +1451,7 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len, NSZone* zone)
NSLog(@"[NSDataMappedFile -initWithContentsOfMappedFile:] mapping failed for %s - %s", thePath, strerror(errno)); NSLog(@"[NSDataMappedFile -initWithContentsOfMappedFile:] mapping failed for %s - %s", thePath, strerror(errno));
close(fd); close(fd);
[self dealloc]; [self dealloc];
self = [NSDataMalloc alloc]; self = [dataMallocClass alloc];
self = [self initWithContentsOfFile: path]; self = [self initWithContentsOfFile: path];
} }
close(fd); close(fd);
@ -1456,7 +1504,7 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len, NSZone* zone)
NSLog(@"[-initWithBytes:length:] shared mem get failed for %u - %s", NSLog(@"[-initWithBytes:length:] shared mem get failed for %u - %s",
bufferSize, strerror(errno)); bufferSize, strerror(errno));
[self dealloc]; [self dealloc];
self = [NSDataMalloc alloc]; self = [dataMallocClass alloc];
return [self initWithBytes: aBuffer length: bufferSize]; return [self initWithBytes: aBuffer length: bufferSize];
} }
@ -1467,7 +1515,7 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len, NSZone* zone)
bufferSize, strerror(errno)); bufferSize, strerror(errno));
bytes = 0; bytes = 0;
[self dealloc]; [self dealloc];
self = [NSDataMalloc alloc]; self = [dataMallocClass alloc];
return [self initWithBytes: aBuffer length: bufferSize]; return [self initWithBytes: aBuffer length: bufferSize];
} }
length = bufferSize; length = bufferSize;
@ -1541,7 +1589,7 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len, NSZone* zone)
@implementation NSMutableDataMalloc @implementation NSMutableDataMalloc
+ (NSData*) allocWithZone: (NSZone*)z + (NSData*) allocWithZone: (NSZone*)z
{ {
return (NSData*)NSAllocateObject([NSMutableDataMalloc class], 0, z); return (NSData*)NSAllocateObject(mutableDataMallocClass, 0, z);
} }
- (unsigned int) capacity - (unsigned int) capacity
@ -1551,17 +1599,17 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len, NSZone* zone)
- (Class) classForArchiver - (Class) classForArchiver
{ {
return [NSMutableDataMalloc class]; return mutableDataMallocClass;
} }
- (Class) classForCoder - (Class) classForCoder
{ {
return [NSMutableDataMalloc class]; return mutableDataMallocClass;
} }
- (Class) classForPortCoder - (Class) classForPortCoder
{ {
return [NSMutableDataMalloc class]; return mutableDataMallocClass;
} }
- (void) dealloc - (void) dealloc
@ -1822,7 +1870,7 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len, NSZone* zone)
{ {
NSLog(@"[NSMutableDataShared -initWithCapacity:] shared memory get failed for %u - %s", bufferSize, strerror(errno)); NSLog(@"[NSMutableDataShared -initWithCapacity:] shared memory get failed for %u - %s", bufferSize, strerror(errno));
[self dealloc]; [self dealloc];
self = [NSMutableDataMalloc alloc]; self = [mutableDataMallocClass alloc];
return [self initWithCapacity: bufferSize]; return [self initWithCapacity: bufferSize];
} }
@ -1833,7 +1881,7 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len, NSZone* zone)
NSLog(@"[NSMutableDataShared -initWithCapacity:] shared memory attach failed for %u - %s", bufferSize, strerror(e)); NSLog(@"[NSMutableDataShared -initWithCapacity:] shared memory attach failed for %u - %s", bufferSize, strerror(e));
bytes = 0; bytes = 0;
[self dealloc]; [self dealloc];
self = [NSMutableDataMalloc alloc]; self = [mutableDataMallocClass alloc];
return [self initWithCapacity: bufferSize]; return [self initWithCapacity: bufferSize];
} }
length = 0; length = 0;