1 /* xmalloc.c declarations.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation,
5 This file is part of GNU Wget.
7 GNU Wget is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 GNU Wget is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Wget. If not, see <http://www.gnu.org/licenses/>.
20 Additional permission under GNU GPL version 3 section 7
22 If you modify this program, or any covered work, by linking or
23 combining it with the OpenSSL project's OpenSSL library (or a
24 modified version of that library), containing parts covered by the
25 terms of the OpenSSL or SSLeay licenses, the Free Software Foundation
26 grants you additional permission to convey the resulting work.
27 Corresponding Source for a non-source form of such a combination
28 shall include the source code for the parts of OpenSSL used as well
29 as that of the covered work. */
34 /* Croak the fatal memory error and bail out with non-zero exit
36 void memfatal (const char *context, long attempted_size);
38 /* Constant is using when we don`t know attempted size exactly */
39 #define UNKNOWN_ATTEMPTED_SIZE -3
41 /* Define this to use Wget's builtin malloc debugging, which is crude
42 but occasionally useful. It will make Wget a lot slower and
43 larger, and susceptible to aborting if malloc_table overflows, so
44 it should be used by developers only. */
47 /* When DEBUG_MALLOC is not defined (which is normally the case), the
48 allocator identifiers are mapped to checking_* wrappers, which exit
49 Wget if malloc/realloc/strdup return NULL
51 In DEBUG_MALLOC mode, the allocators are mapped to debugging_*
52 wrappers, which also record the file and line from which the
53 allocation was attempted. At the end of the program, a detailed
54 summary of unfreed allocations is displayed.
56 *Note*: xfree(NULL) aborts in both modes. If the pointer you're
57 freeing can be NULL, use xfree_null instead. */
61 #define xmalloc checking_malloc
62 #define xmalloc0 checking_malloc0
63 #define xrealloc checking_realloc
64 #define xstrdup checking_strdup
65 #define xfree checking_free
67 void *checking_malloc (size_t);
68 void *checking_malloc0 (size_t);
69 void *checking_realloc (void *, size_t);
70 char *checking_strdup (const char *);
71 void checking_free (void *);
73 #else /* DEBUG_MALLOC */
75 #define xmalloc(s) debugging_malloc (s, __FILE__, __LINE__)
76 #define xmalloc0(s) debugging_malloc0 (s, __FILE__, __LINE__)
77 #define xrealloc(p, s) debugging_realloc (p, s, __FILE__, __LINE__)
78 #define xstrdup(p) debugging_strdup (p, __FILE__, __LINE__)
79 #define xfree(p) debugging_free (p, __FILE__, __LINE__)
81 void *debugging_malloc (size_t, const char *, int);
82 void *debugging_malloc0 (size_t, const char *, int);
83 void *debugging_realloc (void *, size_t, const char *, int);
84 char *debugging_strdup (const char *, const char *, int);
85 void debugging_free (void *, const char *, int);
87 #endif /* DEBUG_MALLOC */
89 /* Macros that interface to malloc, but know about type sizes, and
90 cast the result to the appropriate type. The casts are not
91 necessary in standard C, but Wget performs them anyway for the sake
92 of pre-standard environments and possibly C++. */
94 #define xnew(type) (xmalloc (sizeof (type)))
95 #define xnew0(type) (xmalloc0 (sizeof (type)))
96 #define xnew_array(type, len) (xmalloc ((len) * sizeof (type)))
97 #define xnew0_array(type, len) (xmalloc0 ((len) * sizeof (type)))
99 #define alloca_array(type, size) ((type *) alloca ((size) * sizeof (type)))
101 /* Free P if it is non-NULL. C requires free() to behaves this way by
102 default, but Wget's code is historically careful not to pass NULL
103 to free. This allows us to assert p!=NULL in xfree to check
104 additional errors. (But we currently don't do that!) */
105 #define xfree_null(p) if (!(p)) ; else xfree (p)
107 #endif /* XMALLOC_H */