]> sjero.net Git - wget/blob - src/xmalloc.h
d738afc502f57b2f8ebce6bc83273be3122da6a1
[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 /* When DEBUG_MALLOC is not defined (which is normally the case), the
34    allocation functions directly map to *_real wrappers.  In the
35    DEBUG_MALLOC mode, they also record the file and line where the
36    offending malloc/free/... was invoked.  */
37
38 #ifndef DEBUG_MALLOC
39
40 #define xmalloc  xmalloc_real
41 #define xmalloc0 xmalloc0_real
42 #define xrealloc xrealloc_real
43 #define xstrdup  xstrdup_real
44 #define xfree    free
45
46 void *xmalloc_real PARAMS ((size_t));
47 void *xmalloc0_real PARAMS ((size_t));
48 void *xrealloc_real PARAMS ((void *, size_t));
49 char *xstrdup_real PARAMS ((const char *));
50
51 #else  /* DEBUG_MALLOC */
52
53 #define xmalloc(s)     xmalloc_debug (s, __FILE__, __LINE__)
54 #define xmalloc0(s)    xmalloc0_debug (s, __FILE__, __LINE__)
55 #define xfree(p)       xfree_debug (p, __FILE__, __LINE__)
56 #define xrealloc(p, s) xrealloc_debug (p, s, __FILE__, __LINE__)
57 #define xstrdup(p)     xstrdup_debug (p, __FILE__, __LINE__)
58
59 void *xmalloc_debug PARAMS ((size_t, const char *, int));
60 void *xmalloc0_debug PARAMS ((size_t, const char *, int));
61 void xfree_debug PARAMS ((void *, const char *, int));
62 void *xrealloc_debug PARAMS ((void *, size_t, const char *, int));
63 char *xstrdup_debug PARAMS ((const char *, const char *, int));
64
65 #endif /* DEBUG_MALLOC */
66
67 /* Macros that interface to malloc, but know about type sizes, and
68    cast the result to the appropriate type.  The casts are not
69    necessary in standard C, but Wget performs them anyway for the sake
70    of pre-standard environments and possibly C++.  */
71
72 #define xnew(type) ((type *) xmalloc (sizeof (type)))
73 #define xnew0(type) ((type *) xmalloc0 (sizeof (type)))
74 #define xnew_array(type, len) ((type *) xmalloc ((len) * sizeof (type)))
75 #define xnew0_array(type, len) ((type *) xmalloc0 ((len) * sizeof (type)))
76
77 #define alloca_array(type, size) ((type *) alloca ((size) * sizeof (type)))
78
79 /* Free P if it is non-NULL.  C requires free() to behaves this way by
80    default, but Wget's code is historically careful not to pass NULL
81    to free.  This allows us to assert p!=NULL in xfree to check
82    additional errors.  (But we currently don't do that!)  */
83 #define xfree_null(p) if (!(p)) ; else xfree (p)
84
85 #endif /* XMALLOC_H */