antonpuz
antonpuz

Reputation: 3316

confirming the cache aligment of an array

I have an array of structs which i wish to store as cache aligned. I have read about the __CACHELINE_ALIGNED__ and __cacheline_aligned macros. now I wish to confirm that an array of a structs defined with this attribute are truely cache aligned.

struct test{
 int val1;
 int val2;
 int val3;
} __CACHELINE_ALIGNED__;

struct test tArr[2];

of course if i will print the size of tArr[0] i will get 12 so i came up with the following test:

printf("size of first %p, second %p\n", &tArr[0], &tArr[1]);

And I receive pointers located 12 byte apart. Does that mean that the structs are not cache aligned? how can i verify that the lines are really cache aligned.

thanks.

Upvotes: 0

Views: 89

Answers (1)

moorray
moorray

Reputation: 222

There is nothing called __CACHELINE_ALIGNED__ in standard C, where did you read about it? You are just defining a global variable called __CACHELINE_ALIGNED__ with you code.

If you want your structure to be cache line aligned you have to specify the alignment in a way that is standard for your compiler. For instance assuming 64 byte cache line and GCC you would go:

struct test{
 int val1;
 int val2;
 int val3;
} __attribute__ ((aligned (64)));

Upvotes: 2

Related Questions