]> sjero.net Git - wget/blob - src/xmalloc.h
[svn] Abort on xfree(NULL).
[wget] / src / xmalloc.h
1 /* xmalloc.c declarations.
2    Copyright (C) 2003 Free Software Foundation, Inc.
3
4 This file is part of GNU Wget.
5
6 GNU Wget is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10
11 GNU Wget is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Wget; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20 In addition, as a special exception, the Free Software Foundation
21 gives permission to link the code of its release of Wget with the
22 OpenSSL project's "OpenSSL" library (or with modified versions of it
23 that use the same license as the "OpenSSL" library), and distribute
24 the linked executables.  You must obey the GNU General Public License
25 in all respects for all of the code used other than "OpenSSL".  If you
26 modify this file, you may extend this exception to your version of the
27 file, but you are not obligated to do so.  If you do not wish to do
28 so, delete this exception statement from your version.  */
29
30 #ifndef XMALLOC_H
31 #define XMALLOC_H
32
33 /* Define this if you want primitive but extensive malloc debugging.
34    It will make Wget extremely slow, so only do it in development
35    builds.  */
36 #define DEBUG_MALLOC
37
38 /* When DEBUG_MALLOC is not defined (which is normally the case), the
39    allocation functions directly map to *_real wrappers.  In the
40    DEBUG_MALLOC mode, they also record the file and line where the
41    offending malloc/free/... was invoked.
42
43    *Note*: xfree(NULL) aborts.  If the pointer you're freeing can be
44    NULL, use xfree_null instead.  */
45
46 #ifndef DEBUG_MALLOC
47
48 #define xmalloc  xmalloc_real
49 #define xmalloc0 xmalloc0_real
50 #define xrealloc xrealloc_real
51 #define xstrdup  xstrdup_real
52 #define xfree    xfree_real
53
54 void *xmalloc_real PARAMS ((size_t));
55 void *xmalloc0_real PARAMS ((size_t));
56 void *xrealloc_real PARAMS ((void *, size_t));
57 char *xstrdup_real PARAMS ((const char *));
58 void xfree_real PARAMS ((void *));
59
60 #else  /* DEBUG_MALLOC */
61
62 #define xmalloc(s)     xmalloc_debug (s, __FILE__, __LINE__)
63 #define xmalloc0(s)    xmalloc0_debug (s, __FILE__, __LINE__)
64 #define xrealloc(p, s) xrealloc_debug (p, s, __FILE__, __LINE__)
65 #define xstrdup(p)     xstrdup_debug (p, __FILE__, __LINE__)
66 #define xfree(p)       xfree_debug (p, __FILE__, __LINE__)
67
68 void *xmalloc_debug PARAMS ((size_t, const char *, int));
69 void *xmalloc0_debug PARAMS ((size_t, const char *, int));
70 void *xrealloc_debug PARAMS ((void *, size_t, const char *, int));
71 char *xstrdup_debug PARAMS ((const char *, const char *, int));
72 void xfree_debug PARAMS ((void *, const char *, int));
73
74 #endif /* DEBUG_MALLOC */
75
76 /* Macros that interface to malloc, but know about type sizes, and
77    cast the result to the appropriate type.  The casts are not
78    necessary in standard C, but Wget performs them anyway for the sake
79    of pre-standard environments and possibly C++.  */
80
81 #define xnew(type) ((type *) xmalloc (sizeof (type)))
82 #define xnew0(type) ((type *) xmalloc0 (sizeof (type)))
83 #define xnew_array(type, len) ((type *) xmalloc ((len) * sizeof (type)))
84 #define xnew0_array(type, len) ((type *) xmalloc0 ((len) * sizeof (type)))
85
86 #define alloca_array(type, size) ((type *) alloca ((size) * sizeof (type)))
87
88 /* Free P if it is non-NULL.  C requires free() to behaves this way by
89    default, but Wget's code is historically careful not to pass NULL
90    to free.  This allows us to assert p!=NULL in xfree to check
91    additional errors.  (But we currently don't do that!)  */
92 #define xfree_null(p) if (!(p)) ; else xfree (p)
93
94 #endif /* XMALLOC_H */