16 Slop = 100 /* room to grow with reallocation */
21 sizecache(Buffer *b, uint n)
26 b->c = runerealloc(b->c, b->cmax);
31 addblock(Buffer *b, uint i, uint n)
34 error("internal error: addblock");
36 b->bl = realloc(b->bl, (b->nbl+1)*sizeof b->bl[0]);
38 memmove(b->bl+i+1, b->bl+i, (b->nbl-i)*sizeof(Block*));
39 b->bl[i] = disknewblock(disk, n);
45 delblock(Buffer *b, uint i)
48 error("internal error: delblock");
50 diskrelease(disk, b->bl[i]);
53 memmove(b->bl+i, b->bl+i+1, (b->nbl-i)*sizeof(Block*));
54 b->bl = realloc(b->bl, b->nbl*sizeof b->bl[0]);
58 * Move cache so b->cq <= q0 < b->cq+b->cnc.
59 * If at very end, q0 will fall on end of cache block.
66 if(b->cdirty || b->cnc==0){
70 diskwrite(disk, &b->bl[b->cbi], b->c, b->cnc);
77 setcache(Buffer *b, uint q0)
83 error("internal error: setcache");
85 * flush and reload if q0 is not in cache.
87 if(b->nc == 0 || (b->cq<=q0 && q0<b->cq+b->cnc))
90 * if q0 is at end of file and end of cache, continue to grow this block
92 if(q0==b->nc && q0==b->cq+b->cnc && b->cnc<Maxblock)
104 while(q+(*blp)->u.n <= q0 && q+(*blp)->u.n < b->nc){
109 error("block not found");
112 /* remember position */
115 sizecache(b, bl->u.n);
118 diskread(disk, bl, b->c, b->cnc);
122 bufinsert(Buffer *b, uint q0, Rune *s, uint n)
127 error("internal error: bufinsert");
132 if(b->cnc+n <= Maxblock){
133 /* Everything fits in one block. */
136 if(b->bl == nil){ /* allocate */
138 error("internal error: bufinsert1 cnc!=0");
143 runemove(b->c+off+m, b->c+off, b->cnc-off);
144 runemove(b->c+off, s, m);
149 * We must make a new block. If q0 is at
150 * the very beginning or end of this block,
151 * just make a new block and fill it.
153 if(q0==b->cq || q0==b->cq+b->cnc){
156 m = min(n, Maxblock);
157 if(b->bl == nil){ /* allocate */
159 error("internal error: bufinsert2 cnc!=0");
168 runemove(b->c, s, m);
175 * Split the block; cut off the right side and
182 diskwrite(disk, &b->bl[i], b->c+off, m);
186 * Now at end of block. Take as much input
187 * as possible and tack it on end of block.
189 m = min(n, Maxblock-b->cnc);
190 sizecache(b, b->cnc+m);
191 runemove(b->c+b->cnc, s, m);
203 bufdelete(Buffer *b, uint q0, uint q1)
207 if(!(q0<=q1 && q0<=b->nc && q1<=b->nc))
208 error("internal error: bufdelete");
212 if(q1 > b->cq+b->cnc)
216 m = b->cnc - (off+n);
218 runemove(b->c+off, b->c+off+n, m);
227 bufloader(void *v, uint q0, Rune *r, int nr)
229 bufinsert(v, q0, r, nr);
234 loadfile(int fd, uint q0, int *nulls, int(*f)(void*, uint, Rune*, int), void *arg)
241 p = emalloc((Maxblock+UTFmax+1)*sizeof p[0]);
242 r = runemalloc(Maxblock);
247 * At top of loop, may have m bytes left over from
248 * last pass, possibly representing a partial rune.
251 n = read(fd, p+m, Maxblock);
253 warning(nil, "read error in Buffer.load");
261 cvttorunes(p, l, r, &nb, &nr, nulls);
262 memmove(p, p+nb, m-nb);
264 q1 += (*f)(arg, q1, r, nr);
272 bufload(Buffer *b, uint q0, int fd, int *nulls)
275 error("internal error: bufload");
276 return loadfile(fd, q0, nulls, bufloader, b);
280 bufread(Buffer *b, uint q0, Rune *s, uint n)
284 if(!(q0<=b->nc && q0+n<=b->nc))
285 error("bufread: internal error");
289 m = min(n, b->cnc-(q0-b->cq));
290 runemove(s, b->c+(q0-b->cq), m);
307 /* delete backwards to avoid n² behavior */
308 for(i=b->nbl-1; --i>=0; )