+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.
#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)
{
#include <wchar.h>
-#include "intprops.h"
#include "xalloc.h"
#include "localeinfo.h"
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.
#include <stddef.h>
#include <stdint.h>
+#include "idx.h"
#include "xalloc-oversized.h"
#ifndef _GL_INLINE_HEADER_BEGIN
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)
#include "xalloc.h"
+#include "intprops.h"
+#include "minmax.h"
+
#include <stdlib.h>
#include <string.h>
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). */
c99
ctype
flexmember
-intprops
+idx
locale
regex
stdbool
Depends-on:
c99
extern-inline
+idx
+intprops
+minmax
stdint
xalloc-die
xalloc-oversized