Unverified Commit c3518350 authored by Christophe JAILLET's avatar Christophe JAILLET Committed by Mark Brown
Browse files

ASoC: audio-iio-aux: Use flex array to simplify code



"io-channel-names" is expected to have few values, so there is no real
point to allocate audio_iio_aux_chan structure with a dedicate memory
allocation.

Using a flexible array for struct audio_iio_aux->chans avoids the
overhead of an additional, managed, memory allocation.

This also saves an indirection when the array is accessed.

Finally, __counted_by() can be used for run-time bounds checking if
configured and supported by the compiler.

Signed-off-by: default avatarChristophe JAILLET <christophe.jaillet@wanadoo.fr>
Link: https://lore.kernel.org/r/1c0090aaf49504eaeaff5e7dd119fd37173290b5.1695540940.git.christophe.jaillet@wanadoo.fr


Signed-off-by: default avatarMark Brown <broonie@kernel.org>
parent 39fce972
Loading
Loading
Loading
Loading
+6 −11
Original line number Diff line number Diff line
@@ -26,8 +26,8 @@ struct audio_iio_aux_chan {

struct audio_iio_aux {
	struct device *dev;
	struct audio_iio_aux_chan *chans;
	unsigned int num_chans;
	struct audio_iio_aux_chan chans[]  __counted_by(num_chans);
};

static int audio_iio_aux_info_volsw(struct snd_kcontrol *kcontrol,
@@ -250,23 +250,18 @@ static int audio_iio_aux_probe(struct platform_device *pdev)
	int ret;
	int i;

	iio_aux = devm_kzalloc(dev, sizeof(*iio_aux), GFP_KERNEL);
	count = device_property_string_array_count(dev, "io-channel-names");
	if (count < 0)
		return dev_err_probe(dev, count, "failed to count io-channel-names\n");

	iio_aux = devm_kzalloc(dev, struct_size(iio_aux, chans, count), GFP_KERNEL);
	if (!iio_aux)
		return -ENOMEM;

	iio_aux->dev = dev;

	count = device_property_string_array_count(dev, "io-channel-names");
	if (count < 0)
		return dev_err_probe(dev, count, "failed to count io-channel-names\n");

	iio_aux->num_chans = count;

	iio_aux->chans = devm_kmalloc_array(dev, iio_aux->num_chans,
					    sizeof(*iio_aux->chans), GFP_KERNEL);
	if (!iio_aux->chans)
		return -ENOMEM;

	names = kcalloc(iio_aux->num_chans, sizeof(*names), GFP_KERNEL);
	if (!names)
		return -ENOMEM;