bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
From: Paul Eggert <eggert@cs.ucla.edu>
To: bug-gnulib@gnu.org, berrange@redhat.com
Cc: Paul Eggert <eggert@cs.ucla.edu>
Subject: [PATCH 4/8] xalloc: new function xreallocarray
Date: Sun, 18 Apr 2021 21:01:55 -0700	[thread overview]
Message-ID: <20210419040158.1902066-4-eggert@cs.ucla.edu> (raw)
In-Reply-To: <20210419040158.1902066-1-eggert@cs.ucla.edu>

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



  parent reply	other threads:[~2021-04-19  4:04 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-19  4:01 [PATCH 1/8] safe-alloc: improve doc Paul Eggert
2021-04-19  4:01 ` [PATCH 2/8] backupfile: simplify via realloc-gnu Paul Eggert
2021-04-19  4:01 ` [PATCH 3/8] group-member: " Paul Eggert
2021-04-19  4:01 ` Paul Eggert [this message]
2021-04-22  0:18   ` [PATCH 4/8] xalloc: new function xreallocarray Bruno Haible
2021-04-22 19:39     ` Paul Eggert
2021-10-19 23:49     ` Paul Eggert
2021-04-19  4:01 ` [PATCH 5/8] xalloc-oversized: fix SIZE_MAX optimization bug Paul Eggert
2021-04-19  4:01 ` [PATCH 6/8] safe-alloc: simplify via reallocarray Paul Eggert
2021-04-19  4:01 ` [PATCH 7/8] calloc-gnu: now LGPLv2+ Paul Eggert
2021-04-19  4:01 ` [PATCH 8/8] safe-alloc: fix pointer implementation Paul Eggert
2021-04-22 18:20 ` [PATCH 1/8] safe-alloc: improve doc Eric Blake
2021-04-22 19:32   ` Paul Eggert
2021-04-22 19:43     ` Eric Blake
2021-04-22 20:27   ` Eric Blake

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210419040158.1902066-4-eggert@cs.ucla.edu \
    --to=eggert@cs.ucla.edu \
    --cc=berrange@redhat.com \
    --cc=bug-gnulib@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).