From patchwork Fri Dec 24 18:41:13 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicola Pero X-Patchwork-Id: 76636 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id D4EF5B70A3 for ; Sat, 25 Dec 2010 05:41:33 +1100 (EST) Received: (qmail 7570 invoked by alias); 24 Dec 2010 18:41:31 -0000 Received: (qmail 7561 invoked by uid 22791); 24 Dec 2010 18:41:30 -0000 X-SWARE-Spam-Status: No, hits=-1.4 required=5.0 tests=AWL, BAYES_00, TW_BJ, T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from fencepost.gnu.org (HELO fencepost.gnu.org) (140.186.70.10) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 24 Dec 2010 18:41:23 +0000 Received: from eggs.gnu.org ([140.186.70.92]:51621) by fencepost.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.69) (envelope-from ) id 1PWCZq-0003aY-6M for gcc-patches@gnu.org; Fri, 24 Dec 2010 13:41:18 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PWCZr-0005ND-9O for gcc-patches@gnu.org; Fri, 24 Dec 2010 13:41:21 -0500 Received: from smtp131.iad.emailsrvr.com ([207.97.245.131]:37409) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PWCZr-0005Lp-6R for gcc-patches@gnu.org; Fri, 24 Dec 2010 13:41:19 -0500 Received: from localhost (localhost.localdomain [127.0.0.1]) by smtp33.relay.iad1a.emailsrvr.com (SMTP Server) with ESMTP id BAA54302FA for ; Fri, 24 Dec 2010 13:41:15 -0500 (EST) Received: by smtp33.relay.iad1a.emailsrvr.com (Authenticated sender: nicola.pero-AT-meta-innovation.com) with ESMTPA id 39C6C30168 for ; Fri, 24 Dec 2010 13:41:15 -0500 (EST) Message-Id: <2F993563-D8BD-44F9-8A2D-D9B037E59707@meta-innovation.com> From: Nicola Pero To: gcc-patches@gnu.org Mime-Version: 1.0 (Apple Message framework v936) Subject: libobjc: fix class_addIvar()'s alignment Date: Fri, 24 Dec 2010 19:41:13 +0100 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-IsSubscribed: yes Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org This patch changes class_addIvar() to be compatible with the Apple runtime and understand the alignment argument in the same way as the Apple runtime understands it: as the log2 of the alignment, as opposed to the actual alignment. Committed to trunk. Thanks - __alignof__ (unsigned long), @encode (unsigned long))) + log_2_of (__alignof__ (unsigned long)), @encode (unsigned long))) abort (); objc_registerClassPair (new_class); @@ -135,7 +153,7 @@ abort (); if (! class_addIvar (new_class, "variable_ivar", sizeof (id), - __alignof__ (id), @encode (id))) + log_2_of (__alignof__ (id)), @encode (id))) abort (); if (! class_addMethod (new_class, @selector (setVariable:), method_getImplementation (method1), Index: ChangeLog =================================================================== --- ChangeLog (revision 168229) +++ ChangeLog (working copy) @@ -1,5 +1,12 @@ 2010-12-24 Nicola Pero + * 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 + * objc/runtime.h (sel_getType): Renamed to sel_getTypeEncoding to be consistent with method_getTypeEncoding and ivar_getTypeEncoding. Index: ivars.c =================================================================== --- ivars.c (revision 168228) +++ ivars.c (working copy) @@ -212,7 +212,7 @@ 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) { struct objc_ivar_list *ivars; @@ -270,6 +270,7 @@ size. */ { struct objc_ivar *ivar = &(ivars->ivar_list[ivars->ivar_count - 1]); + unsigned int alignment = 1 << log_2_of_alignment; int misalignment; ivar->ivar_name = objc_malloc (strlen (ivar_name) + 1); Index: objc/runtime.h =================================================================== --- objc/runtime.h (revision 168229) +++ objc/runtime.h (working copy) @@ -352,14 +352,16 @@ using objc_allocateClassPair() and has not been registered with the runtime using objc_registerClassPair() yet. You can not add instance variables to classes already registered with the runtime. - 'size' is the size of the instance variable, 'alignment' the - alignment, and 'type' the type encoding of the variable type. You - can use sizeof(), __alignof__() and @encode() to determine the - right 'size', 'alignment' and 'type' for your instance variable. - For example, to add an instance variable name "my_variable" and of - type 'id', you can use: + 'size' is the size of the instance variable, 'log_2_of_alignment' + the alignment as a power of 2 (so 0 means alignment to a 1 byte + boundary, 1 means alignment to a 2 byte boundary, 2 means alignment + to a 4 byte boundary, etc), and 'type' the type encoding of the + variable type. You can use sizeof(), log2(__alignof__()) and + @encode() to determine the right 'size', 'alignment' and 'type' for + your instance variable. For example, to add an instance variable + name "my_variable" and of type 'id', you can use: - class_addIvar (class, "my_variable", sizeof (id), __alignof__ (id), + class_addIvar (class, "my_variable", sizeof (id), log2 ( __alignof__ (id)), @encode (id)); Return YES if the variable was added, and NO if not. In @@ -368,7 +370,7 @@ 'type' is NULL, or 'size' is 0. */ 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 NULL. */ Index: ChangeLog =================================================================== --- ChangeLog (revision 168229) +++ ChangeLog (working copy) @@ -1,5 +1,11 @@ 2010-12-24 Nicola Pero + * 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 + * objc.dg/gnu-api-2-sel.m: Updated for renaming of sel_getType to sel_getTypeEncoding. Test that sel_getTypeEncoding returns NULL when called with a NULL argument. Added test for Index: objc.dg/gnu-api-2-class.m =================================================================== --- objc.dg/gnu-api-2-class.m (revision 168229) +++ objc.dg/gnu-api-2-class.m (working copy) @@ -62,6 +62,24 @@ - (id) mySelf; @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) { /* Functions are tested in alphabetical order. */ @@ -74,15 +92,15 @@ abort (); if (! class_addIvar (new_class, "variable2_ivar", sizeof (id), - __alignof__ (id), @encode (id))) + log_2_of (__alignof__ (id)), @encode (id))) abort (); 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 (); 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 (); objc_registerClassPair (new_class); @@ -135,7 +153,7 @@ abort (); if (! class_addIvar (new_class, "variable_ivar", sizeof (id), - __alignof__ (id), @encode (id))) + log_2_of (__alignof__ (id)), @encode (id))) abort (); if (! class_addMethod (new_class, @selector (setVariable:), method_getImplementation (method1), Index: obj-c++.dg/gnu-api-2-class.mm =================================================================== --- obj-c++.dg/gnu-api-2-class.mm (revision 168228) +++ obj-c++.dg/gnu-api-2-class.mm (working copy) @@ -62,6 +62,24 @@ - (id) mySelf; @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 () { /* Functions are tested in alphabetical order. */ @@ -74,15 +92,15 @@ abort (); if (! class_addIvar (new_class, "variable2_ivar", sizeof (id), - __alignof__ (id), @encode (id))) + log_2_of (__alignof__ (id)), @encode (id))) abort (); 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 (); if (! class_addIvar (new_class, "variable4_ivar", sizeof (unsigned long),