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