[alsa-devel] [Resend] [PATCH 3/4] ASoC: dapm: Avoid creating kcontrol for params

Charles Keepax ckeepax at opensource.cirrus.com
Wed Sep 20 13:05:34 CEST 2017


On Wed, Sep 20, 2017 at 01:28:35AM -0700, yesanishhere at gmail.com wrote:
> From: anish kumar <yesanishhere at gmail.com>
> 
> Currently in codec to codec dai link if there are multiple
> params defined then dapm can use created kcontrol to
> decide which param to apply at runtime.
> 
> However, in case there is only single param configuration
> then there is no point in creating the kcontrol and also there
> is no point in allocating memory for kcontrol.
> 
> In the snd_soc_dapm_new_pcm function, there is memory
> allocation happening for kcontrol which is later used
> or not used based on num_param. It is better to not
> allocate memory when there is only a single configuration.
> This change is to remedy that anomaly.
> 
> Signed-off-by: anish kumar <yesanishhere at gmail.com>
> ---

This does make the code look quite a bit nicer, I think there is
one small issue below.

<snip>

> +int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
> +			 const struct snd_soc_pcm_stream *params,
> +			 unsigned int num_params,
> +			 struct snd_soc_dapm_widget *source,
> +			 struct snd_soc_dapm_widget *sink)
> +{
> +	struct snd_soc_dapm_widget template;
> +	struct snd_soc_dapm_widget *w;
> +	char *link_name;
> +	int ret;
> +
> +	link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
> +				   source->name, sink->name);
> +	if (!link_name)
> +		return -ENOMEM;
>  
> +	memset(&template, 0, sizeof(template));
> +	template.reg = SND_SOC_NOPM;
> +	template.id = snd_soc_dapm_dai_link;
> +	template.name = link_name;
> +	template.event = snd_soc_dai_link_event;
> +	template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
> +		SND_SOC_DAPM_PRE_PMD;
> +	template.kcontrol_news = NULL;
> +
> +	/* allocate memory for control, only in case of multiple configs */
> +	if (num_params > 1) {
> +		template.num_kcontrols = 1;
> +		template.kcontrol_news =
> +					snd_soc_dapm_alloc_kcontrol(card,
> +						link_name, params, num_params);
> +		if (!template.kcontrol_news) {
> +			ret = -ENOMEM;
> +			goto outfree_link_name;
> +		}
> +	}
>  	dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
>  
>  	w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);

If we fail this call or the call to snd_soc_dapm_add_path that
follows it we don't clean up all the stuff we allocated in
snd_soc_dapm_alloc_kcontrol. We probably need to add a de-alloc
function for the error paths as well.

Thanks,
Charles


More information about the Alsa-devel mailing list