2 * Copyright (c) 1998 Matthew Dillon. All Rights Reserved.
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions
6 * 1. Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * 2. Redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution.
11 * 3. Neither the name of the University nor the names of its contributors
12 * may be used to endorse or promote products derived from this software
13 * without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * BLIST.C - Bitmap allocator/deallocator, using a radix tree with hinting
30 * This module implements a general bitmap allocator/deallocator. The
31 * allocator eats around 2 bits per 'block'. The module does not
32 * try to interpret the meaning of a 'block' other than to return
33 * SWAPBLK_NONE on an allocation failure.
35 * A radix tree is used to maintain the bitmap. Two radix constants are
36 * involved: One for the bitmaps contained in the leaf nodes (typically
37 * 64), and one for the meta nodes (typically 16). Both meta and leaf
38 * nodes have a hint field. This field gives us a hint as to the largest
39 * free contiguous range of blocks under the node. It may contain a
40 * value that is too high, but will never contain a value that is too
41 * low. When the radix tree is searched, allocation failures in subtrees
44 * The radix tree also implements two collapsed states for meta nodes:
45 * the ALL-ALLOCATED state and the ALL-FREE state. If a meta node is
46 * in either of these two states, all information contained underneath
47 * the node is considered stale. These states are used to optimize
48 * allocation and freeing operations.
50 * The hinting greatly increases code efficiency for allocations while
51 * the general radix structure optimizes both allocations and frees. The
52 * radix tree should be able to operate well no matter how much
53 * fragmentation there is and no matter how large a bitmap is used.
55 * The blist code wires all necessary memory at creation time. Neither
56 * allocations nor frees require interaction with the memory subsystem.
57 * The non-blocking features of the blist code are used in the swap code
60 * LAYOUT: The radix tree is laid out recursively using a
61 * linear array. Each meta node is immediately followed (laid out
62 * sequentially in memory) by BLIST_META_RADIX lower level nodes. This
63 * is a recursive structure but one that can be easily scanned through
64 * a very simple 'skip' calculation. In order to support large radixes,
65 * portions of the tree may reside outside our memory allocation. We
66 * handle this with an early-termination optimization (when bighint is
67 * set to -1) on the scan. The memory allocation is only large enough
68 * to cover the number of blocks requested at creation time even if it
69 * must be encompassed in larger root-node radix.
71 * NOTE: the allocator cannot currently allocate more than
72 * BLIST_BMAP_RADIX blocks per call. It will panic with 'allocation too
73 * large' if you try. This is an area that could use improvement. The
74 * radix is large enough that this restriction does not effect the swap
75 * system, though. Currently only the allocation code is affected by
76 * this algorithmic unfeature. The freeing code can handle arbitrary
79 * This code can be compiled stand-alone for debugging.
82 #include <sys/cdefs.h>
83 __FBSDID("$FreeBSD$");
87 #include <sys/param.h>
88 #include <sys/systm.h>
90 #include <sys/kernel.h>
91 #include <sys/blist.h>
92 #include <sys/malloc.h>
94 #include <sys/mutex.h>
98 #ifndef BLIST_NO_DEBUG
102 #include <sys/types.h>
103 #include <sys/malloc.h>
110 #define bitcount64(x) __bitcount64((uint64_t)(x))
111 #define malloc(a,b,c) calloc(a, 1)
112 #define free(a,b) free(a)
114 #include <sys/blist.h>
116 void panic(const char *ctl, ...);
121 * static support functions
123 static daddr_t blst_leaf_alloc(blmeta_t *scan, daddr_t blk, int count,
125 static daddr_t blst_meta_alloc(blmeta_t *scan, daddr_t blk, daddr_t count,
126 daddr_t radix, daddr_t cursor);
127 static void blst_leaf_free(blmeta_t *scan, daddr_t relblk, int count);
128 static void blst_meta_free(blmeta_t *scan, daddr_t freeBlk, daddr_t count,
129 daddr_t radix, daddr_t blk);
130 static void blst_copy(blmeta_t *scan, daddr_t blk, daddr_t radix,
131 blist_t dest, daddr_t count);
132 static daddr_t blst_leaf_fill(blmeta_t *scan, daddr_t blk, int count);
133 static daddr_t blst_meta_fill(blmeta_t *scan, daddr_t allocBlk, daddr_t count,
134 daddr_t radix, daddr_t blk);
135 static daddr_t blst_radix_init(blmeta_t *scan, daddr_t radix, daddr_t count);
137 static void blst_radix_print(blmeta_t *scan, daddr_t blk, daddr_t radix,
142 static MALLOC_DEFINE(M_SWAP, "SWAP", "Swap space");
146 * For a subtree that can represent the state of up to 'radix' blocks, the
147 * number of leaf nodes of the subtree is L=radix/BLIST_BMAP_RADIX. If 'm'
148 * is short for BLIST_META_RADIX, then for a tree of height h with L=m**h
149 * leaf nodes, the total number of tree nodes is 1 + m + m**2 + ... + m**h,
150 * or, equivalently, (m**(h+1)-1)/(m-1). This quantity is called 'skip'
151 * in the 'meta' functions that process subtrees. Since integer division
152 * discards remainders, we can express this computation as
153 * skip = (m * m**h) / (m - 1)
154 * skip = (m * radix / BLIST_BMAP_RADIX) / (m - 1)
155 * and if m divides BLIST_BMAP_RADIX, we can simplify further to
156 * skip = radix / (BLIST_BMAP_RADIX / m * (m - 1))
157 * so that a simple integer division is enough for the calculation.
159 static inline daddr_t
160 radix_to_skip(daddr_t radix)
164 (BLIST_BMAP_RADIX / BLIST_META_RADIX * (BLIST_META_RADIX - 1)));
168 * blist_create() - create a blist capable of handling up to the specified
171 * blocks - must be greater than 0
172 * flags - malloc flags
174 * The smallest blist consists of a single leaf node capable of
175 * managing BLIST_BMAP_RADIX blocks.
178 blist_create(daddr_t blocks, int flags)
181 daddr_t nodes, radix;
184 * Calculate the radix field used for scanning.
186 radix = BLIST_BMAP_RADIX;
187 while (radix < blocks) {
188 radix *= BLIST_META_RADIX;
190 nodes = 1 + blst_radix_init(NULL, radix, blocks);
192 bl = malloc(sizeof(struct blist), M_SWAP, flags);
196 bl->bl_blocks = blocks;
197 bl->bl_radix = radix;
199 bl->bl_root = malloc(nodes * sizeof(blmeta_t), M_SWAP, flags);
200 if (bl->bl_root == NULL) {
204 blst_radix_init(bl->bl_root, radix, blocks);
206 #if defined(BLIST_DEBUG)
208 "BLIST representing %lld blocks (%lld MB of swap)"
209 ", requiring %lldK of ram\n",
210 (long long)bl->bl_blocks,
211 (long long)bl->bl_blocks * 4 / 1024,
212 (long long)(nodes * sizeof(blmeta_t) + 1023) / 1024
214 printf("BLIST raw radix tree contains %lld records\n",
222 blist_destroy(blist_t bl)
224 free(bl->bl_root, M_SWAP);
229 * blist_alloc() - reserve space in the block bitmap. Return the base
230 * of a contiguous region or SWAPBLK_NONE if space could
234 blist_alloc(blist_t bl, daddr_t count)
239 * This loop iterates at most twice. An allocation failure in the
240 * first iteration leads to a second iteration only if the cursor was
241 * non-zero. When the cursor is zero, an allocation failure will
242 * reduce the hint, stopping further iterations.
244 while (count <= bl->bl_root->bm_bighint) {
245 blk = blst_meta_alloc(bl->bl_root, 0, count, bl->bl_radix,
247 if (blk != SWAPBLK_NONE) {
248 bl->bl_cursor = blk + count;
250 } else if (bl->bl_cursor != 0)
253 return (SWAPBLK_NONE);
257 * blist_avail() - return the number of free blocks.
260 blist_avail(blist_t bl)
263 if (bl->bl_radix == BLIST_BMAP_RADIX)
264 return (bitcount64(bl->bl_root->u.bmu_bitmap));
266 return (bl->bl_root->u.bmu_avail);
270 * blist_free() - free up space in the block bitmap. Return the base
271 * of a contiguous region. Panic if an inconsistancy is
275 blist_free(blist_t bl, daddr_t blkno, daddr_t count)
278 blst_meta_free(bl->bl_root, blkno, count, bl->bl_radix, 0);
282 * blist_fill() - mark a region in the block bitmap as off-limits
283 * to the allocator (i.e. allocate it), ignoring any
284 * existing allocations. Return the number of blocks
285 * actually filled that were free before the call.
288 blist_fill(blist_t bl, daddr_t blkno, daddr_t count)
291 return (blst_meta_fill(bl->bl_root, blkno, count, bl->bl_radix, 0));
295 * blist_resize() - resize an existing radix tree to handle the
296 * specified number of blocks. This will reallocate
297 * the tree and transfer the previous bitmap to the new
298 * one. When extending the tree you can specify whether
299 * the new blocks are to left allocated or freed.
302 blist_resize(blist_t *pbl, daddr_t count, int freenew, int flags)
304 blist_t newbl = blist_create(count, flags);
308 if (count > save->bl_blocks)
309 count = save->bl_blocks;
310 blst_copy(save->bl_root, 0, save->bl_radix, newbl, count);
313 * If resizing upwards, should we free the new space or not?
315 if (freenew && count < newbl->bl_blocks) {
316 blist_free(newbl, count, newbl->bl_blocks - count);
324 * blist_print() - dump radix tree
327 blist_print(blist_t bl)
329 printf("BLIST cursor = %08jx {\n", (uintmax_t)bl->bl_cursor);
330 blst_radix_print(bl->bl_root, 0, bl->bl_radix, 4);
336 /************************************************************************
337 * ALLOCATION SUPPORT FUNCTIONS *
338 ************************************************************************
340 * These support functions do all the actual work. They may seem
341 * rather longish, but that's because I've commented them up. The
342 * actual code is straight forward.
347 * blist_leaf_alloc() - allocate at a leaf in the radix tree (a bitmap).
349 * This is the core of the allocator and is optimized for the
350 * BLIST_BMAP_RADIX block allocation case. Otherwise, execution
351 * time is proportional to log2(count) + log2(BLIST_BMAP_RADIX).
354 blst_leaf_alloc(blmeta_t *scan, daddr_t blk, int count, daddr_t cursor)
357 int count1, hi, lo, mid, num_shifts, range1, range_ext;
359 if (count == BLIST_BMAP_RADIX) {
361 * Optimize allocation of BLIST_BMAP_RADIX bits. If this wasn't
362 * a special case, then forming the final value of 'mask' below
363 * would require special handling to avoid an invalid left shift
364 * when count equals the number of bits in mask.
366 if (~scan->u.bmu_bitmap != 0) {
367 scan->bm_bighint = BLIST_BMAP_RADIX - 1;
368 return (SWAPBLK_NONE);
371 return (SWAPBLK_NONE);
372 scan->u.bmu_bitmap = 0;
373 scan->bm_bighint = 0;
378 num_shifts = fls(count1);
379 mask = scan->u.bmu_bitmap;
380 while (mask != 0 && num_shifts > 0) {
382 * If bit i is set in mask, then bits in [i, i+range1] are set
383 * in scan->u.bmu_bitmap. The value of range1 is equal to
384 * count1 >> num_shifts. Grow range and reduce num_shifts to 0,
385 * while preserving these invariants. The updates to mask leave
386 * fewer bits set, but each bit that remains set represents a
387 * longer string of consecutive bits set in scan->u.bmu_bitmap.
390 range_ext = range1 + ((count1 >> num_shifts) & 1);
391 mask &= mask >> range_ext;
396 * Update bighint. There is no allocation bigger than range1
397 * available in this leaf.
399 scan->bm_bighint = range1;
400 return (SWAPBLK_NONE);
404 * Discard any candidates that appear before the cursor.
407 mask &= ~(u_daddr_t)0 << lo;
410 return (SWAPBLK_NONE);
413 * The least significant set bit in mask marks the start of the first
414 * available range of sufficient size. Clear all the bits but that one,
415 * and then perform a binary search to find its position.
418 hi = BLIST_BMAP_RADIX - count1;
419 while (lo + 1 < hi) {
420 mid = (lo + hi) >> 1;
421 if ((mask >> mid) != 0)
428 * Set in mask exactly the bits being allocated, and clear them from
429 * the set of available bits.
431 mask = (mask << count) - mask;
432 scan->u.bmu_bitmap &= ~mask;
437 * blist_meta_alloc() - allocate at a meta in the radix tree.
439 * Attempt to allocate at a meta node. If we can't, we update
440 * bighint and return a failure. Updating bighint optimize future
441 * calls that hit this node. We have to check for our collapse cases
442 * and we have a few optimizations strewn in as well.
445 blst_meta_alloc(blmeta_t *scan, daddr_t blk, daddr_t count, daddr_t radix,
448 daddr_t i, next_skip, r, skip;
450 bool scan_from_start;
452 if (radix == BLIST_BMAP_RADIX)
453 return (blst_leaf_alloc(scan, blk, count, cursor));
454 if (scan->u.bmu_avail < count) {
456 * The meta node's hint must be too large if the allocation
457 * exceeds the number of free blocks. Reduce the hint, and
460 scan->bm_bighint = scan->u.bmu_avail;
461 return (SWAPBLK_NONE);
463 skip = radix_to_skip(radix);
464 next_skip = skip / BLIST_META_RADIX;
467 * An ALL-FREE meta node requires special handling before allocating
470 if (scan->u.bmu_avail == radix) {
471 radix /= BLIST_META_RADIX;
474 * Reinitialize each of the meta node's children. An ALL-FREE
475 * meta node cannot have a terminator in any subtree.
477 for (i = 1; i < skip; i += next_skip) {
479 scan[i].u.bmu_bitmap = (u_daddr_t)-1;
481 scan[i].u.bmu_avail = radix;
482 scan[i].bm_bighint = radix;
485 radix /= BLIST_META_RADIX;
490 * The allocation exceeds the number of blocks that are
491 * managed by a subtree of this meta node.
493 panic("allocation too large");
495 scan_from_start = cursor == blk;
496 child = (cursor - blk) / radix;
497 blk += child * radix;
498 for (i = 1 + child * next_skip; i < skip; i += next_skip) {
499 if (count <= scan[i].bm_bighint) {
501 * The allocation might fit in the i'th subtree.
503 r = blst_meta_alloc(&scan[i], blk, count, radix,
504 cursor > blk ? cursor : blk);
505 if (r != SWAPBLK_NONE) {
506 scan->u.bmu_avail -= count;
509 } else if (scan[i].bm_bighint == (daddr_t)-1) {
519 * We couldn't allocate count in this subtree, update bighint.
521 if (scan_from_start && scan->bm_bighint >= count)
522 scan->bm_bighint = count - 1;
524 return (SWAPBLK_NONE);
528 * BLST_LEAF_FREE() - free allocated block from leaf bitmap
532 blst_leaf_free(blmeta_t *scan, daddr_t blk, int count)
535 * free some data in this bitmap
538 * 0000111111111110000
542 int n = blk & (BLIST_BMAP_RADIX - 1);
545 mask = ((u_daddr_t)-1 << n) &
546 ((u_daddr_t)-1 >> (BLIST_BMAP_RADIX - count - n));
548 if (scan->u.bmu_bitmap & mask)
549 panic("blst_radix_free: freeing free block");
550 scan->u.bmu_bitmap |= mask;
553 * We could probably do a better job here. We are required to make
554 * bighint at least as large as the biggest contiguous block of
555 * data. If we just shoehorn it, a little extra overhead will
556 * be incured on the next allocation (but only that one typically).
558 scan->bm_bighint = BLIST_BMAP_RADIX;
562 * BLST_META_FREE() - free allocated blocks from radix tree meta info
564 * This support routine frees a range of blocks from the bitmap.
565 * The range must be entirely enclosed by this radix node. If a
566 * meta node, we break the range down recursively to free blocks
567 * in subnodes (which means that this code can free an arbitrary
568 * range whereas the allocation code cannot allocate an arbitrary
572 blst_meta_free(blmeta_t *scan, daddr_t freeBlk, daddr_t count, daddr_t radix,
575 daddr_t i, next_skip, skip, v;
578 if (scan->bm_bighint == (daddr_t)-1)
579 panic("freeing invalid range");
580 if (radix == BLIST_BMAP_RADIX)
581 return (blst_leaf_free(scan, freeBlk, count));
582 skip = radix_to_skip(radix);
583 next_skip = skip / BLIST_META_RADIX;
585 if (scan->u.bmu_avail == 0) {
587 * ALL-ALLOCATED special case, with possible
588 * shortcut to ALL-FREE special case.
590 scan->u.bmu_avail = count;
591 scan->bm_bighint = count;
593 if (count != radix) {
594 for (i = 1; i < skip; i += next_skip) {
595 if (scan[i].bm_bighint == (daddr_t)-1)
597 scan[i].bm_bighint = 0;
598 if (next_skip == 1) {
599 scan[i].u.bmu_bitmap = 0;
601 scan[i].u.bmu_avail = 0;
607 scan->u.bmu_avail += count;
608 /* scan->bm_bighint = radix; */
612 * ALL-FREE special case.
615 if (scan->u.bmu_avail == radix)
617 if (scan->u.bmu_avail > radix)
618 panic("blst_meta_free: freeing already free blocks (%lld) %lld/%lld",
619 (long long)count, (long long)scan->u.bmu_avail,
623 * Break the free down into its components
626 radix /= BLIST_META_RADIX;
628 child = (freeBlk - blk) / radix;
629 blk += child * radix;
630 i = 1 + child * next_skip;
631 while (i < skip && blk < freeBlk + count) {
632 v = blk + radix - freeBlk;
635 blst_meta_free(&scan[i], freeBlk, v, radix, blk);
636 if (scan->bm_bighint < scan[i].bm_bighint)
637 scan->bm_bighint = scan[i].bm_bighint;
646 * BLIST_RADIX_COPY() - copy one radix tree to another
648 * Locates free space in the source tree and frees it in the destination
649 * tree. The space may not already be free in the destination.
652 blst_copy(blmeta_t *scan, daddr_t blk, daddr_t radix, blist_t dest,
655 daddr_t i, next_skip, skip;
661 if (radix == BLIST_BMAP_RADIX) {
662 u_daddr_t v = scan->u.bmu_bitmap;
664 if (v == (u_daddr_t)-1) {
665 blist_free(dest, blk, count);
669 for (i = 0; i < BLIST_BMAP_RADIX && i < count; ++i) {
670 if (v & ((u_daddr_t)1 << i))
671 blist_free(dest, blk + i, 1);
681 if (scan->u.bmu_avail == 0) {
683 * Source all allocated, leave dest allocated
687 if (scan->u.bmu_avail == radix) {
689 * Source all free, free entire dest
692 blist_free(dest, blk, count);
694 blist_free(dest, blk, radix);
699 skip = radix_to_skip(radix);
700 next_skip = skip / BLIST_META_RADIX;
701 radix /= BLIST_META_RADIX;
703 for (i = 1; count && i < skip; i += next_skip) {
704 if (scan[i].bm_bighint == (daddr_t)-1)
707 if (count >= radix) {
708 blst_copy(&scan[i], blk, radix, dest, radix);
712 blst_copy(&scan[i], blk, radix, dest, count);
721 * BLST_LEAF_FILL() - allocate specific blocks in leaf bitmap
723 * This routine allocates all blocks in the specified range
724 * regardless of any existing allocations in that range. Returns
725 * the number of blocks allocated by the call.
728 blst_leaf_fill(blmeta_t *scan, daddr_t blk, int count)
730 int n = blk & (BLIST_BMAP_RADIX - 1);
734 mask = ((u_daddr_t)-1 << n) &
735 ((u_daddr_t)-1 >> (BLIST_BMAP_RADIX - count - n));
737 /* Count the number of blocks that we are allocating. */
738 nblks = bitcount64(scan->u.bmu_bitmap & mask);
740 scan->u.bmu_bitmap &= ~mask;
745 * BLIST_META_FILL() - allocate specific blocks at a meta node
747 * This routine allocates the specified range of blocks,
748 * regardless of any existing allocations in the range. The
749 * range must be within the extent of this node. Returns the
750 * number of blocks allocated by the call.
753 blst_meta_fill(blmeta_t *scan, daddr_t allocBlk, daddr_t count, daddr_t radix,
756 daddr_t i, nblks, next_skip, skip, v;
759 if (scan->bm_bighint == (daddr_t)-1)
760 panic("filling invalid range");
763 * The allocation exceeds the number of blocks that are
764 * managed by this node.
766 panic("fill too large");
768 if (radix == BLIST_BMAP_RADIX)
769 return (blst_leaf_fill(scan, allocBlk, count));
770 if (count == radix || scan->u.bmu_avail == 0) {
772 * ALL-ALLOCATED special case
774 nblks = scan->u.bmu_avail;
775 scan->u.bmu_avail = 0;
776 scan->bm_bighint = 0;
779 skip = radix_to_skip(radix);
780 next_skip = skip / BLIST_META_RADIX;
783 * An ALL-FREE meta node requires special handling before allocating
786 if (scan->u.bmu_avail == radix) {
787 radix /= BLIST_META_RADIX;
790 * Reinitialize each of the meta node's children. An ALL-FREE
791 * meta node cannot have a terminator in any subtree.
793 for (i = 1; i < skip; i += next_skip) {
795 scan[i].u.bmu_bitmap = (u_daddr_t)-1;
797 scan[i].u.bmu_avail = radix;
798 scan[i].bm_bighint = radix;
801 radix /= BLIST_META_RADIX;
805 child = (allocBlk - blk) / radix;
806 blk += child * radix;
807 i = 1 + child * next_skip;
808 while (i < skip && blk < allocBlk + count) {
809 v = blk + radix - allocBlk;
812 nblks += blst_meta_fill(&scan[i], allocBlk, v, radix, blk);
818 scan->u.bmu_avail -= nblks;
823 * BLST_RADIX_INIT() - initialize radix tree
825 * Initialize our meta structures and bitmaps and calculate the exact
826 * amount of space required to manage 'count' blocks - this space may
827 * be considerably less than the calculated radix due to the large
828 * RADIX values we use.
831 blst_radix_init(blmeta_t *scan, daddr_t radix, daddr_t count)
833 daddr_t i, memindex, next_skip, skip;
841 if (radix == BLIST_BMAP_RADIX) {
843 scan->bm_bighint = 0;
844 scan->u.bmu_bitmap = 0;
850 * Meta node. If allocating the entire object we can special
851 * case it. However, we need to figure out how much memory
852 * is required to manage 'count' blocks, so we continue on anyway.
856 scan->bm_bighint = 0;
857 scan->u.bmu_avail = 0;
860 skip = radix_to_skip(radix);
861 next_skip = skip / BLIST_META_RADIX;
862 radix /= BLIST_META_RADIX;
864 for (i = 1; i < skip; i += next_skip) {
865 if (count >= radix) {
867 * Allocate the entire object
870 blst_radix_init(((scan) ? &scan[i] : NULL), radix,
873 } else if (count > 0) {
875 * Allocate a partial object
878 blst_radix_init(((scan) ? &scan[i] : NULL), radix,
883 * Add terminator and break out
886 scan[i].bm_bighint = (daddr_t)-1;
898 blst_radix_print(blmeta_t *scan, daddr_t blk, daddr_t radix, int tab)
900 daddr_t i, next_skip, skip;
902 if (radix == BLIST_BMAP_RADIX) {
904 "%*.*s(%08llx,%lld): bitmap %016llx big=%lld\n",
906 (long long)blk, (long long)radix,
907 (long long)scan->u.bmu_bitmap,
908 (long long)scan->bm_bighint
913 if (scan->u.bmu_avail == 0) {
915 "%*.*s(%08llx,%lld) ALL ALLOCATED\n",
922 if (scan->u.bmu_avail == radix) {
924 "%*.*s(%08llx,%lld) ALL FREE\n",
933 "%*.*s(%08llx,%lld): subtree (%lld/%lld) big=%lld {\n",
935 (long long)blk, (long long)radix,
936 (long long)scan->u.bmu_avail,
938 (long long)scan->bm_bighint
941 skip = radix_to_skip(radix);
942 next_skip = skip / BLIST_META_RADIX;
943 radix /= BLIST_META_RADIX;
946 for (i = 1; i < skip; i += next_skip) {
947 if (scan[i].bm_bighint == (daddr_t)-1) {
949 "%*.*s(%08llx,%lld): Terminator\n",
951 (long long)blk, (long long)radix
955 blst_radix_print(&scan[i], blk, radix, tab);
971 main(int ac, char **av)
977 for (i = 1; i < ac; ++i) {
978 const char *ptr = av[i];
980 size = strtol(ptr, NULL, 0);
984 fprintf(stderr, "Bad option: %s\n", ptr - 2);
987 bl = blist_create(size, M_WAITOK);
988 blist_free(bl, 0, size);
995 printf("%lld/%lld/%lld> ", (long long)blist_avail(bl),
996 (long long)size, (long long)bl->bl_radix);
998 if (fgets(buf, sizeof(buf), stdin) == NULL)
1002 if (sscanf(buf + 1, "%lld", &count) == 1) {
1003 blist_resize(&bl, count, 1, M_WAITOK);
1011 if (sscanf(buf + 1, "%lld", &count) == 1) {
1012 daddr_t blk = blist_alloc(bl, count);
1013 printf(" R=%08llx\n", (long long)blk);
1019 if (sscanf(buf + 1, "%llx %lld", &da, &count) == 2) {
1020 blist_free(bl, da, count);
1026 if (sscanf(buf + 1, "%llx %lld", &da, &count) == 2) {
1028 (intmax_t)blist_fill(bl, da, count));
1053 panic(const char *ctl, ...)
1058 vfprintf(stderr, ctl, va);
1059 fprintf(stderr, "\n");