]> sjero.net Git - wget/blob - src/xmalloc.h
Fix ISDIGIT to c_isdigit.
[wget] / src / xmalloc.h
1 /* xmalloc.c declarations.
2    Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 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 /* Croak the fatal memory error and bail out with non-zero exit
35    status.  */
36 void memfatal (const char *context, long attempted_size);
37
38 /* Constant is using when we don`t know attempted size exactly */
39 #define UNKNOWN_ATTEMPTED_SIZE -3
40
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.  */
45 #undef DEBUG_MALLOC
46
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
50
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.
55
56    *Note*: xfree(NULL) aborts in both modes.  If the pointer you're
57    freeing can be NULL, use xfree_null instead.  */
58
59 #ifndef DEBUG_MALLOC
60
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
66
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 *);
72
73 #else  /* DEBUG_MALLOC */
74
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__)
80
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);
86
87 #endif /* DEBUG_MALLOC */
88
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++.  */
93
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)))
98
99 #define alloca_array(type, size) ((type *) alloca ((size) * sizeof (type)))
100
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)
106
107 #endif /* XMALLOC_H */