* Sherlock Library -- Fast Buffered I/O
*
* (c) 1997--2004 Martin Mares <mj@ucw.cz>
+ * (c) 2004 Robert Spalek <robert@ucw.cz>
*
* This software may be freely distributed and used according to the terms
* of the GNU Lesser General Public License.
#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 */
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)
{
f->spout = fbbuf_spout;
f->seek = NULL;
f->close = NULL;
- f->config = NULL;
+ f->config = fbbuf_config;
}
FB_FILE(f)->is_temp_file = value;
return 0;
case BCONFIG_CAN_OVERWRITE:
- return 1;
+ return 2;
default:
return -1;
}
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)
{
F->limit = limit;
f->refill = bfl_refill;
f->close = bfl_close;
+ f->config = bfl_config;
return 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)
{
f->name = "<fbmem-write>";
f->spout = fbmem_spout;
f->close = fbmem_close;
+ f->config = fbmem_config;
return f;
}
f->refill = fbmem_refill;
f->seek = fbmem_seek;
f->close = fbmem_close;
+ f->config = fbmem_config;
return f;
}
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;
}