Subject: Re: [RFC][PATCH] net drivers and cache alignment

From: "J.A. Magallon" <[email-masked]>
Date: Sun, 8 Dec 2002 00:37:46 +0100

On 2002.12.08 Andrew Morton wrote:
>Jeff Garzik wrote:
>> 
>> David S. Miller wrote:
>> > Can't the cacheline_aligned attribute be applied to individual
>> > struct members?  I remember doing this for thread_struct on
>> > sparc ages ago.
>> 
>> Looks like it from the 2.4 processor.h code.
>> 
>> Attached is cut #2.  Thanks for all the near-instant feedback so far :)
>>   Andrew, does the attached still need padding on SMP?
>

What do you all think about this:

#include <stdio.h>

#define CACHE_LINE_SIZE 128
#define ____cacheline_aligned __attribute__((__aligned__(CACHE_LINE_SIZE)))

#define __cacheline_start   struct { } ____cacheline_aligned;

#define offsetof(t, m)  ((int)(&((t *)0)->m))

struct S {
    __cacheline_start

    int x;

    __cacheline_start

    int y;
    int z;
};

int main()
{
    struct S s;

    printf("%d\n",sizeof(struct S));
    printf("%d\n",offsetof(struct S,x));
    printf("%d\n",offsetof(struct S,y));
    printf("%d\n",offsetof(struct S,z));
}

werewolf:~> vi kk.c
werewolf:~> kk
256
0
128
132

So you don't have to modify any field, just put __cacheline_start where
needed ? (and does not add any extra sizeof(int) overhead).

-- 
J.A. Magallon <[email-masked]>      \                 Software is like sex:
werewolf.able.es                         \           It's better when it's free
Mandrake Linux release 9.1 (Cooker) for i586
Linux 2.4.20-jam1 (gcc 3.2 (Mandrake Linux 9.1 3.2-4mdk))