block.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 <ip.h>
  12. #include <auth.h>
  13. #include "ppp.h"
  14. enum
  15. {
  16. PAD = 128,
  17. NLIST = (1<<5),
  18. BPOW = 10,
  19. };
  20. typedef struct Barena Barena;
  21. struct Barena
  22. {
  23. QLock;
  24. Block* list[NLIST];
  25. };
  26. static Barena area;
  27. #define ADEBUG if(0)
  28. /*
  29. * allocation tracing
  30. */
  31. enum
  32. {
  33. Npc= 64,
  34. };
  35. typedef struct Arefent Arefent;
  36. struct Arefent
  37. {
  38. uint pc;
  39. uint n;
  40. };
  41. typedef struct Aref Aref;
  42. struct Aref
  43. {
  44. Arefent tab[Npc];
  45. QLock;
  46. };
  47. Aref arefblock;
  48. static uint32_t callerpc(void*);
  49. static void aref(Aref*, uint32_t);
  50. static void aunref(Aref*, uint32_t);
  51. Block*
  52. allocb(int len)
  53. {
  54. int sz;
  55. Block *bp, **l;
  56. len += PAD;
  57. sz = (len>>BPOW)&(NLIST-1);
  58. qlock(&area);
  59. l = &area.list[sz];
  60. for(bp = *l; bp; bp = bp->flist) {
  61. if(bp->bsz >= len) {
  62. *l = bp->flist;
  63. qunlock(&area);
  64. bp->next = nil;
  65. bp->list = nil;
  66. bp->flist = nil;
  67. bp->base = (uint8_t*)bp+sizeof(Block);
  68. bp->rptr = bp->base+PAD;
  69. bp->wptr = bp->rptr;
  70. bp->lim = bp->base+bp->bsz;
  71. bp->flow = nil;
  72. bp->flags= 0;
  73. ADEBUG {
  74. bp->pc = callerpc(&len);
  75. aref(&arefblock, bp->pc);
  76. }
  77. return bp;
  78. }
  79. l = &bp->flist;
  80. }
  81. qunlock(&area);
  82. bp = mallocz(sizeof(Block)+len, 1);
  83. bp->bsz = len;
  84. bp->base = (uint8_t*)bp+sizeof(Block);
  85. bp->rptr = bp->base+PAD;
  86. bp->wptr = bp->rptr;
  87. bp->lim = bp->base+len;
  88. ADEBUG {
  89. bp->pc = callerpc(&len);
  90. aref(&arefblock, bp->pc);
  91. }
  92. return bp;
  93. }
  94. void
  95. freeb(Block *bp)
  96. {
  97. int sz;
  98. Block **l, *next;
  99. qlock(&area);
  100. while(bp) {
  101. sz = (bp->bsz>>BPOW)&(NLIST-1);
  102. l = &area.list[sz];
  103. bp->flist = *l;
  104. *l = bp;
  105. next = bp->next;
  106. /* to catch use after free */
  107. bp->rptr = (uint8_t*)0xdeadbabe;
  108. bp->wptr = (uint8_t*)0xdeadbabe;
  109. bp->next = (Block*)0xdeadbabe;
  110. bp->list = (Block*)0xdeadbabe;
  111. ADEBUG aunref(&arefblock, bp->pc);
  112. bp = next;
  113. }
  114. qunlock(&area);
  115. }
  116. /*
  117. * concatenate a list of blocks into a single one and make sure
  118. * the result is at least min uchars
  119. */
  120. Block*
  121. concat(Block *bp)
  122. {
  123. int len;
  124. Block *nb, *f;
  125. nb = allocb(blen(bp));
  126. for(f = bp; f; f = f->next) {
  127. len = BLEN(f);
  128. memmove(nb->wptr, f->rptr, len);
  129. nb->wptr += len;
  130. }
  131. freeb(bp);
  132. return nb;
  133. }
  134. int
  135. blen(Block *bp)
  136. {
  137. int len;
  138. len = 0;
  139. while(bp) {
  140. len += BLEN(bp);
  141. bp = bp->next;
  142. }
  143. return len;
  144. }
  145. Block *
  146. pullup(Block *bp, int n)
  147. {
  148. int i;
  149. Block *nbp;
  150. /*
  151. * this should almost always be true, the rest it
  152. * just for to avoid every caller checking.
  153. */
  154. if(BLEN(bp) >= n)
  155. return bp;
  156. /*
  157. * if not enough room in the first block,
  158. * add another to the front of the list.
  159. */
  160. if(bp->lim - bp->rptr < n){
  161. nbp = allocb(n);
  162. nbp->next = bp;
  163. bp = nbp;
  164. }
  165. /*
  166. * copy uchars from the trailing blocks into the first
  167. */
  168. n -= BLEN(bp);
  169. while(nbp = bp->next){
  170. i = BLEN(nbp);
  171. if(i >= n) {
  172. memmove(bp->wptr, nbp->rptr, n);
  173. bp->wptr += n;
  174. nbp->rptr += n;
  175. return bp;
  176. }
  177. else {
  178. memmove(bp->wptr, nbp->rptr, i);
  179. bp->wptr += i;
  180. bp->next = nbp->next;
  181. nbp->next = nil;
  182. freeb(nbp);
  183. n -= i;
  184. }
  185. }
  186. freeb(bp);
  187. return nil;
  188. }
  189. /*
  190. * Pad a block to the front with n uchars. This is used to add protocol
  191. * headers to the front of blocks.
  192. */
  193. Block*
  194. padb(Block *bp, int n)
  195. {
  196. Block *nbp;
  197. if(bp->rptr-bp->base >= n) {
  198. bp->rptr -= n;
  199. return bp;
  200. }
  201. else {
  202. /* fprint(2, "padb: required %d PAD %d\n", n, PAD) = malloc(sizeof(*required %d PAD %d\n", n, PAD))); */
  203. nbp = allocb(n);
  204. nbp->wptr = nbp->lim;
  205. nbp->rptr = nbp->wptr - n;
  206. nbp->next = bp;
  207. return nbp;
  208. }
  209. }
  210. Block *
  211. btrim(Block *bp, int offset, int len)
  212. {
  213. uint32_t l;
  214. Block *nb, *startb;
  215. if(blen(bp) < offset+len) {
  216. freeb(bp);
  217. return nil;
  218. }
  219. while((l = BLEN(bp)) < offset) {
  220. offset -= l;
  221. nb = bp->next;
  222. bp->next = nil;
  223. freeb(bp);
  224. bp = nb;
  225. }
  226. startb = bp;
  227. bp->rptr += offset;
  228. while((l = BLEN(bp)) < len) {
  229. len -= l;
  230. bp = bp->next;
  231. }
  232. bp->wptr -= (BLEN(bp) - len);
  233. bp->flags |= S_DELIM;
  234. if(bp->next) {
  235. freeb(bp->next);
  236. bp->next = nil;
  237. }
  238. return startb;
  239. }
  240. Block*
  241. copyb(Block *bp, int count)
  242. {
  243. int l;
  244. Block *nb, *head, **p;
  245. p = &head;
  246. head = nil;
  247. while(bp != nil && count != 0) {
  248. l = BLEN(bp);
  249. if(count < l)
  250. l = count;
  251. nb = allocb(l);
  252. memmove(nb->wptr, bp->rptr, l);
  253. nb->wptr += l;
  254. count -= l;
  255. nb->flags = bp->flags;
  256. *p = nb;
  257. p = &nb->next;
  258. bp = bp->next;
  259. }
  260. if(count) {
  261. nb = allocb(count);
  262. memset(nb->wptr, 0, count);
  263. nb->wptr += count;
  264. nb->flags |= S_DELIM;
  265. *p = nb;
  266. }
  267. if(blen(head) == 0)
  268. fprint(2, "copyb: zero length\n");
  269. return head;
  270. }
  271. int
  272. pullb(Block **bph, int count)
  273. {
  274. Block *bp;
  275. int n, uchars;
  276. uchars = 0;
  277. if(bph == nil)
  278. return 0;
  279. while(*bph != nil && count != 0) {
  280. bp = *bph;
  281. n = BLEN(bp);
  282. if(count < n)
  283. n = count;
  284. uchars += n;
  285. count -= n;
  286. bp->rptr += n;
  287. if(BLEN(bp) == 0) {
  288. *bph = bp->next;
  289. bp->next = nil;
  290. freeb(bp);
  291. }
  292. }
  293. return uchars;
  294. }
  295. /*
  296. * handy routines for keeping track of allocations
  297. */
  298. static uint32_t
  299. callerpc(void *a)
  300. {
  301. return ((uint32_t*)a)[-1];
  302. }
  303. static void
  304. aref(Aref *ap, uint32_t pc)
  305. {
  306. Arefent *a, *e;
  307. e = &ap->tab[Npc-1];
  308. qlock(ap);
  309. for(a = ap->tab; a < e; a++)
  310. if(a->pc == pc || a->pc == 0)
  311. break;
  312. a->pc = pc;
  313. a->n++;
  314. qunlock(ap);
  315. }
  316. static void
  317. aunref(Aref *ap, uint32_t pc)
  318. {
  319. Arefent *a, *e;
  320. e = &ap->tab[Npc-1];
  321. qlock(ap);
  322. for(a = ap->tab; a < e; a++)
  323. if(a->pc == pc || a->pc == 0)
  324. break;
  325. a->n--;
  326. qunlock(ap);
  327. }