[PATCH 10/17] linux/container_of.h: Implement container_of_safe() in terms of container_of()

From: Alejandro Colomar
Date: Fri Nov 19 2021 - 06:37:45 EST


Avoid duplicate code. There's only one different statement. Let it be so.

Note: I'm not sure if we really need a void pointer for IS_ERR_OR_NULL(),
or we could remove that line altogether.

Signed-off-by: Alejandro Colomar <alx.manpages@xxxxxxxxx>
---
include/linux/container_of.h | 11 ++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/include/linux/container_of.h b/include/linux/container_of.h
index 45aa73f5e392..addd3993fa60 100644
--- a/include/linux/container_of.h
+++ b/include/linux/container_of.h
@@ -30,12 +30,13 @@
*
* If IS_ERR_OR_NULL(ptr), ptr is returned unchanged.
*/
-#define container_of_safe(ptr, type, member) ({ \
+#define container_of_safe(ptr, type, member) ( \
+{ \
void *__mptr = (void *)(ptr); \
- static_assert(__same_type(*(ptr), memberof(type, 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))); })
+ container_of(ptr, type, member); \
+} \
+)

#endif /* _LINUX_CONTAINER_OF_H */
--
2.33.1