From patchwork Fri Jul 8 14:20:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12911208 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 B511BC433EF for ; Fri, 8 Jul 2022 14:20:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237893AbiGHOUi (ORCPT ); Fri, 8 Jul 2022 10:20:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45136 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237641AbiGHOUg (ORCPT ); Fri, 8 Jul 2022 10:20:36 -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 7826E24F15 for ; Fri, 8 Jul 2022 07:20:34 -0700 (PDT) Received: by mail-wr1-x429.google.com with SMTP id q9so30762297wrd.8 for ; Fri, 08 Jul 2022 07:20:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=sgv+eu1DihmDf2Wug9WsVqJiAHTnLTeXvP+NlvaxqxY=; b=fuJCe24hurJGv7b3+AKP9YIzRpZ7cQI76zgcOTViFNyHMqS0mF4yhLWWdD+WhS21/P JwPKNWdQAXhWkXg6kTsDvB9hI3gz3na/OeiCwixTGU/V6H1XvFIUXVWbPSNuqFWVzLbf AJBYh8EsJg98JQHb0XZFbnBL0vWc1kB6LL6MSrzvmgmJfvxu2wXw40Gs3CGBsEUcOGYt 2tWtrPvs6IJGLZ+VoDu3AJt1++xOb0VXDfjw19ksiECKEfyM5Mv8EiF2K954LzLdKse3 Q+TfRMdOa7rApo2s37wP7w060QblcmicoiHBB6GVS+mXGf1MEkdHM0rjr1D58L3OnEUk O3BQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=sgv+eu1DihmDf2Wug9WsVqJiAHTnLTeXvP+NlvaxqxY=; b=aqmksFTB3a3rxKEWpH+bx6tJxvPV5izmCrlWBpzjAGE/HSEvOGWGGvGxKowDjGmBWI eE05cOBiWi/gCcbnbKU8vBRnpDALqvnSk3IaK6Cv4D/grsuxkbWMaEwHrrtbbLB2j7Bv DDrfFSzxyWPJKBVmnwBP9N2iJJEg2OYgo7Pe3SxeEmoempdCI3gHRE931DipLPGH+tNt BgXPu+5ivmEoTikXo6u474Es8CIQFinrx/wCSmSN5ezhF1oj4R4sAQDQbdxnuQLL9pD2 aUgpSAFZ5taVTbpuJFRMFspynqTr1CUq9oB9wu8zp3dsrTWpYUX5femEN3YWL37SN5Zp NOAQ== X-Gm-Message-State: AJIora8qbrfA7NWLOqysq+EPlIHupL0RbCRANXiZNYKRP7+q4JEUrRef 6Gxf3oc2WlKhYmvoLQH0ThRG8hs2hqI= X-Google-Smtp-Source: AGRyM1ticjVXoubRd+hEPlkc/j3D1bgW2QGxjhH5nTZN0xokAyjr98JlbZLcmZy+3oqDMPfFrfv2tA== X-Received: by 2002:adf:fb49:0:b0:21a:3ccc:fb77 with SMTP id c9-20020adffb49000000b0021a3cccfb77mr3551098wrs.280.1657290032663; Fri, 08 Jul 2022 07:20:32 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id q5-20020adff945000000b0021b9585276dsm40393924wrr.101.2022.07.08.07.20.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Jul 2022 07:20:31 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Phillip Wood , Jeff King , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 1/7] xdiff: simplify freeing patterns around xdl_free_env() Date: Fri, 8 Jul 2022 16:20:13 +0200 Message-Id: X-Mailer: git-send-email 2.37.0.913.g189dca38629 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Change the invocations of xdl_free_env() so that only the function that allocated the "xe" variable frees it, rather than passing it to a function that might use "&xe", and on failure having that function free it. This simplifies the allocation management, since due to the new "{ 0 }" initialization we can pass "&xe" to xdl_free_env() without accounting for the "&xe" not being initialized yet. This change was originally suggested as an amendment of the series that since got merged in 47be28e51e6 (Merge branch 'pw/xdiff-alloc-fail', 2022-03-09) in [1]. The "avoid double free of xe2" in that series is one of the pattern we can simplify here. 1. https://lore.kernel.org/git/220216.86tuczt7js.gmgdl@evledraar.gmail.com/ Signed-off-by: Ævar Arnfjörð Bjarmason --- xdiff/xdiffi.c | 40 ++++++++++++++++------------------------ xdiff/xmerge.c | 47 ++++++++++++++++++++++++++++------------------- xdiff/xutils.c | 12 ++++++++---- 3 files changed, 52 insertions(+), 47 deletions(-) diff --git a/xdiff/xdiffi.c b/xdiff/xdiffi.c index 53e803e6bcb..6fded43e87d 100644 --- a/xdiff/xdiffi.c +++ b/xdiff/xdiffi.c @@ -320,15 +320,11 @@ int xdl_do_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, if (xdl_prepare_env(mf1, mf2, xpp, xe) < 0) return -1; - if (XDF_DIFF_ALG(xpp->flags) == XDF_PATIENCE_DIFF) { - res = xdl_do_patience_diff(mf1, mf2, xpp, xe); - goto out; - } + if (XDF_DIFF_ALG(xpp->flags) == XDF_PATIENCE_DIFF) + return xdl_do_patience_diff(mf1, mf2, xpp, xe); - if (XDF_DIFF_ALG(xpp->flags) == XDF_HISTOGRAM_DIFF) { - res = xdl_do_histogram_diff(mf1, mf2, xpp, xe); - goto out; - } + if (XDF_DIFF_ALG(xpp->flags) == XDF_HISTOGRAM_DIFF) + return xdl_do_histogram_diff(mf1, mf2, xpp, xe); /* * Allocate and setup K vectors to be used by the differential @@ -337,11 +333,8 @@ int xdl_do_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, * One is to store the forward path and one to store the backward path. */ ndiags = xe->xdf1.nreff + xe->xdf2.nreff + 3; - if (!XDL_ALLOC_ARRAY(kvd, 2 * ndiags + 2)) { - - xdl_free_env(xe); + if (!XDL_ALLOC_ARRAY(kvd, 2 * ndiags + 2)) return -1; - } kvdf = kvd; kvdb = kvdf + ndiags; kvdf += xe->xdf2.nreff + 1; @@ -366,9 +359,6 @@ int xdl_do_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, kvdf, kvdb, (xpp->flags & XDF_NEED_MINIMAL) != 0, &xenv); xdl_free(kvd); - out: - if (res < 0) - xdl_free_env(xe); return res; } @@ -1054,19 +1044,19 @@ static void xdl_mark_ignorable_regex(xdchange_t *xscr, const xdfenv_t *xe, int xdl_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t const *xecfg, xdemitcb_t *ecb) { xdchange_t *xscr; - xdfenv_t xe; + xdfenv_t xe = { 0 }; emit_func_t ef = xecfg->hunk_func ? xdl_call_hunk_func : xdl_emit_diff; + int status = 0; if (xdl_do_diff(mf1, mf2, xpp, &xe) < 0) { - - return -1; + status = -1; + goto cleanup; } if (xdl_change_compact(&xe.xdf1, &xe.xdf2, xpp->flags) < 0 || xdl_change_compact(&xe.xdf2, &xe.xdf1, xpp->flags) < 0 || xdl_build_script(&xe, &xscr) < 0) { - - xdl_free_env(&xe); - return -1; + status = -1; + goto cleanup; } if (xscr) { if (xpp->flags & XDF_IGNORE_BLANK_LINES) @@ -1078,12 +1068,14 @@ int xdl_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, if (ef(&xe, xscr, ecb, xecfg) < 0) { xdl_free_script(xscr); - xdl_free_env(&xe); - return -1; + status = -1; + goto cleanup; } xdl_free_script(xscr); } + +cleanup: xdl_free_env(&xe); - return 0; + return status; } diff --git a/xdiff/xmerge.c b/xdiff/xmerge.c index af40c88a5b3..ac0cf52f3be 100644 --- a/xdiff/xmerge.c +++ b/xdiff/xmerge.c @@ -365,7 +365,7 @@ static int xdl_refine_conflicts(xdfenv_t *xe1, xdfenv_t *xe2, xdmerge_t *m, { for (; m; m = m->next) { mmfile_t t1, t2; - xdfenv_t xe; + xdfenv_t xe = { 0 }; xdchange_t *xscr, *x; int i1 = m->i1, i2 = m->i2; @@ -387,8 +387,10 @@ static int xdl_refine_conflicts(xdfenv_t *xe1, xdfenv_t *xe2, xdmerge_t *m, t2.ptr = (char *)xe2->xdf2.recs[m->i2]->ptr; t2.size = xe2->xdf2.recs[m->i2 + m->chg2 - 1]->ptr + xe2->xdf2.recs[m->i2 + m->chg2 - 1]->size - t2.ptr; - if (xdl_do_diff(&t1, &t2, xpp, &xe) < 0) + if (xdl_do_diff(&t1, &t2, xpp, &xe) < 0) { + xdl_free_env(&xe); return -1; + } if (xdl_change_compact(&xe.xdf1, &xe.xdf2, xpp->flags) < 0 || xdl_change_compact(&xe.xdf2, &xe.xdf1, xpp->flags) < 0 || xdl_build_script(&xe, &xscr) < 0) { @@ -684,30 +686,37 @@ static int xdl_do_merge(xdfenv_t *xe1, xdchange_t *xscr1, int xdl_merge(mmfile_t *orig, mmfile_t *mf1, mmfile_t *mf2, xmparam_t const *xmp, mmbuffer_t *result) { - xdchange_t *xscr1 = NULL, *xscr2 = NULL; - xdfenv_t xe1, xe2; - int status = -1; + xdchange_t *xscr1, *xscr2; + xdfenv_t xe1 = { 0 }; + xdfenv_t xe2 = { 0 }; + int status; xpparam_t const *xpp = &xmp->xpp; result->ptr = NULL; result->size = 0; - if (xdl_do_diff(orig, mf1, xpp, &xe1) < 0) - return -1; - - if (xdl_do_diff(orig, mf2, xpp, &xe2) < 0) - goto free_xe1; /* avoid double free of xe2 */ - + if (xdl_do_diff(orig, mf1, xpp, &xe1) < 0) { + status = -1; + goto cleanup; + } + if (xdl_do_diff(orig, mf2, xpp, &xe2) < 0) { + status = -1; + goto cleanup; + } if (xdl_change_compact(&xe1.xdf1, &xe1.xdf2, xpp->flags) < 0 || xdl_change_compact(&xe1.xdf2, &xe1.xdf1, xpp->flags) < 0 || - xdl_build_script(&xe1, &xscr1) < 0) - goto out; - + xdl_build_script(&xe1, &xscr1) < 0) { + status = -1; + goto cleanup; + } if (xdl_change_compact(&xe2.xdf1, &xe2.xdf2, xpp->flags) < 0 || xdl_change_compact(&xe2.xdf2, &xe2.xdf1, xpp->flags) < 0 || - xdl_build_script(&xe2, &xscr2) < 0) - goto out; - + xdl_build_script(&xe2, &xscr2) < 0) { + xdl_free_script(xscr1); + status = -1; + goto cleanup; + } + status = 0; if (!xscr1) { result->ptr = xdl_malloc(mf2->size); if (!result->ptr) @@ -731,9 +740,9 @@ int xdl_merge(mmfile_t *orig, mmfile_t *mf1, mmfile_t *mf2, xdl_free_script(xscr1); xdl_free_script(xscr2); - xdl_free_env(&xe2); - free_xe1: +cleanup: xdl_free_env(&xe1); + xdl_free_env(&xe2); return status; } diff --git a/xdiff/xutils.c b/xdiff/xutils.c index 9e36f24875d..a6f10353cff 100644 --- a/xdiff/xutils.c +++ b/xdiff/xutils.c @@ -414,7 +414,8 @@ int xdl_fall_back_diff(xdfenv_t *diff_env, xpparam_t const *xpp, * ranges of lines instead of the whole files. */ mmfile_t subfile1, subfile2; - xdfenv_t env; + xdfenv_t env = { 0 }; + int status = 0; subfile1.ptr = (char *)diff_env->xdf1.recs[line1 - 1]->ptr; subfile1.size = diff_env->xdf1.recs[line1 + count1 - 2]->ptr + @@ -422,15 +423,18 @@ int xdl_fall_back_diff(xdfenv_t *diff_env, xpparam_t const *xpp, subfile2.ptr = (char *)diff_env->xdf2.recs[line2 - 1]->ptr; subfile2.size = diff_env->xdf2.recs[line2 + count2 - 2]->ptr + diff_env->xdf2.recs[line2 + count2 - 2]->size - subfile2.ptr; - if (xdl_do_diff(&subfile1, &subfile2, xpp, &env) < 0) - return -1; + if (xdl_do_diff(&subfile1, &subfile2, xpp, &env) < 0) { + status = -1; + goto cleanup; + } memcpy(diff_env->xdf1.rchg + line1 - 1, env.xdf1.rchg, count1); memcpy(diff_env->xdf2.rchg + line2 - 1, env.xdf2.rchg, count2); +cleanup: xdl_free_env(&env); - return 0; + return status; } void* xdl_alloc_grow_helper(void *p, long nr, long *alloc, size_t size) From patchwork Fri Jul 8 14:20:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12911209 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 B5766C43334 for ; Fri, 8 Jul 2022 14:20:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237920AbiGHOUk (ORCPT ); Fri, 8 Jul 2022 10:20:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45140 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237747AbiGHOUh (ORCPT ); Fri, 8 Jul 2022 10:20:37 -0400 Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D2C6124F2F for ; Fri, 8 Jul 2022 07:20:35 -0700 (PDT) Received: by mail-wr1-x42b.google.com with SMTP id v16so19318075wrd.13 for ; Fri, 08 Jul 2022 07:20:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=7zodhEbtxRjp+ESmro+zWyrYWci5gq5YFp1u+0UDssk=; b=DxP7dKELcw30qDT6E5lVM+opvgPJ+ZOWeSVkQkdhO63BMnnwVb21aP7frHbe/nHqP5 Ma69ogKXTITJF+94+Jm492g8DLd3r8x86nFb22NktMnPCblx31myNMUnEoa/dIuOGw0U N1W04BBfSaSs9gqR9h5BSC7O0eFUS4s1OC81WotSCFhrFsFnq7c2ZC8OAvYK3L7Ndv+I dDJ2xx1XwcBFPiEf+TPLlTaDkNX2PIiS0aPZwIYw5bL9679mCUrswQzuBmKCXL0QKaW1 CsfpeDvwPFwQAFfjkcry6CXIKNei6lAoF1vySaIRx/O2ALQFGm27QeVY0kMVnBQdx2rc mccA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=7zodhEbtxRjp+ESmro+zWyrYWci5gq5YFp1u+0UDssk=; b=hJFMcMySFQ4vx9Q7vZrkW+1zSwAng3+D7hDhBQubJG/30XG3QsiC44G4SqkfYoPdTm 0hYT64lwZnXURwSJ4VqdCSPwC1J9ftg+zvx7826C0wwQ0UiYWS40ySv9BJ31Of+8bT2i J9IDLVgbq8T0UmGifDEoLChv8D10zIBTDR4dG8BgBTIAZsRCzi5QyDO2bjYLw2KjXwxa BN7A9EETSuGmBDjeWyj0vI6fJNZd6zkjUxGommpHq7Z16Jm4EEqJlgM17ipSTURjwuO9 j/0doIurXYsDqeSwa9QQAn7zh3Vli0r9ADvOx53fMZzLE7ZIOvH+0F9y34pKvXyxjy/K adsQ== X-Gm-Message-State: AJIora9tTBi37BmWIXBDD/xxGG4lhtrSPVsWiXJrW7EUa8ZnhUgolCfH v4SyB9HdRtZtPQmmUJqZLiYT8JEquGx8UA== X-Google-Smtp-Source: AGRyM1t/UliQbUVOInxtx/QH0PQ4EVVTeBGxWFl8YM/DuK2Z35c0waqHiXEc4SujIGGNGlQ9bD7ESA== X-Received: by 2002:a5d:584c:0:b0:21c:1395:f0c4 with SMTP id i12-20020a5d584c000000b0021c1395f0c4mr3606226wrf.24.1657290033939; Fri, 08 Jul 2022 07:20:33 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id q5-20020adff945000000b0021b9585276dsm40393924wrr.101.2022.07.08.07.20.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Jul 2022 07:20:33 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Phillip Wood , Jeff King , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 2/7] git-shared-util.h: move "shared" allocation utilities here Date: Fri, 8 Jul 2022 16:20:14 +0200 Message-Id: X-Mailer: git-send-email 2.37.0.913.g189dca38629 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Move the most common allocation utilities from cache.h and git-compat-util.h to a new git-shared-util.h. The idea is that xdiff/ and other in-tree code can share this, and that external projects could then copy this header and include it. They will need to include some things that git-compat-util.h does before that, e.g. we need a "size_t" here, and if they'll end up using any of the x*() functions they'll need to declare those. But doing so should be fairly obvious, and we can always extend this to define some fallback wrappers here if e.g. GIT_COMPAT_UTIL_H isn't defined. Signed-off-by: Ævar Arnfjörð Bjarmason --- cache.h | 75 --------------------------------- git-compat-util.h | 28 ++----------- git-shared-util.h | 104 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 99 deletions(-) create mode 100644 git-shared-util.h diff --git a/cache.h b/cache.h index ac5ab4ef9d3..f29dbbadf77 100644 --- a/cache.h +++ b/cache.h @@ -677,81 +677,6 @@ void initialize_repository_version(int hash_algo, int reinit); void sanitize_stdfds(void); int daemonize(void); -#define alloc_nr(x) (((x)+16)*3/2) - -/** - * Dynamically growing an array using realloc() is error prone and boring. - * - * Define your array with: - * - * - a pointer (`item`) that points at the array, initialized to `NULL` - * (although please name the variable based on its contents, not on its - * type); - * - * - an integer variable (`alloc`) that keeps track of how big the current - * allocation is, initialized to `0`; - * - * - another integer variable (`nr`) to keep track of how many elements the - * array currently has, initialized to `0`. - * - * Then before adding `n`th element to the item, call `ALLOC_GROW(item, n, - * alloc)`. This ensures that the array can hold at least `n` elements by - * calling `realloc(3)` and adjusting `alloc` variable. - * - * ------------ - * sometype *item; - * size_t nr; - * size_t alloc - * - * for (i = 0; i < nr; i++) - * if (we like item[i] already) - * return; - * - * // we did not like any existing one, so add one - * ALLOC_GROW(item, nr + 1, alloc); - * item[nr++] = value you like; - * ------------ - * - * You are responsible for updating the `nr` variable. - * - * If you need to specify the number of elements to allocate explicitly - * then use the macro `REALLOC_ARRAY(item, alloc)` instead of `ALLOC_GROW`. - * - * Consider using ALLOC_GROW_BY instead of ALLOC_GROW as it has some - * added niceties. - * - * DO NOT USE any expression with side-effect for 'x', 'nr', or 'alloc'. - */ -#define ALLOC_GROW(x, nr, alloc) \ - do { \ - if ((nr) > alloc) { \ - if (alloc_nr(alloc) < (nr)) \ - alloc = (nr); \ - else \ - alloc = alloc_nr(alloc); \ - REALLOC_ARRAY(x, alloc); \ - } \ - } while (0) - -/* - * Similar to ALLOC_GROW but handles updating of the nr value and - * zeroing the bytes of the newly-grown array elements. - * - * DO NOT USE any expression with side-effect for any of the - * arguments. - */ -#define ALLOC_GROW_BY(x, nr, increase, alloc) \ - do { \ - if (increase) { \ - size_t new_nr = nr + (increase); \ - if (new_nr < nr) \ - BUG("negative growth in ALLOC_GROW_BY"); \ - ALLOC_GROW(x, new_nr, alloc); \ - memset((x) + nr, 0, sizeof(*(x)) * (increase)); \ - nr = new_nr; \ - } \ - } while (0) - /* Initialize and use the cache information */ struct lock_file; void preload_index(struct index_state *index, diff --git a/git-compat-util.h b/git-compat-util.h index 58d7708296b..eb4b27f4846 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -1034,31 +1034,11 @@ FILE *fopen_or_warn(const char *path, const char *mode); */ int xstrncmpz(const char *s, const char *t, size_t len); -/* - * FREE_AND_NULL(ptr) is like free(ptr) followed by ptr = NULL. Note - * that ptr is used twice, so don't pass e.g. ptr++. +/** + * Common allocation utils, including ones xdiff uses, and thus are + * split into this file for sharing with external projects. */ -#define FREE_AND_NULL(p) do { free(p); (p) = NULL; } while (0) - -#define ALLOC_ARRAY(x, alloc) (x) = xmalloc(st_mult(sizeof(*(x)), (alloc))) -#define CALLOC_ARRAY(x, alloc) (x) = xcalloc((alloc), sizeof(*(x))) -#define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), st_mult(sizeof(*(x)), (alloc))) - -#define COPY_ARRAY(dst, src, n) copy_array((dst), (src), (n), sizeof(*(dst)) + \ - BUILD_ASSERT_OR_ZERO(sizeof(*(dst)) == sizeof(*(src)))) -static inline void copy_array(void *dst, const void *src, size_t n, size_t size) -{ - if (n) - memcpy(dst, src, st_mult(size, n)); -} - -#define MOVE_ARRAY(dst, src, n) move_array((dst), (src), (n), sizeof(*(dst)) + \ - BUILD_ASSERT_OR_ZERO(sizeof(*(dst)) == sizeof(*(src)))) -static inline void move_array(void *dst, const void *src, size_t n, size_t size) -{ - if (n) - memmove(dst, src, st_mult(size, n)); -} +#include "git-shared-util.h" /* * These functions help you allocate structs with flex arrays, and copy diff --git a/git-shared-util.h b/git-shared-util.h new file mode 100644 index 00000000000..7b4479a0f72 --- /dev/null +++ b/git-shared-util.h @@ -0,0 +1,104 @@ +#ifndef GIT_SHARED_UTIL_H +#define GIT_SHARED_UTIL_H + +/* + * FREE_AND_NULL(ptr) is like free(ptr) followed by ptr = NULL. Note + * that ptr is used twice, so don't pass e.g. ptr++. + */ +#define FREE_AND_NULL(p) do { free(p); (p) = NULL; } while (0) + +#define ALLOC_ARRAY(x, alloc) (x) = xmalloc(st_mult(sizeof(*(x)), (alloc))) +#define CALLOC_ARRAY(x, alloc) (x) = xcalloc((alloc), sizeof(*(x))) +#define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), st_mult(sizeof(*(x)), (alloc))) + +#define COPY_ARRAY(dst, src, n) copy_array((dst), (src), (n), sizeof(*(dst)) + \ + BUILD_ASSERT_OR_ZERO(sizeof(*(dst)) == sizeof(*(src)))) +static inline void copy_array(void *dst, const void *src, size_t n, size_t size) +{ + if (n) + memcpy(dst, src, st_mult(size, n)); +} + +#define MOVE_ARRAY(dst, src, n) move_array((dst), (src), (n), sizeof(*(dst)) + \ + BUILD_ASSERT_OR_ZERO(sizeof(*(dst)) == sizeof(*(src)))) +static inline void move_array(void *dst, const void *src, size_t n, size_t size) +{ + if (n) + memmove(dst, src, st_mult(size, n)); +} + +#define alloc_nr(x) (((x)+16)*3/2) + +/** + * Dynamically growing an array using realloc() is error prone and boring. + * + * Define your array with: + * + * - a pointer (`item`) that points at the array, initialized to `NULL` + * (although please name the variable based on its contents, not on its + * type); + * + * - an integer variable (`alloc`) that keeps track of how big the current + * allocation is, initialized to `0`; + * + * - another integer variable (`nr`) to keep track of how many elements the + * array currently has, initialized to `0`. + * + * Then before adding `n`th element to the item, call `ALLOC_GROW(item, n, + * alloc)`. This ensures that the array can hold at least `n` elements by + * calling `realloc(3)` and adjusting `alloc` variable. + * + * ------------ + * sometype *item; + * size_t nr; + * size_t alloc + * + * for (i = 0; i < nr; i++) + * if (we like item[i] already) + * return; + * + * // we did not like any existing one, so add one + * ALLOC_GROW(item, nr + 1, alloc); + * item[nr++] = value you like; + * ------------ + * + * You are responsible for updating the `nr` variable. + * + * If you need to specify the number of elements to allocate explicitly + * then use the macro `REALLOC_ARRAY(item, alloc)` instead of `ALLOC_GROW`. + * + * Consider using ALLOC_GROW_BY instead of ALLOC_GROW as it has some + * added niceties. + * + * DO NOT USE any expression with side-effect for 'x', 'nr', or 'alloc'. + */ +#define ALLOC_GROW(x, nr, alloc) \ + do { \ + if ((nr) > alloc) { \ + if (alloc_nr(alloc) < (nr)) \ + alloc = (nr); \ + else \ + alloc = alloc_nr(alloc); \ + REALLOC_ARRAY(x, alloc); \ + } \ + } while (0) + +/* + * Similar to ALLOC_GROW but handles updating of the nr value and + * zeroing the bytes of the newly-grown array elements. + * + * DO NOT USE any expression with side-effect for any of the + * arguments. + */ +#define ALLOC_GROW_BY(x, nr, increase, alloc) \ + do { \ + if (increase) { \ + size_t new_nr = nr + (increase); \ + if (new_nr < nr) \ + BUG("negative growth in ALLOC_GROW_BY"); \ + ALLOC_GROW(x, new_nr, alloc); \ + memset((x) + nr, 0, sizeof(*(x)) * (increase)); \ + nr = new_nr; \ + } \ + } while (0) +#endif From patchwork Fri Jul 8 14:20:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12911210 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 A4E0DC433EF for ; Fri, 8 Jul 2022 14:20:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237785AbiGHOUm (ORCPT ); Fri, 8 Jul 2022 10:20:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45146 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237853AbiGHOUh (ORCPT ); Fri, 8 Jul 2022 10:20:37 -0400 Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C509C222A6 for ; Fri, 8 Jul 2022 07:20:36 -0700 (PDT) Received: by mail-wr1-x42b.google.com with SMTP id h17so17614757wrx.0 for ; Fri, 08 Jul 2022 07:20:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=5AXBuIp9hCX3KlqE+IoTeTfJOZru5FkD8YdGG01v1qg=; b=hErKp3dUJpTNiIhimsNO53zEUbs2G+iuY4GW0mEMqDeev1hjW+ZMWLsZ6tWYZffuzo jm9g22m3bUm/92nZ7lM4yis0S6F9ixj4eI9CTGEQZzBAt5IStNSwmBxGT6QzsbDPv36c BD2I4RzQlGnntanxSlJK/TCXG1dQtQsbwW4oaIdQDJe7lPs/ogCOxGggDN5HjrHUri2Y O4u1+/bwJ85iVwuIHiML7sAPEcVQnMnFzleJ95k+jZdQELGesY32vElv5B1WvWLVPFLb B8Qf745sOoMACIdTBeuMm2T//ar5bEiGitprLEVIak2dUzSSZajEh0rlz4psr3vrsWhk 5JRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=5AXBuIp9hCX3KlqE+IoTeTfJOZru5FkD8YdGG01v1qg=; b=GQbIeU3mOaEDHH3UmZIHGCBGDCpdt1QvmBqHb4EzQFR/CK6K0IHg6Yw26iCJfweSRb qGyXaQjfbfGf3ZswwAnS3x2qTQHO9zfQfjVXOavAwBykC5GkVlrwZeO8yUcn5VAyCdzW 3+6LDfNsWVtrMrTQOKb/nr7eOP/dB9qs5PmnX68MznCxDy7vjcshGHA8p74lTl20se1/ jFWshtekzL+taPik+DCdtwl24PfPwFo3/cHQ6iIsNdp8KHOdGblKs7oDlcE+G39LLQF2 wV6FZFk484H5WIgMkKH3lvA1E2Av2pTTHYCNQxM3ONEDvMT2/tQvrrBBg9ZM7avWEnmF gBqA== X-Gm-Message-State: AJIora8nuXgdVs5FxYOCTvIhF0xz4OWXUgJoeo+TyFcx/fkgLVsbMp5g gkMZs5UgLMFXeV0If/K26rZelYXuc5c5hw== X-Google-Smtp-Source: AGRyM1tbrWDkeRHeSprUxJc5bhNjAh8JctmhWyqRxz2viDeqKTOzVsOFmB6lUj4rgovEQBuLot3x/g== X-Received: by 2002:a5d:47c1:0:b0:21b:c5c0:b9dc with SMTP id o1-20020a5d47c1000000b0021bc5c0b9dcmr3576930wrc.189.1657290035053; Fri, 08 Jul 2022 07:20:35 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id q5-20020adff945000000b0021b9585276dsm40393924wrr.101.2022.07.08.07.20.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Jul 2022 07:20:34 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Phillip Wood , Jeff King , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 3/7] git-shared-util.h: add G*() versions of *ALLOC_*() Date: Fri, 8 Jul 2022 16:20:15 +0200 Message-Id: X-Mailer: git-send-email 2.37.0.913.g189dca38629 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Add "gently" versions of ALLOC_ARRAY(), CALLOC_ARRAY() etc. using the naming convention G*() as a shorthand for "GENTLY_*()". Nothing uses these functions yet, but as we'll see in subsequent commit(s) we're able to convert things that need e.g. non-fatal "ALLOC_GROW" behavior over to this. Signed-off-by: Ævar Arnfjörð Bjarmason --- git-shared-util.h | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/git-shared-util.h b/git-shared-util.h index 7b4479a0f72..718a8e00732 100644 --- a/git-shared-util.h +++ b/git-shared-util.h @@ -8,8 +8,11 @@ #define FREE_AND_NULL(p) do { free(p); (p) = NULL; } while (0) #define ALLOC_ARRAY(x, alloc) (x) = xmalloc(st_mult(sizeof(*(x)), (alloc))) +#define GALLOC_ARRAY(x, alloc) (x) = malloc(st_mult(sizeof(*(x)), (alloc))) #define CALLOC_ARRAY(x, alloc) (x) = xcalloc((alloc), sizeof(*(x))) +#define GCALLOC_ARRAY(x, alloc) (x) = calloc((alloc), sizeof(*(x))) #define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), st_mult(sizeof(*(x)), (alloc))) +#define GREALLOC_ARRAY(x, alloc) (x) = realloc((x), st_mult(sizeof(*(x)), (alloc))) #define COPY_ARRAY(dst, src, n) copy_array((dst), (src), (n), sizeof(*(dst)) + \ BUILD_ASSERT_OR_ZERO(sizeof(*(dst)) == sizeof(*(src)))) @@ -71,17 +74,25 @@ static inline void move_array(void *dst, const void *src, size_t n, size_t size) * added niceties. * * DO NOT USE any expression with side-effect for 'x', 'nr', or 'alloc'. + * + * GALLOC_GROW() behaves like ALLOC_GROW(), except that in malloc() + * failure we'll return NULL rather than dying. */ -#define ALLOC_GROW(x, nr, alloc) \ +#define ALLOC_GROW_1(x, nr, alloc, gently) \ do { \ if ((nr) > alloc) { \ if (alloc_nr(alloc) < (nr)) \ alloc = (nr); \ else \ alloc = alloc_nr(alloc); \ - REALLOC_ARRAY(x, alloc); \ + if (gently) \ + GREALLOC_ARRAY(x, alloc); \ + else \ + REALLOC_ARRAY(x, alloc); \ } \ } while (0) +#define ALLOC_GROW(x, nr, alloc) ALLOC_GROW_1(x, nr, alloc, 0) +#define GALLOC_GROW(x, nr, alloc) ALLOC_GROW_1(x, nr, alloc, 1) /* * Similar to ALLOC_GROW but handles updating of the nr value and From patchwork Fri Jul 8 14:20:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12911212 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 BB171CCA480 for ; Fri, 8 Jul 2022 14:20:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238043AbiGHOU5 (ORCPT ); Fri, 8 Jul 2022 10:20:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45168 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237903AbiGHOUj (ORCPT ); Fri, 8 Jul 2022 10:20:39 -0400 Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F19DD222A6 for ; Fri, 8 Jul 2022 07:20:37 -0700 (PDT) Received: by mail-wr1-x42b.google.com with SMTP id n10so5467695wrc.4 for ; Fri, 08 Jul 2022 07:20:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=DXlx9XaXuNvNLQdsz6/VrOiQn03h2ExIBrjBwbaZ+Pc=; b=JGjPxpOIInKN3BntO/HlKXs7qvmfsq9IAiz6RICjpDWfgGtnZt2YmerWLUD9jG//6k pqS14fUBHJrtvPAsnUE+zDUU/r82DDld801gzh6Jr5opCZ48VhElXTBni+DdawuDWDmj UorqBx9qgIhqrCFYMQY8i18C0TvTLTDcLJSnX7Z8KzT8IPDwoKvCIbxOxJBwvrGQ/Zr3 kNiIvqpsL+r5p2TjRaD3mZ193ZIC9j5+HezgQAoUrckJ7bh65JvXX0AyS/hcgHeXFlNH 8zqCuoH5pv98I2pLAcc/J9G/1GaF2qCVpOUs+pRicr8eXU0IS/nSQUQdAIai5uxYIEu1 sdMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=DXlx9XaXuNvNLQdsz6/VrOiQn03h2ExIBrjBwbaZ+Pc=; b=BgV2HXur168cntIUgJ2+lCUpmz+VL1i6ilSrf0ouhupYj3fHpEedWS9p6ecD8zkKR4 3N+CSzTePztbuXSUq/DmpiCTdoFwGwb+kP+sUqGo7Bbaa5b61WNDdpg32wE/o4CNQU5q kXKuFJcRwY6r8QZGsCSNKuDTvw7kQbLxqiJzr9biqAmTzipp9TXsCtm4Cl4gUp30Pgtu Dteax5M7lGm9ei57gJeF+w35XxQzoqU2ZdtXQIv8CL9JLN1rHFMvFTbQgYOk5tENMwJs lJsastaU4hJjntqMERBkn6Dyx4Awj5aYY7i80CN0gJkwGWSTpsBVGDlEQguZLE8GCf/Q lBeA== X-Gm-Message-State: AJIora8yaL5cxh7zB12nANs9BYa5sFZCycXb9r0QGlGZJZGkVP8hXYJe k0P5k9iq84hGQg73o8Dx9bYlnOLnuyjCgA== X-Google-Smtp-Source: AGRyM1s2DdsERLWJlmztA2xSCBL5/x0A2GaBgjD+w6qIkHRF3xnyrLWp0hph80ZYgd6p40t6AhGj+A== X-Received: by 2002:adf:f48e:0:b0:21d:6562:4717 with SMTP id l14-20020adff48e000000b0021d65624717mr3488690wro.428.1657290036139; Fri, 08 Jul 2022 07:20:36 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id q5-20020adff945000000b0021b9585276dsm40393924wrr.101.2022.07.08.07.20.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Jul 2022 07:20:35 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Phillip Wood , Jeff King , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 4/7] xdiff: use G[C]ALLOC_ARRAY(), not XDL_CALLOC_ARRAY() Date: Fri, 8 Jul 2022 16:20:16 +0200 Message-Id: X-Mailer: git-send-email 2.37.0.913.g189dca38629 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Use the newly created GCALLOC_ARRAY() helpers rather than the recently introduced XDL_[C]ALLOC_ARRAY(). As shown in this diff the calling convention differs, we cannot use GCALLOC_ARRAY() as an expression, but that's an advantage in not having to relay the "sizeof()" down via a wrapper function. This also: * Fixes long-standing potential overflow issues, as we're using st_mult() in the underlying G_[C]ALLOC(). Note that the * Slightly optimizes the "XDL_CALLOC_ARRAY", as we'll now use calloc() rather than malloc() + memset() (although smart compilers will probably do the same for both...). * Changes the "XDL_CALLOC_ARRAY" behavior where we'd shortcut if the size was already large enough, but this behavior was changed when XDL_ALLOC_ARRAY() was introduced, so this is safe. Signed-off-by: Ævar Arnfjörð Bjarmason --- xdiff/xdiffi.c | 3 ++- xdiff/xhistogram.c | 9 ++++++--- xdiff/xmacros.h | 12 ------------ xdiff/xpatience.c | 6 ++++-- xdiff/xprepare.c | 24 ++++++++++++++++-------- 5 files changed, 28 insertions(+), 26 deletions(-) diff --git a/xdiff/xdiffi.c b/xdiff/xdiffi.c index 6fded43e87d..077cc456087 100644 --- a/xdiff/xdiffi.c +++ b/xdiff/xdiffi.c @@ -333,7 +333,8 @@ int xdl_do_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, * One is to store the forward path and one to store the backward path. */ ndiags = xe->xdf1.nreff + xe->xdf2.nreff + 3; - if (!XDL_ALLOC_ARRAY(kvd, 2 * ndiags + 2)) + GALLOC_ARRAY(kvd, 2 * ndiags + 2); + if (!kvd) return -1; kvdf = kvd; kvdb = kvdf + ndiags; diff --git a/xdiff/xhistogram.c b/xdiff/xhistogram.c index df909004c10..f20592bfbdd 100644 --- a/xdiff/xhistogram.c +++ b/xdiff/xhistogram.c @@ -266,14 +266,17 @@ static int find_lcs(xpparam_t const *xpp, xdfenv_t *env, index.table_bits = xdl_hashbits(count1); index.records_size = 1 << index.table_bits; - if (!XDL_CALLOC_ARRAY(index.records, index.records_size)) + GCALLOC_ARRAY(index.records, index.records_size); + if (!index.records) goto cleanup; index.line_map_size = count1; - if (!XDL_CALLOC_ARRAY(index.line_map, index.line_map_size)) + GCALLOC_ARRAY(index.line_map, index.line_map_size); + if (!index.line_map) goto cleanup; - if (!XDL_CALLOC_ARRAY(index.next_ptrs, index.line_map_size)) + GCALLOC_ARRAY(index.next_ptrs, index.line_map_size); + if (!index.next_ptrs) goto cleanup; /* lines / 4 + 1 comes from xprepare.c:xdl_prepare_ctx() */ diff --git a/xdiff/xmacros.h b/xdiff/xmacros.h index d13a6724629..75506bdf17e 100644 --- a/xdiff/xmacros.h +++ b/xdiff/xmacros.h @@ -49,18 +49,6 @@ do { \ ((unsigned long) __p[2]) << 16 | ((unsigned long) __p[3]) << 24; \ } while (0) -/* Allocate an array of nr elements, returns NULL on failure */ -#define XDL_ALLOC_ARRAY(p, nr) \ - ((p) = SIZE_MAX / sizeof(*(p)) >= (size_t)(nr) \ - ? xdl_malloc((nr) * sizeof(*(p))) \ - : NULL) - -/* Allocate an array of nr zeroed out elements, returns NULL on failure */ -#define XDL_CALLOC_ARRAY(p, nr) \ - (XDL_ALLOC_ARRAY((p), (nr)) \ - ? memset((p), 0, (nr) * sizeof(*(p))) \ - : NULL) - /* * Ensure array p can accommodate at least nr elements, growing the * array and updating alloc (which is the number of allocated diff --git a/xdiff/xpatience.c b/xdiff/xpatience.c index fe39c2978cb..bb328d9f852 100644 --- a/xdiff/xpatience.c +++ b/xdiff/xpatience.c @@ -151,7 +151,8 @@ static int fill_hashmap(mmfile_t *file1, mmfile_t *file2, /* We know exactly how large we want the hash map */ result->alloc = count1 * 2; - if (!XDL_CALLOC_ARRAY(result->entries, result->alloc)) + GCALLOC_ARRAY(result->entries, result->alloc); + if (!result->entries) return -1; /* First, fill with entries from the first file */ @@ -208,7 +209,8 @@ static int find_longest_common_sequence(struct hashmap *map, struct entry **res) */ int anchor_i = -1; - if (!XDL_ALLOC_ARRAY(sequence, map->nr)) + GALLOC_ARRAY(sequence, map->nr); + if (!sequence) return -1; for (entry = map->first; entry; entry = entry->next) { diff --git a/xdiff/xprepare.c b/xdiff/xprepare.c index c84549f6c50..d6cbee32a2a 100644 --- a/xdiff/xprepare.c +++ b/xdiff/xprepare.c @@ -78,15 +78,17 @@ static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags) { return -1; } - if (!XDL_CALLOC_ARRAY(cf->rchash, cf->hsize)) { + GCALLOC_ARRAY(cf->rchash, cf->hsize); + if (!cf->rchash) { xdl_cha_free(&cf->ncha); return -1; } cf->alloc = size; - if (!XDL_ALLOC_ARRAY(cf->rcrecs, cf->alloc)) { + GALLOC_ARRAY(cf->rcrecs, cf->alloc); + if (!cf->rcrecs) { xdl_free(cf->rchash); xdl_cha_free(&cf->ncha); return -1; @@ -170,12 +172,14 @@ static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_ if (xdl_cha_init(&xdf->rcha, sizeof(xrecord_t), narec / 4 + 1) < 0) goto abort; - if (!XDL_ALLOC_ARRAY(recs, narec)) + GALLOC_ARRAY(recs, narec); + if (!recs) goto abort; hbits = xdl_hashbits((unsigned int) narec); hsize = 1 << hbits; - if (!XDL_CALLOC_ARRAY(rhash, hsize)) + GCALLOC_ARRAY(rhash, hsize); + if (!rhash) goto abort; nrec = 0; @@ -196,14 +200,17 @@ static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_ } } - if (!XDL_CALLOC_ARRAY(rchg, nrec + 2)) + GCALLOC_ARRAY(rchg, nrec + 2); + if (!rchg) goto abort; if ((XDF_DIFF_ALG(xpp->flags) != XDF_PATIENCE_DIFF) && (XDF_DIFF_ALG(xpp->flags) != XDF_HISTOGRAM_DIFF)) { - if (!XDL_ALLOC_ARRAY(rindex, nrec + 1)) + GALLOC_ARRAY(rindex, nrec + 1); + if (!rindex) goto abort; - if (!XDL_ALLOC_ARRAY(ha, nrec + 1)) + GALLOC_ARRAY(ha, nrec + 1); + if (!ha) goto abort; } @@ -369,7 +376,8 @@ static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xd xdlclass_t *rcrec; char *dis, *dis1, *dis2; - if (!XDL_CALLOC_ARRAY(dis, xdf1->nrec + xdf2->nrec + 2)) + GCALLOC_ARRAY(dis, xdf1->nrec + xdf2->nrec + 2); + if (!dis) return -1; dis1 = dis; dis2 = dis1 + xdf1->nrec + 1; From patchwork Fri Jul 8 14:20:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12911211 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 1C6C4C43334 for ; Fri, 8 Jul 2022 14:20:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237747AbiGHOUz (ORCPT ); Fri, 8 Jul 2022 10:20:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45166 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237901AbiGHOUi (ORCPT ); Fri, 8 Jul 2022 10:20:38 -0400 Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 02C3E24F15 for ; Fri, 8 Jul 2022 07:20:38 -0700 (PDT) Received: by mail-wr1-x42b.google.com with SMTP id h17so17614898wrx.0 for ; Fri, 08 Jul 2022 07:20:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=P6/CWBHEQn6aKPGvhsB9HgpFaF6KI7T7maR1L0CjhKY=; b=EOsttIjxkTWxNbjLK0/LI1Nde0Y2CyjJ7bvA9yo2JdGSrXqHXYC8ohjnHR2H1sDdn+ EiWp5AyAxYmd+KDa66oHmNkGJs09ROLqT3Ve6CbhlKSGpJd49+OLeSCEBpV/+UClJ97f 47WxS0q2Q3uX/lA1wh+p2RA4UH5YN8JHbtSwpmTHl+6WuaWcrhU0tgBrw/iiE1X5pHau Th9/i8i3kxFdyjxzlYGvfnLpCGzhVnvus06mn4ZAyfH4s0EU0YAIFYMSm1klbI00M3qe tqtsxO6B1WCP29XTQ8eKvqc/NJqgQnOuGCWvBCDGPl4VO57+5LdmH3oSxwfeUoD9fDR0 uJ4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=P6/CWBHEQn6aKPGvhsB9HgpFaF6KI7T7maR1L0CjhKY=; b=CkvwSdw2M4uhUWD/wxbjLgHtf28PLgkG/xwUBWLqfa/6kTSwqAlkzITn3xscZnLNH4 R+tQuas1BZMQSUBNr0HCgkGB7fpkiXUKk6fVXoNm8ERxytebGfwWKITLIz543QxfNCfw R5+OvRf9P8JqbXhHgJj0bYlblswvXTT7GmMHAy1KfeH2vl4NYYQo9+MK7LRqzJSRDTOE TkPeZqVwNs/2WP9/4nRzcEB4n9/VeIdAuvEdG7OvKNHU7Qw8HpzrgH+DHJreQnGWT5er lyJj1HLLHTr53eXWcL6aKzOaRdldip/3aZ8gYAh1Auzj+cqx1kYfYMO3DttN9pJ/JCX3 x0DQ== X-Gm-Message-State: AJIora9ekGmeJnB7lU36T0HAqil109AardDDHu+6RWPb2pzd0Qi8QpZC KAjOnXaB0JcERV9fHXm2FtUhStL0/NszAA== X-Google-Smtp-Source: AGRyM1tuY9jgWKn2fU65RsYH6ZgrSloKgJI1i6ZUwFH5KyYm0K6WmQkopRRTa4R2DTf9wp3D6WEBpw== X-Received: by 2002:adf:f90d:0:b0:20c:de32:4d35 with SMTP id b13-20020adff90d000000b0020cde324d35mr3479689wrr.583.1657290037251; Fri, 08 Jul 2022 07:20:37 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id q5-20020adff945000000b0021b9585276dsm40393924wrr.101.2022.07.08.07.20.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Jul 2022 07:20:36 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Phillip Wood , Jeff King , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 5/7] xdiff: use GALLOC_GROW(), not XDL_ALLOC_GROW() Date: Fri, 8 Jul 2022 16:20:17 +0200 Message-Id: X-Mailer: git-send-email 2.37.0.913.g189dca38629 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Replace the recently introduced XDL_ALLOC_GROW() with invocations of the GALLOC_GROW() from git-shared-util.h. As this change shows the macro + function indirection of XDL_ALLOC_GROW() is something we needed only because the two callsites we used it in wanted to use it as an expression, and we thus had to pass the "sizeof" down. Let's just check the value afterwards instead, which allows us to use the shared macro, we can also remove xdl_reallo(), this was its last user. Signed-off-by: Ævar Arnfjörð Bjarmason --- xdiff/xdiff.h | 1 - xdiff/xmacros.h | 11 ----------- xdiff/xprepare.c | 8 +++++--- xdiff/xutils.c | 17 ----------------- xdiff/xutils.h | 2 -- 5 files changed, 5 insertions(+), 34 deletions(-) diff --git a/xdiff/xdiff.h b/xdiff/xdiff.h index 72e25a9ffa5..832cf9d977e 100644 --- a/xdiff/xdiff.h +++ b/xdiff/xdiff.h @@ -121,7 +121,6 @@ typedef struct s_bdiffparam { #define xdl_malloc(x) xmalloc(x) #define xdl_free(ptr) free(ptr) -#define xdl_realloc(ptr,x) xrealloc(ptr,x) void *xdl_mmfile_first(mmfile_t *mmf, long *size); long xdl_mmfile_size(mmfile_t *mmf); diff --git a/xdiff/xmacros.h b/xdiff/xmacros.h index 75506bdf17e..6a6b3057375 100644 --- a/xdiff/xmacros.h +++ b/xdiff/xmacros.h @@ -48,15 +48,4 @@ do { \ (v) = (unsigned long) __p[0] | ((unsigned long) __p[1]) << 8 | \ ((unsigned long) __p[2]) << 16 | ((unsigned long) __p[3]) << 24; \ } while (0) - -/* - * Ensure array p can accommodate at least nr elements, growing the - * array and updating alloc (which is the number of allocated - * elements) as necessary. Frees p and returns -1 on failure, returns - * 0 on success - */ -#define XDL_ALLOC_GROW(p, nr, alloc) \ - (-!((nr) <= (alloc) || \ - ((p) = xdl_alloc_grow_helper((p), (nr), &(alloc), sizeof(*(p)))))) - #endif /* #if !defined(XMACROS_H) */ diff --git a/xdiff/xprepare.c b/xdiff/xprepare.c index d6cbee32a2a..4182d9e1c0a 100644 --- a/xdiff/xprepare.c +++ b/xdiff/xprepare.c @@ -128,8 +128,9 @@ static int xdl_classify_record(unsigned int pass, xdlclassifier_t *cf, xrecord_t return -1; } rcrec->idx = cf->count++; - if (XDL_ALLOC_GROW(cf->rcrecs, cf->count, cf->alloc)) - return -1; + GALLOC_GROW(cf->rcrecs, cf->count, cf->alloc); + if (!cf->rcrecs) + return -1; cf->rcrecs[rcrec->idx] = rcrec; rcrec->line = line; rcrec->size = rec->size; @@ -187,7 +188,8 @@ static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_ for (top = blk + bsize; cur < top; ) { prev = cur; hav = xdl_hash_record(&cur, top, xpp->flags); - if (XDL_ALLOC_GROW(recs, nrec + 1, narec)) + GALLOC_GROW(recs, nrec + 1, narec); + if (!recs) goto abort; if (!(crec = xdl_cha_alloc(&xdf->rcha))) goto abort; diff --git a/xdiff/xutils.c b/xdiff/xutils.c index a6f10353cff..c0cd5338c4e 100644 --- a/xdiff/xutils.c +++ b/xdiff/xutils.c @@ -436,20 +436,3 @@ int xdl_fall_back_diff(xdfenv_t *diff_env, xpparam_t const *xpp, return status; } - -void* xdl_alloc_grow_helper(void *p, long nr, long *alloc, size_t size) -{ - void *tmp = NULL; - size_t n = ((LONG_MAX - 16) / 2 >= *alloc) ? 2 * *alloc + 16 : LONG_MAX; - if (nr > n) - n = nr; - if (SIZE_MAX / size >= n) - tmp = xdl_realloc(p, n * size); - if (tmp) { - *alloc = n; - } else { - xdl_free(p); - *alloc = 0; - } - return tmp; -} diff --git a/xdiff/xutils.h b/xdiff/xutils.h index fd0bba94e8b..7ae3f897bef 100644 --- a/xdiff/xutils.h +++ b/xdiff/xutils.h @@ -42,7 +42,5 @@ int xdl_emit_hunk_hdr(long s1, long c1, long s2, long c2, int xdl_fall_back_diff(xdfenv_t *diff_env, xpparam_t const *xpp, int line1, int count1, int line2, int count2); -/* Do not call this function, use XDL_ALLOC_GROW instead */ -void* xdl_alloc_grow_helper(void* p, long nr, long* alloc, size_t size); #endif /* #if !defined(XUTILS_H) */ From patchwork Fri Jul 8 14:20:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12911213 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 DAD6AC43334 for ; Fri, 8 Jul 2022 14:20:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237969AbiGHOU7 (ORCPT ); Fri, 8 Jul 2022 10:20:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45198 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237921AbiGHOUl (ORCPT ); Fri, 8 Jul 2022 10:20:41 -0400 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3596E255A1 for ; Fri, 8 Jul 2022 07:20:40 -0700 (PDT) Received: by mail-wr1-x42e.google.com with SMTP id f2so25381013wrr.6 for ; Fri, 08 Jul 2022 07:20:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=sXsg+U9/HWR7ASkvwNAaVPykH4FYO/GZK97OTlELXeI=; b=Trcp3Cn4eSTjeEId/7Ei7kEFddDK4MEeYI6Bb/R6RpssyWV9rWnnC1xQJXAe5UTIfd sgDxKmNq2Gp307kLsqygyApbBG7mnf1PFlMCN/Y8tfzpS2miFl7gk0MYykmdliqj33X3 mtUP6zKHIon600kraYIxr/adVP+9X10ut6EiykzfUm4sKcwmoOUxSfVB8i7Vg1K7h9IZ Dt6KF3H7qtfMqF+xuRoTTr34cYC/E8l1eZO8YXLZs5RXT0QSmRRxGsEZbzx2M92iqCiP NTQ7CZSNMRTo+Wwuj/Yk3GadX33OpXznDkgciAK923cMk/Xrzo6KLx1QPvBxVA1uNcI0 Q8xA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=sXsg+U9/HWR7ASkvwNAaVPykH4FYO/GZK97OTlELXeI=; b=mFBC9v5YdRbBPs0b1U+rOjjo3sKhSUx1vJVtvAtzD30IRF5RKhiMqyqeyzb7Pf9HO6 ydQ5ShcRHj9Q0d+EhtQsN1oRkG447xcBKt4jozkxhNHTaBST4W0mtkeWXSc2kPU7LQ1S 63+HwomhlMKQ/hu5fzAL6t7bsDL93VWnEjhqhv6kzdyGVAP7J7IbhdkXt/iwjTPms1eb y5PJILXT8Vm+8ZeVcY/ujMer800vCJPxH0d2FjOF83mvvOmdE086EJBzuZDjM76bj/i3 WDfVafcClDEgOmPVTf0Z4N5Gol7KWQlZDQuW6WEOp6oXHI9YABcexL70SNlA00cjMTR2 piRQ== X-Gm-Message-State: AJIora+U7Im8EQq+0FIeElJ0FBx/2a+y9a5uqMO8bDTcVS6s9w9pyYJU 0MSEiZysc88363PkJ+NeTB8xsP3I0NE6mw== X-Google-Smtp-Source: AGRyM1vl5hwFil+cCA44PMD0HM8RiMM4KvAagXVjSnzpEAYJ6sTUIq3X0Cf3k2dFYIQJPyyCCVPXEQ== X-Received: by 2002:adf:d84b:0:b0:21d:6efd:efa with SMTP id k11-20020adfd84b000000b0021d6efd0efamr3586520wrl.384.1657290038458; Fri, 08 Jul 2022 07:20:38 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id q5-20020adff945000000b0021b9585276dsm40393924wrr.101.2022.07.08.07.20.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Jul 2022 07:20:37 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Phillip Wood , Jeff King , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 6/7] xdiff: remove xdl_malloc() wrapper, use malloc(), not xmalloc() Date: Fri, 8 Jul 2022 16:20:18 +0200 Message-Id: X-Mailer: git-send-email 2.37.0.913.g189dca38629 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org As noted in 36c83197249 (xdiff: use xmalloc/xrealloc, 2019-04-11) the reason we have xdl_malloc() in the first place was to make the xdiff code safer, it was not handling some allocation failures correctly at the time. But as noted in that commit doing this was intended as a stopgap, as various code in xdiff/* did not correctly handle allocation failures, and would have segfaulted if malloc() returned NULL. But since then and after preceding commits we can be confident that malloc() failures are handled correctly. All of these users of xdl_malloc() are checking their return values, and we're returning -1 (or similar ) to the top-level in case of failures. This also has the big advantage of making the compiler and analysis tools less confused, and potentially avoiding undefined (compiler) behavior. I.e. we define our own xmalloc() to call die() on failure, and that function uses the non-standard "noreturn" attribute on our most common compiler targets. But xdl_malloc()'s use conflicted with that, confusing both human readers of this code, and potentially compilers as well. Signed-off-by: Ævar Arnfjörð Bjarmason --- xdiff/xdiff.h | 1 - xdiff/xdiffi.c | 2 +- xdiff/xmerge.c | 10 +++++----- xdiff/xutils.c | 2 +- 4 files changed, 7 insertions(+), 8 deletions(-) diff --git a/xdiff/xdiff.h b/xdiff/xdiff.h index 832cf9d977e..df048e0099b 100644 --- a/xdiff/xdiff.h +++ b/xdiff/xdiff.h @@ -119,7 +119,6 @@ typedef struct s_bdiffparam { } bdiffparam_t; -#define xdl_malloc(x) xmalloc(x) #define xdl_free(ptr) free(ptr) void *xdl_mmfile_first(mmfile_t *mmf, long *size); diff --git a/xdiff/xdiffi.c b/xdiff/xdiffi.c index 077cc456087..6590811634f 100644 --- a/xdiff/xdiffi.c +++ b/xdiff/xdiffi.c @@ -368,7 +368,7 @@ int xdl_do_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, static xdchange_t *xdl_add_change(xdchange_t *xscr, long i1, long i2, long chg1, long chg2) { xdchange_t *xch; - if (!(xch = (xdchange_t *) xdl_malloc(sizeof(xdchange_t)))) + if (!(xch = (xdchange_t *) malloc(sizeof(xdchange_t)))) return NULL; xch->next = xscr; diff --git a/xdiff/xmerge.c b/xdiff/xmerge.c index ac0cf52f3be..676ad39020d 100644 --- a/xdiff/xmerge.c +++ b/xdiff/xmerge.c @@ -60,7 +60,7 @@ static int xdl_append_merge(xdmerge_t **merge, int mode, m->chg1 = i1 + chg1 - m->i1; m->chg2 = i2 + chg2 - m->i2; } else { - m = xdl_malloc(sizeof(xdmerge_t)); + m = malloc(sizeof(xdmerge_t)); if (!m) return -1; m->next = NULL; @@ -409,7 +409,7 @@ static int xdl_refine_conflicts(xdfenv_t *xe1, xdfenv_t *xe2, xdmerge_t *m, m->i2 = xscr->i2 + i2; m->chg2 = xscr->chg2; while (xscr->next) { - xdmerge_t *m2 = xdl_malloc(sizeof(xdmerge_t)); + xdmerge_t *m2 = malloc(sizeof(xdmerge_t)); if (!m2) { xdl_free_env(&xe); xdl_free_script(x); @@ -670,7 +670,7 @@ static int xdl_do_merge(xdfenv_t *xe1, xdchange_t *xscr1, ancestor_name, favor, changes, NULL, style, marker_size); - result->ptr = xdl_malloc(size); + result->ptr = malloc(size); if (!result->ptr) { xdl_cleanup_merge(changes); return -1; @@ -718,14 +718,14 @@ int xdl_merge(mmfile_t *orig, mmfile_t *mf1, mmfile_t *mf2, } status = 0; if (!xscr1) { - result->ptr = xdl_malloc(mf2->size); + result->ptr = malloc(mf2->size); if (!result->ptr) goto out; status = 0; memcpy(result->ptr, mf2->ptr, mf2->size); result->size = mf2->size; } else if (!xscr2) { - result->ptr = xdl_malloc(mf1->size); + result->ptr = malloc(mf1->size); if (!result->ptr) goto out; status = 0; diff --git a/xdiff/xutils.c b/xdiff/xutils.c index c0cd5338c4e..865e08f0e93 100644 --- a/xdiff/xutils.c +++ b/xdiff/xutils.c @@ -98,7 +98,7 @@ void *xdl_cha_alloc(chastore_t *cha) { void *data; if (!(ancur = cha->ancur) || ancur->icurr == cha->nsize) { - if (!(ancur = (chanode_t *) xdl_malloc(sizeof(chanode_t) + cha->nsize))) { + if (!(ancur = (chanode_t *) malloc(sizeof(chanode_t) + cha->nsize))) { return NULL; } From patchwork Fri Jul 8 14:20:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12911214 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 47F81C433EF for ; Fri, 8 Jul 2022 14:21:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238109AbiGHOVA (ORCPT ); Fri, 8 Jul 2022 10:21:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45316 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237853AbiGHOUy (ORCPT ); Fri, 8 Jul 2022 10:20:54 -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 604F22C126 for ; Fri, 8 Jul 2022 07:20:40 -0700 (PDT) Received: by mail-wr1-x429.google.com with SMTP id q9so30762748wrd.8 for ; Fri, 08 Jul 2022 07:20:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=kK05ySLlHXevAX+06icRmA94Eu/2FwHXfFeZ+orFlqw=; b=Y0YugUoYaLlCXEmyS0U9IRJUfbBjnyQlIGD2V9QjtdxIv88ZO9uOLtCiYFBM0Ov822 5N2IUNuM/s1OpbxTjTS1NJH488usnKILMkpxJhrV1JeliQ65rHxG96rjTAZcaltG3AVm i2dceyzEqJifTByHNzDESXIkB2jBCsPfc0rOiXuvtIeerAMT6XB4jZbmSH3yiuPvd9e8 fEmXUh9m7vLqiInq97asaCyG/7TIoOrMzHPEt9VlkObaBS5IWO79CfHf1i58Z1kr23kJ gqydYsHV6ZJRK7c5fOnb0RtuLjYmklcuo0W/tZVfGL7+gregqVbH94aeRvJGOF6APqq2 XuVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=kK05ySLlHXevAX+06icRmA94Eu/2FwHXfFeZ+orFlqw=; b=w319FJh7+VjaKVjbc0wBjATbirHWBtCfdtnvWP6SxBQxy7KZakewO9i99Z81TEXySM N4vhd+u4NSZQW61ZuEfKdNl3ePLd7dz+Yu9fuDwaK5Hogzu6OZXfRBP9GozMi01RnrKd 1CaLFudJoVSYTgs0aVZJNhn9NYgZEy3uOTrWfD4JlrvzOLHpJrhfR0wd44CsnR39UDt3 dd3NRJ/pOyEre7MvXSWFVekS1PzHt12TW37GMSjK3D9x5RZVwYIxNC6TRg+5q3w40OmI Vpi2LXmCugTolzSm9ilK9QEvn8FxsQfoIfZv62Hr2MTgDGn73Rt/qEeaPce3FKRZfrOC mA5g== X-Gm-Message-State: AJIora8JzIzjaC7mZF+LtF38BkXU+MutXp3TSz9shpNASJDgzMhGJo8+ VRUpeIQ15FFMTeKX1F8kZNtefP8v40PgTA== X-Google-Smtp-Source: AGRyM1uXakj9vkuBwO8gN3/AufQWt+I9yfyjiB0YKu7PDNdx6FEbrp+CgzSvANqMWCn5S6fxTW/l+w== X-Received: by 2002:a5d:584f:0:b0:21b:a557:98fa with SMTP id i15-20020a5d584f000000b0021ba55798famr3522336wrf.462.1657290039575; Fri, 08 Jul 2022 07:20:39 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id q5-20020adff945000000b0021b9585276dsm40393924wrr.101.2022.07.08.07.20.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Jul 2022 07:20:38 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Phillip Wood , Jeff King , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 7/7] xdiff: remove xdl_free(), use free() instead Date: Fri, 8 Jul 2022 16:20:19 +0200 Message-Id: X-Mailer: git-send-email 2.37.0.913.g189dca38629 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Remove the xdl_free() wrapper macro in favor of using free() directly. The wrapper macro was brought in with the initial import of xdiff/ in 3443546f6ef (Use a *real* built-in diff generator, 2006-03-24). As subsequent discussions on the topic[1] have made clear there's no reason to use this wrapper. Both git itself as well as any external users such as libgit2 compile the xdiff/* code as part of their own compilation, and can thus find the right malloc() and free() at link-time. When compiling git we already find a custom malloc() and free() e.g. if compiled with USE_NED_ALLOCATOR=YesPlease. 1. https://lore.kernel.org/git/220415.867d7qbaad.gmgdl@evledraar.gmail.com/ Signed-off-by: Ævar Arnfjörð Bjarmason --- xdiff/xdiff.h | 3 --- xdiff/xdiffi.c | 4 ++-- xdiff/xhistogram.c | 6 +++--- xdiff/xpatience.c | 8 ++++---- xdiff/xprepare.c | 28 ++++++++++++++-------------- xdiff/xutils.c | 2 +- 6 files changed, 24 insertions(+), 27 deletions(-) diff --git a/xdiff/xdiff.h b/xdiff/xdiff.h index df048e0099b..a37d89fcdaf 100644 --- a/xdiff/xdiff.h +++ b/xdiff/xdiff.h @@ -118,9 +118,6 @@ typedef struct s_bdiffparam { long bsize; } bdiffparam_t; - -#define xdl_free(ptr) free(ptr) - void *xdl_mmfile_first(mmfile_t *mmf, long *size); long xdl_mmfile_size(mmfile_t *mmf); diff --git a/xdiff/xdiffi.c b/xdiff/xdiffi.c index 6590811634f..375bb81a8aa 100644 --- a/xdiff/xdiffi.c +++ b/xdiff/xdiffi.c @@ -359,7 +359,7 @@ int xdl_do_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, res = xdl_recs_cmp(&dd1, 0, dd1.nrec, &dd2, 0, dd2.nrec, kvdf, kvdb, (xpp->flags & XDF_NEED_MINIMAL) != 0, &xenv); - xdl_free(kvd); + free(kvd); return res; } @@ -960,7 +960,7 @@ void xdl_free_script(xdchange_t *xscr) { while ((xch = xscr) != NULL) { xscr = xscr->next; - xdl_free(xch); + free(xch); } } diff --git a/xdiff/xhistogram.c b/xdiff/xhistogram.c index f20592bfbdd..be35d9c9697 100644 --- a/xdiff/xhistogram.c +++ b/xdiff/xhistogram.c @@ -240,9 +240,9 @@ static int fall_back_to_classic_diff(xpparam_t const *xpp, xdfenv_t *env, static inline void free_index(struct histindex *index) { - xdl_free(index->records); - xdl_free(index->line_map); - xdl_free(index->next_ptrs); + free(index->records); + free(index->line_map); + free(index->next_ptrs); xdl_cha_free(&index->rcha); } diff --git a/xdiff/xpatience.c b/xdiff/xpatience.c index bb328d9f852..8fffd2b8297 100644 --- a/xdiff/xpatience.c +++ b/xdiff/xpatience.c @@ -233,7 +233,7 @@ static int find_longest_common_sequence(struct hashmap *map, struct entry **res) /* No common unique lines were found */ if (!longest) { *res = NULL; - xdl_free(sequence); + free(sequence); return 0; } @@ -245,7 +245,7 @@ static int find_longest_common_sequence(struct hashmap *map, struct entry **res) entry = entry->previous; } *res = entry; - xdl_free(sequence); + free(sequence); return 0; } @@ -358,7 +358,7 @@ static int patience_diff(mmfile_t *file1, mmfile_t *file2, env->xdf1.rchg[line1++ - 1] = 1; while(count2--) env->xdf2.rchg[line2++ - 1] = 1; - xdl_free(map.entries); + free(map.entries); return 0; } @@ -372,7 +372,7 @@ static int patience_diff(mmfile_t *file1, mmfile_t *file2, result = fall_back_to_classic_diff(&map, line1, count1, line2, count2); out: - xdl_free(map.entries); + free(map.entries); return result; } diff --git a/xdiff/xprepare.c b/xdiff/xprepare.c index 4182d9e1c0a..169629761c0 100644 --- a/xdiff/xprepare.c +++ b/xdiff/xprepare.c @@ -89,7 +89,7 @@ static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags) { GALLOC_ARRAY(cf->rcrecs, cf->alloc); if (!cf->rcrecs) { - xdl_free(cf->rchash); + free(cf->rchash); xdl_cha_free(&cf->ncha); return -1; } @@ -102,8 +102,8 @@ static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags) { static void xdl_free_classifier(xdlclassifier_t *cf) { - xdl_free(cf->rcrecs); - xdl_free(cf->rchash); + free(cf->rcrecs); + free(cf->rchash); xdl_cha_free(&cf->ncha); } @@ -230,11 +230,11 @@ static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_ return 0; abort: - xdl_free(ha); - xdl_free(rindex); - xdl_free(rchg); - xdl_free(rhash); - xdl_free(recs); + free(ha); + free(rindex); + free(rchg); + free(rhash); + free(recs); xdl_cha_free(&xdf->rcha); return -1; } @@ -242,11 +242,11 @@ static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_ static void xdl_free_ctx(xdfile_t *xdf) { - xdl_free(xdf->rhash); - xdl_free(xdf->rindex); - xdl_free(xdf->rchg - 1); - xdl_free(xdf->ha); - xdl_free(xdf->recs); + free(xdf->rhash); + free(xdf->rindex); + free(xdf->rchg - 1); + free(xdf->ha); + free(xdf->recs); xdl_cha_free(&xdf->rcha); } @@ -424,7 +424,7 @@ static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xd } xdf2->nreff = nreff; - xdl_free(dis); + free(dis); return 0; } diff --git a/xdiff/xutils.c b/xdiff/xutils.c index 865e08f0e93..00eeba452a5 100644 --- a/xdiff/xutils.c +++ b/xdiff/xutils.c @@ -88,7 +88,7 @@ void xdl_cha_free(chastore_t *cha) { for (cur = cha->head; (tmp = cur) != NULL;) { cur = cur->next; - xdl_free(tmp); + free(tmp); } }