Re: usb: descriptor structures have to be packed

"Phil Endecott" <[email protected]>
Newsgroups gmane.linux.usb.devel
Message-ID <[email protected]>
David Brownell wrote:
> On Wednesday 07 February 2007 3:04 am, Phil Endecott wrote:
>> I won't pretend to understand all the subtleties of 
>> __attribute__((packed)), but I did learn something about it when I 
>> hacked USB/IP to run on ARM, for the NSLU2, last year.  The issue 
>> boiled down to this:
>> 
>> struct S {
>>    int intfield;
>>    char charfield;
>> } /* maybe add: __attribute__((packed)) here */ ;
>> 
>> f(struct S * s) {
>>    ...
>>    (s->intfield)++;
>>    ...
>> }
>> 
>> ...
>> 
>> What happens when we add __attribute__((packed)) to struct S?  ...
>> will work, but a lot more slowly.  However, the ++ in f() WILL NOT 
>> WORK!  The compiler assumes that the pointer that is passed IS 
>> NATURALLY ALIGNED, and when you pass an unaligned pointer from g() IT 
>> DOES NOT ISSUE A WARNING!  
>
> Looks like a bug in your version of the toolchain.  I just tried
> this not a minute ago (gcc 3.4.3) and it works correctly:  not only
> do both versions of the function handle the "intfield" misaligned
> for loads, they also do so for the store.

Err, maybe my example was over-simplified then, sorry.  Try another 
level of indirection:

void inc(int* i) {
   (*i)++;
}

void f(struct S* s) {
   inc(&(s->intfield);
}


Please look in the gcc-help archives for December and search for my 
name; you'll find a long thread where I slowly come to understand how 
this works (or rather how it doesn't work and how it doesn't warn you 
that it's not going to work).  I believe that the people that I was 
corresponding with know what they are talking about.  In particular, 
here's a quote from Paul Brook (http://gcc.gnu.org/ml/gcc-help/2006-12/msg00115.html):

   " the compiler is allowed to assume that the low 2 bits of an int* 
are zero. "


Phil.





-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
[email protected]
To unsubscribe, use the last form field at:
https://lists.sourceforge.net/lists/listinfo/linux-usb-devel
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.