]> sjero.net Git - wget/blob - src/xmalloc.h
[svn] Merge of fix for bugs 20341 and 20410.
[wget] / src / xmalloc.h
1 /* xmalloc.c declarations.
2    Copyright (C) 2003-2006 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 3 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, see <http://www.gnu.org/licenses/>.
18
19 In addition, as a special exception, the Free Software Foundation
20 gives permission to link the code of its release of Wget with the
21 OpenSSL project's "OpenSSL" library (or with modified versions of it
22 that use the same license as the "OpenSSL" library), and distribute
23 the linked executables.  You must obey the GNU General Public License
24 in all respects for all of the code used other than "OpenSSL".  If you
25 modify this file, you may extend this exception to your version of the
26 file, but you are not obligated to do so.  If you do not wish to do
27 so, delete this exception statement from your version.  */
28
29 #ifndef XMALLOC_H
30 #define XMALLOC_H
31
32 /* Define this to use Wget's builtin malloc debugging, which is crude
33    but occasionally useful.  It will make Wget a lot slower and
34    larger, and susceptible to aborting if malloc_table overflows, so
35    it should be used by developers only.  */
36 #undef DEBUG_MALLOC
37
38 /* When DEBUG_MALLOC is not defined (which is normally the case), the
39    allocator identifiers are mapped to checking_* wrappers, which exit
40    Wget if malloc/realloc/strdup return NULL
41
42    In DEBUG_MALLOC mode, the allocators are mapped to debugging_*
43    wrappers, which also record the file and line from which the
44    allocation was attempted.  At the end of the program, a detailed
45    summary of unfreed allocations is displayed.
46
47    *Note*: xfree(NULL) aborts in both modes.  If the pointer you're
48    freeing can be NULL, use xfree_null instead.  */
49
50 #ifndef DEBUG_MALLOC
51
52 #define xmalloc  checking_malloc
53 #define xmalloc0 checking_malloc0
54 #define xrealloc checking_realloc
55 #define xstrdup  checking_strdup
56 #define xfree    checking_free
57
58 void *checking_malloc (size_t);
59 void *checking_malloc0 (size_t);
60 void *checking_realloc (void *, size_t);
61 char *checking_strdup (const char *);
62 void checking_free (void *);
63
64 #else  /* DEBUG_MALLOC */
65
66 #define xmalloc(s)     debugging_malloc (s, __FILE__, __LINE__)
67 #define xmalloc0(s)    debugging_malloc0 (s, __FILE__, __LINE__)
68 #define xrealloc(p, s) debugging_realloc (p, s, __FILE__, __LINE__)
69 #define xstrdup(p)     debugging_strdup (p, __FILE__, __LINE__)
70 #define xfree(p)       debugging_free (p, __FILE__, __LINE__)
71
72 void *debugging_malloc (size_t, const char *, int);
73 void *debugging_malloc0 (size_t, const char *, int);
74 void *debugging_realloc (void *, size_t, const char *, int);
75 char *debugging_strdup (const char *, const char *, int);
76 void debugging_free (void *, const char *, int);
77
78 #endif /* DEBUG_MALLOC */
79
80 /* Macros that interface to malloc, but know about type sizes, and
81    cast the result to the appropriate type.  The casts are not
82    necessary in standard C, but Wget performs them anyway for the sake
83    of pre-standard environments and possibly C++.  */
84
85 #define xnew(type) (xmalloc (sizeof (type)))
86 #define xnew0(type) (xmalloc0 (sizeof (type)))
87 #define xnew_array(type, len) (xmalloc ((len) * sizeof (type)))
88 #define xnew0_array(type, len) (xmalloc0 ((len) * sizeof (type)))
89
90 #define alloca_array(type, size) ((type *) alloca ((size) * sizeof (type)))
91
92 /* Free P if it is non-NULL.  C requires free() to behaves this way by
93    default, but Wget's code is historically careful not to pass NULL
94    to free.  This allows us to assert p!=NULL in xfree to check
95    additional errors.  (But we currently don't do that!)  */
96 #define xfree_null(p) if (!(p)) ; else xfree (p)
97
98 #endif /* XMALLOC_H */