I am trying to reformat some code that uses a struct aligned by a 4096 byte boundary. The struct is basically a 2d array at each point is 2 different uint16 with some padding at each row.
The way the structs are declared are as follows
frame TemporaryFrame0 __attribute__ ((aligned(4096)));
frame TemporaryFrame1 __attribute__ ((aligned(4096)));
frame TemporaryFrame2 __attribute__ ((aligned(4096)));
I am wanting to declare it as
frame TemporaryFrame[3] __attribute__ ((aligned(4096)));
but when I declare as such, it is not stored in memory properly/ the same.
The struct is as such
typedef struct frame frame;
struct frame {
u8 byte_offset[32];
row row[FRAME_ROW_SIZE];
};
I have also tried to declare the struct as follows with no benefit.
typedef struct frame frame;
struct frame {
u8 byte_offset[32];
row row[FRAME_ROW_SIZE];
}__attribute__ ((aligned(4096)));
Nick Bright is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
frame TemporaryFrame[3] __attribute__ ((aligned(4096)));
says to make the array TemporaryFrame
aligned. Its elements are whatever size they are and are necessarily contiguous in memory. If the size of frame
is X, the address of TemporaryFrame[1]
is X bytes beyond the address of TemporaryFrame[0]
, and it is not 4096-byte aligned unless X is a multiple of 4096.
To make the elements aligned, define struct frame
to be aligned:
struct frame {
u8 byte_offset[32];
row row[FRAME_ROW_SIZE];
} __attribute__ ((aligned(4096)));
It is not possible in C as arrays do not have padding. You need to provide such a padding yourself.
typedef double row[10];
typedef struct frame frame;
struct frame {
struct data
{
char byte_offset[32];
row row[10];
}__attribute__((packed)) data;
char padding[4096 - sizeof(struct data)];
}__attribute__ ((packed,aligned(4096)));
frame farray[3];
int main(void)
{
printf("%pn", (void *)farray);
printf("%pn", (void *)&farray[0]);
printf("%pn", (void *)&farray[1]);
printf("%pn", (void *)&farray[2]);
}
https://godbolt.org/z/n4a5bdbvh