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

ALSA: hda/common: Use auto cleanup for temporary buffers

The release of temporary kmalloced buffers can be nicely handled via
the standard __free(kfree).

Only code refactoring, and no behavior change.

Signed-off-by: Takashi Iwai <tiwai@suse.de>
Link: https://patch.msgid.link/20250827072916.31933-20-tiwai@suse.de

+14 -23
+2 -2
sound/hda/common/codec.c
··· 1858 1858 /* call kctl->put with the given value(s) */ 1859 1859 static int put_kctl_with_value(struct snd_kcontrol *kctl, int val) 1860 1860 { 1861 - struct snd_ctl_elem_value *ucontrol; 1861 + struct snd_ctl_elem_value *ucontrol __free(kfree) = NULL; 1862 + 1862 1863 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL); 1863 1864 if (!ucontrol) 1864 1865 return -ENOMEM; 1865 1866 ucontrol->value.integer.value[0] = val; 1866 1867 ucontrol->value.integer.value[1] = val; 1867 1868 kctl->put(kctl, ucontrol); 1868 - kfree(ucontrol); 1869 1869 return 0; 1870 1870 } 1871 1871
+12 -21
sound/hda/common/sysfs.c
··· 299 299 300 300 static int parse_hints(struct hda_codec *codec, const char *buf) 301 301 { 302 - char *key, *val; 302 + char *key __free(kfree) = NULL; 303 + char *val; 303 304 struct hda_hint *hint; 304 - int err = 0; 305 305 306 306 buf = skip_spaces(buf); 307 307 if (!*buf || *buf == '#' || *buf == '\n') ··· 313 313 return -ENOMEM; 314 314 /* extract key and val */ 315 315 val = strchr(key, '='); 316 - if (!val) { 317 - kfree(key); 316 + if (!val) 318 317 return -EINVAL; 319 - } 320 318 *val++ = 0; 321 319 val = skip_spaces(val); 322 320 remove_trail_spaces(key); ··· 324 326 if (hint) { 325 327 /* replace */ 326 328 kfree(hint->key); 327 - hint->key = key; 328 - hint->val = val; 329 - goto unlock; 329 + goto replace; 330 330 } 331 331 /* allocate a new hint entry */ 332 332 if (codec->hints.used >= MAX_HINTS) 333 - hint = NULL; 334 - else 335 - hint = snd_array_new(&codec->hints); 336 - if (hint) { 337 - hint->key = key; 338 - hint->val = val; 339 - } else { 340 - err = -ENOMEM; 341 - } 342 - unlock: 343 - if (err) 344 - kfree(key); 345 - return err; 333 + return -ENOMEM; 334 + hint = snd_array_new(&codec->hints); 335 + if (!hint) 336 + return -ENOMEM; 337 + replace: 338 + hint->key = no_free_ptr(key); 339 + hint->val = val; 340 + return 0; 346 341 } 347 342 348 343 static ssize_t hints_store(struct device *dev,