[PATCH libinput 1/2] util: use offsetof in container_of

Gabriel Laskar gabriel at lse.epita.fr
Sun May 14 14:33:16 UTC 2017


gcc and clang supports offsetof (defined in stddef.h) as defined by C99
and POSIX.1-2001, use it in container_of.

Signed-off-by: Gabriel Laskar <gabriel at lse.epita.fr>
---
 src/libinput-util.h | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/src/libinput-util.h b/src/libinput-util.h
index 9ecded7..6d9bbe2 100644
--- a/src/libinput-util.h
+++ b/src/libinput-util.h
@@ -34,6 +34,7 @@
 #include <math.h>
 #include <stdarg.h>
 #include <stdbool.h>
+#include <stddef.h>
 #include <stdio.h>
 #include <string.h>
 #include <time.h>
@@ -86,8 +87,8 @@ void list_remove(struct list *elm);
 bool list_empty(const struct list *list);
 
 #define container_of(ptr, sample, member)				\
-	(__typeof__(sample))((char *)(ptr)	-			\
-		 ((char *)&((typeof(sample))0)->member))
+	(__typeof__(sample))((char *)(ptr) -				\
+		 offsetof(__typeof__(*sample), member))
 
 #define list_for_each(pos, head, member)				\
 	for (pos = 0, pos = container_of((head)->next, pos, member);	\
-- 
2.12.2



More information about the wayland-devel mailing list