From efa382e8e53bc9117a91138256354b56e77c798f Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Wed, 6 Sep 2017 00:16:11 +0200 Subject: [PATCH] Fastbufs: bclose() on caller-allocated fastbuf is allowed 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 | 13 ++++++------- ucw/fb-buffer.c | 6 ++++-- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/ucw/fastbuf.h b/ucw/fastbuf.h index 91c42b85..0a04176a 100644 --- a/ucw/fastbuf.h +++ b/ucw/fastbuf.h @@ -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 <>. 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 **/ diff --git a/ucw/fb-buffer.c b/ucw/fb-buffer.c index c6923472..55a92f7c 100644 --- a/ucw/fb-buffer.c +++ b/ucw/fb-buffer.c @@ -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 -- 2.39.2