]> mj.ucw.cz Git - libucw.git/commitdiff
Fastbufs: bclose() on caller-allocated fastbuf is allowed
authorMartin Mares <mj@ucw.cz>
Tue, 5 Sep 2017 22:16:11 +0000 (00:16 +0200)
committerMartin Mares <mj@ucw.cz>
Tue, 5 Sep 2017 22:16:11 +0000 (00:16 +0200)
Previously, it was impossible to catch exceptions thrown by fbbuf
(although it is quite useful in case of the "buffer overflow" exception).

With these changes, bclose() should be allowed and equivalent to bflush(),
which should do nothing :)  Hence, fb_tie() should work and so should
exception handling.

ucw/fastbuf.h
ucw/fb-buffer.c

index 91c42b85fc4f2524f89fa537457c2b3aeecd771b..0a04176a0f8ec18155996d9a6c3b5e1d55e4da41 100644 (file)
@@ -413,8 +413,7 @@ struct fastbuf *fbmem_clone_read(struct fastbuf *f);        /** Given a writing fastbuf
  * of the buffer temporarily. In this case, set @can_overwrite as described
  * in <<internal,Internals>>. If you do not care, keep @can_overwrite zero.
  *
- * It is not possible to close this fastbuf. This implies that no tying to
- * resources takes place.
+ * A @bclose() on this fastbuf is allowed and it does nothing.
  */
 void fbbuf_init_read(struct fastbuf *f, byte *buffer, uint size, uint can_overwrite);
 
@@ -426,8 +425,7 @@ void fbbuf_init_read(struct fastbuf *f, byte *buffer, uint size, uint can_overwr
  * Data are written directly into the buffer, so it is not necessary to call @bflush()
  * at any moment.
  *
- * It is not possible to close this fastbuf. This implies that no tying to
- * resources takes place.
+ * A @bclose() on this fastbuf is allowed and it does nothing.
  */
 void fbbuf_init_write(struct fastbuf *f, byte *buffer, uint size);
 
@@ -473,8 +471,8 @@ struct fbpool { /** Structure for fastbufs & mempools. **/
 };
 
 /**
- * Initialize a new `fbpool`. The structure is allocated by the caller,
- * so @bclose() should not be called and no resource tying takes place.
+ * Initialize a new `fbpool`. The structure is allocated by the caller.
+ * Calling @bclose() is optional.
  **/
 void fbpool_init(struct fbpool *fb);   /** Initialize a new mempool fastbuf. **/
 /**
@@ -625,7 +623,8 @@ int bconfig(struct fastbuf *f, uint type, int data); /** Configure a fastbuf. Re
 
 /**
  * Close and free fastbuf.
- * Can not be used for fastbufs not returned from function (initialized in a parameter, for example the one from `fbbuf_init_read`).
+ * Some kinds of fastbufs are allocated by the caller (e.g., in @fbbuf_init_read());
+ * in such cases, @bclose() does not free any memory.
  */
 void bclose(struct fastbuf *f);
 void bthrow(struct fastbuf *f, const char *id, const char *fmt, ...) FORMAT_CHECK(printf,3,4) NONRET;  /** Throw exception on a given fastbuf **/
index c69234725e0d26901e780fca0d63ec394369b3af..55a92f7c9b1563b8e8e8875a2aa85e7c82ea8c2b 100644 (file)
@@ -47,13 +47,15 @@ fbbuf_init_read(struct fastbuf *f, byte *buf, uint size, uint can_overwrite)
     .pos = size,
     .refill = fbbuf_refill,
     .seek = fbbuf_seek,
-    .can_overwrite_buffer = can_overwrite };
+    .can_overwrite_buffer = can_overwrite
+  };
 }
 
 static void
 fbbuf_spout(struct fastbuf *f)
 {
-  bthrow(f, "write", "fbbuf: buffer overflow on write");
+  if (f->bptr >= f->bufend)
+    bthrow(f, "write", "fbbuf: buffer overflow on write");
 }
 
 void