int
cachechars(Font *f, char **ss, Rune **rr, ushort *cp, int max, int *wp, char **subfontname)
{
- int i, j, th, sh, h, w, rw, wid, nc;
+ int i, j, h, w, rw, wid, nc;
char *sp;
Rune r, *rp, vr;
ulong a;
rp = *rr;
}
wid = 0;
- *subfontname = 0;
+ *subfontname = nil;
for(i=0; i<max && (*sp || *rp); sp+=w, rp+=rw){
if(ss){
r = *(uchar*)sp;
rw = 1;
}
- sh = (17 * (uint)r) & (f->ncache-NFLOOK-1);
- c = &f->cache[sh];
+ a = ~0;
+ h = (17 * (uint)r) & (f->ncache-NFLOOK-1);
+ c = &f->cache[h];
+ tc = c;
ec = c+NFLOOK;
- h = sh;
while(c < ec){
if(c->value==r && c->age)
goto Found;
+ if(c->age < a){
+ a = c->age;
+ tc = c;
+ }
c++;
h++;
}
-
- /*
- * Not found; toss out oldest entry
- */
- a = ~0;
- th = sh;
- tc = &f->cache[th];
- while(tc < ec){
- if(tc->age < a){
- a = tc->age;
- h = th;
- c = tc;
- }
- tc++;
- th++;
- }
+ /* Not found; use oldest entry */
+ c = tc;
+ h = tc - f->cache;
if(a && (f->age-a)<500){ /* kicking out too recent; resize */
nc = 2*(f->ncache-NFLOOK) + NFLOOK;
name = cf->subfontname;
if(name == nil){
- if(f->display && f->display->screenimage)
+ if(f->display != nil && f->display->screenimage != nil)
depth = f->display->screenimage->depth;
else
depth = 8;
subf->age = 0;
}else{ /* too recent; grow instead */
of = f->subf;
- f->subf = malloc((f->nsubf+DSUBF)*sizeof *subf);
+ f->subf = realloc(of, (f->nsubf+DSUBF)*sizeof *subf);
if(f->subf == nil){
f->subf = of;
goto Toss;
}
- memmove(f->subf, of, (f->nsubf+DSUBF)*sizeof *subf);
- memset(f->subf+f->nsubf, 0, DSUBF*sizeof *subf);
subf = &f->subf[f->nsubf];
+ memset(subf, 0, DSUBF*sizeof *subf);
f->nsubf += DSUBF;
- free(of);
}
}
subf->age = 0;
/* c is still valid as didn't reallocate f->cache */
}
c->value = r;
- top = fi->top + (f->ascent-subf->f->ascent);
- bottom = fi->bottom + (f->ascent-subf->f->ascent);
c->width = fi->width;
c->x = h*f->width;
c->left = fi->left;
if(f->display == nil)
return 1;
- flushimage(f->display, 0); /* flush any pending errors */
b = bufimage(f->display, 37);
- if(b == 0)
+ if(b == nil)
return 0;
+ top = fi->top + (f->ascent-subf->f->ascent);
+ bottom = fi->bottom + (f->ascent-subf->f->ascent);
b[0] = 'l';
BPLONG(b+1, f->cacheimage->id);
BPLONG(b+5, subf->f->bits->id);
- BPSHORT(b+9, c-f->cache);
+ BPSHORT(b+9, h);
BPLONG(b+11, c->x);
BPLONG(b+15, top);
- BPLONG(b+19, c->x+((fi+1)->x-fi->x));
+ BPLONG(b+19, c->x+wid);
BPLONG(b+23, bottom);
BPLONG(b+27, fi->x);
BPLONG(b+31, fi->top);
fprint(2, "font cache resize failed: %r\n");
goto Return;
}
- flushimage(d, 0); /* flush any pending errors */
b = bufimage(d, 1+4+4+1);
- if(b == 0){
+ if(b == nil){
freeimage(new);
goto Return;
}
BPLONG(b+1, new->id);
BPLONG(b+5, ncache);
b[9] = f->ascent;
- if(flushimage(d, 0) < 0){
- fprint(2, "resize: init failed: %r\n");
- freeimage(new);
- goto Return;
- }
freeimage(f->cacheimage);
f->cacheimage = new;
Nodisplay: