@@@ man wip
[mLib] / test / tvec-remote.c
index b87351b..1a3bd0e 100644 (file)
@@ -44,6 +44,7 @@
 #include "buf.h"
 #include "compiler.h"
 #include "fdflags.h"
+#include "growbuf.h"
 #include "lbuf.h"
 #include "mdup.h"
 #include "quis.h"
@@ -81,7 +82,7 @@
 
 static void init_comms(struct tvec_remotecomms *rc)
 {
-  rc->bin = 0; rc->binsz = 0; dbuf_create(&rc->bout);
+  rc->bin = 0; rc->binsz = 0; DBCREATE(&rc->bout);
   rc->infd = rc->outfd = -1; rc->f = 0;
 }
 
@@ -120,7 +121,7 @@ static void close_comms(struct tvec_remotecomms *rc)
  */
 
 static void release_comms(struct tvec_remotecomms *rc)
-  { close_comms(rc); xfree(rc->bin); dbuf_destroy(&rc->bout); }
+  { close_comms(rc); xfree(rc->bin); DBDESTROY(&rc->bout); }
 
 /* --- @setup_comms@ --- *
  *
@@ -378,13 +379,7 @@ static int receive_buffered(struct tvec_state *tv,
   if (rc->binlen - rc->binoff >= want) return (RECV_OK);
 
   /* If the buffer is too small then we must grow it. */
-  if (want > rc->binsz) {
-    sz = rc->binsz; if (!sz) sz = RECVBUFSZ;
-    while (sz < want) { assert(sz < (size_t)-1/2); sz *= 2; }
-    if (!rc->bin) rc->bin = xmalloc(sz);
-    else rc->bin = xrealloc(rc->bin, sz, rc->binsz);
-    rc->binsz = sz;
-  }
+  GROWBUF_EXTEND(&arena_stdlib, rc->bin, rc->binsz, want, RECVBUFSZ, 1);
 
   /* Shunt the unused existing material to the start of the buffer. */
   memmove(rc->bin, rc->bin + rc->binoff, rc->binlen - rc->binoff);
@@ -712,12 +707,8 @@ int tvec_remoteserver(int infd, int outfd, const struct tvec_config *config)
              if (vd->regsz <= sizeof(rbuf))
                r = &rbuf;
              else {
-               if (rsz < vd->regsz) {
-                 xfree(r_alloc);
-                 if (!rsz) rsz = 8*sizeof(void *);
-                 while (rsz < vd->regsz) rsz *= 2;
-                 r_alloc = xmalloc(rsz);
-               }
+               GROWBUF_REPLACE(&arena_stdlib, r_alloc, rsz, vd->regsz,
+                               8*sizeof(void *), 1);
                r = r_alloc;
              }