For GFS, allow field record in bend_fetch_rr to hold NULL - meaning
[yaz-moved-to-github.git] / src / nmem.c
index 4f2504e..484836c 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright (C) 1995-2005, Index Data ApS
  * See the file LICENSE for details.
  *
- * $Id: nmem.c,v 1.14 2005-01-21 09:23:27 adam Exp $
+ * $Id: nmem.c,v 1.18 2005-06-07 19:47:31 adam Exp $
  */
 
 /**
 
 #define NMEM_CHUNK (4*1024)
 
+struct nmem_block
+{
+    char *buf;              /* memory allocated in this block */
+    size_t size;            /* size of buf */
+    size_t top;             /* top of buffer */
+    struct nmem_block *next;
+};
+
+struct nmem_control
+{
+    int total;
+    nmem_block *blocks;
+    struct nmem_control *next;
+};
+
 struct align {
     char x;
     union {
@@ -94,12 +109,6 @@ struct nmem_mutex {
 
 YAZ_EXPORT void nmem_mutex_create(NMEM_MUTEX *p)
 {
-    if (!log_level_initialized)
-    {
-        log_level = yaz_log_module_level("nmem");
-        log_level_initialized = 1;
-    }
-
     if (!*p)
     {
        *p = (NMEM_MUTEX) malloc (sizeof(**p));
@@ -111,6 +120,12 @@ YAZ_EXPORT void nmem_mutex_create(NMEM_MUTEX *p)
         pth_mutex_init (&(*p)->m_handle);
 #endif
     }
+    if (!log_level_initialized)
+    {
+        log_level_initialized = 1;
+        log_level = yaz_log_module_level("nmem");
+    }
+
 }
 
 YAZ_EXPORT void nmem_mutex_enter(NMEM_MUTEX p)
@@ -196,12 +211,12 @@ void nmem_print_list_l (int level)
 /*
  * acquire a block with a minimum of size free bytes.
  */
-static nmem_block *get_block(int size)
+static nmem_block *get_block(size_t size)
 {
     nmem_block *r, *l;
 
     if (log_level)
-        yaz_log (log_level, "nmem get_block size=%d", size);
+        yaz_log (log_level, "nmem get_block size=%ld", (long) size);
 
     for (r = freelist, l = 0; r; l = r, r = r->next)
        if (r->size >= size)
@@ -273,7 +288,7 @@ void *nmem_malloc(NMEM n, int size)
 #endif
     NMEM_ENTER;
     p = n->blocks;
-    if (!p || p->size - p->top < size)
+    if (!p || p->size < size + p->top)
     {
        p = get_block(size);
        p->next = n->blocks;