Re: Using C99 in the kernel was Re: [PATCH 1/19] MUTEX: Introduce simple mutex implementation

Al Viro
2005-12-13T23:05:48Z

On Tue, Dec 13, 2005 at 10:56:10PM +0100, Andi Kleen wrote:
> It looks like casts in constant initializers for global structures are not
> allowed anymore: struct foo x = (struct foo) { ... }; warns. That's
> not good because when the (struct foo){} is generated in a macro
> then it's the only easy way to allow initialization outside a declaration.
>
> Common case is SPIN_LOCK_UNLOCKED() / DEFINE_SPINLOCK().

There are two similar things - struct initializers and compound literals.
They are *not* the same, though; compound literal defines an unnamed
object, so
{
struct foo x = (struct foo){...};

is equivalent to
struct foo unnamed_variable = {....};
struct foo x = unnamed_variable;

For auto variables it's fine; there initializer doesn't have to be
constant. For globals it's _not_.

Note that it's really a definition of object - e.. you can say
f(&(struct foo){....});
and have it work just fine.

IOW, DEFINE_SPINLOCK() should be spinlock_t x = __SPIN_LOCK_UNLOCKED()
and SPIN_LOCK_UNLOCKED - (spinlock_t) __SPIN_LOCK_UNLOCKED. That's
enough to make it valid C99...