chiark / gitweb /
Change to arena `realloc' interface, to fix a design bug.
authormdw <mdw>
Sun, 16 Jul 2000 12:31:50 +0000 (12:31 +0000)
committermdw <mdw>
Sun, 16 Jul 2000 12:31:50 +0000 (12:31 +0000)
alloc.c
alloc.h
arena.c
arena.h
darray.c
dstr.c
hash.c
man/alloc.3
man/arena.3
pkbuf.c

diff --git a/alloc.c b/alloc.c
index d425e97573b9f9c718add5cb9a0ccaf35b9e7dd7..9bc536e78a0f1fa11d2a8e96dda7577552023768 100644 (file)
--- a/alloc.c
+++ b/alloc.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: alloc.c,v 1.4 2000/06/17 10:35:51 mdw Exp $
+ * $Id: alloc.c,v 1.5 2000/07/16 12:29:16 mdw Exp $
  *
  * Memory allocation functions
  *
@@ -30,6 +30,9 @@
 /*----- Revision history --------------------------------------------------*
  *
  * $Log: alloc.c,v $
+ * Revision 1.5  2000/07/16 12:29:16  mdw
+ * Change to arena `realloc' interface, to fix a design bug.
+ *
  * Revision 1.4  2000/06/17 10:35:51  mdw
  * Major overhaul for arena support.
  *
@@ -104,6 +107,7 @@ char *x_strdup(arena *a, const char *s)
  * Arguments:  @arena *a@ = pointer to underlying arena
  *             @void *p@ = pointer to a block of memory
  *             @size_t sz@ = new size desired for the block
+ *             @size_t osz@ = size of the old block
  *
  * Returns:    Pointer to the resized memory block (which is almost
  *             certainly not in the same place any more).
@@ -112,9 +116,9 @@ char *x_strdup(arena *a, const char *s)
  *             exception @EXC_NOMEM@ is thrown.
  */
 
-void *x_realloc(arena *a, void *p, size_t sz)
+void *x_realloc(arena *a, void *p, size_t sz, size_t osz)
 {
-  p = A_REALLOC(a, p, sz);
+  p = A_REALLOC(a, p, sz, osz);
   if (!p)
     THROW(EXC_NOMEM);
   return (p);
@@ -163,6 +167,7 @@ char *(xstrdup)(const char *s) { return xstrdup(s); }
  *
  * Arguments:  @void *p@ = pointer to a block of memory
  *             @size_t sz@ = new size desired for the block
+ *             @size_t osz@ = size of the old block
  *
  * Returns:    Pointer to the resized memory block (which is almost
  *             certainly not in the same place any more).
@@ -171,7 +176,8 @@ char *(xstrdup)(const char *s) { return xstrdup(s); }
  *             exception @EXC_NOMEM@ is thrown.
  */
 
-void *(xrealloc)(void *p, size_t sz) { return xrealloc(p, sz); }
+void *(xrealloc)(void *p, size_t sz, size_t osz)
+{ return xrealloc(p, sz, osz); }
 
 /* --- @xfree@ --- *
  *
diff --git a/alloc.h b/alloc.h
index edf555af7a3265afe630caf4629c21102eaedbfe..feed0bfe3621dba82087a4ff0d36bd95ae987996 100644 (file)
--- a/alloc.h
+++ b/alloc.h
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: alloc.h,v 1.5 2000/06/17 10:35:51 mdw Exp $
+ * $Id: alloc.h,v 1.6 2000/07/16 12:29:16 mdw Exp $
  *
  * Memory allocation functions
  *
@@ -30,6 +30,9 @@
 /*----- Revision history --------------------------------------------------*
  *
  * $Log: alloc.h,v $
+ * Revision 1.6  2000/07/16 12:29:16  mdw
+ * Change to arena `realloc' interface, to fix a design bug.
+ *
  * Revision 1.5  2000/06/17 10:35:51  mdw
  * Major overhaul for arena support.
  *
@@ -96,6 +99,7 @@ extern char *x_strdup(arena */*a*/, const char */*s*/);
  * Arguments:  @arena *a@ = pointer to underlying arena
  *             @void *p@ = pointer to a block of memory
  *             @size_t sz@ = new size desired for the block
+ *             @size_t osz@ = size of the old block
  *
  * Returns:    Pointer to the resized memory block (which is almost
  *             certainly not in the same place any more).
@@ -104,7 +108,8 @@ extern char *x_strdup(arena */*a*/, const char */*s*/);
  *             exception @EXC_NOMEM@ is thrown.
  */
 
-extern void *x_realloc(arena */*a*/, void */*p*/, size_t /*sz*/);
+extern void *x_realloc(arena */*a*/, void */*p*/,
+                      size_t /*sz*/, size_t /*osz*/);
 
 /* --- @x_free@ --- *
  *
@@ -152,6 +157,7 @@ extern char *xstrdup(const char */*s*/);
  *
  * Arguments:  @void *p@ = pointer to a block of memory
  *             @size_t sz@ = new size desired for the block
+ *             @size_t osz@ = size of the old block
  *
  * Returns:    Pointer to the resized memory block (which is almost
  *             certainly not in the same place any more).
@@ -160,8 +166,8 @@ extern char *xstrdup(const char */*s*/);
  *             exception @EXC_NOMEM@ is thrown.
  */
 
-extern void *xrealloc(void */*p*/, size_t /*sz*/);
-#define xrealloc(p, sz) x_realloc(arena_global, (p), (sz))
+extern void *xrealloc(void */*p*/, size_t /*sz*/, size_t /*osz*/);
+#define xrealloc(p, sz, osz) x_realloc(arena_global, (p), (sz), (osz))
 
 /* --- @xfree@ --- *
  *
diff --git a/arena.c b/arena.c
index 20d286c00320b8080b13736ca6e9233367422393..3e13bf2909b4e850fdcc2fb89b54125c95e7d25d 100644 (file)
--- a/arena.c
+++ b/arena.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: arena.c,v 1.1 2000/06/17 10:37:53 mdw Exp $
+ * $Id: arena.c,v 1.2 2000/07/16 12:29:16 mdw Exp $
  *
  * Abstraction for memory allocation arenas
  *
@@ -30,6 +30,9 @@
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: arena.c,v $
+ * Revision 1.2  2000/07/16 12:29:16  mdw
+ * Change to arena `realloc' interface, to fix a design bug.
+ *
  * Revision 1.1  2000/06/17 10:37:53  mdw
  * Basic arena management.
  *
@@ -44,7 +47,8 @@
 /*----- The standard arena ------------------------------------------------*/
 
 static void *_alloc(arena *a, size_t sz) { return malloc(sz); }
-static void *_realloc(arena *a, void *p, size_t sz){ return realloc(p, sz); }
+static void *_realloc(arena *a, void *p, size_t sz, size_t osz) 
+  { return realloc(p, sz); }
 static void _free(arena *a, void *p) { free (p); }
 
 static arena_ops stdlib_ops = { _alloc, _realloc, _free, 0 };
@@ -61,6 +65,7 @@ arena *arena_global = &arena_stdlib;
  * Arguments:  @arena *a@ = pointer to arena block
  *             @void *p@ = pointer to memory block to resize
  *             @size_t sz@ = size desired for the block
+ *             @size_t osz@ = size of the old block
  *
  * Returns:    ---
  *
@@ -68,12 +73,12 @@ arena *arena_global = &arena_stdlib;
  *             support @realloc@ properly.
  */
 
-void *arena_fakerealloc(arena *a, void *p, size_t sz)
+void *arena_fakerealloc(arena *a, void *p, size_t sz, size_t osz)
 {
   void *q = A_ALLOC(a, sz);
   if (!q)
     return (0);
-  memcpy(q, p, sz);
+  memcpy(q, p, sz > osz ? osz : sz);
   A_FREE(a, p);
   return (q);
 }
@@ -81,7 +86,8 @@ void *arena_fakerealloc(arena *a, void *p, size_t sz)
 /* --- Function equivalents of the macros --- */
 
 void *a_alloc(arena *a, size_t sz) { return (A_ALLOC(a, sz)); }
-void *a_realloc(arena *a, void *p, size_t sz) { return A_REALLOC(a, p, sz); }
+void *a_realloc(arena *a, void *p, size_t sz, size_t osz)
+{ return A_REALLOC(a, p, sz, osz); }
 void a_free(arena *a, void *p) { A_FREE(a, p); }
 
 /*----- That's all, folks -------------------------------------------------*/
diff --git a/arena.h b/arena.h
index 6ceef09701b121e8dffd47b4cbdc4cf9c269ef28..4b1dffe6ffd0c70231486f20183c13ce13e12632 100644 (file)
--- a/arena.h
+++ b/arena.h
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: arena.h,v 1.1 2000/06/17 10:37:53 mdw Exp $
+ * $Id: arena.h,v 1.2 2000/07/16 12:29:16 mdw Exp $
  *
  * Abstraction for memory allocation arenas
  *
@@ -30,6 +30,9 @@
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: arena.h,v $
+ * Revision 1.2  2000/07/16 12:29:16  mdw
+ * Change to arena `realloc' interface, to fix a design bug.
+ *
  * Revision 1.1  2000/06/17 10:37:53  mdw
  * Basic arena management.
  *
@@ -56,7 +59,7 @@ typedef struct arena {
       
 typedef struct arena_ops {
   void *(*alloc)(arena */*a*/, size_t /*sz*/);
-  void *(*realloc)(arena */*a*/, void */*p*/, size_t /*sz*/);
+  void *(*realloc)(arena */*a*/, void */*p*/, size_t /*sz*/, size_t /*osz*/);
   void (*free)(arena */*a*/, void */*p*/);
   void (*purge)(arena */*a*/);
 } arena_ops;
@@ -73,6 +76,7 @@ extern arena arena_stdlib;            /* Arena based on @malloc@/@free@ */
  * Arguments:  @arena *a@ = pointer to arena block
  *             @void *p@ = pointer to memory block to resize
  *             @size_t sz@ = size desired for the block
+ *             @size_t osz@ = size of the old block
  *
  * Returns:    ---
  *
@@ -80,18 +84,20 @@ extern arena arena_stdlib;          /* Arena based on @malloc@/@free@ */
  *             support @realloc@ properly.
  */
 
-extern void *arena_fakerealloc(arena */*a*/, void */*p*/, size_t /*sz*/);
+extern void *arena_fakerealloc(arena */*a*/, void */*p*/,
+                              size_t /*sz*/, size_t /*osz*/);
 
 /* --- Useful macros --- */
 
 #define A_ALLOC(a, sz) (((a)->ops->alloc)((a), (sz)))
-#define A_REALLOC(a, p, sz) (((a)->ops->realloc)((a), (p), (sz)))
+#define A_REALLOC(a, p, sz, osz) (((a)->ops->realloc)((a), (p), (sz), (osz)))
 #define A_FREE(a, p) (((a)->ops->free)((a), (p)))
 
 /* --- Simple function equivalents --- */
 
 extern void *a_alloc(arena */*a*/, size_t /*sz*/);
-extern void *a_realloc(arena */*a*/, void */*p*/, size_t /*sz*/);
+extern void *a_realloc(arena */*a*/, void */*p*/,
+                      size_t /*sz*/, size_t /*osz*/);
 extern void a_free(arena */*a*/, void */*p*/);
 
 /*----- That's all, folks -------------------------------------------------*/
index 9e3eb905160bb3aa66f60798c76257d37aeebbcc..c1ee606e3aef0e08d0472d7672db09810ab8b1fe 100644 (file)
--- a/darray.c
+++ b/darray.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: darray.c,v 1.5 2000/06/17 10:37:39 mdw Exp $
+ * $Id: darray.c,v 1.6 2000/07/16 12:29:16 mdw Exp $
  *
  * Dynamically growing dense arrays
  *
@@ -30,6 +30,9 @@
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: darray.c,v $
+ * Revision 1.6  2000/07/16 12:29:16  mdw
+ * Change to arena `realloc' interface, to fix a design bug.
+ *
  * Revision 1.5  2000/06/17 10:37:39  mdw
  * Add support for arena management.
  *
@@ -144,7 +147,7 @@ void *da_ensure(da_base *b, void *v, size_t sz, size_t n)
    */
 
   if (p && slots == b->off) {
-    q = x_realloc(b->a, p - b->off * sz, nsz * sz);
+    q = x_realloc(b->a, p - b->off * sz, nsz * sz, b->sz + b->off);
     q += slots * sz;
   } else {
     q = x_alloc(b->a, nsz * sz);
diff --git a/dstr.c b/dstr.c
index d76ccf51187b6d282d28250433f2d96e5151c41e..273c53acebe51229361e11d22d23f872986392d3 100644 (file)
--- a/dstr.c
+++ b/dstr.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: dstr.c,v 1.14 2000/06/17 10:37:39 mdw Exp $
+ * $Id: dstr.c,v 1.15 2000/07/16 12:29:16 mdw Exp $
  *
  * Handle dynamically growing strings
  *
@@ -30,6 +30,9 @@
 /*----- Revision history --------------------------------------------------*
  *
  * $Log: dstr.c,v $
+ * Revision 1.15  2000/07/16 12:29:16  mdw
+ * Change to arena `realloc' interface, to fix a design bug.
+ *
  * Revision 1.14  2000/06/17 10:37:39  mdw
  * Add support for arena management.
  *
@@ -159,7 +162,7 @@ void dstr_ensure(dstr *d, size_t sz)
   do nsz <<= 1; while (nsz < rq);
 
   if (d->buf)
-    d->buf = x_realloc(d->a, d->buf, nsz);
+    d->buf = x_realloc(d->a, d->buf, nsz, d->sz);
   else
     d->buf = x_alloc(d->a, nsz);
   d->sz = nsz;
@@ -240,7 +243,7 @@ void dstr_putm(dstr *d, const void *p, size_t sz) { DPUTM(d, p, sz); }
 
 void dstr_tidy(dstr *d)
 {
-  d->buf = x_realloc(d->a, d->buf, d->len + 1);
+  d->buf = x_realloc(d->a, d->buf, d->len + 1, d->sz);
   d->buf[d->len] = 0;
   d->sz = d->len + 1;
 }
diff --git a/hash.c b/hash.c
index 3945755004ca82fa7dc3907ce1fb9fd0961d874e..9483e103d57349aec352a0928a74ffa2c83823b8 100644 (file)
--- a/hash.c
+++ b/hash.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: hash.c,v 1.2 2000/06/17 10:37:39 mdw Exp $
+ * $Id: hash.c,v 1.3 2000/07/16 12:29:16 mdw Exp $
  *
  * General hashtable infrastructure
  *
@@ -30,6 +30,9 @@
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: hash.c,v $
+ * Revision 1.3  2000/07/16 12:29:16  mdw
+ * Change to arena `realloc' interface, to fix a design bug.
+ *
  * Revision 1.2  2000/06/17 10:37:39  mdw
  * Add support for arena management.
  *
@@ -124,7 +127,9 @@ int hash_extend(hash_table *t)
 
   /* --- Allocate a new hash bin vector --- */
 
-  if ((v = A_REALLOC(t->a, t->v, m * 2 * sizeof(hash_base *))) == 0) {
+  if ((v = A_REALLOC(t->a, t->v,
+                    2 * m * sizeof(hash_base *),
+                    m * sizeof(hash_base *))) == 0) {
     return (0);
   }
   t->v = v;
index ed3a03690ba712f2673298c69757271386b2a650..295ae2c837eb3f5426d8ee355046a026d75a5436 100644 (file)
@@ -9,13 +9,13 @@ alloc \- mLib low-level memory allocation
 .nf
 .B "#include <mLib/alloc.h>"
 
-.BI "void *x_alloc(size_t " sz );
-.BI "char *x_strdup(const char *" s );
-.BI "void *x_realloc(void *" p ", size_t " sz );
-.BI "void x_free(void *" p );
+.BI "void *x_alloc(arena *" a ", size_t " sz );
+.BI "char *x_strdup(arena *" a ", const char *" s );
+.BI "void *x_realloc(arena *" a ", void *" p ", size_t " sz ", size_t " osz );
+.BI "void x_free(arena *" a ", void *" p );
 
 .BI "void *xmalloc(size_t " sz );
-.BI "void *xrealloc(void *" p ", size_t " sz );
+.BI "void *xrealloc(void *" p ", size_t " sz ", size_t " osz );
 .BI "char *xstrdup(const char *" s );
 .BI "void xfree(void *" p );
 .fi
index d7631c28d2fd7f260d25a71f1a8f6e65e85992bf..5b4509979eaa095bda3224c54f3d31294cb70dc1 100644 (file)
@@ -18,14 +18,15 @@ arena \- control of memory allocation
 .BI "arena *arena_global;"
 .BI "arena arena_stdlib;"
 
-.BI "void *arena_fakerealloc(arena *" a ", void *" p ", size_t " sz );
+.BI "void *arena_fakerealloc(arena *" a ", void *" p ,
+.BI "                        size_t " sz ", size_t " osz );
 
 .BI "void *a_alloc(arena *" a ", size_t " sz );
-.BI "void *a_realloc(arena *" a ", void *" p ", size_t " sz );
+.BI "void *a_realloc(arena *" a ", void *" p ", size_t " sz ", size_t " osz );
 .BI "void a_free(arena *" a );
 
 .BI "void *A_ALLOC(arena *" a ", size_t " sz );
-.BI "void *A_REALLOC(arena *" a ", void *" p ", size_t " sz );
+.BI "void *A_REALLOC(arena *" a ", void *" p ", size_t " sz ", size_t " osz );
 .BI "void A_FREE(arena *" a );
 .fi
 .SH "DESCRIPTION"
@@ -60,6 +61,14 @@ function-call equivalents with lower-case names too.  For a more
 convenient interface to memory allocation, see
 .BR alloc (3).
 .PP
+.B Note:
+The
+.B realloc
+function has an extra argument
+.I osz
+specifying the old size of the block.  This is for the benefit of arena
+handlers which can't easily find the old block's size.
+.PP
 .SS "Defining new arenas"
 An
 .B arena
@@ -81,10 +90,15 @@ memory allocation tasks:
 Allocates a block of memory, of at least
 .I sz
 bytes in size, appropriately aligned, and returns its address.
+.nf
 .TP
-.BI "void *(*" realloc ")(arena *" a ", void *" p ", size_t " sz );
+.BI "void *(*" realloc ")(arena *" a ", void *" p ", size_t " sz ", size_t " osz );
+.fi
 Resizes the block pointed to by
 .IR p ,
+with
+.I osz
+interesting bytes in it,
 so that it is at least
 .I sz
 bytes long.  You can use
diff --git a/pkbuf.c b/pkbuf.c
index 73e2ca520783e6b2818d6b6a547ad85d5c69148a..31830e0e00320e4d8728d3d95d2008da0a8768e1 100644 (file)
--- a/pkbuf.c
+++ b/pkbuf.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: pkbuf.c,v 1.1 2000/06/17 10:39:19 mdw Exp $
+ * $Id: pkbuf.c,v 1.2 2000/07/16 12:29:16 mdw Exp $
  *
  * Simple packet buffering
  *
@@ -30,6 +30,9 @@
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: pkbuf.c,v $
+ * Revision 1.2  2000/07/16 12:29:16  mdw
+ * Change to arena `realloc' interface, to fix a design bug.
+ *
  * Revision 1.1  2000/06/17 10:39:19  mdw
  * Experimental new support for packet buffering.
  *
@@ -206,7 +209,7 @@ void pkbuf_want(pkbuf *pk, size_t want)
     do pk->sz <<= 1; while (want < pk->sz);
     if (pk->buf) {
       if (pk->len)
-       pk->buf = x_realloc(pk->a, pk->buf, pk->sz);
+       pk->buf = x_realloc(pk->a, pk->buf, pk->sz, pk->len);
       else {
        x_free(pk->a, pk->buf);
        pk->buf = 0;