Added the debuggin_strdup() function and the associated safestrdup()
macro. Also, added asserts to the other debugging_* functions.
This commit is contained in:
1 parent
dc18888c83
commit
ce4687fbf9
3 files changed
+53
-5
No files matched your search
+38
-4
@@ -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
|
||||
* 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,
|
||||
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,
|
||||
line);
|
||||
return ptr;
|
||||
@@ -46,7 +51,11 @@ debugging_calloc(size_t nmemb, size_t size, const char *file,
|
||||
void *
|
||||
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);
|
||||
return ptr;
|
||||
}
|
||||
@@ -54,7 +63,12 @@ debugging_malloc(size_t size, const char *file, unsigned long line)
|
||||
void *
|
||||
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,
|
||||
file, line);
|
||||
return newptr;
|
||||
@@ -63,11 +77,31 @@ debugging_realloc(void *ptr, size_t size, const char *file, unsigned long line)
|
||||
void
|
||||
debugging_free(void *ptr, const char *file, unsigned long line)
|
||||
{
|
||||
assert(ptr != NULL);
|
||||
|
||||
fprintf(stderr, "{free: %p} %s:%lu\n", ptr, file, line);
|
||||
free(ptr);
|
||||
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
|
||||
|
||||
#define HEADER_SIZE (1024 * 8)
|
||||
|
||||
+5
-1
@@ -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.
|
||||
*
|
||||
@@ -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_realloc(void *ptr, size_t size, const char *file,
|
||||
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 safemalloc(x) debugging_malloc(x, __FILE__, __LINE__)
|
||||
# define saferealloc(x, y) debugging_realloc(x, y, __FILE__, __LINE__)
|
||||
# define safefree(x) debugging_free(x, __FILE__, __LINE__)
|
||||
# define safestrdup(x) debugging_strdup(x, __FILE__, __LINE__)
|
||||
#else
|
||||
# define safecalloc(x, y) calloc(x, y)
|
||||
# define safemalloc(x) malloc(x)
|
||||
# define saferealloc(x, y) realloc(x, y)
|
||||
# define safefree(x) free(x)
|
||||
# define safestrdup(x) strdup(x)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user