X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;ds=sidebyside;f=lib%2Ffb-mem.c;h=d5e6e8b96f4e54bfd4b70fd4d6ae02849a7e8849;hb=8ffdaed1c5f5bf4ad0914b8d17f14af51ce037a5;hp=f582e2e3f9cc5f4a733a9f756a693ed20d1b9043;hpb=5b53087fa5a07ff89d34cf3bf3bc1b28809f05c2;p=libucw.git diff --git a/lib/fb-mem.c b/lib/fb-mem.c index f582e2e3..d5e6e8b9 100644 --- a/lib/fb-mem.c +++ b/lib/fb-mem.c @@ -4,12 +4,12 @@ * (c) 1997--2000 Martin Mares */ -#include -#include - #include "lib/lib.h" #include "lib/fastbuf.h" +#include +#include + 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 = ""; 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 = ""; f->lldata = s; f->refill = fbmem_refill; - f->spout = NULL; f->seek = fbmem_seek; f->close = fbmem_close; return f;