]> mj.ucw.cz Git - libucw.git/blobdiff - lib/fb-buffer.c
Use the generic thread context to hold per-thread I/O queue.
[libucw.git] / lib / fb-buffer.c
index 03c8e32108ca7bc0abff0999ce7875cd2be7d58d..09c7429357e6c518e9630b418067037e4986a43f 100644 (file)
@@ -1,7 +1,7 @@
 /*
 /*
- *     Sherlock Library -- Fast Buffered I/O on Static Buffers
+ *     UCW Library -- Fast Buffered I/O on Static Buffers
  *
  *
- *     (c) 2003 Martin Mares <mj@ucw.cz>
+ *     (c) 2003--2006 Martin Mares <mj@ucw.cz>
  *
  *     This software may be freely distributed and used according to the terms
  *     of the GNU Lesser General Public License.
  *
  *     This software may be freely distributed and used according to the terms
  *     of the GNU Lesser General Public License.
 #include "lib/lib.h"
 #include "lib/fastbuf.h"
 
 #include "lib/lib.h"
 #include "lib/fastbuf.h"
 
+#include <stdlib.h>
+
 static int
 fbbuf_refill(struct fastbuf *f UNUSED)
 {
   return 0;
 }
 
 static int
 fbbuf_refill(struct fastbuf *f UNUSED)
 {
   return 0;
 }
 
+static void
+fbbuf_seek(struct fastbuf *f, sh_off_t pos, int whence)
+{
+  /* Somebody might want to seek to the end of buffer, try to be nice to him. */
+  sh_off_t len = f->bufend - f->buffer;
+  if (whence == SEEK_END)
+    pos += len;
+  ASSERT(pos >= 0 && pos <= len);
+  f->bptr = f->buffer + pos;
+  f->bstop = f->bufend;
+  f->pos = len;
+}
+
 void
 void
-fbbuf_init_read(struct fastbuf *f, byte *buf, uns size)
+fbbuf_init_read(struct fastbuf *f, byte *buf, uns size, uns can_overwrite)
 {
   f->buffer = f->bptr = buf;
   f->bstop = f->bufend = buf + size;
 {
   f->buffer = f->bptr = buf;
   f->bstop = f->bufend = buf + size;
@@ -25,9 +40,10 @@ fbbuf_init_read(struct fastbuf *f, byte *buf, uns size)
   f->pos = size;
   f->refill = fbbuf_refill;
   f->spout = NULL;
   f->pos = size;
   f->refill = fbbuf_refill;
   f->spout = NULL;
-  f->seek = NULL;
+  f->seek = fbbuf_seek;
   f->close = NULL;
   f->config = NULL;
   f->close = NULL;
   f->config = NULL;
+  f->can_overwrite_buffer = can_overwrite;
 }
 
 static void
 }
 
 static void
@@ -48,4 +64,5 @@ fbbuf_init_write(struct fastbuf *f, byte *buf, uns size)
   f->seek = NULL;
   f->close = NULL;
   f->config = NULL;
   f->seek = NULL;
   f->close = NULL;
   f->config = NULL;
+  f->can_overwrite_buffer = 0;
 }
 }