Added OID utility
[yaz-moved-to-github.git] / odr / odr_mem.c
index 01c96f6..fb6caeb 100644 (file)
@@ -1,10 +1,25 @@
 /*
- * Copyright (C) 1994, Index Data I/S 
- * All rights reserved.
+ * Copyright (c) 1995, Index Data
+ * See the file LICENSE for details.
  * Sebastian Hammer, Adam Dickmeiss
  *
  * $Log: odr_mem.c,v $
- * Revision 1.4  1995-05-15 11:56:09  quinn
+ * Revision 1.9  1995-10-13 16:08:08  quinn
+ * Added OID utility
+ *
+ * Revision 1.8  1995/09/29  17:12:24  quinn
+ * Smallish
+ *
+ * Revision 1.7  1995/09/27  15:02:59  quinn
+ * Modified function heads & prototypes.
+ *
+ * Revision 1.6  1995/08/21  09:10:41  quinn
+ * Smallish fixes to suppport new formats.
+ *
+ * Revision 1.5  1995/05/16  08:50:55  quinn
+ * License, documentation, and memory fixes
+ *
+ * Revision 1.4  1995/05/15  11:56:09  quinn
  * More work on memory management.
  *
  * Revision 1.3  1995/04/18  08:15:21  quinn
@@ -33,6 +48,7 @@ typedef struct odr_memblock
     char *buf;
     int size;
     int top;
+    int total;
     struct odr_memblock *next;
 } odr_memblock;
 
@@ -71,6 +87,7 @@ static odr_memblock *get_block(int size)
            abort();
     }
     r->top = 0;
+    r->total = 0;
     return r;
 }
 
@@ -105,20 +122,34 @@ void *odr_malloc(ODR o, int size)
     struct odr_memblock *p = o->mem;
     char *r;
 
+    if (!o)
+    {
+       if (!(r = malloc(size)))
+           abort();
+       return r;
+    }
     if (!p || p->size - p->top < size)
        if (!(p = get_block(size)))
            abort();
        else
        {
+           if (o->mem)
+               p->total = o->mem->total;
            p->next = o->mem;
            o->mem = p;
        }
     r = p->buf + p->top;
     /* align size */
     p->top += (size + (sizeof(long) - 1)) & ~(sizeof(long) - 1);
+    p->total += size;
     return r;
 }
 
+int odr_total(ODR o)
+{
+    return o->mem ? o->mem->total : 0;
+}
+
 /* ---------- memory management for data encoding ----------*/