5 Slop = 100, /* room to grow with reallocation */
10 sizecache(Buffer *b, uint n)
15 b->c = runerealloc(b->c, b->cmax);
20 addblock(Buffer *b, uint i, uint n)
23 panic("internal error: addblock");
25 b->bl = realloc(b->bl, (b->nbl+1)*sizeof b->bl[0]);
27 memmove(b->bl+i+1, b->bl+i, (b->nbl-i)*sizeof(Block*));
28 b->bl[i] = disknewblock(disk, n);
35 delblock(Buffer *b, uint i)
38 panic("internal error: delblock");
40 diskrelease(disk, b->bl[i]);
43 memmove(b->bl+i, b->bl+i+1, (b->nbl-i)*sizeof(Block*));
44 b->bl = realloc(b->bl, b->nbl*sizeof b->bl[0]);
48 * Move cache so b->cq <= q0 < b->cq+b->cnc.
49 * If at very end, q0 will fall on end of cache block.
56 if(b->cdirty || b->cnc==0){
60 diskwrite(disk, &b->bl[b->cbi], b->c, b->cnc);
67 setcache(Buffer *b, uint q0)
73 panic("internal error: setcache");
75 * flush and reload if q0 is not in cache.
77 if(b->nc == 0 || (b->cq<=q0 && q0<b->cq+b->cnc))
80 * if q0 is at end of file and end of cache, continue to grow this block
82 if(q0==b->nc && q0==b->cq+b->cnc && b->cnc<=Maxblock)
94 while(q+(*blp)->u.n <= q0 && q+(*blp)->u.n < b->nc){
99 panic("block not found");
102 /* remember position */
105 sizecache(b, bl->u.n);
108 diskread(disk, bl, b->c, b->cnc);
112 bufinsert(Buffer *b, uint q0, Rune *s, uint n)
117 panic("internal error: bufinsert");
122 if(b->cnc+n <= Maxblock){
123 /* Everything fits in one block. */
126 if(b->bl == nil){ /* allocate */
128 panic("internal error: bufinsert1 cnc!=0");
133 runemove(b->c+off+m, b->c+off, b->cnc-off);
134 runemove(b->c+off, s, m);
139 * We must make a new block. If q0 is at
140 * the very beginning or end of this block,
141 * just make a new block and fill it.
143 if(q0==b->cq || q0==b->cq+b->cnc){
146 m = min(n, Maxblock);
147 if(b->bl == nil){ /* allocate */
149 panic("internal error: bufinsert2 cnc!=0");
158 runemove(b->c, s, m);
165 * Split the block; cut off the right side and
172 diskwrite(disk, &b->bl[i], b->c+off, m);
176 * Now at end of block. Take as much input
177 * as possible and tack it on end of block.
179 m = min(n, Maxblock-b->cnc);
180 sizecache(b, b->cnc+m);
181 runemove(b->c+b->cnc, s, m);
193 bufdelete(Buffer *b, uint q0, uint q1)
197 if(!(q0<=q1 && q0<=b->nc && q1<=b->nc))
198 panic("internal error: bufdelete");
202 if(q1 > b->cq+b->cnc)
206 m = b->cnc - (off+n);
208 runemove(b->c+off, b->c+off+n, m);
217 bufload(Buffer *b, uint q0, int fd, int *nulls)
225 panic("internal error: bufload");
226 p = malloc((Maxblock+UTFmax+1)*sizeof p[0]);
228 panic("bufload: malloc failed");
229 r = runemalloc(Maxblock);
234 * At top of loop, may have m bytes left over from
235 * last pass, possibly representing a partial rune.
238 n = read(fd, p+m, Maxblock);
248 cvttorunes(p, l, r, &nb, &nr, nulls);
249 memmove(p, p+nb, m-nb);
251 bufinsert(b, q1, r, nr);
260 bufread(Buffer *b, uint q0, Rune *s, uint n)
264 if(!(q0<=b->nc && q0+n<=b->nc))
265 panic("bufread: internal error");
269 m = min(n, b->cnc-(q0-b->cq));
270 runemove(s, b->c+(q0-b->cq), m);
287 /* delete backwards to avoid n² behavior */
288 for(i=b->nbl-1; --i>=0; )