chiark / gitweb /
Modify and debug allocation routines.
authormdw <mdw>
Thu, 28 Oct 1999 22:05:29 +0000 (22:05 +0000)
committermdw <mdw>
Thu, 28 Oct 1999 22:05:29 +0000 (22:05 +0000)
darray.c
dstr.c

index 0825bcae50fc222243d0e06bcd966d3ceaebe165..bee6955b201f91a9e6b86a1e4b902aef14ca540b 100644 (file)
--- a/darray.c
+++ b/darray.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: darray.c,v 1.1 1999/10/22 22:37:26 mdw Exp $
+ * $Id: darray.c,v 1.2 1999/10/28 22:05:28 mdw Exp $
  *
  * Dynamically growing dense arrays
  *
@@ -30,6 +30,9 @@
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: darray.c,v $
+ * Revision 1.2  1999/10/28 22:05:28  mdw
+ * Modify and debug allocation routines.
+ *
  * Revision 1.1  1999/10/22 22:37:26  mdw
  * New dynamic array implementation replaces `dynarray.h'.
  *
@@ -46,7 +49,7 @@
 
 /*----- Magic numbers -----------------------------------------------------*/
 
-#define DA_INITSZ 64                   /* Default size for new array */
+#define DA_INITSZ 16                   /* Default size for new array */
 #define DA_SLOTS 8                     /* Number of preshifted slots */
 
 /*----- Main code ---------------------------------------------------------*/
@@ -114,7 +117,7 @@ void *da_ensure(da_base *b, void *v, size_t sz, size_t n)
 
   /* --- Reallocate the array --- */
 
-  nsz = v ? b->sz + b->off : DA_INITSZ;
+  nsz = v ? b->sz + b->off : (DA_INITSZ >> 1);
   do nsz <<= 1; while (nsz < rq);
   q = xmalloc(nsz * sz);
   q += slots * sz;
@@ -193,7 +196,7 @@ void *da_shunt(da_base *b, void *v, size_t sz, size_t n)
 
   /* --- Reallocate the array --- */
 
-  nsz = v ? b->sz + b->off : DA_INITSZ;
+  nsz = v ? b->sz + b->off : (DA_INITSZ >> 1);
   do nsz <<= 1; while (nsz < rq);
   q = xmalloc(nsz * sz);
   q += (nsz - slots) * sz;
diff --git a/dstr.c b/dstr.c
index 82f0805e87dbb584b28a80920191be4c5424b879..7c03a3c5496346e77d92cce48bfbfb9065d4b04e 100644 (file)
--- a/dstr.c
+++ b/dstr.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: dstr.c,v 1.10 1999/10/04 21:44:47 mdw Exp $
+ * $Id: dstr.c,v 1.11 1999/10/28 22:05:29 mdw Exp $
  *
  * Handle dynamically growing strings
  *
@@ -30,6 +30,9 @@
 /*----- Revision history --------------------------------------------------*
  *
  * $Log: dstr.c,v $
+ * Revision 1.11  1999/10/28 22:05:29  mdw
+ * Modify and debug allocation routines.
+ *
  * Revision 1.10  1999/10/04 21:44:47  mdw
  * Move `dstr_putf' and `dstr_vputf' into a separate source file.
  *
@@ -80,7 +83,7 @@
  * memory-hungry, but efficient.
  */
 
-#define DSTR_INITSZ 256                        /* Initial buffer size */
+#define DSTR_INITSZ 64                 /* Initial buffer size */
 
 /*----- Main code ---------------------------------------------------------*/
 
@@ -142,10 +145,9 @@ void dstr_ensure(dstr *d, size_t sz)
 
   nsz = d->sz;
 
-  if (nsz == 0 && rq < DSTR_INITSZ)
-    nsz = DSTR_INITSZ;
-  else
-    do nsz <<= 1; while (nsz < rq);
+  if (nsz == 0)
+    nsz = (DSTR_INITSZ >> 1);
+  do nsz <<= 1; while (nsz < rq);
 
   if (d->buf)
     d->buf = xrealloc(d->buf, nsz);
@@ -229,8 +231,8 @@ void dstr_putm(dstr *d, const void *p, size_t sz) { DPUTM(d, p, sz); }
 
 void dstr_tidy(dstr *d)
 {
-  dstr_putz(d);
   d->buf = xrealloc(d->buf, d->len + 1);
+  d->buf[d->len] = 0;
   d->sz = d->len + 1;
 }
 
@@ -256,13 +258,6 @@ int dstr_putline(dstr *d, FILE *fp)
 
   for (;;) {
 
-    /* --- Make sure there's some buffer space --- */
-
-    if (!left) {
-      dstr_ensure(d, 1);
-      left = d->sz - off;
-    }
-
     /* --- Read the next byte --- */
 
     ch = getc(fp);
@@ -272,6 +267,13 @@ int dstr_putline(dstr *d, FILE *fp)
     if (ch == EOF && !rd)
       return (EOF);
 
+    /* --- Make sure there's some buffer space --- */
+
+    if (!left) {
+      dstr_ensure(d, 1);
+      left = d->sz - off;
+    }
+
     /* --- End-of-file or newline ends the loop --- */
 
     if (ch == EOF || ch == '\n') {