diff mbox series

[2/9] drm/vc4: hdmi: Constify container_of wrappers

Message ID 20221207-rpi-hdmi-improvements-v1-2-6b15f774c13a@cerno.tech (mailing list archive)
State New, archived
Headers show
Series drm/vc4: hdmi: Broadcast RGB, BT601, BT2020 | expand

Commit Message

Maxime Ripard Dec. 7, 2022, 4:07 p.m. UTC
None of our wrappers around container_of to access our objects from the
DRM object pointer actually modify the latter.

Let's make them const.

Signed-off-by: Maxime Ripard <maxime@cerno.tech>
---
 drivers/gpu/drm/vc4/vc4_hdmi.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

Comments

Thomas Zimmermann Jan. 11, 2023, 2:02 p.m. UTC | #1
Hi

Am 07.12.22 um 17:07 schrieb Maxime Ripard:
> None of our wrappers around container_of to access our objects from the
> DRM object pointer actually modify the latter.
> 
> Let's make them const.
> 
> Signed-off-by: Maxime Ripard <maxime@cerno.tech>

Personally, I wouldn't take this patch as it does an implicit const cast 
on the pointer.

For const correctness, it seems better to add new helpers that keep the 
const. Those could be use in places where the caller is not allowed to 
modify the state (i.e., atomic_update, et al).

Something like this:

const struct vc4_hdmi *
const_connector_to_vc4_hdmi(const struct drm_connector *connector)
{
	return container_of(connector, const struct vc4_hdmi,
		connector);
}

Best regards
Thomas

> ---
>   drivers/gpu/drm/vc4/vc4_hdmi.h | 6 +++---
>   1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/gpu/drm/vc4/vc4_hdmi.h b/drivers/gpu/drm/vc4/vc4_hdmi.h
> index dc3ccd8002a0..023ea64ef006 100644
> --- a/drivers/gpu/drm/vc4/vc4_hdmi.h
> +++ b/drivers/gpu/drm/vc4/vc4_hdmi.h
> @@ -224,13 +224,13 @@ struct vc4_hdmi {
>   };
>   
>   static inline struct vc4_hdmi *
> -connector_to_vc4_hdmi(struct drm_connector *connector)
> +connector_to_vc4_hdmi(const struct drm_connector *connector)
>   {
>   	return container_of(connector, struct vc4_hdmi, connector);
>   }
>   
>   static inline struct vc4_hdmi *
> -encoder_to_vc4_hdmi(struct drm_encoder *encoder)
> +encoder_to_vc4_hdmi(const struct drm_encoder *encoder)
>   {
>   	struct vc4_encoder *_encoder = to_vc4_encoder(encoder);
>   	return container_of(_encoder, struct vc4_hdmi, encoder);
> @@ -244,7 +244,7 @@ struct vc4_hdmi_connector_state {
>   };
>   
>   static inline struct vc4_hdmi_connector_state *
> -conn_state_to_vc4_hdmi_conn_state(struct drm_connector_state *conn_state)
> +conn_state_to_vc4_hdmi_conn_state(const struct drm_connector_state *conn_state)
>   {
>   	return container_of(conn_state, struct vc4_hdmi_connector_state, base);
>   }
>
Jani Nikula Jan. 11, 2023, 2:17 p.m. UTC | #2
On Wed, 11 Jan 2023, Thomas Zimmermann <tzimmermann@suse.de> wrote:
> Hi
>
> Am 07.12.22 um 17:07 schrieb Maxime Ripard:
>> None of our wrappers around container_of to access our objects from the
>> DRM object pointer actually modify the latter.
>> 
>> Let's make them const.
>> 
>> Signed-off-by: Maxime Ripard <maxime@cerno.tech>
>
> Personally, I wouldn't take this patch as it does an implicit const cast 
> on the pointer.
>
> For const correctness, it seems better to add new helpers that keep the 
> const. Those could be use in places where the caller is not allowed to 
> modify the state (i.e., atomic_update, et al).
>
> Something like this:
>
> const struct vc4_hdmi *
> const_connector_to_vc4_hdmi(const struct drm_connector *connector)
> {
> 	return container_of(connector, const struct vc4_hdmi,
> 		connector);
> }

See container_of_const().

BR,
Jani.

>
> Best regards
> Thomas
>
>> ---
>>   drivers/gpu/drm/vc4/vc4_hdmi.h | 6 +++---
>>   1 file changed, 3 insertions(+), 3 deletions(-)
>> 
>> diff --git a/drivers/gpu/drm/vc4/vc4_hdmi.h b/drivers/gpu/drm/vc4/vc4_hdmi.h
>> index dc3ccd8002a0..023ea64ef006 100644
>> --- a/drivers/gpu/drm/vc4/vc4_hdmi.h
>> +++ b/drivers/gpu/drm/vc4/vc4_hdmi.h
>> @@ -224,13 +224,13 @@ struct vc4_hdmi {
>>   };
>>   
>>   static inline struct vc4_hdmi *
>> -connector_to_vc4_hdmi(struct drm_connector *connector)
>> +connector_to_vc4_hdmi(const struct drm_connector *connector)
>>   {
>>   	return container_of(connector, struct vc4_hdmi, connector);
>>   }
>>   
>>   static inline struct vc4_hdmi *
>> -encoder_to_vc4_hdmi(struct drm_encoder *encoder)
>> +encoder_to_vc4_hdmi(const struct drm_encoder *encoder)
>>   {
>>   	struct vc4_encoder *_encoder = to_vc4_encoder(encoder);
>>   	return container_of(_encoder, struct vc4_hdmi, encoder);
>> @@ -244,7 +244,7 @@ struct vc4_hdmi_connector_state {
>>   };
>>   
>>   static inline struct vc4_hdmi_connector_state *
>> -conn_state_to_vc4_hdmi_conn_state(struct drm_connector_state *conn_state)
>> +conn_state_to_vc4_hdmi_conn_state(const struct drm_connector_state *conn_state)
>>   {
>>   	return container_of(conn_state, struct vc4_hdmi_connector_state, base);
>>   }
>>
diff mbox series

Patch

diff --git a/drivers/gpu/drm/vc4/vc4_hdmi.h b/drivers/gpu/drm/vc4/vc4_hdmi.h
index dc3ccd8002a0..023ea64ef006 100644
--- a/drivers/gpu/drm/vc4/vc4_hdmi.h
+++ b/drivers/gpu/drm/vc4/vc4_hdmi.h
@@ -224,13 +224,13 @@  struct vc4_hdmi {
 };
 
 static inline struct vc4_hdmi *
-connector_to_vc4_hdmi(struct drm_connector *connector)
+connector_to_vc4_hdmi(const struct drm_connector *connector)
 {
 	return container_of(connector, struct vc4_hdmi, connector);
 }
 
 static inline struct vc4_hdmi *
-encoder_to_vc4_hdmi(struct drm_encoder *encoder)
+encoder_to_vc4_hdmi(const struct drm_encoder *encoder)
 {
 	struct vc4_encoder *_encoder = to_vc4_encoder(encoder);
 	return container_of(_encoder, struct vc4_hdmi, encoder);
@@ -244,7 +244,7 @@  struct vc4_hdmi_connector_state {
 };
 
 static inline struct vc4_hdmi_connector_state *
-conn_state_to_vc4_hdmi_conn_state(struct drm_connector_state *conn_state)
+conn_state_to_vc4_hdmi_conn_state(const struct drm_connector_state *conn_state)
 {
 	return container_of(conn_state, struct vc4_hdmi_connector_state, base);
 }