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 08/13] readtokens: prefer idx_t for indexes
Date: Fri, 11 Jun 2021 17:25:48 -0700	[thread overview]
Message-ID: <20210612002553.1105537-8-eggert@cs.ucla.edu> (raw)
In-Reply-To: <20210612002553.1105537-1-eggert@cs.ucla.edu>

* lib/readtokens.c (readtoken, readtokens):
Prefer idx_t to size_t for indexes, using idx_t-related allocators.
---
 ChangeLog        |  1 +
 lib/readtokens.c | 14 ++++++--------
 lib/readtokens.h |  2 ++
 3 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 5dd638456..6e2c98014 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -10,6 +10,7 @@
 	* lib/getusershell.c (line_size, readname):
 	* lib/linebuffer.c (readlinebuffer_delim):
 	* lib/linebuffer.h (struct linebuffer):
+	* lib/readtokens.c (readtoken, readtokens):
 	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/readtokens.c b/lib/readtokens.c
index 2c08283e6..ca9ffd50f 100644
--- a/lib/readtokens.c
+++ b/lib/readtokens.c
@@ -82,9 +82,8 @@ readtoken (FILE *stream,
            size_t n_delim,
            token_buffer *tokenbuffer)
 {
-  char *p;
   int c;
-  size_t i, n;
+  idx_t i;
   word isdelim[(UCHAR_MAX + bits_per_word) / bits_per_word];
 
   memset (isdelim, 0, sizeof isdelim);
@@ -100,8 +99,8 @@ readtoken (FILE *stream,
       /* empty */
     }
 
-  p = tokenbuffer->buffer;
-  n = tokenbuffer->size;
+  char *p = tokenbuffer->buffer;
+  idx_t n = tokenbuffer->size;
   i = 0;
   for (;;)
     {
@@ -109,7 +108,7 @@ readtoken (FILE *stream,
         return -1;
 
       if (i == n)
-        p = x2nrealloc (p, &n, sizeof *p);
+        p = xpalloc (p, &n, 1, -1, sizeof *p);
 
       if (c < 0)
         {
@@ -148,8 +147,7 @@ readtokens (FILE *stream,
   token_buffer tb, *token = &tb;
   char **tokens;
   size_t *lengths;
-  size_t sz;
-  size_t n_tokens;
+  idx_t sz, n_tokens;
 
   if (projected_n_tokens == 0)
     projected_n_tokens = 64;
@@ -168,7 +166,7 @@ readtokens (FILE *stream,
       size_t token_length = readtoken (stream, delim, n_delim, token);
       if (n_tokens >= sz)
         {
-          tokens = x2nrealloc (tokens, &sz, sizeof *tokens);
+          tokens = xpalloc (tokens, &sz, 1, -1, sizeof *tokens);
           lengths = xreallocarray (lengths, sz, sizeof *lengths);
         }
 
diff --git a/lib/readtokens.h b/lib/readtokens.h
index 9842f3a2e..6bffb2e43 100644
--- a/lib/readtokens.h
+++ b/lib/readtokens.h
@@ -23,6 +23,8 @@
 
 # include <stdio.h>
 
+/* FIXME: This header should use idx_t, not size_t.  */
+
 struct tokenbuffer
 {
   size_t size;
-- 
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 ` [PATCH 04/13] dfa: prefer idx_t for indexes Paul Eggert
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 ` Paul Eggert [this message]
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-8-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).