[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Bug-gnulib] Patch proposal: 1-gary-safe-xfree.patch
From: |
Gary V. Vaughan |
Subject: |
[Bug-gnulib] Patch proposal: 1-gary-safe-xfree.patch |
Date: |
Wed, 10 Sep 2003 17:11:13 +0100 |
User-agent: |
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.5b) Gecko/20030903 Thunderbird/0.2 |
Flushing some changes I have in GNU m4 upstream :-)
Okay to commit?
This patch does change the semantics of XFREE slightly, but in a beneficial
way. Maybe XFREE should still be a macroised xfree for speed, and the XFREE
implementation in this patch renamed to DELETE or something? It is nice to
have an xfree function to use for callbacks that autofree etc. when you still
need the NULL safety.
Cheers,
Gary.
--
())_. Gary V. Vaughan gary@(lilith.warpmail.net|gnu.org)
( '/ Research Scientist http://www.oranda.demon.co.uk ,_())____
/ )= GNU Hacker http://www.gnu.org/software/libtool \' `&
`(_~)_ Tech' Author http://sources.redhat.com/autobook =`---d__/
Index: ChangeLog
from Gary V. Vaughan <address@hidden>
* lib/xalloc.h (xfree): Make XFREE a function, without the
disadvantage of multiple argument expansions.
(XFREE): Use it to zero out freed variables.
* lib/xmalloc.c (xfree): New function.
Index: lib/xalloc.h
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/xalloc.h,v
retrieving revision 1.16
diff -u -p -u -r1.16 xalloc.h
--- lib/xalloc.h 22 Jul 2003 22:10:56 -0000 1.16
+++ lib/xalloc.h 10 Sep 2003 15:59:01 -0000
@@ -53,19 +53,21 @@ void *xcalloc (size_t n, size_t s);
void *xrealloc (void *p, size_t n);
char *xstrdup (const char *str);
+/* Only free P if it is non-NULL. Unlike free(), this function
+ always returns NULL so that you can easily reset the address of any
+ variable whose memory you free:
+ foo = xfree (foo);
+ You can pass it to callbacks requiring free() if necessary:
+ lt_dlfree = (void (*)(void *)) xfree; */
+void *xfree (void *p);
+
# define XMALLOC(Type, N_items) xmalloc (sizeof (Type) * (N_items))
# define XCALLOC(Type, N_items) xcalloc (sizeof (Type), N_items)
# define XREALLOC(Ptr, Type, N_items) xrealloc (Ptr, sizeof (Type) * (N_items))
+# define XFREE(Var) ((Var) = xfree (Var))
/* Declare and alloc memory for VAR of type TYPE. */
# 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)
/* Return a pointer to a malloc'ed copy of the array SRC of NUM elements. */
# define CCLONE(Src, Num) \
Index: lib/xmalloc.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/xmalloc.c,v
retrieving revision 1.25
diff -u -p -u -r1.25 xmalloc.c
--- lib/xmalloc.c 22 Jul 2003 22:10:56 -0000 1.25
+++ lib/xmalloc.c 10 Sep 2003 15:59:01 -0000
@@ -110,3 +110,14 @@ xcalloc (size_t n, size_t s)
xalloc_die ();
return p;
}
+
+/* Only free P if non-NULL. */
+
+void *
+xfree (void *p)
+{
+ if (p)
+ free (p);
+ return 0;
+}
+
- [Bug-gnulib] Patch proposal: 1-gary-safe-xfree.patch,
Gary V. Vaughan <=
- Re: [Bug-gnulib] Patch proposal: 1-gary-safe-xfree.patch, Bruno Haible, 2003/09/10
- Re: [Bug-gnulib] Patch proposal: 1-gary-safe-xfree.patch, Paul Eggert, 2003/09/11
- Re: [Bug-gnulib] Patch proposal: 1-gary-safe-xfree.patch, Paul Eggert, 2003/09/11
- Re: [Bug-gnulib] Patch proposal: 1-gary-safe-xfree.patch, Gary V. Vaughan, 2003/09/11
- Re: [Bug-gnulib] Patch proposal: 1-gary-safe-xfree.patch, Paul Eggert, 2003/09/12
- Re: [Bug-gnulib] Patch proposal: 1-gary-safe-xfree.patch, Jim Meyering, 2003/09/13
- Re: [Bug-gnulib] Patch proposal: 1-gary-safe-xfree.patch, Gary V. Vaughan, 2003/09/15
- Re: [Bug-gnulib] Patch proposal: 1-gary-safe-xfree.patch, Paul Eggert, 2003/09/15