From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 25B97C43334 for ; Wed, 20 Jul 2022 00:56:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238358AbiGTA4L (ORCPT ); Tue, 19 Jul 2022 20:56:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50630 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229556AbiGTA4J (ORCPT ); Tue, 19 Jul 2022 20:56:09 -0400 Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0D60D422D3 for ; Tue, 19 Jul 2022 17:56:06 -0700 (PDT) Received: by mail-wr1-x429.google.com with SMTP id z13so4401025wro.13 for ; Tue, 19 Jul 2022 17:56:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=Ibqk5fisgJ/ziZRD3smb5yzx395lJhCMKBmOKX4NldQ=; b=CNA/W/uTIUbWX/lOwQw/p6BLCNyrFKSMaE4xaXDyd2kK3EDB7lSYdoWcqB4vJX2Lyv 9wMOa3UOFVd7t2+JLomPWtwhkJqp8HyG/di+yjjJegDIvBFzD1sDg0z8u2zupohNdge4 Ia/UxzH/8tqF9pEEwd42G5kzZ0KduPQOfrknHk8TnKQrxn0JEaE3xTlZl5OYnH81QO9C T7UKQGb1anI5rRRvtH1REJoJxzMQPt1pwBSj+7w1NUpeIHACPZmQplD4mXTaXakTQ1ls C7vMcJ05maM6Ipo83zmuToSSx5C2fF9gT37a89zcNSEYLp79tcAc/+l1YskqF3lApe1l ZfEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=Ibqk5fisgJ/ziZRD3smb5yzx395lJhCMKBmOKX4NldQ=; b=q1WNpQBKDJxPqgKot0CXzQZNOZ6mW4u6pjvy0iuOoCz5TNwCgBNkjJSl2DGVTEBLOL 8r2re88cKchlZcI837vGqGcpXdGO0FGtcG7iHdQtuMWjZfRr8yFG4Jw9EZcCXOFupPP4 U8uuIO2XknHbWQOwEOLYCEsTY0PhBveXX5xb5yzB8Eb1IVNsgr8Y+MUCIfED+Mim4QGN S3a7vvpI73g0uzeBuV1HjuEqlpbnLd0Ju6FGL3zGLiSviwlK+JbcuWt06UdkXyMrsnPe k4YellB06QqRpDtDfgcTGkSQMdB4yS/CqKqlHZtBHti6WZR+ffWf2Eg3omaMS8c4NhdV 20Jw== X-Gm-Message-State: AJIora+39RlyIXt+5jT7TRe2vAKUrtCPclZ3y0u9C6rWtujmdcqnLrFI uiU/hgFcziNx+k+vIvvKi3nvZoQdWaxuMJla8jLkBl5sZTOUYQ== X-Google-Smtp-Source: AGRyM1sWB+7bN6Q+hdPG/nbCGFYDQBcv40nJ++FCTuumemqbD4hnoAb8q3fE0XNlrJXRmT+Hcl2vH2Z8kwDF4bQCGis= X-Received: by 2002:a5d:6a4c:0:b0:21e:46d4:6eec with SMTP id t12-20020a5d6a4c000000b0021e46d46eecmr1655649wrw.375.1658278564481; Tue, 19 Jul 2022 17:56:04 -0700 (PDT) MIME-Version: 1.0 References: <20220711093218.10967-1-adrian.hunter@intel.com> <20220711093218.10967-24-adrian.hunter@intel.com> In-Reply-To: <20220711093218.10967-24-adrian.hunter@intel.com> From: Ian Rogers Date: Tue, 19 Jul 2022 17:55:52 -0700 Message-ID: Subject: Re: [PATCH 23/35] perf tools: Add reallocarray_as_needed() To: Adrian Hunter Cc: Arnaldo Carvalho de Melo , Jiri Olsa , Namhyung Kim , Andi Kleen , linux-kernel@vger.kernel.org, kvm@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Jul 11, 2022 at 2:33 AM Adrian Hunter wrote: > > Add helper reallocarray_as_needed() to reallocate an array to a larger > size and initialize the extra entries to an arbitrary value. > > Signed-off-by: Adrian Hunter > --- > tools/perf/util/util.c | 33 +++++++++++++++++++++++++++++++++ > tools/perf/util/util.h | 15 +++++++++++++++ > 2 files changed, 48 insertions(+) > > diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c > index 9b02edf9311d..391c1e928bd7 100644 > --- a/tools/perf/util/util.c > +++ b/tools/perf/util/util.c > @@ -18,6 +18,7 @@ > #include > #include > #include > +#include > #include > #include "cap.h" > #include "strlist.h" > @@ -500,3 +501,35 @@ char *filename_with_chroot(int pid, const char *filename) > > return new_name; > } > + > +/* > + * Reallocate an array *arr of size *arr_sz so that it is big enough to contain > + * x elements of size msz, initializing new entries to *init_val or zero if > + * init_val is NULL > + */ > +int do_realloc_array_as_needed(void **arr, size_t *arr_sz, size_t x, size_t msz, const void *init_val) This feels a little like a 1-dimensional xyarray, could we make a similar abstraction to avoid passing all these values around? Thanks, Ian > +{ > + size_t new_sz = *arr_sz; > + void *new_arr; > + size_t i; > + > + if (!new_sz) > + new_sz = msz >= 64 ? 1 : roundup(64, msz); /* Start with at least 64 bytes */ > + while (x >= new_sz) { > + if (check_mul_overflow(new_sz, (size_t)2, &new_sz)) > + return -ENOMEM; > + } > + if (new_sz == *arr_sz) > + return 0; > + new_arr = calloc(new_sz, msz); > + if (!new_arr) > + return -ENOMEM; > + memcpy(new_arr, *arr, *arr_sz * msz); > + if (init_val) { > + for (i = *arr_sz; i < new_sz; i++) > + memcpy(new_arr + (i * msz), init_val, msz); > + } > + *arr = new_arr; > + *arr_sz = new_sz; > + return 0; > +} > diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h > index 0f78f1e7782d..c1f2d423a9ec 100644 > --- a/tools/perf/util/util.h > +++ b/tools/perf/util/util.h > @@ -79,4 +79,19 @@ struct perf_debuginfod { > void perf_debuginfod_setup(struct perf_debuginfod *di); > > char *filename_with_chroot(int pid, const char *filename); > + > +int do_realloc_array_as_needed(void **arr, size_t *arr_sz, size_t x, > + size_t msz, const void *init_val); > + > +#define realloc_array_as_needed(a, n, x, v) ({ \ > + typeof(x) __x = (x); \ > + __x >= (n) ? \ > + do_realloc_array_as_needed((void **)&(a), \ > + &(n), \ > + __x, \ > + sizeof(*(a)), \ > + (const void *)(v)) : \ > + 0; \ > + }) > + > #endif /* GIT_COMPAT_UTIL_H */ > -- > 2.25.1 >