2008-05-24 16:05:49 +08:00
|
|
|
/* tinyproxy - A fast light-weight HTTP proxy
|
|
|
|
* Copyright (C) 2002 Robert James Kaes <rjkaes@users.sourceforge.net>
|
2002-05-23 12:41:10 +08:00
|
|
|
*
|
2008-05-24 16:05:49 +08:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Debugging versions of various heap related functions are combined
|
2002-05-23 12:41:10 +08:00
|
|
|
* here. The debugging versions include assertions and also print
|
|
|
|
* (to standard error) the function called along with the amount
|
|
|
|
* of memory allocated, and where the memory is pointing. The
|
|
|
|
* format of the log message is standardized.
|
|
|
|
*/
|
|
|
|
|
2009-08-07 06:12:53 +08:00
|
|
|
#include "main.h"
|
2002-05-23 12:41:10 +08:00
|
|
|
#include "heap.h"
|
2002-05-30 04:51:35 +08:00
|
|
|
#include "text.h"
|
2002-05-23 12:41:10 +08:00
|
|
|
|
2009-09-23 09:42:30 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
|
2009-09-15 03:41:25 +08:00
|
|
|
void *debugging_calloc (size_t nmemb, size_t size, const char *file,
|
|
|
|
unsigned long line)
|
2002-05-23 12:41:10 +08:00
|
|
|
{
|
2009-09-15 03:41:25 +08:00
|
|
|
void *ptr;
|
2002-05-23 12:41:10 +08:00
|
|
|
|
2009-09-15 03:41:25 +08:00
|
|
|
assert (nmemb > 0);
|
|
|
|
assert (size > 0);
|
2002-05-23 12:41:10 +08:00
|
|
|
|
2009-09-15 03:41:25 +08:00
|
|
|
ptr = calloc (nmemb, size);
|
2009-11-17 00:19:19 +08:00
|
|
|
fprintf (stderr, "{calloc: %p:%lu x %lu} %s:%lu\n", ptr,
|
|
|
|
(unsigned long) nmemb, (unsigned long) size, file, line);
|
2009-09-15 03:41:25 +08:00
|
|
|
return ptr;
|
2002-05-23 12:41:10 +08:00
|
|
|
}
|
|
|
|
|
2009-09-15 03:41:25 +08:00
|
|
|
void *debugging_malloc (size_t size, const char *file, unsigned long line)
|
2002-05-23 12:41:10 +08:00
|
|
|
{
|
2009-09-15 03:41:25 +08:00
|
|
|
void *ptr;
|
2002-05-23 12:41:10 +08:00
|
|
|
|
2009-09-15 03:41:25 +08:00
|
|
|
assert (size > 0);
|
2002-05-23 12:41:10 +08:00
|
|
|
|
2009-09-15 03:41:25 +08:00
|
|
|
ptr = malloc (size);
|
2009-11-17 00:19:19 +08:00
|
|
|
fprintf (stderr, "{malloc: %p:%lu} %s:%lu\n", ptr,
|
|
|
|
(unsigned long) size, file, line);
|
2009-09-15 03:41:25 +08:00
|
|
|
return ptr;
|
2002-05-23 12:41:10 +08:00
|
|
|
}
|
|
|
|
|
2009-09-15 03:41:25 +08:00
|
|
|
void *debugging_realloc (void *ptr, size_t size, const char *file,
|
|
|
|
unsigned long line)
|
2002-05-23 12:41:10 +08:00
|
|
|
{
|
2009-09-15 03:41:25 +08:00
|
|
|
void *newptr;
|
2005-08-15 11:54:31 +08:00
|
|
|
|
2009-09-15 03:41:25 +08:00
|
|
|
assert (size > 0);
|
2005-08-15 11:54:31 +08:00
|
|
|
|
2009-09-15 03:41:25 +08:00
|
|
|
newptr = realloc (ptr, size);
|
2009-11-17 00:19:19 +08:00
|
|
|
fprintf (stderr, "{realloc: %p -> %p:%lu} %s:%lu\n", ptr, newptr,
|
|
|
|
(unsigned long) size, file, line);
|
2009-09-15 03:41:25 +08:00
|
|
|
return newptr;
|
2002-05-23 12:41:10 +08:00
|
|
|
}
|
|
|
|
|
2009-09-15 03:41:25 +08:00
|
|
|
void debugging_free (void *ptr, const char *file, unsigned long line)
|
2002-05-23 12:41:10 +08:00
|
|
|
{
|
2009-09-15 03:41:25 +08:00
|
|
|
fprintf (stderr, "{free: %p} %s:%lu\n", ptr, file, line);
|
2003-06-27 02:14:13 +08:00
|
|
|
|
2009-09-15 03:41:25 +08:00
|
|
|
if (ptr != NULL)
|
|
|
|
free (ptr);
|
|
|
|
return;
|
2002-05-23 12:41:10 +08:00
|
|
|
}
|
|
|
|
|
2009-09-15 03:41:25 +08:00
|
|
|
char *debugging_strdup (const char *s, const char *file, unsigned long line)
|
2002-05-23 12:41:10 +08:00
|
|
|
{
|
2009-09-15 03:41:25 +08:00
|
|
|
char *ptr;
|
|
|
|
size_t len;
|
2002-05-23 12:41:10 +08:00
|
|
|
|
2009-09-15 03:41:25 +08:00
|
|
|
assert (s != NULL);
|
2002-05-23 12:41:10 +08:00
|
|
|
|
2009-09-15 03:41:25 +08:00
|
|
|
len = strlen (s) + 1;
|
|
|
|
ptr = (char *) malloc (len);
|
|
|
|
if (!ptr)
|
|
|
|
return NULL;
|
|
|
|
memcpy (ptr, s, len);
|
2002-05-23 12:41:10 +08:00
|
|
|
|
2009-11-17 00:19:19 +08:00
|
|
|
fprintf (stderr, "{strdup: %p:%lu} %s:%lu\n", ptr,
|
|
|
|
(unsigned long) len, file, line);
|
2009-09-15 03:41:25 +08:00
|
|
|
return ptr;
|
2002-05-23 12:41:10 +08:00
|
|
|
}
|
2002-05-27 02:56:06 +08:00
|
|
|
|
2009-09-23 09:42:30 +08:00
|
|
|
#endif /* !NDEBUG */
|
|
|
|
|