]> mj.ucw.cz Git - libucw.git/blobdiff - lib/fb-mem.c
Forgot to commit this one during the "search by age" changes.
[libucw.git] / lib / fb-mem.c
index f582e2e3f9cc5f4a733a9f756a693ed20d1b9043..d5e6e8b96f4e54bfd4b70fd4d6ae02849a7e8849 100644 (file)
@@ -4,12 +4,12 @@
  *     (c) 1997--2000 Martin Mares <mj@ucw.cz>
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-
 #include "lib/lib.h"
 #include "lib/fastbuf.h"
 
+#include <stdio.h>
+#include <stdlib.h>
+
 struct memstream {
   unsigned blocksize;
   unsigned uc;
@@ -34,7 +34,7 @@ fbmem_refill(struct fastbuf *f)
       if (!b)
        return 0;
     }
-  else if (f->bstop < b->data + b->size)
+  else if (f->buffer == b->data && f->bstop < b->data + b->size)
     {
       f->bstop = b->data + b->size;
       return 1;
@@ -46,6 +46,8 @@ fbmem_refill(struct fastbuf *f)
       f->pos += b->size;
       b = b->next;
     }
+  if (!b->size)
+    return 0;
   f->buffer = f->bptr = b->data;
   f->bufend = f->bstop = b->data + b->size;
   f->llpos = b;
@@ -89,7 +91,7 @@ fbmem_seek(struct fastbuf *f, sh_off_t pos, int whence)
     die("fbmem_seek: only SEEK_SET supported");
   for (b=m->first; b; b=b->next)
     {
-      if (pos <= p + b->size)  /* <=, because we need to be able to seek just after file end */
+      if ((unsigned) pos <= p + b->size) /* <=, because we need to be able to seek just after file end */
        {
          f->pos = p;
          f->buffer = b->data;
@@ -115,28 +117,23 @@ fbmem_close(struct fastbuf *f)
   while (b = m->first)
     {
       m->first = b->next;
-      free(b);
+      xfree(b);
     }
-  free(m);
+  xfree(m);
 }
 
 struct fastbuf *
 fbmem_create(unsigned blocksize)
 {
-  struct fastbuf *f = xmalloc(sizeof(struct fastbuf));
-  struct memstream *m = xmalloc(sizeof(struct memstream));
+  struct fastbuf *f = xmalloc_zero(sizeof(struct fastbuf));
+  struct memstream *m = xmalloc_zero(sizeof(struct memstream));
 
   m->blocksize = blocksize;
   m->uc = 1;
-  m->first = NULL;
 
-  f->bptr = f->bstop = f->buffer = f->bufend = NULL;
-  f->pos = f->fdpos = 0;
   f->name = "<fbmem-write>";
   f->lldata = m;
-  f->refill = NULL;
   f->spout = fbmem_spout;
-  f->seek = NULL;
   f->close = fbmem_close;
   return f;
 }
@@ -144,17 +141,15 @@ fbmem_create(unsigned blocksize)
 struct fastbuf *
 fbmem_clone_read(struct fastbuf *b)
 {
-  struct fastbuf *f = xmalloc(sizeof(struct fastbuf));
+  struct fastbuf *f = xmalloc_zero(sizeof(struct fastbuf));
   struct memstream *s = b->lldata;
 
+  bflush(b);
   s->uc++;
 
-  f->bptr = f->bstop = f->buffer = f->bufend = NULL;
-  f->pos = f->fdpos = 0;
   f->name = "<fbmem-read>";
   f->lldata = s;
   f->refill = fbmem_refill;
-  f->spout = NULL;
   f->seek = fbmem_seek;
   f->close = fbmem_close;
   return f;