summaryrefslogtreecommitdiff
path: root/lib/realloc.c
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2007-09-09 12:56:27 +0000
committerBruno Haible <bruno@clisp.org>2007-09-09 12:56:27 +0000
commit471d58a565341806b6ed60b3a4a68a40fb1c932d (patch)
treeee8b7725472638485aafb7483252f8f5908334b3 /lib/realloc.c
parente8b82791a9b8ebb792a6a40e0b0db3c1edf71f5d (diff)
downloadgnulib-471d58a565341806b6ed60b3a4a68a40fb1c932d.tar.gz
New module 'realloc-posix'.
Diffstat (limited to 'lib/realloc.c')
-rw-r--r--lib/realloc.c28
1 files changed, 22 insertions, 6 deletions
diff --git a/lib/realloc.c b/lib/realloc.c
index c1fe9e5a18..a68f144bd3 100644
--- a/lib/realloc.c
+++ b/lib/realloc.c
@@ -1,6 +1,6 @@
/* realloc() function that is glibc compatible.
- Copyright (C) 1997, 2003, 2004, 2006 Free Software Foundation, Inc.
+ Copyright (C) 1997, 2003, 2004, 2006, 2007 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -16,13 +16,20 @@
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
-/* written by Jim Meyering */
+/* written by Jim Meyering and Bruno Haible */
#include <config.h>
-#undef realloc
+/* Only the AC_FUNC_REALLOC macro defines 'realloc' already in config.h. */
+#ifdef realloc
+# define NEED_REALLOC_GNU
+# undef realloc
+#endif
+/* Specification. */
#include <stdlib.h>
+#include <errno.h>
+
/* Change the size of an allocated block of memory P to N bytes,
with error checking. If N is zero, change it to 1. If P is NULL,
use malloc. */
@@ -30,6 +37,9 @@
void *
rpl_realloc (void *p, size_t n)
{
+ void *result;
+
+#ifdef NEED_REALLOC_GNU
if (n == 0)
{
n = 1;
@@ -38,8 +48,14 @@ rpl_realloc (void *p, size_t n)
free (p);
p = NULL;
}
+#endif
+
+ result = (p == NULL ? malloc (n) : realloc (p, n));
+
+#if !HAVE_REALLOC_POSIX
+ if (result == NULL)
+ errno = ENOMEM;
+#endif
- if (p == NULL)
- return malloc (n);
- return realloc (p, n);
+ return result;
}