]> Savannah Git Hosting - gnulib.git/commitdiff
xalloc: new function xpalloc, from dfa
authorPaul Eggert <eggert@cs.ucla.edu>
Mon, 29 Mar 2021 03:02:21 +0000 (20:02 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Mon, 29 Mar 2021 03:04:52 +0000 (20:04 -0700)
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
lib/dfa.c
lib/xalloc.h
lib/xmalloc.c
modules/dfa
modules/xalloc

index 9f010fd4ec3b8058d68ba7bb0b9fb292328da799..78bf3a5a749ef4af21d0c1623988e5f66b841d88 100644 (file)
--- 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.
index 4929e4c3487a3a3b6e6f56724c7aac70a3081d3f..33de2fb82e71ca71b3e017f40d965bed400025bb 100644 (file)
--- 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>
 #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.
index 7ab68f4e6caf6286d924ea6c6a41179f6486d148..76d83c63c11c2a83530985b721bafec6a5f6437c 100644 (file)
@@ -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)
index 4203f19ce54ae92201a0fdf897922a64e471563e..faeccacc9bad9d8a853612a2922151c8cdd5a690 100644 (file)
@@ -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).  */
index 303957fa50284fc4ce8335724a84d1ba3ad7efd9..4b78ef4870f002c56c9e6141ae4df652688f065f 100644 (file)
@@ -12,7 +12,7 @@ assert
 c99
 ctype
 flexmember
-intprops
+idx
 locale
 regex
 stdbool
index 65007561b54b21a62cbf98a451ac5b4060815125..5fa386a5d91e70d21a5e71c4f0f1fd48b11d1690 100644 (file)
@@ -9,6 +9,9 @@ m4/xalloc.m4
 Depends-on:
 c99
 extern-inline
+idx
+intprops
+minmax
 stdint
 xalloc-die
 xalloc-oversized