/*
- * $Id: util.c,v 1.40 1998/02/02 21:16:15 wessels Exp $
+ * $Id: util.c,v 1.41 1998/02/06 00:54:59 wessels Exp $
*
* DEBUG:
* AUTHOR: Harvest Derived
+#if XMALLOC_TRACE
+char *xmalloc_file="";
+int xmalloc_line=0;
+char *xmalloc_func="";
+static int xmalloc_count=0;
+#undef xmalloc
+#undef xfree
+#undef xxfree
+#undef xrealloc
+#undef xcalloc
+#undef xstrdup
+#endif
+
#if XMALLOC_DEBUG
#define DBG_ARRY_SZ (1<<10)
#define DBG_ARRY_BKTS (1<<8)
static void *malloc_ptrs[DBG_ARRY_BKTS][DBG_ARRY_SZ];
static int malloc_size[DBG_ARRY_BKTS][DBG_ARRY_SZ];
+#if XMALLOC_TRACE
+static void *malloc_file[DBG_ARRY_BKTS][DBG_ARRY_SZ];
+static int malloc_line[DBG_ARRY_BKTS][DBG_ARRY_SZ];
+static int malloc_count[DBG_ARRY_BKTS][DBG_ARRY_SZ];
+#endif
static int dbg_initd = 0;
static int B = 0;
static int I = 0;
for (I = 0; I < DBG_ARRY_SZ; I++) {
malloc_ptrs[B][I] = NULL;
malloc_size[B][I] = 0;
+#if XMALLOC_TRACE
+ malloc_file[B][I] = NULL;
+ malloc_line[B][I] = 0;
+ malloc_count[B][I] = 0;
+#endif
}
}
dbg_initd = 1;
continue;
malloc_ptrs[B][I] = NULL;
malloc_size[B][I] = 0;
+#if XMALLOC_TRACE
+ malloc_file[B][I] = NULL;
+ malloc_line[B][I] = 0;
+ malloc_count[B][I] = 0;
+#endif
break;
}
if (I == DBG_ARRY_SZ) {
continue;
malloc_ptrs[B][I] = p;
malloc_size[B][I] = (int) sz;
+#if XMALLOC_TRACE
+ malloc_file[B][I] = xmalloc_file;
+ malloc_line[B][I] = xmalloc_line;
+ malloc_count[B][I] = xmalloc_count;
+#endif
break;
}
if (I == DBG_ARRY_SZ)
}
#endif
-#if XMALLOC_COUNT && !HAVE_MALLOCBLKSIZE
+#if XMALLOC_TRACE && !HAVE_MALLOCBLKSIZE
int
mallocblksize(void *p)
{
}
#endif
-#ifdef XMALLOC_COUNT
+#ifdef XMALLOC_TRACE
+static char *
+malloc_file_name(void *p)
+{
+ B = (((int) p) >> 4) & 0xFF;
+ for (I = 0; I < DBG_ARRY_SZ; I++) {
+ if (malloc_ptrs[B][I] == p)
+ return malloc_file[B][I];
+ }
+ return 0;
+}
+int
+malloc_line_number(void *p)
+{
+ B = (((int) p) >> 4) & 0xFF;
+ for (I = 0; I < DBG_ARRY_SZ; I++) {
+ if (malloc_ptrs[B][I] == p)
+ return malloc_line[B][I];
+ }
+ return 0;
+}
+int
+malloc_number(void *p)
+{
+ B = (((int) p) >> 4) & 0xFF;
+ for (I = 0; I < DBG_ARRY_SZ; I++) {
+ if (malloc_ptrs[B][I] == p)
+ return malloc_count[B][I];
+ }
+ return 0;
+}
static void
-xmalloc_count(void *p, int sign)
+xmalloc_trace(void *p, int sign)
{
int statMemoryAccounted();
static size_t last_total = 0, last_accounted = 0, last_mallinfo = 0;
static size_t total = 0;
sz = mallocblksize(p) * sign;
total += sz;
- fprintf(stderr, "xmalloc_count=%7d/%9d accounted=%7d/%9d mallinfo=%7d%9d\n",
+ xmalloc_count += sign>0;
+ fprintf(stderr, "%c%8p size=%5d/%d acc=%5d/%d mallinfo=%5d/%d %s:%d %s",
+ sign>0 ? '+':'-',p,
(int) total - last_total, (int) total,
(int) accounted - last_accounted, (int) accounted,
- (int) mi - last_mallinfo, (int) mi);
+ (int) mi - last_mallinfo, (int) mi,
+ xmalloc_file, xmalloc_line, xmalloc_func);
+ if (sign<0)
+ fprintf(stderr," (%d %s:%d)\n",malloc_number(p),malloc_file_name(p),malloc_line_number(p));
+ else
+ fprintf(stderr," %d\n",xmalloc_count);
last_total = total;
last_accounted = accounted;
last_mallinfo = mi;
}
-#endif /* XMALLOC_COUNT */
+#endif /* XMALLOC_TRACE */
/*
* xmalloc() - same as malloc(3). Used for portability.
#if XMALLOC_STATISTICS
malloc_stat(sz);
#endif
-#if XMALLOC_COUNT
- xmalloc_count(p, 1);
+#if XMALLOC_TRACE
+ xmalloc_trace(p, 1);
#endif
return (p);
}
void
xfree(void *s)
{
-#if XMALLOC_COUNT
- xmalloc_count(s, -1);
+#if XMALLOC_TRACE
+ xmalloc_trace(s, -1);
#endif
#if XMALLOC_DEBUG
check_free(s);
void
xxfree(void *s)
{
-#if XMALLOC_COUNT
- xmalloc_count(s, -1);
+#if XMALLOC_TRACE
+ xmalloc_trace(s, -1);
#endif
#if XMALLOC_DEBUG
check_free(s);
{
static void *p;
-#if XMALLOC_COUNT
- xmalloc_count(s, -1);
+#if XMALLOC_TRACE
+ xmalloc_trace(s, -1);
#endif
if (sz < 1)
#if XMALLOC_STATISTICS
malloc_stat(sz);
#endif
-#if XMALLOC_COUNT
- xmalloc_count(p, 1);
+#if XMALLOC_TRACE
+ xmalloc_trace(p, 1);
#endif
return (p);
}
#if XMALLOC_STATISTICS
malloc_stat(sz);
#endif
-#if XMALLOC_COUNT
- xmalloc_count(p, 1);
+#if XMALLOC_TRACE
+ xmalloc_trace(p, 1);
#endif
return (p);
}