I am working with the ARM compiler and have a HW peripheral (having direct memory access) which is requiring a specific alignment for the memory buffers passed to it (32-byte alignment). This is not a problem when the buffers are global/static and can be defined using the aligned
attribute the compiler is supporting. The problem is arising whenever there is a need to pass a buffer defined in some function locally, i.e. having an automatic storage class. I tried to do something similar to following:
typedef struct __attribute__((aligned(32)))
{
char bytes[32];
} aligned_t;
_Static_assert(sizeof(aligned_t)==32, "Bad size");
void foo(void)
{
aligned_t alignedArray[NEEDED_SIZE/sizeof(aligned_t)];
//.... use alignedArray
}
and this was happily compiled and working on x86 compiler. But not in armcc, which is complaining:
Warning: #1041-D: alignment for an auto object may not be larger than 8
So this approach does not work. There is another one, which I consider ugly:
void foo(void)
{
char unalignedBuffer[NEEDED_SIZE + 32 - 1];
char pAlignedBuffer = ALIGN_UP_32(unalignedBuffer);
//.... use pAlignedBuffer
}
while the ALIGN_UP_32
is a macro to return the first aligned address within unalignedBuffer
(implementation details are not important here I guess).
As I said, I don't like this approach and wondering if there is a more elegant way to achieve the same?