Re: GCC #define for alignment

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

 



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).  However, with the vector support on modern
processors, this fails unless you start using processor specific
vector types in the union.

If you permit gcc extensions, you can use the aligned attribute:
    int i __attribute__ ((aligned));
This will give i the maximum alignment required by the processor.  You
can then use __alignof__ to inquire about the alignment of i, and use
that to adjust your addresses.

Ian

[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