perf tools: Avoid a malloc() for array events
authorIan Rogers <irogers@google.com>
Wed, 23 Oct 2019 00:53:33 +0000 (17:53 -0700)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Wed, 6 Nov 2019 18:43:05 +0000 (15:43 -0300)
Use realloc() rather than malloc()+memcpy() to possibly avoid a memory
allocation when appending array elements.

Signed-off-by: Ian Rogers <irogers@google.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: Jin Yao <yao.jin@linux.intel.com>
Cc: John Garry <john.garry@huawei.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Martin KaFai Lau <kafai@fb.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Song Liu <songliubraving@fb.com>
Cc: Stephane Eranian <eranian@google.com>
Cc: Yonghong Song <yhs@fb.com>
Cc: bpf@vger.kernel.org
Cc: clang-built-linux@googlegroups.com
Cc: netdev@vger.kernel.org
Link: http://lore.kernel.org/lkml/20191023005337.196160-6-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/util/parse-events.y

index 5863acb347808a7092ca5433289754951c91c71a..ffa1a1b63796e25fe2fa291938b236a21469dcb8 100644 (file)
@@ -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;
 }