bug-gnulib
[Top][All Lists]
Advanced

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

[Bug-gnulib] remove dependency of xalloc on gettext, error, etc.


From: Paul Eggert
Subject: [Bug-gnulib] remove dependency of xalloc on gettext, error, etc.
Date: Mon, 09 Aug 2004 00:43:12 -0700
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux)

Simon Josefsson <address@hidden> writes:

> Like a xmalloc.c that does puts(strerror (ENOMEM)) instead of
> requiring error + gettext, both of which are huge and complex.

How about this patch instead?  It removes the dependency of xalloc on
those other modules in a different way: by having the app specify
xalloc_die, and by supplying a different module xalloc-die for apps
that want the current behavior.

Also, it removes external access to xalloc_msg_memory_exhausted (which
I think only userspec needs -- easily fixed, as shown below) and it
removes the ability to have apps dynamically specify
xalloc_fail_function (which I don't think anybody's using, really).

This proposal will require changes to nearly every app using xalloc:
most will need to use the xalloc-die module as well.  For coreutils,
this patch requires about 5 lines of changes to src/csplit.c.c, and
adding xalloc-die.c to lib/Makefile.am; that's a pretty small change,
which is a good sign.  I'd guess other packages would be similar.

2004-08-09  Paul Eggert  <address@hidden>

        * modules/xalloc-die: New file.
        * modules/xalloc: Remove dependencies on error, gettext, exitfail.
        * lib/xalloc-die.c: New files.
        * lib/xalloc.h (xalloc_fail_func, xalloc_msg_memory_exhausted): Remove.
        All uses removed.
        * lib/xmalloc.c (xalloc_fail_func, xalloc_msg_memory_exhausted):
        Likewise.
        Move inclusions of gettext.h, error.h, exitfail.h to xalloc-die.c.
        (_, N_, xalloc_die): Move to xalloc-die.c.
        * lib/userspec.c (parse_user_spaec): Use xstrdup rather than strdup,
        so that we needn't mess with xalloc_msg_memory_exhausted.

Index: modules/xalloc
===================================================================
RCS file: /cvsroot/gnulib/gnulib/modules/xalloc,v
retrieving revision 1.11
diff -p -u -r1.11 xalloc
--- modules/xalloc      1 Jun 2004 03:49:49 -0000       1.11
+++ modules/xalloc      9 Aug 2004 07:31:24 -0000
@@ -1,5 +1,5 @@
 Description:
-Memory allocation with out-of-memory checking.
+Memory allocation with out-of-memory checking.  Also see xalloc-die.
 
 Files:
 lib/xalloc.h
@@ -8,9 +8,6 @@ lib/xstrdup.c
 m4/xalloc.m4
 
 Depends-on:
-error
-gettext
-exitfail
 
 configure.ac:
 gl_XALLOC
--- /dev/null   2003-03-18 13:55:57 -0800
+++ modules/xalloc-die  2004-08-09 00:25:35 -0700
@@ -0,0 +1,23 @@
+Description:
+Report a memory allocation failure and exit.
+
+Files:
+lib/xalloc-die.c
+
+Depends-on:
+xalloc
+error
+gettext
+exitfail
+
+configure.ac:
+
+Makefile.am:
+lib_SOURCES += xalloc-die.c
+
+Include:
+"xalloc.h"
+
+Maintainer:
+all
+
Index: lib/xalloc.h
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/xalloc.h,v
retrieving revision 1.25
diff -p -u -r1.25 xalloc.h
--- lib/xalloc.h        16 Jul 2004 16:28:52 -0000      1.25
+++ lib/xalloc.h        9 Aug 2004 07:31:24 -0000
@@ -38,19 +38,9 @@ extern "C" {
 #  define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
 # endif
 
-
-/* If this pointer is non-zero, run the specified function upon each
-   allocation failure.  It is initialized to zero. */
-extern void (*xalloc_fail_func) (void);
-
-/* If XALLOC_FAIL_FUNC is undefined or a function that returns, this
-   message is output.  It is translated via gettext.
-   Its value is "memory exhausted".  */
-extern char const xalloc_msg_memory_exhausted[];
-
-/* This function is always triggered when memory is exhausted.  It is
-   in charge of honoring the two previous items.  It exits with status
-   exit_failure (defined in exitfail.h).  This is the
+/* This function is always triggered when memory is exhausted.
+   It must be defined by the application, either explicitly
+   or by using gnulib's xalloc-die module.  This is the
    function to call when one wants the program to die because of a
    memory allocation failure.  */
 extern void xalloc_die (void) ATTRIBUTE_NORETURN;
Index: lib/xmalloc.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/xmalloc.c,v
retrieving revision 1.33
diff -p -u -r1.33 xmalloc.c
--- lib/xmalloc.c       1 Jun 2004 03:49:47 -0000       1.33
+++ lib/xmalloc.c       9 Aug 2004 07:31:24 -0000
@@ -1,6 +1,6 @@
 /* xmalloc.c -- malloc with out of memory checking
 
-   Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2003,
+   Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
    1999, 2000, 2002, 2003, 2004 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
@@ -26,36 +26,10 @@
 #include <stdlib.h>
 #include <string.h>
 
-#include "gettext.h"
-#define _(msgid) gettext (msgid)
-#define N_(msgid) msgid
-
-#include "error.h"
-#include "exitfail.h"
-
 #ifndef SIZE_MAX
 # define SIZE_MAX ((size_t) -1)
 #endif
 
-/* If non NULL, call this function when memory is exhausted. */
-void (*xalloc_fail_func) (void) = 0;
-
-/* If XALLOC_FAIL_FUNC is NULL, or does return, display this message
-   before exiting when memory is exhausted.  Goes through gettext. */
-char const xalloc_msg_memory_exhausted[] = N_("memory exhausted");
-
-void
-xalloc_die (void)
-{
-  if (xalloc_fail_func)
-    (*xalloc_fail_func) ();
-  error (exit_failure, 0, "%s", _(xalloc_msg_memory_exhausted));
-  /* The `noreturn' cannot be given to error, since it may return if
-     its first argument is 0.  To help compilers understand the
-     xalloc_die does terminate, call abort.  */
-  abort ();
-}
-
 /* Allocate an array of N objects, each with S bytes of memory,
    dynamically, with error checking.  S must be nonzero.  */
 
--- /dev/null   2003-03-18 13:55:57 -0800
+++ lib/xalloc-die.c    2004-08-09 00:22:52 -0700
@@ -0,0 +1,45 @@
+/* Report a memory allocation failure and exit.
+
+   Copyright (C) 1997, 1998, 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
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software Foundation,
+   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include "xalloc.h"
+
+#include <stdlib.h>
+
+#include "error.h"
+#include "exitfail.h"
+
+#include "gettext.h"
+#define _(msgid) gettext (msgid)
+#define N_(msgid) msgid
+
+void
+xalloc_die (void)
+{
+  error (exit_failure, 0, "%s", _("memory exhausted"));
+
+  /* The `noreturn' cannot be given to error, since it may return if
+     its first argument is 0.  To help compilers understand the
+     xalloc_die does not return, call abort.  Also, the abort is a
+     safety feature if exit_failure is 0 (which shouldn't happen).  */
+  abort ();
+}
Index: lib/userspec.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/userspec.c,v
retrieving revision 1.42
diff -p -u -r1.42 userspec.c
--- lib/userspec.c      7 Aug 2004 00:09:39 -0000       1.42
+++ lib/userspec.c      9 Aug 2004 07:31:24 -0000
@@ -277,25 +277,10 @@ parse_user_spec (const char *spec_arg, u
   if (error_msg == NULL)
     {
       if (u != NULL)
-       {
-         *username_arg = strdup (u);
-         if (*username_arg == NULL)
-           error_msg = xalloc_msg_memory_exhausted;
-       }
-
-      if (groupname != NULL && error_msg == NULL)
-       {
-         *groupname_arg = strdup (groupname);
-         if (*groupname_arg == NULL)
-           {
-             if (*username_arg != NULL)
-               {
-                 free (*username_arg);
-                 *username_arg = NULL;
-               }
-             error_msg = xalloc_msg_memory_exhausted;
-           }
-       }
+       *username_arg = xstrdup (u);
+
+      if (groupname != NULL)
+       *groupname_arg = xstrdup (groupname);
     }
 
   if (error_msg && dot)




reply via email to

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