bug-gnulib
[Top][All Lists]
Advanced

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

[Bug-gnulib] xmalloc and rpl_malloc


From: Bruno Haible
Subject: [Bug-gnulib] xmalloc and rpl_malloc
Date: Mon, 13 Jan 2003 20:50:50 +0100 (CET)

Hi,

Now this is probably a controversial proposal. GNU gettext has not been
able to use gnulib's xalloc module because the latter depends on the modules
called malloc and realloc. This is a problem for gettext, because if on
some platforms gettext's config.h contains "#define malloc rpl_malloc",
the resulting libintl.a and libintl.so won't be self-contained any more.
libintl must be built without such #defines, inside GNU gettext.

Also I generally argue that the modules malloc and realloc shouldn't be
used: they add a performance penalty to programs for the sake of using a
nonstandard calling convention for two standard functions. Better fix the
programs.

So I propose to remove the dependencies from xalloc to malloc and realloc.
Of course, packages that want to continue using malloc and realloc with
the conventions that were never standard can do so; just mention them
explicitly when you call gnulib-tool. Also note that the behaviour of
xmalloc() and xrealloc() is unchanged.


2003-01-12  Bruno Haible  <address@hidden>

        * m4/malloc.m4 (jm_FUNC_MALLOC): Define macro MALLOC_0_IS_NONNULL
        to 1 or -1.
        * m4/realloc.m4 (jm_FUNC_REALLOC): Define macro
        REALLOC_NULL_0_IS_NONNULL to 1 or -1.
        * lib/xmalloc.c: Use standard include file stdlib.h.
        (xmalloc, xrealloc, xcalloc): Make dependency on jm_FUNC_MALLOC and
        jm_FUNC_REALLOC optional. Call original malloc() and realloc()
        functions, with a fixup a posteriori.
        * modules/xalloc: Don't depend on malloc and realloc.

diff -r -c3 gnulib-cvs/m4/malloc.m4 gnulib/m4/malloc.m4
*** gnulib-cvs/m4/malloc.m4     2002-12-31 14:34:38.000000000 +0100
--- gnulib/m4/malloc.m4 2003-01-12 20:06:50.000000000 +0100
***************
*** 1,5 ****
! # malloc.m4 serial 7
! dnl Copyright (C) 2002 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
  dnl Public License, this file may be distributed as part of a program
--- 1,5 ----
! # malloc.m4 serial 8
! dnl Copyright (C) 2002-2003 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
  dnl Public License, this file may be distributed as part of a program
***************
*** 16,22 ****
--- 16,29 ----
    dnl autoconf < 2.57 used the symbol ac_cv_func_malloc_works.
    if test X"$ac_cv_func_malloc_0_nonnull" = Xno || test 
X"$ac_cv_func_malloc_works" = Xno; then
      gl_PREREQ_MALLOC
+     jm_val=-1
+   else
+     jm_val=1
    fi
+   AC_DEFINE_UNQUOTED([MALLOC_0_IS_NONNULL], $jm_val,
+     [If malloc(0) is != NULL, define this to 1.  Otherwise, if malloc
+      is being aliased to rpl_malloc, define this to -1, otherwise define this
+      to 0 or leave it undefined.])
  ])
  
  # Prerequisites of lib/malloc.c.
diff -r -c3 gnulib-cvs/m4/realloc.m4 gnulib/m4/realloc.m4
*** gnulib-cvs/m4/realloc.m4    2002-12-31 14:34:38.000000000 +0100
--- gnulib/m4/realloc.m4        2003-01-12 20:06:50.000000000 +0100
***************
*** 1,5 ****
! # realloc.m4 serial 7
! dnl Copyright (C) 2002 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
  dnl Public License, this file may be distributed as part of a program
--- 1,5 ----
! # realloc.m4 serial 8
! dnl Copyright (C) 2002-2003 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
  dnl Public License, this file may be distributed as part of a program
***************
*** 16,22 ****
--- 16,29 ----
    dnl autoconf < 2.57 used the symbol ac_cv_func_realloc_works.
    if test X"$ac_cv_func_realloc_0_nonnull" = Xno || test 
X"$ac_cv_func_realloc_works" = Xno; then
      gl_PREREQ_REALLOC
+     jm_val=-1
+   else
+     jm_val=1
    fi
+   AC_DEFINE_UNQUOTED([REALLOC_NULL_0_IS_NONNULL], $jm_val,
+     [If realloc(NULL,0) is != NULL, define this to 1.  Otherwise, if realloc
+      is being aliased to rpl_realloc, define this to -1, otherwise define this
+      to 0 or leave it undefined.])
  ])
  
  # Prerequisites of lib/realloc.c.
diff -r -c3 gnulib-cvs/lib/xmalloc.c gnulib/lib/xmalloc.c
*** gnulib-cvs/lib/xmalloc.c    2002-11-21 20:13:34.000000000 +0100
--- gnulib/lib/xmalloc.c        2003-01-12 21:34:00.000000000 +0100
***************
*** 1,5 ****
  /* xmalloc.c -- malloc with out of memory checking
!    Copyright (C) 1990-1999, 2000, 2002 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
--- 1,5 ----
  /* xmalloc.c -- malloc with out of memory checking
!    Copyright (C) 1990-1999, 2000, 2002-2003 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
***************
*** 19,54 ****
  # include <config.h>
  #endif
  
! #include <sys/types.h>
  
! #if STDC_HEADERS
! # include <stdlib.h>
! #else
! void *calloc ();
! void *malloc ();
! void *realloc ();
! void free ();
! #endif
  
  #include "gettext.h"
  #define _(msgid) gettext (msgid)
  #define N_(msgid) msgid
  
  #include "error.h"
- #include "xalloc.h"
  
  #ifndef EXIT_FAILURE
  # define EXIT_FAILURE 1
  #endif
  
! /* The following tests require AC_PREREQ(2.54).  */
! 
! #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
  
  /* Exit value when the requested amount of memory is not available.
--- 19,50 ----
  # include <config.h>
  #endif
  
! /* Specification.  */
! #include "xalloc.h"
  
! #include <stdlib.h>
  
  #include "gettext.h"
  #define _(msgid) gettext (msgid)
  #define N_(msgid) msgid
  
  #include "error.h"
  
  #ifndef EXIT_FAILURE
  # define EXIT_FAILURE 1
  #endif
  
! /* When malloc is aliased to rpl_malloc, we want to call malloc directly here;
!    this saves a function call.  Similarly for realloc and rpl_realloc.
!    Note that these uses of MALLOC_0_IS_NONNULL and REALLOC_NULL_0_IS_NONNULL
!    don't require jm_FUNC_MALLOC and jm_FUNC_REALLOC to be invoked; if they
!    aren't, MALLOC_0_IS_NONNULL and REALLOC_NULL_0_IS_NONNULL will evaluate
!    to 0, which is perfectly fine.  */
! #if MALLOC_0_IS_NONNULL < 0
! # undef malloc
  #endif
! #if REALLOC_NULL_0_IS_NONNULL < 0
! # undef realloc
  #endif
  
  /* Exit value when the requested amount of memory is not available.
***************
*** 82,89 ****
    void *p;
  
    p = malloc (n);
!   if (p == 0)
!     xalloc_die ();
    return p;
  }
  
--- 78,97 ----
    void *p;
  
    p = malloc (n);
!   if (p == NULL)
!     {
!       /* Handle the case n == 0 specially.  */
! #if MALLOC_0_IS_NONNULL <= 0
!       if (n == 0)
!       {
!         p = malloc (1);
!         if (p == NULL)
!           xalloc_die ();
!       }
!       else
! #endif
!       xalloc_die ();
!     }
    return p;
  }
  
***************
*** 94,101 ****
  xrealloc (void *p, size_t n)
  {
    p = realloc (p, n);
!   if (p == 0)
!     xalloc_die ();
    return p;
  }
  
--- 102,121 ----
  xrealloc (void *p, size_t n)
  {
    p = realloc (p, n);
!   if (p == NULL)
!     {
!       /* Handle the case n == 0 specially.  */
! #if REALLOC_NULL_0_IS_NONNULL <= 0
!       if (n == 0)
!       {
!         p = malloc (1);
!         if (p == NULL)
!           xalloc_die ();
!       }
!       else
! #endif
!       xalloc_die ();
!     }
    return p;
  }
  
***************
*** 107,113 ****
    void *p;
  
    p = calloc (n, s);
!   if (p == 0)
!     xalloc_die ();
    return p;
  }
--- 127,145 ----
    void *p;
  
    p = calloc (n, s);
!   if (p == NULL)
!     {
!       /* Handle the case n * s == 0 specially.  */
! #if MALLOC_0_IS_NONNULL <= 0
!       if (n == 0 || s == 0)
!       {
!         p = malloc (1);
!         if (p == NULL)
!           xalloc_die ();
!       }
!       else
! #endif
!       xalloc_die ();
!     }
    return p;
  }
diff -c3 gnulib-cvs/modules/xalloc gnulib/modules/xalloc
*** gnulib-cvs/modules/xalloc   2003-01-12 00:29:32.000000000 +0100
--- gnulib/modules/xalloc       2003-01-12 21:37:22.000000000 +0100
***************
*** 8,15 ****
  m4/xalloc.m4
  
  Depends-on:
- malloc
- realloc
  error
  gettext
  
--- 8,13 ----




reply via email to

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