Re: [PATCH v2] kernel.h: handle pointers to arrays better in container_of()

From: Ian Abbott
Date: Tue May 23 2017 - 08:03:10 EST


On 23/05/17 12:24, Peter Zijlstra wrote:
On Tue, May 23, 2017 at 11:32:02AM +0100, Ian Abbott wrote:

#define container_of(ptr, type, member) ( \
_Static_assert(__builtin_types_compatible_p( \
typeof(*ptr), typeof( ((type *)0)->member )), "WUT"), \
((type *)((char *)(ptr) - offsetof(type, member))); \
)

It's a fine suggestion (if more parentheses are added), but _Static_assert
is a C11 feature, and I thought the kernel was using gnu89 (unless it's been
updated since).

We have BUILD_BUG_ON() that should be similar in functionality.

Yes, I think BUILD_BUG_ON_ZERO() will be better in this case to avoid some baggage. Also, __same_type() can be used for type checking.

--
-=( Ian Abbott @ MEV Ltd. E-mail: <abbotti@xxxxxxxxx> )=-
-=( Web: http://www.mev.co.uk/ )=-