Re: [PATCH v2 08/20] Implement container_of_safe() in terms of container_of()

From: Arnd Bergmann
Date: Sun Nov 21 2021 - 08:32:22 EST


On Sat, Nov 20, 2021 at 2:00 PM Alejandro Colomar
<alx.manpages@xxxxxxxxx> wrote:
>
> There's no more a need for the temporary variable __mptr,
> since now it's only passed to functions that accept a 'const void *',
> and everything can convert automatically to it,
> reducing the need for the cast too.

The purpose of the temporary variable is to avoid evaluating the macro
multiple times, this is still required.

> * If IS_ERR_OR_NULL(ptr), ptr is returned unchanged.
> */
> -#define container_of_safe(ptr, type, member) ({ \
> - void *__mptr = (void *)(ptr); \
> - static_assert(__same_type(*(ptr), ((type *)0)->member) || \
> - __same_type(*(ptr), void), \
> - "pointer type mismatch in container_of_safe()"); \
> - IS_ERR_OR_NULL(__mptr) ? ERR_CAST(__mptr) : \
> - ((type *)(__mptr - offsetof(type, member))); })
> +#define container_of_safe(ptr, type, member) \
> + (IS_ERR_OR_NULL(ptr) ? ERR_CAST(ptr) : container_of(type, member))

It's possible that you may find a way to improve this macro using
__auto_type, as a way to reduce the macro expansion further.

Arnd