Added the debuggin_strdup() function and the associated safestrdup()

macro.  Also, added asserts to the other debugging_* functions.
master
Robert James Kaes 2002-04-18 17:49:14 +00:00
parent dc18888c83
commit ce4687fbf9
3 changed files with 53 additions and 5 deletions

View File

@ -1,5 +1,15 @@
2002-04-18 Robert James Kaes <rjkaes@flarenet.com> 2002-04-18 Robert James Kaes <rjkaes@flarenet.com>
* src/utils.c (debugging_strdup): Added this function to be used
by the safestrdup() macro to replace all the calls to strdup().
This should allow better tracking of the memory usage.
Also, all the debugging_* functions have had asserts added to them
to hopefully improve the quality of the code.
* src/reqs.c (get_all_headers): Fixed a memory leak since I was
not freeing the header variable, even though the hashmap makes a
copy of it. Thanks to Petr Lampa for finding this one.
* src/tinyproxy.c (takesig): Moved the filter_destroy() code out * src/tinyproxy.c (takesig): Moved the filter_destroy() code out
of the signal handler and placed it inside of main(). Same of the signal handler and placed it inside of main(). Same
reasoning as the rotate_log_files() changes below. reasoning as the rotate_log_files() changes below.

View File

@ -1,4 +1,4 @@
/* $Id: utils.c,v 1.26 2002-04-18 16:57:06 rjkaes Exp $ /* $Id: utils.c,v 1.27 2002-04-18 17:49:14 rjkaes Exp $
* *
* Misc. routines which are used by the various functions to handle strings * 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, * and memory allocation and pretty much anything else we can think of. Also,
@ -37,7 +37,12 @@ void *
debugging_calloc(size_t nmemb, size_t size, const char *file, debugging_calloc(size_t nmemb, size_t size, const char *file,
unsigned long line) unsigned long line)
{ {
void *ptr = calloc(nmemb, size); void *ptr;
assert(nmemb > 0);
assert(size > 0);
ptr = calloc(nmemb, size);
fprintf(stderr, "{calloc: %p:%u x %u} %s:%lu\n", ptr, nmemb, size, file, fprintf(stderr, "{calloc: %p:%u x %u} %s:%lu\n", ptr, nmemb, size, file,
line); line);
return ptr; return ptr;
@ -46,7 +51,11 @@ debugging_calloc(size_t nmemb, size_t size, const char *file,
void * void *
debugging_malloc(size_t size, const char *file, unsigned long line) debugging_malloc(size_t size, const char *file, unsigned long line)
{ {
void *ptr = malloc(size); void *ptr;
assert(size > 0);
ptr = malloc(size);
fprintf(stderr, "{malloc: %p:%u} %s:%lu\n", ptr, size, file, line); fprintf(stderr, "{malloc: %p:%u} %s:%lu\n", ptr, size, file, line);
return ptr; return ptr;
} }
@ -54,7 +63,12 @@ debugging_malloc(size_t size, const char *file, unsigned long line)
void * void *
debugging_realloc(void *ptr, size_t size, const char *file, unsigned long line) debugging_realloc(void *ptr, size_t size, const char *file, unsigned long line)
{ {
void *newptr = realloc(ptr, size); void *newptr;
assert(ptr != NULL);
assert(size > 0);
newptr = realloc(ptr, size);
fprintf(stderr, "{realloc: %p -> %p:%u} %s:%lu\n", ptr, newptr, size, fprintf(stderr, "{realloc: %p -> %p:%u} %s:%lu\n", ptr, newptr, size,
file, line); file, line);
return newptr; return newptr;
@ -63,11 +77,31 @@ debugging_realloc(void *ptr, size_t size, const char *file, unsigned long line)
void void
debugging_free(void *ptr, const char *file, unsigned long line) debugging_free(void *ptr, const char *file, unsigned long line)
{ {
assert(ptr != NULL);
fprintf(stderr, "{free: %p} %s:%lu\n", ptr, file, line); fprintf(stderr, "{free: %p} %s:%lu\n", ptr, file, line);
free(ptr); free(ptr);
return; return;
} }
char*
debugging_strdup(const char* s, const char* file, unsigned long line)
{
char* ptr;
size_t len;
assert(s != NULL);
len = strlen(s) + 1;
ptr = malloc(len);
if (!ptr)
return NULL;
memcpy(ptr, s, len);
fprintf(stderr, "{strdup: %p:%u} %s:%lu\n", ptr, len, file, line);
return ptr;
}
#endif #endif
#define HEADER_SIZE (1024 * 8) #define HEADER_SIZE (1024 * 8)

View File

@ -1,4 +1,4 @@
/* $Id: utils.h,v 1.15 2002-04-18 16:57:06 rjkaes Exp $ /* $Id: utils.h,v 1.16 2002-04-18 17:49:14 rjkaes Exp $
* *
* See 'utils.h' for a detailed description. * See 'utils.h' for a detailed description.
* *
@ -63,16 +63,20 @@ extern void *debugging_malloc(size_t size, const char *file,
extern void debugging_free(void *ptr, const char *file, unsigned long line); extern void debugging_free(void *ptr, const char *file, unsigned long line);
extern void *debugging_realloc(void *ptr, size_t size, const char *file, extern void *debugging_realloc(void *ptr, size_t size, const char *file,
unsigned long line); unsigned long line);
extern char *debugging_strdup(const char* s, const char* file,
unsigned long line);
# define safecalloc(x, y) debugging_calloc(x, y, __FILE__, __LINE__) # define safecalloc(x, y) debugging_calloc(x, y, __FILE__, __LINE__)
# define safemalloc(x) debugging_malloc(x, __FILE__, __LINE__) # define safemalloc(x) debugging_malloc(x, __FILE__, __LINE__)
# define saferealloc(x, y) debugging_realloc(x, y, __FILE__, __LINE__) # define saferealloc(x, y) debugging_realloc(x, y, __FILE__, __LINE__)
# define safefree(x) debugging_free(x, __FILE__, __LINE__) # define safefree(x) debugging_free(x, __FILE__, __LINE__)
# define safestrdup(x) debugging_strdup(x, __FILE__, __LINE__)
#else #else
# define safecalloc(x, y) calloc(x, y) # define safecalloc(x, y) calloc(x, y)
# define safemalloc(x) malloc(x) # define safemalloc(x) malloc(x)
# define saferealloc(x, y) realloc(x, y) # define saferealloc(x, y) realloc(x, y)
# define safefree(x) free(x) # define safefree(x) free(x)
# define safestrdup(x) strdup(x)
#endif #endif
#endif #endif