shithub: riscv

Download patch

ref: 515893dda6d67853ff271819d877fe2986b03456
parent: f4c6dad8e13c289914962d3b43b07cfdaca3e33a
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Mon Dec 22 05:49:52 EST 2014

pc, pc64, xen: simplify #P/irqalloc

--- a/sys/src/9/pc/trap.c
+++ b/sys/src/9/pc/trap.c
@@ -109,42 +109,29 @@
 }
 
 static long
-irqallocread(Chan*, void *vbuf, long n, vlong offset)
+irqallocread(Chan*, void *a, long n, vlong offset)
 {
-	char *buf, *p, str[2*(11+1)+KNAMELEN+1+1];
-	int m, vno;
-	long oldn;
+	char buf[2*(11+1)+KNAMELEN+1+1];
+	int vno, m;
 	Vctl *v;
 
 	if(n < 0 || offset < 0)
 		error(Ebadarg);
 
-	oldn = n;
-	buf = vbuf;
 	for(vno=0; vno<nelem(vctl); vno++){
 		for(v=vctl[vno]; v; v=v->next){
-			m = snprint(str, sizeof str, "%11d %11d %.*s\n", vno, v->irq, KNAMELEN, v->name);
-			if(m <= offset)	/* if do not want this, skip entry */
-				offset -= m;
-			else{
-				/* skip offset bytes */
-				m -= offset;
-				p = str+offset;
-				offset = 0;
-
-				/* write at most max(n,m) bytes */
-				if(m > n)
-					m = n;
-				memmove(buf, p, m);
-				n -= m;
-				buf += m;
-
-				if(n == 0)
-					return oldn;
-			}
+			m = snprint(buf, sizeof(buf), "%11d %11d %.*s\n", vno, v->irq, KNAMELEN, v->name);
+			offset -= m;
+			if(offset >= 0)
+				continue;
+			if(n > -offset)
+				n = -offset;
+			offset += m;
+			memmove(a, buf+offset, n);
+			return n;
 		}
 	}
-	return oldn - n;
+	return 0;
 }
 
 void
--- a/sys/src/9/pc64/trap.c
+++ b/sys/src/9/pc64/trap.c
@@ -109,42 +109,29 @@
 }
 
 static long
-irqallocread(Chan*, void *vbuf, long n, vlong offset)
+irqallocread(Chan*, void *a, long n, vlong offset)
 {
-	char *buf, *p, str[2*(11+1)+KNAMELEN+1+1];
-	int m, vno;
-	long oldn;
+	char buf[2*(11+1)+KNAMELEN+1+1];
+	int vno, m;
 	Vctl *v;
 
 	if(n < 0 || offset < 0)
 		error(Ebadarg);
 
-	oldn = n;
-	buf = vbuf;
 	for(vno=0; vno<nelem(vctl); vno++){
 		for(v=vctl[vno]; v; v=v->next){
-			m = snprint(str, sizeof str, "%11d %11d %.*s\n", vno, v->irq, KNAMELEN, v->name);
-			if(m <= offset)	/* if do not want this, skip entry */
-				offset -= m;
-			else{
-				/* skip offset bytes */
-				m -= offset;
-				p = str+offset;
-				offset = 0;
-
-				/* write at most max(n,m) bytes */
-				if(m > n)
-					m = n;
-				memmove(buf, p, m);
-				n -= m;
-				buf += m;
-
-				if(n == 0)
-					return oldn;
-			}
+			m = snprint(buf, sizeof(buf), "%11d %11d %.*s\n", vno, v->irq, KNAMELEN, v->name);
+			offset -= m;
+			if(offset >= 0)
+				continue;
+			if(n > -offset)
+				n = -offset;
+			offset += m;
+			memmove(a, buf+offset, n);
+			return n;
 		}
 	}
-	return oldn - n;
+	return 0;
 }
 
 void
--- a/sys/src/9/xen/trap.c
+++ b/sys/src/9/xen/trap.c
@@ -123,42 +123,29 @@
 }
 
 static long
-irqallocread(Chan*, void *vbuf, long n, vlong offset)
+irqallocread(Chan*, void *a, long n, vlong offset)
 {
-	char *buf, *p, str[2*(11+1)+KNAMELEN+1+1];
-	int m, vno;
-	long oldn;
+	char buf[2*(11+1)+KNAMELEN+1+1];
+	int vno, m;
 	Vctl *v;
 
 	if(n < 0 || offset < 0)
 		error(Ebadarg);
 
-	oldn = n;
-	buf = vbuf;
 	for(vno=0; vno<nelem(vctl); vno++){
 		for(v=vctl[vno]; v; v=v->next){
-			m = snprint(str, sizeof str, "%11d %11d %.*s\n", vno, v->irq, KNAMELEN, v->name);
-			if(m <= offset)	/* if do not want this, skip entry */
-				offset -= m;
-			else{
-				/* skip offset bytes */
-				m -= offset;
-				p = str+offset;
-				offset = 0;
-
-				/* write at most max(n,m) bytes */
-				if(m > n)
-					m = n;
-				memmove(buf, p, m);
-				n -= m;
-				buf += m;
-
-				if(n == 0)
-					return oldn;
-			}	
+			m = snprint(buf, sizeof(buf), "%11d %11d %.*s\n", vno, v->irq, KNAMELEN, v->name);
+			offset -= m;
+			if(offset >= 0)
+				continue;
+			if(n > -offset)
+				n = -offset;
+			offset += m;
+			memmove(a, buf+offset, n);
+			return n;
 		}
 	}
-	return oldn - n;
+	return 0;
 }
 
 void