diff options
author | Robert James Kaes <rjkaes@users.sourceforge.net> | 2001-09-08 18:55:58 +0000 |
---|---|---|
committer | Robert James Kaes <rjkaes@users.sourceforge.net> | 2001-09-08 18:55:58 +0000 |
commit | 4923dd22a7c5d9984d822310fed466af27a35abb (patch) | |
tree | 34028e9818060ef764b940509cd79f41034de55e | |
parent | b8a694e7a327ecb73511077d26b3d70b01615a7a (diff) | |
download | tinyproxy-4923dd22a7c5d9984d822310fed466af27a35abb.tar.gz tinyproxy-4923dd22a7c5d9984d822310fed466af27a35abb.zip |
Added the debugging_(malloc|calloc|free) functions to help track memory
usage. There are also now defines for safe(malloc|calloc|free) which allow
for debugging code to be enabled or not.
Diffstat (limited to '')
-rw-r--r-- | src/utils.c | 36 | ||||
-rw-r--r-- | src/utils.h | 20 |
2 files changed, 51 insertions, 5 deletions
diff --git a/src/utils.c b/src/utils.c index 228b79f..7d9a872 100644 --- a/src/utils.c +++ b/src/utils.c @@ -1,4 +1,4 @@ -/* $Id: utils.c,v 1.9 2001-09-07 04:21:07 rjkaes Exp $ +/* $Id: utils.c,v 1.10 2001-09-08 18:55:58 rjkaes Exp $ * * Misc. routines which are used by the various functions to handle strings * and memory allocation and pretty much anything else we can think of. Also, @@ -31,6 +31,34 @@ #include "utils.h" /* + * These are the debugging calloc, malloc, and free versions + */ +#ifndef NDEBUG + +void *debugging_calloc(size_t nmemb, size_t size, const char *file, unsigned long line) +{ + void *ptr = calloc(nmemb, size); + fprintf(stderr, "{calloc: %p:%u x %u} %s:%lu\n", ptr, nmemb, size, file, line); + return ptr; +} + +void *debugging_malloc(size_t size, const char *file, unsigned long line) +{ + void *ptr = malloc(size); + fprintf(stderr, "{malloc: %p:%u} %s:%lu\n", ptr, size, file, line); + return ptr; +} + +void debugging_free(void *ptr, const char *file, unsigned long line) +{ + fprintf(stderr, "{free: %p} %s:%lu\n", ptr, file, line); + free(ptr); + return; +} + +#endif + +/* * Display an error to the client. */ #define HEADER_SIZE (1024 * 8) @@ -60,13 +88,13 @@ int httperr(struct conn_s *connptr, int err, const char *msg) char timebuf[30]; time_t global_time; - header_buffer = malloc(HEADER_SIZE); + header_buffer = safemalloc(HEADER_SIZE); if (!header_buffer) { log_message(LOG_ERR, "Could not allocate memory."); return -1; } - message_buffer = malloc(MAXBUFFSIZE); + message_buffer = safemalloc(MAXBUFFSIZE); if (!message_buffer) { log_message(LOG_ERR, "Could not allocate memory."); safefree(header_buffer); @@ -80,7 +108,7 @@ int httperr(struct conn_s *connptr, int err, const char *msg) snprintf(header_buffer, HEADER_SIZE - 1, headers, err, msg, PACKAGE, VERSION, timebuf, strlen(message_buffer)); output_size = strlen(message_buffer) + strlen(header_buffer); - connptr->output_message = malloc(output_size + 1); + connptr->output_message = safemalloc(output_size + 1); if (!connptr->output_message) { log_message(LOG_ERR, "Could not allocate memory."); safefree(header_buffer); diff --git a/src/utils.h b/src/utils.h index 6c22395..24b7758 100644 --- a/src/utils.h +++ b/src/utils.h @@ -1,4 +1,4 @@ -/* $Id: utils.h,v 1.6 2001-08-30 16:52:56 rjkaes Exp $ +/* $Id: utils.h,v 1.7 2001-09-08 18:55:58 rjkaes Exp $ * * See 'utils.h' for a detailed description. * @@ -34,4 +34,22 @@ extern size_t strlcat(char *dst, const char *src, size_t size); extern size_t strlcpy(char *dst, const char *src, size_t size); #endif /* HAVE_STRLCPY */ +/* + * The following is to allow for better memory checking. + */ +#ifndef NDEBUG + +extern void *debugging_calloc(size_t nmemb, size_t size, const char *file, unsigned long line); +extern void *debugging_malloc(size_t size, const char *file, unsigned long line); +extern void debugging_free(void *ptr, const char *file, unsigned long line); + +# define safecalloc(x, y) debugging_calloc(x, y, __FILE__, __LINE__) +# define safemalloc(x) debugging_malloc(x, __FILE__, __LINE__) +# define safefree(x) debugging_free(x, __FILE__, __LINE__) +#else +# define safecalloc(x, y) calloc(x, y) +# define safemalloc(x) malloc(x) +# define safefree(x) free(x) +#endif + #endif |