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

index fa26fa1..2c046ba 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,21 @@
 2007-09-09  Bruno Haible  <bruno@clisp.org>
 
+       * modules/malloc-posix: New file.
+       * modules/malloc (Depends-on): Add malloc-posix.
+       * lib/malloc.c: Include errno.h.
+       (rpl_malloc): Merge the requirements of a glibc-compatible malloc
+       and a POSIX-compatible malloc into a single function. Set ENOMEM
+       when returning NULL.
+       * m4/malloc.m4: New file.
+       * doc/functions/malloc.texi: Mention the malloc-posix module.
+       * lib/stdlib_.h (malloc): New declaration.
+       * m4/stdlib_h.m4 (gl_STDLIB_H_DEFAULTS): Initialize
+       GNULIB_MALLOC_POSIX and HAVE_MALLOC_POSIX.
+       * modules/stdlib (stdlib.h): Substitute also GNULIB_MALLOC_POSIX
+       and HAVE_MALLOC_POSIX.
+
+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.
index d13267a..0b8cd0d 100644 (file)
@@ -4,10 +4,14 @@
 
 POSIX specification: @url{http://www.opengroup.org/susv3xsh/malloc.html}
 
-Gnulib module: ---
+Gnulib module: malloc-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 d4dae3e..b6ecf37 100644 (file)
@@ -1,6 +1,6 @@
 /* malloc() function that is glibc compatible.
 
-   Copyright (C) 1997, 1998, 2006 Free Software Foundation, Inc.
+   Copyright (C) 1997, 1998, 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 malloc
+/* Only the AC_FUNC_MALLOC macro defines 'malloc' already in config.h.  */
+#ifdef malloc
+# define NEED_MALLOC_GNU
+# undef malloc
+#endif
 
+/* Specification.  */
 #include <stdlib.h>
 
+#include <errno.h>
+
 /* Allocate an N-byte block of memory from the heap.
    If N is zero, allocate a 1-byte block.  */
 
 void *
 rpl_malloc (size_t n)
 {
+  void *result;
+
+#ifdef NEED_MALLOC_GNU
   if (n == 0)
     n = 1;
-  return malloc (n);
+#endif
+
+  result = malloc (n);
+
+#if !HAVE_MALLOC_POSIX
+  if (result == NULL)
+    errno = ENOMEM;
+#endif
+
+  return result;
 }
index 72488c7..a4946e6 100644 (file)
@@ -55,6 +55,21 @@ extern "C" {
 #endif
 
 
+#if @GNULIB_MALLOC_POSIX@
+# if !@HAVE_MALLOC_POSIX@
+#  undef malloc
+#  define malloc rpl_malloc
+extern void * malloc (size_t size);
+# endif
+#elif defined GNULIB_POSIXCHECK
+# undef malloc
+# define malloc(s) \
+    (GL_LINK_WARNING ("malloc is not POSIX compliant everywhere - " \
+                      "use gnulib module malloc-posix for portability"), \
+     malloc (s))
+#endif
+
+
 #if @GNULIB_REALLOC_POSIX@
 # if !@HAVE_REALLOC_POSIX@
 #  undef realloc
diff --git a/m4/malloc.m4 b/m4/malloc.m4
new file mode 100644 (file)
index 0000000..727192c
--- /dev/null
@@ -0,0 +1,34 @@
+# malloc.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_MALLOC_POSIX
+# --------------------
+# Test whether 'malloc' is POSIX compliant (sets errno to ENOMEM when it
+# fails), and replace malloc if it is not.
+AC_DEFUN([gl_FUNC_MALLOC_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_MALLOC_POSIX=1
+    AC_DEFINE([HAVE_MALLOC_POSIX], 1,
+      [Define if the 'malloc' function is POSIX compliant.])
+  else
+    AC_LIBOBJ([malloc])
+    HAVE_MALLOC_POSIX=0
+  fi
+  AC_SUBST([HAVE_MALLOC_POSIX])
+])
index 600bd09..ea9286e 100644 (file)
@@ -19,6 +19,7 @@ AC_DEFUN([gl_STDLIB_MODULE_INDICATOR],
 
 AC_DEFUN([gl_STDLIB_H_DEFAULTS],
 [
+  GNULIB_MALLOC_POSIX=0;  AC_SUBST([GNULIB_MALLOC_POSIX])
   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])
@@ -27,6 +28,7 @@ AC_DEFUN([gl_STDLIB_H_DEFAULTS],
   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_MALLOC_POSIX=1;    AC_SUBST([HAVE_MALLOC_POSIX])
   HAVE_MKDTEMP=1;         AC_SUBST([HAVE_MKDTEMP])
   HAVE_REALLOC_POSIX=1;   AC_SUBST([HAVE_REALLOC_POSIX])
   REPLACE_MKSTEMP=0;      AC_SUBST([REPLACE_MKSTEMP])
index d3cb7ee..e571d3b 100644 (file)
@@ -5,6 +5,7 @@ Files:
 lib/malloc.c
 
 Depends-on:
+malloc-posix
 
 configure.ac:
 AC_FUNC_MALLOC
diff --git a/modules/malloc-posix b/modules/malloc-posix
new file mode 100644 (file)
index 0000000..17f4513
--- /dev/null
@@ -0,0 +1,25 @@
+Description:
+malloc() function: allocate memory with indefinite extent.
+
+Files:
+lib/malloc.c
+m4/malloc.m4
+
+Depends-on:
+stdlib
+
+configure.ac:
+gl_FUNC_MALLOC_POSIX
+gl_STDLIB_MODULE_INDICATOR([malloc-posix])
+
+Makefile.am:
+
+Include:
+<stdlib.h>
+
+License:
+LGPL
+
+Maintainer:
+Bruno Haible
+
index 8a1839a..5a68117 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_MALLOC_POSIX''@|$(GNULIB_MALLOC_POSIX)|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' \
@@ -30,6 +31,7 @@ stdlib.h: stdlib_.h
              -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_MALLOC_POSIX''@|$(HAVE_MALLOC_POSIX)|g' \
              -e 's|@''HAVE_MKDTEMP''@|$(HAVE_MKDTEMP)|g' \
              -e 's|@''HAVE_REALLOC_POSIX''@|$(HAVE_REALLOC_POSIX)|g' \
              -e 's|@''REPLACE_MKSTEMP''@|$(REPLACE_MKSTEMP)|g' \