[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 4/8] xalloc: new function xreallocarray
From: |
Paul Eggert |
Subject: |
[PATCH 4/8] xalloc: new function xreallocarray |
Date: |
Sun, 18 Apr 2021 21:01:55 -0700 |
This effectively replaces xnmalloc, which perhaps should be deprecated.
The name xreallocarray should be easier to remember now that
reallocarray is a standard GNU function.
* lib/xalloc.h [GNULIB_XALLOC]: Do not include xalloc-oversized.h.
(xnmalloc, xnrealloc, x2nrealloc): Simplify by using xreallocarray.
* lib/xmalloc.c (xreallocarray): New function.
* modules/xalloc (Depends-on): Add reallocarray;
remove xalloc-oversized.
---
ChangeLog | 10 ++++++++++
lib/xalloc.h | 26 ++++++++++++++------------
lib/xmalloc.c | 12 ++++++++++++
modules/xalloc | 2 +-
4 files changed, 37 insertions(+), 13 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index aff0e87fc..857b7048f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,15 @@
2021-04-18 Paul Eggert <eggert@cs.ucla.edu>
+ xalloc: new function xreallocarray
+ This effectively replaces xnmalloc, which perhaps should be deprecated.
+ The name xreallocarray should be easier to remember now that
+ reallocarray is a standard GNU function.
+ * lib/xalloc.h [GNULIB_XALLOC]: Do not include xalloc-oversized.h.
+ (xnmalloc, xnrealloc, x2nrealloc): Simplify by using xreallocarray.
+ * lib/xmalloc.c (xreallocarray): New function.
+ * modules/xalloc (Depends-on): Add reallocarray;
+ remove xalloc-oversized.
+
group-member: simplify via realloc-gnu
* lib/group-member.c, modules/group-member:
Simplify similarly to backupfile.
diff --git a/lib/xalloc.h b/lib/xalloc.h
index b08ab4e07..6cd7a680c 100644
--- a/lib/xalloc.h
+++ b/lib/xalloc.h
@@ -24,7 +24,6 @@
#if GNULIB_XALLOC
# include "idx.h"
# include "intprops.h"
-# include "xalloc-oversized.h"
#endif
#ifndef _GL_INLINE_HEADER_BEGIN
@@ -62,6 +61,8 @@ 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));
+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);
@@ -104,11 +105,10 @@ XALLOC_INLINE void *xnmalloc (size_t n, size_t s)
XALLOC_INLINE void *
xnmalloc (size_t n, size_t s)
{
- if (xalloc_oversized (n, s))
- xalloc_die ();
- return xmalloc (n * s);
+ return xreallocarray (NULL, n, s);
}
+/* FIXME: Deprecate this in favor of xreallocarray? */
/* Change the size of an allocated block of memory P to an array of N
objects each of S bytes, with error checking. S must be nonzero. */
@@ -117,9 +117,7 @@ XALLOC_INLINE void *xnrealloc (void *p, size_t n, size_t s)
XALLOC_INLINE void *
xnrealloc (void *p, size_t n, size_t s)
{
- if (xalloc_oversized (n, s))
- xalloc_die ();
- return xrealloc (p, n * s);
+ return xreallocarray (p, n, s);
}
/* If P is null, allocate a block of at least *PN such objects;
@@ -202,10 +200,7 @@ x2nrealloc (void *p, size_t *pn, size_t s)
xalloc_die ();
}
- xalloc_count_t nbytes;
- if (INT_MULTIPLY_WRAPV (n, s, &nbytes))
- xalloc_die ();
- p = xrealloc (p, nbytes);
+ p = xreallocarray (p, n, s);
*pn = n;
return p;
}
@@ -241,10 +236,17 @@ xrealloc (T *p, size_t s)
return (T *) xrealloc ((void *) p, s);
}
+template <typename T> inline T *
+xreallocarray (T *p, size_t n, size_t s)
+{
+ return (T *) xreallocarray ((void *) p, n, s);
+}
+
+/* FIXME: Deprecate this in favor of xreallocarray? */
template <typename T> inline T *
xnrealloc (T *p, size_t n, size_t s)
{
- return (T *) xnrealloc ((void *) p, n, s);
+ return xreallocarray (p, n, s);
}
template <typename T> inline T *
diff --git a/lib/xmalloc.c b/lib/xmalloc.c
index 39ce893ad..88698fade 100644
--- a/lib/xmalloc.c
+++ b/lib/xmalloc.c
@@ -50,6 +50,18 @@ xrealloc (void *p, size_t n)
return r;
}
+/* Change the size of an allocated block of memory P to an array of N
+ objects each of S bytes, with error checking. */
+
+void *
+xreallocarray (void *p, size_t n, size_t s)
+{
+ void *r = reallocarray (p, n, s);
+ if (!r && (!p || (n && s)))
+ xalloc_die ();
+ 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
diff --git a/modules/xalloc b/modules/xalloc
index 000933e94..0dbae1c86 100644
--- a/modules/xalloc
+++ b/modules/xalloc
@@ -15,9 +15,9 @@ intprops
malloc-gnu
minmax
realloc-gnu
+reallocarray
stdint
xalloc-die
-xalloc-oversized
configure.ac:
gl_XALLOC
--
2.27.0
- [PATCH 1/8] safe-alloc: improve doc, Paul Eggert, 2021/04/19
- [PATCH 2/8] backupfile: simplify via realloc-gnu, Paul Eggert, 2021/04/19
- [PATCH 3/8] group-member: simplify via realloc-gnu, Paul Eggert, 2021/04/19
- [PATCH 4/8] xalloc: new function xreallocarray,
Paul Eggert <=
- [PATCH 6/8] safe-alloc: simplify via reallocarray, Paul Eggert, 2021/04/19
- [PATCH 7/8] calloc-gnu: now LGPLv2+, Paul Eggert, 2021/04/19
- [PATCH 8/8] safe-alloc: fix pointer implementation, Paul Eggert, 2021/04/19
- [PATCH 5/8] xalloc-oversized: fix SIZE_MAX optimization bug, Paul Eggert, 2021/04/19
- Re: [PATCH 1/8] safe-alloc: improve doc, Eric Blake, 2021/04/22