From patchwork Fri Oct 25 18:08:21 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 1184384 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: incoming-bpf@patchwork.ozlabs.org Delivered-To: patchwork-incoming-bpf@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=bpf-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.b="S1GOx2ch"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 470BwQ1rFKz9sR6 for ; Sat, 26 Oct 2019 05:09:58 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1733185AbfJYSIp (ORCPT ); Fri, 25 Oct 2019 14:08:45 -0400 Received: from mail-pl1-f202.google.com ([209.85.214.202]:55743 "EHLO mail-pl1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1733155AbfJYSIo (ORCPT ); Fri, 25 Oct 2019 14:08:44 -0400 Received: by mail-pl1-f202.google.com with SMTP id g11so1986508plm.22 for ; Fri, 25 Oct 2019 11:08:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=2S2DsvKndhCCCwXhy7APxIUiBul3NbHlVg5iB3GS6wI=; b=S1GOx2chJq005n3l26PE/OxzhX5C+hRjXWDeFhCkywyuslxoRAeqypOqIuChfSPfUu NA1KhxahOriQwx+Z0BGci3W2BvxgOZ4rdrGOZxI1SKIQSknC8epGu4qXsv/k893YtYdg WpOhSw/4+Afdl6vLLCjgFzadtYVTPJe27sil2puX6SEQfWnaOqwv913miQpe9jhpgNVP IwZ+wOOqqRJZaO2uGGzu2DukYz3QSOj4FjyMPSnCsMFkongN9d6l+UzyX9w+4bZESB6O tq6e2DSiaqb5XaJ+mIvS6WrlxxKexhmJKDP+A/AIzZiyYL3a3f8E1zVVOEwkFpBnODLK Q0aA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=2S2DsvKndhCCCwXhy7APxIUiBul3NbHlVg5iB3GS6wI=; b=TEq3LkDI4hg10IWwh0c91wQdYGlcWhfKt74inIsBKYf6R38GXQlEf7K5tHV7RpWxU1 doP07XN4q4xQEA2k83wFTBf+eS11ZJgGzMGYZQjjc1WMX2f81vNhDPo1QWzy4gEUerzv rHw1GAFbVOPQjrgFO7Kx1pe91TifZ1hd/blGOCt47VXuh8NdMHe9/o+FFPxVEBs7Hxgf I+DelevL3WzfziURBBK4PnrMdJeXzUVlWNdbYY+RDpTHqfKVG4B0meXEah70uwcWxdyi r2jLJl+nRW9IFTmuieSP7sJYSi5VXgf5nu0B0GYUk7qUhM+itd8Nl90ENnUgbHyJOAJG JDmg== X-Gm-Message-State: APjAAAXG6/SRubcE3QTvhng6+l1+oSsYM7KFkXjEg0vT6225sw4IKX5W NbSeflPn5bOjU96gqhpFwrvETmcKqYUT X-Google-Smtp-Source: APXvYqwCiOCxWKogyeiMwYvwOtugEYW5dgD9xy0gca9fx0LItS/FqQFuYGbkyTwUtw9zrUcH6p/Wu4erFpVv X-Received: by 2002:a63:e60b:: with SMTP id g11mr3770780pgh.119.1572026923546; Fri, 25 Oct 2019 11:08:43 -0700 (PDT) Date: Fri, 25 Oct 2019 11:08:21 -0700 In-Reply-To: <20191025180827.191916-1-irogers@google.com> Message-Id: <20191025180827.191916-4-irogers@google.com> Mime-Version: 1.0 References: <20191024190202.109403-1-irogers@google.com> <20191025180827.191916-1-irogers@google.com> X-Mailer: git-send-email 2.24.0.rc0.303.g954a862665-goog Subject: [PATCH v4 3/9] perf tools: avoid a malloc for array events From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , Andi Kleen , Jin Yao , Adrian Hunter , Kan Liang , John Garry , linux-kernel@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, clang-built-linux@googlegroups.com Cc: Stephane Eranian , Ian Rogers Sender: bpf-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org Use realloc rather than malloc+memcpy to possibly avoid a memory allocation when appending array elements. Signed-off-by: Ian Rogers --- 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; }