gnustep-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Alignment issue?


From: David Chisnall
Subject: Re: Alignment issue?
Date: Fri, 5 Jul 2013 14:36:57 +0100

Hi,

This is a bug.  class_createInstance() should create an object with the 
required alignment for the class.  This is complicated, however, by the fact 
that it must store a word-sized pointer before the start of the object to 
contain the retain count.  In general, this works as very few types require 
alignment greater than a word (vectors on some architectures are the only ones 
that I'm aware of, and these are not commonly stored in Objective-C objects).

Note that in the non-fragile ABI, the offset of an instance variable is not 
defined until load time, however the type stored in the instance variable 
metadata does not contain anything to propagate explicitly defined alignment.  
This makes it impossible to implement correctly without changing the ABI.  I 
may do this for clang 3.4, but it won't be done any time soon...

The system malloc(), which class_createInstance() wraps, should return memory 
allocated to the greatest alignment that any of the primitive types support, 
which is 16 bytes on x86.  The correct fix for this is probably to remember 
that the start of the object is 8-byte, but not 16-byte aligned when 
calculating offsets so that the padding can be defined correctly.  An simpler 
fix would be to simply tweak allocate_class to pad the allocation with two 
words, which should guarantee that the alignment remains correct, but will 
affect other classes.  Probably the best compromise is to record the greatest 
required alignment for each class and create padding reflecting this when 
allocating a class.

I'll investigate fixing this, however it is not likely to be trivial.  

David

On 5 Jul 2013, at 14:09, Laurent Michel <address@hidden> wrote:

> Good morning!
> 
> I have ported an application to GNUstep (from MacOS) and everything works 
> very well on a ubuntu 12.04 LTS (32-bit). 
> 
> However, the same port fails under 12.04 LTS (64-bit) for a simple reason:
> 
> On GNUstep the NSAllocateObject function (and therefore all the alloc 
> methods) do _not_ return 
> 16 bytes aligned objects. On Linux, the objects are always 8 bytes aligned. I 
> checked, and my code on Cocoa (MacOS 10.8.4) [the same call to 
> NSAllocateObject] returns 16 bytes aligned objects.
> 
> Either this is a bug, or I'm missing a macro/define somewhere to tell GNUstep 
> I'd like to have 16 bytes aligned objects. 
> 
> Naturally, I can redefine a new method for the meta-class whose instances 
> require a 16 byte alignment, but this is ad-hoc and would be required for 
> every object susceptible of storing a structure expecting 16 bytes 
> alignments. 
> 
> Note that the structure whose field require a 16 byte alignment and which is 
> hosted in instances of that specific class is correctly tagged with a clang 
> attribute to require that the structure start address be correctly aligned. 
> Of course, that _assumes_ that the object itself is 16 bytes aligned which is 
> not the case here. 
> 
> Is there a suitable incantation to get a 16-byte alignment on Linux64 ?  Do 
> you agree that this is a bug ? 
> 
> Thanks for your help,
> 
> All the best,
> 
> 
> --
>   Laurent
> 
> _______________________________________________
> Gnustep-dev mailing list
> address@hidden
> https://lists.gnu.org/mailman/listinfo/gnustep-dev



--
This email complies with ISO 3103




reply via email to

[Prev in Thread] Current Thread [Next in Thread]