]> mj.ucw.cz Git - libucw.git/commitdiff
Fastbuf: fbmulti: fixes from MJ
authorJan 'Moskyt' Matejka <mq@ucw.cz>
Mon, 23 Jul 2012 09:40:51 +0000 (11:40 +0200)
committerJan 'Moskyt' Matejka <mq@ucw.cz>
Mon, 23 Jul 2012 09:40:51 +0000 (11:40 +0200)
ucw/fastbuf.h
ucw/fb-multi.c

index 4989e22ef877c31aab60e462ac6694eaf23da03e..4f0d99d3190be67c6fad8ae22e4e11a46760167d 100644 (file)
@@ -492,32 +492,33 @@ static inline void fbatomic_commit(struct fastbuf *b)
  * are connected into fbmulti.
  *
  * The fbmulti is inited by @fbmulti_create(). It returns an empty fbmulti.
- * Then you call @fbmulti_append() for each fbmulti
+ * Then you call @fbmulti_append() for each fbmulti.
  *
  * If @bclose() is called on fbmulti, all the underlying buffers get closed
  * recursively.
  *
- * You may init a fbmulti by @fbmulti_create(bufsize) with no underlying buffers
- * and then append the underlying buffers one by one.
- *
- * If used in some formatter, you'll probably have a large and deep structure
- * of nested fastbufs. Just before reading from the fbmulti, you may call
- * @fbmulti_flatten() to flatten the structure. After @fbmulti_flatten(), the
- * fbmulti is seeked to the beginning, flushed and ready to read the whole buffer.
- *
  * If you want to keep an underlying fastbuf open after @bclose, just remove it
  * by @fbmulti_remove where the second parameter is a pointer to the removed
  * fastbuf. If you pass NULL, all the underlying fastbufs are removed.
  *
- * After a @fbmulti_remove, the state of fbmulti is undefined. The only allowed
- * operation is then another @fbmulti_remove or @bclose on that fbmulti.
- *
+ * After @fbmulti_remove, the state of the fbmulti is undefined. The only allowed
+ * operation is either another @fbmulti_remove or @bclose on the fbmulti.
  ***/
 
+/**
+ * Create an empty fbmulti
+ **/
 struct fastbuf *fbmulti_create(void);
-void fbmulti_append(struct fastbuf *f, struct fastbuf *fa);
-void fbmulti_remove(struct fastbuf *f, struct fastbuf *fb);
 
+/**
+ * Append a fb to fbmulti
+ **/
+void fbmulti_append(struct fastbuf *f, struct fastbuf *fb);
+
+/**
+ * Remove a fb from fbmulti
+ **/
+void fbmulti_remove(struct fastbuf *f, struct fastbuf *fb);
 
 /*** === Configuring stream parameters [[bconfig]] ***/
 
index 7059d4bbaf3c77efc5720f9b1565d8e736c6199f..4cd6244d444aa719ea0f640de29d1fb69bd1ed10 100644 (file)
@@ -59,9 +59,9 @@ fbmulti_subbuf_next(struct fastbuf *f)
   if (!next)
     return 0;
 
-  // Get the end of current buf if not known yet.
+  // Get the end of current buf if not known yet
   if (!f->seek && !next->begin)
-    next->begin = FB_MULTI(f)->cur->end = f->pos;
+    next->begin = cur->end = f->pos;
 
   // Set the beginning of the next buf
   if (next->fb->seek)
@@ -132,21 +132,25 @@ fbmulti_seek(struct fastbuf *f, ucw_off_t pos, int whence)
        bthrow(f, "seek", "Seek out of range");
 
       while (pos > FB_MULTI(f)->cur->end) // Moving forward
-       ASSERT(fbmulti_subbuf_next(f));
+       {
+         int r = fbmulti_subbuf_next(f);
+         ASSERT(r);
+       }
 
       while (pos < FB_MULTI(f)->cur->begin) // Moving backwards
-       ASSERT(fbmulti_subbuf_prev(f));
+       {
+         int r = fbmulti_subbuf_prev(f);
+         ASSERT(r);
+       }
 
       // Now cur is the right buffer.
       FB_MULTI(f)->cur->fb->seek(FB_MULTI(f)->cur->fb, (pos - FB_MULTI(f)->cur->begin), SEEK_SET);
 
       fbmulti_get_ptrs(f);
       return 1;
-      break;
 
     case SEEK_END:
-      return fbmulti_seek(f, FB_MULTI(f)->len+pos, SEEK_SET);
-      break;
+      return fbmulti_seek(f, FB_MULTI(f)->len + pos, SEEK_SET);
 
     default:
       ASSERT(0);
@@ -165,7 +169,7 @@ fbmulti_close(struct fastbuf *f)
 struct fastbuf *
 fbmulti_create(void)
 {
-  struct mempool *mp = mp_new(128);
+  struct mempool *mp = mp_new(1024);
   struct fastbuf *fb_out = mp_alloc(mp, sizeof(struct fb_multi));
   struct fb_multi *fbm = FB_MULTI(fb_out);
 
@@ -187,7 +191,7 @@ fbmulti_append(struct fastbuf *f, struct fastbuf *fb)
 {
   struct subbuf *last = clist_tail(&FB_MULTI(f)->subbufs);
 
-  struct subbuf *sb = mp_alloc(FB_MULTI(f)->mp, sizeof(struct subbuf));
+  struct subbuf *sb = mp_alloc(FB_MULTI(f)->mp, sizeof(*sb));
   sb->fb = fb;
   clist_add_tail(&FB_MULTI(f)->subbufs, &(sb->n));
 
@@ -231,9 +235,9 @@ fbmulti_remove(struct fastbuf *f, struct fastbuf *fb)
          {
            clist_remove(&(n->n));
            return;
-         };
+         }
 
-      die("Given fastbuf %p not in given fbmulti %p.", fb, f);
+      die("Given fastbuf %p not in given fbmulti %p", fb, f);
     }
   else
     clist_init(&FB_MULTI(f)->subbufs);
@@ -245,7 +249,7 @@ int main(int argc, char **argv)
 {
   if (argc < 2)
     {
-      fprintf(stderr, "You must specify a test (r, w, o)\n");
+      fprintf(stderr, "You must specify a test (r, m, i, n)\n");
       return 1;
     }
   switch (*argv[1])