C++ compilation.
[idzebra-moved-to-github.git] / isam / physical.c
index d8883f1..09de594 100644 (file)
@@ -1,10 +1,28 @@
 /*
- * Copyright (C) 1994, Index Data I/S 
+ * Copyright (C) 1994-1999, Index Data
  * All rights reserved.
  * Sebastian Hammer, Adam Dickmeiss
  *
  * $Log: physical.c,v $
- * Revision 1.10  1996-03-19 19:22:44  quinn
+ * Revision 1.16  1999-05-26 07:49:14  adam
+ * C++ compilation.
+ *
+ * Revision 1.15  1999/02/02 14:51:22  adam
+ * Updated WIN32 code specific sections. Changed header.
+ *
+ * Revision 1.14  1997/09/09 13:38:12  adam
+ * Partial port to WIN95/NT.
+ *
+ * Revision 1.13  1996/10/29 13:56:57  adam
+ * Include of zebrautl.h instead of alexutil.h.
+ *
+ * Revision 1.12  1996/03/20 16:17:11  quinn
+ * Bug hunting
+ *
+ * Revision 1.11  1996/03/20  13:29:17  quinn
+ * Bug-fix
+ *
+ * Revision 1.10  1996/03/19  19:22:44  quinn
  * Fixed update-bug
  *
  * Revision 1.9  1996/02/06  10:19:57  quinn
@@ -43,7 +61,9 @@
 
 #include <assert.h>
 #include <stdio.h>
+#include <string.h>
 
+#include <log.h>
 #include <isam.h>
 
 static int is_freestore_alloc(ISAM is, int type)
@@ -106,6 +126,7 @@ int is_p_read_partial(is_mtable *tab, is_mblock *block)
     /* extract header info */
     buf->offset = 0;
     memcpy(&block->num_records, buf->data, sizeof(block->num_records));
+    assert(block->num_records > 0);
     buf->offset += sizeof(block->num_records);
     memcpy(&block->nextpos, buf->data + buf->offset,
        sizeof(block->nextpos));
@@ -116,6 +137,9 @@ int is_p_read_partial(is_mtable *tab, is_mblock *block)
            sizeof(tab->num_records));
        buf->offset +=sizeof(tab->num_records);
     }
+    logf(LOG_DEBUG, "R: Block #%d: num %d nextpos %d total %d",
+        block->diskpos, block->num_records, block->nextpos,
+       block == tab->data ? tab->num_records : -1);
     buf->num = (toread - buf->offset) / is_keysize(tab->is);
     if (buf->num >= block->num_records)
     {
@@ -207,10 +231,15 @@ void is_p_sync(is_mtable *tab)
                sizeof(tab->num_records));
            sum += sizeof(tab->num_records);
        }
+       logf (LOG_DEBUG, "W: Block #%d contains %d records.", p->diskpos,
+           p->num_records);
+       assert(p->num_records > 0);
        for (b = p->data; b; b = b->next)
        {
-           memcpy(type->dbuf + sum, b->data + b->offset, v = b->num *
-               is_keysize(tab->is));
+            logf(LOG_DEBUG, "   buf: offset %d, keys %d, type %d, ref %d",
+               b->offset, b->num, b->type, b->refcount);
+           if ((v = b->num * is_keysize(tab->is)) > 0)
+               memcpy(type->dbuf + sum, b->data + b->offset, v);
 
            sum += v;
            assert(sum <= type->blocksize);
@@ -220,7 +249,6 @@ void is_p_sync(is_mtable *tab)
            logf (LOG_FATAL, "Failed to write block.");
            exit(1);
        }
-       logf (LOG_DEBUG, "W: Block #%d contains %d records.", p->diskpos, p->num_records);
     }
 }
 
@@ -243,7 +271,7 @@ void is_p_unmap(is_mtable *tab)
 
 static is_mbuf *mbuf_takehead(is_mbuf **mb, int *num, int keysize)
 {
-    is_mbuf *p = 0, **pp = &p, *new;
+    is_mbuf *p = 0, **pp = &p, *inew;
     int toget = *num;
 
     if (!toget)
@@ -258,13 +286,13 @@ static is_mbuf *mbuf_takehead(is_mbuf **mb, int *num, int keysize)
     }
     if (toget > 0 && *mb)
     {
-       new = xmalloc_mbuf(IS_MBUF_TYPE_SMALL);
-       new->next = (*mb)->next;
-       (*mb)->next = new;
-       new->data = (*mb)->data;
+       inew = xmalloc_mbuf(IS_MBUF_TYPE_SMALL);
+       inew->next = (*mb)->next;
+       (*mb)->next = inew;
+       inew->data = (*mb)->data;
        (*mb)->refcount++;
-       new->offset = (*mb)->offset + toget * keysize;
-       new->num = (*mb)->num - toget;
+       inew->offset = (*mb)->offset + toget * keysize;
+       inew->num = (*mb)->num - toget;
        (*mb)->num = toget;
        *pp = *mb;
        *mb = (*mb)->next;
@@ -282,7 +310,7 @@ static is_mbuf *mbuf_takehead(is_mbuf **mb, int *num, int keysize)
  */
 void is_p_align(is_mtable *tab)
 {
-    is_mblock *mblock, *new, *last = 0, *next;
+    is_mblock *mblock, *inew, *last = 0, *next;
     is_mbuf *mbufs, *mbp;
     int blocks, recsblock;
 
@@ -300,8 +328,7 @@ void is_p_align(is_mtable *tab)
            }
            else
            {
-               tab->data = tab->data->next;
-               next = tab->data;
+               next = tab->data->next;
                if (next)
                {
                    if (next->state < IS_MBSTATE_CLEAN)
@@ -320,6 +347,7 @@ void is_p_align(is_mtable *tab)
                        }
                    }
                    next->state = IS_MBSTATE_DIRTY; /* force re-process */
+                   tab->data = next;
                }
            }
            if (mblock->diskpos >= 0)
@@ -345,11 +373,11 @@ void is_p_align(is_mtable *tab)
            {
                if (mbufs)
                {
-                   new = xmalloc_mblock();
-                   new->diskpos = -1;
-                   new->state = IS_MBSTATE_DIRTY;
-                   new->next = mblock->next;
-                   mblock->next = new;
+                   inew = xmalloc_mblock();
+                   inew->diskpos = -1;
+                   inew->state = IS_MBSTATE_DIRTY;
+                   inew->next = mblock->next;
+                   mblock->next = inew;
                }
                mblock->data = mbp;
                mblock->num_records = recsblock;