]> sjero.net Git - wget/blob - src/xmalloc.h
Updated licensing exception for OpenSSL from the SFLC.
[wget] / src / xmalloc.h
1 /* xmalloc.c declarations.
2    Copyright (C) 2003, 2004, 2005, 2006, 2007 Free Software Foundation,
3    Inc.
4
5 This file is part of GNU Wget.
6
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.
11
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.
16
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/>.
19
20 Additional permission under GNU GPL version 3 section 7
21
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.  */
30
31 #ifndef XMALLOC_H
32 #define XMALLOC_H
33
34 /* Define this to use Wget's builtin malloc debugging, which is crude
35    but occasionally useful.  It will make Wget a lot slower and
36    larger, and susceptible to aborting if malloc_table overflows, so
37    it should be used by developers only.  */
38 #undef DEBUG_MALLOC
39
40 /* When DEBUG_MALLOC is not defined (which is normally the case), the
41    allocator identifiers are mapped to checking_* wrappers, which exit
42    Wget if malloc/realloc/strdup return NULL
43
44    In DEBUG_MALLOC mode, the allocators are mapped to debugging_*
45    wrappers, which also record the file and line from which the
46    allocation was attempted.  At the end of the program, a detailed
47    summary of unfreed allocations is displayed.
48
49    *Note*: xfree(NULL) aborts in both modes.  If the pointer you're
50    freeing can be NULL, use xfree_null instead.  */
51
52 #ifndef DEBUG_MALLOC
53
54 #define xmalloc  checking_malloc
55 #define xmalloc0 checking_malloc0
56 #define xrealloc checking_realloc
57 #define xstrdup  checking_strdup
58 #define xfree    checking_free
59
60 void *checking_malloc (size_t);
61 void *checking_malloc0 (size_t);
62 void *checking_realloc (void *, size_t);
63 char *checking_strdup (const char *);
64 void checking_free (void *);
65
66 #else  /* DEBUG_MALLOC */
67
68 #define xmalloc(s)     debugging_malloc (s, __FILE__, __LINE__)
69 #define xmalloc0(s)    debugging_malloc0 (s, __FILE__, __LINE__)
70 #define xrealloc(p, s) debugging_realloc (p, s, __FILE__, __LINE__)
71 #define xstrdup(p)     debugging_strdup (p, __FILE__, __LINE__)
72 #define xfree(p)       debugging_free (p, __FILE__, __LINE__)
73
74 void *debugging_malloc (size_t, const char *, int);
75 void *debugging_malloc0 (size_t, const char *, int);
76 void *debugging_realloc (void *, size_t, const char *, int);
77 char *debugging_strdup (const char *, const char *, int);
78 void debugging_free (void *, const char *, int);
79
80 #endif /* DEBUG_MALLOC */
81
82 /* Macros that interface to malloc, but know about type sizes, and
83    cast the result to the appropriate type.  The casts are not
84    necessary in standard C, but Wget performs them anyway for the sake
85    of pre-standard environments and possibly C++.  */
86
87 #define xnew(type) (xmalloc (sizeof (type)))
88 #define xnew0(type) (xmalloc0 (sizeof (type)))
89 #define xnew_array(type, len) (xmalloc ((len) * sizeof (type)))
90 #define xnew0_array(type, len) (xmalloc0 ((len) * sizeof (type)))
91
92 #define alloca_array(type, size) ((type *) alloca ((size) * sizeof (type)))
93
94 /* Free P if it is non-NULL.  C requires free() to behaves this way by
95    default, but Wget's code is historically careful not to pass NULL
96    to free.  This allows us to assert p!=NULL in xfree to check
97    additional errors.  (But we currently don't do that!)  */
98 #define xfree_null(p) if (!(p)) ; else xfree (p)
99
100 #endif /* XMALLOC_H */