Changed include/yaz/diagbib1.h and added include/yaz/diagsrw.h with
[yaz-moved-to-github.git] / src / nmem.c
index 2526dbb..4f2504e 100644 (file)
@@ -1,22 +1,29 @@
 /*
- * Copyright (c) 1995-2003, Index Data.
+ * Copyright (C) 1995-2005, Index Data ApS
  * See the file LICENSE for details.
- * Sebastian Hammer, Adam Dickmeiss
  *
- * $Id: nmem.c,v 1.1 2003-10-27 12:21:32 adam Exp $
+ * $Id: nmem.c,v 1.14 2005-01-21 09:23:27 adam Exp $
  */
 
-/*
+/**
+ * \file nmem.c
+ * \brief Implements Nibble Memory
+ *
  * This is a simple and fairly wasteful little module for nibble memory
  * allocation. Evemtually we'll put in something better.
+ *
+ * FIXME - it also has some semaphore stuff, and stuff to handle errno.
+ *         These should be moved to some other place!
  */
 #if HAVE_CONFIG_H
 #include <config.h>
 #endif
 
 #include <assert.h>
+#include <stdlib.h>
 #include <string.h>
 #include <errno.h>
+#include <stddef.h>
 #include <yaz/xmalloc.h>
 #include <yaz/nmem.h>
 #include <yaz/log.h>
 
 #define NMEM_CHUNK (4*1024)
 
+struct align {
+    char x;
+    union {
+       char c;
+       short s;
+       int i;
+       long l;
+#if HAVE_LONG_LONG
+       long long ll;
+#endif
+       float f;
+       double d;
+    } u;
+};
+
+#define NMEM_ALIGN (offsetof(struct align, u))
+
+static int log_level = 0;
+static int log_level_initialized = 0;
+
 #ifdef WIN32
 static CRITICAL_SECTION critical_section;
 #define NMEM_ENTER EnterCriticalSection(&critical_section)
@@ -67,7 +94,12 @@ struct nmem_mutex {
 
 YAZ_EXPORT void nmem_mutex_create(NMEM_MUTEX *p)
 {
-    NMEM_ENTER;
+    if (!log_level_initialized)
+    {
+        log_level = yaz_log_module_level("nmem");
+        log_level_initialized = 1;
+    }
+
     if (!*p)
     {
        *p = (NMEM_MUTEX) malloc (sizeof(**p));
@@ -79,7 +111,6 @@ YAZ_EXPORT void nmem_mutex_create(NMEM_MUTEX *p)
         pth_mutex_init (&(*p)->m_handle);
 #endif
     }
-    NMEM_LEAVE;
 }
 
 YAZ_EXPORT void nmem_mutex_enter(NMEM_MUTEX p)
@@ -108,7 +139,6 @@ YAZ_EXPORT void nmem_mutex_leave(NMEM_MUTEX p)
 
 YAZ_EXPORT void nmem_mutex_destroy(NMEM_MUTEX *p)
 {
-    NMEM_ENTER;
     if (*p)
     {
 #ifdef WIN32
@@ -117,7 +147,6 @@ YAZ_EXPORT void nmem_mutex_destroy(NMEM_MUTEX *p)
        free (*p);
        *p = 0;
     }
-    NMEM_LEAVE;
 }
 
 static nmem_block *freelist = NULL;        /* "global" freelists */
@@ -138,22 +167,28 @@ struct nmem_debug_info *nmem_debug_list = 0;
 
 static void free_block(nmem_block *p)
 {  
+    memset(p->buf, 'Y', p->size);
     p->next = freelist;
     freelist = p;
-#if NMEM_DEBUG
-    yaz_log (LOG_DEBUG, "nmem free_block p=%p", p);
-#endif
+    if (log_level)
+        yaz_log (log_level, "nmem free_block p=%p", p);
 }
 
 #if NMEM_DEBUG
 void nmem_print_list (void)
 {
+    if(log_level)
+        nmem_print_list_l(log_level);
+}
+
+void nmem_print_list_l (int level)
+{
     struct nmem_debug_info *p;
 
-    yaz_log (LOG_DEBUG, "nmem print list");
+    yaz_log (level, "nmem print list");
     NMEM_ENTER;
     for (p = nmem_debug_list; p; p = p->next)
-       yaz_log (LOG_DEBUG, " %s:%d p=%p size=%d", p->file, p->line, p->p,
+       yaz_log (level, " %s:%d p=%p size=%d", p->file, p->line, p->p,
                 nmem_total(p->p));
     NMEM_LEAVE;
 }
@@ -165,17 +200,16 @@ static nmem_block *get_block(int size)
 {
     nmem_block *r, *l;
 
-#if NMEM_DEBUG
-    yaz_log (LOG_DEBUG, "nmem get_block size=%d", size);
-#endif
+    if (log_level)
+        yaz_log (log_level, "nmem get_block size=%d", size);
+
     for (r = freelist, l = 0; r; l = r, r = r->next)
        if (r->size >= size)
            break;
     if (r)
     {
-#if NMEM_DEBUG
-       yaz_log (LOG_DEBUG, "nmem get_block found free block p=%p", r);
-#endif
+        if (log_level)
+           yaz_log (log_level, "nmem get_block found free block p=%p", r);
        if (l)
            l->next = r->next;
        else
@@ -187,9 +221,9 @@ static nmem_block *get_block(int size)
 
        if (get < size)
            get = size;
-#if NMEM_DEBUG
-       yaz_log (LOG_DEBUG, "nmem get_block alloc new block size=%d", get);
-#endif
+        if(log_level)
+           yaz_log (log_level, "nmem get_block alloc new block size=%d", get);
+
        r = (nmem_block *)xmalloc(sizeof(*r));
        r->buf = (char *)xmalloc(r->size = get);
     }
@@ -200,10 +234,8 @@ static nmem_block *get_block(int size)
 void nmem_reset(NMEM n)
 {
     nmem_block *t;
-
-#if NMEM_DEBUG
-    yaz_log (LOG_DEBUG, "nmem_reset p=%p", n);
-#endif
+    
+    yaz_log (log_level, "nmem_reset p=%p", n);
     if (!n)
        return;
     NMEM_ENTER;
@@ -227,12 +259,13 @@ void *nmem_malloc(NMEM n, int size)
     char *r;
 
 #if NMEM_DEBUG
-    yaz_log (LOG_DEBUG, "%s:%d: nmem_malloc p=%p size=%d", file, line,
-                     n, size);
+    if (log_level)
+        yaz_log (log_level, "%s:%d: nmem_malloc p=%p size=%d", 
+                file, line, n, size);
 #endif
     if (!n)
     {
-        yaz_log (LOG_FATAL, "calling nmem_malloc with an null pointer");
+        yaz_log (YLOG_FATAL, "calling nmem_malloc with an null pointer");
         abort ();
     }
 #ifdef WIN32
@@ -248,7 +281,7 @@ void *nmem_malloc(NMEM n, int size)
     }
     r = p->buf + p->top;
     /* align size */
-    p->top += (size + (sizeof(long) - 1)) & ~(sizeof(long) - 1);
+    p->top += (size + (NMEM_ALIGN - 1)) & ~(NMEM_ALIGN - 1);
     n->total += size;
     NMEM_LEAVE;
     return r;
@@ -269,6 +302,11 @@ NMEM nmem_create(void)
 #if NMEM_DEBUG
     struct nmem_debug_info *debug_p;
 #endif
+    if (!log_level_initialized)
+    {
+        log_level = yaz_log_module_level("nmem");
+        log_level_initialized = 1;
+    }
     
     NMEM_ENTER;
     nmem_active_no++;
@@ -280,7 +318,7 @@ NMEM nmem_create(void)
     NMEM_LEAVE;
 
 #if NMEM_DEBUG
-    yaz_log (LOG_DEBUG, "%s:%d: nmem_create %d p=%p", file, line,
+    yaz_log (YLOG_DEBUG, "%s:%d: nmem_create %d p=%p", file, line,
                      nmem_active_no, r);
 #endif
     r->blocks = 0;
@@ -291,7 +329,7 @@ NMEM nmem_create(void)
     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");
+           yaz_log (YLOG_FATAL, "multi used block in nmem");
            abort ();
        }
     debug_p = xmalloc (sizeof(*debug_p));
@@ -321,7 +359,7 @@ void nmem_destroy(NMEM n)
        return;
     
 #if NMEM_DEBUG
-    yaz_log (LOG_DEBUG, "%s:%d: nmem_destroy %d p=%p", file, line,
+    yaz_log (log_level, "%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)
@@ -337,7 +375,7 @@ void nmem_destroy(NMEM n)
     nmem_print_list();
     if (!ok)
     {
-       yaz_log (LOG_WARN, "%s:%d destroying unallocated nmem block p=%p",
+       yaz_log (YLOG_WARN, "%s:%d destroying unallocated nmem block p=%p",
                 file, line, n);
        return;
     }
@@ -353,7 +391,7 @@ void nmem_destroy(NMEM n)
 void nmem_transfer (NMEM dst, NMEM src)
 {
     nmem_block *t;
-    while ((t=src->blocks))
+    while ((t = src->blocks))
     {
        src->blocks = t->next;
        t->next = dst->blocks;
@@ -375,18 +413,23 @@ void nmem_critical_leave (void)
 
 void nmem_init (void)
 {
+    
     if (++nmem_init_flag == 1)
     {
 #ifdef WIN32
        InitializeCriticalSection(&critical_section);
 #elif YAZ_GNU_THREADS
-       yaz_log (LOG_LOG, "pth_init");
         pth_init ();
 #endif
        nmem_active_no = 0;
        freelist = NULL;
        cfreelist = NULL;
     }
+    if (!log_level_initialized)
+    {
+        log_level = yaz_log_module_level("nmem");
+        log_level_initialized = 1;
+    }
 }
 
 void nmem_exit (void)
@@ -443,9 +486,18 @@ void yaz_set_errno(int v)
 
 void yaz_strerror(char *buf, int max)
 {
+#ifdef WIN32
+    DWORD err;
+#endif
     char *cp;
+    if (!log_level_initialized)
+    {
+        log_level = yaz_log_module_level("nmem");
+        log_level_initialized = 1;
+    }
+    
 #ifdef WIN32
-    DWORD err = GetLastError();
+    err = GetLastError();
     if (err)
     {
         FormatMessage(
@@ -476,8 +528,8 @@ void yaz_strerror(char *buf, int max)
 #endif
 /* UNIX */
 #endif
-    if ((cp=strrchr(buf, '\n')))
+    if ((cp = strrchr(buf, '\n')))
        *cp = '\0';
-    if ((cp=strrchr(buf, '\r')))
+    if ((cp = strrchr(buf, '\r')))
        *cp = '\0';
 }