[cfe-dev] Alignment problems in Clang's internal data structures?

Renato Golin renato.golin at linaro.org
Tue Feb 17 07:00:36 PST 2015


On 17 February 2015 at 09:42, Renato Golin <renato.golin at linaro.org> wrote:
>    std::size_t Size = sizeof(DeclRefExpr);
>    std::size_t Align = llvm::alignOf<DeclRefExpr>();
>    if (...) {
>      Size += sizeof(...);
>      Align = std::max(Align, llvm::alignOf<...>());
>    }
>    ...
>    void *Mem = Context.Allocate(Size, Align);

Another thing to consider is the alignment of the beginning of the
extra elements.

Say you have a structure and an array of doubles. If your structure
aligns to 4 and ends at byte 1, you'll have to add 7 bytes padding, or
the access to each of the elements in the double array will be
unaligned. This is, of course, target dependent, but alignOf() should
do the trick.

So, just adding padding to the structure's alignment is not enough,
you have to check the other elements (per element) and add a padding
before each one of them, if needed. The problem this creates is that
the reading end will know nothing of the padding and may need
additional logic to use the padding accordingly.

cheers,
--renato



More information about the cfe-dev mailing list