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

index 5db53d6..9892c43 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,21 @@
 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.
+       (rpl_calloc): Merge the requirements of a glibc-compatible calloc
+       and a POSIX-compatible calloc into a single function. Set ENOMEM
+       when returning NULL.
+       * m4/calloc.m4 (gl_FUNC_CALLOC_POSIX): New macro.
+       * doc/functions/calloc.texi: Mention the calloc-posix module.
+       * lib/stdlib_.h (calloc): New declaration.
+       * m4/stdlib_h.m4 (gl_STDLIB_H_DEFAULTS): Initialize
+       GNULIB_CALLOC_POSIX and HAVE_CALLOC_POSIX.
+       * modules/stdlib (stdlib.h): Substitute also GNULIB_CALLOC_POSIX
+       and HAVE_CALLOC_POSIX.
+
+2007-09-09  Bruno Haible  <bruno@clisp.org>
+
        Allow for modules to show an arbitrary notice.
        * modules/TEMPLATE-EXTENDED: Add 'Notice' field.
        * gnulib-tool: New option --extract-notice.
index 9c3ca79..7c04faa 100644 (file)
@@ -4,10 +4,14 @@
 
 POSIX specification: @url{http://www.opengroup.org/susv3xsh/calloc.html}
 
-Gnulib module: ---
+Gnulib module: calloc-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 c8b1e1e..0b92d2f 100644 (file)
@@ -1,6 +1,6 @@
 /* calloc() function that is glibc compatible.
-   This wrapper function is required at least on Tru64 UNIX 5.1.
-   Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
+   This wrapper function is required at least on Tru64 UNIX 5.1 and mingw.
+   Copyright (C) 2004, 2005, 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 calloc
+/* Only the AC_FUNC_CALLOC macro defines 'calloc' already in config.h.  */
+#ifdef calloc
+# define NEED_CALLOC_GNU
+# undef calloc
+#endif
 
+/* Specification.  */
 #include <stdlib.h>
 
+#include <errno.h>
+
 /* Allocate and zero-fill an NxS-byte block of memory from the heap.
    If N or S is zero, allocate and zero-fill a 1-byte block.  */
 
 void *
 rpl_calloc (size_t n, size_t s)
 {
-  size_t bytes;
+  void *result;
 
+#ifdef NEED_CALLOC_GNU
   if (n == 0 || s == 0)
-    return calloc (1, 1);
+    {
+      n = 1;
+      s = 1;
+    }
+  else
+    {
+      /* Defend against buggy calloc implementations that mishandle
+        size_t overflow.  */
+      size_t bytes = n * s;
+      if (bytes / s != n)
+       {
+         errno = ENOMEM;
+         return NULL;
+       }
+    }
+#endif
+
+  result = calloc (n, s);
 
-  /* Defend against buggy calloc implementations that mishandle
-     size_t overflow.  */
-  bytes = n * s;
-  if (bytes / s != n)
-    return NULL;
+#if !HAVE_CALLOC_POSIX
+  if (result == NULL)
+    errno = ENOMEM;
+#endif
 
-  return calloc (n, s);
+  return result;
 }
index d1e254a..adff158 100644 (file)
@@ -55,6 +55,21 @@ extern "C" {
 #endif
 
 
+#if @GNULIB_CALLOC_POSIX@
+# if !@HAVE_CALLOC_POSIX@
+#  undef calloc
+#  define calloc rpl_calloc
+extern void * calloc (size_t nmemb, size_t size);
+# endif
+#elif defined GNULIB_POSIXCHECK
+# undef calloc
+# define calloc(n,s) \
+    (GL_LINK_WARNING ("calloc is not POSIX compliant everywhere - " \
+                      "use gnulib module calloc-posix for portability"), \
+     calloc (n, s))
+#endif
+
+
 #if @GNULIB_GETSUBOPT@
 /* Assuming *OPTIONP is a comma separated list of elements of the form
    "token" or "token=value", getsubopt parses the first of these elements.
index c109171..3432493 100644 (file)
@@ -1,6 +1,6 @@
-# calloc.m4 serial 6
+# calloc.m4 serial 7
 
-# Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
+# Copyright (C) 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
 # This file is free software; the Free Software Foundation
 # gives unlimited permission to copy and/or distribute it,
 # with or without modifications, as long as this notice is preserved.
@@ -41,3 +41,33 @@ AC_DEFUN([AC_FUNC_CALLOC],
    AC_DEFINE([calloc], [rpl_calloc],
       [Define to rpl_calloc if the replacement function should be used.])])
 ])# AC_FUNC_CALLOC
+
+
+# gl_FUNC_CALLOC_POSIX
+# --------------------
+# Test whether 'calloc' is POSIX compliant (sets errno to ENOMEM when it
+# fails), and replace calloc if it is not.
+AC_DEFUN([gl_FUNC_CALLOC_POSIX],
+[
+  AC_CACHE_CHECK([whether calloc is POSIX compliant],
+    [gl_cv_func_calloc_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_calloc_posix=yes], [gl_cv_func_calloc_posix=no])
+    ])
+  if test $gl_cv_func_calloc_posix = yes; then
+    HAVE_CALLOC_POSIX=1
+    AC_DEFINE([HAVE_CALLOC_POSIX], 1,
+      [Define if the 'calloc' function is POSIX compliant.])
+  else
+    AC_LIBOBJ([calloc])
+    HAVE_CALLOC_POSIX=0
+  fi
+  AC_SUBST([HAVE_CALLOC_POSIX])
+])
index f13b429..989f539 100644 (file)
@@ -1,4 +1,4 @@
-# stdlib_h.m4 serial 2
+# stdlib_h.m4 serial 3
 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,
@@ -19,11 +19,13 @@ AC_DEFUN([gl_STDLIB_MODULE_INDICATOR],
 
 AC_DEFUN([gl_STDLIB_H_DEFAULTS],
 [
-  GNULIB_GETSUBOPT=0; AC_SUBST([GNULIB_GETSUBOPT])
-  GNULIB_MKDTEMP=0;   AC_SUBST([GNULIB_MKDTEMP])
-  GNULIB_MKSTEMP=0;   AC_SUBST([GNULIB_MKSTEMP])
+  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_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])
+  REPLACE_MKSTEMP=0;     AC_SUBST([REPLACE_MKSTEMP])
 ])
index 2d3bfc2..52c9594 100644 (file)
@@ -6,6 +6,7 @@ lib/calloc.c
 m4/calloc.m4
 
 Depends-on:
+calloc-posix
 
 configure.ac:
 AC_FUNC_CALLOC
diff --git a/modules/calloc-posix b/modules/calloc-posix
new file mode 100644 (file)
index 0000000..49e3051
--- /dev/null
@@ -0,0 +1,25 @@
+Description:
+calloc() function: allocate memory with indefinite extent.
+
+Files:
+lib/calloc.c
+m4/calloc.m4
+
+Depends-on:
+stdlib
+
+configure.ac:
+gl_FUNC_CALLOC_POSIX
+gl_STDLIB_MODULE_INDICATOR([calloc-posix])
+
+Makefile.am:
+
+Include:
+<stdlib.h>
+
+License:
+LGPL
+
+Maintainer:
+Bruno Haible
+
index 2d4ce38..5ef9c4f 100644 (file)
@@ -23,9 +23,11 @@ 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_CALLOC_POSIX''@|$(GNULIB_CALLOC_POSIX)|g' \
              -e 's|@''GNULIB_GETSUBOPT''@|$(GNULIB_GETSUBOPT)|g' \
              -e 's|@''GNULIB_MKDTEMP''@|$(GNULIB_MKDTEMP)|g' \
              -e 's|@''GNULIB_MKSTEMP''@|$(GNULIB_MKSTEMP)|g' \
+             -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|@''REPLACE_MKSTEMP''@|$(REPLACE_MKSTEMP)|g' \