Re: GCC #define for alignment

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

 



Ian Lance Taylor wrote:
> Duane Ellis <duane@xxxxxxxxxxxxxx> writes:
> 
>> I have a problem in a memory allocator that acts like Malloc(),  where
>> I need to align the result value in the same form as malloc.
>>
>> I can easily hard code something :-( but I'd rather use some "well
>> known" perhaps ANSI-C  #define for the alignment value.
>> It could be a GCC specific #define :-( but I'd rather it not be.
>>
>> Simply put, for portability reasons I'd rather not have a hard coded
>> thing like this:
>>
>>    // ensure proper machine alignment
>>    x = (x + 3) & (~3);
>>
>> Suggestions? Sure I could use sizeof(void *) also..
> 
> There is no portable way to do this.  The usual approach is to write
> something like
>     union allocation_unit {
>       long long l;
>       double d;
>     };
> and then do all your allocations in terms of sizeof(union
> allocation_unit).

But that might not work either: I've seen arches where struct/union
members had different alignment requirements from the same objects
outside structs.  IIRC on x86 double is 8-aligned but struct{double}
is 4-aligned.

Andrew.

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux