bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Bug-gnulib] removed dependency of (xalloc, hash) on (malloc, realloc)


From: Paul Eggert
Subject: [Bug-gnulib] removed dependency of (xalloc, hash) on (malloc, realloc)
Date: Mon, 31 May 2004 21:04:00 -0700
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux)

The gnulib module 'hash' depends on the 'malloc' and 'realloc'
modules, but there's no reason for this that I can see.

Also, the 'xalloc' module depends on the 'malloc' and 'realloc'
modules, and this has been a hassle in practice.  I recently discussed
this with Jim Meyering and we decided to rewrite xalloc so that it
doesn't rely on malloc(0) returning nonnull on success.  This solves
the problem that Bruno Haible originally mentioned in
<http://lists.gnu.org/archive/html/bug-gnulib/2003-01/msg00040.html>.

I see the following potential downsides of this approach:

 * Programs might now free (NULL) that otherwise wouldn't have, and they
   might not be portable to ancient systems where free (NULL) doesn't work.
   However, the 'free' module addresses this issue.

 * Programs might be more likely to add zero to the null pointer, which
   the C standard does not guarantee equals the null pointer.  However,
   gnulib/README already addresses this iseue.

 * Bruno's eealloc module becomes a bit of an orphan, as it was
   intended to be used by xalloc to address this problem.  But I don't
   think he'll mind, as my understanding is that his main goal was to
   remove the dependency of xalloc on malloc.  (If I'm wrong, Bruno,
   please let us know.)

Anyway, I installed this:

Index: ChangeLog
===================================================================
RCS file: /cvsroot/gnulib/gnulib/ChangeLog,v
retrieving revision 1.152
diff -p -u -r1.152 ChangeLog
--- ChangeLog   30 May 2004 07:29:34 -0000      1.152
+++ ChangeLog   1 Jun 2004 03:44:57 -0000
@@ -1,5 +1,8 @@
 2004-05-30  Paul Eggert  <address@hidden>
 
+       * modules/hash (Depends-on): Remove malloc, realloc.
+       * modules/xalloc (Depends-on): Likewise.
+
        * README: Mention that the 'free' module works around the
        problem with 'free (0)'.
        Mention LIA-1 and C99.
Index: lib/ChangeLog
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/ChangeLog,v
retrieving revision 1.666
diff -p -u -r1.666 ChangeLog
--- lib/ChangeLog       21 May 2004 04:51:32 -0000      1.666
+++ lib/ChangeLog       1 Jun 2004 03:44:58 -0000
@@ -1,3 +1,10 @@
+2004-05-30  Paul Eggert  <address@hidden>
+
+       * xmalloc.c (HAVE_MALLOC, HAVE_REALLOC): Do not require these
+       macros to be defined.
+       (xnmalloc_inline, xnrealloc_inline, xcalloc): Do not die if
+       the allocator returns NULL because the requested size is zero.
+
 2004-05-20  Paul Eggert  <address@hidden>
 
        * malloc/obstack.c (_obstack) [defined _LIBC]: Bring back this
Index: lib/xmalloc.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/xmalloc.c,v
retrieving revision 1.32
diff -p -u -r1.32 xmalloc.c
--- lib/xmalloc.c       22 Nov 2003 15:07:36 -0000      1.32
+++ lib/xmalloc.c       1 Jun 2004 03:44:58 -0000
@@ -1,7 +1,7 @@
 /* xmalloc.c -- malloc with out of memory checking
 
    Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2003,
-   1999, 2000, 2002, 2003 Free Software Foundation, Inc.
+   1999, 2000, 2002, 2003, 2004 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
@@ -37,14 +37,6 @@
 # define SIZE_MAX ((size_t) -1)
 #endif
 
-#ifndef HAVE_MALLOC
-"you must run the autoconf test for a GNU libc compatible malloc"
-#endif
-
-#ifndef HAVE_REALLOC
-"you must run the autoconf test for a GNU libc compatible realloc"
-#endif
-
 /* If non NULL, call this function when memory is exhausted. */
 void (*xalloc_fail_func) (void) = 0;
 
@@ -71,7 +63,7 @@ static inline void *
 xnmalloc_inline (size_t n, size_t s)
 {
   void *p;
-  if (xalloc_oversized (n, s) || ! (p = malloc (n * s)))
+  if (xalloc_oversized (n, s) || (! (p = malloc (n * s)) && n != 0))
     xalloc_die ();
   return p;
 }
@@ -96,7 +88,7 @@ xmalloc (size_t n)
 static inline void *
 xnrealloc_inline (void *p, size_t n, size_t s)
 {
-  if (xalloc_oversized (n, s) || ! (p = realloc (p, n * s)))
+  if (xalloc_oversized (n, s) || (! (p = realloc (p, n * s)) && n != 0))
     xalloc_die ();
   return p;
 }
@@ -239,7 +231,7 @@ xcalloc (size_t n, size_t s)
   void *p;
   /* Test for overflow, since some calloc implementations don't have
      proper overflow checks.  */
-  if (xalloc_oversized (n, s) || ! (p = calloc (n, s)))
+  if (xalloc_oversized (n, s) || (! (p = calloc (n, s)) && n != 0))
     xalloc_die ();
   return p;
 }
Index: m4/ChangeLog
===================================================================
RCS file: /cvsroot/gnulib/gnulib/m4/ChangeLog,v
retrieving revision 1.555
diff -p -u -r1.555 ChangeLog
--- m4/ChangeLog        21 May 2004 06:09:22 -0000      1.555
+++ m4/ChangeLog        1 Jun 2004 03:44:59 -0000
@@ -1,3 +1,8 @@
+2004-05-30  Paul Eggert  <address@hidden>
+
+       * xalloc.m4 (gl_PREREQ_XMALLOC): Do not require AC_FUNC_MALLOC
+       or AC_FUNC_REALLOC.
+
 2004-05-20  Andreas Schwab  <address@hidden>
 
        * free.m4: Replace free if it not known to work, not the other
Index: m4/xalloc.m4
===================================================================
RCS file: /cvsroot/gnulib/gnulib/m4/xalloc.m4,v
retrieving revision 1.10
diff -p -u -r1.10 xalloc.m4
--- m4/xalloc.m4        31 Mar 2004 07:40:53 -0000      1.10
+++ m4/xalloc.m4        1 Jun 2004 03:44:59 -0000
@@ -1,4 +1,4 @@
-# xalloc.m4 serial 8
+# xalloc.m4 serial 9
 dnl Copyright (C) 2002-2004 Free Software Foundation, Inc.
 dnl This file is free software, distributed under the terms of the GNU
 dnl General Public License.  As a special exception to the GNU General
@@ -21,8 +21,6 @@ AC_DEFUN([gl_PREREQ_XALLOC], [
 # Prerequisites of lib/xmalloc.c.
 AC_DEFUN([gl_PREREQ_XMALLOC], [
   AC_REQUIRE([AC_C_INLINE])
-  AC_REQUIRE([AC_FUNC_MALLOC])
-  AC_REQUIRE([AC_FUNC_REALLOC])
   :
 ])
 
Index: modules/hash
===================================================================
RCS file: /cvsroot/gnulib/gnulib/modules/hash,v
retrieving revision 1.3
diff -p -u -r1.3 hash
--- modules/hash        20 Jan 2003 10:02:37 -0000      1.3
+++ modules/hash        1 Jun 2004 03:44:59 -0000
@@ -7,8 +7,6 @@ lib/hash.c
 m4/hash.m4
 
 Depends-on:
-malloc
-realloc
 stdbool
 
 configure.ac:
Index: modules/xalloc
===================================================================
RCS file: /cvsroot/gnulib/gnulib/modules/xalloc,v
retrieving revision 1.10
diff -p -u -r1.10 xalloc
--- modules/xalloc      22 Feb 2004 14:09:56 -0000      1.10
+++ modules/xalloc      1 Jun 2004 03:44:59 -0000
@@ -8,8 +8,6 @@ lib/xstrdup.c
 m4/xalloc.m4
 
 Depends-on:
-malloc
-realloc
 error
 gettext
 exitfail




reply via email to

[Prev in Thread] Current Thread [Next in Thread]