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 <gabriel@lse.epita.fr>
Reviewed-by: Peter Hutterer <peter.hutterer@who-t.net>
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
This commit is contained in:
Gabriel Laskar 2017-05-15 13:08:17 +02:00 committed by Peter Hutterer
parent cef2a09524
commit 20f5f2d962
6 changed files with 17 additions and 26 deletions

View 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

View 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) \

View 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 *

View 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

View 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 *

View 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)