bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
* [PATCH] xalloc: new function xpalloc, from dfa
@ 2021-03-29  3:06 Paul Eggert
  2021-04-03 16:27 ` Bruno Haible
  0 siblings, 1 reply; 2+ messages in thread
From: Paul Eggert @ 2021-03-29  3:06 UTC (permalink / raw)
  To: bug-gnulib, arnold; +Cc: Paul Eggert

Move xpalloc from dfa.c to xmalloc.c and change it from static to
extern.  The function is useful in other contexts; I’m about to
use it in coreutils.
* lib/dfa.c: Include idx.h, instead of rolling our own idx_t and
IDX_MAX.  Do not include intprops.h; no longer needed.
(xpalloc): Move from here ...
* lib/xmalloc.c (xpalloc): ... to here, and make it extern.
Include intprops.h and minmax.h, needed by xpalloc.
* lib/xalloc.h: Include idx.h, for idx_t.
* modules/dfa (Depends-on): Add idx; remove intprops.
* modules/xalloc (Depends-on): Add idx, intprops, minmax.
---
 ChangeLog      | 15 +++++++++++
 lib/dfa.c      | 68 +-------------------------------------------------
 lib/xalloc.h   |  3 +++
 lib/xmalloc.c  | 63 ++++++++++++++++++++++++++++++++++++++++++++++
 modules/dfa    |  2 +-
 modules/xalloc |  3 +++
 6 files changed, 86 insertions(+), 68 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 9f010fd4e..78bf3a5a7 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,18 @@
+2021-03-28  Paul Eggert  <eggert@cs.ucla.edu>
+
+	xalloc: new function xpalloc, from dfa
+	Move xpalloc from dfa.c to xmalloc.c and change it from static to
+	extern.  The function is useful in other contexts; I’m about to
+	use it in coreutils.
+	* lib/dfa.c: Include idx.h, instead of rolling our own idx_t and
+	IDX_MAX.  Do not include intprops.h; no longer needed.
+	(xpalloc): Move from here ...
+	* lib/xmalloc.c (xpalloc): ... to here, and make it extern.
+	Include intprops.h and minmax.h, needed by xpalloc.
+	* lib/xalloc.h: Include idx.h, for idx_t.
+	* modules/dfa (Depends-on): Add idx; remove intprops.
+	* modules/xalloc (Depends-on): Add idx, intprops, minmax.
+
 2021-03-28  Bruno Haible  <bruno@clisp.org>
 
 	linked-list tests: Add another test for SIGNAL_SAFE_LIST.
diff --git a/lib/dfa.c b/lib/dfa.c
index 4929e4c34..33de2fb82 100644
--- a/lib/dfa.c
+++ b/lib/dfa.c
@@ -25,6 +25,7 @@
 #include "dfa.h"
 
 #include "flexmember.h"
+#include "idx.h"
 
 #include <assert.h>
 #include <ctype.h>
@@ -34,12 +35,6 @@
 #include <limits.h>
 #include <string.h>
 
-/* Another name for ptrdiff_t, for sizes of objects and nonnegative
-   indexes into objects.  It is signed to help catch integer overflow.
-   It has its own name because it is for nonnegative values only.  */
-typedef ptrdiff_t idx_t;
-static idx_t const IDX_MAX = PTRDIFF_MAX;
-
 static bool
 streq (char const *a, char const *b)
 {
@@ -57,7 +52,6 @@ isasciidigit (char c)
 
 #include <wchar.h>
 
-#include "intprops.h"
 #include "xalloc.h"
 #include "localeinfo.h"
 
@@ -791,66 +785,6 @@ emptyset (charclass const *s)
   return w == 0;
 }
 
-/* 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.
-
-   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
-   infinity.
-
-   If PA is null, then allocate a new array instead of reallocating
-   the old one.
-
-   Thus, to grow an array A without saving its old contents, do
-   { free (A); A = xpalloc (NULL, &AITEMS, ...); }.  */
-
-static void *
-xpalloc (void *pa, idx_t *nitems, idx_t nitems_incr_min,
-         ptrdiff_t nitems_max, idx_t item_size)
-{
-  idx_t n0 = *nitems;
-
-  /* The approximate size to use for initial small allocation
-     requests.  This is the largest "small" request for the GNU C
-     library malloc.  */
-  enum { DEFAULT_MXFAST = 64 * sizeof (size_t) / 4 };
-
-  /* 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.  */
-
-  idx_t n, nbytes;
-  if (INT_ADD_WRAPV (n0, n0 >> 1, &n))
-    n = IDX_MAX;
-  if (0 <= nitems_max && nitems_max < n)
-    n = nitems_max;
-
-  idx_t adjusted_nbytes
-    = ((INT_MULTIPLY_WRAPV (n, item_size, &nbytes) || SIZE_MAX < 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;
-    }
-
-  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)))
-    xalloc_die ();
-  pa = xrealloc (pa, nbytes);
-  *nitems = n;
-  return pa;
-}
-
 /* Ensure that the array addressed by PA holds at least I + 1 items.
    Either return PA, or reallocate the array and return its new address.
    Although PA may be null, the returned value is never null.
diff --git a/lib/xalloc.h b/lib/xalloc.h
index 7ab68f4e6..76d83c63c 100644
--- a/lib/xalloc.h
+++ b/lib/xalloc.h
@@ -21,6 +21,7 @@
 #include <stddef.h>
 #include <stdint.h>
 
+#include "idx.h"
 #include "xalloc-oversized.h"
 
 #ifndef _GL_INLINE_HEADER_BEGIN
@@ -59,6 +60,8 @@ void *xcalloc (size_t n, size_t s)
 void *xrealloc (void *p, size_t s)
       _GL_ATTRIBUTE_ALLOC_SIZE ((2));
 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));
 char *xstrdup (char const *str)
diff --git a/lib/xmalloc.c b/lib/xmalloc.c
index 4203f19ce..faeccacc9 100644
--- a/lib/xmalloc.c
+++ b/lib/xmalloc.c
@@ -21,6 +21,9 @@
 
 #include "xalloc.h"
 
+#include "intprops.h"
+#include "minmax.h"
+
 #include <stdlib.h>
 #include <string.h>
 
@@ -87,6 +90,66 @@ x2realloc (void *p, size_t *pn)
   return x2nrealloc (p, pn, 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.
+
+   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
+   infinity.
+
+   If PA is null, then allocate a new array instead of reallocating
+   the old one.
+
+   Thus, to grow an array A without saving its old contents, do
+   { 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)
+{
+  idx_t n0 = *nitems;
+
+  /* The approximate size to use for initial small allocation
+     requests.  This is the largest "small" request for the GNU C
+     library malloc.  */
+  enum { DEFAULT_MXFAST = 64 * sizeof (size_t) / 4 };
+
+  /* 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.  */
+
+  idx_t n, nbytes;
+  if (INT_ADD_WRAPV (n0, n0 >> 1, &n))
+    n = IDX_MAX;
+  if (0 <= nitems_max && nitems_max < n)
+    n = nitems_max;
+
+  idx_t adjusted_nbytes
+    = ((INT_MULTIPLY_WRAPV (n, item_size, &nbytes) || SIZE_MAX < 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;
+    }
+
+  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)))
+    xalloc_die ();
+  pa = xrealloc (pa, nbytes);
+  *nitems = n;
+  return pa;
+}
+
 /* Allocate N 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).  */
diff --git a/modules/dfa b/modules/dfa
index 303957fa5..4b78ef487 100644
--- a/modules/dfa
+++ b/modules/dfa
@@ -12,7 +12,7 @@ assert
 c99
 ctype
 flexmember
-intprops
+idx
 locale
 regex
 stdbool
diff --git a/modules/xalloc b/modules/xalloc
index 65007561b..5fa386a5d 100644
--- a/modules/xalloc
+++ b/modules/xalloc
@@ -9,6 +9,9 @@ m4/xalloc.m4
 Depends-on:
 c99
 extern-inline
+idx
+intprops
+minmax
 stdint
 xalloc-die
 xalloc-oversized
-- 
2.30.2



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] xalloc: new function xpalloc, from dfa
  2021-03-29  3:06 [PATCH] xalloc: new function xpalloc, from dfa Paul Eggert
@ 2021-04-03 16:27 ` Bruno Haible
  0 siblings, 0 replies; 2+ messages in thread
From: Bruno Haible @ 2021-04-03 16:27 UTC (permalink / raw)
  To: bug-gnulib; +Cc: Paul Eggert

Paul Eggert wrote:
> * lib/xalloc.h: Include idx.h, for idx_t.

This causes a compilation error in a testdir:

In file included from ../../gltests/xalloc-die.c:21:0:
../../gltests/xalloc.h:24:17: fatal error: idx.h: No such file or directory
compilation terminated.
make[4]: *** [Makefile:3215: xalloc-die.o] Error 1

This patch fixes it.


2021-04-03  Bruno Haible  <bruno@clisp.org>

	xalloc-die: Fix compilation error (regression from 2021-03-28).
	* lib/xalloc.h: Don't include idx.h and xalloc-oversized.h if the module
	'xalloc' is not in use.
	* modules/xalloc-die (Depends-on): Remove xalloc-oversized.

diff --git a/lib/xalloc.h b/lib/xalloc.h
index 5633fdf..6e7de60 100644
--- a/lib/xalloc.h
+++ b/lib/xalloc.h
@@ -21,8 +21,10 @@
 #include <stddef.h>
 #include <stdint.h>
 
-#include "idx.h"
-#include "xalloc-oversized.h"
+#if GNULIB_XALLOC
+# include "idx.h"
+# include "xalloc-oversized.h"
+#endif
 
 #ifndef _GL_INLINE_HEADER_BEGIN
  #error "Please include config.h first."
diff --git a/modules/xalloc-die b/modules/xalloc-die
index f53de5a..e8ef52a 100644
--- a/modules/xalloc-die
+++ b/modules/xalloc-die
@@ -11,7 +11,6 @@ extern-inline
 gettext-h
 exitfail
 stdint
-xalloc-oversized
 
 configure.ac:
 gl_MODULE_INDICATOR([xalloc-die])



^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-04-03 16:49 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-29  3:06 [PATCH] xalloc: new function xpalloc, from dfa Paul Eggert
2021-04-03 16:27 ` 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).