* (c) 1997--2000 Martin Mares <mj@ucw.cz>
*/
+#include "lib/lib.h"
+#include "lib/fastbuf.h"
+
#include <stdio.h>
#include <stdlib.h>
-#include "lib.h"
-#include "fastbuf.h"
-
struct memstream {
unsigned blocksize;
unsigned uc;
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;
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;
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;
}
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;