2003-10-31 09:31:56 -05:00
|
|
|
/* xmalloc.c declarations.
|
2007-09-28 18:45:31 -04:00
|
|
|
Copyright (C) 2003, 2004, 2005, 2006, 2007 Free Software Foundation,
|
|
|
|
Inc.
|
2003-10-31 09:31:56 -05:00
|
|
|
|
|
|
|
This file is part of GNU Wget.
|
|
|
|
|
|
|
|
GNU Wget is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
2007-07-10 01:53:22 -04:00
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
2003-10-31 09:31:56 -05:00
|
|
|
|
|
|
|
GNU Wget is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2007-07-10 01:53:22 -04:00
|
|
|
along with Wget. If not, see <http://www.gnu.org/licenses/>.
|
2003-10-31 09:31:56 -05:00
|
|
|
|
|
|
|
In addition, as a special exception, the Free Software Foundation
|
|
|
|
gives permission to link the code of its release of Wget with the
|
|
|
|
OpenSSL project's "OpenSSL" library (or with modified versions of it
|
|
|
|
that use the same license as the "OpenSSL" library), and distribute
|
|
|
|
the linked executables. You must obey the GNU General Public License
|
|
|
|
in all respects for all of the code used other than "OpenSSL". If you
|
|
|
|
modify this file, you may extend this exception to your version of the
|
|
|
|
file, but you are not obligated to do so. If you do not wish to do
|
|
|
|
so, delete this exception statement from your version. */
|
|
|
|
|
|
|
|
#ifndef XMALLOC_H
|
|
|
|
#define XMALLOC_H
|
|
|
|
|
2005-04-10 20:41:20 -04:00
|
|
|
/* Define this to use Wget's builtin malloc debugging, which is crude
|
|
|
|
but occasionally useful. It will make Wget a lot slower and
|
|
|
|
larger, and susceptible to aborting if malloc_table overflows, so
|
|
|
|
it should be used by developers only. */
|
2003-11-02 16:15:57 -05:00
|
|
|
#undef DEBUG_MALLOC
|
2003-11-02 16:12:49 -05:00
|
|
|
|
2003-10-31 09:31:56 -05:00
|
|
|
/* When DEBUG_MALLOC is not defined (which is normally the case), the
|
2005-03-20 10:07:40 -05:00
|
|
|
allocator identifiers are mapped to checking_* wrappers, which exit
|
|
|
|
Wget if malloc/realloc/strdup return NULL
|
2003-11-02 16:12:49 -05:00
|
|
|
|
2005-03-20 10:07:40 -05:00
|
|
|
In DEBUG_MALLOC mode, the allocators are mapped to debugging_*
|
|
|
|
wrappers, which also record the file and line from which the
|
|
|
|
allocation was attempted. At the end of the program, a detailed
|
|
|
|
summary of unfreed allocations is displayed.
|
|
|
|
|
|
|
|
*Note*: xfree(NULL) aborts in both modes. If the pointer you're
|
|
|
|
freeing can be NULL, use xfree_null instead. */
|
2003-10-31 09:31:56 -05:00
|
|
|
|
|
|
|
#ifndef DEBUG_MALLOC
|
|
|
|
|
2005-03-20 10:07:40 -05:00
|
|
|
#define xmalloc checking_malloc
|
|
|
|
#define xmalloc0 checking_malloc0
|
|
|
|
#define xrealloc checking_realloc
|
|
|
|
#define xstrdup checking_strdup
|
|
|
|
#define xfree checking_free
|
2003-10-31 09:31:56 -05:00
|
|
|
|
2005-06-19 18:34:58 -04:00
|
|
|
void *checking_malloc (size_t);
|
|
|
|
void *checking_malloc0 (size_t);
|
|
|
|
void *checking_realloc (void *, size_t);
|
|
|
|
char *checking_strdup (const char *);
|
|
|
|
void checking_free (void *);
|
2003-10-31 09:31:56 -05:00
|
|
|
|
|
|
|
#else /* DEBUG_MALLOC */
|
|
|
|
|
2005-03-20 10:07:40 -05:00
|
|
|
#define xmalloc(s) debugging_malloc (s, __FILE__, __LINE__)
|
|
|
|
#define xmalloc0(s) debugging_malloc0 (s, __FILE__, __LINE__)
|
|
|
|
#define xrealloc(p, s) debugging_realloc (p, s, __FILE__, __LINE__)
|
|
|
|
#define xstrdup(p) debugging_strdup (p, __FILE__, __LINE__)
|
|
|
|
#define xfree(p) debugging_free (p, __FILE__, __LINE__)
|
|
|
|
|
2005-06-19 18:34:58 -04:00
|
|
|
void *debugging_malloc (size_t, const char *, int);
|
|
|
|
void *debugging_malloc0 (size_t, const char *, int);
|
|
|
|
void *debugging_realloc (void *, size_t, const char *, int);
|
|
|
|
char *debugging_strdup (const char *, const char *, int);
|
|
|
|
void debugging_free (void *, const char *, int);
|
2003-10-31 09:31:56 -05:00
|
|
|
|
|
|
|
#endif /* DEBUG_MALLOC */
|
|
|
|
|
|
|
|
/* Macros that interface to malloc, but know about type sizes, and
|
|
|
|
cast the result to the appropriate type. The casts are not
|
|
|
|
necessary in standard C, but Wget performs them anyway for the sake
|
|
|
|
of pre-standard environments and possibly C++. */
|
|
|
|
|
2005-06-19 19:03:27 -04:00
|
|
|
#define xnew(type) (xmalloc (sizeof (type)))
|
|
|
|
#define xnew0(type) (xmalloc0 (sizeof (type)))
|
|
|
|
#define xnew_array(type, len) (xmalloc ((len) * sizeof (type)))
|
|
|
|
#define xnew0_array(type, len) (xmalloc0 ((len) * sizeof (type)))
|
2003-10-31 09:31:56 -05:00
|
|
|
|
|
|
|
#define alloca_array(type, size) ((type *) alloca ((size) * sizeof (type)))
|
|
|
|
|
2003-11-02 14:56:37 -05:00
|
|
|
/* Free P if it is non-NULL. C requires free() to behaves this way by
|
|
|
|
default, but Wget's code is historically careful not to pass NULL
|
|
|
|
to free. This allows us to assert p!=NULL in xfree to check
|
|
|
|
additional errors. (But we currently don't do that!) */
|
|
|
|
#define xfree_null(p) if (!(p)) ; else xfree (p)
|
|
|
|
|
2003-10-31 09:31:56 -05:00
|
|
|
#endif /* XMALLOC_H */
|