Renamed logf function to yaz_log. Removed VC++ project files.
[yaz-moved-to-github.git] / util / nmem.c
index 4f45847..61fe862 100644 (file)
@@ -1,10 +1,33 @@
 /*
 /*
- * Copyright (c) 1995-1998, Index Data.
+ * Copyright (c) 1995-1999, Index Data.
  * See the file LICENSE for details.
  * Sebastian Hammer, Adam Dickmeiss
  *
  * $Log: nmem.c,v $
  * See the file LICENSE for details.
  * Sebastian Hammer, Adam Dickmeiss
  *
  * $Log: nmem.c,v $
- * Revision 1.11  1998-08-21 14:13:36  adam
+ * Revision 1.18  1999-08-27 09:40:32  adam
+ * Renamed logf function to yaz_log. Removed VC++ project files.
+ *
+ * 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
+ * Function nmem_init only mandatory on Windows.
+ *
+ * Revision 1.14  1999/02/02 13:57:40  adam
+ * Uses preprocessor define WIN32 instead of WINDOWS to build code
+ * for Microsoft WIN32.
+ *
+ * Revision 1.13  1998/10/19 15:24:21  adam
+ * New nmem utility, nmem_transfer, that transfer blocks from one
+ * NMEM to another.
+ *
+ * Revision 1.12  1998/10/13 16:00:18  adam
+ * Implemented nmem_critical_{enter,leave}.
+ *
+ * Revision 1.11  1998/08/21 14:13:36  adam
  * Added GNU Configure script to build Makefiles.
  *
  * Revision 1.10  1998/07/20 12:35:57  adam
  * Added GNU Configure script to build Makefiles.
  *
  * Revision 1.10  1998/07/20 12:35:57  adam
  * allocation. Evemtually we'll put in something better.
  */
 
  * allocation. Evemtually we'll put in something better.
  */
 
+#include <assert.h>
+#include <string.h>
 #include <xmalloc.h>
 #include <nmem.h>
 #include <log.h>
 #include <xmalloc.h>
 #include <nmem.h>
 #include <log.h>
-#ifdef WINDOWS
+#ifdef WIN32
 #include <windows.h>
 #elif _REENTRANT
 
 #include <windows.h>
 #elif _REENTRANT
 
@@ -66,7 +91,7 @@
 
 #define NMEM_CHUNK (4*1024)
 
 
 #define NMEM_CHUNK (4*1024)
 
-#ifdef WINDOWS
+#ifdef WIN32
 static CRITICAL_SECTION critical_section;
 #define NMEM_ENTER EnterCriticalSection(&critical_section)
 #define NMEM_LEAVE LeaveCriticalSection(&critical_section)
 static CRITICAL_SECTION critical_section;
 #define NMEM_ENTER EnterCriticalSection(&critical_section)
 #define NMEM_LEAVE LeaveCriticalSection(&critical_section)
@@ -82,16 +107,40 @@ static pthread_mutex_t nmem_mutex = PTHREAD_MUTEX_INITIALIZER;
 static nmem_block *freelist = NULL;        /* "global" freelists */
 static nmem_control *cfreelist = NULL;
 static int nmem_active_no = 0;
 static nmem_block *freelist = NULL;        /* "global" freelists */
 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;
     freelist = p;
 #if NMEM_DEBUG
 
 static void free_block(nmem_block *p)
 {  
     p->next = freelist;
     freelist = p;
 #if NMEM_DEBUG
-    logf (LOG_DEBUG, "nmem free_block p=%p", p);
+    yaz_log (LOG_DEBUG, "nmem free_block p=%p", p);
 #endif
 }
 
 #endif
 }
 
+#if NMEM_DEBUG
+void nmem_print_list (void)
+{
+    struct nmem_debug *p;
+
+    yaz_log (LOG_DEBUG, "nmem print list");
+    NMEM_ENTER;
+    for (p = nmem_debug_list; p; p = p->next)
+       yaz_log (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.
  */
 /*
  * acquire a block with a minimum of size free bytes.
  */
@@ -100,7 +149,7 @@ static nmem_block *get_block(int size)
     nmem_block *r, *l;
 
 #if NMEM_DEBUG
     nmem_block *r, *l;
 
 #if NMEM_DEBUG
-    logf (LOG_DEBUG, "nmem get_block size=%d", size);
+    yaz_log (LOG_DEBUG, "nmem get_block size=%d", size);
 #endif
     for (r = freelist, l = 0; r; l = r, r = r->next)
        if (r->size >= size)
 #endif
     for (r = freelist, l = 0; r; l = r, r = r->next)
        if (r->size >= size)
@@ -108,7 +157,7 @@ static nmem_block *get_block(int size)
     if (r)
     {
 #if NMEM_DEBUG
     if (r)
     {
 #if NMEM_DEBUG
-       logf (LOG_DEBUG, "nmem get_block found free block p=%p", r);
+       yaz_log (LOG_DEBUG, "nmem get_block found free block p=%p", r);
 #endif
        if (l)
            l->next = r->next;
 #endif
        if (l)
            l->next = r->next;
@@ -122,7 +171,7 @@ static nmem_block *get_block(int size)
        if (get < size)
            get = size;
 #if NMEM_DEBUG
        if (get < size)
            get = size;
 #if NMEM_DEBUG
-       logf (LOG_DEBUG, "nmem get_block alloc new block size=%d", get);
+       yaz_log (LOG_DEBUG, "nmem get_block alloc new block size=%d", get);
 #endif
        r = (nmem_block *)xmalloc(sizeof(*r));
        r->buf = (char *)xmalloc(r->size = get);
 #endif
        r = (nmem_block *)xmalloc(sizeof(*r));
        r->buf = (char *)xmalloc(r->size = get);
@@ -136,7 +185,7 @@ void nmem_reset(NMEM n)
     nmem_block *t;
 
 #if NMEM_DEBUG
     nmem_block *t;
 
 #if NMEM_DEBUG
-    logf (LOG_DEBUG, "nmem_reset p=%p", n);
+    yaz_log (LOG_DEBUG, "nmem_reset p=%p", n);
 #endif
     if (!n)
        return;
 #endif
     if (!n)
        return;
@@ -147,8 +196,8 @@ void nmem_reset(NMEM n)
        n->blocks = n->blocks->next;
        free_block(t);
     }
        n->blocks = n->blocks->next;
        free_block(t);
     }
-    NMEM_LEAVE;
     n->total = 0;
     n->total = 0;
+    NMEM_LEAVE;
 }
 
 #if NMEM_DEBUG
 }
 
 #if NMEM_DEBUG
@@ -161,11 +210,17 @@ void *nmem_malloc(NMEM n, int size)
     char *r;
 
 #if NMEM_DEBUG
     char *r;
 
 #if NMEM_DEBUG
-    logf (LOG_DEBUG, "%s:%d: nmem_malloc p=%p size=%d", file, line,
+    yaz_log (LOG_DEBUG, "%s:%d: nmem_malloc p=%p size=%d", file, line,
                      n, size);
 #endif
     if (!n)
                      n, size);
 #endif
     if (!n)
+    {
+        abort ();
        return xmalloc(size);
        return xmalloc(size);
+    }
+#ifdef WIN32
+    assert (nmem_init_flag);
+#endif
     NMEM_ENTER;
     p = n->blocks;
     if (!p || p->size - p->top < size)
     NMEM_ENTER;
     p = n->blocks;
     if (!p || p->size - p->top < size)
@@ -194,6 +249,9 @@ NMEM nmem_create(void)
 #endif
 {
     NMEM r;
 #endif
 {
     NMEM r;
+#if NMEM_DEBUG
+    struct nmem_debug *debug_p;
+#endif
     
     NMEM_ENTER;
     nmem_active_no++;
     
     NMEM_ENTER;
     nmem_active_no++;
@@ -205,12 +263,30 @@ NMEM nmem_create(void)
     NMEM_LEAVE;
 
 #if NMEM_DEBUG
     NMEM_LEAVE;
 
 #if NMEM_DEBUG
-    logf (LOG_DEBUG, "%s:%d: nmem_create %d p=%p", file, line,
-                     nmem_active_no-1, r);
+    yaz_log (LOG_DEBUG, "%s:%d: nmem_create %d p=%p", file, line,
+                     nmem_active_no, r);
 #endif
     r->blocks = 0;
     r->total = 0;
     r->next = 0;
 #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)
+       {
+           yaz_log (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;
 }
 
     return r;
 }
 
@@ -220,23 +296,70 @@ void nmem_destroy_f(const char *file, int line, NMEM n)
 void nmem_destroy(NMEM n)
 #endif
 {
 void nmem_destroy(NMEM n)
 #endif
 {
+#if NMEM_DEBUG
+    struct nmem_debug **debug_p;
+    int ok = 0;
+#endif
     if (!n)
        return;
     if (!n)
        return;
+    
+#if NMEM_DEBUG
+    yaz_log (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)
+    {
+       yaz_log (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;
     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)
+{
+    nmem_block *t;
+    while ((t=src->blocks))
+    {
+       src->blocks = t->next;
+       t->next = dst->blocks;
+       dst->blocks = t;
+    }
+    dst->total += src->total;
+    src->total = 0;
+}
+
+void nmem_critical_enter (void)
+{
+    NMEM_ENTER;
+}
+
+void nmem_critical_leave (void)
+{
+    NMEM_LEAVE;
 }
 
 void nmem_init (void)
 {
 }
 
 void nmem_init (void)
 {
-#ifdef WINDOWS
+    nmem_init_flag = 1;
+#ifdef WIN32
     InitializeCriticalSection(&critical_section);
 #endif
     nmem_active_no = 0;
     InitializeCriticalSection(&critical_section);
 #endif
     nmem_active_no = 0;
@@ -259,7 +382,7 @@ void nmem_exit (void)
        cfreelist = cfreelist->next;
        xfree (cfl);
     }
        cfreelist = cfreelist->next;
        xfree (cfl);
     }
-#ifdef WINDOWS
+#ifdef WIN32
     DeleteCriticalSection(&critical_section);
 #endif
 }
     DeleteCriticalSection(&critical_section);
 #endif
 }