Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
Link: https://lore.kernel.org/r/1c0090aaf49504eaeaff5e7dd119fd37173290b5.1695540940.git.christophe.jaillet@wanadoo.fr
Signed-off-by: Mark Brown <broonie@kernel.org>

authored by

Christophe JAILLET and committed by
Mark Brown
c3518350 39fce972

+6 -11
+6 -11
sound/soc/codecs/audio-iio-aux.c
··· 26 26 27 27 struct audio_iio_aux { 28 28 struct device *dev; 29 - struct audio_iio_aux_chan *chans; 30 29 unsigned int num_chans; 30 + struct audio_iio_aux_chan chans[] __counted_by(num_chans); 31 31 }; 32 32 33 33 static int audio_iio_aux_info_volsw(struct snd_kcontrol *kcontrol, ··· 250 250 int ret; 251 251 int i; 252 252 253 - iio_aux = devm_kzalloc(dev, sizeof(*iio_aux), GFP_KERNEL); 253 + count = device_property_string_array_count(dev, "io-channel-names"); 254 + if (count < 0) 255 + return dev_err_probe(dev, count, "failed to count io-channel-names\n"); 256 + 257 + iio_aux = devm_kzalloc(dev, struct_size(iio_aux, chans, count), GFP_KERNEL); 254 258 if (!iio_aux) 255 259 return -ENOMEM; 256 260 257 261 iio_aux->dev = dev; 258 262 259 - count = device_property_string_array_count(dev, "io-channel-names"); 260 - if (count < 0) 261 - return dev_err_probe(dev, count, "failed to count io-channel-names\n"); 262 - 263 263 iio_aux->num_chans = count; 264 - 265 - iio_aux->chans = devm_kmalloc_array(dev, iio_aux->num_chans, 266 - sizeof(*iio_aux->chans), GFP_KERNEL); 267 - if (!iio_aux->chans) 268 - return -ENOMEM; 269 264 270 265 names = kcalloc(iio_aux->num_chans, sizeof(*names), GFP_KERNEL); 271 266 if (!names)