Created
May 14, 2015 03:00
-
-
Save lf94/185ae79cfd0e6d2ea0cb to your computer and use it in GitHub Desktop.
Plan 9 Sam's buffer data management
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void | |
bufinsert(Buffer *b, uint q0, Rune *s, uint n) | |
{ | |
uint i, m, t, off; | |
if(q0 > b->nc) | |
panic("internal error: bufinsert"); | |
while(n > 0){ | |
setcache(b, q0); | |
off = q0-b->cq; | |
if(b->cnc+n <= Maxblock){ | |
/* Everything fits in one block. */ | |
t = b->cnc+n; | |
m = n; | |
if(b->bl == nil){ /* allocate */ | |
if(b->cnc != 0) | |
panic("internal error: bufinsert1 cnc!=0"); | |
addblock(b, 0, t); | |
b->cbi = 0; | |
} | |
sizecache(b, t); | |
runemove(b->c+off+m, b->c+off, b->cnc-off); | |
runemove(b->c+off, s, m); | |
b->cnc = t; | |
goto Tail; | |
} | |
/* | |
* We must make a new block. If q0 is at | |
* the very beginning or end of this block, | |
* just make a new block and fill it. | |
*/ | |
if(q0==b->cq || q0==b->cq+b->cnc){ | |
if(b->cdirty) | |
flush(b); | |
m = min(n, Maxblock); | |
if(b->bl == nil){ /* allocate */ | |
if(b->cnc != 0) | |
panic("internal error: bufinsert2 cnc!=0"); | |
i = 0; | |
}else{ | |
i = b->cbi; | |
if(q0 > b->cq) | |
i++; | |
} | |
addblock(b, i, m); | |
sizecache(b, m); | |
runemove(b->c, s, m); | |
b->cq = q0; | |
b->cbi = i; | |
b->cnc = m; | |
goto Tail; | |
} | |
/* | |
* Split the block; cut off the right side and | |
* let go of it. | |
*/ | |
m = b->cnc-off; | |
if(m > 0){ | |
i = b->cbi+1; | |
addblock(b, i, m); | |
diskwrite(disk, &b->bl[i], b->c+off, m); | |
b->cnc -= m; | |
} | |
/* | |
* Now at end of block. Take as much input | |
* as possible and tack it on end of block. | |
*/ | |
m = min(n, Maxblock-b->cnc); | |
sizecache(b, b->cnc+m); | |
runemove(b->c+b->cnc, s, m); | |
b->cnc += m; | |
Tail: | |
b->nc += m; | |
q0 += m; | |
s += m; | |
n -= m; | |
b->cdirty = TRUE; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment