* [PATCH 01/13] ialloc: new module
@ 2021-06-12 0:25 Paul Eggert
2021-06-12 0:25 ` [PATCH 02/13] xalloc: new idx_t-based allocators Paul Eggert
` (12 more replies)
0 siblings, 13 replies; 16+ messages in thread
From: Paul Eggert @ 2021-06-12 0:25 UTC (permalink / raw)
To: bug-gnulib; +Cc: Paul Eggert
* lib/ialloc.c, lib/ialloc.h, modules/ialloc: New files.
---
ChangeLog | 3 ++
lib/ialloc.c | 3 ++
lib/ialloc.h | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++
modules/ialloc | 29 ++++++++++++++++
4 files changed, 129 insertions(+)
create mode 100644 lib/ialloc.c
create mode 100644 lib/ialloc.h
create mode 100644 modules/ialloc
diff --git a/ChangeLog b/ChangeLog
index 304599f81..dd9aa5015 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,8 @@
2021-06-11 Paul Eggert <eggert@cs.ucla.edu>
+ ialloc: new module
+ * lib/ialloc.c, lib/ialloc.h, modules/ialloc: New files.
+
exclude: improve wide-character hashing
* lib/exclude.c (string_hasher_ci): Take the modulo at the end
rather than each time a wide character is retrieved; this should
diff --git a/lib/ialloc.c b/lib/ialloc.c
new file mode 100644
index 000000000..9eff29035
--- /dev/null
+++ b/lib/ialloc.c
@@ -0,0 +1,3 @@
+#include <config.h>
+#define IALLOC_INLINE _GL_EXTERN_INLINE
+#include "ialloc.h"
diff --git a/lib/ialloc.h b/lib/ialloc.h
new file mode 100644
index 000000000..e243c928c
--- /dev/null
+++ b/lib/ialloc.h
@@ -0,0 +1,94 @@
+/* ialloc.h -- malloc with idx_t rather than size_t
+
+ Copyright 2021 Free Software Foundation, Inc.
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>. */
+
+#ifndef IALLOC_H_
+#define IALLOC_H_
+
+#include "idx.h"
+
+#include <errno.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+#ifndef _GL_INLINE_HEADER_BEGIN
+ #error "Please include config.h first."
+#endif
+_GL_INLINE_HEADER_BEGIN
+#ifndef IALLOC_INLINE
+# define IALLOC_INLINE _GL_INLINE
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+IALLOC_INLINE void * _GL_ATTRIBUTE_COLD
+_gl_alloc_nomem (void)
+{
+ errno = ENOMEM;
+ return NULL;
+}
+
+IALLOC_INLINE void *
+imalloc (idx_t s)
+{
+ return s <= SIZE_MAX ? malloc (s) : _gl_alloc_nomem ();
+}
+
+IALLOC_INLINE void *
+irealloc (void *p, idx_t s)
+{
+ /* Work around GNU realloc glitch by treating a zero size as if it
+ were 1, so that returning NULL is equivalent to failing. */
+ return s <= SIZE_MAX ? realloc (p, s | !s) : _gl_alloc_nomem ();
+}
+
+IALLOC_INLINE void *
+icalloc (idx_t n, idx_t s)
+{
+ if (SIZE_MAX < n)
+ {
+ if (s != 0)
+ return _gl_alloc_nomem ();
+ n = 0;
+ }
+ if (SIZE_MAX < s)
+ {
+ if (n != 0)
+ return _gl_alloc_nomem ();
+ s = 0;
+ }
+ return calloc (n, s);
+}
+
+IALLOC_INLINE void *
+ireallocarray (void *p, idx_t n, idx_t s)
+{
+ /* Work around GNU reallocarray glitch by treating a zero size as if
+ it were 1, so that returning NULL is equivalent to failing. */
+ if (n == 0 || s == 0)
+ n = s = 1;
+ return (n <= SIZE_MAX && s <= SIZE_MAX
+ ? reallocarray (p, n, s)
+ : _gl_alloc_nomem ());
+}
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/modules/ialloc b/modules/ialloc
new file mode 100644
index 000000000..3bf377a62
--- /dev/null
+++ b/modules/ialloc
@@ -0,0 +1,29 @@
+Description:
+Memory allocation using idx_t instead of size_t
+
+Files:
+lib/ialloc.c
+lib/ialloc.h
+
+Depends-on:
+calloc-gnu
+extern-inline
+idx
+malloc-gnu
+realloc-gnu
+reallocarray
+stdint
+
+configure.ac:
+
+Makefile.am:
+lib_SOURCES += ialloc.c
+
+Include:
+"ialloc.h"
+
+License:
+LGPL
+
+Maintainer:
+all
--
2.30.2
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH 02/13] xalloc: new idx_t-based allocators
2021-06-12 0:25 [PATCH 01/13] ialloc: new module Paul Eggert
@ 2021-06-12 0:25 ` Paul Eggert
2021-06-12 0:25 ` [PATCH 03/13] dirname: prefer idx_t for some indexes Paul Eggert
` (11 subsequent siblings)
12 siblings, 0 replies; 16+ messages in thread
From: Paul Eggert @ 2021-06-12 0:25 UTC (permalink / raw)
To: bug-gnulib; +Cc: Paul Eggert
This is for code that prefers to use idx_t for sizes.
* lib/xalloc.h (ximalloc, xizalloc, xicalloc, xirealloc)
(xireallocarray, ximemdup, ximemdup0) [GNULIB_XALLOC]:
New decls.
(x2nrealloc): Now just a decl, as the body is moved into xmalloc.c.
* lib/xmalloc.c: Include ialloc.h.
Rename some local parameters to be consistent with the .h files.
(nonnull): New static function.
(xmalloc, xcalloc): Simplify by using nonnull.
(ximalloc, xirealloc, xireallocarray, xizalloc, xicalloc)
(ximemdup, ximemdup0): New functions.
(x2nrealloc): Moved here from xalloc.h.
* modules/xalloc (Depends-on): Add ialloc.
---
ChangeLog | 15 ++++
lib/xalloc.h | 114 ++++---------------------
lib/xmalloc.c | 224 +++++++++++++++++++++++++++++++++++++++----------
modules/xalloc | 1 +
4 files changed, 212 insertions(+), 142 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index dd9aa5015..42d748a38 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,20 @@
2021-06-11 Paul Eggert <eggert@cs.ucla.edu>
+ xalloc: new idx_t-based allocators
+ This is for code that prefers to use idx_t for sizes.
+ * lib/xalloc.h (ximalloc, xizalloc, xicalloc, xirealloc)
+ (xireallocarray, ximemdup, ximemdup0) [GNULIB_XALLOC]:
+ New decls.
+ (x2nrealloc): Now just a decl, as the body is moved into xmalloc.c.
+ * lib/xmalloc.c: Include ialloc.h.
+ Rename some local parameters to be consistent with the .h files.
+ (nonnull): New static function.
+ (xmalloc, xcalloc): Simplify by using nonnull.
+ (ximalloc, xirealloc, xireallocarray, xizalloc, xicalloc)
+ (ximemdup, ximemdup0): New functions.
+ (x2nrealloc): Moved here from xalloc.h.
+ * modules/xalloc (Depends-on): Add ialloc.
+
ialloc: new module
* lib/ialloc.c, lib/ialloc.h, modules/ialloc: New files.
diff --git a/lib/xalloc.h b/lib/xalloc.h
index 6cd7a680c..70ef0971f 100644
--- a/lib/xalloc.h
+++ b/lib/xalloc.h
@@ -53,21 +53,26 @@ extern "C" {
#if GNULIB_XALLOC
-void *xmalloc (size_t s)
- _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1));
-void *xzalloc (size_t s)
- _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1));
+void *xmalloc (size_t s) _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1));
+void *ximalloc (idx_t s) _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1));
+void *xzalloc (size_t s) _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1));
+void *xizalloc (idx_t s) _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1));
void *xcalloc (size_t n, size_t s)
- _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2));
-void *xrealloc (void *p, size_t s)
- _GL_ATTRIBUTE_ALLOC_SIZE ((2));
+ _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2));
+void *xicalloc (idx_t n, idx_t s)
+ _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2));
+void *xrealloc (void *p, size_t s) _GL_ATTRIBUTE_ALLOC_SIZE ((2));
+void *xirealloc (void *p, idx_t s) _GL_ATTRIBUTE_ALLOC_SIZE ((2));
void *xreallocarray (void *p, size_t n, size_t s)
_GL_ATTRIBUTE_ALLOC_SIZE ((2, 3));
-void *x2realloc (void *p, size_t *pn);
-void *xpalloc (void *pa, idx_t *nitems, idx_t nitems_incr_min,
- ptrdiff_t nitems_max, idx_t item_size);
-void *xmemdup (void const *p, size_t s)
- _GL_ATTRIBUTE_ALLOC_SIZE ((2));
+void *xireallocarray (void *p, idx_t n, idx_t s)
+ _GL_ATTRIBUTE_ALLOC_SIZE ((2, 3));
+void *x2realloc (void *p, size_t *ps); /* superseded by xpalloc */
+void *x2nrealloc (void *p, size_t *pn, size_t s); /* superseded by xpalloc */
+void *xpalloc (void *pa, idx_t *pn, idx_t n_incr_min, ptrdiff_t n_max, idx_t s);
+void *xmemdup (void const *p, size_t s) _GL_ATTRIBUTE_ALLOC_SIZE ((2));
+void *ximemdup (void const *p, idx_t s) _GL_ATTRIBUTE_ALLOC_SIZE ((2));
+char *ximemdup0 (void const *p, idx_t s) _GL_ATTRIBUTE_MALLOC;
char *xstrdup (char const *str)
_GL_ATTRIBUTE_MALLOC;
@@ -120,91 +125,6 @@ xnrealloc (void *p, size_t n, size_t s)
return xreallocarray (p, n, s);
}
-/* If P is null, allocate a block of at least *PN such objects;
- otherwise, reallocate P so that it contains more than *PN objects
- each of S bytes. S must be nonzero. Set *PN to the new number of
- objects, and return the pointer to the new block. *PN is never set
- to zero, and the returned pointer is never null.
-
- Repeated reallocations are guaranteed to make progress, either by
- allocating an initial block with a nonzero size, or by allocating a
- larger block.
-
- In the following implementation, nonzero sizes are increased by a
- factor of approximately 1.5 so that repeated reallocations have
- O(N) overall cost rather than O(N**2) cost, but the
- specification for this function does not guarantee that rate.
-
- Here is an example of use:
-
- int *p = NULL;
- size_t used = 0;
- size_t allocated = 0;
-
- void
- append_int (int value)
- {
- if (used == allocated)
- p = x2nrealloc (p, &allocated, sizeof *p);
- p[used++] = value;
- }
-
- This causes x2nrealloc to allocate a block of some nonzero size the
- first time it is called.
-
- To have finer-grained control over the initial size, set *PN to a
- nonzero value before calling this function with P == NULL. For
- example:
-
- int *p = NULL;
- size_t used = 0;
- size_t allocated = 0;
- size_t allocated1 = 1000;
-
- void
- append_int (int value)
- {
- if (used == allocated)
- {
- p = x2nrealloc (p, &allocated1, sizeof *p);
- allocated = allocated1;
- }
- p[used++] = value;
- }
-
- */
-
-XALLOC_INLINE void *
-x2nrealloc (void *p, size_t *pn, size_t s)
-{
- size_t n = *pn;
-
- if (! p)
- {
- if (! n)
- {
- /* The approximate size to use for initial small allocation
- requests, when the invoking code specifies an old size of
- zero. This is the largest "small" request for the GNU C
- library malloc. */
- enum { DEFAULT_MXFAST = 64 * sizeof (size_t) / 4 };
-
- n = DEFAULT_MXFAST / s;
- n += !n;
- }
- }
- else
- {
- /* Set N = floor (1.5 * N) + 1 to make progress even if N == 0. */
- if (INT_ADD_WRAPV (n, (n >> 1) + 1, &n))
- xalloc_die ();
- }
-
- p = xreallocarray (p, n, s);
- *pn = n;
- return p;
-}
-
/* Return a pointer to a new buffer of N bytes. This is like xmalloc,
except it returns char *. */
diff --git a/lib/xmalloc.c b/lib/xmalloc.c
index 88698fade..413ee1b3c 100644
--- a/lib/xmalloc.c
+++ b/lib/xmalloc.c
@@ -21,35 +21,53 @@
#include "xalloc.h"
+#include "ialloc.h"
#include "intprops.h"
#include "minmax.h"
#include <stdlib.h>
#include <string.h>
-/* Allocate N bytes of memory dynamically, with error checking. */
-
-void *
-xmalloc (size_t n)
+static void *
+nonnull (void *p)
{
- void *p = malloc (n);
if (!p)
xalloc_die ();
return p;
}
-/* Change the size of an allocated block of memory P to N bytes,
+/* Allocate S bytes of memory dynamically, with error checking. */
+
+void *
+xmalloc (size_t s)
+{
+ return nonnull (malloc (s));
+}
+
+void *
+ximalloc (idx_t s)
+{
+ return nonnull (imalloc (s));
+}
+
+/* Change the size of an allocated block of memory P to S bytes,
with error checking. */
void *
-xrealloc (void *p, size_t n)
+xrealloc (void *p, size_t s)
{
- void *r = realloc (p, n);
- if (!r && (!p || n))
+ void *r = realloc (p, s);
+ if (!r && (!p || s))
xalloc_die ();
return r;
}
+void *
+xirealloc (void *p, idx_t s)
+{
+ return nonnull (irealloc (p, s));
+}
+
/* Change the size of an allocated block of memory P to an array of N
objects each of S bytes, with error checking. */
@@ -62,26 +80,117 @@ xreallocarray (void *p, size_t n, size_t s)
return r;
}
-/* If P is null, allocate a block of at least *PN bytes; otherwise,
- reallocate P so that it contains more than *PN bytes. *PN must be
- nonzero unless P is null. Set *PN to the new block's size, and
- return the pointer to the new block. *PN is never set to zero, and
+void *
+xireallocarray (void *p, idx_t n, idx_t s)
+{
+ return nonnull (ireallocarray (p, n, s));
+}
+
+/* If P is null, allocate a block of at least *PS bytes; otherwise,
+ reallocate P so that it contains more than *PS bytes. *PS must be
+ nonzero unless P is null. Set *PS to the new block's size, and
+ return the pointer to the new block. *PS is never set to zero, and
the returned pointer is never null. */
void *
-x2realloc (void *p, size_t *pn)
+x2realloc (void *p, size_t *ps)
{
- return x2nrealloc (p, pn, 1);
+ return x2nrealloc (p, ps, 1);
}
-/* Grow PA, which points to an array of *NITEMS items, and return the
- location of the reallocated array, updating *NITEMS to reflect its
- new size. The new array will contain at least NITEMS_INCR_MIN more
- items, but will not contain more than NITEMS_MAX items total.
- ITEM_SIZE is the size of each item, in bytes.
+/* If P is null, allocate a block of at least *PN such objects;
+ otherwise, reallocate P so that it contains more than *PN objects
+ each of S bytes. S must be nonzero. Set *PN to the new number of
+ objects, and return the pointer to the new block. *PN is never set
+ to zero, and the returned pointer is never null.
+
+ Repeated reallocations are guaranteed to make progress, either by
+ allocating an initial block with a nonzero size, or by allocating a
+ larger block.
+
+ In the following implementation, nonzero sizes are increased by a
+ factor of approximately 1.5 so that repeated reallocations have
+ O(N) overall cost rather than O(N**2) cost, but the
+ specification for this function does not guarantee that rate.
+
+ Here is an example of use:
+
+ int *p = NULL;
+ size_t used = 0;
+ size_t allocated = 0;
+
+ void
+ append_int (int value)
+ {
+ if (used == allocated)
+ p = x2nrealloc (p, &allocated, sizeof *p);
+ p[used++] = value;
+ }
+
+ This causes x2nrealloc to allocate a block of some nonzero size the
+ first time it is called.
+
+ To have finer-grained control over the initial size, set *PN to a
+ nonzero value before calling this function with P == NULL. For
+ example:
+
+ int *p = NULL;
+ size_t used = 0;
+ size_t allocated = 0;
+ size_t allocated1 = 1000;
+
+ void
+ append_int (int value)
+ {
+ if (used == allocated)
+ {
+ p = x2nrealloc (p, &allocated1, sizeof *p);
+ allocated = allocated1;
+ }
+ p[used++] = value;
+ }
+
+ */
- ITEM_SIZE and NITEMS_INCR_MIN must be positive. *NITEMS must be
- nonnegative. If NITEMS_MAX is -1, it is treated as if it were
+void *
+x2nrealloc (void *p, size_t *pn, size_t s)
+{
+ size_t n = *pn;
+
+ if (! p)
+ {
+ if (! n)
+ {
+ /* The approximate size to use for initial small allocation
+ requests, when the invoking code specifies an old size of
+ zero. This is the largest "small" request for the GNU C
+ library malloc. */
+ enum { DEFAULT_MXFAST = 64 * sizeof (size_t) / 4 };
+
+ n = DEFAULT_MXFAST / s;
+ n += !n;
+ }
+ }
+ else
+ {
+ /* Set N = floor (1.5 * N) + 1 to make progress even if N == 0. */
+ if (INT_ADD_WRAPV (n, (n >> 1) + 1, &n))
+ xalloc_die ();
+ }
+
+ p = xreallocarray (p, n, s);
+ *pn = n;
+ return p;
+}
+
+/* Grow PA, which points to an array of *PN items, and return the
+ location of the reallocated array, updating *PN to reflect its
+ new size. The new array will contain at least N_INCR_MIN more
+ items, but will not contain more than N_MAX items total.
+ S is the size of each item, in bytes.
+
+ S and N_INCR_MIN must be positive. *PN must be
+ nonnegative. If N_MAX is -1, it is treated as if it were
infinity.
If PA is null, then allocate a new array instead of reallocating
@@ -91,10 +200,9 @@ x2realloc (void *p, size_t *pn)
{ free (A); A = xpalloc (NULL, &AITEMS, ...); }. */
void *
-xpalloc (void *pa, idx_t *nitems, idx_t nitems_incr_min,
- ptrdiff_t nitems_max, idx_t item_size)
+xpalloc (void *pa, idx_t *pn, idx_t n_incr_min, ptrdiff_t n_max, idx_t s)
{
- idx_t n0 = *nitems;
+ idx_t n0 = *pn;
/* The approximate size to use for initial small allocation
requests. This is the largest "small" request for the GNU C
@@ -103,14 +211,14 @@ xpalloc (void *pa, idx_t *nitems, idx_t nitems_incr_min,
/* If the array is tiny, grow it to about (but no greater than)
DEFAULT_MXFAST bytes. Otherwise, grow it by about 50%.
- Adjust the growth according to three constraints: NITEMS_INCR_MIN,
- NITEMS_MAX, and what the C language can represent safely. */
+ Adjust the growth according to three constraints: N_INCR_MIN,
+ N_MAX, and what the C language can represent safely. */
idx_t n;
if (INT_ADD_WRAPV (n0, n0 >> 1, &n))
n = IDX_MAX;
- if (0 <= nitems_max && nitems_max < n)
- n = nitems_max;
+ if (0 <= n_max && n_max < n)
+ n = n_max;
/* NBYTES is of a type suitable for holding the count of bytes in an object.
This is typically idx_t, but it should be size_t on (theoretical?)
@@ -122,35 +230,41 @@ xpalloc (void *pa, idx_t *nitems, idx_t nitems_incr_min,
size_t nbytes;
#endif
idx_t adjusted_nbytes
- = (INT_MULTIPLY_WRAPV (n, item_size, &nbytes)
+ = (INT_MULTIPLY_WRAPV (n, s, &nbytes)
? MIN (IDX_MAX, SIZE_MAX)
: nbytes < DEFAULT_MXFAST ? DEFAULT_MXFAST : 0);
if (adjusted_nbytes)
{
- n = adjusted_nbytes / item_size;
- nbytes = adjusted_nbytes - adjusted_nbytes % item_size;
+ n = adjusted_nbytes / s;
+ nbytes = adjusted_nbytes - adjusted_nbytes % s;
}
if (! pa)
- *nitems = 0;
- if (n - n0 < nitems_incr_min
- && (INT_ADD_WRAPV (n0, nitems_incr_min, &n)
- || (0 <= nitems_max && nitems_max < n)
- || INT_MULTIPLY_WRAPV (n, item_size, &nbytes)))
+ *pn = 0;
+ if (n - n0 < n_incr_min
+ && (INT_ADD_WRAPV (n0, n_incr_min, &n)
+ || (0 <= n_max && n_max < n)
+ || INT_MULTIPLY_WRAPV (n, s, &nbytes)))
xalloc_die ();
pa = xrealloc (pa, nbytes);
- *nitems = n;
+ *pn = n;
return pa;
}
-/* Allocate N bytes of zeroed memory dynamically, with error checking.
+/* Allocate S bytes of zeroed memory dynamically, with error checking.
There's no need for xnzalloc (N, S), since it would be equivalent
to xcalloc (N, S). */
void *
-xzalloc (size_t n)
+xzalloc (size_t s)
{
- return xcalloc (n, 1);
+ return xcalloc (s, 1);
+}
+
+void *
+xizalloc (idx_t s)
+{
+ return xicalloc (s, 1);
}
/* Allocate zeroed memory for N elements of S bytes, with error
@@ -159,10 +273,13 @@ xzalloc (size_t n)
void *
xcalloc (size_t n, size_t s)
{
- void *p = calloc (n, s);
- if (!p)
- xalloc_die ();
- return p;
+ return nonnull (calloc (n, s));
+}
+
+void *
+xicalloc (idx_t n, idx_t s)
+{
+ return nonnull (icalloc (n, s));
}
/* Clone an object P of size S, with error checking. There's no need
@@ -175,6 +292,23 @@ xmemdup (void const *p, size_t s)
return memcpy (xmalloc (s), p, s);
}
+void *
+ximemdup (void const *p, idx_t s)
+{
+ return memcpy (ximalloc (s), p, s);
+}
+
+/* Clone an object P of size S, with error checking. Append
+ a terminating NUL byte. */
+
+char *
+ximemdup0 (void const *p, idx_t s)
+{
+ char *result = ximalloc (s + 1);
+ result[s] = 0;
+ return memcpy (result, p, s);
+}
+
/* Clone STRING. */
char *
diff --git a/modules/xalloc b/modules/xalloc
index 0dbae1c86..0fc3836c2 100644
--- a/modules/xalloc
+++ b/modules/xalloc
@@ -10,6 +10,7 @@ Depends-on:
c99
calloc-gnu
extern-inline
+ialloc
idx
intprops
malloc-gnu
--
2.30.2
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH 03/13] dirname: prefer idx_t for some indexes
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 ` Paul Eggert
2021-06-12 0:25 ` [PATCH 04/13] dfa: prefer idx_t for indexes Paul Eggert
` (10 subsequent siblings)
12 siblings, 0 replies; 16+ messages in thread
From: Paul Eggert @ 2021-06-12 0:25 UTC (permalink / raw)
To: bug-gnulib; +Cc: Paul Eggert
* lib/basename.c (base_name):
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.
* modules/dirname (Depends-on): Removbe xstrndup.
---
ChangeLog | 5 +++++
lib/basename.c | 50 +++++++++++++++++++++++++++----------------------
modules/dirname | 1 -
3 files changed, 33 insertions(+), 23 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 42d748a38..0fdbfe60b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -2,6 +2,10 @@
xalloc: new idx_t-based allocators
This is for code that prefers to use idx_t for sizes.
+ * lib/basename.c (base_name):
+ 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.
* lib/xalloc.h (ximalloc, xizalloc, xicalloc, xirealloc)
(xireallocarray, ximemdup, ximemdup0) [GNULIB_XALLOC]:
New decls.
@@ -14,6 +18,7 @@
(ximemdup, ximemdup0): New functions.
(x2nrealloc): Moved here from xalloc.h.
* modules/xalloc (Depends-on): Add ialloc.
+ * modules/dirname (Depends-on): Removbe xstrndup.
ialloc: new module
* lib/ialloc.c, lib/ialloc.h, modules/ialloc: New files.
diff --git a/lib/basename.c b/lib/basename.c
index 0ba28780c..1181134ed 100644
--- a/lib/basename.c
+++ b/lib/basename.c
@@ -22,37 +22,43 @@
#include <string.h>
#include "xalloc.h"
-#include "xstrndup.h"
char *
base_name (char const *name)
{
char const *base = last_component (name);
- size_t length;
-
- /* If there is no last component, then name is a file system root or the
- empty string. */
- if (! *base)
- return xstrndup (name, base_len (name));
-
- /* Collapse a sequence of trailing slashes into one. */
- length = base_len (base);
- if (ISSLASH (base[length]))
- length++;
-
- /* On systems with drive letters, "a/b:c" must return "./b:c" rather
- than "b:c" to avoid confusion with a drive letter. On systems
- with pure POSIX semantics, this is not an issue. */
- if (FILE_SYSTEM_PREFIX_LEN (base))
+ idx_t length;
+ int dotslash_len;
+ if (*base)
+ {
+ length = base_len (base);
+
+ /* Collapse a sequence of trailing slashes into one. */
+ length += ISSLASH (base[length]);
+
+ /* On systems with drive letters, "a/b:c" must return "./b:c" rather
+ than "b:c" to avoid confusion with a drive letter. On systems
+ with pure POSIX semantics, this is not an issue. */
+ dotslash_len = FILE_SYSTEM_PREFIX_LEN (base) != 0 ? 2 : 0;
+ }
+ else
+ {
+ /* There is no last component, so NAME is a file system root or
+ the empty string. */
+ base = name;
+ length = base_len (base);
+ dotslash_len = 0;
+ }
+
+ char *p = ximalloc (dotslash_len + length + 1);
+ if (dotslash_len)
{
- char *p = xmalloc (length + 3);
p[0] = '.';
p[1] = '/';
- memcpy (p + 2, base, length);
- p[length + 2] = '\0';
- return p;
}
/* Finally, copy the basename. */
- return xstrndup (base, length);
+ memcpy (p + dotslash_len, base, length);
+ p[dotslash_len + length] = '\0';
+ return p;
}
diff --git a/modules/dirname b/modules/dirname
index e3ffbe04d..483103eba 100644
--- a/modules/dirname
+++ b/modules/dirname
@@ -9,7 +9,6 @@ lib/stripslash.c
Depends-on:
dirname-lgpl
xalloc
-xstrndup
configure.ac:
gl_MODULE_INDICATOR([dirname])
--
2.30.2
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH 04/13] dfa: prefer idx_t for indexes
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
2021-06-12 0:25 ` [PATCH 05/13] exclude: prefer idx_t for most indexes Paul Eggert
` (9 subsequent siblings)
12 siblings, 0 replies; 16+ messages in thread
From: Paul Eggert @ 2021-06-12 0:25 UTC (permalink / raw)
To: bug-gnulib; +Cc: Paul Eggert
* 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
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH 05/13] exclude: prefer idx_t for most indexes
2021-06-12 0:25 [PATCH 01/13] ialloc: new module Paul Eggert
` (2 preceding siblings ...)
2021-06-12 0:25 ` [PATCH 04/13] dfa: prefer idx_t for indexes Paul Eggert
@ 2021-06-12 0:25 ` Paul Eggert
2021-06-12 0:25 ` [PATCH 06/13] getusershell: prefer idx_t for indexes Paul Eggert
` (8 subsequent siblings)
12 siblings, 0 replies; 16+ messages in thread
From: Paul Eggert @ 2021-06-12 0:25 UTC (permalink / raw)
To: bug-gnulib; +Cc: Paul Eggert
* lib/exclude.c (struct exclude_pattern, free_exclude_segment)
(file_pattern_matches, add_exclude, add_exclude_fp):
Prefer idx_t to size_t for indexes, and use idx_t-related allocators.
---
ChangeLog | 2 ++
lib/exclude.c | 29 +++++++++++++----------------
2 files changed, 15 insertions(+), 16 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index d02e4ce0d..1fca94c03 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -5,6 +5,8 @@
* lib/basename.c (base_name):
* lib/dfa.c (mbs_to_wchar, state_index, dfaoptimize, dfaanalyze)
(icatalloc, enlist, allocmust, dfamust):
+ * lib/exclude.c (struct exclude_pattern, free_exclude_segment)
+ (file_pattern_matches, add_exclude, add_exclude_fp):
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/exclude.c b/lib/exclude.c
index 6287fbc68..417ab23d1 100644
--- a/lib/exclude.c
+++ b/lib/exclude.c
@@ -88,8 +88,8 @@ struct patopts
struct exclude_pattern
{
struct patopts *exclude;
- size_t exclude_alloc;
- size_t exclude_count;
+ idx_t exclude_alloc;
+ idx_t exclude_count;
};
enum exclude_type
@@ -281,12 +281,10 @@ new_exclude_segment (struct exclude *ex, enum exclude_type type, int options)
static void
free_exclude_segment (struct exclude_segment *seg)
{
- size_t i;
-
switch (seg->type)
{
case exclude_pattern:
- for (i = 0; i < seg->v.pat.exclude_count; i++)
+ for (idx_t i = 0; i < seg->v.pat.exclude_count; i++)
{
if (seg->v.pat.exclude[i].options & EXCLUDE_REGEX)
regfree (&seg->v.pat.exclude[i].v.re);
@@ -407,11 +405,10 @@ exclude_patopts (struct patopts const *opts, char const *f)
static bool
file_pattern_matches (struct exclude_segment const *seg, char const *f)
{
- size_t exclude_count = seg->v.pat.exclude_count;
+ idx_t exclude_count = seg->v.pat.exclude_count;
struct patopts const *exclude = seg->v.pat.exclude;
- size_t i;
- for (i = 0; i < exclude_count; i++)
+ for (idx_t i = 0; i < exclude_count; i++)
{
if (exclude_patopts (exclude + i, f))
return true;
@@ -533,8 +530,8 @@ add_exclude (struct exclude *ex, char const *pattern, int options)
pat = &seg->v.pat;
if (pat->exclude_count == pat->exclude_alloc)
- pat->exclude = x2nrealloc (pat->exclude, &pat->exclude_alloc,
- sizeof *pat->exclude);
+ pat->exclude = xpalloc (pat->exclude, &pat->exclude_alloc, 1, -1,
+ sizeof *pat->exclude);
patopts = &pat->exclude[pat->exclude_count++];
patopts->options = options;
@@ -547,7 +544,7 @@ add_exclude (struct exclude *ex, char const *pattern, int options)
if (options & FNM_LEADING_DIR)
{
char *tmp;
- size_t len = strlen (pattern);
+ idx_t len = strlen (pattern);
while (len > 0 && ISSLASH (pattern[len-1]))
--len;
@@ -556,7 +553,7 @@ add_exclude (struct exclude *ex, char const *pattern, int options)
rc = 1;
else
{
- tmp = xmalloc (len + 7);
+ tmp = ximalloc (len + 7);
memcpy (tmp, pattern, len);
strcpy (tmp + len, "(/.*)?");
rc = regcomp (&patopts->v.re, tmp, cflags);
@@ -617,22 +614,22 @@ add_exclude_fp (void (*add_func) (struct exclude *, char const *, int, void *),
char *p;
char *pattern;
char const *lim;
- size_t buf_alloc = 0;
- size_t buf_count = 0;
+ idx_t buf_alloc = 0;
+ idx_t buf_count = 0;
int c;
int e = 0;
while ((c = getc (fp)) != EOF)
{
if (buf_count == buf_alloc)
- buf = x2realloc (buf, &buf_alloc);
+ buf = xpalloc (buf, &buf_alloc, 1, -1, 1);
buf[buf_count++] = c;
}
if (ferror (fp))
e = errno;
- buf = xrealloc (buf, buf_count + 1);
+ buf = xirealloc (buf, buf_count + 1);
buf[buf_count] = line_end;
lim = buf + buf_count + ! (buf_count == 0 || buf[buf_count - 1] == line_end);
--
2.30.2
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH 06/13] getusershell: prefer idx_t for indexes
2021-06-12 0:25 [PATCH 01/13] ialloc: new module Paul Eggert
` (3 preceding siblings ...)
2021-06-12 0:25 ` [PATCH 05/13] exclude: prefer idx_t for most indexes Paul Eggert
@ 2021-06-12 0:25 ` Paul Eggert
2021-06-12 0:25 ` [PATCH 07/13] linebuffer: " Paul Eggert
` (7 subsequent siblings)
12 siblings, 0 replies; 16+ messages in thread
From: Paul Eggert @ 2021-06-12 0:25 UTC (permalink / raw)
To: bug-gnulib; +Cc: Paul Eggert
* lib/getusershell.c (line_size, readname):
Prefer idx_t to size_t for indexes, using idx_t-related allocators.
---
ChangeLog | 1 +
lib/getusershell.c | 10 +++++-----
2 files changed, 6 insertions(+), 5 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 1fca94c03..84ccb3645 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -7,6 +7,7 @@
(icatalloc, enlist, allocmust, dfamust):
* lib/exclude.c (struct exclude_pattern, free_exclude_segment)
(file_pattern_matches, add_exclude, add_exclude_fp):
+ * lib/getusershell.c (line_size, readname):
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/getusershell.c b/lib/getusershell.c
index be8a06845..32acd76c0 100644
--- a/lib/getusershell.c
+++ b/lib/getusershell.c
@@ -43,7 +43,7 @@
# include "unlocked-io.h"
#endif
-static size_t readname (char **, size_t *, FILE *);
+static idx_t readname (char **, idx_t *, FILE *);
#if ! defined ADDITIONAL_DEFAULT_SHELLS && defined __MSDOS__
# define ADDITIONAL_DEFAULT_SHELLS \
@@ -70,7 +70,7 @@ static FILE *shellstream = NULL;
static char *line = NULL;
/* Number of bytes allocated for 'line'. */
-static size_t line_size = 0;
+static idx_t line_size = 0;
\f
/* Return an entry from the shells file, ignoring comment lines.
If the file doesn't exist, use the list in DEFAULT_SHELLS (above).
@@ -137,8 +137,8 @@ endusershell (void)
Return the number of bytes placed in *NAME
if some nonempty sequence was found, otherwise 0. */
-static size_t
-readname (char **name, size_t *size, FILE *stream)
+static idx_t
+readname (char **name, idx_t *size, FILE *stream)
{
int c;
size_t name_index = 0;
@@ -150,7 +150,7 @@ readname (char **name, size_t *size, FILE *stream)
for (;;)
{
if (*size <= name_index)
- *name = x2nrealloc (*name, size, sizeof **name);
+ *name = xpalloc (*name, size, 1, -1, sizeof **name);
if (c == EOF || isspace (c))
break;
(*name)[name_index++] = c;
--
2.30.2
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH 07/13] linebuffer: prefer idx_t for indexes
2021-06-12 0:25 [PATCH 01/13] ialloc: new module Paul Eggert
` (4 preceding siblings ...)
2021-06-12 0:25 ` [PATCH 06/13] getusershell: prefer idx_t for indexes Paul Eggert
@ 2021-06-12 0:25 ` Paul Eggert
2021-06-12 0:25 ` [PATCH 08/13] readtokens: " Paul Eggert
` (6 subsequent siblings)
12 siblings, 0 replies; 16+ messages in thread
From: Paul Eggert @ 2021-06-12 0:25 UTC (permalink / raw)
To: bug-gnulib; +Cc: Paul Eggert
* lib/linebuffer.c (readlinebuffer_delim):
* lib/linebuffer.h (struct linebuffer):
Prefer idx_t to size_t for indexes, using idx_t-related allocators.
* lib/linebuffer.h: Include idx.h.
* modules/linebuffer (Depends-on): Add idx.
---
ChangeLog | 3 +++
lib/linebuffer.c | 4 ++--
lib/linebuffer.h | 5 +++--
modules/linebuffer | 2 +-
4 files changed, 9 insertions(+), 5 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 84ccb3645..5dd638456 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -8,6 +8,8 @@
* lib/exclude.c (struct exclude_pattern, free_exclude_segment)
(file_pattern_matches, add_exclude, add_exclude_fp):
* lib/getusershell.c (line_size, readname):
+ * lib/linebuffer.c (readlinebuffer_delim):
+ * lib/linebuffer.h (struct linebuffer):
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.
@@ -24,6 +26,7 @@
(x2nrealloc): Moved here from xalloc.h.
* modules/xalloc (Depends-on): Add ialloc.
* modules/dirname (Depends-on): Removbe xstrndup.
+ * modules/linebuffer (Depends-on): Add idx.
ialloc: new module
* lib/ialloc.c, lib/ialloc.h, modules/ialloc: New files.
diff --git a/lib/linebuffer.c b/lib/linebuffer.c
index 10b0428d0..5b579019f 100644
--- a/lib/linebuffer.c
+++ b/lib/linebuffer.c
@@ -80,8 +80,8 @@ readlinebuffer_delim (struct linebuffer *linebuffer, FILE *stream,
}
if (p == end)
{
- size_t oldsize = linebuffer->size;
- buffer = x2realloc (buffer, &linebuffer->size);
+ idx_t oldsize = linebuffer->size;
+ buffer = xpalloc (buffer, &linebuffer->size, 1, -1, 1);
p = buffer + oldsize;
linebuffer->buffer = buffer;
end = buffer + linebuffer->size;
diff --git a/lib/linebuffer.h b/lib/linebuffer.h
index 691a4c2f8..5fa5ad2fe 100644
--- a/lib/linebuffer.h
+++ b/lib/linebuffer.h
@@ -19,14 +19,15 @@
#if !defined LINEBUFFER_H
# define LINEBUFFER_H
+# include "idx.h"
# include <stdio.h>
/* A 'struct linebuffer' holds a line of text. */
struct linebuffer
{
- size_t size; /* Allocated. */
- size_t length; /* Used. */
+ idx_t size; /* Allocated. */
+ idx_t length; /* Used. */
char *buffer;
};
diff --git a/modules/linebuffer b/modules/linebuffer
index 1444ff074..4ddd3c38e 100644
--- a/modules/linebuffer
+++ b/modules/linebuffer
@@ -6,6 +6,7 @@ lib/linebuffer.h
lib/linebuffer.c
Depends-on:
+idx
xalloc
configure.ac:
@@ -21,4 +22,3 @@ GPL
Maintainer:
Jim Meyering
-
--
2.30.2
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH 08/13] readtokens: prefer idx_t for indexes
2021-06-12 0:25 [PATCH 01/13] ialloc: new module Paul Eggert
` (5 preceding siblings ...)
2021-06-12 0:25 ` [PATCH 07/13] linebuffer: " Paul Eggert
@ 2021-06-12 0:25 ` Paul Eggert
2021-06-12 0:25 ` [PATCH 09/13] readutmp: " Paul Eggert
` (5 subsequent siblings)
12 siblings, 0 replies; 16+ messages in thread
From: Paul Eggert @ 2021-06-12 0:25 UTC (permalink / raw)
To: bug-gnulib; +Cc: Paul Eggert
* 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
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH 09/13] readutmp: prefer idx_t for indexes
2021-06-12 0:25 [PATCH 01/13] ialloc: new module Paul Eggert
` (6 preceding siblings ...)
2021-06-12 0:25 ` [PATCH 08/13] readtokens: " Paul Eggert
@ 2021-06-12 0:25 ` Paul Eggert
2021-06-12 0:25 ` [PATCH 10/13] savedir: " Paul Eggert
` (4 subsequent siblings)
12 siblings, 0 replies; 16+ messages in thread
From: Paul Eggert @ 2021-06-12 0:25 UTC (permalink / raw)
To: bug-gnulib; +Cc: Paul Eggert
* lib/readutmp.c (read_utmp):
Prefer idx_t to size_t for indexes, using idx_t-related allocators.
---
ChangeLog | 1 +
lib/readutmp.c | 12 ++++++------
lib/readutmp.h | 2 ++
3 files changed, 9 insertions(+), 6 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 6e2c98014..be79b866c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -11,6 +11,7 @@
* lib/linebuffer.c (readlinebuffer_delim):
* lib/linebuffer.h (struct linebuffer):
* lib/readtokens.c (readtoken, readtokens):
+ * lib/readutmp.c (read_utmp):
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/readutmp.c b/lib/readutmp.c
index 73db856c5..1035a2b8a 100644
--- a/lib/readutmp.c
+++ b/lib/readutmp.c
@@ -91,8 +91,8 @@ int
read_utmp (char const *file, size_t *n_entries, STRUCT_UTMP **utmp_buf,
int options)
{
- size_t n_read = 0;
- size_t n_alloc = 0;
+ idx_t n_read = 0;
+ idx_t n_alloc = 0;
STRUCT_UTMP *utmp = NULL;
STRUCT_UTMP *u;
@@ -108,7 +108,7 @@ read_utmp (char const *file, size_t *n_entries, STRUCT_UTMP **utmp_buf,
if (desirable_utmp_entry (u, options))
{
if (n_read == n_alloc)
- utmp = x2nrealloc (utmp, &n_alloc, sizeof *utmp);
+ utmp = xpalloc (utmp, &n_alloc, 1, -1, sizeof *utmp);
utmp[n_read++] = *u;
}
@@ -127,8 +127,8 @@ int
read_utmp (char const *file, size_t *n_entries, STRUCT_UTMP **utmp_buf,
int options)
{
- size_t n_read = 0;
- size_t n_alloc = 0;
+ idx_t n_read = 0;
+ idx_t n_alloc = 0;
STRUCT_UTMP *utmp = NULL;
int saved_errno;
FILE *f = fopen (file, "re");
@@ -139,7 +139,7 @@ read_utmp (char const *file, size_t *n_entries, STRUCT_UTMP **utmp_buf,
for (;;)
{
if (n_read == n_alloc)
- utmp = x2nrealloc (utmp, &n_alloc, sizeof *utmp);
+ utmp = xpalloc (utmp, &n_alloc, 1, -1, sizeof *utmp);
if (fread (&utmp[n_read], sizeof utmp[n_read], 1, f) == 0)
break;
n_read += desirable_utmp_entry (&utmp[n_read], options);
diff --git a/lib/readutmp.h b/lib/readutmp.h
index 5971e7fd8..a3a7eda01 100644
--- a/lib/readutmp.h
+++ b/lib/readutmp.h
@@ -212,6 +212,8 @@ enum
};
char *extract_trimmed_name (const STRUCT_UTMP *ut);
+
+/* FIXME: This header should use idx_t, not size_t. */
int read_utmp (char const *file, size_t *n_entries, STRUCT_UTMP **utmp_buf,
int options);
--
2.30.2
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH 10/13] savedir: prefer idx_t for indexes
2021-06-12 0:25 [PATCH 01/13] ialloc: new module Paul Eggert
` (7 preceding siblings ...)
2021-06-12 0:25 ` [PATCH 09/13] readutmp: " Paul Eggert
@ 2021-06-12 0:25 ` Paul Eggert
2021-06-12 0:25 ` [PATCH 11/13] stack: " Paul Eggert
` (3 subsequent siblings)
12 siblings, 0 replies; 16+ messages in thread
From: Paul Eggert @ 2021-06-12 0:25 UTC (permalink / raw)
To: bug-gnulib; +Cc: Paul Eggert
* lib/savedir.c (streamsavedir):
Prefer idx_t to size_t for indexes, using idx_t-related allocators.
---
ChangeLog | 1 +
lib/savedir.c | 14 ++++++--------
2 files changed, 7 insertions(+), 8 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index be79b866c..93039a69a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -12,6 +12,7 @@
* lib/linebuffer.h (struct linebuffer):
* lib/readtokens.c (readtoken, readtokens):
* lib/readutmp.c (read_utmp):
+ * lib/savedir.c (streamsavedir):
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/savedir.c b/lib/savedir.c
index 1c23d75b6..55e0acd87 100644
--- a/lib/savedir.c
+++ b/lib/savedir.c
@@ -93,7 +93,7 @@ streamsavedir (DIR *dirp, enum savedir_option option)
char *name_space = NULL;
idx_t allocated = 0;
direntry_t *entries = NULL;
- size_t entries_allocated = 0;
+ idx_t entries_allocated = 0;
idx_t entries_used = 0;
idx_t used = 0;
comparison_function cmp = comparison_function_table[option];
@@ -120,8 +120,8 @@ streamsavedir (DIR *dirp, enum savedir_option option)
if (cmp)
{
if (entries_allocated == entries_used)
- entries = x2nrealloc (entries, &entries_allocated,
- sizeof *entries);
+ entries = xpalloc (entries, &entries_allocated, 1, -1,
+ sizeof *entries);
entries[entries_used].name = xstrdup (entry);
#if D_INO_IN_DIRENT
entries[entries_used].ino = dp->d_ino;
@@ -149,13 +149,11 @@ streamsavedir (DIR *dirp, enum savedir_option option)
if (cmp)
{
- size_t i;
-
if (entries_used)
qsort (entries, entries_used, sizeof *entries, cmp);
- name_space = xmalloc (used + 1);
+ name_space = ximalloc (used + 1);
used = 0;
- for (i = 0; i < entries_used; i++)
+ for (idx_t i = 0; i < entries_used; i++)
{
char *dest = name_space + used;
used += stpcpy (dest, entries[i].name) - dest + 1;
@@ -164,7 +162,7 @@ streamsavedir (DIR *dirp, enum savedir_option option)
free (entries);
}
else if (used == allocated)
- name_space = xrealloc (name_space, used + 1);
+ name_space = xirealloc (name_space, used + 1);
name_space[used] = '\0';
return name_space;
--
2.30.2
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH 11/13] stack: prefer idx_t for indexes
2021-06-12 0:25 [PATCH 01/13] ialloc: new module Paul Eggert
` (8 preceding siblings ...)
2021-06-12 0:25 ` [PATCH 10/13] savedir: " Paul Eggert
@ 2021-06-12 0:25 ` Paul Eggert
2021-06-13 10:19 ` Bruno Haible
2021-06-12 0:25 ` [PATCH 12/13] userspec: " Paul Eggert
` (2 subsequent siblings)
12 siblings, 1 reply; 16+ messages in thread
From: Paul Eggert @ 2021-06-12 0:25 UTC (permalink / raw)
To: bug-gnulib; +Cc: Paul Eggert
* lib/stack.h (_GL_STACK_TYPE, _GL_STACK_PREFIX):
Prefer idx_t to size_t for indexes, using idx_t-related allocators.
---
ChangeLog | 1 +
lib/stack.h | 6 +++---
2 files changed, 4 insertions(+), 3 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 93039a69a..fc3c91d63 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -13,6 +13,7 @@
* lib/readtokens.c (readtoken, readtokens):
* lib/readutmp.c (read_utmp):
* lib/savedir.c (streamsavedir):
+ * lib/stack.h (_GL_STACK_TYPE, _GL_STACK_PREFIX):
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/stack.h b/lib/stack.h
index dfd57501f..9c3afe724 100644
--- a/lib/stack.h
+++ b/lib/stack.h
@@ -78,7 +78,7 @@ typedef struct
{
GL_STACK_ELEMENT *base;
size_t size;
- size_t allocated;
+ idx_t allocated;
} _GL_STACK_TYPE;
/* Initialize a stack. */
@@ -119,8 +119,8 @@ GL_STACK_STORAGECLASS void
_GL_STACK_PREFIX (push) (_GL_STACK_TYPE *stack, GL_STACK_ELEMENT item)
{
if (stack->size == stack->allocated)
- stack->base = x2nrealloc (stack->base, &stack->allocated,
- sizeof (GL_STACK_ELEMENT));
+ stack->base = xpalloc (stack->base, &stack->allocated, 1, -1,
+ sizeof *stack->base);;
stack->base [stack->size++] = item;
}
--
2.30.2
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH 12/13] userspec: prefer idx_t for indexes
2021-06-12 0:25 [PATCH 01/13] ialloc: new module Paul Eggert
` (9 preceding siblings ...)
2021-06-12 0:25 ` [PATCH 11/13] stack: " Paul Eggert
@ 2021-06-12 0:25 ` 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
12 siblings, 0 replies; 16+ messages in thread
From: Paul Eggert @ 2021-06-12 0:25 UTC (permalink / raw)
To: bug-gnulib; +Cc: Paul Eggert
* lib/userspec.c (parse_with_separator):
Prefer idx_t to size_t for indexes, using idx_t-related allocators.
---
ChangeLog | 1 +
lib/userspec.c | 4 ++--
2 files changed, 3 insertions(+), 2 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index fc3c91d63..e14aeb06a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -14,6 +14,7 @@
* lib/readutmp.c (read_utmp):
* lib/savedir.c (streamsavedir):
* lib/stack.h (_GL_STACK_TYPE, _GL_STACK_PREFIX):
+ * lib/userspec.c (parse_with_separator):
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/userspec.c b/lib/userspec.c
index 68d54b4a1..a58c4896b 100644
--- a/lib/userspec.c
+++ b/lib/userspec.c
@@ -134,10 +134,10 @@ parse_with_separator (char const *spec, char const *separator,
}
else
{
- size_t ulen = separator - spec;
+ idx_t ulen = separator - spec;
if (ulen != 0)
{
- u = xmemdup (spec, ulen + 1);
+ u = ximemdup (spec, ulen + 1);
u[ulen] = '\0';
}
}
--
2.30.2
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH 13/13] xgethostname: prefer idx_t for indexes
2021-06-12 0:25 [PATCH 01/13] ialloc: new module Paul Eggert
` (10 preceding siblings ...)
2021-06-12 0:25 ` [PATCH 12/13] userspec: " Paul Eggert
@ 2021-06-12 0:25 ` Paul Eggert
2021-06-13 10:05 ` [PATCH 01/13] ialloc: new module Bruno Haible
12 siblings, 0 replies; 16+ messages in thread
From: Paul Eggert @ 2021-06-12 0:25 UTC (permalink / raw)
To: bug-gnulib; +Cc: Paul Eggert
* lib/xgethostname.c (XGETANAME):
Prefer idx_t to size_t for indexes, using idx_t-related allocators.
---
ChangeLog | 1 +
lib/xgethostname.c | 2 +-
2 files changed, 2 insertions(+), 1 deletion(-)
diff --git a/ChangeLog b/ChangeLog
index e14aeb06a..c9fd316ab 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -15,6 +15,7 @@
* lib/savedir.c (streamsavedir):
* lib/stack.h (_GL_STACK_TYPE, _GL_STACK_PREFIX):
* lib/userspec.c (parse_with_separator):
+ * lib/xgethostname.c (XGETANAME):
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/xgethostname.c b/lib/xgethostname.c
index 87ae6dce8..84c2f279b 100644
--- a/lib/xgethostname.c
+++ b/lib/xgethostname.c
@@ -59,7 +59,7 @@ XGETANAME (void)
specify whether a truncated name is null-terminated. */
idx_t actual_size = strlen (name) + 1;
if (actual_size < size_1)
- return alloc ? alloc : xmemdup (name, actual_size);
+ return alloc ? alloc : ximemdup (name, actual_size);
errno = 0;
}
free (alloc);
--
2.30.2
^ permalink raw reply related [flat|nested] 16+ messages in thread
* Re: [PATCH 01/13] ialloc: new module
2021-06-12 0:25 [PATCH 01/13] ialloc: new module Paul Eggert
` (11 preceding siblings ...)
2021-06-12 0:25 ` [PATCH 13/13] xgethostname: " Paul Eggert
@ 2021-06-13 10:05 ` Bruno Haible
12 siblings, 0 replies; 16+ messages in thread
From: Bruno Haible @ 2021-06-13 10:05 UTC (permalink / raw)
To: bug-gnulib; +Cc: Paul Eggert
Hi Paul,
> + ialloc: new module
> + * lib/ialloc.c, lib/ialloc.h, modules/ialloc: New files.
The 'check-copyright' test now fails:
$ ./check-copyright
Module License File License File name
================= ============== =====================================
LGPL ?? lib/ialloc.c
LGPL GPL lib/ialloc.h
This patch fixes it, following the new habits, see
<https://lists.gnu.org/archive/html/bug-gnulib/2021-06/msg00003.html>
<https://lists.gnu.org/archive/html/bug-gnulib/2021-06/msg00004.html>.
2021-06-13 Bruno Haible <bruno@clisp.org>
ialloc: Put appropriate license notice in source files.
* lib/ialloc.h: Use LGPLv3+ notice.
* lib/ialloc.c: Likewise.
diff --git a/lib/ialloc.c b/lib/ialloc.c
index 9eff290..f506b84 100644
--- a/lib/ialloc.c
+++ b/lib/ialloc.c
@@ -1,3 +1,21 @@
+/* malloc with idx_t rather than size_t
+
+ Copyright 2021 Free Software Foundation, Inc.
+
+ This file is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 3 of the
+ License, or (at your option) any later version.
+
+ This file is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>. */
+
#include <config.h>
+
#define IALLOC_INLINE _GL_EXTERN_INLINE
#include "ialloc.h"
diff --git a/lib/ialloc.h b/lib/ialloc.h
index e243c92..ebe4aaa 100644
--- a/lib/ialloc.h
+++ b/lib/ialloc.h
@@ -2,17 +2,17 @@
Copyright 2021 Free Software Foundation, Inc.
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 3 of the License, or
- (at your option) any later version.
+ This file is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 3 of the
+ License, or (at your option) any later version.
- This program is distributed in the hope that it will be useful,
+ This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
+ GNU Lesser General Public License for more details.
- You should have received a copy of the GNU General Public License
+ You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
#ifndef IALLOC_H_
^ permalink raw reply related [flat|nested] 16+ messages in thread
* Re: [PATCH 11/13] stack: prefer idx_t for indexes
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
0 siblings, 1 reply; 16+ messages in thread
From: Bruno Haible @ 2021-06-13 10:19 UTC (permalink / raw)
To: bug-gnulib; +Cc: Paul Eggert
Paul Eggert wrote:
> @@ -119,8 +119,8 @@ GL_STACK_STORAGECLASS void
> _GL_STACK_PREFIX (push) (_GL_STACK_TYPE *stack, GL_STACK_ELEMENT item)
> {
> if (stack->size == stack->allocated)
> - stack->base = x2nrealloc (stack->base, &stack->allocated,
> - sizeof (GL_STACK_ELEMENT));
> + stack->base = xpalloc (stack->base, &stack->allocated, 1, -1,
> + sizeof *stack->base);;
There is a stray semicolon here.
Bruno
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH 11/13] stack: prefer idx_t for indexes
2021-06-13 10:19 ` Bruno Haible
@ 2021-06-13 17:47 ` Paul Eggert
0 siblings, 0 replies; 16+ messages in thread
From: Paul Eggert @ 2021-06-13 17:47 UTC (permalink / raw)
To: Bruno Haible; +Cc: Gnulib bugs
On 6/13/21 3:19 AM, Bruno Haible wrote:
> There is a stray semicolon here.
Thanks, fixed it.
Also, thanks for the copyright notice fixes: I had drafted those files
before the recent copyright-notice changes and forgot to check the
file's notices when installing them.
^ permalink raw reply [flat|nested] 16+ messages in thread
end of thread, other threads:[~2021-06-13 17:47 UTC | newest]
Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [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
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).