Fix crash in record conv rule select YAZ-812
[yaz-moved-to-github.git] / src / nmem.c
index ff34165..5aaa6c1 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of the YAZ toolkit.
- * Copyright (C) 1995-2008 Index Data
+ * Copyright (C) Index Data
  * See the file LICENSE for details.
  */
 
@@ -8,10 +8,8 @@
  * \brief Implements Nibble Memory
  *
  * This is a simple and fairly wasteful little module for nibble memory
- * allocation. Evemtually we'll put in something better.
+ * allocation.
  *
- * 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>
 #include <yaz/nmem.h>
 #include <yaz/log.h>
 
-#ifdef WIN32
-#include <windows.h>
-#endif
-
 #define NMEM_CHUNK (4*1024)
 
 struct nmem_block
@@ -42,7 +36,7 @@ struct nmem_block
 
 struct nmem_control
 {
-    int total;
+    size_t total;
     struct nmem_block *blocks;
     struct nmem_control *next;
 };
@@ -68,11 +62,11 @@ static int log_level = 0;
 static int log_level_initialized = 0;
 
 static void free_block(struct nmem_block *p)
-{  
+{
     xfree(p->buf);
     xfree(p);
     if (log_level)
-        yaz_log (log_level, "nmem free_block p=%p", p);
+        yaz_log(log_level, "nmem free_block p=%p", p);
 }
 
 /*
@@ -84,15 +78,14 @@ static struct nmem_block *get_block(size_t size)
     size_t get = NMEM_CHUNK;
 
     if (log_level)
-        yaz_log (log_level, "nmem get_block size=%ld", (long) size);
+        yaz_log(log_level, "nmem get_block size=%ld", (long) size);
 
-    
     if (get < size)
         get = size;
-    if(log_level)
-        yaz_log (log_level, "nmem get_block alloc new block size=%ld",
-                 (long) get);
-    
+    if (log_level)
+        yaz_log(log_level, "nmem get_block alloc new block size=%ld",
+                (long) get);
+
     r = (struct nmem_block *) xmalloc(sizeof(*r));
     r->buf = (char *)xmalloc(r->size = get);
     r->top = 0;
@@ -102,8 +95,8 @@ static struct nmem_block *get_block(size_t size)
 void nmem_reset(NMEM n)
 {
     struct nmem_block *t;
-    
-    yaz_log (log_level, "nmem_reset p=%p", n);
+
+    yaz_log(log_level, "nmem_reset p=%p", n);
     if (!n)
         return;
     while (n->blocks)
@@ -115,15 +108,15 @@ void nmem_reset(NMEM n)
     n->total = 0;
 }
 
-void *nmem_malloc(NMEM n, int size)
+void *nmem_malloc(NMEM n, size_t size)
 {
     struct nmem_block *p;
     char *r;
 
     if (!n)
     {
-        yaz_log (YLOG_FATAL, "calling nmem_malloc with an null pointer");
-        abort ();
+        yaz_log(YLOG_FATAL, "calling nmem_malloc with an null pointer");
+        abort();
     }
     p = n->blocks;
     if (!p || p->size < size + p->top)
@@ -139,7 +132,7 @@ void *nmem_malloc(NMEM n, int size)
     return r;
 }
 
-int nmem_total(NMEM n)
+size_t nmem_total(NMEM n)
 {
     return n->total;
 }
@@ -152,7 +145,7 @@ NMEM nmem_create(void)
         log_level = yaz_log_module_level("nmem");
         log_level_initialized = 1;
     }
-    
+
     r = (struct nmem_control *)xmalloc(sizeof(*r));
 
     r->blocks = 0;
@@ -166,12 +159,12 @@ void nmem_destroy(NMEM n)
 {
     if (!n)
         return;
-    
+
     nmem_reset(n);
     xfree(n);
 }
 
-void nmem_transfer (NMEM dst, NMEM src)
+void nmem_transfer(NMEM dst, NMEM src)
 {
     struct nmem_block *t;
     while ((t = src->blocks))
@@ -184,64 +177,10 @@ void nmem_transfer (NMEM dst, NMEM src)
     src->total = 0;
 }
 
-int yaz_errno(void)
-{
-    return errno;
-}
-
-void yaz_set_errno(int v)
-{
-    errno = 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
-    err = GetLastError();
-    if (err)
-    {
-        FormatMessage(
-                FORMAT_MESSAGE_FROM_SYSTEM,
-                NULL,
-                err,
-                MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default lang */
-                (LPTSTR) buf,
-                max-1,
-                NULL);
-    }
-    else
-        *buf = '\0';
-#else
-/* UNIX */
-#if HAVE_STRERROR_R
-    *buf = '\0';
-    strerror_r(errno, buf, max);
-    /* if buffer is unset - use strerror anyway (GLIBC bug) */
-    if (*buf == '\0')
-        strcpy(buf, strerror(yaz_errno()));
-#else
-    strcpy(buf, strerror(yaz_errno()));
-#endif
-/* UNIX */
-#endif
-    if ((cp = strrchr(buf, '\n')))
-        *cp = '\0';
-    if ((cp = strrchr(buf, '\r')))
-        *cp = '\0';
-}
 /*
  * Local variables:
  * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
  * indent-tabs-mode: nil
  * End:
  * vim: shiftwidth=4 tabstop=8 expandtab