Linux-Renesas-SoC Archive mirror
 help / color / mirror / Atom feed
From: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
To: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
Cc: linux-media@vger.kernel.org, linux-renesas-soc@vger.kernel.org,
	Sakari Ailus <sakari.ailus@iki.fi>,
	Hans Verkuil <hverkuil-cisco@xs4all.nl>,
	Dafna Hirschfeld <dafna@fastmail.com>,
	linux-rockchip@lists.infradead.org
Subject: Re: [PATCH v2 1/2] media: v4l2-subdev: Provide const-aware subdev state accessors
Date: Tue, 7 May 2024 10:00:35 +0300	[thread overview]
Message-ID: <20240507070035.GA27500@pendragon.ideasonboard.com> (raw)
In-Reply-To: <1114679f-841e-4dca-92f8-3f4e4735bfac@ideasonboard.com>

On Tue, May 07, 2024 at 09:46:08AM +0300, Tomi Valkeinen wrote:
> Hi,
> 
> On 07/05/2024 09:03, Laurent Pinchart wrote:
> > It would be useful to mark instances of v4l2_subdev_state structures as
> > const when code needs to access them read-only. This isn't currently
> > possible, as the v4l2_subdev_state_get_*() accessor functions take a
> > non-const pointer to the state.
> > 
> > Use _Generic() to provide two different versions of the accessors, for
> > const and non-const states respectively. The former returns a const
> > pointer to the requested format, rectangle or interval, implementing
> > const-correctness. The latter returns a non-const pointer, preserving
> > the current behaviour for drivers.
> > 
> > Signed-off-by: Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>
> > ---
> > Changes since v1:
> > 
> > - Wrap the accessors with _Generic() using a single macro instead of
> >    adding a _Generic() statement in each of them.
> > ---
> >   include/media/v4l2-subdev.h | 40 +++++++++++++++++++++++++------------
> >   1 file changed, 27 insertions(+), 13 deletions(-)
> > 
> > diff --git a/include/media/v4l2-subdev.h b/include/media/v4l2-subdev.h
> > index e30c463d90e5..c8cfa1eaa82f 100644
> > --- a/include/media/v4l2-subdev.h
> > +++ b/include/media/v4l2-subdev.h
> > @@ -1326,6 +1326,16 @@ void v4l2_subdev_cleanup(struct v4l2_subdev *sd);
> >   #define __v4l2_subdev_state_gen_call(NAME, _1, ARG, ...)	\
> >   	__v4l2_subdev_state_get_ ## NAME ## ARG
> >   
> > +/*
> > + * A macro to constify the return value of the state accessors when the state
> > + * parameter is const.
> > + */
> > +#define __v4l2_subdev_state_constify_call(state, value)			\
> > +	_Generic(state,							\
> > +		const typeof(*(state)) *: (const typeof(value))value,	\
> 
> Why is the first typeof not typeof(state)?

Wouldn't that be "const const struct v4l2_subdev_state *" when the state
is const ?

> > +		default: value						\
> > +	)
> 
> For non-const state this expands to:
> 
> _Generic(state,
> 	const struct v4l2_subdev_state *: (const struct valuetype *)value,
> 	default: value
> )
> 
> And for const state:
> 
> _Generic(state,
> 	const const struct v4l2_subdev_state *: (const struct valuetype *)value,
> 	default: value
> )

Indeed it would :-)

> 
> Afaics, that works as the double-const is just considered a single-const, but the
> macro looks like as if you use typeof(state) thinking that it would always produce
> the non-const base type. Also, the macro would not work if the default case was
> "typeof(*(state)) *".

Indeed.

> Is there a reason to use the typeof()? As the result of typeof depends on the state
> type, the macro also changes depending on the state type, which I feel is an extra,
> unnecessary, complication in an already not-so-clear system.
> 
> Why not just use "struct v4l2_subdev_state" directly? Or if typeof() is used, maybe
> explain in a comment that the "double-constifying" is intended, and it happens to
> work because we have the default case (instead of explicit non-const case).

Using the explicit type works for me.

> >   /**
> >    * v4l2_subdev_state_get_format() - Get pointer to a stream format
> >    * @state: subdevice state
> > @@ -1340,16 +1350,17 @@ void v4l2_subdev_cleanup(struct v4l2_subdev *sd);
> >    */
> >   /*
> >    * Wrap v4l2_subdev_state_get_format(), allowing the function to be called with
> > - * two or three arguments. The purpose of the __v4l2_subdev_state_get_format()
> > + * two or three arguments. The purpose of the __v4l2_subdev_state_gen_call()
> >    * macro below is to come up with the name of the function or macro to call,
> >    * using the last two arguments (_stream and _pad). The selected function or
> >    * macro is then called using the arguments specified by the caller. A similar
> >    * arrangement is used for v4l2_subdev_state_crop() and
> >    * v4l2_subdev_state_compose() below.
> >    */
> > -#define v4l2_subdev_state_get_format(state, pad, ...)			\
> > -	__v4l2_subdev_state_gen_call(format, ##__VA_ARGS__, , _pad)	\
> > -		(state, pad, ##__VA_ARGS__)
> > +#define v4l2_subdev_state_get_format(state, pad, ...)				\
> > +	__v4l2_subdev_state_constify_call(state,				\
> > +		__v4l2_subdev_state_gen_call(format, ##__VA_ARGS__, , _pad)	\
> > +			((struct v4l2_subdev_state *)state, pad, ##__VA_ARGS__))
> >   #define __v4l2_subdev_state_get_format_pad(state, pad)	\
> >   	__v4l2_subdev_state_get_format(state, pad, 0)
> >   struct v4l2_mbus_framefmt *
> > @@ -1368,9 +1379,10 @@ __v4l2_subdev_state_get_format(struct v4l2_subdev_state *state,
> >    * For stream-unaware drivers the crop rectangle for the corresponding pad is
> >    * returned. If the pad does not exist, NULL is returned.
> >    */
> > -#define v4l2_subdev_state_get_crop(state, pad, ...)			\
> > -	__v4l2_subdev_state_gen_call(crop, ##__VA_ARGS__, , _pad)	\
> > -		(state, pad, ##__VA_ARGS__)
> > +#define v4l2_subdev_state_get_crop(state, pad, ...)				\
> > +	__v4l2_subdev_state_constify_call(state,				\
> > +		__v4l2_subdev_state_gen_call(crop, ##__VA_ARGS__, , _pad)	\
> > +			((struct v4l2_subdev_state *)state, pad, ##__VA_ARGS__))
> >   #define __v4l2_subdev_state_get_crop_pad(state, pad)	\
> >   	__v4l2_subdev_state_get_crop(state, pad, 0)
> >   struct v4l2_rect *
> > @@ -1389,9 +1401,10 @@ __v4l2_subdev_state_get_crop(struct v4l2_subdev_state *state, unsigned int pad,
> >    * For stream-unaware drivers the compose rectangle for the corresponding pad is
> >    * returned. If the pad does not exist, NULL is returned.
> >    */
> > -#define v4l2_subdev_state_get_compose(state, pad, ...)			\
> > -	__v4l2_subdev_state_gen_call(compose, ##__VA_ARGS__, , _pad)	\
> > -		(state, pad, ##__VA_ARGS__)
> > +#define v4l2_subdev_state_get_compose(state, pad, ...)				\
> > +	__v4l2_subdev_state_constify_call(state,				\
> > +		__v4l2_subdev_state_gen_call(compose, ##__VA_ARGS__, , _pad)	\
> > +			((struct v4l2_subdev_state *)state, pad, ##__VA_ARGS__))
> >   #define __v4l2_subdev_state_get_compose_pad(state, pad)	\
> >   	__v4l2_subdev_state_get_compose(state, pad, 0)
> >   struct v4l2_rect *
> > @@ -1410,9 +1423,10 @@ __v4l2_subdev_state_get_compose(struct v4l2_subdev_state *state,
> >    * For stream-unaware drivers the frame interval for the corresponding pad is
> >    * returned. If the pad does not exist, NULL is returned.
> >    */
> > -#define v4l2_subdev_state_get_interval(state, pad, ...)			\
> > -	__v4l2_subdev_state_gen_call(interval, ##__VA_ARGS__, , _pad)	\
> > -		(state, pad, ##__VA_ARGS__)
> > +#define v4l2_subdev_state_get_interval(state, pad, ...)				\
> > +	__v4l2_subdev_state_constify_call(state,				\
> > +		__v4l2_subdev_state_gen_call(interval, ##__VA_ARGS__, , _pad)	\
> > +			((struct v4l2_subdev_state *)state, pad, ##__VA_ARGS__))
> >   #define __v4l2_subdev_state_get_interval_pad(state, pad)	\
> >   	__v4l2_subdev_state_get_interval(state, pad, 0)
> >   struct v4l2_fract *

-- 
Regards,

Laurent Pinchart

  reply	other threads:[~2024-05-07  7:00 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-07  6:03 [PATCH v2 0/2] media: v4l2-subdev: Support const-awareness in state accessors Laurent Pinchart
2024-05-07  6:03 ` [PATCH v2 1/2] media: v4l2-subdev: Provide const-aware subdev " Laurent Pinchart
2024-05-07  6:46   ` Tomi Valkeinen
2024-05-07  7:00     ` Laurent Pinchart [this message]
2024-05-07  6:03 ` [PATCH v2 2/2] media: rkisp1: Mark subdev state pointers as const Laurent Pinchart

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240507070035.GA27500@pendragon.ideasonboard.com \
    --to=laurent.pinchart@ideasonboard.com \
    --cc=dafna@fastmail.com \
    --cc=hverkuil-cisco@xs4all.nl \
    --cc=linux-media@vger.kernel.org \
    --cc=linux-renesas-soc@vger.kernel.org \
    --cc=linux-rockchip@lists.infradead.org \
    --cc=sakari.ailus@iki.fi \
    --cc=tomi.valkeinen@ideasonboard.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).