From: Julius Schmidt <aiju@phicode.de>
To: 9front@9front.org
Subject: bio io functions
Date: Thu, 4 May 2017 18:02:19 +0200 (CEST) [thread overview]
Message-ID: <alpine.LNX.2.00.1705041754480.9667@phi> (raw)
[-- Attachment #1: Type: TEXT/PLAIN, Size: 525 bytes --]
I would like to change bio to do all I/O through a user replaceable
function. The attached patch does just that, adding a new function Biofn()
to register such a function, which replaces read/write.
Main uses I have in mind:
1. Allow programs to flush output buffers before stalling for more input,
which would most likely be activated with a command line flag.
2. Use bio with things that are not file descriptors.
3. Apply filters to input/outputs (arguably could also be done with
pipes).
4. Use bio with ioprocs.
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: Type: TEXT/x-diff; name=bio.patch, Size: 4208 bytes --]
diff -r 36315eb9647c sys/include/bio.h
--- a/sys/include/bio.h Tue May 02 04:07:21 2017 +0200
+++ b/sys/include/bio.h Thu May 04 15:50:15 2017 +0000
@@ -33,6 +33,7 @@
uchar* ebuf; /* pointer to end of buffer */
uchar* gbuf; /* pointer to good data in buf */
void (*errorf)(char *); /* called on error if not nil */
+ int (*iof)(Biobufhdr*, void *, long); /* called to do i/o */
};
struct Biobuf
@@ -74,6 +75,7 @@
long Bwrite(Biobufhdr*, void*, long);
void Blethal(Biobufhdr*, void(*)(char*));
void Berror(Biobufhdr*, char*, ...);
+void Biofn(Biobufhdr*, int(*)(Biobufhdr*, void*, long));
#pragma varargck argpos Bprint 2
#pragma varargck argpos Berror 2
diff -r 36315eb9647c sys/src/libbio/bflush.c
--- a/sys/src/libbio/bflush.c Tue May 02 04:07:21 2017 +0200
+++ b/sys/src/libbio/bflush.c Thu May 04 15:50:15 2017 +0000
@@ -12,7 +12,7 @@
n = bp->bsize+bp->ocount;
if(n == 0)
return 0;
- c = write(bp->fid, bp->bbuf, n);
+ c = bp->iof(bp, bp->bbuf, n);
if(n == c) {
bp->offset += n;
bp->ocount = -bp->bsize;
diff -r 36315eb9647c sys/src/libbio/bgetc.c
--- a/sys/src/libbio/bgetc.c Tue May 02 04:07:21 2017 +0200
+++ b/sys/src/libbio/bgetc.c Thu May 04 15:50:15 2017 +0000
@@ -24,7 +24,7 @@
* buffer to allow that many ungets.
*/
memmove(bp->bbuf-Bungetsize, bp->ebuf-Bungetsize, Bungetsize);
- i = read(bp->fid, bp->bbuf, bp->bsize);
+ i = bp->iof(bp, bp->bbuf, bp->bsize);
bp->gbuf = bp->bbuf;
if(i <= 0) {
bp->state = Bracteof;
diff -r 36315eb9647c sys/src/libbio/binit.c
--- a/sys/src/libbio/binit.c Tue May 02 04:07:21 2017 +0200
+++ b/sys/src/libbio/binit.c Thu May 04 15:50:15 2017 +0000
@@ -50,6 +50,18 @@
}
}
+static int
+bioread(Biobufhdr *bp, void *v, long n)
+{
+ return read(bp->fid, v, n);
+}
+
+static int
+biowrite(Biobufhdr *bp, void *v, long n)
+{
+ return write(bp->fid, v, n);
+}
+
int
Binits(Biobufhdr *bp, int f, int mode, uchar *p, int size)
{
@@ -64,12 +76,14 @@
case OREAD:
bp->state = Bractive;
bp->ocount = 0;
+ bp->iof = bioread;
break;
case OWRITE:
install(bp);
bp->state = Bwactive;
bp->ocount = -size;
+ bp->iof = biowrite;
break;
}
bp->bbuf = p;
@@ -154,3 +168,15 @@
/* otherwise opened with Binit(s) */
return r;
}
+
+void
+Biofn(Biobufhdr *bp, int (*f)(Biobufhdr *, void *, long))
+{
+ if(f == nil)
+ if(bp->state == Bwactive)
+ bp->iof = biowrite;
+ else
+ bp->iof = bioread;
+ else
+ bp->iof = f;
+}
diff -r 36315eb9647c sys/src/libbio/brdline.c
--- a/sys/src/libbio/brdline.c Tue May 02 04:07:21 2017 +0200
+++ b/sys/src/libbio/brdline.c Thu May 04 15:50:15 2017 +0000
@@ -46,7 +46,7 @@
*/
ip = (char*)bp->bbuf + i;
while(i < bp->bsize) {
- j = read(bp->fid, ip, bp->bsize-i);
+ j = bp->iof(bp, ip, bp->bsize-i);
if(j < 0)
Berror(bp, "read error: %r");
if(j <= 0) {
diff -r 36315eb9647c sys/src/libbio/brdstr.c
--- a/sys/src/libbio/brdstr.c Tue May 02 04:07:21 2017 +0200
+++ b/sys/src/libbio/brdstr.c Thu May 04 15:50:15 2017 +0000
@@ -69,7 +69,7 @@
for(;;){
ip = (char*)bp->bbuf + i;
while(i < bp->bsize) {
- j = read(bp->fid, ip, bp->bsize-i);
+ j = bp->iof(bp, ip, bp->bsize-i);
if(j < 0)
Berror(bp, "read error: %r");
if(j <= 0 && i == 0)
diff -r 36315eb9647c sys/src/libbio/bread.c
--- a/sys/src/libbio/bread.c Tue May 02 04:07:21 2017 +0200
+++ b/sys/src/libbio/bread.c Thu May 04 15:50:15 2017 +0000
@@ -20,7 +20,7 @@
if(n == 0) {
if(bp->state != Bractive)
break;
- i = read(bp->fid, bp->bbuf, bp->bsize);
+ i = bp->iof(bp, bp->bbuf, bp->bsize);
if(i <= 0) {
bp->state = Bracteof;
if(i < 0) {
diff -r 36315eb9647c sys/src/libbio/bwrite.c
--- a/sys/src/libbio/bwrite.c Tue May 02 04:07:21 2017 +0200
+++ b/sys/src/libbio/bwrite.c Thu May 04 15:50:15 2017 +0000
@@ -21,7 +21,7 @@
if(n == 0) {
if(bp->state != Bwactive)
return Beof;
- i = write(bp->fid, bp->bbuf, bp->bsize);
+ i = bp->iof(bp, bp->bbuf, bp->bsize);
if(i != bp->bsize) {
errstr(errbuf, sizeof errbuf);
if(strstr(errbuf, "interrupt") == nil) {
next reply other threads:[~2017-05-04 16:02 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-05-04 16:02 Julius Schmidt [this message]
2017-05-04 16:23 ` [9front] " Benjamin Purcell
2017-05-04 17:52 ` Giacomo Tesio
2017-05-04 20:18 ` Bruce Ellis
2017-05-04 20:21 ` Benjamin Purcell
2017-05-05 9:11 ` hiro
2017-05-05 9:14 ` Bruce Ellis
2017-05-05 9:55 ` Julius Schmidt
2017-05-05 9:56 ` Bruce Ellis
2017-05-05 10:21 ` Stanley Lieber
2017-05-05 10:29 ` Bruce Ellis
2017-05-05 10:32 ` Stanley Lieber
2017-05-05 10:33 ` Bruce Ellis
2017-05-05 11:05 ` Stanley Lieber
2017-05-05 12:11 ` hiro
2017-05-05 15:24 ` Kurt H Maier
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=alpine.LNX.2.00.1705041754480.9667@phi \
--to=aiju@phicode.de \
--cc=9front@9front.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).