Skip to contentSkip to conversation

ALSA: pcm: Use krealloc() for resizing the rules array

PATCH In review Takashi Iwai 2 participants 2 messages Mar 13, 2018 Sound

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ALSA: pcm: Use krealloc() for resizing the rules array

Just a minor simplification. Change from kcalloc() shouldn't matter as each array element is fully initialized.

---
 sound/core/pcm_lib.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
sound/core/pcm_lib.c +2 -6
index a83152e7d387..f4a19509cccf 100644
@@ -1129,16 +1129,12 @@ int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
 	if (constrs->rules_num >= constrs->rules_all) {
 		struct snd_pcm_hw_rule *new;
 		unsigned int new_rules = constrs->rules_all + 16;
-		new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
+		new = krealloc(constrs->rules, new_rules * sizeof(*c),
+			       GFP_KERNEL);
 		if (!new) {
 			va_end(args);
 			return -ENOMEM;
 		}
-		if (constrs->rules) {
-			memcpy(new, constrs->rules,
-			       constrs->rules_num * sizeof(*c));
-			kfree(constrs->rules);
-		}
 		constrs->rules = new;
 		constrs->rules_all = new_rules;
 	}
Signature · 1 line
2.16.2
Reviewed-by on v1 1/1
#1 lore raw

Hi,

Quoted 7 lines · Takashi Iwai

Just a minor simplification. Change from kcalloc() shouldn't matter as each array element is fully initialized.

---
  sound/core/pcm_lib.c | 8 ++------
  1 file changed, 2 insertions(+), 6 deletions(-)

I think this is a good simplification and preferable, too. Some local variables in this block could be replaced by assignment but for readability it's better to keep them as is, in my opinion.

Quoted 13 lines
sound/core/pcm_lib.c +2 -1
index a83152e7d387..f4a19509cccf 100644
@@ -1129,16 +1129,12 @@ int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
  	if (constrs->rules_num >= constrs->rules_all) {
  		struct snd_pcm_hw_rule *new;
  		unsigned int new_rules = constrs->rules_all + 16;
-		new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
+		new = krealloc(constrs->rules, new_rules * sizeof(*c),
+			       GFP_KERNEL);
  		if (!new) {
  			va_end(args);

sound/core/pcm_lib.c int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond, · line 1140

Quoted 10 lines
sound/core/pcm_lib.c +0 -5
  			return -ENOMEM;
  		}
-		if (constrs->rules) {
-			memcpy(new, constrs->rules,
-			       constrs->rules_num * sizeof(*c));
-			kfree(constrs->rules);
-		}
  		constrs->rules = new;
  		constrs->rules_all = new_rules;
  	}

Regards

Takashi Sakamoto