Block* disknewblock(Disk *d, uint n) { uint i, j, size; Block *b; size = ntosize(n, &i); b = d->free[i]; if(b) d->free[i] = b->u.next; else{ /* allocate in chunks to reduce malloc overhead */ if(blist == nil){ blist = emalloc(100*sizeof(Block)); for(j=0; j<100-1; j++) blist[j].u.next = &blist[j+1]; } b = blist; blist = b->u.next; b->addr = d->addr; d->addr += size; } b->u.n = n; return b; }
void diskrelease(Disk *d, Block *b) { uint i; ntosize(b->u.n, &i); b->u.next = d->free[i]; d->free[i] = b; }
void diskwrite(Disk *d, Block **bp, Rune *r, uint n) { int size, nsize; Block *b; b = *bp; size = ntosize(b->u.n, nil); nsize = ntosize(n, nil); if(size != nsize){ diskrelease(d, b); b = disknewblock(d, n); *bp = b; } if(pwrite(d->fd, r, n*sizeof(Rune), b->addr) != n*sizeof(Rune)) error("write error to temp file"); b->u.n = n; }
void diskread(Disk *d, Block *b, Rune *r, uint n) { if(n > b->u.n) error("internal error: diskread"); ntosize(b->u.n, nil); if(pread(d->fd, r, n*sizeof(Rune), b->addr) != n*sizeof(Rune)) error("read error from temp file"); }
void diskread(Disk *d, Block *b, Rune *r, uint n) { if(n > b->u.n) panic("internal error: diskread"); ntosize(b->u.n, nil); /* called only for sanity check on Maxblock */ if(pread(d->fd, r, n*sizeof(Rune), b->addr) != n*sizeof(Rune)) panic("read error from temp file"); }
void diskread(Disk *d, Block *b, Rune *r, uint n) { int tot, nr; char *p; if(n > b->n) error("internal error: diskread"); ntosize(b->n, nil); n *= sizeof(Rune); p = (char*)r; for(tot = 0; tot < n; tot += nr){ nr = pread(d->fd, p+tot, n-tot, b->addr+tot); if(nr <= 0) error("read error from temp file"); } if(tot != n) error("read error from temp file"); }