From 42b93184c8c5ea607008970ab094f193d46591ad Mon Sep 17 00:00:00 2001 From: Amos Jeffries Date: Fri, 6 Jan 2012 03:12:04 +1300 Subject: [PATCH] Polish compat library xfree/xxfree * remove xxfree() entirely it has been a useless wrapper for free_const() for some time now. * update replace xfree() to a static inline function to permit better compiler optimization --- compat/xalloc.cc | 9 --------- compat/xalloc.h | 20 +++++++++++--------- include/util.h | 1 - lib/malloc_trace.cc | 1 - 4 files changed, 11 insertions(+), 20 deletions(-) diff --git a/compat/xalloc.cc b/compat/xalloc.cc index db13493c41..bd84bef9e2 100644 --- a/compat/xalloc.cc +++ b/compat/xalloc.cc @@ -193,15 +193,6 @@ xrealloc(void *s, size_t sz) return (p); } -void -xfree(void *s) -{ - if (s == NULL) - return; - - free_const(s); -} - void free_const(const void *s_const) { diff --git a/compat/xalloc.h b/compat/xalloc.h index 1d86f404e2..1daff1504e 100644 --- a/compat/xalloc.h +++ b/compat/xalloc.h @@ -30,27 +30,29 @@ extern "C" { void *xrealloc(void *s, size_t sz); /** - * xfree() - same as free(3). Used for portability. - * Will not call free(3) if s == NULL. + * free_const() - Same as free(3). Used for portability. + * Accepts pointers to dynamically allocated const data. * * Define failure_notify to receive error message. * otherwise perror() is used to display it. */ - void xfree(void *s); + void free_const(const void *s); /** - * xxfree() / free_const() - Same as free(3). Used for portability. + * xfree() - same as free(3). Used for portability. * Accepts pointers to dynamically allocated const data. + * Will not call free(3) if the pointer is NULL. + * + * Pointer is left with a value on completion. + * Use safe_free() if the pointer needs to be set to NULL afterward. * * Define failure_notify to receive error message. * otherwise perror() is used to display it. */ - void free_const(const void *s); - -/// Backward compatibility alias for free_const(const void *s) -#define xxfree(x) free_const((x)) + static inline void xfree(const void *p) { if (p) free_const(p); } /** + * safe_free() - same as free(3). Used for portability. * Accepts pointers to dynamically allocated const data. * Will not call free(3) if the pointer is NULL. * Sets the pointer to NULL on completion. @@ -60,7 +62,7 @@ extern "C" { * Define failure_notify to receive error message. * otherwise perror() is used to display it. */ -#define safe_free(x) while (x) { xxfree(x); (x) = NULL; } +#define safe_free(x) while ((x)) { free_const((x)); (x) = NULL; } #ifdef __cplusplus } diff --git a/include/util.h b/include/util.h index 4bcd24e0b4..cb75b96889 100644 --- a/include/util.h +++ b/include/util.h @@ -69,7 +69,6 @@ SQUIDCEXTERN void Tolower(char *); #if XMALLOC_TRACE #define xmalloc(size) (xmalloc_func="xmalloc",xmalloc_line=__LINE__,xmalloc_file=__FILE__,xmalloc(size)) #define xfree(ptr) (xmalloc_func="xfree",xmalloc_line=__LINE__,xmalloc_file=__FILE__,xfree(ptr)) -#define xxfree(ptr) (xmalloc_func="xxfree",xmalloc_line=__LINE__,xmalloc_file=__FILE__,xxfree(ptr)) #define xrealloc(ptr,size) (xmalloc_func="xrealloc",xmalloc_line=__LINE__,xmalloc_file=__FILE__,xrealloc(ptr,size)) #define xcalloc(n,size) (xmalloc_func="xcalloc",xmalloc_line=__LINE__,xmalloc_file=__FILE__,xcalloc(n,size)) #define xstrdup(ptr) (xmalloc_func="xstrdup",xmalloc_line=__LINE__,xmalloc_file=__FILE__,xstrdup(ptr)) diff --git a/lib/malloc_trace.cc b/lib/malloc_trace.cc index fe1224a690..4d107b60d3 100644 --- a/lib/malloc_trace.cc +++ b/lib/malloc_trace.cc @@ -98,7 +98,6 @@ int xmalloc_trace = 0; /* Enable with -m option */ size_t xmalloc_total = 0; #undef xmalloc #undef xfree -#undef xxfree #undef xrealloc #undef xcalloc #undef xstrdup -- 2.47.2