From 64d911da4c79982db410fc56e74e43aedf34f937 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Tue, 13 Jul 1999 13:24:53 +0000 Subject: [PATCH] Updated memory debugging memory allocatation routines. --- include/xmalloc.h | 28 +++---- util/xmalloc.c | 219 +++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 218 insertions(+), 29 deletions(-) diff --git a/include/xmalloc.h b/include/xmalloc.h index a2758c6..ac77917 100644 --- a/include/xmalloc.h +++ b/include/xmalloc.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 1995-1998, Index Data. + * Copyright (c) 1995-1999, Index Data. * * Permission to use, copy, modify, distribute, and sell this software and * its documentation, in whole or in part, for any purpose, is hereby granted, @@ -24,7 +24,10 @@ * OF THIS SOFTWARE. * * $Log: xmalloc.h,v $ - * Revision 1.5 1998-07-20 12:36:22 adam + * Revision 1.6 1999-07-13 13:24:53 adam + * Updated memory debugging memory allocatation routines. + * + * Revision 1.5 1998/07/20 12:36:22 adam * Minor changes. * * Revision 1.4 1997/10/31 12:20:08 adam @@ -79,27 +82,24 @@ #include -#define TRACE_XMALLOC 0 - -#ifndef TRACE_XMALLOC -#define TRACE_XMALLOC 0 -#endif - #ifdef __cplusplus extern "C" { #endif -YAZ_EXPORT void *xrealloc_f (void *o, size_t size, char *file, int line); -YAZ_EXPORT void *xmalloc_f (size_t size, char *file, int line); -YAZ_EXPORT void *xcalloc_f (size_t nmemb, size_t size, char *file, int line); -YAZ_EXPORT char *xstrdup_f (const char *, char *file, int line); -YAZ_EXPORT void xfree_f (void *p, char *file, int line); - #define xrealloc(o, x) xrealloc_f(o, x, __FILE__, __LINE__) #define xmalloc(x) xmalloc_f(x, __FILE__, __LINE__) #define xcalloc(x,y) xcalloc_f(x,y, __FILE__, __LINE__) #define xfree(x) xfree_f(x, __FILE__, __LINE__) #define xstrdup(s) xstrdup_f(s, __FILE__, __LINE__) +#define xmalloc_trav(s) xmalloc_trav_f(s, __FILE__, __LINE__) + +YAZ_EXPORT void *xrealloc_f (void *o, size_t size, const char *file, int line); +YAZ_EXPORT void *xmalloc_f (size_t size, const char *file, int line); +YAZ_EXPORT void *xcalloc_f (size_t nmemb, size_t size, + const char *file, int line); +YAZ_EXPORT char *xstrdup_f (const char *p, const char *file, int line); +YAZ_EXPORT void xfree_f (void *p, const char *file, int line); +YAZ_EXPORT void xmalloc_trav_f(const char *s, const char *file, int line); #ifdef __cplusplus } diff --git a/util/xmalloc.c b/util/xmalloc.c index 4264b24..d4aa919 100644 --- a/util/xmalloc.c +++ b/util/xmalloc.c @@ -1,10 +1,13 @@ /* - * Copyright (C) 1994, Index Data I/S + * Copyright (C) 1994-1999, Index Data * All rights reserved. * Sebastian Hammer, Adam Dickmeiss * * $Log: xmalloc.c,v $ - * Revision 1.6 1998-02-11 11:53:36 adam + * Revision 1.7 1999-07-13 13:24:53 adam + * Updated memory debugging memory allocatation routines. + * + * Revision 1.6 1998/02/11 11:53:36 adam * Changed code so that it compiles as C++. * * Revision 1.5 1997/10/31 12:20:09 adam @@ -50,11 +53,198 @@ #include #include -#include -void *xrealloc_f (void *o, size_t size, char *file, int line) +#define TRACE_XMALLOC 0 + +#if TRACE_XMALLOC > 1 + +static const unsigned char head[] = {44, 33, 22, 11}; +static const unsigned char tail[] = {11, 22, 33, 44}; +static const unsigned char freed[] = {11, 22, 33, 44}; + +struct dmalloc_info { + int len; + char file[16]; + int line; + struct dmalloc_info *next; + struct dmalloc_info *prev; +}; + +struct dmalloc_info *dmalloc_list = 0; + +void *xmalloc_d(size_t nbytes, const char *file, int line) +{ + char *res; + struct dmalloc_info *dinfo; + + if (!(res = (char*) malloc(nbytes + sizeof(*dinfo)+8*sizeof(char)))) + return 0; + dinfo = (struct dmalloc_info *) res; + strncpy (dinfo->file, file, sizeof(dinfo->file)-1); + dinfo->file[sizeof(dinfo->file)-1] = '\0'; + dinfo->line = line; + dinfo->len = nbytes; + + dinfo->prev = 0; + dinfo->next = dmalloc_list; + if (dinfo->next) + dinfo->next->prev = dinfo; + dmalloc_list = dinfo; + + memcpy(res + sizeof(*dinfo), head, 4*sizeof(char)); + res += sizeof(*dinfo) + 4*sizeof(char); + memcpy(res + nbytes, tail, 4*sizeof(char)); + return res; +} + +void xfree_d(void *ptr, const char *file, int line) +{ + struct dmalloc_info *dinfo; + + if (!ptr) + return; + dinfo = (struct dmalloc_info *) + ((char*)ptr - 4*sizeof(char) - sizeof(*dinfo)); + if (memcmp(head, (char*) ptr - 4*sizeof(char), 4*sizeof(char))) + { + logf(LOG_FATAL, "xfree_d bad head, %s:%d, %p", file, line, ptr); + abort(); + } + if (memcmp((char*) ptr + dinfo->len, tail, 4*sizeof(char))) + { + logf(LOG_FATAL, "xfree_d bad tail, %s:%d, %p", file, line, ptr); + abort(); + } + if (dinfo->prev) + dinfo->prev->next = dinfo->next; + else + dmalloc_list = dinfo->next; + if (dinfo->next) + dinfo->next->prev = dinfo->prev; + memcpy ((char*) ptr - 4*sizeof(char), freed, 4*sizeof(char)); + free(dinfo); + return; +} + +void *xrealloc_d(void *p, size_t nbytes, const char *file, int line) +{ + struct dmalloc_info *dinfo; + char *ptr = (char*) p; + char *res; + + if (!ptr) + { + if (!nbytes) + return 0; + res = (char *) malloc(nbytes + sizeof(*dinfo) + 8*sizeof(char)); + } + else + { + if (memcmp(head, ptr - 4*sizeof(char), 4*sizeof(char))) + { + logf(LOG_FATAL, "xrealloc_d bad head, %s:%d, %p", file, line, ptr); + abort(); + } + dinfo = (struct dmalloc_info *) (ptr-4*sizeof(char) - sizeof(*dinfo)); + if (memcmp(ptr + dinfo->len, tail, 4*sizeof(char))) + { + logf(LOG_FATAL, "xrealloc_d bad tail, %s:%d, %p", file, line, ptr); + abort(); + } + if (dinfo->prev) + dinfo->prev->next = dinfo->next; + else + dmalloc_list = dinfo->next; + if (dinfo->next) + dinfo->next->prev = dinfo->prev; + + if (!nbytes) + { + free (dinfo); + return 0; + } + res = (char *) + realloc(dinfo, nbytes + sizeof(*dinfo) + 8*sizeof(char)); + } + if (!res) + return 0; + dinfo = (struct dmalloc_info *) res; + strncpy (dinfo->file, file, sizeof(dinfo->file)-1); + dinfo->file[sizeof(dinfo->file)-1] = '\0'; + dinfo->line = line; + dinfo->len = nbytes; + + dinfo->prev = 0; + dinfo->next = dmalloc_list; + if (dmalloc_list) + dmalloc_list->prev = dinfo; + dmalloc_list = dinfo; + + memcpy(res + sizeof(*dinfo), head, 4*sizeof(char)); + res += sizeof(*dinfo) + 4*sizeof(char); + memcpy(res + nbytes, tail, 4*sizeof(char)); + return res; +} + +void *xcalloc_d(size_t nmemb, size_t size, const char *file, int line) +{ + char *res; + struct dmalloc_info *dinfo; + size_t nbytes = nmemb * size; + + if (!(res = (char*) calloc(1, nbytes+sizeof(*dinfo)+8*sizeof(char)))) + return 0; + dinfo = (struct dmalloc_info *) res; + strncpy (dinfo->file, file, sizeof(dinfo->file)-1); + dinfo->file[sizeof(dinfo->file)-1] = '\0'; + dinfo->line = line; + dinfo->len = nbytes; + + dinfo->prev = 0; + dinfo->next = dmalloc_list; + if (dinfo->next) + dinfo->next->prev = dinfo; + dmalloc_list = dinfo; + + memcpy(res + sizeof(*dinfo), head, 4*sizeof(char)); + res += sizeof(*dinfo) + 4*sizeof(char); + memcpy(res + nbytes, tail, 4*sizeof(char)); + return res; +} + +void xmalloc_trav_d(const char *file, int line) +{ + size_t size = 0; + struct dmalloc_info *dinfo = dmalloc_list; + + logf (LOG_LOG, "malloc_trav %s:%d", file, line); + while (dinfo) + { + logf (LOG_LOG, " %20s:%d p=%p size=%d", dinfo->file, dinfo->line, + dinfo+sizeof(*dinfo)+4*sizeof(char), dinfo->len); + size += dinfo->len; + dinfo = dinfo->next; + } + logf (LOG_LOG, "total bytes %ld", (long) size); +} + +#else +/* ! TRACE_XMALLOC */ +#define xrealloc_d(o, x, f, l) realloc(o, x) +#define xmalloc_d(x, f, l) malloc(x) +#define xcalloc_d(x,y, f, l) calloc(x,y) +#define xfree_d(x, f, l) free(x) +#define xmalloc_trav_d(f, l) +#endif + +void xmalloc_trav_f(const char *s, const char *file, int line) { - void *p = realloc (o, size); + xmalloc_trav_d(file, line); +} + +void *xrealloc_f (void *o, size_t size, const char *file, int line) +{ + void *p = xrealloc_d (o, size, file, line); #if TRACE_XMALLOC logf (LOG_DEBUG, @@ -68,10 +258,10 @@ void *xrealloc_f (void *o, size_t size, char *file, int line) return p; } -void *xmalloc_f (size_t size, char *file, int line) +void *xmalloc_f (size_t size, const char *file, int line) { - void *p = malloc (size); - + void *p = xmalloc_d (size, file, line); + #if TRACE_XMALLOC logf (LOG_DEBUG, "%s:%d: xmalloc(s=%d) %p", file, line, size, p); #endif @@ -83,9 +273,9 @@ void *xmalloc_f (size_t size, char *file, int line) return p; } -void *xcalloc_f (size_t nmemb, size_t size, char *file, int line) +void *xcalloc_f (size_t nmemb, size_t size, const char *file, int line) { - void *p = calloc (nmemb, size); + void *p = xcalloc_d (nmemb, size, file, line); #if TRACE_XMALLOC logf (LOG_DEBUG, "%s:%d: xcalloc(s=%d) %p", file, line, size, p); #endif @@ -97,9 +287,9 @@ void *xcalloc_f (size_t nmemb, size_t size, char *file, int line) return p; } -char *xstrdup_f (const char *s, char *file, int line) +char *xstrdup_f (const char *s, const char *file, int line) { - char *p = (char *)xmalloc (strlen(s)+1); + char *p = (char *)xmalloc_d (strlen(s)+1, file, line); #if TRACE_XMALLOC logf (LOG_DEBUG, "%s:%d: xstrdup(s=%d) %p", file, line, strlen(s)+1, p); #endif @@ -107,8 +297,7 @@ char *xstrdup_f (const char *s, char *file, int line) return p; } - -void xfree_f(void *p, char *file, int line) +void xfree_f(void *p, const char *file, int line) { if (!p) return ; @@ -116,5 +305,5 @@ void xfree_f(void *p, char *file, int line) if (p) logf (LOG_DEBUG, "%s:%d: xfree %p", file, line, p); #endif - free(p); + xfree_d(p, file, line); } -- 1.7.10.4