2 * This allocator takes blocks from a coarser allocator (p->alloc) and
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.
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().
20 * When freed, adjacent blocks are coalesced to create larger blocks when
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.
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.
45 typedef struct Alloc Alloc;
46 typedef struct Arena Arena;
47 typedef struct Bhdr Bhdr;
48 typedef struct Btail Btail;
49 typedef struct Free Free;
56 NOT_MAGIC = 0xdeadfa11,
57 DEAD_MAGIC = 0xdeaddead,
59 #define B2NB(b) ((Bhdr*)((uchar*)(b)+(b)->size))
61 #define SHORT(x) (((x)[0] << 8) | (x)[1])
62 #define PSHORT(p, x) \
63 (((uchar*)(p))[0] = ((x)>>8)&0xFF, \
64 ((uchar*)(p))[1] = (x)&0xFF)
74 ulong size; /* same as Bhdr->size */
76 #define B2T(b) ((Btail*)((uchar*)(b)+(b)->size-sizeof(Btail)))
77 #define B2PT(b) ((Btail*)((uchar*)(b)-sizeof(Btail)))
78 #define T2HDR(t) ((Bhdr*)((uchar*)(t)+sizeof(Btail)-(t)->size))
87 FREE_MAGIC = 0xBA5EBA11,
91 * the point of the notused fields is to make 8c differentiate
92 * between Bhdr and Allocblk, and between Kempt and Unkempt.
98 ALLOC_MAGIC = 0x0A110C09,
99 UNALLOC_MAGIC = 0xCAB00D1E+1,
107 ulong pad; /* to a multiple of 8 bytes */
110 ARENA_MAGIC = 0xC0A1E5CE+1,
111 ARENATAIL_MAGIC = 0xEC5E1A0C+1,
113 #define A2TB(a) ((Bhdr*)((uchar*)(a)+(a)->asize-sizeof(Bhdr)))
114 #define A2B(a) B2NB(a)
117 ALIGN_MAGIC = 0xA1F1D1C1,
121 MINBLOCKSIZE = sizeof(Free)+sizeof(Btail)
124 static uchar datamagic[] = { 0xFE, 0xF1, 0xF0, 0xFA };
126 #define Poison ((void*)-0x35014542) /* cafebabe */
128 #define _B2D(a) ((void*)((uchar*)a+sizeof(Bhdr)))
129 #define _D2B(v) ((Alloc*)((uchar*)v-sizeof(Bhdr)))
131 // static void* _B2D(void*);
132 // static void* _D2B(void*);
133 static void* B2D(Pool*, Alloc*);
134 static Alloc* D2B(Pool*, void*);
135 static Arena* arenamerge(Pool*, Arena*, Arena*);
136 static void blockcheck(Pool*, Bhdr*);
137 static Alloc* blockmerge(Pool*, Bhdr*, Bhdr*);
138 static Alloc* blocksetdsize(Pool*, Alloc*, ulong);
139 static Bhdr* blocksetsize(Bhdr*, ulong);
140 static ulong bsize2asize(Pool*, ulong);
141 static ulong dsize2bsize(Pool*, ulong);
142 static ulong getdsize(Alloc*);
143 static Alloc* trim(Pool*, Alloc*, ulong);
144 static void logstack(Pool*);
145 static void memmark(void*, int, ulong);
146 static Free* pooladd(Pool*, Alloc*);
147 static void* poolallocl(Pool*, ulong);
148 static void poolcheckl(Pool*);
149 static void poolcheckarena(Pool*, Arena*);
150 static int poolcompactl(Pool*);
151 static Alloc* pooldel(Pool*, Free*);
152 static void pooldumpl(Pool*);
153 static void pooldumparena(Pool*, Arena*);
154 static void poolfreel(Pool*, void*);
155 static void poolnewarena(Pool*, ulong);
156 static void* poolreallocl(Pool*, void*, ulong);
157 static Free* treelookupgt(Free*, ulong);
158 static Free* treesplay(Free*, ulong);
163 * Antagonism causes blocks to always be filled with garbage if their
164 * contents are undefined. This tickles both programs and the library.
165 * It's a linear time hit but not so noticeable during nondegenerate use.
166 * It would be worth leaving in except that it negates the benefits of the
167 * kernel's demand-paging. The tail magic and end-of-data magic
168 * provide most of the user-visible benefit that antagonism does anyway.
170 * Paranoia causes the library to recheck the entire pool on each lock
171 * or unlock. A failed check on unlock means we tripped over ourselves,
172 * while a failed check on lock tends to implicate the user. Paranoia has
173 * the potential to slow things down a fair amount for pools with large
174 * numbers of allocated blocks. It completely negates all benefits won
175 * by the binary tree. Turning on paranoia in the kernel makes it painfully
178 * Verbosity induces the dumping of the pool via p->print at each lock operation.
179 * By default, only one line is logged for each alloc, free, and realloc.
182 /* the if(!x);else avoids ``dangling else'' problems */
183 #define antagonism if(!(p->flags & POOL_ANTAGONISM)){}else
184 #define paranoia if(!(p->flags & POOL_PARANOIA)){}else
185 #define verbosity if(!(p->flags & POOL_VERBOSITY)){}else
187 #define DPRINT if(!(p->flags & POOL_DEBUGGING)){}else p->print
188 #define LOG if(!(p->flags & POOL_LOGGING)){}else p->print
199 for(q=t->next; q!=t; q=q->next){
200 assert(q->magic==FREE_MAGIC);
201 assert(q->size==t->size);
202 assert(q->left==Poison);
203 assert(q->right==Poison);
204 assert(q->next!=nil && q->next!=Poison && q->next->prev==q);
205 assert(q->prev!=nil && q->prev!=Poison && q->prev->next==q);
210 checktree(Free *t, int a, int b)
212 assert(t->magic==FREE_MAGIC);
213 assert(a < t->size && t->size < b);
214 assert(t->left!=Poison);
215 assert(t->right!=Poison);
216 assert(t->next!=nil && t->next!=Poison && t->next->prev==t);
217 assert(t->prev!=nil && t->prev!=Poison && t->prev->next==t);
220 checktree(t->left, a, t->size);
222 checktree(t->right, t->size, b);
226 /* treelookupgt: find smallest node in tree with size >= size */
228 treelookupgt(Free *t, ulong size)
230 Free *lastgood; /* last node we saw that was big enough */
236 assert(t->magic == FREE_MAGIC);
247 /* treesplay: splay node of size size to the root and return new root */
249 treesplay(Free *t, ulong size)
253 N.left = N.right = nil;
257 assert(t->magic == FREE_MAGIC);
261 assert(y->magic == FREE_MAGIC);
273 } else if(size > t->size) {
276 assert(y->magic == FREE_MAGIC);
300 /* pooladd: add anode to the free pool */
302 pooladd(Pool *p, Alloc *anode)
307 memmark(_B2D(anode), 0xF7, anode->size-sizeof(Bhdr)-sizeof(Btail));
311 node->magic = FREE_MAGIC;
312 node->left = node->right = nil;
313 node->next = node->prev = node;
315 if(p->freeroot != nil) {
316 root = treesplay(p->freeroot, node->size);
317 if(root->size > node->size) {
318 node->left = root->left;
321 } else if(root->size < node->size) {
322 node->right = root->right;
326 node->left = root->left;
327 node->right = root->right;
328 root->left = root->right = Poison;
330 node->prev = root->prev;
332 node->prev->next = node;
333 node->next->prev = node;
337 p->curfree += node->size;
342 /* pooldel: remove node from the free pool */
344 pooldel(Pool *p, Free *node)
348 root = treesplay(p->freeroot, node->size);
349 if(node == root && node->next == node) {
350 if(node->left == nil)
353 root = treesplay(node->left, node->size);
354 assert(root->right == nil);
355 root->right = node->right;
360 root->left = node->left;
361 root->right = node->right;
363 assert(root->magic == FREE_MAGIC && root->size == node->size);
364 node->next->prev = node->prev;
365 node->prev->next = node->next;
368 p->curfree -= node->size;
370 node->left = node->right = node->next = node->prev = Poison;
373 memmark(_B2D(node), 0xF9, node->size-sizeof(Bhdr)-sizeof(Btail));
376 node->magic = UNALLOC_MAGIC;
384 /* block allocation */
386 dsize2bsize(Pool *p, ulong sz)
388 sz += sizeof(Bhdr)+sizeof(Btail);
391 if(sz < MINBLOCKSIZE)
393 sz = (sz+p->quantum-1)&~(p->quantum-1);
398 bsize2asize(Pool *p, ulong sz)
400 sz += sizeof(Arena)+sizeof(Btail);
403 sz = (sz+p->quantum)&~(p->quantum-1);
407 /* blockmerge: merge a and b, known to be adjacent */
408 /* both are removed from pool if necessary. */
410 blockmerge(Pool *pool, Bhdr *a, Bhdr *b)
414 assert(B2NB(a) == b);
416 if(a->magic == FREE_MAGIC)
417 pooldel(pool, (Free*)a);
418 if(b->magic == FREE_MAGIC)
419 pooldel(pool, (Free*)b);
422 t->size = (ulong)Poison;
423 t->magic0 = NOT_MAGIC;
424 t->magic1 = NOT_MAGIC;
425 PSHORT(t->datasize, NOT_MAGIC);
430 PSHORT(t->datasize, 0xFFFF);
433 b->magic = NOT_MAGIC;
435 a->magic = UNALLOC_MAGIC;
439 /* blocksetsize: set the total size of a block, fixing tail pointers */
441 blocksetsize(Bhdr *b, ulong bsize)
445 assert(b->magic != FREE_MAGIC /* blocksetsize */);
450 t->magic0 = TAIL_MAGIC0;
451 t->magic1 = TAIL_MAGIC1;
455 /* getdsize: return the requested data size for an allocated block */
461 return b->size - SHORT(t->datasize);
464 /* blocksetdsize: set the user data size of a block */
466 blocksetdsize(Pool *p, Alloc *b, ulong dsize)
471 assert(b->size >= dsize2bsize(p, dsize));
472 assert(b->size - dsize < 0x10000);
475 PSHORT(t->datasize, b->size - dsize);
477 q=(uchar*)_B2D(b)+dsize;
482 *q = datamagic[((ulong)(uintptr)q)%nelem(datamagic)];
487 /* trim: trim a block down to what is needed to hold dsize bytes of user data */
489 trim(Pool *p, Alloc *b, ulong dsize)
494 bsize = dsize2bsize(p, dsize);
495 extra = b->size - bsize;
496 if(b->size - dsize >= 0x10000 ||
497 (extra >= bsize>>2 && extra >= MINBLOCKSIZE && extra >= p->minblock)) {
498 blocksetsize(b, bsize);
499 frag = (Alloc*) B2NB(b);
502 memmark(frag, 0xF1, extra);
505 frag->magic = UNALLOC_MAGIC;
506 blocksetsize(frag, extra);
510 b->magic = ALLOC_MAGIC;
511 blocksetdsize(p, b, dsize);
516 freefromfront(Pool *p, Alloc *b, ulong skip)
520 skip = skip&~(p->quantum-1);
521 if(skip >= 0x1000 || (skip >= b->size>>2 && skip >= MINBLOCKSIZE && skip >= p->minblock)){
522 bb = (Alloc*)((uchar*)b+skip);
523 bb->magic = UNALLOC_MAGIC;
524 blocksetsize(bb, b->size-skip);
525 b->magic = UNALLOC_MAGIC;
526 blocksetsize(b, skip);
537 /* arenasetsize: set arena size, updating tail */
539 arenasetsize(Arena *a, ulong asize)
545 atail->magic = ARENATAIL_MAGIC;
549 /* poolnewarena: allocate new arena */
551 poolnewarena(Pool *p, ulong asize)
557 LOG(p, "newarena %lud\n", asize);
558 if(p->cursize+asize > p->maxsize) {
559 if(poolcompactl(p) == 0){
560 LOG(p, "pool too big: %llud+%lud > %llud\n",
561 (uvlong)p->cursize, asize, (uvlong)p->maxsize);
562 werrstr("memory pool too large");
567 if((a = p->alloc(asize)) == nil) {
568 /* assume errstr set by p->alloc */
575 a->magic = ARENA_MAGIC;
576 blocksetsize(a, sizeof(Arena));
577 arenasetsize(a, asize);
580 /* create one large block in arena */
582 b->magic = UNALLOC_MAGIC;
583 blocksetsize(b, (uchar*)A2TB(a)-(uchar*)b);
588 /* sort arena into descending sorted arena list */
589 for(lastap=nil, ap=p->arenalist; ap > a; lastap=ap, ap=ap->down)
592 if(a->down = ap) /* assign = */
595 if(a->aup = lastap) /* assign = */
600 /* merge with surrounding arenas if possible */
601 /* must do a with up before down with a (think about it) */
603 arenamerge(p, a, a->aup);
605 arenamerge(p, a->down, a);
608 /* blockresize: grow a block to encompass space past its end, possibly by */
609 /* trimming it into two different blocks. */
611 blockgrow(Pool *p, Bhdr *b, ulong nsize)
613 if(b->magic == FREE_MAGIC) {
616 a = pooldel(p, (Free*)b);
618 blocksetsize(a, nsize);
621 if(bnxt->magic == FREE_MAGIC)
622 a = blockmerge(p, a, bnxt);
630 p->curalloc -= a->size;
632 blocksetsize(a, nsize);
634 p->curalloc += a->size;
638 /* arenamerge: attempt to coalesce to arenas that might be adjacent */
640 arenamerge(Pool *p, Arena *bot, Arena *top)
648 assert(bot->aup == top && top > bot);
650 newsize = top->asize + ((uchar*)top - (uchar*)bot);
651 if(newsize < top->asize || p->merge == nil || p->merge(bot, top) == 0)
654 /* remove top from list */
655 if(bot->aup = top->aup) /* assign = */
656 bot->aup->down = bot;
660 /* save ptrs to last block in bot, first block in top */
667 /* grow bottom arena to encompass top */
668 arenasetsize(bot, newsize);
670 /* grow bottom block to encompass space between arenas */
671 blockgrow(p, bbot, (uchar*)btop-(uchar*)bbot);
676 /* dumpblock: print block's vital stats */
678 dumpblock(Pool *p, Bhdr *b)
685 p->print(p, "pool %s block %p\nhdr %.8lux %.8lux %.8lux %.8lux %.8lux %.8lux\n",
686 p->name, b, dp[0], dp[1], dp[2], dp[3], dp[4], dp[5], dp[6]);
689 p->print(p, "tail %.8lux %.8lux %.8lux %.8lux %.8lux %.8lux | %.8lux %.8lux\n",
690 dp[-6], dp[-5], dp[-4], dp[-3], dp[-2], dp[-1], dp[0], dp[1]);
692 if(b->magic == ALLOC_MAGIC){
693 dsize = getdsize((Alloc*)b);
694 if(dsize >= b->size) /* user data size corrupt */
697 cp = (uchar*)_B2D(b)+dsize;
698 p->print(p, "user data ");
699 p->print(p, "%.2ux %.2ux %.2ux %.2ux %.2ux %.2ux %.2ux %.2ux",
700 cp[-8], cp[-7], cp[-6], cp[-5], cp[-4], cp[-3], cp[-2], cp[-1]);
701 p->print(p, " | %.2ux %.2ux %.2ux %.2ux %.2ux %.2ux %.2ux %.2ux\n",
702 cp[0], cp[1], cp[2], cp[3], cp[4], cp[5], cp[6], cp[7]);
707 printblock(Pool *p, Bhdr *b, char *msg)
709 p->print(p, "%s\n", msg);
714 panicblock(Pool *p, Bhdr *b, char *msg)
716 p->print(p, "%s\n", msg);
718 p->panic(p, "pool panic");
721 /* blockcheck: ensure a block consistent with our expectations */
722 /* should only be called when holding pool lock */
724 blockcheck(Pool *p, Bhdr *b)
734 panicblock(p, b, "bad magic");
738 if(t->magic0 != TAIL_MAGIC0 || t->magic1 != TAIL_MAGIC1)
739 panicblock(p, b, "corrupt tail magic");
741 panicblock(p, b, "corrupt tail ptr");
745 if(t->magic0 != TAIL_MAGIC0 || t->magic1 != TAIL_MAGIC1)
746 panicblock(p, b, "corrupt tail magic");
748 panicblock(p, b, "corrupt tail ptr");
749 n = getdsize((Alloc*)b);
754 panicblock(p, b, "dangling pointer write");
758 if(b->magic != ARENATAIL_MAGIC)
759 panicblock(p, b, "bad arena size");
761 case ARENATAIL_MAGIC:
763 panicblock(p, b, "bad arena tail size");
769 bq = (uchar*)_B2D(a)+dsize;
772 if(t->magic0 != TAIL_MAGIC0){
773 /* if someone wrote exactly one byte over and it was a NUL, we sometimes only complain. */
774 if((p->flags & POOL_TOLERANCE) && bq == eq && t->magic0 == 0)
775 printblock(p, b, "mem user overflow (magic0)");
777 panicblock(p, b, "corrupt tail magic0");
780 if(t->magic1 != TAIL_MAGIC1)
781 panicblock(p, b, "corrupt tail magic1");
783 panicblock(p, b, "corrupt tail ptr");
785 if(dsize2bsize(p, dsize) > a->size)
786 panicblock(p, b, "too much block data");
790 for(q=bq; q<eq; q++){
791 if(*q != datamagic[((uintptr)q)%nelem(datamagic)]){
792 if(q == bq && *q == 0 && (p->flags & POOL_TOLERANCE)){
793 printblock(p, b, "mem user overflow");
796 panicblock(p, b, "mem user overflow");
804 * compact an arena by shifting all the free blocks to the end.
805 * assumes pool lock is held.
808 FLOATING_MAGIC = 0xCBCBCBCB, /* temporarily neither allocated nor in the free tree */
812 arenacompact(Pool *p, Arena *a)
814 Bhdr *b, *wb, *eb, *nxt;
818 p->panic(p, "don't call me when pool->move is nil\n");
820 poolcheckarena(p, a);
823 for(b=wb=A2B(a); b && b < eb; b=nxt) {
827 pooldel(p, (Free*)b);
828 b->magic = FLOATING_MAGIC;
832 memmove(wb, b, b->size);
833 p->move(_B2D(b), _B2D(wb));
842 * the only free data is now at the end of the arena, pointed
843 * at by wb. all we need to do is set its size and get out.
846 wb->magic = UNALLOC_MAGIC;
847 blocksetsize(wb, (uchar*)eb-(uchar*)wb);
848 pooladd(p, (Alloc*)wb);
855 * compact a pool by compacting each individual arena.
856 * 'twould be nice to shift blocks from one arena to the
857 * next but it's a pain to code.
860 poolcompactl(Pool *pool)
865 if(pool->move == nil || pool->lastcompact == pool->nfree)
868 pool->lastcompact = pool->nfree;
870 for(a=pool->arenalist; a; a=a->down)
871 compacted |= arenacompact(pool, a);
891 return (uchar*)a+sizeof(Bhdr);
896 B2D(Pool *p, Alloc *a)
898 if(a->magic != ALLOC_MAGIC)
899 p->panic(p, "B2D called on unworthy block");
908 a = (Alloc*)((uchar*)v-sizeof(Bhdr));
914 D2B(Pool *p, void *v)
919 if((uintptr)v&(sizeof(ulong)-1))
920 v = (char*)v - ((uintptr)v&(sizeof(ulong)-1));
922 while(u[-1] == ALIGN_MAGIC)
925 if(a->magic != ALLOC_MAGIC)
926 p->panic(p, "D2B called on non-block %p (double-free?)", v);
930 /* poolallocl: attempt to allocate block to hold dsize user bytes; assumes lock held */
932 poolallocl(Pool *p, ulong dsize)
938 if(dsize >= 0x80000000UL){ /* for sanity, overflow */
939 werrstr("invalid allocation size");
943 bsize = dsize2bsize(p, dsize);
945 fb = treelookupgt(p->freeroot, bsize);
947 poolnewarena(p, bsize2asize(p, bsize));
948 if((fb = treelookupgt(p->freeroot, bsize)) == nil) {
949 /* assume poolnewarena failed and set %r */
954 ab = trim(p, pooldel(p, fb), dsize);
955 p->curalloc += ab->size;
957 memset(B2D(p, ab), 0xDF, dsize);
962 /* poolreallocl: attempt to grow v to ndsize bytes; assumes lock held */
964 poolreallocl(Pool *p, void *v, ulong ndsize)
967 Bhdr *left, *right, *newb;
974 if(v == nil) /* for ANSI */
975 return poolallocl(p, ndsize);
982 odsize = getdsize(a);
985 /* can reuse the same block? */
986 nbsize = dsize2bsize(p, ndsize);
987 if(nbsize <= a->size) {
990 memmove(_B2D(a), v, odsize);
991 a = trim(p, a, ndsize);
992 p->curalloc -= obsize;
993 p->curalloc += a->size;
998 /* can merge with surrounding blocks? */
1000 if(right->magic == FREE_MAGIC && a->size+right->size >= nbsize) {
1001 a = blockmerge(p, a, right);
1007 if(left->magic == FREE_MAGIC && left->size+a->size >= nbsize) {
1008 a = blockmerge(p, left, a);
1012 if(left->magic == FREE_MAGIC && right->magic == FREE_MAGIC
1013 && left->size+a->size+right->size >= nbsize) {
1014 a = blockmerge(p, blockmerge(p, left, a), right);
1018 if((nv = poolallocl(p, ndsize)) == nil)
1021 /* maybe the new block is next to us; if so, merge */
1022 left = T2HDR(B2PT(a));
1025 if(left == newb || right == newb) {
1026 if(left == newb || left->magic == FREE_MAGIC)
1027 a = blockmerge(p, left, a);
1028 if(right == newb || right->magic == FREE_MAGIC)
1029 a = blockmerge(p, a, right);
1030 assert(a->size >= nbsize);
1034 /* enough cleverness */
1035 memmove(nv, v, odsize);
1037 memset((char*)nv+odsize, 0xDE, ndsize-odsize);
1044 alignptr(void *v, ulong align, long offset)
1051 off = ((ulong)(uintptr)c) % align;
1062 /* poolspanallocl: allocate as described below; assumes pool locked */
1064 poolallocalignl(Pool *p, ulong dsize, ulong align, long offset, ulong span)
1076 * addr == offset (modulo align)
1077 * does not cross span-byte block boundary
1079 * to satisfy alignment, just allocate an extra
1080 * align bytes and then shift appropriately.
1082 * to satisfy span, try once and see if we're
1083 * lucky. the second time, allocate 2x asize
1084 * so that we definitely get one not crossing
1089 offset = align - ((-offset)%align);
1092 asize = dsize+align;
1093 v = poolallocl(p, asize);
1096 if(span && (uintptr)v/span != ((uintptr)v+asize)/span){
1099 v = poolallocl(p, 2*asize);
1105 * figure out what pointer we want to return
1107 c = alignptr(v, align, offset);
1108 if(span && (uintptr)c/span != (uintptr)(c+dsize-1)/span){
1109 c += span - (uintptr)c%span;
1110 c = alignptr(c, align, offset);
1111 if((uintptr)c/span != (uintptr)(c+dsize-1)/span){
1113 werrstr("cannot satisfy dsize %lud span %lud with align %lud+%ld", dsize, span, align, offset);
1117 skip = c - (char*)v;
1120 * free up the skip bytes before that pointer
1121 * or mark it as unavailable.
1124 p->curalloc -= b->size;
1125 b = freefromfront(p, b, skip);
1127 skip = c - (char*)v;
1130 while(c >= (char*)u+sizeof(ulong))
1133 trim(p, b, skip+dsize);
1134 p->curalloc += b->size;
1135 assert(D2B(p, c) == b);
1137 memset(c, 0xDD, dsize);
1142 /* poolfree: free block obtained from poolalloc; assumes lock held */
1144 poolfreel(Pool *p, void *v)
1149 if(v == nil) /* for ANSI */
1155 if(p->flags&POOL_NOREUSE){
1158 ab->magic = DEAD_MAGIC;
1161 memset((uchar*)v+8, 0xDA, n);
1166 p->curalloc -= ab->size;
1167 back = T2HDR(B2PT(ab));
1168 if(back->magic == FREE_MAGIC)
1169 ab = blockmerge(p, back, ab);
1172 if(fwd->magic == FREE_MAGIC)
1173 ab = blockmerge(p, ab, fwd);
1179 poolalloc(Pool *p, ulong n)
1190 v = poolallocl(p, n);
1197 if(p->logstack && (p->flags & POOL_LOGGING)) p->logstack(p);
1198 LOG(p, "poolalloc %p %lud = %p\n", p, n, v);
1204 poolallocalign(Pool *p, ulong n, ulong align, long offset, ulong span)
1215 v = poolallocalignl(p, n, align, offset, span);
1222 if(p->logstack && (p->flags & POOL_LOGGING)) p->logstack(p);
1223 LOG(p, "poolallocalign %p %lud %lud %ld %lud = %p\n", p, n, align, offset, span, v);
1229 poolcompact(Pool *p)
1240 rv = poolcompactl(p);
1247 LOG(p, "poolcompact %p\n", p);
1253 poolrealloc(Pool *p, void *v, ulong n)
1264 nv = poolreallocl(p, v, n);
1271 if(p->logstack && (p->flags & POOL_LOGGING)) p->logstack(p);
1272 LOG(p, "poolrealloc %p %p %ld = %p\n", p, v, n, nv);
1278 poolfree(Pool *p, void *v)
1294 if(p->logstack && (p->flags & POOL_LOGGING)) p->logstack(p);
1295 LOG(p, "poolfree %p %p\n", p, v);
1300 * Return the real size of a block, and let the user use it.
1303 poolmsize(Pool *p, void *v)
1315 if(v == nil) /* consistency with other braindead ANSI-ness */
1319 dsize = (b->size&~(p->quantum-1)) - sizeof(Bhdr) - sizeof(Btail);
1320 assert(dsize >= getdsize(b));
1321 blocksetdsize(p, b, dsize);
1329 if(p->logstack && (p->flags & POOL_LOGGING)) p->logstack(p);
1330 LOG(p, "poolmsize %p %p = %ld\n", p, v, dsize);
1336 poolisoverlap(Pool *p, void *v, ulong n)
1341 for(a = p->arenalist; a != nil; a = a->down)
1342 if((uchar*)v+n > (uchar*)a && (uchar*)v < (uchar*)a+a->asize)
1353 poolcheckarena(Pool *p, Arena *a)
1359 for(b=a; b->magic != ARENATAIL_MAGIC && b<atail; b=B2NB(b))
1363 p->panic(p, "found wrong tail");
1371 for(a=p->arenalist; a; a=a->down)
1372 poolcheckarena(p, a);
1374 checktree(p->freeroot, 0, 1<<30);
1386 poolblockcheck(Pool *p, void *v)
1392 blockcheck(p, D2B(p, v));
1401 p->print(p, "pool %p %s\n", p, p->name);
1402 for(a=p->arenalist; a; a=a->down)
1403 pooldumparena(p, a);
1415 pooldumparena(Pool *p, Arena *a)
1419 for(b=a; b->magic != ARENATAIL_MAGIC; b=B2NB(b))
1420 p->print(p, "(%p %.8lux %lud)", b, b->magic, b->size);
1425 * mark the memory in such a way that we know who marked it
1426 * (via the signature) and we know where the marking started.
1429 memmark(void *v, int sig, ulong size)
1436 *lp++ = (sig<<24) ^ ((uintptr)lp-(uintptr)v);
1438 ep = (uchar*)v+size;