ref: a74542613df2305bd2fde7485e55fce4fafee0e6
parent: 84e16f5b5c91048bfc3898654d99fb2bcfa9dfa2
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Mon Apr 4 02:47:26 EDT 2016
rio: /dev/kbd cleanup - use free running unsigned indeces for window kbdq - emalloc() vs malloc() in xfid
--- a/sys/src/cmd/rio/wind.c
+++ b/sys/src/cmd/rio/wind.c
@@ -159,7 +159,7 @@
void
winctl(void *arg)
{
- Rune *rp, *up;
+ Rune *rp, *up, r;
uint qh, q0;
int nr, nb, c, wid, i, npart, initial, lastb;
char *s, *t, part[3];
@@ -174,7 +174,7 @@
Wctlmesg wcm;
Completion *cr;
char *kbdq[32], *kbds;
- int kbdqr, kbdqw;
+ uint kbdqr, kbdqw;
w = arg;
threadsetname("winctl-id%d", w->id);
@@ -216,7 +216,6 @@
alts[Wgone].op = CHANNOP;
alts[NWALT].op = CHANEND;
- memset(kbdq, 0, sizeof(kbdq));
kbdqr = kbdqw = 0;
npart = 0;
lastb = -1;
@@ -258,45 +257,38 @@
}
switch(alt(alts)){
case WKbd:
- if(w->kbdopen){
- i = (kbdqw+1) % nelem(kbdq);
- if(i != kbdqr)
- kbdqw = i;
- } else if(*kbds == 'c'){
- Rune r;
-
- chartorune(&r, kbds+1);
- if(r)
- wkeyctl(w, r);
+ if(kbdqw - kbdqr < nelem(kbdq))
+ kbdq[kbdqw++ % nelem(kbdq)] = kbds;
+ else
+ free(kbds);
+ if(w->kbdopen)
+ continue;
+ while(kbdqr != kbdqw){
+ kbds = kbdq[kbdqr++ % nelem(kbdq)];
+ if(*kbds == 'c'){
+ chartorune(&r, kbds+1);
+ if(r)
+ wkeyctl(w, r);
+ }
+ free(kbds);
}
- free(kbdq[kbdqw]);
- kbdq[kbdqw] = kbds;
break;
-
case WKbdread:
recv(crm.c1, &pair);
- nb = pair.ns;
- pair.ns = 0;
- t = pair.s;
+ nb = 0;
while(kbdqr != kbdqw){
- int m;
-
- i = (kbdqr+1) % nelem(kbdq);
- if(kbdq[i] == nil)
+ kbds = kbdq[kbdqr % nelem(kbdq)];
+ i = strlen(kbds)+1;
+ if(nb+i > pair.ns)
break;
- m = strlen(kbdq[i])+1;
- nb -= m;
- if(nb < 0)
- break;
- memmove(t, kbdq[i], m);
- t += m, pair.ns += m;
- free(kbdq[i]);
- kbdq[i] = nil;
- kbdqr = i;
+ memmove((char*)pair.s + nb, kbds, i);
+ free(kbds);
+ nb += i;
+ kbdqr++;
}
+ pair.ns = nb;
send(crm.c2, &pair);
continue;
-
case WMouse:
if(w->mouseopen) {
w->mouse.counter++;
@@ -332,8 +324,8 @@
continue;
case WCtl:
if(wctlmesg(w, wcm.type, wcm.r, wcm.p) == Exited){
- for(i=0; i<nelem(kbdq); i++)
- free(kbdq[i]);
+ while(kbdqr != kbdqw)
+ free(kbdq[kbdqr++ % nelem(kbdq)]);
chanfree(crm.c1);
chanfree(crm.c2);
chanfree(mrm.cm);
--- a/sys/src/cmd/rio/xfid.c
+++ b/sys/src/cmd/rio/xfid.c
@@ -630,7 +630,7 @@
case Qwctl:
if(cnt < 4*12){
filsysrespond(x->fs, x, &fc, Etooshort);
- break;
+ return;
}
alts[Adata].c = w->wctlread;
goto Consmesg;
@@ -665,13 +665,13 @@
}
c1 = crm.c1;
c2 = crm.c2;
- t = malloc(cnt+UTFmax+1); /* room to unpack partial rune plus */
+ t = emalloc(cnt+UTFmax+1); /* room to unpack partial rune plus */
pair.s = t;
pair.ns = cnt;
send(c1, &pair);
recv(c2, &pair);
fc.data = pair.s;
- fc.count = pair.ns;
+ fc.count = min(cnt, pair.ns);
filsysrespond(x->fs, x, &fc, nil);
free(t);
break;