changeset 9190:a4f83e0e605c

New module 'calloc-posix'.
author Bruno Haible <bruno@clisp.org>
date Sun, 09 Sep 2007 12:29:37 +0000
parents 1d7d9694f1e2
children c7875dab3c3e
files ChangeLog doc/functions/calloc.texi lib/calloc.c lib/stdlib_.h m4/calloc.m4 m4/stdlib_h.m4 modules/calloc modules/calloc-posix modules/stdlib
diffstat 9 files changed, 143 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,19 @@
+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.
--- a/doc/functions/calloc.texi
+++ b/doc/functions/calloc.texi
@@ -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:
--- a/lib/calloc.c
+++ b/lib/calloc.c
@@ -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
@@ -16,29 +16,53 @@
    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;
-
-  if (n == 0 || s == 0)
-    return calloc (1, 1);
+  void *result;
 
-  /* Defend against buggy calloc implementations that mishandle
-     size_t overflow.  */
-  bytes = n * s;
-  if (bytes / s != n)
-    return NULL;
+#ifdef NEED_CALLOC_GNU
+  if (n == 0 || s == 0)
+    {
+      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
 
-  return calloc (n, s);
+  result = calloc (n, s);
+
+#if !HAVE_CALLOC_POSIX
+  if (result == NULL)
+    errno = ENOMEM;
+#endif
+
+  return result;
 }
--- a/lib/stdlib_.h
+++ b/lib/stdlib_.h
@@ -55,6 +55,21 @@
 #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.
--- a/m4/calloc.m4
+++ b/m4/calloc.m4
@@ -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_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])
+])
--- a/m4/stdlib_h.m4
+++ b/m4/stdlib_h.m4
@@ -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_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])
 ])
--- a/modules/calloc
+++ b/modules/calloc
@@ -6,6 +6,7 @@
 m4/calloc.m4
 
 Depends-on:
+calloc-posix
 
 configure.ac:
 AC_FUNC_CALLOC
new file mode 100644
--- /dev/null
+++ b/modules/calloc-posix
@@ -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
+
--- a/modules/stdlib
+++ b/modules/stdlib
@@ -23,9 +23,11 @@
 	{ 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' \