bug-gnulib
[Top][All Lists]
Advanced

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

Re: [Bug-gnulib] Patch proposal: 1-gary-safe-xfree.patch


From: Paul Eggert
Subject: Re: [Bug-gnulib] Patch proposal: 1-gary-safe-xfree.patch
Date: 11 Sep 2003 01:29:59 -0700
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3

Here is an even better implementation for xfree (p).  It evaluates p
only once, and it makes xfree (p) an expression so that you can say
something like this:

   p = (xfree (p), NULL);

if you like.  Personally I prefer 'xfree' to 'XFREE', since xfree is
really a function, but this patch leaves 'XFREE' in for backwards
compatibility.

I suppose we could use Autoconf to test whether 'free (NULL)' works,
and optimize xfree in that case.  This would improve performance only
for native compiles, but that's an important special case.

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

        * lib/xalloc.h: Include <stdlib.h>, to declare 'free'.
        (xfree): New function.
        (XFREE): Use it.
        * modules/xalloc: Depend on inline.

Index: lib/xalloc.h
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/xalloc.h,v
retrieving revision 1.16
diff -p -u -r1.16 xalloc.h
--- lib/xalloc.h        22 Jul 2003 22:10:56 -0000      1.16
+++ lib/xalloc.h        11 Sep 2003 08:20:22 -0000
@@ -61,11 +61,13 @@ char *xstrdup (const char *str);
 # define NEW(Type, Var)  Type *(Var) = XMALLOC (Type, 1)
 
 /* Free VAR only if non NULL. */
-# define XFREE(Var)    \
-   do {                 \
-      if (Var)          \
-        free (Var);     \
-   } while (0)
+# define XFREE(Var) xfree (Var)
+static inline void
+xfree (void *p)
+{
+  if (p)
+    free (p);
+}
 
 /* Return a pointer to a malloc'ed copy of the array SRC of NUM elements. */
 # define CCLONE(Src, Num) \
Index: modules/xalloc
===================================================================
RCS file: /cvsroot/gnulib/gnulib/modules/xalloc,v
retrieving revision 1.4
diff -p -u -r1.4 xalloc
--- modules/xalloc      22 Jul 2003 22:10:56 -0000      1.4
+++ modules/xalloc      11 Sep 2003 08:20:22 -0000
@@ -8,6 +8,7 @@ lib/xstrdup.c
 m4/xalloc.m4
 
 Depends-on:
+inline
 malloc
 realloc
 error




reply via email to

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