]> mj.ucw.cz Git - libucw.git/blobdiff - lib/fastbuf.h
Use big_alloc().
[libucw.git] / lib / fastbuf.h
index 25c11d70e0d2aaed6bf38b13cdd41b2d29ebd189..922798ff183515a3d102191c6e614569373088e1 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *     Sherlock Library -- Fast Buffered I/O
+ *     UCW Library -- Fast Buffered I/O
  *
  *     (c) 1997--2004 Martin Mares <mj@ucw.cz>
  *     (c) 2004 Robert Spalek <robert@ucw.cz>
@@ -8,14 +8,15 @@
  *     of the GNU Lesser General Public License.
  */
 
-#ifndef _SHERLOCK_FASTBUF_H
-#define _SHERLOCK_FASTBUF_H
+#ifndef _UCW_FASTBUF_H
+#define _UCW_FASTBUF_H
 
 #ifndef EOF
 #include <stdio.h>
 #endif
 
 #include <string.h>
+#include <alloca.h>
 
 #include "lib/unaligned.h"
 
@@ -81,6 +82,7 @@ struct fastbuf {
 /* FastIO on standard files (specify buffer size 0 to enable mmaping) */
 
 struct fastbuf *bopen(byte *name, uns mode, uns buflen);
+struct fastbuf *bopen_try(byte *name, uns mode, uns buflen);
 struct fastbuf *bopen_tmp(uns buflen);
 struct fastbuf *bfdopen(int fd, uns buflen);
 struct fastbuf *bfdopen_shared(int fd, uns buflen);
@@ -109,6 +111,32 @@ fbbuf_count_written(struct fastbuf *f)
   return f->bptr - f->bstop;
 }
 
+/* FastIO on recyclable growing buffers */
+
+struct fastbuf *fbgrow_create(unsigned basic_size);
+void fbgrow_reset(struct fastbuf *b);                  /* Reset stream and prepare for writing */
+void fbgrow_rewind(struct fastbuf *b);                 /* Prepare for reading */
+
+/* FastO with atomic writes for multi-threaded programs */
+
+struct fb_atomic {
+  struct fastbuf fb;
+  struct fb_atomic_file *af;
+  byte *expected_max_bptr;
+  uns slack_size;
+};
+#define FB_ATOMIC(f) ((struct fb_atomic *)(f)->is_fastbuf)
+
+struct fastbuf *fbatomic_open(byte *name, struct fastbuf *master, uns bufsize, int record_len);
+void fbatomic_internal_write(struct fastbuf *b);
+
+static inline void
+fbatomic_commit(struct fastbuf *b)
+{
+  if (b->bptr >= ((struct fb_atomic *)b)->expected_max_bptr)
+    fbatomic_internal_write(b);
+}
+
 /* Configuring stream parameters */
 
 int bconfig(struct fastbuf *f, uns type, int data);
@@ -122,7 +150,6 @@ void bflush(struct fastbuf *f);
 void bseek(struct fastbuf *f, sh_off_t pos, int whence);
 void bsetpos(struct fastbuf *f, sh_off_t pos);
 void brewind(struct fastbuf *f);
-void bskip(struct fastbuf *f, uns len);
 sh_off_t bfilesize(struct fastbuf *f);
 
 static inline sh_off_t btell(struct fastbuf *f)
@@ -313,6 +340,20 @@ byte *bgets(struct fastbuf *f, byte *b, uns l);    /* Non-std */
 int bgets_nodie(struct fastbuf *f, byte *b, uns l);
 byte *bgets0(struct fastbuf *f, byte *b, uns l);
 
+struct mempool;
+struct bb_t;
+uns bgets_bb(struct fastbuf *f, struct bb_t *b, uns limit);
+byte *bgets_mp(struct fastbuf *f, struct mempool *mp);
+
+struct bgets_stk_struct {
+  struct fastbuf *f;
+  byte *old_buf, *cur_buf, *src;
+  uns old_len, cur_len, src_len;
+};
+void bgets_stk_init(struct bgets_stk_struct *s);
+void bgets_stk_step(struct bgets_stk_struct *s);
+#define bgets_stk(fb) ({ struct bgets_stk_struct _s; _s.f = (fb); for (bgets_stk_init(&_s); _s.cur_len; _s.cur_buf = alloca(_s.cur_len), bgets_stk_step(&_s)); _s.cur_buf; })
+
 static inline void
 bputs(struct fastbuf *f, byte *b)
 {
@@ -346,6 +387,18 @@ bbcopy(struct fastbuf *f, struct fastbuf *t, uns l)
     bbcopy_slow(f, t, l);
 }
 
+int bskip_slow(struct fastbuf *f, uns len);
+static inline int bskip(struct fastbuf *f, uns len)
+{
+  if (bavailr(f) >= len)
+    {
+      f->bptr += len;
+      return 1;
+    }
+  else
+    return bskip_slow(f, len);
+}
+
 /* I/O on addr_int_t */
 
 #ifdef CPU_64BIT_POINTERS
@@ -362,7 +415,10 @@ static inline uns
 bdirect_read_prepare(struct fastbuf *f, byte **buf)
 {
   if (f->bptr == f->bstop && !f->refill(f))
-    return 0;
+    {
+      *buf = NULL;  // This is not needed, but it helps to get rid of spurious warnings
+      return 0;
+    }
   *buf = f->bptr;
   return bavailr(f);
 }
@@ -397,7 +453,7 @@ bdirect_write_commit(struct fastbuf *f, byte *pos)
 
 /* Formatted output */
 
-int bprintf(struct fastbuf *b, byte *msg, ...);
-int vbprintf(struct fastbuf *b, byte *msg, va_list args);
+int bprintf(struct fastbuf *b, char *msg, ...) FORMAT_CHECK(printf,2,3);
+int vbprintf(struct fastbuf *b, char *msg, va_list args);
 
 #endif