diff mbox series

ASoC: use inline function for type safety in snd_soc_substream_to_rtd()

Message ID 20240430140938.328088-1-krzysztof.kozlowski@linaro.org (mailing list archive)
State New, archived
Headers show
Series ASoC: use inline function for type safety in snd_soc_substream_to_rtd() | expand

Commit Message

Krzysztof Kozlowski April 30, 2024, 2:09 p.m. UTC
A common pattern in sound drivers is getting 'struct snd_soc_pcm_runtime'
from 'struct snd_pcm_substream' opaque pointer private_data field with
snd_soc_substream_to_rtd().  However 'private_data' appears in several
other structures as well, including 'struct snd_compr_stream'.  The
field might not hold the same type for every structure, although seems
the case at least for 'struct snd_compr_stream', so code can easily make
a mistake by using macro for wrong structures.

Switch from macro to inline function, so such mistake will be build-time
detectable.

Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
---
 include/sound/soc.h | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

Comments

Mark Brown April 30, 2024, 2:56 p.m. UTC | #1
On Tue, Apr 30, 2024 at 04:09:38PM +0200, Krzysztof Kozlowski wrote:

> -#define snd_soc_substream_to_rtd(substream) \
> -	(struct snd_soc_pcm_runtime *)snd_pcm_substream_chip(substream)
> +
> +static inline struct snd_soc_pcm_runtime *
> +snd_soc_substream_to_rtd(const struct snd_pcm_substream *substream)
> +{
> +	return substream->private_data;
> +}

This is not an obvious direct substitution, we've skipped the
snd_pcm_subsystem_chip() (which is also a macro, but that just suggests
it could do with it's own cleanup) and the commit log didn't mention it.
Krzysztof Kozlowski April 30, 2024, 3:52 p.m. UTC | #2
On 30/04/2024 16:56, Mark Brown wrote:
> On Tue, Apr 30, 2024 at 04:09:38PM +0200, Krzysztof Kozlowski wrote:
> 
>> -#define snd_soc_substream_to_rtd(substream) \
>> -	(struct snd_soc_pcm_runtime *)snd_pcm_substream_chip(substream)
>> +
>> +static inline struct snd_soc_pcm_runtime *
>> +snd_soc_substream_to_rtd(const struct snd_pcm_substream *substream)
>> +{
>> +	return substream->private_data;
>> +}
> 
> This is not an obvious direct substitution, we've skipped the
> snd_pcm_subsystem_chip() (which is also a macro, but that just suggests
> it could do with it's own cleanup) and the commit log didn't mention it.

Yes, you're right. I should keep snd_pcm_substream_chip().

Best regards,
Krzysztof
diff mbox series

Patch

diff --git a/include/sound/soc.h b/include/sound/soc.h
index 2a1b6c198547..cacaa92487a0 100644
--- a/include/sound/soc.h
+++ b/include/sound/soc.h
@@ -1215,8 +1215,12 @@  struct snd_soc_pcm_runtime {
 /* see soc_new_pcm_runtime()  */
 #define snd_soc_rtd_to_cpu(rtd, n)   (rtd)->dais[n]
 #define snd_soc_rtd_to_codec(rtd, n) (rtd)->dais[n + (rtd)->dai_link->num_cpus]
-#define snd_soc_substream_to_rtd(substream) \
-	(struct snd_soc_pcm_runtime *)snd_pcm_substream_chip(substream)
+
+static inline struct snd_soc_pcm_runtime *
+snd_soc_substream_to_rtd(const struct snd_pcm_substream *substream)
+{
+	return substream->private_data;
+}
 
 #define for_each_rtd_components(rtd, i, component)			\
 	for ((i) = 0, component = NULL;					\