pool.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466
  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. }
  224. /* treelookup: find node in tree with size == size */
  225. static Free*
  226. treelookup(Free *t, ulong size)
  227. {
  228. return *ltreewalk(&t, size);
  229. }
  230. /* treeinsert: insert node into tree */
  231. static Free*
  232. treeinsert(Free *tree, Free *node)
  233. {
  234. Free **loc, *repl;
  235. assert(node != nil /* treeinsert */);
  236. loc = ltreewalk(&tree, node->size);
  237. if(*loc == nil) {
  238. node->left = nil;
  239. node->right = nil;
  240. } else { /* replace existing node */
  241. repl = *loc;
  242. node->left = repl->left;
  243. node->right = repl->right;
  244. }
  245. *loc = node;
  246. return tree;
  247. }
  248. /* treedelete: remove node from tree */
  249. static Free*
  250. treedelete(Free *tree, Free *node)
  251. {
  252. Free **loc, **lsucc, *succ;
  253. assert(node != nil /* treedelete */);
  254. loc = ltreewalk(&tree, node->size);
  255. assert(*loc == node);
  256. if(node->left == nil)
  257. *loc = node->right;
  258. else if(node->right == nil)
  259. *loc = node->left;
  260. else {
  261. /* have two children, use inorder successor as replacement */
  262. for(lsucc = &node->right; (*lsucc)->left; lsucc = &(*lsucc)->left)
  263. ;
  264. succ = *lsucc;
  265. *lsucc = succ->right;
  266. succ->left = node->left;
  267. succ->right = node->right;
  268. *loc = succ;
  269. }
  270. node->left = node->right = Poison;
  271. return tree;
  272. }
  273. /* treelookupgt: find smallest node in tree with size >= size */
  274. static Free*
  275. treelookupgt(Free *t, ulong size)
  276. {
  277. Free *lastgood; /* last node we saw that was big enough */
  278. lastgood = nil;
  279. for(;;) {
  280. if(t == nil)
  281. return lastgood;
  282. if(size == t->size)
  283. return t;
  284. if(size < t->size) {
  285. lastgood = t;
  286. t = t->left;
  287. } else
  288. t = t->right;
  289. }
  290. }
  291. /*
  292. * List maintenance
  293. */
  294. /* listadd: add a node to a doubly linked list */
  295. static Free*
  296. listadd(Free *list, Free *node)
  297. {
  298. if(list == nil) {
  299. node->next = node;
  300. node->prev = node;
  301. return node;
  302. }
  303. node->prev = list->prev;
  304. node->next = list;
  305. node->prev->next = node;
  306. node->next->prev = node;
  307. return list;
  308. }
  309. /* listdelete: remove node from a doubly linked list */
  310. static Free*
  311. listdelete(Free *list, Free *node)
  312. {
  313. if(node->next == node) { /* singular list */
  314. node->prev = node->next = Poison;
  315. return nil;
  316. }
  317. node->next->prev = node->prev;
  318. node->prev->next = node->next;
  319. if(list == node)
  320. list = node->next;
  321. node->prev = node->next = Poison;
  322. return list;
  323. }
  324. /*
  325. * Pool maintenance
  326. */
  327. /* pooladd: add anode to the free pool */
  328. static Free*
  329. pooladd(Pool *p, Alloc *anode)
  330. {
  331. Free *lst, *olst;
  332. Free *node;
  333. Free **parent;
  334. antagonism {
  335. memmark(_B2D(anode), 0xF7, anode->size-sizeof(Bhdr)-sizeof(Btail));
  336. }
  337. node = (Free*)anode;
  338. node->magic = FREE_MAGIC;
  339. parent = ltreewalk(&p->freeroot, node->size);
  340. olst = *parent;
  341. lst = listadd(olst, node);
  342. if(olst != lst) /* need to update tree */
  343. *parent = treeinsert(*parent, lst);
  344. p->curfree += node->size;
  345. return node;
  346. }
  347. /* pooldel: remove node from the free pool */
  348. static Alloc*
  349. pooldel(Pool *p, Free *node)
  350. {
  351. Free *lst, *olst;
  352. Free **parent;
  353. parent = ltreewalk(&p->freeroot, node->size);
  354. olst = *parent;
  355. assert(olst != nil /* pooldel */);
  356. lst = listdelete(olst, node);
  357. if(lst == nil)
  358. *parent = treedelete(*parent, olst);
  359. else if(lst != olst)
  360. *parent = treeinsert(*parent, lst);
  361. node->left = node->right = Poison;
  362. p->curfree -= node->size;
  363. antagonism {
  364. memmark(_B2D(node), 0xF9, node->size-sizeof(Bhdr)-sizeof(Btail));
  365. }
  366. node->magic = UNALLOC_MAGIC;
  367. return (Alloc*)node;
  368. }
  369. /*
  370. * Block maintenance
  371. */
  372. /* block allocation */
  373. static ulong
  374. dsize2bsize(Pool *p, ulong sz)
  375. {
  376. sz += sizeof(Bhdr)+sizeof(Btail);
  377. if(sz < p->minblock)
  378. sz = p->minblock;
  379. if(sz < MINBLOCKSIZE)
  380. sz = MINBLOCKSIZE;
  381. sz = (sz+p->quantum-1)&~(p->quantum-1);
  382. return sz;
  383. }
  384. static ulong
  385. bsize2asize(Pool *p, ulong sz)
  386. {
  387. sz += sizeof(Arena)+sizeof(Btail);
  388. if(sz < p->minarena)
  389. sz = p->minarena;
  390. sz = (sz+p->quantum)&~(p->quantum-1);
  391. return sz;
  392. }
  393. /* blockmerge: merge a and b, known to be adjacent */
  394. /* both are removed from pool if necessary. */
  395. static Alloc*
  396. blockmerge(Pool *pool, Bhdr *a, Bhdr *b)
  397. {
  398. Btail *t;
  399. assert(B2NB(a) == b);
  400. if(a->magic == FREE_MAGIC)
  401. pooldel(pool, (Free*)a);
  402. if(b->magic == FREE_MAGIC)
  403. pooldel(pool, (Free*)b);
  404. t = B2T(a);
  405. t->size = (ulong)Poison;
  406. t->magic0 = NOT_MAGIC;
  407. t->magic1 = NOT_MAGIC;
  408. PSHORT(t->datasize, NOT_MAGIC);
  409. a->size += b->size;
  410. t = B2T(a);
  411. t->size = a->size;
  412. PSHORT(t->datasize, 0xFFFF);
  413. b->size = NOT_MAGIC;
  414. b->magic = NOT_MAGIC;
  415. a->magic = UNALLOC_MAGIC;
  416. return (Alloc*)a;
  417. }
  418. /* blocksetsize: set the total size of a block, fixing tail pointers */
  419. static Bhdr*
  420. blocksetsize(Bhdr *b, ulong bsize)
  421. {
  422. Btail *t;
  423. assert(b->magic != FREE_MAGIC /* blocksetsize */);
  424. b->size = bsize;
  425. t = B2T(b);
  426. t->size = b->size;
  427. t->magic0 = TAIL_MAGIC0;
  428. t->magic1 = TAIL_MAGIC1;
  429. return b;
  430. }
  431. /* getdsize: return the requested data size for an allocated block */
  432. static ulong
  433. getdsize(Alloc *b)
  434. {
  435. Btail *t;
  436. t = B2T(b);
  437. return b->size - SHORT(t->datasize);
  438. }
  439. /* blocksetdsize: set the user data size of a block */
  440. static Alloc*
  441. blocksetdsize(Pool *p, Alloc *b, ulong dsize)
  442. {
  443. Btail *t;
  444. uchar *q, *eq;
  445. assert(b->size >= dsize2bsize(p, dsize));
  446. assert(b->size - dsize < 0x10000);
  447. t = B2T(b);
  448. PSHORT(t->datasize, b->size - dsize);
  449. q=(uchar*)_B2D(b)+dsize;
  450. eq = (uchar*)t;
  451. if(eq > q+4)
  452. eq = q+4;
  453. for(; q<eq; q++)
  454. *q = datamagic[((ulong)(uintptr)q)%nelem(datamagic)];
  455. return b;
  456. }
  457. /* trim: trim a block down to what is needed to hold dsize bytes of user data */
  458. static Alloc*
  459. trim(Pool *p, Alloc *b, ulong dsize)
  460. {
  461. ulong extra, bsize;
  462. Alloc *frag;
  463. bsize = dsize2bsize(p, dsize);
  464. extra = b->size - bsize;
  465. if(b->size - dsize >= 0x10000 ||
  466. (extra >= bsize>>2 && extra >= MINBLOCKSIZE && extra >= p->minblock)) {
  467. blocksetsize(b, bsize);
  468. frag = (Alloc*) B2NB(b);
  469. antagonism {
  470. memmark(frag, 0xF1, extra);
  471. }
  472. frag->magic = UNALLOC_MAGIC;
  473. blocksetsize(frag, extra);
  474. pooladd(p, frag);
  475. }
  476. b->magic = ALLOC_MAGIC;
  477. blocksetdsize(p, b, dsize);
  478. return b;
  479. }
  480. static Alloc*
  481. freefromfront(Pool *p, Alloc *b, ulong skip)
  482. {
  483. Alloc *bb;
  484. skip = skip&~(p->quantum-1);
  485. if(skip >= 0x1000 || (skip >= b->size>>2 && skip >= MINBLOCKSIZE && skip >= p->minblock)){
  486. bb = (Alloc*)((uchar*)b+skip);
  487. blocksetsize(bb, b->size-skip);
  488. bb->magic = UNALLOC_MAGIC;
  489. blocksetsize(b, skip);
  490. b->magic = UNALLOC_MAGIC;
  491. pooladd(p, b);
  492. return bb;
  493. }
  494. return b;
  495. }
  496. /*
  497. * Arena maintenance
  498. */
  499. /* arenasetsize: set arena size, updating tail */
  500. static void
  501. arenasetsize(Arena *a, ulong asize)
  502. {
  503. Bhdr *atail;
  504. a->asize = asize;
  505. atail = A2TB(a);
  506. atail->magic = ARENATAIL_MAGIC;
  507. atail->size = 0;
  508. }
  509. /* poolnewarena: allocate new arena */
  510. static void
  511. poolnewarena(Pool *p, ulong asize)
  512. {
  513. Arena *a;
  514. Arena *ap, *lastap;
  515. Alloc *b;
  516. LOG(p, "newarena %lud\n", asize);
  517. if(p->cursize+asize > p->maxsize) {
  518. if(poolcompactl(p) == 0){
  519. LOG(p, "pool too big: %lud+%lud > %lud\n",
  520. p->cursize, asize, p->maxsize);
  521. werrstr("memory pool too large");
  522. }
  523. return;
  524. }
  525. if((a = p->alloc(asize)) == nil) {
  526. /* assume errstr set by p->alloc */
  527. return;
  528. }
  529. p->cursize += asize;
  530. /* arena hdr */
  531. a->magic = ARENA_MAGIC;
  532. blocksetsize(a, sizeof(Arena));
  533. arenasetsize(a, asize);
  534. blockcheck(p, a);
  535. /* create one large block in arena */
  536. b = (Alloc*)A2B(a);
  537. b->magic = UNALLOC_MAGIC;
  538. blocksetsize(b, (uchar*)A2TB(a)-(uchar*)b);
  539. blockcheck(p, b);
  540. pooladd(p, b);
  541. blockcheck(p, b);
  542. /* sort arena into descending sorted arena list */
  543. for(lastap=nil, ap=p->arenalist; ap > a; lastap=ap, ap=ap->down)
  544. ;
  545. if(a->down = ap) /* assign = */
  546. a->down->aup = a;
  547. if(a->aup = lastap) /* assign = */
  548. a->aup->down = a;
  549. else
  550. p->arenalist = a;
  551. /* merge with surrounding arenas if possible */
  552. /* must do a with up before down with a (think about it) */
  553. if(a->aup)
  554. arenamerge(p, a, a->aup);
  555. if(a->down)
  556. arenamerge(p, a->down, a);
  557. }
  558. /* blockresize: grow a block to encompass space past its end, possibly by */
  559. /* trimming it into two different blocks. */
  560. static void
  561. blockgrow(Pool *p, Bhdr *b, ulong nsize)
  562. {
  563. if(b->magic == FREE_MAGIC) {
  564. Alloc *a;
  565. Bhdr *bnxt;
  566. a = pooldel(p, (Free*)b);
  567. blockcheck(p, a);
  568. blocksetsize(a, nsize);
  569. blockcheck(p, a);
  570. bnxt = B2NB(a);
  571. if(bnxt->magic == FREE_MAGIC)
  572. a = blockmerge(p, a, bnxt);
  573. blockcheck(p, a);
  574. pooladd(p, a);
  575. } else {
  576. Alloc *a;
  577. ulong dsize;
  578. a = (Alloc*)b;
  579. dsize = getdsize(a);
  580. blocksetsize(a, nsize);
  581. trim(p, a, dsize);
  582. }
  583. }
  584. /* arenamerge: attempt to coalesce to arenas that might be adjacent */
  585. static Arena*
  586. arenamerge(Pool *p, Arena *bot, Arena *top)
  587. {
  588. Bhdr *bbot, *btop;
  589. Btail *t;
  590. blockcheck(p, bot);
  591. blockcheck(p, top);
  592. assert(bot->aup == top && top > bot);
  593. if(p->merge == nil || p->merge(bot, top) == 0)
  594. return nil;
  595. /* remove top from list */
  596. if(bot->aup = top->aup) /* assign = */
  597. bot->aup->down = bot;
  598. else
  599. p->arenalist = bot;
  600. /* save ptrs to last block in bot, first block in top */
  601. t = B2PT(A2TB(bot));
  602. bbot = T2HDR(t);
  603. btop = A2B(top);
  604. blockcheck(p, bbot);
  605. blockcheck(p, btop);
  606. /* grow bottom arena to encompass top */
  607. arenasetsize(bot, top->asize + ((uchar*)top - (uchar*)bot));
  608. /* grow bottom block to encompass space between arenas */
  609. blockgrow(p, bbot, (uchar*)btop-(uchar*)bbot);
  610. blockcheck(p, bbot);
  611. return bot;
  612. }
  613. /* dumpblock: print block's vital stats */
  614. static void
  615. dumpblock(Pool *p, Bhdr *b)
  616. {
  617. ulong *dp;
  618. ulong dsize;
  619. uchar *cp;
  620. dp = (ulong*)b;
  621. p->print(p, "pool %s block %p\nhdr %.8lux %.8lux %.8lux %.8lux %.8lux %.8lux\n",
  622. p->name, b, dp[0], dp[1], dp[2], dp[3], dp[4], dp[5], dp[6]);
  623. dp = (ulong*)B2T(b);
  624. p->print(p, "tail %.8lux %.8lux %.8lux %.8lux %.8lux %.8lux | %.8lux %.8lux\n",
  625. dp[-6], dp[-5], dp[-4], dp[-3], dp[-2], dp[-1], dp[0], dp[1]);
  626. if(b->magic == ALLOC_MAGIC){
  627. dsize = getdsize((Alloc*)b);
  628. if(dsize >= b->size) /* user data size corrupt */
  629. return;
  630. cp = (uchar*)_B2D(b)+dsize;
  631. p->print(p, "user data ");
  632. p->print(p, "%.2ux %.2ux %.2ux %.2ux %.2ux %.2ux %.2ux %.2ux",
  633. cp[-8], cp[-7], cp[-6], cp[-5], cp[-4], cp[-3], cp[-2], cp[-1]);
  634. p->print(p, " | %.2ux %.2ux %.2ux %.2ux %.2ux %.2ux %.2ux %.2ux\n",
  635. cp[0], cp[1], cp[2], cp[3], cp[4], cp[5], cp[6], cp[7]);
  636. }
  637. }
  638. static void
  639. printblock(Pool *p, Bhdr *b, char *msg)
  640. {
  641. p->print(p, "%s\n", msg);
  642. dumpblock(p, b);
  643. }
  644. static void
  645. panicblock(Pool *p, Bhdr *b, char *msg)
  646. {
  647. p->print(p, "%s\n", msg);
  648. dumpblock(p, b);
  649. p->panic(p, "pool panic");
  650. }
  651. /* blockcheck: ensure a block consistent with our expectations */
  652. /* should only be called when holding pool lock */
  653. static void
  654. blockcheck(Pool *p, Bhdr *b)
  655. {
  656. Alloc *a;
  657. Btail *t;
  658. int i, n;
  659. uchar *q, *bq, *eq;
  660. ulong dsize;
  661. switch(b->magic) {
  662. default:
  663. panicblock(p, b, "bad magic");
  664. case FREE_MAGIC:
  665. case UNALLOC_MAGIC:
  666. t = B2T(b);
  667. if(t->magic0 != TAIL_MAGIC0 || t->magic1 != TAIL_MAGIC1)
  668. panicblock(p, b, "corrupt tail magic");
  669. if(T2HDR(t) != b)
  670. panicblock(p, b, "corrupt tail ptr");
  671. break;
  672. case DEAD_MAGIC:
  673. t = B2T(b);
  674. if(t->magic0 != TAIL_MAGIC0 || t->magic1 != TAIL_MAGIC1)
  675. panicblock(p, b, "corrupt tail magic");
  676. if(T2HDR(t) != b)
  677. panicblock(p, b, "corrupt tail ptr");
  678. n = getdsize((Alloc*)b);
  679. q = _B2D(b);
  680. q += 8;
  681. for(i=8; i<n; i++)
  682. if(*q++ != 0xDA)
  683. panicblock(p, b, "dangling pointer write");
  684. break;
  685. case ARENA_MAGIC:
  686. b = A2TB((Arena*)b);
  687. if(b->magic != ARENATAIL_MAGIC)
  688. panicblock(p, b, "bad arena size");
  689. /* fall through */
  690. case ARENATAIL_MAGIC:
  691. if(b->size != 0)
  692. panicblock(p, b, "bad arena tail size");
  693. break;
  694. case ALLOC_MAGIC:
  695. a = (Alloc*)b;
  696. t = B2T(b);
  697. dsize = getdsize(a);
  698. bq = (uchar*)_B2D(a)+dsize;
  699. eq = (uchar*)t;
  700. if(t->magic0 != TAIL_MAGIC0){
  701. /* if someone wrote exactly one byte over and it was a NUL, we sometimes only complain. */
  702. if((p->flags & POOL_TOLERANCE) && bq == eq && t->magic0 == 0)
  703. printblock(p, b, "mem user overflow (magic0)");
  704. else
  705. panicblock(p, b, "corrupt tail magic0");
  706. }
  707. if(t->magic1 != TAIL_MAGIC1)
  708. panicblock(p, b, "corrupt tail magic1");
  709. if(T2HDR(t) != b)
  710. panicblock(p, b, "corrupt tail ptr");
  711. if(dsize2bsize(p, dsize) > a->size)
  712. panicblock(p, b, "too much block data");
  713. if(eq > bq+4)
  714. eq = bq+4;
  715. for(q=bq; q<eq; q++){
  716. if(*q != datamagic[((uintptr)q)%nelem(datamagic)]){
  717. if(q == bq && *q == 0 && (p->flags & POOL_TOLERANCE)){
  718. printblock(p, b, "mem user overflow");
  719. continue;
  720. }
  721. panicblock(p, b, "mem user overflow");
  722. }
  723. }
  724. break;
  725. }
  726. }
  727. /*
  728. * compact an arena by shifting all the free blocks to the end.
  729. * assumes pool lock is held.
  730. */
  731. enum {
  732. FLOATING_MAGIC = 0xCBCBCBCB, /* temporarily neither allocated nor in the free tree */
  733. };
  734. static int
  735. arenacompact(Pool *p, Arena *a)
  736. {
  737. Bhdr *b, *wb, *eb, *nxt;
  738. int compacted;
  739. if(p->move == nil)
  740. p->panic(p, "don't call me when pool->move is nil\n");
  741. poolcheckarena(p, a);
  742. eb = A2TB(a);
  743. compacted = 0;
  744. for(b=wb=A2B(a); b && b < eb; b=nxt) {
  745. nxt = B2NB(b);
  746. switch(b->magic) {
  747. case FREE_MAGIC:
  748. pooldel(p, (Free*)b);
  749. b->magic = FLOATING_MAGIC;
  750. break;
  751. case ALLOC_MAGIC:
  752. if(wb != b) {
  753. memmove(wb, b, b->size);
  754. p->move(_B2D(b), _B2D(wb));
  755. compacted = 1;
  756. }
  757. wb = B2NB(wb);
  758. break;
  759. }
  760. }
  761. /*
  762. * the only free data is now at the end of the arena, pointed
  763. * at by wb. all we need to do is set its size and get out.
  764. */
  765. if(wb < eb) {
  766. wb->magic = UNALLOC_MAGIC;
  767. blocksetsize(wb, (uchar*)eb-(uchar*)wb);
  768. pooladd(p, (Alloc*)wb);
  769. }
  770. return compacted;
  771. }
  772. /*
  773. * compact a pool by compacting each individual arena.
  774. * 'twould be nice to shift blocks from one arena to the
  775. * next but it's a pain to code.
  776. */
  777. static int
  778. poolcompactl(Pool *pool)
  779. {
  780. Arena *a;
  781. int compacted;
  782. if(pool->move == nil || pool->lastcompact == pool->nfree)
  783. return 0;
  784. pool->lastcompact = pool->nfree;
  785. compacted = 0;
  786. for(a=pool->arenalist; a; a=a->down)
  787. compacted |= arenacompact(pool, a);
  788. return compacted;
  789. }
  790. /*
  791. static int
  792. poolcompactl(Pool*)
  793. {
  794. return 0;
  795. }
  796. */
  797. /*
  798. * Actual allocators
  799. */
  800. /*
  801. static void*
  802. _B2D(void *a)
  803. {
  804. return (uchar*)a+sizeof(Bhdr);
  805. }
  806. */
  807. static void*
  808. B2D(Pool *p, Alloc *a)
  809. {
  810. if(a->magic != ALLOC_MAGIC)
  811. p->panic(p, "B2D called on unworthy block");
  812. return _B2D(a);
  813. }
  814. /*
  815. static void*
  816. _D2B(void *v)
  817. {
  818. Alloc *a;
  819. a = (Alloc*)((uchar*)v-sizeof(Bhdr));
  820. return a;
  821. }
  822. */
  823. static Alloc*
  824. D2B(Pool *p, void *v)
  825. {
  826. Alloc *a;
  827. ulong *u;
  828. if((uintptr)v&(sizeof(ulong)-1))
  829. v = (char*)v - ((uintptr)v&(sizeof(ulong)-1));
  830. u = v;
  831. while(u[-1] == ALIGN_MAGIC)
  832. u--;
  833. a = _D2B(u);
  834. if(a->magic != ALLOC_MAGIC)
  835. p->panic(p, "D2B called on non-block %p (double-free?)", v);
  836. return a;
  837. }
  838. /* poolallocl: attempt to allocate block to hold dsize user bytes; assumes lock held */
  839. static void*
  840. poolallocl(Pool *p, ulong dsize)
  841. {
  842. ulong bsize;
  843. Free *fb;
  844. Alloc *ab;
  845. if(dsize >= 0x80000000UL){ /* for sanity, overflow */
  846. werrstr("invalid allocation size");
  847. return nil;
  848. }
  849. bsize = dsize2bsize(p, dsize);
  850. fb = treelookupgt(p->freeroot, bsize);
  851. if(fb == nil) {
  852. poolnewarena(p, bsize2asize(p, bsize));
  853. if((fb = treelookupgt(p->freeroot, bsize)) == nil) {
  854. /* assume poolnewarena failed and set %r */
  855. return nil;
  856. }
  857. }
  858. ab = trim(p, pooldel(p, fb), dsize);
  859. p->curalloc += ab->size;
  860. antagonism {
  861. memset(B2D(p, ab), 0xDF, dsize);
  862. }
  863. return B2D(p, ab);
  864. }
  865. /* poolreallocl: attempt to grow v to ndsize bytes; assumes lock held */
  866. static void*
  867. poolreallocl(Pool *p, void *v, ulong ndsize)
  868. {
  869. Alloc *a;
  870. Bhdr *left, *right, *newb;
  871. Btail *t;
  872. ulong nbsize;
  873. ulong odsize;
  874. ulong obsize;
  875. void *nv;
  876. if(v == nil) /* for ANSI */
  877. return poolallocl(p, ndsize);
  878. if(ndsize == 0) {
  879. poolfreel(p, v);
  880. return nil;
  881. }
  882. a = D2B(p, v);
  883. blockcheck(p, a);
  884. odsize = getdsize(a);
  885. obsize = a->size;
  886. /* can reuse the same block? */
  887. nbsize = dsize2bsize(p, ndsize);
  888. if(nbsize <= a->size) {
  889. Returnblock:
  890. if(v != _B2D(a))
  891. memmove(_B2D(a), v, odsize);
  892. a = trim(p, a, ndsize);
  893. p->curalloc -= obsize;
  894. p->curalloc += a->size;
  895. v = B2D(p, a);
  896. return v;
  897. }
  898. /* can merge with surrounding blocks? */
  899. right = B2NB(a);
  900. if(right->magic == FREE_MAGIC && a->size+right->size >= nbsize) {
  901. a = blockmerge(p, a, right);
  902. goto Returnblock;
  903. }
  904. t = B2PT(a);
  905. left = T2HDR(t);
  906. if(left->magic == FREE_MAGIC && left->size+a->size >= nbsize) {
  907. a = blockmerge(p, left, a);
  908. goto Returnblock;
  909. }
  910. if(left->magic == FREE_MAGIC && right->magic == FREE_MAGIC
  911. && left->size+a->size+right->size >= nbsize) {
  912. a = blockmerge(p, blockmerge(p, left, a), right);
  913. goto Returnblock;
  914. }
  915. if((nv = poolallocl(p, ndsize)) == nil)
  916. return nil;
  917. /* maybe the new block is next to us; if so, merge */
  918. left = T2HDR(B2PT(a));
  919. right = B2NB(a);
  920. newb = D2B(p, nv);
  921. if(left == newb || right == newb) {
  922. if(left == newb || left->magic == FREE_MAGIC)
  923. a = blockmerge(p, left, a);
  924. if(right == newb || right->magic == FREE_MAGIC)
  925. a = blockmerge(p, a, right);
  926. assert(a->size >= nbsize);
  927. goto Returnblock;
  928. }
  929. /* enough cleverness */
  930. memmove(nv, v, odsize);
  931. antagonism {
  932. memset((char*)nv+odsize, 0xDE, ndsize-odsize);
  933. }
  934. poolfreel(p, v);
  935. return nv;
  936. }
  937. static void*
  938. alignptr(void *v, ulong align, long offset)
  939. {
  940. char *c;
  941. ulong off;
  942. c = v;
  943. if(align){
  944. off = (uintptr)c%align;
  945. if(off != offset){
  946. c += offset - off;
  947. if(off > offset)
  948. c += align;
  949. }
  950. }
  951. return c;
  952. }
  953. /* poolspanallocl: allocate as described below; assumes pool locked */
  954. static void*
  955. poolallocalignl(Pool *p, ulong dsize, ulong align, long offset, ulong span)
  956. {
  957. ulong asize;
  958. void *v;
  959. char *c;
  960. ulong *u;
  961. int skip;
  962. Alloc *b;
  963. /*
  964. * allocate block
  965. * dsize bytes
  966. * addr == offset (modulo align)
  967. * does not cross span-byte block boundary
  968. *
  969. * to satisfy alignment, just allocate an extra
  970. * align bytes and then shift appropriately.
  971. *
  972. * to satisfy span, try once and see if we're
  973. * lucky. the second time, allocate 2x asize
  974. * so that we definitely get one not crossing
  975. * the boundary.
  976. */
  977. if(align){
  978. if(offset < 0)
  979. offset = align - ((-offset)%align);
  980. else
  981. offset %= align;
  982. }
  983. asize = dsize+align;
  984. v = poolallocl(p, asize);
  985. if(v == nil)
  986. return nil;
  987. if(span && (uintptr)v/span != ((uintptr)v+asize)/span){
  988. /* try again */
  989. poolfreel(p, v);
  990. v = poolallocl(p, 2*asize);
  991. if(v == nil)
  992. return nil;
  993. }
  994. /*
  995. * figure out what pointer we want to return
  996. */
  997. c = alignptr(v, align, offset);
  998. if(span && (uintptr)c/span != (uintptr)(c+dsize-1)/span){
  999. c += span - (uintptr)c%span;
  1000. c = alignptr(c, align, offset);
  1001. if((uintptr)c/span != (uintptr)(c+dsize-1)/span){
  1002. poolfreel(p, v);
  1003. werrstr("cannot satisfy dsize %lud span %lud with align %lud+%ld", dsize, span, align, offset);
  1004. return nil;
  1005. }
  1006. }
  1007. skip = c - (char*)v;
  1008. /*
  1009. * free up the skip bytes before that pointer
  1010. * or mark it as unavailable.
  1011. */
  1012. b = _D2B(v);
  1013. b = freefromfront(p, b, skip);
  1014. v = _B2D(b);
  1015. skip = c - (char*)v;
  1016. if(c > (char*)v){
  1017. u = v;
  1018. while(c >= (char*)u+sizeof(ulong))
  1019. *u++ = ALIGN_MAGIC;
  1020. }
  1021. trim(p, b, skip+dsize);
  1022. assert(D2B(p, c) == b);
  1023. antagonism {
  1024. memset(c, 0xDD, dsize);
  1025. }
  1026. return c;
  1027. }
  1028. /* poolfree: free block obtained from poolalloc; assumes lock held */
  1029. static void
  1030. poolfreel(Pool *p, void *v)
  1031. {
  1032. Alloc *ab;
  1033. Bhdr *back, *fwd;
  1034. if(v == nil) /* for ANSI */
  1035. return;
  1036. ab = D2B(p, v);
  1037. blockcheck(p, ab);
  1038. if(p->flags&POOL_NOREUSE){
  1039. int n;
  1040. ab->magic = DEAD_MAGIC;
  1041. n = getdsize(ab)-8;
  1042. if(n > 0)
  1043. memset((uchar*)v+8, 0xDA, n);
  1044. return;
  1045. }
  1046. p->nfree++;
  1047. p->curalloc -= ab->size;
  1048. back = T2HDR(B2PT(ab));
  1049. if(back->magic == FREE_MAGIC)
  1050. ab = blockmerge(p, back, ab);
  1051. fwd = B2NB(ab);
  1052. if(fwd->magic == FREE_MAGIC)
  1053. ab = blockmerge(p, ab, fwd);
  1054. pooladd(p, ab);
  1055. }
  1056. void*
  1057. poolalloc(Pool *p, ulong n)
  1058. {
  1059. void *v;
  1060. p->lock(p);
  1061. paranoia {
  1062. poolcheckl(p);
  1063. }
  1064. verbosity {
  1065. pooldumpl(p);
  1066. }
  1067. v = poolallocl(p, n);
  1068. paranoia {
  1069. poolcheckl(p);
  1070. }
  1071. verbosity {
  1072. pooldumpl(p);
  1073. }
  1074. if(p->logstack && (p->flags & POOL_LOGGING)) p->logstack(p);
  1075. LOG(p, "poolalloc %p %lud = %p\n", p, n, v);
  1076. p->unlock(p);
  1077. return v;
  1078. }
  1079. void*
  1080. poolallocalign(Pool *p, ulong n, ulong align, long offset, ulong span)
  1081. {
  1082. void *v;
  1083. p->lock(p);
  1084. paranoia {
  1085. poolcheckl(p);
  1086. }
  1087. verbosity {
  1088. pooldumpl(p);
  1089. }
  1090. v = poolallocalignl(p, n, align, offset, span);
  1091. paranoia {
  1092. poolcheckl(p);
  1093. }
  1094. verbosity {
  1095. pooldumpl(p);
  1096. }
  1097. if(p->logstack && (p->flags & POOL_LOGGING)) p->logstack(p);
  1098. LOG(p, "poolalignspanalloc %p %lud %lud %lud %ld = %p\n", p, n, align, span, offset, v);
  1099. p->unlock(p);
  1100. return v;
  1101. }
  1102. int
  1103. poolcompact(Pool *p)
  1104. {
  1105. int rv;
  1106. p->lock(p);
  1107. paranoia {
  1108. poolcheckl(p);
  1109. }
  1110. verbosity {
  1111. pooldumpl(p);
  1112. }
  1113. rv = poolcompactl(p);
  1114. paranoia {
  1115. poolcheckl(p);
  1116. }
  1117. verbosity {
  1118. pooldumpl(p);
  1119. }
  1120. LOG(p, "poolcompact %p\n", p);
  1121. p->unlock(p);
  1122. return rv;
  1123. }
  1124. void*
  1125. poolrealloc(Pool *p, void *v, ulong n)
  1126. {
  1127. void *nv;
  1128. p->lock(p);
  1129. paranoia {
  1130. poolcheckl(p);
  1131. }
  1132. verbosity {
  1133. pooldumpl(p);
  1134. }
  1135. nv = poolreallocl(p, v, n);
  1136. paranoia {
  1137. poolcheckl(p);
  1138. }
  1139. verbosity {
  1140. pooldumpl(p);
  1141. }
  1142. if(p->logstack && (p->flags & POOL_LOGGING)) p->logstack(p);
  1143. LOG(p, "poolrealloc %p %p %ld = %p\n", p, v, n, nv);
  1144. p->unlock(p);
  1145. return nv;
  1146. }
  1147. void
  1148. poolfree(Pool *p, void *v)
  1149. {
  1150. p->lock(p);
  1151. paranoia {
  1152. poolcheckl(p);
  1153. }
  1154. verbosity {
  1155. pooldumpl(p);
  1156. }
  1157. poolfreel(p, v);
  1158. paranoia {
  1159. poolcheckl(p);
  1160. }
  1161. verbosity {
  1162. pooldumpl(p);
  1163. }
  1164. if(p->logstack && (p->flags & POOL_LOGGING)) p->logstack(p);
  1165. LOG(p, "poolfree %p %p\n", p, v);
  1166. p->unlock(p);
  1167. }
  1168. /*
  1169. * Return the real size of a block, and let the user use it.
  1170. */
  1171. ulong
  1172. poolmsize(Pool *p, void *v)
  1173. {
  1174. Alloc *b;
  1175. ulong dsize;
  1176. p->lock(p);
  1177. paranoia {
  1178. poolcheckl(p);
  1179. }
  1180. verbosity {
  1181. pooldumpl(p);
  1182. }
  1183. if(v == nil) /* consistency with other braindead ANSI-ness */
  1184. dsize = 0;
  1185. else {
  1186. b = D2B(p, v);
  1187. dsize = (b->size&~(p->quantum-1)) - sizeof(Bhdr) - sizeof(Btail);
  1188. assert(dsize >= getdsize(b));
  1189. blocksetdsize(p, b, dsize);
  1190. }
  1191. paranoia {
  1192. poolcheckl(p);
  1193. }
  1194. verbosity {
  1195. pooldumpl(p);
  1196. }
  1197. if(p->logstack && (p->flags & POOL_LOGGING)) p->logstack(p);
  1198. LOG(p, "poolmsize %p %p = %ld\n", p, v, dsize);
  1199. p->unlock(p);
  1200. return dsize;
  1201. }
  1202. /*
  1203. * Debugging
  1204. */
  1205. static void
  1206. poolcheckarena(Pool *p, Arena *a)
  1207. {
  1208. Bhdr *b;
  1209. Bhdr *atail;
  1210. atail = A2TB(a);
  1211. for(b=a; b->magic != ARENATAIL_MAGIC && b<atail; b=B2NB(b))
  1212. blockcheck(p, b);
  1213. blockcheck(p, b);
  1214. if(b != atail)
  1215. p->panic(p, "found wrong tail");
  1216. }
  1217. static void
  1218. poolcheckl(Pool *p)
  1219. {
  1220. Arena *a;
  1221. for(a=p->arenalist; a; a=a->down)
  1222. poolcheckarena(p, a);
  1223. if(p->freeroot)
  1224. checktree(p->freeroot, 0, 1<<30);
  1225. }
  1226. void
  1227. poolcheck(Pool *p)
  1228. {
  1229. p->lock(p);
  1230. poolcheckl(p);
  1231. p->unlock(p);
  1232. }
  1233. void
  1234. poolblockcheck(Pool *p, void *v)
  1235. {
  1236. if(v == nil)
  1237. return;
  1238. p->lock(p);
  1239. blockcheck(p, D2B(p, v));
  1240. p->unlock(p);
  1241. }
  1242. static void
  1243. pooldumpl(Pool *p)
  1244. {
  1245. Arena *a;
  1246. p->print(p, "pool %p %s\n", p, p->name);
  1247. for(a=p->arenalist; a; a=a->down)
  1248. pooldumparena(p, a);
  1249. }
  1250. void
  1251. pooldump(Pool *p)
  1252. {
  1253. p->lock(p);
  1254. pooldumpl(p);
  1255. p->unlock(p);
  1256. }
  1257. static void
  1258. pooldumparena(Pool *p, Arena *a)
  1259. {
  1260. Bhdr *b;
  1261. for(b=a; b->magic != ARENATAIL_MAGIC; b=B2NB(b))
  1262. p->print(p, "(%p %.8lux %lud)", b, b->magic, b->size);
  1263. p->print(p, "\n");
  1264. }
  1265. /*
  1266. * mark the memory in such a way that we know who marked it
  1267. * (via the signature) and we know where the marking started.
  1268. */
  1269. static void
  1270. memmark(void *v, int sig, ulong size)
  1271. {
  1272. uchar *p, *ep;
  1273. ulong *lp, *elp;
  1274. lp = v;
  1275. elp = lp+size/4;
  1276. while(lp < elp)
  1277. *lp++ = (sig<<24) ^ ((uintptr)lp-(uintptr)v);
  1278. p = (uchar*)lp;
  1279. ep = (uchar*)v+size;
  1280. while(p<ep)
  1281. *p++ = sig;
  1282. }