From f185581ca3237ec1c37bc4ad92aab4c3ce8e7236 Mon Sep 17 00:00:00 2001 From: Robert Spalek Date: Fri, 25 Jun 2004 12:47:17 +0000 Subject: [PATCH] generalized BCONFIG_CAN_OVERWRITE to three different write policies --- lib/fastbuf.h | 9 +++++++++ lib/fb-buffer.c | 14 +++++++++++++- lib/fb-file.c | 2 +- lib/fb-limfd.c | 13 +++++++++++++ lib/fb-mem.c | 14 ++++++++++++++ lib/fb-mmap.c | 2 +- 6 files changed, 51 insertions(+), 3 deletions(-) diff --git a/lib/fastbuf.h b/lib/fastbuf.h index 0104a61f..9cc646d3 100644 --- a/lib/fastbuf.h +++ b/lib/fastbuf.h @@ -2,6 +2,7 @@ * Sherlock Library -- Fast Buffered I/O * * (c) 1997--2004 Martin Mares + * (c) 2004 Robert Spalek * * This software may be freely distributed and used according to the terms * of the GNU Lesser General Public License. @@ -101,6 +102,14 @@ int bconfig(struct fastbuf *f, uns type, int data); #define BCONFIG_IS_TEMP_FILE 0 #define BCONFIG_CAN_OVERWRITE 1 + /* Specified whether the caller is allowed to perform the following optimized + * 0-copy write operation: + * - get the buffer by bdirect_read_prepare() + * - modify the buffer, e.g. by putting \0's inside + * - call bflush() to let the fastbuf know + * 0: read-only memory + * 1: you can write into read-write memory, if you restore the original value + * 2: you can rewrite the original content */ /* Universal functions working on all fastbuf's */ diff --git a/lib/fb-buffer.c b/lib/fb-buffer.c index 03c8e321..2e2d4454 100644 --- a/lib/fb-buffer.c +++ b/lib/fb-buffer.c @@ -36,6 +36,18 @@ fbbuf_spout(struct fastbuf *f UNUSED) die("fbbuf: buffer overflow on write"); } +static int +fbbuf_config(struct fastbuf *f UNUSED, uns item, int value UNUSED) +{ + switch (item) + { + case BCONFIG_CAN_OVERWRITE: + return 1; + default: + return -1; + } +} + void fbbuf_init_write(struct fastbuf *f, byte *buf, uns size) { @@ -47,5 +59,5 @@ fbbuf_init_write(struct fastbuf *f, byte *buf, uns size) f->spout = fbbuf_spout; f->seek = NULL; f->close = NULL; - f->config = NULL; + f->config = fbbuf_config; } diff --git a/lib/fb-file.c b/lib/fb-file.c index abb29ddf..3b416de3 100644 --- a/lib/fb-file.c +++ b/lib/fb-file.c @@ -90,7 +90,7 @@ bfd_config(struct fastbuf *f, uns item, int value) FB_FILE(f)->is_temp_file = value; return 0; case BCONFIG_CAN_OVERWRITE: - return 1; + return 2; default: return -1; } diff --git a/lib/fb-limfd.c b/lib/fb-limfd.c index c1b6cb2c..ac95d908 100644 --- a/lib/fb-limfd.c +++ b/lib/fb-limfd.c @@ -39,6 +39,18 @@ bfl_close(struct fastbuf *f) xfree(f); } +static int +bfl_config(struct fastbuf *f UNUSED, uns item, int value UNUSED) +{ + switch (item) + { + case BCONFIG_CAN_OVERWRITE: + return 2; + default: + return -1; + } +} + struct fastbuf * bopen_limited_fd(int fd, uns buflen, uns limit) { @@ -54,6 +66,7 @@ bopen_limited_fd(int fd, uns buflen, uns limit) F->limit = limit; f->refill = bfl_refill; f->close = bfl_close; + f->config = bfl_config; return f; } diff --git a/lib/fb-mem.c b/lib/fb-mem.c index daa80733..efe749c5 100644 --- a/lib/fb-mem.c +++ b/lib/fb-mem.c @@ -149,6 +149,18 @@ fbmem_close(struct fastbuf *f) xfree(f); } +static int +fbmem_config(struct fastbuf *f UNUSED, uns item, int value UNUSED) +{ + switch (item) + { + case BCONFIG_CAN_OVERWRITE: + return 1; + default: + return -1; + } +} + struct fastbuf * fbmem_create(unsigned blocksize) { @@ -162,6 +174,7 @@ fbmem_create(unsigned blocksize) f->name = ""; f->spout = fbmem_spout; f->close = fbmem_close; + f->config = fbmem_config; return f; } @@ -179,6 +192,7 @@ fbmem_clone_read(struct fastbuf *b) f->refill = fbmem_refill; f->seek = fbmem_seek; f->close = fbmem_close; + f->config = fbmem_config; return f; } diff --git a/lib/fb-mmap.c b/lib/fb-mmap.c index f4f90184..bbd3a11e 100644 --- a/lib/fb-mmap.c +++ b/lib/fb-mmap.c @@ -161,7 +161,7 @@ bfmm_config(struct fastbuf *f, uns item, int value) FB_MMAP(f)->is_temp_file = value; return 0; case BCONFIG_CAN_OVERWRITE: - return 0; + return 0; /* cannot use 1, because the pages would become dirty */ default: return -1; } -- 2.39.2