xref: /freebsd/sys/kern/subr_blist.c (revision ce4946daa5ce852d28008dac492029500ab2ee95)
1 
2 /*
3  * BLIST.C -	Bitmap allocator/deallocator, using a radix tree with hinting
4  *
5  *	(c)Copyright 1998, Matthew Dillon.  Terms for use and redistribution
6  *	are covered by the BSD Copyright as found in /usr/src/COPYRIGHT.
7  *
8  *	This module implements a general bitmap allocator/deallocator.  The
9  *	allocator eats around 2 bits per 'block'.  The module does not
10  *	try to interpret the meaning of a 'block' other then to return
11  *	SWAPBLK_NONE on an allocation failure.
12  *
13  *	A radix tree is used to maintain the bitmap.  Two radix constants are
14  *	involved:  One for the bitmaps contained in the leaf nodes (typically
15  *	32), and one for the meta nodes (typically 16).  Both meta and leaf
16  *	nodes have a hint field.  This field gives us a hint as to the largest
17  *	free contiguous range of blocks under the node.  It may contain a
18  *	value that is too high, but will never contain a value that is too
19  *	low.  When the radix tree is searched, allocation failures in subtrees
20  *	update the hint.
21  *
22  *	The radix tree also implements two collapsed states for meta nodes:
23  *	the ALL-ALLOCATED state and the ALL-FREE state.  If a meta node is
24  *	in either of these two states, all information contained underneath
25  *	the node is considered stale.  These states are used to optimize
26  *	allocation and freeing operations.
27  *
28  * 	The hinting greatly increases code efficiency for allocations while
29  *	the general radix structure optimizes both allocations and frees.  The
30  *	radix tree should be able to operate well no matter how much
31  *	fragmentation there is and no matter how large a bitmap is used.
32  *
33  *	Unlike the rlist code, the blist code wires all necessary memory at
34  *	creation time.  Neither allocations nor frees require interaction with
35  *	the memory subsystem.  In contrast, the rlist code may allocate memory
36  *	on an rlist_free() call.  The non-blocking features of the blist code
37  *	are used to great advantage in the swap code (vm/nswap_pager.c).  The
38  *	rlist code uses a little less overall memory then the blist code (but
39  *	due to swap interleaving not all that much less), but the blist code
40  *	scales much, much better.
41  *
42  *	LAYOUT: The radix tree is layed out recursively using a
43  *	linear array.  Each meta node is immediately followed (layed out
44  *	sequentially in memory) by BLIST_META_RADIX lower level nodes.  This
45  *	is a recursive structure but one that can be easily scanned through
46  *	a very simple 'skip' calculation.  In order to support large radixes,
47  *	portions of the tree may reside outside our memory allocation.  We
48  *	handle this with an early-termination optimization (when bighint is
49  *	set to -1) on the scan.  The memory allocation is only large enough
50  *	to cover the number of blocks requested at creation time even if it
51  *	must be encompassed in larger root-node radix.
52  *
53  *	NOTE: the allocator cannot currently allocate more then
54  *	BLIST_BMAP_RADIX blocks per call.  It will panic with 'allocation too
55  *	large' if you try.  This is an area that could use improvement.  The
56  *	radix is large enough that this restriction does not effect the swap
57  *	system, though.  Currently only the allocation code is effected by
58  *	this algorithmic unfeature.  The freeing code can handle arbitrary
59  *	ranges.
60  *
61  *	This code can be compiled stand-alone for debugging.
62  *
63  * $FreeBSD$
64  */
65 
66 #ifdef _KERNEL
67 
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/lock.h>
71 #include <sys/kernel.h>
72 #include <sys/blist.h>
73 #include <sys/malloc.h>
74 #include <vm/vm.h>
75 #include <vm/vm_object.h>
76 #include <vm/vm_kern.h>
77 #include <vm/vm_extern.h>
78 #include <vm/vm_page.h>
79 
80 #else
81 
82 #ifndef BLIST_NO_DEBUG
83 #define BLIST_DEBUG
84 #endif
85 
86 #define SWAPBLK_NONE ((daddr_t)-1)
87 
88 #include <sys/types.h>
89 #include <stdio.h>
90 #include <string.h>
91 #include <stdlib.h>
92 #include <stdarg.h>
93 
94 #define malloc(a,b,c)	malloc(a)
95 #define free(a,b)	free(a)
96 
97 typedef unsigned int u_daddr_t;
98 
99 #include <sys/blist.h>
100 
101 void panic(const char *ctl, ...);
102 
103 #endif
104 
105 /*
106  * static support functions
107  */
108 
109 static daddr_t blst_leaf_alloc(blmeta_t *scan, daddr_t blk, int count);
110 static daddr_t blst_meta_alloc(blmeta_t *scan, daddr_t blk,
111 				daddr_t count, daddr_t radix, int skip);
112 static void blst_leaf_free(blmeta_t *scan, daddr_t relblk, int count);
113 static void blst_meta_free(blmeta_t *scan, daddr_t freeBlk, daddr_t count,
114 					daddr_t radix, int skip, daddr_t blk);
115 static void blst_copy(blmeta_t *scan, daddr_t blk, daddr_t radix,
116 				daddr_t skip, blist_t dest, daddr_t count);
117 static daddr_t	blst_radix_init(blmeta_t *scan, daddr_t radix,
118 						int skip, daddr_t count);
119 #ifndef _KERNEL
120 static void	blst_radix_print(blmeta_t *scan, daddr_t blk,
121 					daddr_t radix, int skip, int tab);
122 #endif
123 
124 #ifdef _KERNEL
125 static MALLOC_DEFINE(M_SWAP, "SWAP", "Swap space");
126 #endif
127 
128 /*
129  * blist_create() - create a blist capable of handling up to the specified
130  *		    number of blocks
131  *
132  *	blocks must be greater then 0
133  *
134  *	The smallest blist consists of a single leaf node capable of
135  *	managing BLIST_BMAP_RADIX blocks.
136  */
137 
138 blist_t
139 blist_create(daddr_t blocks)
140 {
141 	blist_t bl;
142 	int radix;
143 	int skip = 0;
144 
145 	/*
146 	 * Calculate radix and skip field used for scanning.
147 	 */
148 	radix = BLIST_BMAP_RADIX;
149 
150 	while (radix < blocks) {
151 		radix <<= BLIST_META_RADIX_SHIFT;
152 		skip = (skip + 1) << BLIST_META_RADIX_SHIFT;
153 	}
154 
155 	bl = malloc(sizeof(struct blist), M_SWAP, M_WAITOK | M_ZERO);
156 
157 	bl->bl_blocks = blocks;
158 	bl->bl_radix = radix;
159 	bl->bl_skip = skip;
160 	bl->bl_rootblks = 1 +
161 	    blst_radix_init(NULL, bl->bl_radix, bl->bl_skip, blocks);
162 	bl->bl_root = malloc(sizeof(blmeta_t) * bl->bl_rootblks, M_SWAP, M_WAITOK);
163 
164 #if defined(BLIST_DEBUG)
165 	printf(
166 		"BLIST representing %d blocks (%d MB of swap)"
167 		", requiring %dK of ram\n",
168 		bl->bl_blocks,
169 		bl->bl_blocks * 4 / 1024,
170 		(bl->bl_rootblks * sizeof(blmeta_t) + 1023) / 1024
171 	);
172 	printf("BLIST raw radix tree contains %d records\n", bl->bl_rootblks);
173 #endif
174 	blst_radix_init(bl->bl_root, bl->bl_radix, bl->bl_skip, blocks);
175 
176 	return(bl);
177 }
178 
179 void
180 blist_destroy(blist_t bl)
181 {
182 	free(bl->bl_root, M_SWAP);
183 	free(bl, M_SWAP);
184 }
185 
186 /*
187  * blist_alloc() - reserve space in the block bitmap.  Return the base
188  *		     of a contiguous region or SWAPBLK_NONE if space could
189  *		     not be allocated.
190  */
191 
192 daddr_t
193 blist_alloc(blist_t bl, daddr_t count)
194 {
195 	daddr_t blk = SWAPBLK_NONE;
196 
197 	if (bl) {
198 		if (bl->bl_radix == BLIST_BMAP_RADIX)
199 			blk = blst_leaf_alloc(bl->bl_root, 0, count);
200 		else
201 			blk = blst_meta_alloc(bl->bl_root, 0, count, bl->bl_radix, bl->bl_skip);
202 		if (blk != SWAPBLK_NONE)
203 			bl->bl_free -= count;
204 	}
205 	return(blk);
206 }
207 
208 /*
209  * blist_free() -	free up space in the block bitmap.  Return the base
210  *		     	of a contiguous region.  Panic if an inconsistancy is
211  *			found.
212  */
213 
214 void
215 blist_free(blist_t bl, daddr_t blkno, daddr_t count)
216 {
217 	if (bl) {
218 		if (bl->bl_radix == BLIST_BMAP_RADIX)
219 			blst_leaf_free(bl->bl_root, blkno, count);
220 		else
221 			blst_meta_free(bl->bl_root, blkno, count, bl->bl_radix, bl->bl_skip, 0);
222 		bl->bl_free += count;
223 	}
224 }
225 
226 /*
227  * blist_resize() -	resize an existing radix tree to handle the
228  *			specified number of blocks.  This will reallocate
229  *			the tree and transfer the previous bitmap to the new
230  *			one.  When extending the tree you can specify whether
231  *			the new blocks are to left allocated or freed.
232  */
233 
234 void
235 blist_resize(blist_t *pbl, daddr_t count, int freenew)
236 {
237     blist_t newbl = blist_create(count);
238     blist_t save = *pbl;
239 
240     *pbl = newbl;
241     if (count > save->bl_blocks)
242 	    count = save->bl_blocks;
243     blst_copy(save->bl_root, 0, save->bl_radix, save->bl_skip, newbl, count);
244 
245     /*
246      * If resizing upwards, should we free the new space or not?
247      */
248     if (freenew && count < newbl->bl_blocks) {
249 	    blist_free(newbl, count, newbl->bl_blocks - count);
250     }
251     blist_destroy(save);
252 }
253 
254 #ifdef BLIST_DEBUG
255 
256 /*
257  * blist_print()    - dump radix tree
258  */
259 
260 void
261 blist_print(blist_t bl)
262 {
263 	printf("BLIST {\n");
264 	blst_radix_print(bl->bl_root, 0, bl->bl_radix, bl->bl_skip, 4);
265 	printf("}\n");
266 }
267 
268 #endif
269 
270 /************************************************************************
271  *			  ALLOCATION SUPPORT FUNCTIONS			*
272  ************************************************************************
273  *
274  *	These support functions do all the actual work.  They may seem
275  *	rather longish, but that's because I've commented them up.  The
276  *	actual code is straight forward.
277  *
278  */
279 
280 /*
281  * blist_leaf_alloc() -	allocate at a leaf in the radix tree (a bitmap).
282  *
283  *	This is the core of the allocator and is optimized for the 1 block
284  *	and the BLIST_BMAP_RADIX block allocation cases.  Other cases are
285  *	somewhat slower.  The 1 block allocation case is log2 and extremely
286  *	quick.
287  */
288 
289 static daddr_t
290 blst_leaf_alloc(
291 	blmeta_t *scan,
292 	daddr_t blk,
293 	int count
294 ) {
295 	u_daddr_t orig = scan->u.bmu_bitmap;
296 
297 	if (orig == 0) {
298 		/*
299 		 * Optimize bitmap all-allocated case.  Also, count = 1
300 		 * case assumes at least 1 bit is free in the bitmap, so
301 		 * we have to take care of this case here.
302 		 */
303 		scan->bm_bighint = 0;
304 		return(SWAPBLK_NONE);
305 	}
306 	if (count == 1) {
307 		/*
308 		 * Optimized code to allocate one bit out of the bitmap
309 		 */
310 		u_daddr_t mask;
311 		int j = BLIST_BMAP_RADIX/2;
312 		int r = 0;
313 
314 		mask = (u_daddr_t)-1 >> (BLIST_BMAP_RADIX/2);
315 
316 		while (j) {
317 			if ((orig & mask) == 0) {
318 			    r += j;
319 			    orig >>= j;
320 			}
321 			j >>= 1;
322 			mask >>= j;
323 		}
324 		scan->u.bmu_bitmap &= ~(1 << r);
325 		return(blk + r);
326 	}
327 	if (count <= BLIST_BMAP_RADIX) {
328 		/*
329 		 * non-optimized code to allocate N bits out of the bitmap.
330 		 * The more bits, the faster the code runs.  It will run
331 		 * the slowest allocating 2 bits, but since there aren't any
332 		 * memory ops in the core loop (or shouldn't be, anyway),
333 		 * you probably won't notice the difference.
334 		 */
335 		int j;
336 		int n = BLIST_BMAP_RADIX - count;
337 		u_daddr_t mask;
338 
339 		mask = (u_daddr_t)-1 >> n;
340 
341 		for (j = 0; j <= n; ++j) {
342 			if ((orig & mask) == mask) {
343 				scan->u.bmu_bitmap &= ~mask;
344 				return(blk + j);
345 			}
346 			mask = (mask << 1);
347 		}
348 	}
349 	/*
350 	 * We couldn't allocate count in this subtree, update bighint.
351 	 */
352 	scan->bm_bighint = count - 1;
353 	return(SWAPBLK_NONE);
354 }
355 
356 /*
357  * blist_meta_alloc() -	allocate at a meta in the radix tree.
358  *
359  *	Attempt to allocate at a meta node.  If we can't, we update
360  *	bighint and return a failure.  Updating bighint optimize future
361  *	calls that hit this node.  We have to check for our collapse cases
362  *	and we have a few optimizations strewn in as well.
363  */
364 
365 static daddr_t
366 blst_meta_alloc(
367 	blmeta_t *scan,
368 	daddr_t blk,
369 	daddr_t count,
370 	daddr_t radix,
371 	int skip
372 ) {
373 	int i;
374 	int next_skip = (skip >> BLIST_META_RADIX_SHIFT);
375 
376 	if (scan->u.bmu_avail == 0)  {
377 		/*
378 		 * ALL-ALLOCATED special case
379 		 */
380 		scan->bm_bighint = count;
381 		return(SWAPBLK_NONE);
382 	}
383 
384 	if (scan->u.bmu_avail == radix) {
385 		radix >>= BLIST_META_RADIX_SHIFT;
386 
387 		/*
388 		 * ALL-FREE special case, initialize uninitialize
389 		 * sublevel.
390 		 */
391 		for (i = 1; i <= skip; i += next_skip) {
392 			if (scan[i].bm_bighint == (daddr_t)-1)
393 				break;
394 			if (next_skip == 1) {
395 				scan[i].u.bmu_bitmap = (u_daddr_t)-1;
396 				scan[i].bm_bighint = BLIST_BMAP_RADIX;
397 			} else {
398 				scan[i].bm_bighint = radix;
399 				scan[i].u.bmu_avail = radix;
400 			}
401 		}
402 	} else {
403 		radix >>= BLIST_META_RADIX_SHIFT;
404 	}
405 
406 	for (i = 1; i <= skip; i += next_skip) {
407 		if (count <= scan[i].bm_bighint) {
408 			/*
409 			 * count fits in object
410 			 */
411 			daddr_t r;
412 			if (next_skip == 1) {
413 				r = blst_leaf_alloc(&scan[i], blk, count);
414 			} else {
415 				r = blst_meta_alloc(&scan[i], blk, count, radix, next_skip - 1);
416 			}
417 			if (r != SWAPBLK_NONE) {
418 				scan->u.bmu_avail -= count;
419 				if (scan->bm_bighint > scan->u.bmu_avail)
420 					scan->bm_bighint = scan->u.bmu_avail;
421 				return(r);
422 			}
423 		} else if (scan[i].bm_bighint == (daddr_t)-1) {
424 			/*
425 			 * Terminator
426 			 */
427 			break;
428 		} else if (count > radix) {
429 			/*
430 			 * count does not fit in object even if it were
431 			 * complete free.
432 			 */
433 			panic("blist_meta_alloc: allocation too large");
434 		}
435 		blk += radix;
436 	}
437 
438 	/*
439 	 * We couldn't allocate count in this subtree, update bighint.
440 	 */
441 	if (scan->bm_bighint >= count)
442 		scan->bm_bighint = count - 1;
443 	return(SWAPBLK_NONE);
444 }
445 
446 /*
447  * BLST_LEAF_FREE() -	free allocated block from leaf bitmap
448  *
449  */
450 
451 static void
452 blst_leaf_free(
453 	blmeta_t *scan,
454 	daddr_t blk,
455 	int count
456 ) {
457 	/*
458 	 * free some data in this bitmap
459 	 *
460 	 * e.g.
461 	 *	0000111111111110000
462 	 *          \_________/\__/
463 	 *		v        n
464 	 */
465 	int n = blk & (BLIST_BMAP_RADIX - 1);
466 	u_daddr_t mask;
467 
468 	mask = ((u_daddr_t)-1 << n) &
469 	    ((u_daddr_t)-1 >> (BLIST_BMAP_RADIX - count - n));
470 
471 	if (scan->u.bmu_bitmap & mask)
472 		panic("blst_radix_free: freeing free block");
473 	scan->u.bmu_bitmap |= mask;
474 
475 	/*
476 	 * We could probably do a better job here.  We are required to make
477 	 * bighint at least as large as the biggest contiguous block of
478 	 * data.  If we just shoehorn it, a little extra overhead will
479 	 * be incured on the next allocation (but only that one typically).
480 	 */
481 	scan->bm_bighint = BLIST_BMAP_RADIX;
482 }
483 
484 /*
485  * BLST_META_FREE() - free allocated blocks from radix tree meta info
486  *
487  *	This support routine frees a range of blocks from the bitmap.
488  *	The range must be entirely enclosed by this radix node.  If a
489  *	meta node, we break the range down recursively to free blocks
490  *	in subnodes (which means that this code can free an arbitrary
491  *	range whereas the allocation code cannot allocate an arbitrary
492  *	range).
493  */
494 
495 static void
496 blst_meta_free(
497 	blmeta_t *scan,
498 	daddr_t freeBlk,
499 	daddr_t count,
500 	daddr_t radix,
501 	int skip,
502 	daddr_t blk
503 ) {
504 	int i;
505 	int next_skip = (skip >> BLIST_META_RADIX_SHIFT);
506 
507 #if 0
508 	printf("FREE (%x,%d) FROM (%x,%d)\n",
509 	    freeBlk, count,
510 	    blk, radix
511 	);
512 #endif
513 
514 	if (scan->u.bmu_avail == 0) {
515 		/*
516 		 * ALL-ALLOCATED special case, with possible
517 		 * shortcut to ALL-FREE special case.
518 		 */
519 		scan->u.bmu_avail = count;
520 		scan->bm_bighint = count;
521 
522 		if (count != radix)  {
523 			for (i = 1; i <= skip; i += next_skip) {
524 				if (scan[i].bm_bighint == (daddr_t)-1)
525 					break;
526 				scan[i].bm_bighint = 0;
527 				if (next_skip == 1) {
528 					scan[i].u.bmu_bitmap = 0;
529 				} else {
530 					scan[i].u.bmu_avail = 0;
531 				}
532 			}
533 			/* fall through */
534 		}
535 	} else {
536 		scan->u.bmu_avail += count;
537 		/* scan->bm_bighint = radix; */
538 	}
539 
540 	/*
541 	 * ALL-FREE special case.
542 	 */
543 
544 	if (scan->u.bmu_avail == radix)
545 		return;
546 	if (scan->u.bmu_avail > radix)
547 		panic("blst_meta_free: freeing already free blocks (%d) %d/%d", count, scan->u.bmu_avail, radix);
548 
549 	/*
550 	 * Break the free down into its components
551 	 */
552 
553 	radix >>= BLIST_META_RADIX_SHIFT;
554 
555 	i = (freeBlk - blk) / radix;
556 	blk += i * radix;
557 	i = i * next_skip + 1;
558 
559 	while (i <= skip && blk < freeBlk + count) {
560 		daddr_t v;
561 
562 		v = blk + radix - freeBlk;
563 		if (v > count)
564 			v = count;
565 
566 		if (scan->bm_bighint == (daddr_t)-1)
567 			panic("blst_meta_free: freeing unexpected range");
568 
569 		if (next_skip == 1) {
570 			blst_leaf_free(&scan[i], freeBlk, v);
571 		} else {
572 			blst_meta_free(&scan[i], freeBlk, v, radix, next_skip - 1, blk);
573 		}
574 		if (scan->bm_bighint < scan[i].bm_bighint)
575 		    scan->bm_bighint = scan[i].bm_bighint;
576 		count -= v;
577 		freeBlk += v;
578 		blk += radix;
579 		i += next_skip;
580 	}
581 }
582 
583 /*
584  * BLIST_RADIX_COPY() - copy one radix tree to another
585  *
586  *	Locates free space in the source tree and frees it in the destination
587  *	tree.  The space may not already be free in the destination.
588  */
589 
590 static void blst_copy(
591 	blmeta_t *scan,
592 	daddr_t blk,
593 	daddr_t radix,
594 	daddr_t skip,
595 	blist_t dest,
596 	daddr_t count
597 ) {
598 	int next_skip;
599 	int i;
600 
601 	/*
602 	 * Leaf node
603 	 */
604 
605 	if (radix == BLIST_BMAP_RADIX) {
606 		u_daddr_t v = scan->u.bmu_bitmap;
607 
608 		if (v == (u_daddr_t)-1) {
609 			blist_free(dest, blk, count);
610 		} else if (v != 0) {
611 			int i;
612 
613 			for (i = 0; i < BLIST_BMAP_RADIX && i < count; ++i) {
614 				if (v & (1 << i))
615 					blist_free(dest, blk + i, 1);
616 			}
617 		}
618 		return;
619 	}
620 
621 	/*
622 	 * Meta node
623 	 */
624 
625 	if (scan->u.bmu_avail == 0) {
626 		/*
627 		 * Source all allocated, leave dest allocated
628 		 */
629 		return;
630 	}
631 	if (scan->u.bmu_avail == radix) {
632 		/*
633 		 * Source all free, free entire dest
634 		 */
635 		if (count < radix)
636 			blist_free(dest, blk, count);
637 		else
638 			blist_free(dest, blk, radix);
639 		return;
640 	}
641 
642 
643 	radix >>= BLIST_META_RADIX_SHIFT;
644 	next_skip = (skip >> BLIST_META_RADIX_SHIFT);
645 
646 	for (i = 1; count && i <= skip; i += next_skip) {
647 		if (scan[i].bm_bighint == (daddr_t)-1)
648 			break;
649 
650 		if (count >= radix) {
651 			blst_copy(
652 			    &scan[i],
653 			    blk,
654 			    radix,
655 			    next_skip - 1,
656 			    dest,
657 			    radix
658 			);
659 			count -= radix;
660 		} else {
661 			if (count) {
662 				blst_copy(
663 				    &scan[i],
664 				    blk,
665 				    radix,
666 				    next_skip - 1,
667 				    dest,
668 				    count
669 				);
670 			}
671 			count = 0;
672 		}
673 		blk += radix;
674 	}
675 }
676 
677 /*
678  * BLST_RADIX_INIT() - initialize radix tree
679  *
680  *	Initialize our meta structures and bitmaps and calculate the exact
681  *	amount of space required to manage 'count' blocks - this space may
682  *	be considerably less then the calculated radix due to the large
683  *	RADIX values we use.
684  */
685 
686 static daddr_t
687 blst_radix_init(blmeta_t *scan, daddr_t radix, int skip, daddr_t count)
688 {
689 	int i;
690 	int next_skip;
691 	daddr_t memindex = 0;
692 
693 	/*
694 	 * Leaf node
695 	 */
696 
697 	if (radix == BLIST_BMAP_RADIX) {
698 		if (scan) {
699 			scan->bm_bighint = 0;
700 			scan->u.bmu_bitmap = 0;
701 		}
702 		return(memindex);
703 	}
704 
705 	/*
706 	 * Meta node.  If allocating the entire object we can special
707 	 * case it.  However, we need to figure out how much memory
708 	 * is required to manage 'count' blocks, so we continue on anyway.
709 	 */
710 
711 	if (scan) {
712 		scan->bm_bighint = 0;
713 		scan->u.bmu_avail = 0;
714 	}
715 
716 	radix >>= BLIST_META_RADIX_SHIFT;
717 	next_skip = (skip >> BLIST_META_RADIX_SHIFT);
718 
719 	for (i = 1; i <= skip; i += next_skip) {
720 		if (count >= radix) {
721 			/*
722 			 * Allocate the entire object
723 			 */
724 			memindex = i + blst_radix_init(
725 			    ((scan) ? &scan[i] : NULL),
726 			    radix,
727 			    next_skip - 1,
728 			    radix
729 			);
730 			count -= radix;
731 		} else if (count > 0) {
732 			/*
733 			 * Allocate a partial object
734 			 */
735 			memindex = i + blst_radix_init(
736 			    ((scan) ? &scan[i] : NULL),
737 			    radix,
738 			    next_skip - 1,
739 			    count
740 			);
741 			count = 0;
742 		} else {
743 			/*
744 			 * Add terminator and break out
745 			 */
746 			if (scan)
747 				scan[i].bm_bighint = (daddr_t)-1;
748 			break;
749 		}
750 	}
751 	if (memindex < i)
752 		memindex = i;
753 	return(memindex);
754 }
755 
756 #ifdef BLIST_DEBUG
757 
758 static void
759 blst_radix_print(blmeta_t *scan, daddr_t blk, daddr_t radix, int skip, int tab)
760 {
761 	int i;
762 	int next_skip;
763 	int lastState = 0;
764 
765 	if (radix == BLIST_BMAP_RADIX) {
766 		printf(
767 		    "%*.*s(%04x,%d): bitmap %08x big=%d\n",
768 		    tab, tab, "",
769 		    blk, radix,
770 		    scan->u.bmu_bitmap,
771 		    scan->bm_bighint
772 		);
773 		return;
774 	}
775 
776 	if (scan->u.bmu_avail == 0) {
777 		printf(
778 		    "%*.*s(%04x,%d) ALL ALLOCATED\n",
779 		    tab, tab, "",
780 		    blk,
781 		    radix
782 		);
783 		return;
784 	}
785 	if (scan->u.bmu_avail == radix) {
786 		printf(
787 		    "%*.*s(%04x,%d) ALL FREE\n",
788 		    tab, tab, "",
789 		    blk,
790 		    radix
791 		);
792 		return;
793 	}
794 
795 	printf(
796 	    "%*.*s(%04x,%d): subtree (%d/%d) big=%d {\n",
797 	    tab, tab, "",
798 	    blk, radix,
799 	    scan->u.bmu_avail,
800 	    radix,
801 	    scan->bm_bighint
802 	);
803 
804 	radix >>= BLIST_META_RADIX_SHIFT;
805 	next_skip = (skip >> BLIST_META_RADIX_SHIFT);
806 	tab += 4;
807 
808 	for (i = 1; i <= skip; i += next_skip) {
809 		if (scan[i].bm_bighint == (daddr_t)-1) {
810 			printf(
811 			    "%*.*s(%04x,%d): Terminator\n",
812 			    tab, tab, "",
813 			    blk, radix
814 			);
815 			lastState = 0;
816 			break;
817 		}
818 		blst_radix_print(
819 		    &scan[i],
820 		    blk,
821 		    radix,
822 		    next_skip - 1,
823 		    tab
824 		);
825 		blk += radix;
826 	}
827 	tab -= 4;
828 
829 	printf(
830 	    "%*.*s}\n",
831 	    tab, tab, ""
832 	);
833 }
834 
835 #endif
836 
837 #ifdef BLIST_DEBUG
838 
839 int
840 main(int ac, char **av)
841 {
842 	int size = 1024;
843 	int i;
844 	blist_t bl;
845 
846 	for (i = 1; i < ac; ++i) {
847 		const char *ptr = av[i];
848 		if (*ptr != '-') {
849 			size = strtol(ptr, NULL, 0);
850 			continue;
851 		}
852 		ptr += 2;
853 		fprintf(stderr, "Bad option: %s\n", ptr - 2);
854 		exit(1);
855 	}
856 	bl = blist_create(size);
857 	blist_free(bl, 0, size);
858 
859 	for (;;) {
860 		char buf[1024];
861 		daddr_t da = 0;
862 		daddr_t count = 0;
863 
864 
865 		printf("%d/%d/%d> ", bl->bl_free, size, bl->bl_radix);
866 		fflush(stdout);
867 		if (fgets(buf, sizeof(buf), stdin) == NULL)
868 			break;
869 		switch(buf[0]) {
870 		case 'r':
871 			if (sscanf(buf + 1, "%d", &count) == 1) {
872 				blist_resize(&bl, count, 1);
873 			} else {
874 				printf("?\n");
875 			}
876 		case 'p':
877 			blist_print(bl);
878 			break;
879 		case 'a':
880 			if (sscanf(buf + 1, "%d", &count) == 1) {
881 				daddr_t blk = blist_alloc(bl, count);
882 				printf("    R=%04x\n", blk);
883 			} else {
884 				printf("?\n");
885 			}
886 			break;
887 		case 'f':
888 			if (sscanf(buf + 1, "%x %d", &da, &count) == 2) {
889 				blist_free(bl, da, count);
890 			} else {
891 				printf("?\n");
892 			}
893 			break;
894 		case '?':
895 		case 'h':
896 			puts(
897 			    "p          -print\n"
898 			    "a %d       -allocate\n"
899 			    "f %x %d    -free\n"
900 			    "r %d       -resize\n"
901 			    "h/?        -help"
902 			);
903 			break;
904 		default:
905 			printf("?\n");
906 			break;
907 		}
908 	}
909 	return(0);
910 }
911 
912 void
913 panic(const char *ctl, ...)
914 {
915 	va_list va;
916 
917 	va_start(va, ctl);
918 	vfprintf(stderr, ctl, va);
919 	fprintf(stderr, "\n");
920 	va_end(va);
921 	exit(1);
922 }
923 
924 #endif
925 
926