Better debugging for NMEM routines.
authorAdam Dickmeiss <adam@indexdata.dk>
Tue, 13 Jul 1999 13:28:24 +0000 (13:28 +0000)
committerAdam Dickmeiss <adam@indexdata.dk>
Tue, 13 Jul 1999 13:28:24 +0000 (13:28 +0000)
util/Makefile.in
util/dmalloc.c [deleted file]
util/nmem.c

index 9fbf236..9cbece9 100644 (file)
@@ -1,7 +1,7 @@
 # Copyright (C) 1994-1998, Index Data
 # All rights reserved.
 # Sebastian Hammer, Adam Dickmeiss
-# $Id: Makefile.in,v 1.7 1999-06-09 09:43:31 adam Exp $
+# $Id: Makefile.in,v 1.8 1999-07-13 13:28:24 adam Exp $
 
 SHELL=/bin/sh
 
@@ -17,8 +17,7 @@ LIBDIR=../lib
 DEFS=$(INCLUDE) $(CDEFS)
 LIB=$(LIBDIR)/libutil.a
 PO = options.o log.o marcdisp.o oid.o wrbuf.o nmemsdup.o \
-   xmalloc.o readconf.o tpath.o nmem.o matchstr.o \
-   atoin.o # dmalloc.o 
+   xmalloc.o readconf.o tpath.o nmem.o matchstr.o atoin.o 
 
 all: $(LIB) marcdump
 
diff --git a/util/dmalloc.c b/util/dmalloc.c
deleted file mode 100644 (file)
index 78603c8..0000000
+++ /dev/null
@@ -1,97 +0,0 @@
-/*
- * Copyright (c) 1995, Index Data
- * See the file LICENSE for details.
- * Sebastian Hammer, Adam Dickmeiss
- *
- * $Log: dmalloc.c,v $
- * Revision 1.8  1995-09-29 17:12:33  quinn
- * Smallish
- *
- * Revision 1.7  1995/09/29  17:01:50  quinn
- * More Windows work
- *
- * Revision 1.6  1995/09/27  15:03:02  quinn
- * Modified function heads & prototypes.
- *
- * Revision 1.5  1995/05/16  08:51:10  quinn
- * License, documentation, and memory fixes
- *
- * Revision 1.4  1995/05/15  13:25:13  quinn
- * Fixed memory bug.
- *
- * Revision 1.3  1995/05/15  11:56:55  quinn
- * Debuggng & adjustments.
- *
- * Revision 1.2  1995/04/10  10:23:50  quinn
- * Fixes.
- *
- * Revision 1.1  1995/03/27  08:35:17  quinn
- * Created util library
- * Added memory debugging module. Imported options-manager
- *
- *
- */
-
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <yconfig.h>
-
-static const unsigned char head[] = {44, 33, 22, 11};
-static const unsigned char tail[] = {11, 22, 33, 44};
-static const unsigned char freed[] = {99, 99, 99, 99};
-
-void *d_malloc(char *file, int line, int nbytes)
-{
-    char *res;
-    int long len;
-
-    if (!(res = malloc(nbytes + 3 * sizeof(long))))
-        return 0;
-    fprintf(stderr, "---d_malloc, '%s':%d, %d->%p\n",
-        file, line, nbytes, res + 2 * sizeof(long));
-    len = nbytes;
-    memcpy(res, &head, sizeof(long));
-    memcpy(res + sizeof(long), &len, sizeof(long));
-    res += 2 * sizeof(long);
-    memcpy(res + nbytes, &tail, sizeof(long));
-    return res;
-}
-
-void d_free(char *file, int line, char *ptr)
-{
-    long len;
-
-    if (memcmp(&head, ptr - 2 * sizeof(long), sizeof(long)))
-        abort();
-    memcpy(ptr - 2 * sizeof(long), &freed, sizeof(long));
-    memcpy(&len, ptr - sizeof(long), sizeof(long));
-    if (memcmp(ptr + len, &tail, sizeof(long)))
-        abort();
-    fprintf(stderr, "---d_free, '%s':%d, %p (%d)\n",
-        file, line, ptr, len);
-    free(ptr - 2 * sizeof(long));
-    return;
-}
-
-void *d_realloc(char *file, int line, char *ptr, int nbytes)
-{
-    long len, nlen = nbytes;
-    char *p = ptr;
-    char *r;
-
-    if (memcmp(&head, ptr - 2 * sizeof(long), sizeof(long)))
-        abort();
-    memcpy(&len, ptr - sizeof(long), sizeof(long));
-    if (memcmp(ptr + len, &tail, sizeof(long)))
-        abort();
-    if (!(r = realloc(ptr - 2 * sizeof(long), nbytes + 3 * sizeof(long))))
-        return 0;
-    fprintf(stderr, "---d_realloc, '%s':%d, %d->%d, %p->%p\n",
-        file, line, len, nbytes, p, r + 2 * sizeof(long));
-    memcpy(r, &head, sizeof(long));
-    memcpy(r + sizeof(long), &nlen, sizeof(long));
-    r += 2 * sizeof(long);
-    memcpy(r + nbytes, &tail, sizeof(long));
-    return r;
-}
index d72cf70..16d8421 100644 (file)
@@ -4,7 +4,10 @@
  * Sebastian Hammer, Adam Dickmeiss
  *
  * $Log: nmem.c,v $
- * Revision 1.16  1999-03-31 11:18:25  adam
+ * Revision 1.17  1999-07-13 13:28:25  adam
+ * Better debugging for NMEM routines.
+ *
+ * Revision 1.16  1999/03/31 11:18:25  adam
  * Implemented odr_strdup. Added Reference ID to backend server API.
  *
  * Revision 1.15  1999/02/11 09:10:26  adam
@@ -67,6 +70,7 @@
  */
 
 #include <assert.h>
+#include <string.h>
 #include <xmalloc.h>
 #include <nmem.h>
 #include <log.h>
@@ -102,6 +106,17 @@ static nmem_control *cfreelist = NULL;
 static int nmem_active_no = 0;
 static int nmem_init_flag = 0;
 
+#if NMEM_DEBUG
+struct nmem_debug {
+    void *p;
+    char file[40];
+    int line;
+    struct nmem_debug *next;
+};
+  
+struct nmem_debug *nmem_debug_list = 0;  
+#endif
+
 static void free_block(nmem_block *p)
 {  
     p->next = freelist;
@@ -111,6 +126,18 @@ static void free_block(nmem_block *p)
 #endif
 }
 
+#if NMEM_DEBUG
+void nmem_print_list (void)
+{
+    struct nmem_debug *p;
+
+    logf (LOG_DEBUG, "nmem print list");
+    NMEM_ENTER;
+    for (p = nmem_debug_list; p; p = p->next)
+       logf (LOG_DEBUG, " %s:%d p=%p", p->file, p->line, p->p);
+    NMEM_LEAVE;
+}
+#endif
 /*
  * acquire a block with a minimum of size free bytes.
  */
@@ -219,6 +246,9 @@ NMEM nmem_create(void)
 #endif
 {
     NMEM r;
+#if NMEM_DEBUG
+    struct nmem_debug *debug_p;
+#endif
     
     NMEM_ENTER;
     nmem_active_no++;
@@ -231,11 +261,29 @@ NMEM nmem_create(void)
 
 #if NMEM_DEBUG
     logf (LOG_DEBUG, "%s:%d: nmem_create %d p=%p", file, line,
-                     nmem_active_no-1, r);
+                     nmem_active_no, r);
 #endif
     r->blocks = 0;
     r->total = 0;
     r->next = 0;
+
+#if NMEM_DEBUG
+    for (debug_p = nmem_debug_list; debug_p; debug_p = debug_p->next)
+       if (debug_p->p == r)
+       {
+           logf (LOG_FATAL, "multi used block in nmem");
+           abort ();
+       }
+    debug_p = xmalloc (sizeof(*debug_p));
+    strncpy (debug_p->file, file, sizeof(debug_p->file)-1);
+    debug_p->file[sizeof(debug_p->file)-1] = '\0';
+    debug_p->line = line;
+    debug_p->p = r;
+    debug_p->next = nmem_debug_list;
+    nmem_debug_list = debug_p;
+
+    nmem_print_list();
+#endif
     return r;
 }
 
@@ -245,18 +293,41 @@ void nmem_destroy_f(const char *file, int line, NMEM n)
 void nmem_destroy(NMEM n)
 #endif
 {
+#if NMEM_DEBUG
+    struct nmem_debug **debug_p;
+    int ok = 0;
+#endif
     if (!n)
        return;
+    
+#if NMEM_DEBUG
+    logf (LOG_DEBUG, "%s:%d: nmem_destroy %d p=%p", file, line,
+                     nmem_active_no-1, n);
+    NMEM_ENTER;
+    for (debug_p = &nmem_debug_list; *debug_p; debug_p = &(*debug_p)->next)
+       if ((*debug_p)->p == n)
+       {
+           struct nmem_debug *debug_save = *debug_p;
+           *debug_p = (*debug_p)->next;
+           xfree (debug_save);
+           ok = 1;
+           break;
+       }
+    NMEM_LEAVE;
+    nmem_print_list();
+    if (!ok)
+    {
+       logf (LOG_WARN, "%s:%d destroying unallocated nmem block p=%p",
+             file, line, n);
+       return;
+    }
+#endif
     nmem_reset(n);
     NMEM_ENTER;
     nmem_active_no--;
     n->next = cfreelist;
     cfreelist = n;
     NMEM_LEAVE;
-#if NMEM_DEBUG
-    logf (LOG_DEBUG, "%s:%d: nmem_destroy %d p=%p", file, line,
-                     nmem_active_no, n);
-#endif
 }
 
 void nmem_transfer (NMEM dst, NMEM src)