[PATCH libinput] util: harmonize container_of() definition with linux kernel one

Peter Hutterer peter.hutterer at who-t.net
Wed May 17 05:08:48 UTC 2017


On Mon, May 15, 2017 at 01:08:17PM +0200, Gabriel Laskar wrote:
> 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 at lse.epita.fr>
> ---
> 
> As they were no documentation for all the list functions/macros I did not add
> one for list_first_entry(), if it is necessary, I will add them.

thanks, much appreciated

   cef2a095..20f5f2d9  master -> master

If you want to write the documentation for the list functions, I'll merge
them. I have also had a long-standing task of separating out a lot of the
helpers into their own files where possible and just #including them from
libinput-util.h. Makes it easier to re-use those in other projects.

Cheers,
   Peter

> 
>  src/evdev-lid.c         |  4 +---
>  src/evdev-mt-touchpad.h |  4 +---
>  src/evdev-tablet-pad.h  |  4 +---
>  src/evdev-tablet.h      |  4 +---
>  src/evdev.h             |  8 ++------
>  src/libinput-util.h     | 19 +++++++++++--------
>  6 files changed, 17 insertions(+), 26 deletions(-)
> 
> diff --git a/src/evdev-lid.c b/src/evdev-lid.c
> index baf7185..9e694ba 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 304e92f..ef0171d 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 5569007..c80e144 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 43ed897..7d17e36 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 c9a44f8..2bd58c1 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 6d9bbe2..e34a500 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)
> -- 
> 2.12.2
> 


More information about the wayland-devel mailing list