pool.c 30 KB

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