pool.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465
  1. /*
  2. * This allocator takes blocks from a coarser allocator (p->alloc) and
  3. * uses them as arenas.
  4. *
  5. * An arena is split into a sequence of blocks of variable size. The
  6. * blocks begin with a Bhdr that denotes the length (including the Bhdr)
  7. * of the block. An arena begins with an Arena header block (Arena,
  8. * ARENA_MAGIC) and ends with a Bhdr block with magic ARENATAIL_MAGIC and
  9. * size 0. Intermediate blocks are either allocated or free. At the end
  10. * of each intermediate block is a Btail, which contains information
  11. * about where the block starts. This is useful for walking backwards.
  12. *
  13. * Free blocks (Free*) have a magic value of FREE_MAGIC in their Bhdr
  14. * headers. They are kept in a binary tree (p->freeroot) traversible by
  15. * walking ->left and ->right. Each node of the binary tree is a pointer
  16. * to a circular doubly-linked list (next, prev) of blocks of identical
  17. * size. Blocks are added to this ``tree of lists'' by pooladd(), and
  18. * removed by pooldel().
  19. *
  20. * When freed, adjacent blocks are coalesced to create larger blocks when
  21. * possible.
  22. *
  23. * Allocated blocks (Alloc*) have one of two magic values: ALLOC_MAGIC or
  24. * UNALLOC_MAGIC. When blocks are released from the pool, they have
  25. * magic value UNALLOC_MAGIC. Once the block has been trimmed by trim()
  26. * and the amount of user-requested data has been recorded in the
  27. * datasize field of the tail, the magic value is changed to ALLOC_MAGIC.
  28. * All blocks returned to callers should be of type ALLOC_MAGIC, as
  29. * should all blocks passed to us by callers. The amount of data the user
  30. * asked us for can be found by subtracting the short in tail->datasize
  31. * from header->size. Further, the up to at most four bytes between the
  32. * end of the user-requested data block and the actual Btail structure are
  33. * marked with a magic value, which is checked to detect user overflow.
  34. *
  35. * The arenas returned by p->alloc are kept in a doubly-linked list
  36. * (p->arenalist) running through the arena headers, sorted by descending
  37. * base address (prev, next). When a new arena is allocated, we attempt
  38. * to merge it with its two neighbors via p->merge.
  39. */
  40. #include <u.h>
  41. #include <libc.h>
  42. #include <pool.h>
  43. typedef struct Alloc Alloc;
  44. typedef struct Arena Arena;
  45. typedef struct Bhdr Bhdr;
  46. typedef struct Btail Btail;
  47. typedef struct Free Free;
  48. struct Bhdr {
  49. ulong magic;
  50. ulong size;
  51. };
  52. enum {
  53. NOT_MAGIC = 0xdeadfa11,
  54. DEAD_MAGIC = 0xdeaddead,
  55. };
  56. #define B2NB(b) ((Bhdr*)((uchar*)(b)+(b)->size))
  57. #define SHORT(x) (((x)[0] << 8) | (x)[1])
  58. #define PSHORT(p, x) \
  59. (((uchar*)(p))[0] = ((x)>>8)&0xFF, \
  60. ((uchar*)(p))[1] = (x)&0xFF)
  61. enum {
  62. TAIL_MAGIC0 = 0xBE,
  63. TAIL_MAGIC1 = 0xEF
  64. };
  65. struct Btail {
  66. uchar magic0;
  67. uchar datasize[2];
  68. uchar magic1;
  69. ulong size; /* same as Bhdr->size */
  70. };
  71. #define B2T(b) ((Btail*)((uchar*)(b)+(b)->size-sizeof(Btail)))
  72. #define B2PT(b) ((Btail*)((uchar*)(b)-sizeof(Btail)))
  73. #define T2HDR(t) ((Bhdr*)((uchar*)(t)+sizeof(Btail)-(t)->size))
  74. struct Free {
  75. Bhdr;
  76. Free* left;
  77. Free* right;
  78. Free* next;
  79. Free* prev;
  80. };
  81. enum {
  82. FREE_MAGIC = 0xBA5EBA11,
  83. };
  84. /*
  85. * the point of the notused fields is to make 8c differentiate
  86. * between Bhdr and Allocblk, and between Kempt and Unkempt.
  87. */
  88. struct Alloc {
  89. Bhdr;
  90. };
  91. enum {
  92. ALLOC_MAGIC = 0x0A110C09,
  93. UNALLOC_MAGIC = 0xCAB00D1E+1,
  94. };
  95. struct Arena {
  96. Bhdr;
  97. Arena* aup;
  98. Arena* down;
  99. ulong asize;
  100. ulong pad; /* to a multiple of 8 bytes */
  101. };
  102. enum {
  103. ARENA_MAGIC = 0xC0A1E5CE+1,
  104. ARENATAIL_MAGIC = 0xEC5E1A0C+1,
  105. };
  106. #define A2TB(a) ((Bhdr*)((uchar*)(a)+(a)->asize-sizeof(Bhdr)))
  107. #define A2B(a) B2NB(a)
  108. enum {
  109. ALIGN_MAGIC = 0xA1F1D1C1,
  110. };
  111. enum {
  112. MINBLOCKSIZE = sizeof(Free)+sizeof(Btail)
  113. };
  114. static uchar datamagic[] = { 0xFE, 0xF1, 0xF0, 0xFA };
  115. #define Poison (void*)0xCafeBabe
  116. #define _B2D(a) ((void*)((uchar*)a+sizeof(Bhdr)))
  117. #define _D2B(v) ((Alloc*)((uchar*)v-sizeof(Bhdr)))
  118. // static void* _B2D(void*);
  119. // static void* _D2B(void*);
  120. static void* B2D(Pool*, Alloc*);
  121. static Alloc* D2B(Pool*, void*);
  122. static Arena* arenamerge(Pool*, Arena*, Arena*);
  123. static void blockcheck(Pool*, Bhdr*);
  124. static Alloc* blockmerge(Pool*, Bhdr*, Bhdr*);
  125. static Alloc* blocksetdsize(Pool*, Alloc*, ulong);
  126. static Bhdr* blocksetsize(Bhdr*, ulong);
  127. static ulong bsize2asize(Pool*, ulong);
  128. static ulong dsize2bsize(Pool*, ulong);
  129. static ulong getdsize(Alloc*);
  130. static Alloc* trim(Pool*, Alloc*, ulong);
  131. static Free* listadd(Free*, Free*);
  132. static Free* listdelete(Free*, Free*);
  133. static void logstack(Pool*);
  134. static Free** ltreewalk(Free**, ulong);
  135. static void memmark(void*, int, ulong);
  136. static Free* pooladd(Pool*, Alloc*);
  137. static void* poolallocl(Pool*, ulong);
  138. static void poolcheckl(Pool*);
  139. static void poolcheckarena(Pool*, Arena*);
  140. static int poolcompactl(Pool*);
  141. static Alloc* pooldel(Pool*, Free*);
  142. static void pooldumpl(Pool*);
  143. static void pooldumparena(Pool*, Arena*);
  144. static void poolfreel(Pool*, void*);
  145. static void poolnewarena(Pool*, ulong);
  146. static void* poolreallocl(Pool*, void*, ulong);
  147. static Free* treedelete(Free*, Free*);
  148. static Free* treeinsert(Free*, Free*);
  149. static Free* treelookup(Free*, ulong);
  150. static Free* treelookupgt(Free*, ulong);
  151. /*
  152. * Debugging
  153. *
  154. * Antagonism causes blocks to always be filled with garbage if their
  155. * contents are undefined. This tickles both programs and the library.
  156. * It's a linear time hit but not so noticeable during nondegenerate use.
  157. * It would be worth leaving in except that it negates the benefits of the
  158. * kernel's demand-paging. The tail magic and end-of-data magic
  159. * provide most of the user-visible benefit that antagonism does anyway.
  160. *
  161. * Paranoia causes the library to recheck the entire pool on each lock
  162. * or unlock. A failed check on unlock means we tripped over ourselves,
  163. * while a failed check on lock tends to implicate the user. Paranoia has
  164. * the potential to slow things down a fair amount for pools with large
  165. * numbers of allocated blocks. It completely negates all benefits won
  166. * by the binary tree. Turning on paranoia in the kernel makes it painfully
  167. * slow.
  168. *
  169. * Verbosity induces the dumping of the pool via p->print at each lock operation.
  170. * By default, only one line is logged for each alloc, free, and realloc.
  171. */
  172. /* the if(!x);else avoids ``dangling else'' problems */
  173. #define antagonism if(!(p->flags & POOL_ANTAGONISM)){}else
  174. #define paranoia if(!(p->flags & POOL_PARANOIA)){}else
  175. #define verbosity if(!(p->flags & POOL_VERBOSITY)){}else
  176. #define DPRINT if(!(p->flags & POOL_DEBUGGING)){}else p->print
  177. #define LOG if(!(p->flags & POOL_LOGGING)){}else p->print
  178. /*
  179. * Tree walking
  180. */
  181. static void
  182. checklist(Free *t)
  183. {
  184. Free *q;
  185. for(q=t->next; q!=t; q=q->next){
  186. assert(q->size == t->size);
  187. assert(q->next==nil || q->next->prev==q);
  188. assert(q->prev==nil || q->prev->next==q);
  189. // assert(q->left==nil);
  190. // assert(q->right==nil);
  191. assert(q->magic==FREE_MAGIC);
  192. }
  193. }
  194. static void
  195. checktree(Free *t, int a, int b)
  196. {
  197. assert(t->magic==FREE_MAGIC);
  198. assert(a < t->size && t->size < b);
  199. assert(t->next==nil || t->next->prev==t);
  200. assert(t->prev==nil || t->prev->next==t);
  201. checklist(t);
  202. if(t->left)
  203. checktree(t->left, a, t->size);
  204. if(t->right)
  205. checktree(t->right, t->size, b);
  206. }
  207. /* ltreewalk: return address of pointer to node of size == size */
  208. static Free**
  209. ltreewalk(Free **t, ulong size)
  210. {
  211. assert(t != nil /* ltreewalk */);
  212. for(;;) {
  213. if(*t == nil)
  214. return t;
  215. assert((*t)->magic == FREE_MAGIC);
  216. if(size == (*t)->size)
  217. return t;
  218. if(size < (*t)->size)
  219. t = &(*t)->left;
  220. else
  221. t = &(*t)->right;
  222. }
  223. return nil; /* not reached */
  224. }
  225. /* treelookup: find node in tree with size == size */
  226. static Free*
  227. treelookup(Free *t, ulong size)
  228. {
  229. return *ltreewalk(&t, size);
  230. }
  231. /* treeinsert: insert node into tree */
  232. static Free*
  233. treeinsert(Free *tree, Free *node)
  234. {
  235. Free **loc, *repl;
  236. assert(node != nil /* treeinsert */);
  237. loc = ltreewalk(&tree, node->size);
  238. if(*loc == nil) {
  239. node->left = nil;
  240. node->right = nil;
  241. } else { /* replace existing node */
  242. repl = *loc;
  243. node->left = repl->left;
  244. node->right = repl->right;
  245. }
  246. *loc = node;
  247. return tree;
  248. }
  249. /* treedelete: remove node from tree */
  250. static Free*
  251. treedelete(Free *tree, Free *node)
  252. {
  253. Free **loc, **lsucc, *succ;
  254. assert(node != nil /* treedelete */);
  255. loc = ltreewalk(&tree, node->size);
  256. assert(*loc == node);
  257. if(node->left == nil)
  258. *loc = node->right;
  259. else if(node->right == nil)
  260. *loc = node->left;
  261. else {
  262. /* have two children, use inorder successor as replacement */
  263. for(lsucc = &node->right; (*lsucc)->left; lsucc = &(*lsucc)->left)
  264. ;
  265. succ = *lsucc;
  266. *lsucc = succ->right;
  267. succ->left = node->left;
  268. succ->right = node->right;
  269. *loc = succ;
  270. }
  271. node->left = node->right = Poison;
  272. return tree;
  273. }
  274. /* treelookupgt: find smallest node in tree with size >= size */
  275. static Free*
  276. treelookupgt(Free *t, ulong size)
  277. {
  278. Free *lastgood; /* last node we saw that was big enough */
  279. lastgood = nil;
  280. for(;;) {
  281. if(t == nil)
  282. return lastgood;
  283. if(size == t->size)
  284. return t;
  285. if(size < t->size) {
  286. lastgood = t;
  287. t = t->left;
  288. } else {
  289. t = t->right;
  290. }
  291. }
  292. return nil; /* not reached */
  293. }
  294. /*
  295. * List maintenance
  296. */
  297. /* listadd: add a node to a doubly linked list */
  298. static Free*
  299. listadd(Free *list, Free *node)
  300. {
  301. if(list == nil) {
  302. node->next = node;
  303. node->prev = node;
  304. return node;
  305. }
  306. node->prev = list->prev;
  307. node->next = list;
  308. node->prev->next = node;
  309. node->next->prev = node;
  310. return list;
  311. }
  312. /* listdelete: remove node from a doubly linked list */
  313. static Free*
  314. listdelete(Free *list, Free *node)
  315. {
  316. if(node->next == node) { /* singular list */
  317. node->prev = node->next = Poison;
  318. return nil;
  319. }
  320. node->next->prev = node->prev;
  321. node->prev->next = node->next;
  322. if(list == node)
  323. list = node->next;
  324. node->prev = node->next = Poison;
  325. return list;
  326. }
  327. /*
  328. * Pool maintenance
  329. */
  330. /* pooladd: add anode to the free pool */
  331. static Free*
  332. pooladd(Pool *p, Alloc *anode)
  333. {
  334. Free *lst, *olst;
  335. Free *node;
  336. Free **parent;
  337. antagonism {
  338. memmark(_B2D(anode), 0xF7, anode->size-sizeof(Bhdr)-sizeof(Btail));
  339. }
  340. node = (Free*)anode;
  341. node->magic = FREE_MAGIC;
  342. parent = ltreewalk(&p->freeroot, node->size);
  343. olst = *parent;
  344. lst = listadd(olst, node);
  345. if(olst != lst) /* need to update tree */
  346. *parent = treeinsert(*parent, lst);
  347. p->curfree += node->size;
  348. return node;
  349. }
  350. /* pooldel: remove node from the free pool */
  351. static Alloc*
  352. pooldel(Pool *p, Free *node)
  353. {
  354. Free *lst, *olst;
  355. Free **parent;
  356. parent = ltreewalk(&p->freeroot, node->size);
  357. olst = *parent;
  358. assert(olst != nil /* pooldel */);
  359. lst = listdelete(olst, node);
  360. if(lst == nil)
  361. *parent = treedelete(*parent, olst);
  362. else if(lst != olst)
  363. *parent = treeinsert(*parent, lst);
  364. node->left = node->right = Poison;
  365. p->curfree -= node->size;
  366. antagonism {
  367. memmark(_B2D(node), 0xF9, node->size-sizeof(Bhdr)-sizeof(Btail));
  368. }
  369. node->magic = UNALLOC_MAGIC;
  370. return (Alloc*)node;
  371. }
  372. /*
  373. * Block maintenance
  374. */
  375. /* block allocation */
  376. static ulong
  377. dsize2bsize(Pool *p, ulong sz)
  378. {
  379. sz += sizeof(Bhdr)+sizeof(Btail);
  380. if(sz < p->minblock)
  381. sz = p->minblock;
  382. if(sz < MINBLOCKSIZE)
  383. sz = MINBLOCKSIZE;
  384. sz = (sz+p->quantum-1)&~(p->quantum-1);
  385. return sz;
  386. }
  387. static ulong
  388. bsize2asize(Pool *p, ulong sz)
  389. {
  390. sz += sizeof(Arena)+sizeof(Btail);
  391. if(sz < p->minarena)
  392. sz = p->minarena;
  393. sz = (sz+p->quantum)&~(p->quantum-1);
  394. return sz;
  395. }
  396. /* blockmerge: merge a and b, known to be adjacent */
  397. /* both are removed from pool if necessary. */
  398. static Alloc*
  399. blockmerge(Pool *pool, Bhdr *a, Bhdr *b)
  400. {
  401. Btail *t;
  402. assert(B2NB(a) == b);
  403. if(a->magic == FREE_MAGIC)
  404. pooldel(pool, (Free*)a);
  405. if(b->magic == FREE_MAGIC)
  406. pooldel(pool, (Free*)b);
  407. t = B2T(a);
  408. t->size = (ulong)Poison;
  409. t->magic0 = NOT_MAGIC;
  410. t->magic1 = NOT_MAGIC;
  411. PSHORT(t->datasize, NOT_MAGIC);
  412. a->size += b->size;
  413. t = B2T(a);
  414. t->size = a->size;
  415. PSHORT(t->datasize, 0xFFFF);
  416. b->size = NOT_MAGIC;
  417. b->magic = NOT_MAGIC;
  418. a->magic = UNALLOC_MAGIC;
  419. return (Alloc*)a;
  420. }
  421. /* blocksetsize: set the total size of a block, fixing tail pointers */
  422. static Bhdr*
  423. blocksetsize(Bhdr *b, ulong bsize)
  424. {
  425. Btail *t;
  426. assert(b->magic != FREE_MAGIC /* blocksetsize */);
  427. b->size = bsize;
  428. t = B2T(b);
  429. t->size = b->size;
  430. t->magic0 = TAIL_MAGIC0;
  431. t->magic1 = TAIL_MAGIC1;
  432. return b;
  433. }
  434. /* getdsize: return the requested data size for an allocated block */
  435. static ulong
  436. getdsize(Alloc *b)
  437. {
  438. Btail *t;
  439. t = B2T(b);
  440. return b->size - SHORT(t->datasize);
  441. }
  442. /* blocksetdsize: set the user data size of a block */
  443. static Alloc*
  444. blocksetdsize(Pool *p, Alloc *b, ulong dsize)
  445. {
  446. Btail *t;
  447. uchar *q, *eq;
  448. assert(b->size >= dsize2bsize(p, dsize));
  449. assert(b->size - dsize < 0x10000);
  450. t = B2T(b);
  451. PSHORT(t->datasize, b->size - dsize);
  452. q=(uchar*)_B2D(b)+dsize;
  453. eq = (uchar*)t;
  454. if(eq > q+4)
  455. eq = q+4;
  456. for(; q<eq; q++)
  457. *q = datamagic[((ulong)(uintptr)q)%nelem(datamagic)];
  458. return b;
  459. }
  460. /* trim: trim a block down to what is needed to hold dsize bytes of user data */
  461. static Alloc*
  462. trim(Pool *p, Alloc *b, ulong dsize)
  463. {
  464. ulong extra, bsize;
  465. Alloc *frag;
  466. bsize = dsize2bsize(p, dsize);
  467. extra = b->size - bsize;
  468. if(b->size - dsize >= 0x10000 ||
  469. (extra >= bsize>>2 && extra >= MINBLOCKSIZE && extra >= p->minblock)) {
  470. blocksetsize(b, bsize);
  471. frag = (Alloc*) B2NB(b);
  472. antagonism {
  473. memmark(frag, 0xF1, extra);
  474. }
  475. frag->magic = UNALLOC_MAGIC;
  476. blocksetsize(frag, extra);
  477. pooladd(p, frag);
  478. }
  479. b->magic = ALLOC_MAGIC;
  480. blocksetdsize(p, b, dsize);
  481. return b;
  482. }
  483. static Alloc*
  484. freefromfront(Pool *p, Alloc *b, ulong skip)
  485. {
  486. Alloc *bb;
  487. skip = skip&~(p->quantum-1);
  488. if(skip >= 0x1000 || (skip >= b->size>>2 && skip >= MINBLOCKSIZE && skip >= p->minblock)){
  489. bb = (Alloc*)((uchar*)b+skip);
  490. blocksetsize(bb, b->size-skip);
  491. bb->magic = UNALLOC_MAGIC;
  492. blocksetsize(b, skip);
  493. b->magic = UNALLOC_MAGIC;
  494. pooladd(p, b);
  495. return bb;
  496. }
  497. return b;
  498. }
  499. /*
  500. * Arena maintenance
  501. */
  502. /* arenasetsize: set arena size, updating tail */
  503. static void
  504. arenasetsize(Arena *a, ulong asize)
  505. {
  506. Bhdr *atail;
  507. a->asize = asize;
  508. atail = A2TB(a);
  509. atail->magic = ARENATAIL_MAGIC;
  510. atail->size = 0;
  511. }
  512. /* poolnewarena: allocate new arena */
  513. static void
  514. poolnewarena(Pool *p, ulong asize)
  515. {
  516. Arena *a;
  517. Arena *ap, *lastap;
  518. Alloc *b;
  519. LOG(p, "newarena %lud\n", asize);
  520. if(p->cursize+asize > p->maxsize) {
  521. if(poolcompactl(p) == 0){
  522. LOG(p, "pool too big: %lud+%lud > %lud\n",
  523. p->cursize, asize, p->maxsize);
  524. werrstr("memory pool too large");
  525. }
  526. return;
  527. }
  528. if((a = p->alloc(asize)) == nil) {
  529. /* assume errstr set by p->alloc */
  530. return;
  531. }
  532. p->cursize += asize;
  533. /* arena hdr */
  534. a->magic = ARENA_MAGIC;
  535. blocksetsize(a, sizeof(Arena));
  536. arenasetsize(a, asize);
  537. blockcheck(p, a);
  538. /* create one large block in arena */
  539. b = (Alloc*)A2B(a);
  540. b->magic = UNALLOC_MAGIC;
  541. blocksetsize(b, (uchar*)A2TB(a)-(uchar*)b);
  542. blockcheck(p, b);
  543. pooladd(p, b);
  544. blockcheck(p, b);
  545. /* sort arena into descending sorted arena list */
  546. for(lastap=nil, ap=p->arenalist; ap > a; lastap=ap, ap=ap->down)
  547. ;
  548. if(a->down = ap) /* assign = */
  549. a->down->aup = a;
  550. if(a->aup = lastap) /* assign = */
  551. a->aup->down = a;
  552. else
  553. p->arenalist = a;
  554. /* merge with surrounding arenas if possible */
  555. /* must do a with up before down with a (think about it) */
  556. if(a->aup)
  557. arenamerge(p, a, a->aup);
  558. if(a->down)
  559. arenamerge(p, a->down, a);
  560. }
  561. /* blockresize: grow a block to encompass space past its end, possibly by */
  562. /* trimming it into two different blocks. */
  563. static void
  564. blockgrow(Pool *p, Bhdr *b, ulong nsize)
  565. {
  566. if(b->magic == FREE_MAGIC) {
  567. Alloc *a;
  568. Bhdr *bnxt;
  569. a = pooldel(p, (Free*)b);
  570. blockcheck(p, a);
  571. blocksetsize(a, nsize);
  572. blockcheck(p, a);
  573. bnxt = B2NB(a);
  574. if(bnxt->magic == FREE_MAGIC)
  575. a = blockmerge(p, a, bnxt);
  576. blockcheck(p, a);
  577. pooladd(p, a);
  578. } else {
  579. Alloc *a;
  580. ulong dsize;
  581. a = (Alloc*)b;
  582. dsize = getdsize(a);
  583. blocksetsize(a, nsize);
  584. trim(p, a, dsize);
  585. }
  586. }
  587. /* arenamerge: attempt to coalesce to arenas that might be adjacent */
  588. static Arena*
  589. arenamerge(Pool *p, Arena *bot, Arena *top)
  590. {
  591. Bhdr *bbot, *btop;
  592. Btail *t;
  593. blockcheck(p, bot);
  594. blockcheck(p, top);
  595. assert(bot->aup == top && top > bot);
  596. if(p->merge == nil || p->merge(bot, top) == 0)
  597. return nil;
  598. /* remove top from list */
  599. if(bot->aup = top->aup) /* assign = */
  600. bot->aup->down = bot;
  601. else
  602. p->arenalist = bot;
  603. /* save ptrs to last block in bot, first block in top */
  604. t = B2PT(A2TB(bot));
  605. bbot = T2HDR(t);
  606. btop = A2B(top);
  607. blockcheck(p, bbot);
  608. blockcheck(p, btop);
  609. /* grow bottom arena to encompass top */
  610. arenasetsize(bot, top->asize + ((uchar*)top - (uchar*)bot));
  611. /* grow bottom block to encompass space between arenas */
  612. blockgrow(p, bbot, (uchar*)btop-(uchar*)bbot);
  613. blockcheck(p, bbot);
  614. return bot;
  615. }
  616. /* dumpblock: print block's vital stats */
  617. static void
  618. dumpblock(Pool *p, Bhdr *b)
  619. {
  620. ulong *dp;
  621. ulong dsize;
  622. uchar *cp;
  623. dp = (ulong*)b;
  624. p->print(p, "pool %s block %p\nhdr %.8lux %.8lux %.8lux %.8lux %.8lux %.8lux\n",
  625. p->name, b, dp[0], dp[1], dp[2], dp[3], dp[4], dp[5], dp[6]);
  626. if(b->size >= 1024*1024*1024) /* tail pointer corrupt; printing tail will fault */
  627. return;
  628. dp = (ulong*)B2T(b);
  629. p->print(p, "tail %.8lux %.8lux %.8lux %.8lux %.8lux %.8lux | %.8lux %.8lux\n",
  630. dp[-6], dp[-5], dp[-4], dp[-3], dp[-2], dp[-1], dp[0], dp[1]);
  631. if(b->magic == ALLOC_MAGIC){
  632. dsize = getdsize((Alloc*)b);
  633. if(dsize >= b->size) /* user data size corrupt */
  634. return;
  635. cp = (uchar*)_B2D(b)+dsize;
  636. p->print(p, "user data ");
  637. p->print(p, "%.2ux %.2ux %.2ux %.2ux %.2ux %.2ux %.2ux %.2ux",
  638. cp[-8], cp[-7], cp[-6], cp[-5], cp[-4], cp[-3], cp[-2], cp[-1]);
  639. p->print(p, " | %.2ux %.2ux %.2ux %.2ux %.2ux %.2ux %.2ux %.2ux\n",
  640. cp[0], cp[1], cp[2], cp[3], cp[4], cp[5], cp[6], cp[7]);
  641. }
  642. }
  643. static void
  644. printblock(Pool *p, Bhdr *b, char *msg)
  645. {
  646. p->print(p, "%s\n", msg);
  647. dumpblock(p, b);
  648. }
  649. static void
  650. panicblock(Pool *p, Bhdr *b, char *msg)
  651. {
  652. p->print(p, "%s\n", msg);
  653. dumpblock(p, b);
  654. p->panic(p, "pool panic");
  655. }
  656. /* blockcheck: ensure a block consistent with our expectations */
  657. /* should only be called when holding pool lock */
  658. static void
  659. blockcheck(Pool *p, Bhdr *b)
  660. {
  661. Alloc *a;
  662. Btail *t;
  663. int i, n;
  664. uchar *q, *bq, *eq;
  665. ulong dsize;
  666. switch(b->magic) {
  667. default:
  668. panicblock(p, b, "bad magic");
  669. case FREE_MAGIC:
  670. case UNALLOC_MAGIC:
  671. t = B2T(b);
  672. if(t->magic0 != TAIL_MAGIC0 || t->magic1 != TAIL_MAGIC1)
  673. panicblock(p, b, "corrupt tail magic");
  674. if(T2HDR(t) != b)
  675. panicblock(p, b, "corrupt tail ptr");
  676. break;
  677. case DEAD_MAGIC:
  678. t = B2T(b);
  679. if(t->magic0 != TAIL_MAGIC0 || t->magic1 != TAIL_MAGIC1)
  680. panicblock(p, b, "corrupt tail magic");
  681. if(T2HDR(t) != b)
  682. panicblock(p, b, "corrupt tail ptr");
  683. n = getdsize((Alloc*)b);
  684. q = _B2D(b);
  685. q += 8;
  686. for(i=8; i<n; i++)
  687. if(*q++ != 0xDA)
  688. panicblock(p, b, "dangling pointer write");
  689. break;
  690. case ARENA_MAGIC:
  691. b = A2TB((Arena*)b);
  692. if(b->magic != ARENATAIL_MAGIC)
  693. panicblock(p, b, "bad arena size");
  694. /* fall through */
  695. case ARENATAIL_MAGIC:
  696. if(b->size != 0)
  697. panicblock(p, b, "bad arena tail size");
  698. break;
  699. case ALLOC_MAGIC:
  700. a = (Alloc*)b;
  701. if(a->size > 1024*1024*1024)
  702. panicblock(p, b, "block too big");
  703. t = B2T(b);
  704. dsize = getdsize(a);
  705. bq = (uchar*)_B2D(a)+dsize;
  706. eq = (uchar*)t;
  707. if(t->magic0 != TAIL_MAGIC0){
  708. /* if someone wrote exactly one byte over and it was a NUL, we sometimes only complain. */
  709. if((p->flags & POOL_TOLERANCE) && bq == eq && t->magic0 == 0)
  710. printblock(p, b, "mem user overflow (magic0)");
  711. else
  712. panicblock(p, b, "corrupt tail magic0");
  713. }
  714. if(t->magic1 != TAIL_MAGIC1)
  715. panicblock(p, b, "corrupt tail magic1");
  716. if(T2HDR(t) != b)
  717. panicblock(p, b, "corrupt tail ptr");
  718. if(dsize2bsize(p, dsize) > a->size)
  719. panicblock(p, b, "too much block data");
  720. if(eq > bq+4)
  721. eq = bq+4;
  722. for(q=bq; q<eq; q++){
  723. if(*q != datamagic[((uintptr)q)%nelem(datamagic)]){
  724. if(q == bq && *q == 0 && (p->flags & POOL_TOLERANCE)){
  725. printblock(p, b, "mem user overflow");
  726. continue;
  727. }
  728. panicblock(p, b, "mem user overflow");
  729. }
  730. }
  731. break;
  732. }
  733. }
  734. /*
  735. * compact an arena by shifting all the free blocks to the end.
  736. * assumes pool lock is held.
  737. */
  738. enum {
  739. FLOATING_MAGIC = 0xCBCBCBCB, /* temporarily neither allocated nor in the free tree */
  740. };
  741. static int
  742. arenacompact(Pool *p, Arena *a)
  743. {
  744. Bhdr *b, *wb, *eb, *nxt;
  745. int compacted;
  746. if(p->move == nil)
  747. p->panic(p, "don't call me when pool->move is nil\n");
  748. poolcheckarena(p, a);
  749. eb = A2TB(a);
  750. compacted = 0;
  751. for(b=wb=A2B(a); b && b < eb; b=nxt) {
  752. nxt = B2NB(b);
  753. switch(b->magic) {
  754. case FREE_MAGIC:
  755. pooldel(p, (Free*)b);
  756. b->magic = FLOATING_MAGIC;
  757. break;
  758. case ALLOC_MAGIC:
  759. if(wb != b) {
  760. memmove(wb, b, b->size);
  761. p->move(_B2D(b), _B2D(wb));
  762. compacted = 1;
  763. }
  764. wb = B2NB(wb);
  765. break;
  766. }
  767. }
  768. /*
  769. * the only free data is now at the end of the arena, pointed
  770. * at by wb. all we need to do is set its size and get out.
  771. */
  772. if(wb < eb) {
  773. wb->magic = UNALLOC_MAGIC;
  774. blocksetsize(wb, (uchar*)eb-(uchar*)wb);
  775. pooladd(p, (Alloc*)wb);
  776. }
  777. return compacted;
  778. }
  779. /*
  780. * compact a pool by compacting each individual arena.
  781. * 'twould be nice to shift blocks from one arena to the
  782. * next but it's a pain to code.
  783. */
  784. static int
  785. poolcompactl(Pool *pool)
  786. {
  787. Arena *a;
  788. int compacted;
  789. if(pool->move == nil || pool->lastcompact == pool->nfree)
  790. return 0;
  791. pool->lastcompact = pool->nfree;
  792. compacted = 0;
  793. for(a=pool->arenalist; a; a=a->down)
  794. compacted |= arenacompact(pool, a);
  795. return compacted;
  796. }
  797. /*
  798. static int
  799. poolcompactl(Pool*)
  800. {
  801. return 0;
  802. }
  803. */
  804. /*
  805. * Actual allocators
  806. */
  807. /*
  808. static void*
  809. _B2D(void *a)
  810. {
  811. return (uchar*)a+sizeof(Bhdr);
  812. }
  813. */
  814. static void*
  815. B2D(Pool *p, Alloc *a)
  816. {
  817. if(a->magic != ALLOC_MAGIC)
  818. p->panic(p, "B2D called on unworthy block");
  819. return _B2D(a);
  820. }
  821. /*
  822. static void*
  823. _D2B(void *v)
  824. {
  825. Alloc *a;
  826. a = (Alloc*)((uchar*)v-sizeof(Bhdr));
  827. return a;
  828. }
  829. */
  830. static Alloc*
  831. D2B(Pool *p, void *v)
  832. {
  833. Alloc *a;
  834. ulong *u;
  835. if((uintptr)v&(sizeof(ulong)-1))
  836. v = (char*)v - ((uintptr)v&(sizeof(ulong)-1));
  837. u = v;
  838. while(u[-1] == ALIGN_MAGIC)
  839. u--;
  840. a = _D2B(u);
  841. if(a->magic != ALLOC_MAGIC)
  842. p->panic(p, "D2B called on non-block %p (double-free?)", v);
  843. return a;
  844. }
  845. /* poolallocl: attempt to allocate block to hold dsize user bytes; assumes lock held */
  846. static void*
  847. poolallocl(Pool *p, ulong dsize)
  848. {
  849. ulong bsize;
  850. Free *fb;
  851. Alloc *ab;
  852. if(dsize < 0 || dsize >= 0x80000000UL){ /* for sanity, overflow */
  853. werrstr("invalid allocation size");
  854. return nil;
  855. }
  856. bsize = dsize2bsize(p, dsize);
  857. fb = treelookupgt(p->freeroot, bsize);
  858. if(fb == nil) {
  859. poolnewarena(p, bsize2asize(p, bsize));
  860. if((fb = treelookupgt(p->freeroot, bsize)) == nil) {
  861. /* assume poolnewarena failed and set %r */
  862. return nil;
  863. }
  864. }
  865. ab = trim(p, pooldel(p, fb), dsize);
  866. p->curalloc += ab->size;
  867. return B2D(p, ab);
  868. }
  869. /* poolreallocl: attempt to grow v to ndsize bytes; assumes lock held */
  870. static void*
  871. poolreallocl(Pool *p, void *v, ulong ndsize)
  872. {
  873. Alloc *a;
  874. Bhdr *left, *right, *newb;
  875. Btail *t;
  876. ulong nbsize;
  877. ulong odsize;
  878. ulong obsize;
  879. void *nv;
  880. if(v == nil) /* for ANSI */
  881. return poolallocl(p, ndsize);
  882. if(ndsize == 0) {
  883. poolfreel(p, v);
  884. return nil;
  885. }
  886. a = D2B(p, v);
  887. blockcheck(p, a);
  888. odsize = getdsize(a);
  889. obsize = a->size;
  890. /* can reuse the same block? */
  891. nbsize = dsize2bsize(p, ndsize);
  892. if(nbsize <= a->size) {
  893. Returnblock:
  894. if(v != _B2D(a))
  895. memmove(_B2D(a), v, odsize);
  896. a = trim(p, a, ndsize);
  897. p->curalloc -= obsize;
  898. p->curalloc += a->size;
  899. v = B2D(p, a);
  900. return v;
  901. }
  902. /* can merge with surrounding blocks? */
  903. right = B2NB(a);
  904. if(right->magic == FREE_MAGIC && a->size+right->size >= nbsize) {
  905. a = blockmerge(p, a, right);
  906. goto Returnblock;
  907. }
  908. t = B2PT(a);
  909. left = T2HDR(t);
  910. if(left->magic == FREE_MAGIC && left->size+a->size >= nbsize) {
  911. a = blockmerge(p, left, a);
  912. goto Returnblock;
  913. }
  914. if(left->magic == FREE_MAGIC && right->magic == FREE_MAGIC
  915. && left->size+a->size+right->size >= nbsize) {
  916. a = blockmerge(p, blockmerge(p, left, a), right);
  917. goto Returnblock;
  918. }
  919. if((nv = poolallocl(p, ndsize)) == nil)
  920. return nil;
  921. /* maybe the new block is next to us; if so, merge */
  922. left = T2HDR(B2PT(a));
  923. right = B2NB(a);
  924. newb = D2B(p, nv);
  925. if(left == newb || right == newb) {
  926. if(left == newb || left->magic == FREE_MAGIC)
  927. a = blockmerge(p, left, a);
  928. if(right == newb || right->magic == FREE_MAGIC)
  929. a = blockmerge(p, a, right);
  930. assert(a->size >= nbsize);
  931. goto Returnblock;
  932. }
  933. /* enough cleverness */
  934. memmove(nv, v, odsize);
  935. poolfreel(p, v);
  936. return nv;
  937. }
  938. static void*
  939. alignptr(void *v, ulong align, long offset)
  940. {
  941. char *c;
  942. ulong off;
  943. c = v;
  944. if(align){
  945. off = (uintptr)c%align;
  946. if(off != offset){
  947. c += offset - off;
  948. if(off > offset)
  949. c += align;
  950. }
  951. }
  952. return c;
  953. }
  954. /* poolspanallocl: allocate as described below; assumes pool locked */
  955. static void*
  956. poolallocalignl(Pool *p, ulong dsize, ulong align, long offset, ulong span)
  957. {
  958. ulong asize;
  959. void *v;
  960. char *c;
  961. ulong *u;
  962. int skip;
  963. Alloc *b;
  964. /*
  965. * allocate block
  966. * dsize bytes
  967. * addr == offset (modulo align)
  968. * does not cross span-byte block boundary
  969. *
  970. * to satisfy alignment, just allocate an extra
  971. * align bytes and then shift appropriately.
  972. *
  973. * to satisfy span, try once and see if we're
  974. * lucky. the second time, allocate 2x asize
  975. * so that we definitely get one not crossing
  976. * the boundary.
  977. */
  978. if(align){
  979. if(offset < 0)
  980. offset = align - ((-offset)%align);
  981. else
  982. offset %= align;
  983. }
  984. asize = dsize+align;
  985. v = poolallocl(p, asize);
  986. if(v == nil)
  987. return nil;
  988. if(span && (uintptr)v/span != ((uintptr)v+asize)/span){
  989. /* try again */
  990. poolfreel(p, v);
  991. v = poolallocl(p, 2*asize);
  992. if(v == nil)
  993. return nil;
  994. }
  995. /*
  996. * figure out what pointer we want to return
  997. */
  998. c = alignptr(v, align, offset);
  999. if(span && (uintptr)c/span != (uintptr)(c+dsize-1)/span){
  1000. c += span - (uintptr)c%span;
  1001. c = alignptr(c, align, offset);
  1002. if((uintptr)c/span != (uintptr)(c+dsize-1)/span){
  1003. poolfreel(p, v);
  1004. werrstr("cannot satisfy dsize %lud span %lud with align %lud+%ld", dsize, span, align, offset);
  1005. return nil;
  1006. }
  1007. }
  1008. skip = c - (char*)v;
  1009. /*
  1010. * free up the skip bytes before that pointer
  1011. * or mark it as unavailable.
  1012. */
  1013. b = _D2B(v);
  1014. b = freefromfront(p, b, skip);
  1015. v = _B2D(b);
  1016. skip = c - (char*)v;
  1017. if(c > (char*)v){
  1018. u = v;
  1019. while(c >= (char*)u+sizeof(ulong))
  1020. *u++ = ALIGN_MAGIC;
  1021. }
  1022. trim(p, b, skip+dsize);
  1023. assert(D2B(p, c) == b);
  1024. return c;
  1025. }
  1026. /* poolfree: free block obtained from poolalloc; assumes lock held */
  1027. static void
  1028. poolfreel(Pool *p, void *v)
  1029. {
  1030. Alloc *ab;
  1031. Bhdr *back, *fwd;
  1032. if(v == nil) /* for ANSI */
  1033. return;
  1034. ab = D2B(p, v);
  1035. blockcheck(p, ab);
  1036. if(p->flags&POOL_NOREUSE){
  1037. int n;
  1038. ab->magic = DEAD_MAGIC;
  1039. n = getdsize(ab)-8;
  1040. if(n > 0)
  1041. memset((uchar*)v+8, 0xDA, n);
  1042. return;
  1043. }
  1044. p->nfree++;
  1045. p->curalloc -= ab->size;
  1046. back = T2HDR(B2PT(ab));
  1047. if(back->magic == FREE_MAGIC)
  1048. ab = blockmerge(p, back, ab);
  1049. fwd = B2NB(ab);
  1050. if(fwd->magic == FREE_MAGIC)
  1051. ab = blockmerge(p, ab, fwd);
  1052. pooladd(p, ab);
  1053. }
  1054. void*
  1055. poolalloc(Pool *p, ulong n)
  1056. {
  1057. void *v;
  1058. p->lock(p);
  1059. paranoia {
  1060. poolcheckl(p);
  1061. }
  1062. verbosity {
  1063. pooldumpl(p);
  1064. }
  1065. v = poolallocl(p, n);
  1066. paranoia {
  1067. poolcheckl(p);
  1068. }
  1069. verbosity {
  1070. pooldumpl(p);
  1071. }
  1072. if(p->logstack && (p->flags & POOL_LOGGING)) p->logstack(p);
  1073. LOG(p, "poolalloc %p %lud = %p\n", p, n, v);
  1074. p->unlock(p);
  1075. return v;
  1076. }
  1077. void*
  1078. poolallocalign(Pool *p, ulong n, ulong align, long offset, ulong span)
  1079. {
  1080. void *v;
  1081. p->lock(p);
  1082. paranoia {
  1083. poolcheckl(p);
  1084. }
  1085. verbosity {
  1086. pooldumpl(p);
  1087. }
  1088. v = poolallocalignl(p, n, align, offset, span);
  1089. paranoia {
  1090. poolcheckl(p);
  1091. }
  1092. verbosity {
  1093. pooldumpl(p);
  1094. }
  1095. if(p->logstack && (p->flags & POOL_LOGGING)) p->logstack(p);
  1096. LOG(p, "poolalignspanalloc %p %lud %lud %lud %ld = %p\n", p, n, align, span, offset, v);
  1097. p->unlock(p);
  1098. return v;
  1099. }
  1100. int
  1101. poolcompact(Pool *p)
  1102. {
  1103. int rv;
  1104. p->lock(p);
  1105. paranoia {
  1106. poolcheckl(p);
  1107. }
  1108. verbosity {
  1109. pooldumpl(p);
  1110. }
  1111. rv = poolcompactl(p);
  1112. paranoia {
  1113. poolcheckl(p);
  1114. }
  1115. verbosity {
  1116. pooldumpl(p);
  1117. }
  1118. LOG(p, "poolcompact %p\n", p);
  1119. p->unlock(p);
  1120. return rv;
  1121. }
  1122. void*
  1123. poolrealloc(Pool *p, void *v, ulong n)
  1124. {
  1125. void *nv;
  1126. p->lock(p);
  1127. paranoia {
  1128. poolcheckl(p);
  1129. }
  1130. verbosity {
  1131. pooldumpl(p);
  1132. }
  1133. nv = poolreallocl(p, v, n);
  1134. paranoia {
  1135. poolcheckl(p);
  1136. }
  1137. verbosity {
  1138. pooldumpl(p);
  1139. }
  1140. if(p->logstack && (p->flags & POOL_LOGGING)) p->logstack(p);
  1141. LOG(p, "poolrealloc %p %p %ld = %p\n", p, v, n, nv);
  1142. p->unlock(p);
  1143. return nv;
  1144. }
  1145. void
  1146. poolfree(Pool *p, void *v)
  1147. {
  1148. p->lock(p);
  1149. paranoia {
  1150. poolcheckl(p);
  1151. }
  1152. verbosity {
  1153. pooldumpl(p);
  1154. }
  1155. poolfreel(p, v);
  1156. paranoia {
  1157. poolcheckl(p);
  1158. }
  1159. verbosity {
  1160. pooldumpl(p);
  1161. }
  1162. if(p->logstack && (p->flags & POOL_LOGGING)) p->logstack(p);
  1163. LOG(p, "poolfree %p %p\n", p, v);
  1164. p->unlock(p);
  1165. }
  1166. /*
  1167. * Return the real size of a block, and let the user use it.
  1168. */
  1169. ulong
  1170. poolmsize(Pool *p, void *v)
  1171. {
  1172. Alloc *b;
  1173. ulong dsize;
  1174. p->lock(p);
  1175. paranoia {
  1176. poolcheckl(p);
  1177. }
  1178. verbosity {
  1179. pooldumpl(p);
  1180. }
  1181. if(v == nil) /* consistency with other braindead ANSI-ness */
  1182. dsize = 0;
  1183. else {
  1184. b = D2B(p, v);
  1185. dsize = (b->size&~(p->quantum-1)) - sizeof(Bhdr) - sizeof(Btail);
  1186. assert(dsize >= getdsize(b));
  1187. blocksetdsize(p, b, dsize);
  1188. }
  1189. paranoia {
  1190. poolcheckl(p);
  1191. }
  1192. verbosity {
  1193. pooldumpl(p);
  1194. }
  1195. if(p->logstack && (p->flags & POOL_LOGGING)) p->logstack(p);
  1196. LOG(p, "poolmsize %p %p = %ld\n", p, v, dsize);
  1197. p->unlock(p);
  1198. return dsize;
  1199. }
  1200. /*
  1201. * Debugging
  1202. */
  1203. static void
  1204. poolcheckarena(Pool *p, Arena *a)
  1205. {
  1206. Bhdr *b;
  1207. Bhdr *atail;
  1208. atail = A2TB(a);
  1209. for(b=a; b->magic != ARENATAIL_MAGIC && b<atail; b=B2NB(b))
  1210. blockcheck(p, b);
  1211. blockcheck(p, b);
  1212. if(b != atail)
  1213. p->panic(p, "found wrong tail");
  1214. }
  1215. static void
  1216. poolcheckl(Pool *p)
  1217. {
  1218. Arena *a;
  1219. for(a=p->arenalist; a; a=a->down)
  1220. poolcheckarena(p, a);
  1221. if(p->freeroot)
  1222. checktree(p->freeroot, 0, 1<<30);
  1223. }
  1224. void
  1225. poolcheck(Pool *p)
  1226. {
  1227. p->lock(p);
  1228. poolcheckl(p);
  1229. p->unlock(p);
  1230. }
  1231. void
  1232. poolblockcheck(Pool *p, void *v)
  1233. {
  1234. if(v == nil)
  1235. return;
  1236. p->lock(p);
  1237. blockcheck(p, D2B(p, v));
  1238. p->unlock(p);
  1239. }
  1240. static void
  1241. pooldumpl(Pool *p)
  1242. {
  1243. Arena *a;
  1244. p->print(p, "pool %p %s\n", p, p->name);
  1245. for(a=p->arenalist; a; a=a->down)
  1246. pooldumparena(p, a);
  1247. }
  1248. void
  1249. pooldump(Pool *p)
  1250. {
  1251. p->lock(p);
  1252. pooldumpl(p);
  1253. p->unlock(p);
  1254. }
  1255. static void
  1256. pooldumparena(Pool *p, Arena *a)
  1257. {
  1258. Bhdr *b;
  1259. for(b=a; b->magic != ARENATAIL_MAGIC; b=B2NB(b))
  1260. p->print(p, "(%p %.8lux %lud)", b, b->magic, b->size);
  1261. p->print(p, "\n");
  1262. }
  1263. /*
  1264. * mark the memory in such a way that we know who marked it
  1265. * (via the signature) and we know where the marking started.
  1266. */
  1267. static void
  1268. memmark(void *v, int sig, ulong size)
  1269. {
  1270. uchar *p, *ep;
  1271. ulong *lp, *elp;
  1272. lp = v;
  1273. elp = lp+size/4;
  1274. while(lp < elp)
  1275. *lp++ = (sig<<24) ^ (ulong)(uintptr)v;
  1276. p = (uchar*)lp;
  1277. ep = (uchar*)v+size;
  1278. while(p<ep)
  1279. *p++ = sig;
  1280. }