]> sjero.net Git - wget/blobdiff - src/xmalloc.h
[svn] Remove K&R support.
[wget] / src / xmalloc.h
index 220fc312fe3c841744c598be6c16bd187cf43ac5..c094e7c9bda68045ee5e04e1e0f6cc558b2dfb51 100644 (file)
@@ -30,37 +30,51 @@ so, delete this exception statement from your version.  */
 #ifndef XMALLOC_H
 #define XMALLOC_H
 
+/* Define this to use Wget's builtin malloc debugging, which is crude
+   but occasionally useful.  It will make Wget a lot slower and
+   larger, and susceptible to aborting if malloc_table overflows, so
+   it should be used by developers only.  */
+#undef DEBUG_MALLOC
+
 /* When DEBUG_MALLOC is not defined (which is normally the case), the
-   allocation functions directly map to *_real wrappers.  In the
-   DEBUG_MALLOC mode, they also record the file and line where the
-   offending malloc/free/... was invoked.  */
+   allocator identifiers are mapped to checking_* wrappers, which exit
+   Wget if malloc/realloc/strdup return NULL
+
+   In DEBUG_MALLOC mode, the allocators are mapped to debugging_*
+   wrappers, which also record the file and line from which the
+   allocation was attempted.  At the end of the program, a detailed
+   summary of unfreed allocations is displayed.
+
+   *Note*: xfree(NULL) aborts in both modes.  If the pointer you're
+   freeing can be NULL, use xfree_null instead.  */
 
 #ifndef DEBUG_MALLOC
 
-#define xmalloc  xmalloc_real
-#define xmalloc0 xmalloc0_real
-#define xrealloc xrealloc_real
-#define xstrdup  xstrdup_real
-#define xfree    free
+#define xmalloc  checking_malloc
+#define xmalloc0 checking_malloc0
+#define xrealloc checking_realloc
+#define xstrdup  checking_strdup
+#define xfree    checking_free
 
-void *xmalloc_real PARAMS ((size_t));
-void *xmalloc0_real PARAMS ((size_t));
-void *xrealloc_real PARAMS ((void *, size_t));
-char *xstrdup_real PARAMS ((const char *));
+void *checking_malloc (size_t);
+void *checking_malloc0 (size_t);
+void *checking_realloc (void *, size_t);
+char *checking_strdup (const char *);
+void checking_free (void *);
 
 #else  /* DEBUG_MALLOC */
 
-#define xmalloc(s)     xmalloc_debug (s, __FILE__, __LINE__)
-#define xmalloc0(s)    xmalloc0_debug (s, __FILE__, __LINE__)
-#define xfree(p)       xfree_debug (p, __FILE__, __LINE__)
-#define xrealloc(p, s) xrealloc_debug (p, s, __FILE__, __LINE__)
-#define xstrdup(p)     xstrdup_debug (p, __FILE__, __LINE__)
+#define xmalloc(s)     debugging_malloc (s, __FILE__, __LINE__)
+#define xmalloc0(s)    debugging_malloc0 (s, __FILE__, __LINE__)
+#define xrealloc(p, s) debugging_realloc (p, s, __FILE__, __LINE__)
+#define xstrdup(p)     debugging_strdup (p, __FILE__, __LINE__)
+#define xfree(p)       debugging_free (p, __FILE__, __LINE__)
 
-void *xmalloc_debug PARAMS ((size_t, const char *, int));
-void *xmalloc0_debug PARAMS ((size_t, const char *, int));
-void xfree_debug PARAMS ((void *, const char *, int));
-void *xrealloc_debug PARAMS ((void *, size_t, const char *, int));
-char *xstrdup_debug PARAMS ((const char *, const char *, int));
+void *debugging_malloc (size_t, const char *, int);
+void *debugging_malloc0 (size_t, const char *, int);
+void *debugging_realloc (void *, size_t, const char *, int);
+char *debugging_strdup (const char *, const char *, int);
+void debugging_free (void *, const char *, int);
 
 #endif /* DEBUG_MALLOC */
 
@@ -76,4 +90,10 @@ char *xstrdup_debug PARAMS ((const char *, const char *, int));
 
 #define alloca_array(type, size) ((type *) alloca ((size) * sizeof (type)))
 
+/* Free P if it is non-NULL.  C requires free() to behaves this way by
+   default, but Wget's code is historically careful not to pass NULL
+   to free.  This allows us to assert p!=NULL in xfree to check
+   additional errors.  (But we currently don't do that!)  */
+#define xfree_null(p) if (!(p)) ; else xfree (p)
+
 #endif /* XMALLOC_H */