From: Gabriel Laskar Date: Mon, 15 May 2017 11:08:17 +0000 (+0200) Subject: util: harmonize container_of() definition with linux kernel one X-Git-Tag: 1.7.901~53 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=20f5f2d962037888a1a00f651efb8d38f0480e57;p=platform%2Fupstream%2Flibinput.git util: harmonize container_of() definition with linux kernel one 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 Reviewed-by: Peter Hutterer Signed-off-by: Peter Hutterer --- diff --git a/src/evdev-lid.c b/src/evdev-lid.c index baf71851..9e694ba1 100644 --- a/src/evdev-lid.c +++ b/src/evdev-lid.c @@ -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 diff --git a/src/evdev-mt-touchpad.h b/src/evdev-mt-touchpad.h index 304e92f8..ef0171d4 100644 --- a/src/evdev-mt-touchpad.h +++ b/src/evdev-mt-touchpad.h @@ -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) \ diff --git a/src/evdev-tablet-pad.h b/src/evdev-tablet-pad.h index 55690071..c80e144b 100644 --- a/src/evdev-tablet-pad.h +++ b/src/evdev-tablet-pad.h @@ -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 * diff --git a/src/evdev-tablet.h b/src/evdev-tablet.h index 43ed8978..7d17e366 100644 --- a/src/evdev-tablet.h +++ b/src/evdev-tablet.h @@ -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 diff --git a/src/evdev.h b/src/evdev.h index c9a44f8f..2bd58c13 100644 --- a/src/evdev.h +++ b/src/evdev.h @@ -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 * diff --git a/src/libinput-util.h b/src/libinput-util.h index 6d9bbe23..e34a5002 100644 --- a/src/libinput-util.h +++ b/src/libinput-util.h @@ -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)