buff.c 5.8 KB

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