Search code examples
cstructmagenta

C Structs with three options in the magenta kernel


In the magenta kernel there is a paragraph in which struct got not only a type and name but one option more. I found in the references nothing to explain that syntax. So what is __CPU_ALIGN as argument in struct for and where do I find the syntax for it?

struct type name ???

#if WITH_SMP
/* a global state structure, aligned on cpu cache line to minimize aliasing */
struct mp_state mp __CPU_ALIGN = {
    .hotplug_lock = MUTEX_INITIAL_VALUE(mp.hotplug_lock),
    .ipi_task_lock = SPIN_LOCK_INITIAL_VALUE,
};

I know that __CPU_ALIGN itself is used to have aligned bytes for the CPU memory size.


Solution

  • It's a macro shorthand for the aligned attribute, which is a GCC extension.

    The macro is defined as follows:

    #define __CPU_ALIGN __ALIGNED(CACHE_LINE)
    

    The macro __ALIGNED in turn is defined like this:

    #define __ALIGNED(x) __attribute__((aligned(x)))
    

    ...which matches the syntax in the GCC documentation. (The value of CACHE_LINE depends on the architecture.)