Use NSGetSizeAndAlignment

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@29798 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Richard Frith-MacDonald 2010-02-28 11:51:59 +00:00
parent c0c7403c0f
commit bbc0ddd60f
2 changed files with 9 additions and 251 deletions

View file

@ -150,11 +150,6 @@ extern "C" {
#endif
/** Returns the size of the data type described by the type encoding string.
*/
GS_EXPORT unsigned
objc_sizeOfType(const char *type);
/*
* Functions for accessing instance variables directly -
* We can copy an ivar into arbitrary data,

View file

@ -70,214 +70,6 @@
do { if (behavior_debug) { fprintf(stderr, (format) , ## args); } } while (0)
GS_EXPORT unsigned
objc_sizeOfType(const char *type)
{
#if __APPLE__
/* Skip the variable name if any */
if (*type == '"')
{
for (type++; *type++ != '"';)
/* do nothing */;
}
switch (*type)
{
#if defined(_C_BOOL)
case _C_BOOL:
return sizeof (_Bool);
break;
#endif
case _C_ID:
return sizeof (id);
break;
case _C_CLASS:
return sizeof (Class);
break;
case _C_SEL:
return sizeof (SEL);
break;
case _C_CHR:
return sizeof (char);
break;
case _C_UCHR:
return sizeof (unsigned char);
break;
case _C_SHT:
return sizeof (short);
break;
case _C_USHT:
return sizeof (unsigned short);
break;
case _C_INT:
return sizeof (int);
break;
case _C_UINT:
return sizeof (unsigned int);
break;
case _C_LNG:
return sizeof (long);
break;
case _C_ULNG:
return sizeof (unsigned long);
break;
case _C_LNG_LNG:
return sizeof (long long);
break;
case _C_ULNG_LNG:
return sizeof (unsigned long long);
break;
case _C_FLT:
return sizeof (float);
break;
case _C_DBL:
return sizeof (double);
break;
case _C_VOID:
return sizeof (void);
break;
case _C_PTR:
case _C_ATOM:
case _C_CHARPTR:
return sizeof (char *);
break;
case _C_ARY_B:
{
int len = atoi (type + 1);
while (isdigit ((unsigned char)*++type))
;
return len * objc_aligned_size (type);
}
break;
case _C_BFLD:
#if !defined(BITS_PER_UNIT)
# define BITS_PER_UNIT (sizeof(int)*8)
#endif
{
/* The new encoding of bitfields is: b 'position' 'type' 'size' */
int position, size;
int startByte, endByte;
position = atoi (type + 1);
while (isdigit ((unsigned char)*++type))
;
size = atoi (type + 1);
startByte = position / BITS_PER_UNIT;
endByte = (position + size) / BITS_PER_UNIT;
return endByte - startByte;
}
case _C_UNION_B:
case _C_STRUCT_B:
{
struct objc_struct_layout layout;
unsigned int size;
objc_layout_structure (type, &layout);
while (objc_layout_structure_next_member (&layout))
/* do nothing */ ;
objc_layout_finish_structure (&layout, &size, NULL);
return size;
}
#if defined(_C_COMPLEX)
case _C_COMPLEX:
{
type++; /* Skip after the 'j'. */
switch (*type)
{
case _C_CHR:
return sizeof (_Complex char);
break;
case _C_UCHR:
return sizeof (_Complex unsigned char);
break;
case _C_SHT:
return sizeof (_Complex short);
break;
case _C_USHT:
return sizeof (_Complex unsigned short);
break;
case _C_INT:
return sizeof (_Complex int);
break;
case _C_UINT:
return sizeof (_Complex unsigned int);
break;
case _C_LNG:
return sizeof (_Complex long);
break;
case _C_ULNG:
return sizeof (_Complex unsigned long);
break;
case _C_LNG_LNG:
return sizeof (_Complex long long);
break;
case _C_ULNG_LNG:
return sizeof (_Complex unsigned long long);
break;
case _C_FLT:
return sizeof (_Complex float);
break;
case _C_DBL:
return sizeof (_Complex double);
break;
default:
{
fprintf(stderr,
"objc_sizeOfType() unknown complex type %s\n", type);
return 0;
}
}
}
#endif
default:
{
fprintf(stderr,
"objc_sizeOfType() unknown type %s\n", type);
return 0;
}
}
#else
return objc_sizeof_type(type);
#endif
}
Class
GSObjCClass(id obj)
{
@ -420,7 +212,6 @@ GSObjCFindVariable(id obj, const char *name,
const char **type, unsigned int *size, int *offset)
{
Class class = object_getClass(obj);
#if 1
Ivar ivar = 0;
while (class != 0 && ivar == 0)
@ -445,7 +236,11 @@ GSObjCFindVariable(id obj, const char *name,
}
if (size != 0)
{
*size = objc_sizeOfType(enc);
NSUInteger s;
NSUInteger a;
NSGetSizeAndAlignment(enc,&s, &a);
*size = s;
}
if (offset != 0)
{
@ -453,41 +248,6 @@ GSObjCFindVariable(id obj, const char *name,
}
return YES;
}
#else /* OBSOLETE */
struct objc_ivar_list *ivars;
struct objc_ivar *ivar = 0;
while (class != nil && ivar == 0)
{
ivars = class->ivars;
class = class->super_class;
if (ivars != 0)
{
int i;
for (i = 0; i < ivars->ivar_count; i++)
{
if (strcmp(ivars->ivar_list[i].ivar_name, name) == 0)
{
ivar = &ivars->ivar_list[i];
break;
}
}
}
}
if (ivar == 0)
{
return NO;
}
if (type)
*type = ivar->ivar_type;
if (size)
*size = objc_sizeOfType(ivar->ivar_type);
if (offset)
*offset = ivar->ivar_offset;
return YES;
#endif
}
/**
@ -796,6 +556,8 @@ GSObjCMakeClass(NSString *name, NSString *superName, NSDictionary *iVars)
{
const char *iVarName = [key cString];
const char *iVarType = [[iVars objectForKey: key] cString];
NSUInteger a;
NSUInteger s;
tmp = objc_malloc(strlen(iVarName) + 1);
strcpy(tmp, iVarName);
@ -806,7 +568,8 @@ GSObjCMakeClass(NSString *name, NSString *superName, NSDictionary *iVars)
// align the ivar (i.e. put it on the first aligned address
ivar->ivar_offset = iVarSize;
iVarSize += objc_sizeOfType(ivar->ivar_type); // add the ivar size
NSGetSizeAndAlignment(ivar->ivar_type, &s, &a);
iVarSize += s; // add the ivar size
ivar = ivar + 1;
}
}