From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS22989 209.51.188.0/24 X-Spam-Status: No, score=-3.8 required=3.0 tests=AWL,BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id CDD321F5AE for ; Sat, 12 Jun 2021 00:27:05 +0000 (UTC) Received: from localhost ([::1]:59904 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lrrUO-0001KM-SZ for normalperson@yhbt.net; Fri, 11 Jun 2021 20:27:04 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:54146) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lrrTg-00006n-3x for bug-gnulib@gnu.org; Fri, 11 Jun 2021 20:26:20 -0400 Received: from zimbra.cs.ucla.edu ([131.179.128.68]:39192) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lrrTb-0003q8-La for bug-gnulib@gnu.org; Fri, 11 Jun 2021 20:26:19 -0400 Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id 138FE16011F for ; Fri, 11 Jun 2021 17:26:13 -0700 (PDT) Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id Zqd5tXNDaxJs; Fri, 11 Jun 2021 17:26:09 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id B9687160120; Fri, 11 Jun 2021 17:26:09 -0700 (PDT) X-Virus-Scanned: amavisd-new at zimbra.cs.ucla.edu Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id 3cNqbRxf4m-y; Fri, 11 Jun 2021 17:26:09 -0700 (PDT) Received: from day.example.com (cpe-172-91-119-151.socal.res.rr.com [172.91.119.151]) by zimbra.cs.ucla.edu (Postfix) with ESMTPSA id 5BEB416011D; Fri, 11 Jun 2021 17:26:09 -0700 (PDT) From: Paul Eggert To: bug-gnulib@gnu.org Subject: [PATCH 04/13] dfa: prefer idx_t for indexes Date: Fri, 11 Jun 2021 17:25:44 -0700 Message-Id: <20210612002553.1105537-4-eggert@cs.ucla.edu> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210612002553.1105537-1-eggert@cs.ucla.edu> References: <20210612002553.1105537-1-eggert@cs.ucla.edu> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass client-ip=131.179.128.68; envelope-from=eggert@cs.ucla.edu; helo=zimbra.cs.ucla.edu X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: bug-gnulib@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Gnulib discussion list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Paul Eggert Errors-To: bug-gnulib-bounces+normalperson=yhbt.net@gnu.org Sender: "bug-gnulib" * lib/dfa.c (mbs_to_wchar, state_index, dfaoptimize, dfaanalyze) (icatalloc, enlist, allocmust, dfamust): Prefer idx_t to size_t for indexes, and use idx_t-related allocators. --- ChangeLog | 2 ++ lib/dfa.c | 28 ++++++++++++++-------------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/ChangeLog b/ChangeLog index 0fdbfe60b..d02e4ce0d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,6 +3,8 @@ xalloc: new idx_t-based allocators This is for code that prefers to use idx_t for sizes. * lib/basename.c (base_name): + * lib/dfa.c (mbs_to_wchar, state_index, dfaoptimize, dfaanalyze) + (icatalloc, enlist, allocmust, dfamust): Prefer idx_t to size_t for indexes, and use idx_t-related allocators. * lib/basename.c: Do not include xstrndup.h. (basename): Simplify by always using memcpy. diff --git a/lib/dfa.c b/lib/dfa.c index 5aa4e9913..7e05a78da 100644 --- a/lib/dfa.c +++ b/lib/dfa.c @@ -617,14 +617,14 @@ static void regexp (struct dfa *dfa); * PWC points to wint_t, not to wchar_t. * The last arg is a dfa *D instead of merely a multibyte conversion state D->mbs. - * N must be at least 1. + * N is idx_t not size_t, and must be at least 1. * S[N - 1] must be a sentinel byte. * Shift encodings are not supported. * The return value is always in the range 1..N. * D->mbs is always valid afterwards. * *PWC is always set to something. */ static int -mbs_to_wchar (wint_t *pwc, char const *s, size_t n, struct dfa *d) +mbs_to_wchar (wint_t *pwc, char const *s, idx_t n, struct dfa *d) { unsigned char uc =3D s[0]; wint_t wc =3D d->localeinfo.sbctowc[uc]; @@ -2165,7 +2165,7 @@ state_index (struct dfa *d, position_set const *s, = int context) =20 for (i =3D 0; i < s->nelem; ++i) { - size_t ind =3D s->elems[i].index; + idx_t ind =3D s->elems[i].index; hash ^=3D ind + s->elems[i].constraint; } =20 @@ -2488,7 +2488,7 @@ reorder_tokens (struct dfa *d) static void dfaoptimize (struct dfa *d) { - char *flags =3D xzalloc (d->tindex); + char *flags =3D xizalloc (d->tindex); =20 for (idx_t i =3D 0; i < d->tindex; i++) { @@ -2511,7 +2511,7 @@ dfaoptimize (struct dfa *d) position_set *merged =3D &merged0; alloc_position_set (merged, d->nleaves); =20 - d->constraints =3D xcalloc (d->tindex, sizeof *d->constraints); + d->constraints =3D xicalloc (d->tindex, sizeof *d->constraints); =20 for (idx_t i =3D 0; i < d->tindex; i++) if (flags[i] & OPT_QUEUED) @@ -2614,9 +2614,9 @@ dfaanalyze (struct dfa *d, bool searchflag) =20 d->searchflag =3D searchflag; alloc_position_set (&merged, d->nleaves); - d->follows =3D xcalloc (tindex, sizeof *d->follows); + d->follows =3D xicalloc (tindex, sizeof *d->follows); position_set *backward - =3D d->epsilon ? xcalloc (tindex, sizeof *backward) : NULL; + =3D d->epsilon ? xicalloc (tindex, sizeof *backward) : NULL; =20 for (idx_t i =3D 0; i < tindex; i++) { @@ -2799,7 +2799,7 @@ dfaanalyze (struct dfa *d, bool searchflag) =20 append (pos, &tmp); =20 - d->separates =3D xcalloc (tindex, sizeof *d->separates); + d->separates =3D xicalloc (tindex, sizeof *d->separates); =20 for (idx_t i =3D 0; i < tindex; i++) { @@ -3900,7 +3900,7 @@ icatalloc (char *old, char const *new) if (newsize =3D=3D 0) return old; idx_t oldsize =3D strlen (old); - char *result =3D xrealloc (old, oldsize + newsize + 1); + char *result =3D xirealloc (old, oldsize + newsize + 1); memcpy (result + oldsize, new, newsize + 1); return result; } @@ -3915,7 +3915,7 @@ freelist (char **cpp) static char ** enlist (char **cpp, char *new, idx_t len) { - new =3D memcpy (xmalloc (len + 1), new, len); + new =3D memcpy (ximalloc (len + 1), new, len); new[len] =3D '\0'; /* Is there already something in the list that's new (or longer)? */ idx_t i; @@ -4016,9 +4016,9 @@ allocmust (must *mp, idx_t size) { must *new_mp =3D xmalloc (sizeof *new_mp); new_mp->in =3D xzalloc (sizeof *new_mp->in); - new_mp->left =3D xzalloc (size); - new_mp->right =3D xzalloc (size); - new_mp->is =3D xzalloc (size); + new_mp->left =3D xizalloc (size); + new_mp->right =3D xizalloc (size); + new_mp->is =3D xizalloc (size); new_mp->begline =3D false; new_mp->endline =3D false; new_mp->prev =3D mp; @@ -4169,7 +4169,7 @@ dfamust (struct dfa const *d) { idx_t lrlen =3D strlen (lmp->right); idx_t rllen =3D strlen (rmp->left); - char *tp =3D xmalloc (lrlen + rllen); + char *tp =3D ximalloc (lrlen + rllen); memcpy (tp, lmp->right, lrlen); memcpy (tp + lrlen, rmp->left, rllen); lmp->in =3D enlist (lmp->in, tp, lrlen + rllen); --=20 2.30.2