4

I know that you can align variables to a cache line by using for example attribute((align(64))) in gcc. However, I'm interested in aligning (or you could call it padding) at structure declaration time. So for example, for the following struct I want to ask the compiler to create necessary padding so that any object of this structure is always aligned with a cache line.

typedef struct
{
 int a;
 int b;
 // I want the compiler to create a padding here for cache alignment
} my_type;
pythonic
  • 20,589
  • 43
  • 136
  • 219
  • Add a big array of characters in the middle?.. – Sergey Kalinichenko Apr 13 '12 at 16:14
  • Does this answer your question:http://stackoverflow.com/questions/1744407/cache-line-alignment-need-clarification-on-article ? – N_A Apr 13 '12 at 16:14
  • possible duplicate of [Aligning to cache line and knowing the cache line size](http://stackoverflow.com/questions/7281699/aligning-to-cache-line-and-knowing-the-cache-line-size) – Mat Apr 13 '12 at 16:16
  • Mat: That question is about aligning variables, here I'm talking about padding in a struct to make it align to the cache line, so its different! – pythonic Apr 13 '12 at 16:34
  • i think gcc now supports #pragma [pack](http://gcc.gnu.org/onlinedocs/gcc/Structure_002dPacking-Pragmas.html) – violet313 Apr 13 '12 at 16:35

2 Answers2

3

Yes. I can't remember where I got this code from. I think it might have been Herb Sutter's blog:

    #define CACHE_LINE_SIZE 64 // Intel Core 2 cache line size.

    template<typename T>
    struct CacheLineStorage {

    public:

       [[ align(CACHE_LINE_SIZE) ]] T data;

    private:

       char pad[ CACHE_LINE_SIZE > sizeof(T)
            ? CACHE_LINE_SIZE - sizeof(T)
            : 1 ];
    };
Robinson
  • 9,666
  • 16
  • 71
  • 115
  • Sutter's blog referenced here: http://stackoverflow.com/questions/1744407/cache-line-alignment-need-clarification-on-article – N_A Apr 13 '12 at 16:15
  • Why is there char pad[1] when CACHE_LINE_SIZE <= sizeof(T)? Ah maybe because you can't declare char pad[0] :)! – pythonic Apr 13 '12 at 16:26
  • @user1018562 : What's the alternative? 0-length C-arrays are illegal. – ildjarn Apr 13 '12 at 16:28
  • Seems that 0 length arrays are perfectly legal in C++, http://stackoverflow.com/questions/295027/array-of-zero-length! – pythonic Apr 13 '12 at 16:30
  • 1
    @user1018562 : That answer is wrong -- the C++ standard says otherwise (§8.3.4/1): "*If the constant-expression is present, it shall be an integral constant expression and its value shall be greater than zero.*" – ildjarn Apr 13 '12 at 16:37
2

This is straightforward. You probably just missed the "ed" in aligned.

typedef struct
{
 int a __attribute__((aligned(64)));
 int b;

} my_type;

The resulting struct will have a 56 byte padding after b if you create variables or an array of it.

Gunther Piez
  • 29,760
  • 6
  • 71
  • 103
  • The question specifically was to make "any object of this structure is always aligned", so the attributes should be present for both members, making this 128 byte structure (on x86 in 64-bit mode at least). – Vlad Dec 27 '22 at 15:23