Subject: [PATCH 12/63] perf tools: Avoid a malloc() for array events

From: Arnaldo Carvalho de Melo <[email-masked]>
Date: Thu, 7 Nov 2019 15:59:20 -0300

From: Ian Rogers <[email-masked]>

Use realloc() rather than malloc()+memcpy() to possibly avoid a memory
allocation when appending array elements.

Signed-off-by: Ian Rogers <[email-masked]>
Acked-by: Jiri Olsa <[email-masked]>
Cc: Adrian Hunter <[email-masked]>
Cc: Alexander Shishkin <[email-masked]>
Cc: Alexei Starovoitov <[email-masked]>
Cc: Andi Kleen <[email-masked]>
Cc: Daniel Borkmann <[email-masked]>
Cc: Jin Yao <[email-masked]>
Cc: John Garry <[email-masked]>
Cc: Kan Liang <[email-masked]>
Cc: Mark Rutland <[email-masked]>
Cc: Martin KaFai Lau <[email-masked]>
Cc: Namhyung Kim <[email-masked]>
Cc: Peter Zijlstra <[email-masked]>
Cc: Song Liu <[email-masked]>
Cc: Stephane Eranian <[email-masked]>
Cc: Yonghong Song <[email-masked]>
Cc: [email-masked]
Cc: [email-masked]
Cc: [email-masked]
Link: http://lore.kernel.org/lkml/[email-masked]
Signed-off-by: Arnaldo Carvalho de Melo <[email-masked]>
---
 tools/perf/util/parse-events.y | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/tools/perf/util/parse-events.y b/tools/perf/util/parse-events.y
index 5863acb34780..ffa1a1b63796 100644
--- a/tools/perf/util/parse-events.y
+++ b/tools/perf/util/parse-events.y
@@ -689,14 +689,12 @@ array_terms ',' array_term
 	struct parse_events_array new_array;
 
 	new_array.nr_ranges = $1.nr_ranges + $3.nr_ranges;
-	new_array.ranges = malloc(sizeof(new_array.ranges[0]) *
-				  new_array.nr_ranges);
+	new_array.ranges = realloc($1.ranges,
+				sizeof(new_array.ranges[0]) *
+				new_array.nr_ranges);
 	ABORT_ON(!new_array.ranges);
-	memcpy(&new_array.ranges[0], $1.ranges,
-	       $1.nr_ranges * sizeof(new_array.ranges[0]));
 	memcpy(&new_array.ranges[$1.nr_ranges], $3.ranges,
 	       $3.nr_ranges * sizeof(new_array.ranges[0]));
-	free($1.ranges);
 	free($3.ranges);
 	$$ = new_array;
 }
-- 
2.21.0