util: harmonize container_of() definition with linux kernel one
authorGabriel Laskar <gabriel@lse.epita.fr>
Mon, 15 May 2017 11:08:17 +0000 (13:08 +0200)
committerPeter Hutterer <peter.hutterer@who-t.net>
Wed, 17 May 2017 05:04:41 +0000 (15:04 +1000)
commit 3925936 introduced changes to container_of, this is hopefully the
last part of it.

In the linux kernel, container_of() takes a type name, and not a
variable. Without this, in some cases it is needed to declare an unused
variable in order to call container_of().

example:

return container_of(dispatch, struct fallback_dispatch, base);

instead of:

struct fallback_dispatch *p;
return container_of(dispatch, p, base);

This introduce also list_first_entry(), a simple wrapper around
container_of() to retrieve the first element of a non empty list. It
allows to simplify list_for_each() and list_for_each_safe().

Signed-off-by: Gabriel Laskar <gabriel@lse.epita.fr>
Reviewed-by: Peter Hutterer <peter.hutterer@who-t.net>
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
src/evdev-lid.c
src/evdev-mt-touchpad.h
src/evdev-tablet-pad.h
src/evdev-tablet.h
src/evdev.h
src/libinput-util.h

index baf718519ee686312808ce934b35fc690deca351..9e694ba1096a05f3f0118eb6545eee875f064c9a 100644 (file)
@@ -44,11 +44,9 @@ struct lid_switch_dispatch {
 static inline struct lid_switch_dispatch*
 lid_dispatch(struct evdev_dispatch *dispatch)
 {
-       struct lid_switch_dispatch *l;
-
        evdev_verify_dispatch_type(dispatch, DISPATCH_LID_SWITCH);
 
-       return container_of(dispatch, l, base);
+       return container_of(dispatch, struct lid_switch_dispatch, base);
 }
 
 static void
index 304e92f83e4b1ed043d0fef856a543e6524e5525..ef0171d40ec2680fc10ce7315b7eb6331bbd20a3 100644 (file)
@@ -391,11 +391,9 @@ struct tp_dispatch {
 static inline struct tp_dispatch*
 tp_dispatch(struct evdev_dispatch *dispatch)
 {
-       struct tp_dispatch *tp;
-
        evdev_verify_dispatch_type(dispatch, DISPATCH_TOUCHPAD);
 
-       return container_of(dispatch, tp, base);
+       return container_of(dispatch, struct tp_dispatch, base);
 }
 
 #define tp_for_each_touch(_tp, _t) \
index 55690071e27b4d3142708d114090773ebaa3fdde..c80e144b07377c553c5e5f76ae5a233febdb2ae9 100644 (file)
@@ -73,11 +73,9 @@ struct pad_dispatch {
 static inline struct pad_dispatch*
 pad_dispatch(struct evdev_dispatch *dispatch)
 {
-       struct pad_dispatch *p;
-
        evdev_verify_dispatch_type(dispatch, DISPATCH_TABLET_PAD);
 
-       return container_of(dispatch, p, base);
+       return container_of(dispatch, struct pad_dispatch, base);
 }
 
 static inline struct libinput *
index 43ed89789a1bfb737008f10b5cce7d2c456f6505..7d17e3669219301ce505ed980d54d32edaf8acaa 100644 (file)
@@ -88,11 +88,9 @@ struct tablet_dispatch {
 static inline struct tablet_dispatch*
 tablet_dispatch(struct evdev_dispatch *dispatch)
 {
-       struct tablet_dispatch *t;
-
        evdev_verify_dispatch_type(dispatch, DISPATCH_TABLET);
 
-       return container_of(dispatch, t, base);
+       return container_of(dispatch, struct tablet_dispatch, base);
 }
 
 static inline enum libinput_tablet_tool_axis
index c9a44f8f3f2029f3202cb432e8e80344a4196561..2bd58c139057ff663e89a92d665f395d4f997d59 100644 (file)
@@ -247,9 +247,7 @@ struct evdev_device {
 static inline struct evdev_device *
 evdev_device(struct libinput_device *device)
 {
-       struct evdev_device *d;
-
-       return container_of(device, d, base);
+       return container_of(device, struct evdev_device, base);
 }
 
 #define EVDEV_UNHANDLED_DEVICE ((struct evdev_device *) 1)
@@ -371,11 +369,9 @@ struct fallback_dispatch {
 static inline struct fallback_dispatch*
 fallback_dispatch(struct evdev_dispatch *dispatch)
 {
-       struct fallback_dispatch *f;
-
        evdev_verify_dispatch_type(dispatch, DISPATCH_FALLBACK);
 
-       return container_of(dispatch, f, base);
+       return container_of(dispatch, struct fallback_dispatch, base);
 }
 
 struct evdev_device *
index 6d9bbe23bec79eb4a0d133ed2abdaf6ef6c53b6b..e34a50020c38b81fbd2698afb0a4a788b873f826 100644 (file)
@@ -86,22 +86,25 @@ void list_insert(struct list *list, struct list *elm);
 void list_remove(struct list *elm);
 bool list_empty(const struct list *list);
 
-#define container_of(ptr, sample, member)                              \
-       (__typeof__(sample))((char *)(ptr) -                            \
-                offsetof(__typeof__(*sample), member))
+#define container_of(ptr, type, member)                                        \
+       (__typeof__(type) *)((char *)(ptr) -                            \
+                offsetof(__typeof__(type), member))
+
+#define list_first_entry(head, pos, member)                            \
+       container_of((head)->next, __typeof__(*pos), member)
 
 #define list_for_each(pos, head, member)                               \
-       for (pos = 0, pos = container_of((head)->next, pos, member);    \
+       for (pos = 0, pos = list_first_entry(head, pos, member);        \
             &pos->member != (head);                                    \
-            pos = container_of(pos->member.next, pos, member))
+            pos = list_first_entry(&pos->member, pos, member))
 
 #define list_for_each_safe(pos, tmp, head, member)                     \
        for (pos = 0, tmp = 0,                                          \
-            pos = container_of((head)->next, pos, member),             \
-            tmp = container_of((pos)->member.next, tmp, member);       \
+            pos = list_first_entry(head, pos, member),                 \
+            tmp = list_first_entry(&pos->member, tmp, member);         \
             &pos->member != (head);                                    \
             pos = tmp,                                                 \
-            tmp = container_of(pos->member.next, tmp, member))
+            tmp = list_first_entry(&pos->member, tmp, member))
 
 #define NBITS(b) (b * 8)
 #define LONG_BITS (sizeof(long) * 8)