buff.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "sam.h"
  10. enum
  11. {
  12. Slop = 100, /* room to grow with reallocation */
  13. };
  14. static
  15. void
  16. sizecache(Buffer *b, uint n)
  17. {
  18. if(n <= b->cmax)
  19. return;
  20. b->cmax = n+Slop;
  21. b->c = runerealloc(b->c, b->cmax);
  22. }
  23. static
  24. void
  25. addblock(Buffer *b, uint i, uint n)
  26. {
  27. if(i > b->nbl)
  28. panic("internal error: addblock");
  29. b->bl = realloc(b->bl, (b->nbl+1)*sizeof b->bl[0]);
  30. if(i < b->nbl)
  31. memmove(b->bl+i+1, b->bl+i, (b->nbl-i)*sizeof(Block*));
  32. b->bl[i] = disknewblock(disk, n);
  33. b->nbl++;
  34. }
  35. static
  36. void
  37. delblock(Buffer *b, uint i)
  38. {
  39. if(i >= b->nbl)
  40. panic("internal error: delblock");
  41. diskrelease(disk, b->bl[i]);
  42. b->nbl--;
  43. if(i < b->nbl)
  44. memmove(b->bl+i, b->bl+i+1, (b->nbl-i)*sizeof(Block*));
  45. b->bl = realloc(b->bl, b->nbl*sizeof b->bl[0]);
  46. }
  47. /*
  48. * Move cache so b->cq <= q0 < b->cq+b->cnc.
  49. * If at very end, q0 will fall on end of cache block.
  50. */
  51. static
  52. void
  53. flush(Buffer *b)
  54. {
  55. if(b->cdirty || b->cnc==0){
  56. if(b->cnc == 0)
  57. delblock(b, b->cbi);
  58. else
  59. diskwrite(disk, &b->bl[b->cbi], b->c, b->cnc);
  60. b->cdirty = FALSE;
  61. }
  62. }
  63. static
  64. void
  65. setcache(Buffer *b, uint q0)
  66. {
  67. Block **blp, *bl;
  68. uint i, q;
  69. if(q0 > b->nc)
  70. panic("internal error: setcache");
  71. /*
  72. * flush and reload if q0 is not in cache.
  73. */
  74. if(b->nc == 0 || (b->cq<=q0 && q0<b->cq+b->cnc))
  75. return;
  76. /*
  77. * if q0 is at end of file and end of cache, continue to grow this block
  78. */
  79. if(q0==b->nc && q0==b->cq+b->cnc && b->cnc<=Maxblock)
  80. return;
  81. flush(b);
  82. /* find block */
  83. if(q0 < b->cq){
  84. q = 0;
  85. i = 0;
  86. }else{
  87. q = b->cq;
  88. i = b->cbi;
  89. }
  90. blp = &b->bl[i];
  91. while(q+(*blp)->n <= q0 && q+(*blp)->n < b->nc){
  92. q += (*blp)->n;
  93. i++;
  94. blp++;
  95. if(i >= b->nbl)
  96. panic("block not found");
  97. }
  98. bl = *blp;
  99. /* remember position */
  100. b->cbi = i;
  101. b->cq = q;
  102. sizecache(b, bl->n);
  103. b->cnc = bl->n;
  104. /*read block*/
  105. diskread(disk, bl, b->c, b->cnc);
  106. }
  107. void
  108. bufinsert(Buffer *b, uint q0, Rune *s, uint n)
  109. {
  110. uint i, m, t, off;
  111. if(q0 > b->nc)
  112. panic("internal error: bufinsert");
  113. while(n > 0){
  114. setcache(b, q0);
  115. off = q0-b->cq;
  116. if(b->cnc+n <= Maxblock){
  117. /* Everything fits in one block. */
  118. t = b->cnc+n;
  119. m = n;
  120. if(b->bl == nil){ /* allocate */
  121. if(b->cnc != 0)
  122. panic("internal error: bufinsert1 cnc!=0");
  123. addblock(b, 0, t);
  124. b->cbi = 0;
  125. }
  126. sizecache(b, t);
  127. runemove(b->c+off+m, b->c+off, b->cnc-off);
  128. runemove(b->c+off, s, m);
  129. b->cnc = t;
  130. goto Tail;
  131. }
  132. /*
  133. * We must make a new block. If q0 is at
  134. * the very beginning or end of this block,
  135. * just make a new block and fill it.
  136. */
  137. if(q0==b->cq || q0==b->cq+b->cnc){
  138. if(b->cdirty)
  139. flush(b);
  140. m = min(n, Maxblock);
  141. if(b->bl == nil){ /* allocate */
  142. if(b->cnc != 0)
  143. panic("internal error: bufinsert2 cnc!=0");
  144. i = 0;
  145. }else{
  146. i = b->cbi;
  147. if(q0 > b->cq)
  148. i++;
  149. }
  150. addblock(b, i, m);
  151. sizecache(b, m);
  152. runemove(b->c, s, m);
  153. b->cq = q0;
  154. b->cbi = i;
  155. b->cnc = m;
  156. goto Tail;
  157. }
  158. /*
  159. * Split the block; cut off the right side and
  160. * let go of it.
  161. */
  162. m = b->cnc-off;
  163. if(m > 0){
  164. i = b->cbi+1;
  165. addblock(b, i, m);
  166. diskwrite(disk, &b->bl[i], b->c+off, m);
  167. b->cnc -= m;
  168. }
  169. /*
  170. * Now at end of block. Take as much input
  171. * as possible and tack it on end of block.
  172. */
  173. m = min(n, Maxblock-b->cnc);
  174. sizecache(b, b->cnc+m);
  175. runemove(b->c+b->cnc, s, m);
  176. b->cnc += m;
  177. Tail:
  178. b->nc += m;
  179. q0 += m;
  180. s += m;
  181. n -= m;
  182. b->cdirty = TRUE;
  183. }
  184. }
  185. void
  186. bufdelete(Buffer *b, uint q0, uint q1)
  187. {
  188. uint m, n, off;
  189. if(!(q0<=q1 && q0<=b->nc && q1<=b->nc))
  190. panic("internal error: bufdelete");
  191. while(q1 > q0){
  192. setcache(b, q0);
  193. off = q0-b->cq;
  194. if(q1 > b->cq+b->cnc)
  195. n = b->cnc - off;
  196. else
  197. n = q1-q0;
  198. m = b->cnc - (off+n);
  199. if(m > 0)
  200. runemove(b->c+off, b->c+off+n, m);
  201. b->cnc -= n;
  202. b->cdirty = TRUE;
  203. q1 -= n;
  204. b->nc -= n;
  205. }
  206. }
  207. uint
  208. bufload(Buffer *b, uint q0, int fd, int *nulls)
  209. {
  210. char *p;
  211. Rune *r;
  212. int l, m, n, nb, nr;
  213. uint q1;
  214. if(q0 > b->nc)
  215. panic("internal error: bufload");
  216. p = malloc((Maxblock+UTFmax+1)*sizeof p[0]);
  217. if(p == nil)
  218. panic("bufload: malloc failed");
  219. r = runemalloc(Maxblock);
  220. m = 0;
  221. n = 1;
  222. q1 = q0;
  223. /*
  224. * At top of loop, may have m bytes left over from
  225. * last pass, possibly representing a partial rune.
  226. */
  227. while(n > 0){
  228. n = read(fd, p+m, Maxblock);
  229. if(n < 0){
  230. error(Ebufload);
  231. break;
  232. }
  233. m += n;
  234. p[m] = 0;
  235. l = m;
  236. if(n > 0)
  237. l -= UTFmax;
  238. cvttorunes(p, l, r, &nb, &nr, nulls);
  239. memmove(p, p+nb, m-nb);
  240. m -= nb;
  241. bufinsert(b, q1, r, nr);
  242. q1 += nr;
  243. }
  244. free(p);
  245. free(r);
  246. return q1-q0;
  247. }
  248. void
  249. bufread(Buffer *b, uint q0, Rune *s, uint n)
  250. {
  251. uint m;
  252. if(!(q0<=b->nc && q0+n<=b->nc))
  253. panic("bufread: internal error");
  254. while(n > 0){
  255. setcache(b, q0);
  256. m = min(n, b->cnc-(q0-b->cq));
  257. runemove(s, b->c+(q0-b->cq), m);
  258. q0 += m;
  259. s += m;
  260. n -= m;
  261. }
  262. }
  263. void
  264. bufreset(Buffer *b)
  265. {
  266. int i;
  267. b->nc = 0;
  268. b->cnc = 0;
  269. b->cq = 0;
  270. b->cdirty = 0;
  271. b->cbi = 0;
  272. /* delete backwards to avoid n² behavior */
  273. for(i=b->nbl-1; --i>=0; )
  274. delblock(b, i);
  275. }
  276. void
  277. bufclose(Buffer *b)
  278. {
  279. bufreset(b);
  280. free(b->c);
  281. b->c = nil;
  282. b->cnc = 0;
  283. free(b->bl);
  284. b->bl = nil;
  285. b->nbl = 0;
  286. }