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();
/*
* 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
@ -158,6 +162,29 @@ fastInstanceIsKindOfClass(NSObject *obj, Class 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)

View file

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

View file

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