bug-gnulib
[Top][All Lists]
Advanced

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

Re: [Bug-gnulib] extended xmalloc.c


From: Paul Eggert
Subject: Re: [Bug-gnulib] extended xmalloc.c
Date: 29 Sep 2003 14:19:06 -0700
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3

Dave Love <address@hidden> writes:

> It's moot if gnulib won't accept things Emacs needs.

Quite true.  I was misled by the comment "Emacs doesn't actually use
this file" in the proposed patch, which suggested that that particular
patch isn't needed by Emacs.  But if we want Emacs to use more gnulib
code (which I think is desirable, and I think is the direction you're
headed), then we need to worry about this stuff.

>From a gnulib point of view, though, it wouldn't be adequate to change
only xmalloc.c.  The change should be made to all code that uses
malloc, realloc, calloc, or free, either directly or indirectly.

So, how about the patch enclosed below instead?  It attacks the
problem systematically, by making the change at the
malloc/free/etc. level.  This change shouldn't affect current
consumers

2003-09-29  Paul Eggert  <address@hidden>

        Add support for Emacs-style signal-blocking around malloc/free/etc.
        * lib/free.c (BLOCK_INPUT, UNBLOCK_INPUT): Define either by
        including "blockinput.h" (if defined DO_BLOCK_INPUT), or to
        the empty macro otherwise.
        (rpl_free): Use them.
        * lib/malloc.c (rpl_malloc): Likewise.
        * lib/realloc.c (rpl_realloc): Likewise.
        * modules/calloc, lib/calloc.c, m4/calloc.m4: New files.

Index: lib/free.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/free.c,v
retrieving revision 1.1
diff -p -u -r1.1 free.c
--- lib/free.c  27 Sep 2003 16:13:48 -0000      1.1
+++ lib/free.c  29 Sep 2003 21:16:05 -0000
@@ -1,4 +1,4 @@
-/* Work around incompatibility on older systems where free (NULL) fails.
+/* free wrapper.
 
    Copyright (C) 2003 Free Software Foundation, Inc.
 
@@ -25,9 +25,23 @@
 
 #include <stdlib.h>
 
+/* Programs can define BLOCK_INPUT and UNBLOCK_INPUT in blockinput.h
+   to enter and exit critical sections around potentially
+   non-reentrant code like malloc.  */
+#ifdef DO_BLOCK_INPUT
+# include "blockinput.h"
+#else
+# define BLOCK_INPUT
+# define UNBLOCK_INPUT
+#endif
+
 void
 rpl_free (void *p)
 {
   if (p)
-    free (p);
+    {
+      BLOCK_INPUT;
+      free (p);
+      UNBLOCK_INPUT;
+    }
 }
Index: lib/malloc.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/malloc.c,v
retrieving revision 1.6
diff -p -u -r1.6 malloc.c
--- lib/malloc.c        9 Sep 2003 21:56:21 -0000       1.6
+++ lib/malloc.c        29 Sep 2003 21:16:05 -0000
@@ -1,5 +1,6 @@
-/* Work around bug on some systems where malloc (0) fails.
-   Copyright (C) 1997, 1998 Free Software Foundation, Inc.
+/* malloc wrapper.
+
+   Copyright (C) 1997, 1998, 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
@@ -24,13 +25,28 @@
 
 #include <stdlib.h>
 
+/* Programs can define BLOCK_INPUT and UNBLOCK_INPUT in blockinput.h
+   to enter and exit critical sections around potentially
+   non-reentrant code like malloc.  */
+#ifdef DO_BLOCK_INPUT
+# include "blockinput.h"
+#else
+# define BLOCK_INPUT
+# define UNBLOCK_INPUT
+#endif
+
 /* 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 *p;
   if (n == 0)
     n = 1;
-  return malloc (n);
+
+  BLOCK_INPUT;
+  p = malloc (n);
+  UNBLOCK_INPUT;
+  return p;
 }
Index: lib/realloc.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/realloc.c,v
retrieving revision 1.9
diff -p -u -r1.9 realloc.c
--- lib/realloc.c       9 Sep 2003 21:56:21 -0000       1.9
+++ lib/realloc.c       29 Sep 2003 21:16:05 -0000
@@ -1,4 +1,5 @@
-/* Work around bug on some systems where realloc (NULL, 0) fails.
+/* realloc wrapper.
+
    Copyright (C) 1997, 2003 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
@@ -24,6 +25,16 @@
 
 #include <stdlib.h>
 
+/* Programs can define BLOCK_INPUT and UNBLOCK_INPUT in blockinput.h
+   to enter and exit critical sections around potentially
+   non-reentrant code.  */
+#ifdef DO_BLOCK_INPUT
+# include "blockinput.h"
+#else
+# define BLOCK_INPUT
+# define UNBLOCK_INPUT
+#endif
+
 /* Change the size of an allocated block of memory P to N bytes,
    with error checking.  If N is zero, change it to 1.  If P is NULL,
    use malloc.  */
@@ -33,7 +44,8 @@ rpl_realloc (void *p, size_t n)
 {
   if (n == 0)
     n = 1;
-  if (p == 0)
-    return malloc (n);
-  return realloc (p, n);
+  BLOCK_INPUT;
+  p = (p ? realloc (p, n) : malloc (n));
+  UNBLOCK_INPUT;
+  return p;
 }
Index: modules/free
===================================================================
RCS file: /cvsroot/gnulib/gnulib/modules/free,v
retrieving revision 1.1
diff -p -u -r1.1 free
--- modules/free        27 Sep 2003 16:13:49 -0000      1.1
+++ modules/free        29 Sep 2003 21:16:05 -0000
@@ -1,5 +1,5 @@
 Description:
-Work around incompatibility on older systems where free (NULL) fails.
+free wrapper.
 
 Files:
 lib/free.c
Index: modules/malloc
===================================================================
RCS file: /cvsroot/gnulib/gnulib/modules/malloc,v
retrieving revision 1.3
diff -p -u -r1.3 malloc
--- modules/malloc      20 Jan 2003 10:02:38 -0000      1.3
+++ modules/malloc      29 Sep 2003 21:16:05 -0000
@@ -1,5 +1,5 @@
 Description:
-malloc() function that is glibc compatible.
+malloc wrapper
 
 Files:
 lib/malloc.c
Index: modules/realloc
===================================================================
RCS file: /cvsroot/gnulib/gnulib/modules/realloc,v
retrieving revision 1.3
diff -p -u -r1.3 realloc
--- modules/realloc     20 Jan 2003 10:02:38 -0000      1.3
+++ modules/realloc     29 Sep 2003 21:16:05 -0000
@@ -1,5 +1,5 @@
 Description:
-realloc() function that is glibc compatible.
+realloc wrapper
 
 Files:
 lib/realloc.c
--- /dev/null   Tue Mar 18 13:55:57 2003
+++ modules/calloc      Mon Sep 29 14:17:17 2003
@@ -0,0 +1,20 @@
+Description:
+calloc wrapper
+
+Files:
+lib/calloc.c
+m4/calloc.m4
+
+Depends-on:
+
+configure.ac:
+gl_FUNC_CALLOC
+
+Makefile.am:
+
+Include:
+<stdlib.h>
+
+Maintainer:
+Paul Eggert
+
--- /dev/null   Tue Mar 18 13:55:57 2003
+++ lib/calloc.c        Mon Sep 29 14:06:18 2003
@@ -0,0 +1,58 @@
+/* calloc wrapper.
+
+   Copyright (C) 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
+   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.  */
+
+/* written by Paul Eggert */
+
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
+#undef calloc
+
+#include <stdlib.h>
+
+/* Programs can define BLOCK_INPUT and UNBLOCK_INPUT in blockinput.h
+   to enter and exit critical sections around potentially
+   non-reentrant code.  */
+#ifdef DO_BLOCK_INPUT
+# include "blockinput.h"
+#else
+# define BLOCK_INPUT
+# define UNBLOCK_INPUT
+#endif
+
+#ifndef SIZE_MAX
+# define SIZE_MAX ((size_t) -1)
+#endif
+
+void
+rpl_calloc (size_t n, size_t s)
+{
+  if (n == 0 || s == 0)
+    n = s = 1;
+
+  if (SIZE_MAX / s < n)
+    return NULL;
+  else
+    {
+      void *p;
+      BLOCK_INPUT;
+      p = calloc (n, s);
+      UNBLOCK_INPUT;
+      return p;
+    }
+}
--- /dev/null   Tue Mar 18 13:55:57 2003
+++ m4/calloc.m4        Mon Sep 29 14:11:21 2003
@@ -0,0 +1,28 @@
+# calloc.m4 serial 1
+dnl Copyright (C) 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
+dnl that contains a configuration script generated by Autoconf, under
+dnl the same distribution terms as the rest of that program.
+
+dnl From Paul Eggert
+dnl Determine whether calloc (N, 0) and calloc (0, N) return non-NULL.
+dnl If they don't, arrange to use the replacement function.
+dnl Cheat by assuming calloc is consistent with malloc.
+
+AC_DEFUN([gl_FUNC_CALLOC],
+[
+  AC_REQUIRE([AC_FUNC_MALLOC])
+  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
+    AC_LIBOBJ(calloc)
+    AC_DEFINE([calloc], [rpl_calloc],
+       [Define to rpl_calloc if the replacement function should be used.])
+  fi
+])
+
+# Prerequisites of lib/calloc.c.
+AC_DEFUN([gl_PREREQ_CALLOC], [
+  :
+])




reply via email to

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