bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
From: Paul Eggert <eggert@cs.ucla.edu>
To: bug-gnulib@gnu.org
Cc: Paul Eggert <eggert@cs.ucla.edu>
Subject: [PATCH 04/13] dfa: prefer idx_t for indexes
Date: Fri, 11 Jun 2021 17:25:44 -0700	[thread overview]
Message-ID: <20210612002553.1105537-4-eggert@cs.ucla.edu> (raw)
In-Reply-To: <20210612002553.1105537-1-eggert@cs.ucla.edu>

* 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 = s[0];
   wint_t wc = d->localeinfo.sbctowc[uc];
@@ -2165,7 +2165,7 @@ state_index (struct dfa *d, position_set const *s, int context)
 
   for (i = 0; i < s->nelem; ++i)
     {
-      size_t ind = s->elems[i].index;
+      idx_t ind = s->elems[i].index;
       hash ^= ind + s->elems[i].constraint;
     }
 
@@ -2488,7 +2488,7 @@ reorder_tokens (struct dfa *d)
 static void
 dfaoptimize (struct dfa *d)
 {
-  char *flags = xzalloc (d->tindex);
+  char *flags = xizalloc (d->tindex);
 
   for (idx_t i = 0; i < d->tindex; i++)
     {
@@ -2511,7 +2511,7 @@ dfaoptimize (struct dfa *d)
   position_set *merged = &merged0;
   alloc_position_set (merged, d->nleaves);
 
-  d->constraints = xcalloc (d->tindex, sizeof *d->constraints);
+  d->constraints = xicalloc (d->tindex, sizeof *d->constraints);
 
   for (idx_t i = 0; i < d->tindex; i++)
     if (flags[i] & OPT_QUEUED)
@@ -2614,9 +2614,9 @@ dfaanalyze (struct dfa *d, bool searchflag)
 
   d->searchflag = searchflag;
   alloc_position_set (&merged, d->nleaves);
-  d->follows = xcalloc (tindex, sizeof *d->follows);
+  d->follows = xicalloc (tindex, sizeof *d->follows);
   position_set *backward
-    = d->epsilon ? xcalloc (tindex, sizeof *backward) : NULL;
+    = d->epsilon ? xicalloc (tindex, sizeof *backward) : NULL;
 
   for (idx_t i = 0; i < tindex; i++)
     {
@@ -2799,7 +2799,7 @@ dfaanalyze (struct dfa *d, bool searchflag)
 
   append (pos, &tmp);
 
-  d->separates = xcalloc (tindex, sizeof *d->separates);
+  d->separates = xicalloc (tindex, sizeof *d->separates);
 
   for (idx_t i = 0; i < tindex; i++)
     {
@@ -3900,7 +3900,7 @@ icatalloc (char *old, char const *new)
   if (newsize == 0)
     return old;
   idx_t oldsize = strlen (old);
-  char *result = xrealloc (old, oldsize + newsize + 1);
+  char *result = 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 = memcpy (xmalloc (len + 1), new, len);
+  new = memcpy (ximalloc (len + 1), new, len);
   new[len] = '\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 = xmalloc (sizeof *new_mp);
   new_mp->in = xzalloc (sizeof *new_mp->in);
-  new_mp->left = xzalloc (size);
-  new_mp->right = xzalloc (size);
-  new_mp->is = xzalloc (size);
+  new_mp->left = xizalloc (size);
+  new_mp->right = xizalloc (size);
+  new_mp->is = xizalloc (size);
   new_mp->begline = false;
   new_mp->endline = false;
   new_mp->prev = mp;
@@ -4169,7 +4169,7 @@ dfamust (struct dfa const *d)
               {
                 idx_t lrlen = strlen (lmp->right);
                 idx_t rllen = strlen (rmp->left);
-                char *tp = xmalloc (lrlen + rllen);
+                char *tp = ximalloc (lrlen + rllen);
                 memcpy (tp, lmp->right, lrlen);
                 memcpy (tp + lrlen, rmp->left, rllen);
                 lmp->in = enlist (lmp->in, tp, lrlen + rllen);
-- 
2.30.2



  parent reply	other threads:[~2021-06-12  0:27 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-12  0:25 [PATCH 01/13] ialloc: new module Paul Eggert
2021-06-12  0:25 ` [PATCH 02/13] xalloc: new idx_t-based allocators Paul Eggert
2021-06-12  0:25 ` [PATCH 03/13] dirname: prefer idx_t for some indexes Paul Eggert
2021-06-12  0:25 ` Paul Eggert [this message]
2021-06-12  0:25 ` [PATCH 05/13] exclude: prefer idx_t for most indexes Paul Eggert
2021-06-12  0:25 ` [PATCH 06/13] getusershell: prefer idx_t for indexes Paul Eggert
2021-06-12  0:25 ` [PATCH 07/13] linebuffer: " Paul Eggert
2021-06-12  0:25 ` [PATCH 08/13] readtokens: " Paul Eggert
2021-06-12  0:25 ` [PATCH 09/13] readutmp: " Paul Eggert
2021-06-12  0:25 ` [PATCH 10/13] savedir: " Paul Eggert
2021-06-12  0:25 ` [PATCH 11/13] stack: " Paul Eggert
2021-06-13 10:19   ` Bruno Haible
2021-06-13 17:47     ` Paul Eggert
2021-06-12  0:25 ` [PATCH 12/13] userspec: " Paul Eggert
2021-06-12  0:25 ` [PATCH 13/13] xgethostname: " Paul Eggert
2021-06-13 10:05 ` [PATCH 01/13] ialloc: new module Bruno Haible

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210612002553.1105537-4-eggert@cs.ucla.edu \
    --to=eggert@cs.ucla.edu \
    --cc=bug-gnulib@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).