New module 'realloc-posix'.
authorBruno Haible <bruno@clisp.org>
Sun, 9 Sep 2007 12:56:27 +0000 (12:56 +0000)
committerBruno Haible <bruno@clisp.org>
Sun, 9 Sep 2007 12:56:27 +0000 (12:56 +0000)
ChangeLog
doc/functions/realloc.texi
lib/realloc.c
lib/stdlib_.h
m4/realloc.m4 [new file with mode: 0644]
m4/stdlib_h.m4
modules/realloc
modules/realloc-posix [new file with mode: 0644]
modules/stdlib

index 9892c43..fa26fa1 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,21 @@
 2007-09-09  Bruno Haible  <bruno@clisp.org>
 
+       * modules/realloc-posix: New file.
+       * modules/realloc (Depends-on): Add realloc-posix.
+       * lib/realloc.c: Include errno.h.
+       (rpl_realloc): Merge the requirements of a glibc-compatible realloc
+       and a POSIX-compatible realloc into a single function. Set ENOMEM
+       when returning NULL.
+       * m4/realloc.m4: New file.
+       * doc/functions/realloc.texi: Mention the realloc-posix module.
+       * lib/stdlib_.h (realloc): New declaration.
+       * m4/stdlib_h.m4 (gl_STDLIB_H_DEFAULTS): Initialize
+       GNULIB_REALLOC_POSIX and HAVE_REALLOC_POSIX.
+       * modules/stdlib (stdlib.h): Substitute also GNULIB_REALLOC_POSIX
+       and HAVE_REALLOC_POSIX.
+
+2007-09-09  Bruno Haible  <bruno@clisp.org>
+
        * modules/calloc-posix: New file.
        * modules/calloc (Depends-on): Add calloc-posix.
        * lib/calloc.c: Include errno.h.
index 84e20f9..58944d1 100644 (file)
@@ -4,10 +4,14 @@
 
 POSIX specification: @url{http://www.opengroup.org/susv3xsh/realloc.html}
 
-Gnulib module: ---
+Gnulib module: realloc-posix
 
 Portability problems fixed by Gnulib:
 @itemize
+@item
+Upon failure, the function does not set @code{errno} to @code{ENOMEM} on
+some platforms:
+mingw.
 @end itemize
 
 Portability problems not fixed by Gnulib:
index c1fe9e5..a68f144 100644 (file)
@@ -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
    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;
 }
index adff158..72488c7 100644 (file)
@@ -55,6 +55,21 @@ extern "C" {
 #endif
 
 
+#if @GNULIB_REALLOC_POSIX@
+# if !@HAVE_REALLOC_POSIX@
+#  undef realloc
+#  define realloc rpl_realloc
+extern void * realloc (void *ptr, size_t size);
+# endif
+#elif defined GNULIB_POSIXCHECK
+# undef realloc
+# define realloc(p,s) \
+    (GL_LINK_WARNING ("realloc is not POSIX compliant everywhere - " \
+                      "use gnulib module realloc-posix for portability"), \
+     realloc (p, s))
+#endif
+
+
 #if @GNULIB_CALLOC_POSIX@
 # if !@HAVE_CALLOC_POSIX@
 #  undef calloc
diff --git a/m4/realloc.m4 b/m4/realloc.m4
new file mode 100644 (file)
index 0000000..c00f047
--- /dev/null
@@ -0,0 +1,34 @@
+# realloc.m4 serial 8
+dnl Copyright (C) 2007 Free Software Foundation, Inc.
+dnl This file is free software; the Free Software Foundation
+dnl gives unlimited permission to copy and/or distribute it,
+dnl with or without modifications, as long as this notice is preserved.
+
+# gl_FUNC_REALLOC_POSIX
+# --------------------
+# Test whether 'realloc' is POSIX compliant (sets errno to ENOMEM when it
+# fails), and replace realloc if it is not.
+AC_DEFUN([gl_FUNC_REALLOC_POSIX],
+[
+  AC_CACHE_CHECK([whether malloc, realloc, calloc are POSIX compliant],
+    [gl_cv_func_malloc_posix],
+    [
+      dnl It is too dangerous to try to allocate a large amount of memory:
+      dnl some systems go to their knees when you do that. So assume that
+      dnl all Unix implementations of the function are POSIX compliant.
+      AC_TRY_COMPILE([],
+        [#if !((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__)
+         choke me
+         #endif
+        ], [gl_cv_func_malloc_posix=yes], [gl_cv_func_malloc_posix=no])
+    ])
+  if test $gl_cv_func_malloc_posix = yes; then
+    HAVE_REALLOC_POSIX=1
+    AC_DEFINE([HAVE_REALLOC_POSIX], 1,
+      [Define if the 'realloc' function is POSIX compliant.])
+  else
+    AC_LIBOBJ([realloc])
+    HAVE_REALLOC_POSIX=0
+  fi
+  AC_SUBST([HAVE_REALLOC_POSIX])
+])
index 989f539..600bd09 100644 (file)
@@ -19,13 +19,15 @@ AC_DEFUN([gl_STDLIB_MODULE_INDICATOR],
 
 AC_DEFUN([gl_STDLIB_H_DEFAULTS],
 [
-  GNULIB_CALLOC_POSIX=0; AC_SUBST([GNULIB_CALLOC_POSIX])
-  GNULIB_GETSUBOPT=0;    AC_SUBST([GNULIB_GETSUBOPT])
-  GNULIB_MKDTEMP=0;      AC_SUBST([GNULIB_MKDTEMP])
-  GNULIB_MKSTEMP=0;      AC_SUBST([GNULIB_MKSTEMP])
+  GNULIB_REALLOC_POSIX=0; AC_SUBST([GNULIB_REALLOC_POSIX])
+  GNULIB_CALLOC_POSIX=0;  AC_SUBST([GNULIB_CALLOC_POSIX])
+  GNULIB_GETSUBOPT=0;     AC_SUBST([GNULIB_GETSUBOPT])
+  GNULIB_MKDTEMP=0;       AC_SUBST([GNULIB_MKDTEMP])
+  GNULIB_MKSTEMP=0;       AC_SUBST([GNULIB_MKSTEMP])
   dnl Assume proper GNU behavior unless another module says otherwise.
-  HAVE_CALLOC_POSIX=1;   AC_SUBST([HAVE_CALLOC_POSIX])
-  HAVE_GETSUBOPT=1;      AC_SUBST([HAVE_GETSUBOPT])
-  HAVE_MKDTEMP=1;        AC_SUBST([HAVE_MKDTEMP])
-  REPLACE_MKSTEMP=0;     AC_SUBST([REPLACE_MKSTEMP])
+  HAVE_CALLOC_POSIX=1;    AC_SUBST([HAVE_CALLOC_POSIX])
+  HAVE_GETSUBOPT=1;       AC_SUBST([HAVE_GETSUBOPT])
+  HAVE_MKDTEMP=1;         AC_SUBST([HAVE_MKDTEMP])
+  HAVE_REALLOC_POSIX=1;   AC_SUBST([HAVE_REALLOC_POSIX])
+  REPLACE_MKSTEMP=0;      AC_SUBST([REPLACE_MKSTEMP])
 ])
index 5aa68be..316f588 100644 (file)
@@ -5,6 +5,7 @@ Files:
 lib/realloc.c
 
 Depends-on:
+realloc-posix
 
 configure.ac:
 AC_FUNC_REALLOC
diff --git a/modules/realloc-posix b/modules/realloc-posix
new file mode 100644 (file)
index 0000000..4186e90
--- /dev/null
@@ -0,0 +1,25 @@
+Description:
+realloc() function: allocate memory with indefinite extent.
+
+Files:
+lib/realloc.c
+m4/realloc.m4
+
+Depends-on:
+stdlib
+
+configure.ac:
+gl_FUNC_REALLOC_POSIX
+gl_STDLIB_MODULE_INDICATOR([realloc-posix])
+
+Makefile.am:
+
+Include:
+<stdlib.h>
+
+License:
+LGPL
+
+Maintainer:
+Bruno Haible
+
index 5ef9c4f..8a1839a 100644 (file)
@@ -23,6 +23,7 @@ stdlib.h: stdlib_.h
        { echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */' && \
          sed -e 's/@''INCLUDE_NEXT''@/$(INCLUDE_NEXT)/g' \
              -e 's|@''NEXT_STDLIB_H''@|$(NEXT_STDLIB_H)|g' \
+             -e 's|@''GNULIB_REALLOC_POSIX''@|$(GNULIB_REALLOC_POSIX)|g' \
              -e 's|@''GNULIB_CALLOC_POSIX''@|$(GNULIB_CALLOC_POSIX)|g' \
              -e 's|@''GNULIB_GETSUBOPT''@|$(GNULIB_GETSUBOPT)|g' \
              -e 's|@''GNULIB_MKDTEMP''@|$(GNULIB_MKDTEMP)|g' \
@@ -30,6 +31,7 @@ stdlib.h: stdlib_.h
              -e 's|@''HAVE_CALLOC_POSIX''@|$(HAVE_CALLOC_POSIX)|g' \
              -e 's|@''HAVE_GETSUBOPT''@|$(HAVE_GETSUBOPT)|g' \
              -e 's|@''HAVE_MKDTEMP''@|$(HAVE_MKDTEMP)|g' \
+             -e 's|@''HAVE_REALLOC_POSIX''@|$(HAVE_REALLOC_POSIX)|g' \
              -e 's|@''REPLACE_MKSTEMP''@|$(REPLACE_MKSTEMP)|g' \
              -e '/definition of GL_LINK_WARNING/r $(LINK_WARNING_H)' \
              < $(srcdir)/stdlib_.h; \