Commit 410644c4 by Nicola Pero Committed by Nicola Pero

In libobjc/: 2010-12-24 Nicola Pero <nicola.pero@meta-innovation.com>

In libobjc/:
2010-12-24  Nicola Pero  <nicola.pero@meta-innovation.com>

	* objc/runtime.h (class_addIvar): Updated documentation.  The
	alignment is actually the log_2 of the alignment in bytes.
	* ivars.c (class_addIvar): Corresponding change to the
	implementation.

In gcc/testsuite/:
2010-12-24  Nicola Pero  <nicola.pero@meta-innovation.com>

	* objc.dg/gnu-api-2-class.m: Updated test to pass log_2 of the
	alignment to class_addIvar, instead of the alignment itself.
	* obj-c++.dg/gnu-api-2-class.mm: Same change.

From-SVN: r168230
parent 5750872c
2010-12-24 Nicola Pero <nicola.pero@meta-innovation.com> 2010-12-24 Nicola Pero <nicola.pero@meta-innovation.com>
* objc.dg/gnu-api-2-class.m: Updated test to pass log_2 of the
alignment to class_addIvar, instead of the alignment itself.
* obj-c++.dg/gnu-api-2-class.mm: Same change.
2010-12-24 Nicola Pero <nicola.pero@meta-innovation.com>
* objc.dg/gnu-api-2-sel.m: Updated for renaming of sel_getType to * objc.dg/gnu-api-2-sel.m: Updated for renaming of sel_getType to
sel_getTypeEncoding. Test that sel_getTypeEncoding returns NULL sel_getTypeEncoding. Test that sel_getTypeEncoding returns NULL
when called with a NULL argument. Added test for when called with a NULL argument. Added test for
......
...@@ -62,6 +62,24 @@ ...@@ -62,6 +62,24 @@
- (id) mySelf; - (id) mySelf;
@end @end
/* Hack to calculate the log2 of a byte alignment. */
unsigned char
log_2_of (unsigned int x)
{
unsigned char result = 0;
/* We count how many times we need to divide by 2 before we reach 1.
This algorithm is good enough for the small numbers (such as 8,
16 or 64) that we have to deal with. */
while (x > 1)
{
x = x / 2;
result++;
}
return result;
}
int main () int main ()
{ {
/* Functions are tested in alphabetical order. */ /* Functions are tested in alphabetical order. */
...@@ -74,15 +92,15 @@ int main () ...@@ -74,15 +92,15 @@ int main ()
abort (); abort ();
if (! class_addIvar (new_class, "variable2_ivar", sizeof (id), if (! class_addIvar (new_class, "variable2_ivar", sizeof (id),
__alignof__ (id), @encode (id))) log_2_of (__alignof__ (id)), @encode (id)))
abort (); abort ();
if (! class_addIvar (new_class, "variable3_ivar", sizeof (unsigned char), if (! class_addIvar (new_class, "variable3_ivar", sizeof (unsigned char),
__alignof__ (unsigned char), @encode (unsigned char))) log_2_of (__alignof__ (unsigned char)), @encode (unsigned char)))
abort (); abort ();
if (! class_addIvar (new_class, "variable4_ivar", sizeof (unsigned long), if (! class_addIvar (new_class, "variable4_ivar", sizeof (unsigned long),
__alignof__ (unsigned long), @encode (unsigned long))) log_2_of (__alignof__ (unsigned long)), @encode (unsigned long)))
abort (); abort ();
objc_registerClassPair (new_class); objc_registerClassPair (new_class);
...@@ -135,7 +153,7 @@ int main () ...@@ -135,7 +153,7 @@ int main ()
abort (); abort ();
if (! class_addIvar (new_class, "variable_ivar", sizeof (id), if (! class_addIvar (new_class, "variable_ivar", sizeof (id),
__alignof__ (id), @encode (id))) log_2_of (__alignof__ (id)), @encode (id)))
abort (); abort ();
if (! class_addMethod (new_class, @selector (setVariable:), method_getImplementation (method1), if (! class_addMethod (new_class, @selector (setVariable:), method_getImplementation (method1),
......
...@@ -62,6 +62,24 @@ ...@@ -62,6 +62,24 @@
- (id) mySelf; - (id) mySelf;
@end @end
/* Hack to calculate the log2 of a byte alignment. */
unsigned char
log_2_of (unsigned int x)
{
unsigned char result = 0;
/* We count how many times we need to divide by 2 before we reach 1.
This algorithm is good enough for the small numbers (such as 8,
16 or 64) that we have to deal with. */
while (x > 1)
{
x = x / 2;
result++;
}
return result;
}
int main(int argc, void **args) int main(int argc, void **args)
{ {
/* Functions are tested in alphabetical order. */ /* Functions are tested in alphabetical order. */
...@@ -74,15 +92,15 @@ int main(int argc, void **args) ...@@ -74,15 +92,15 @@ int main(int argc, void **args)
abort (); abort ();
if (! class_addIvar (new_class, "variable2_ivar", sizeof (id), if (! class_addIvar (new_class, "variable2_ivar", sizeof (id),
__alignof__ (id), @encode (id))) log_2_of (__alignof__ (id)), @encode (id)))
abort (); abort ();
if (! class_addIvar (new_class, "variable3_ivar", sizeof (unsigned char), if (! class_addIvar (new_class, "variable3_ivar", sizeof (unsigned char),
__alignof__ (unsigned char), @encode (unsigned char))) log_2_of (__alignof__ (unsigned char)), @encode (unsigned char)))
abort (); abort ();
if (! class_addIvar (new_class, "variable4_ivar", sizeof (unsigned long), if (! class_addIvar (new_class, "variable4_ivar", sizeof (unsigned long),
__alignof__ (unsigned long), @encode (unsigned long))) log_2_of (__alignof__ (unsigned long)), @encode (unsigned long)))
abort (); abort ();
objc_registerClassPair (new_class); objc_registerClassPair (new_class);
...@@ -135,7 +153,7 @@ int main(int argc, void **args) ...@@ -135,7 +153,7 @@ int main(int argc, void **args)
abort (); abort ();
if (! class_addIvar (new_class, "variable_ivar", sizeof (id), if (! class_addIvar (new_class, "variable_ivar", sizeof (id),
__alignof__ (id), @encode (id))) log_2_of (__alignof__ (id)), @encode (id)))
abort (); abort ();
if (! class_addMethod (new_class, @selector (setVariable:), method_getImplementation (method1), if (! class_addMethod (new_class, @selector (setVariable:), method_getImplementation (method1),
......
2010-12-24 Nicola Pero <nicola.pero@meta-innovation.com> 2010-12-24 Nicola Pero <nicola.pero@meta-innovation.com>
* objc/runtime.h (class_addIvar): Updated documentation. The
alignment is actually the log_2 of the alignment in bytes.
* ivars.c (class_addIvar): Corresponding change to the
implementation.
2010-12-24 Nicola Pero <nicola.pero@meta-innovation.com>
* objc/runtime.h (sel_getType): Renamed to sel_getTypeEncoding to * objc/runtime.h (sel_getType): Renamed to sel_getTypeEncoding to
be consistent with method_getTypeEncoding and be consistent with method_getTypeEncoding and
ivar_getTypeEncoding. ivar_getTypeEncoding.
......
...@@ -212,7 +212,7 @@ struct objc_ivar ** class_copyIvarList (Class class_, unsigned int *numberOfRetu ...@@ -212,7 +212,7 @@ struct objc_ivar ** class_copyIvarList (Class class_, unsigned int *numberOfRetu
BOOL BOOL
class_addIvar (Class class_, const char * ivar_name, size_t size, class_addIvar (Class class_, const char * ivar_name, size_t size,
unsigned char alignment, const char *type) unsigned char log_2_of_alignment, const char *type)
{ {
struct objc_ivar_list *ivars; struct objc_ivar_list *ivars;
...@@ -270,6 +270,7 @@ class_addIvar (Class class_, const char * ivar_name, size_t size, ...@@ -270,6 +270,7 @@ class_addIvar (Class class_, const char * ivar_name, size_t size,
size. */ size. */
{ {
struct objc_ivar *ivar = &(ivars->ivar_list[ivars->ivar_count - 1]); struct objc_ivar *ivar = &(ivars->ivar_list[ivars->ivar_count - 1]);
unsigned int alignment = 1 << log_2_of_alignment;
int misalignment; int misalignment;
ivar->ivar_name = objc_malloc (strlen (ivar_name) + 1); ivar->ivar_name = objc_malloc (strlen (ivar_name) + 1);
......
...@@ -352,14 +352,16 @@ objc_EXPORT Ivar * class_copyIvarList (Class class_, unsigned int *numberOfRetur ...@@ -352,14 +352,16 @@ objc_EXPORT Ivar * class_copyIvarList (Class class_, unsigned int *numberOfRetur
using objc_allocateClassPair() and has not been registered with the using objc_allocateClassPair() and has not been registered with the
runtime using objc_registerClassPair() yet. You can not add runtime using objc_registerClassPair() yet. You can not add
instance variables to classes already registered with the runtime. instance variables to classes already registered with the runtime.
'size' is the size of the instance variable, 'alignment' the 'size' is the size of the instance variable, 'log_2_of_alignment'
alignment, and 'type' the type encoding of the variable type. You the alignment as a power of 2 (so 0 means alignment to a 1 byte
can use sizeof(), __alignof__() and @encode() to determine the boundary, 1 means alignment to a 2 byte boundary, 2 means alignment
right 'size', 'alignment' and 'type' for your instance variable. to a 4 byte boundary, etc), and 'type' the type encoding of the
For example, to add an instance variable name "my_variable" and of variable type. You can use sizeof(), log2(__alignof__()) and
type 'id', you can use: @encode() to determine the right 'size', 'alignment' and 'type' for
your instance variable. For example, to add an instance variable
class_addIvar (class, "my_variable", sizeof (id), __alignof__ (id), name "my_variable" and of type 'id', you can use:
class_addIvar (class, "my_variable", sizeof (id), log2 ( __alignof__ (id)),
@encode (id)); @encode (id));
Return YES if the variable was added, and NO if not. In Return YES if the variable was added, and NO if not. In
...@@ -368,7 +370,7 @@ objc_EXPORT Ivar * class_copyIvarList (Class class_, unsigned int *numberOfRetur ...@@ -368,7 +370,7 @@ objc_EXPORT Ivar * class_copyIvarList (Class class_, unsigned int *numberOfRetur
'type' is NULL, or 'size' is 0. 'type' is NULL, or 'size' is 0.
*/ */
objc_EXPORT BOOL class_addIvar (Class class_, const char * ivar_name, size_t size, objc_EXPORT BOOL class_addIvar (Class class_, const char * ivar_name, size_t size,
unsigned char alignment, const char *type); unsigned char log_2_of_alignment, const char *type);
/* Return the name of the property. Return NULL if 'property' is /* Return the name of the property. Return NULL if 'property' is
NULL. */ NULL. */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment