1 /* $NetBSD: subr_blist.c,v 1.8 2005/12/11 12:24:30 christos Exp $ */
4 * Copyright (c) 1998 Matthew Dillon. All Rights Reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * BLIST.C - Bitmap allocator/deallocator, using a radix tree with hinting
32 * This module implements a general bitmap allocator/deallocator. The
33 * allocator eats around 2 bits per 'block'. The module does not
34 * try to interpret the meaning of a 'block' other then to return
35 * BLIST_NONE on an allocation failure.
37 * A radix tree is used to maintain the bitmap. Two radix constants are
38 * involved: One for the bitmaps contained in the leaf nodes (typically
39 * 32), and one for the meta nodes (typically 16). Both meta and leaf
40 * nodes have a hint field. This field gives us a hint as to the largest
41 * free contiguous range of blocks under the node. It may contain a
42 * value that is too high, but will never contain a value that is too
43 * low. When the radix tree is searched, allocation failures in subtrees
46 * The radix tree also implements two collapsed states for meta nodes:
47 * the ALL-ALLOCATED state and the ALL-FREE state. If a meta node is
48 * in either of these two states, all information contained underneath
49 * the node is considered stale. These states are used to optimize
50 * allocation and freeing operations.
52 * The hinting greatly increases code efficiency for allocations while
53 * the general radix structure optimizes both allocations and frees. The
54 * radix tree should be able to operate well no matter how much
55 * fragmentation there is and no matter how large a bitmap is used.
57 * Unlike the rlist code, the blist code wires all necessary memory at
58 * creation time. Neither allocations nor frees require interaction with
59 * the memory subsystem. In contrast, the rlist code may allocate memory
60 * on an rlist_free() call. The non-blocking features of the blist code
61 * are used to great advantage in the swap code (vm/nswap_pager.c). The
62 * rlist code uses a little less overall memory then the blist code (but
63 * due to swap interleaving not all that much less), but the blist code
64 * scales much, much better.
66 * LAYOUT: The radix tree is layed out recursively using a
67 * linear array. Each meta node is immediately followed (layed out
68 * sequentially in memory) by BLIST_META_RADIX lower level nodes. This
69 * is a recursive structure but one that can be easily scanned through
70 * a very simple 'skip' calculation. In order to support large radixes,
71 * portions of the tree may reside outside our memory allocation. We
72 * handle this with an early-termination optimization (when bighint is
73 * set to -1) on the scan. The memory allocation is only large enough
74 * to cover the number of blocks requested at creation time even if it
75 * must be encompassed in larger root-node radix.
77 * NOTE: the allocator cannot currently allocate more then
78 * BLIST_BMAP_RADIX blocks per call. It will panic with 'allocation too
79 * large' if you try. This is an area that could use improvement. The
80 * radix is large enough that this restriction does not effect the swap
81 * system, though. Currently only the allocation code is effected by
82 * this algorithmic unfeature. The freeing code can handle arbitrary
85 * This code can be compiled stand-alone for debugging.
88 #include <sys/cdefs.h>
89 __KERNEL_RCSID(0, "$NetBSD: subr_blist.c,v 1.8 2005/12/11 12:24:30 christos Exp $");
91 __FBSDID("$FreeBSD: src/sys/kern/subr_blist.c,v 1.17 2004/06/04 04:03:25 alc Exp $");
96 #include <sys/param.h>
97 #include <sys/systm.h>
98 #include <sys/blist.h>
99 #include <sys/malloc.h>
103 #ifndef BLIST_NO_DEBUG
107 #include <sys/types.h>
112 #include <inttypes.h>
114 #define malloc(a,b,c) calloc(a, 1)
115 #define free(a,b) free(a)
117 #include "../sys/blist.h"
119 void panic(const char *ctl
, ...);
124 * blmeta and bl_bitmap_t MUST be a power of 2 in size.
127 typedef struct blmeta
{
129 blist_blkno_t bmu_avail
; /* space available under us */
130 blist_bitmap_t bmu_bitmap
; /* bitmap if we are a leaf */
132 blist_blkno_t bm_bighint
; /* biggest contiguous block hint*/
136 blist_blkno_t bl_blocks
; /* area of coverage */
137 blist_blkno_t bl_radix
; /* coverage radix */
138 blist_blkno_t bl_skip
; /* starting skip */
139 blist_blkno_t bl_free
; /* number of free blocks */
140 blmeta_t
*bl_root
; /* root of radix tree */
141 blist_blkno_t bl_rootblks
; /* blks allocated for tree */
144 #define BLIST_META_RADIX 16
147 * static support functions
150 static blist_blkno_t
blst_leaf_alloc(blmeta_t
*scan
, blist_blkno_t blk
,
152 static blist_blkno_t
blst_meta_alloc(blmeta_t
*scan
, blist_blkno_t blk
,
153 blist_blkno_t count
, blist_blkno_t radix
, blist_blkno_t skip
);
154 static void blst_leaf_free(blmeta_t
*scan
, blist_blkno_t relblk
, int count
);
155 static void blst_meta_free(blmeta_t
*scan
, blist_blkno_t freeBlk
,
156 blist_blkno_t count
, blist_blkno_t radix
, blist_blkno_t skip
,
158 static void blst_copy(blmeta_t
*scan
, blist_blkno_t blk
, blist_blkno_t radix
,
159 blist_blkno_t skip
, blist_t dest
, blist_blkno_t count
);
160 static int blst_leaf_fill(blmeta_t
*scan
, blist_blkno_t blk
, int count
);
161 static blist_blkno_t
blst_meta_fill(blmeta_t
*scan
, blist_blkno_t allocBlk
,
162 blist_blkno_t count
, blist_blkno_t radix
, blist_blkno_t skip
,
164 static blist_blkno_t
blst_radix_init(blmeta_t
*scan
, blist_blkno_t radix
,
165 blist_blkno_t skip
, blist_blkno_t count
);
167 static void blst_radix_print(blmeta_t
*scan
, blist_blkno_t blk
,
168 blist_blkno_t radix
, blist_blkno_t skip
, int tab
);
172 static MALLOC_DEFINE(M_BLIST
, "blist", "Bitmap allocator");
176 * blist_create() - create a blist capable of handling up to the specified
179 * blocks must be greater then 0
181 * The smallest blist consists of a single leaf node capable of
182 * managing BLIST_BMAP_RADIX blocks.
186 blist_create(blist_blkno_t blocks
)
190 blist_blkno_t skip
= 0;
193 * Calculate radix and skip field used for scanning.
197 radix
= BLIST_BMAP_RADIX
;
199 while (radix
< blocks
) {
200 radix
*= BLIST_META_RADIX
;
201 skip
= (skip
+ 1) * BLIST_META_RADIX
;
204 bl
= malloc(sizeof(struct blist
), M_BLIST
, M_WAITOK
| M_ZERO
);
206 bl
->bl_blocks
= blocks
;
207 bl
->bl_radix
= radix
;
209 bl
->bl_rootblks
= 1 +
210 blst_radix_init(NULL
, bl
->bl_radix
, bl
->bl_skip
, blocks
);
211 bl
->bl_root
= malloc(sizeof(blmeta_t
) * bl
->bl_rootblks
, M_BLIST
, M_WAITOK
);
213 #if defined(BLIST_DEBUG)
215 "BLIST representing %" PRIu64
" blocks (%" PRIu64
" MB of swap)"
216 ", requiring %" PRIu64
"K of ram\n",
217 (uint64_t)bl
->bl_blocks
,
218 (uint64_t)bl
->bl_blocks
* 4 / 1024,
219 ((uint64_t)bl
->bl_rootblks
* sizeof(blmeta_t
) + 1023) / 1024
221 printf("BLIST raw radix tree contains %" PRIu64
" records\n",
222 (uint64_t)bl
->bl_rootblks
);
224 blst_radix_init(bl
->bl_root
, bl
->bl_radix
, bl
->bl_skip
, blocks
);
230 blist_destroy(blist_t bl
)
232 free(bl
->bl_root
, M_BLIST
);
237 * blist_alloc() - reserve space in the block bitmap. Return the base
238 * of a contiguous region or BLIST_NONE if space could
243 blist_alloc(blist_t bl
, blist_blkno_t count
)
245 blist_blkno_t blk
= BLIST_NONE
;
248 if (bl
->bl_radix
== BLIST_BMAP_RADIX
)
249 blk
= blst_leaf_alloc(bl
->bl_root
, 0, count
);
251 blk
= blst_meta_alloc(bl
->bl_root
, 0, count
, bl
->bl_radix
, bl
->bl_skip
);
252 if (blk
!= BLIST_NONE
)
253 bl
->bl_free
-= count
;
259 * blist_free() - free up space in the block bitmap. Return the base
260 * of a contiguous region. Panic if an inconsistancy is
265 blist_free(blist_t bl
, blist_blkno_t blkno
, blist_blkno_t count
)
268 if (bl
->bl_radix
== BLIST_BMAP_RADIX
)
269 blst_leaf_free(bl
->bl_root
, blkno
, count
);
271 blst_meta_free(bl
->bl_root
, blkno
, count
, bl
->bl_radix
, bl
->bl_skip
, 0);
272 bl
->bl_free
+= count
;
277 * blist_fill() - mark a region in the block bitmap as off-limits
278 * to the allocator (i.e. allocate it), ignoring any
279 * existing allocations. Return the number of blocks
280 * actually filled that were free before the call.
284 blist_fill(blist_t bl
, blist_blkno_t blkno
, blist_blkno_t count
)
286 blist_blkno_t filled
;
289 if (bl
->bl_radix
== BLIST_BMAP_RADIX
)
290 filled
= blst_leaf_fill(bl
->bl_root
, blkno
, count
);
292 filled
= blst_meta_fill(bl
->bl_root
, blkno
, count
,
293 bl
->bl_radix
, bl
->bl_skip
, 0);
294 bl
->bl_free
-= filled
;
301 * blist_resize() - resize an existing radix tree to handle the
302 * specified number of blocks. This will reallocate
303 * the tree and transfer the previous bitmap to the new
304 * one. When extending the tree you can specify whether
305 * the new blocks are to left allocated or freed.
309 blist_resize(blist_t
*pbl
, blist_blkno_t count
, int freenew
)
311 blist_t newbl
= blist_create(count
);
315 if (count
> save
->bl_blocks
)
316 count
= save
->bl_blocks
;
317 blst_copy(save
->bl_root
, 0, save
->bl_radix
, save
->bl_skip
, newbl
, count
);
320 * If resizing upwards, should we free the new space or not?
322 if (freenew
&& count
< newbl
->bl_blocks
) {
323 blist_free(newbl
, count
, newbl
->bl_blocks
- count
);
331 * blist_print() - dump radix tree
335 blist_print(blist_t bl
)
338 blst_radix_print(bl
->bl_root
, 0, bl
->bl_radix
, bl
->bl_skip
, 4);
344 /************************************************************************
345 * ALLOCATION SUPPORT FUNCTIONS *
346 ************************************************************************
348 * These support functions do all the actual work. They may seem
349 * rather longish, but that's because I've commented them up. The
350 * actual code is straight forward.
355 * blist_leaf_alloc() - allocate at a leaf in the radix tree (a bitmap).
357 * This is the core of the allocator and is optimized for the 1 block
358 * and the BLIST_BMAP_RADIX block allocation cases. Other cases are
359 * somewhat slower. The 1 block allocation case is log2 and extremely
369 blist_bitmap_t orig
= scan
->u
.bmu_bitmap
;
373 * Optimize bitmap all-allocated case. Also, count = 1
374 * case assumes at least 1 bit is free in the bitmap, so
375 * we have to take care of this case here.
377 scan
->bm_bighint
= 0;
382 * Optimized code to allocate one bit out of the bitmap
385 int j
= BLIST_BMAP_RADIX
/2;
388 mask
= (blist_bitmap_t
)-1 >> (BLIST_BMAP_RADIX
/2);
391 if ((orig
& mask
) == 0) {
398 scan
->u
.bmu_bitmap
&= ~((blist_bitmap_t
)1 << r
);
401 if (count
<= BLIST_BMAP_RADIX
) {
403 * non-optimized code to allocate N bits out of the bitmap.
404 * The more bits, the faster the code runs. It will run
405 * the slowest allocating 2 bits, but since there aren't any
406 * memory ops in the core loop (or shouldn't be, anyway),
407 * you probably won't notice the difference.
410 int n
= BLIST_BMAP_RADIX
- count
;
413 mask
= (blist_bitmap_t
)-1 >> n
;
415 for (j
= 0; j
<= n
; ++j
) {
416 if ((orig
& mask
) == mask
) {
417 scan
->u
.bmu_bitmap
&= ~mask
;
424 * We couldn't allocate count in this subtree, update bighint.
426 scan
->bm_bighint
= count
- 1;
431 * blist_meta_alloc() - allocate at a meta in the radix tree.
433 * Attempt to allocate at a meta node. If we can't, we update
434 * bighint and return a failure. Updating bighint optimize future
435 * calls that hit this node. We have to check for our collapse cases
436 * and we have a few optimizations strewn in as well.
448 blist_blkno_t next_skip
= (skip
/ BLIST_META_RADIX
);
450 if (scan
->u
.bmu_avail
== 0) {
452 * ALL-ALLOCATED special case
454 scan
->bm_bighint
= count
;
458 if (scan
->u
.bmu_avail
== radix
) {
459 radix
/= BLIST_META_RADIX
;
462 * ALL-FREE special case, initialize uninitialize
465 for (i
= 1; i
<= skip
; i
+= next_skip
) {
466 if (scan
[i
].bm_bighint
== (blist_blkno_t
)-1)
468 if (next_skip
== 1) {
469 scan
[i
].u
.bmu_bitmap
= (blist_bitmap_t
)-1;
470 scan
[i
].bm_bighint
= BLIST_BMAP_RADIX
;
472 scan
[i
].bm_bighint
= radix
;
473 scan
[i
].u
.bmu_avail
= radix
;
477 radix
/= BLIST_META_RADIX
;
480 for (i
= 1; i
<= skip
; i
+= next_skip
) {
481 if (scan
[i
].bm_bighint
== (blist_blkno_t
)-1) {
486 } else if (count
<= scan
[i
].bm_bighint
) {
488 * count fits in object
491 if (next_skip
== 1) {
492 r
= blst_leaf_alloc(&scan
[i
], blk
, count
);
494 r
= blst_meta_alloc(&scan
[i
], blk
, count
, radix
, next_skip
- 1);
496 if (r
!= BLIST_NONE
) {
497 scan
->u
.bmu_avail
-= count
;
498 if (scan
->bm_bighint
> scan
->u
.bmu_avail
)
499 scan
->bm_bighint
= scan
->u
.bmu_avail
;
502 } else if (count
> radix
) {
504 * count does not fit in object even if it were
507 panic("blist_meta_alloc: allocation too large");
513 * We couldn't allocate count in this subtree, update bighint.
515 if (scan
->bm_bighint
>= count
)
516 scan
->bm_bighint
= count
- 1;
521 * BLST_LEAF_FREE() - free allocated block from leaf bitmap
532 * free some data in this bitmap
535 * 0000111111111110000
539 int n
= blk
& (BLIST_BMAP_RADIX
- 1);
542 mask
= ((blist_bitmap_t
)-1 << n
) &
543 ((blist_bitmap_t
)-1 >> (BLIST_BMAP_RADIX
- count
- n
));
545 if (scan
->u
.bmu_bitmap
& mask
)
546 panic("blst_radix_free: freeing free block");
547 scan
->u
.bmu_bitmap
|= mask
;
550 * We could probably do a better job here. We are required to make
551 * bighint at least as large as the biggest contiguous block of
552 * data. If we just shoehorn it, a little extra overhead will
553 * be incured on the next allocation (but only that one typically).
555 scan
->bm_bighint
= BLIST_BMAP_RADIX
;
559 * BLST_META_FREE() - free allocated blocks from radix tree meta info
561 * This support routine frees a range of blocks from the bitmap.
562 * The range must be entirely enclosed by this radix node. If a
563 * meta node, we break the range down recursively to free blocks
564 * in subnodes (which means that this code can free an arbitrary
565 * range whereas the allocation code cannot allocate an arbitrary
572 blist_blkno_t freeBlk
,
579 blist_blkno_t next_skip
= (skip
/ BLIST_META_RADIX
);
582 printf("FREE (%" PRIx64
",%" PRIu64
583 ") FROM (%" PRIx64
",%" PRIu64
")\n",
584 (uint64_t)freeBlk
, (uint64_t)count
,
585 (uint64_t)blk
, (uint64_t)radix
589 if (scan
->u
.bmu_avail
== 0) {
591 * ALL-ALLOCATED special case, with possible
592 * shortcut to ALL-FREE special case.
594 scan
->u
.bmu_avail
= count
;
595 scan
->bm_bighint
= count
;
597 if (count
!= radix
) {
598 for (i
= 1; i
<= skip
; i
+= next_skip
) {
599 if (scan
[i
].bm_bighint
== (blist_blkno_t
)-1)
601 scan
[i
].bm_bighint
= 0;
602 if (next_skip
== 1) {
603 scan
[i
].u
.bmu_bitmap
= 0;
605 scan
[i
].u
.bmu_avail
= 0;
611 scan
->u
.bmu_avail
+= count
;
612 /* scan->bm_bighint = radix; */
616 * ALL-FREE special case.
619 if (scan
->u
.bmu_avail
== radix
)
621 if (scan
->u
.bmu_avail
> radix
)
622 panic("blst_meta_free: freeing already free blocks (%"
623 PRIu64
") %" PRIu64
"/%" PRIu64
,
625 (uint64_t)scan
->u
.bmu_avail
,
629 * Break the free down into its components
632 radix
/= BLIST_META_RADIX
;
634 i
= (freeBlk
- blk
) / radix
;
636 i
= i
* next_skip
+ 1;
638 while (i
<= skip
&& blk
< freeBlk
+ count
) {
641 v
= blk
+ radix
- freeBlk
;
645 if (scan
->bm_bighint
== (blist_blkno_t
)-1)
646 panic("blst_meta_free: freeing unexpected range");
648 if (next_skip
== 1) {
649 blst_leaf_free(&scan
[i
], freeBlk
, v
);
651 blst_meta_free(&scan
[i
], freeBlk
, v
, radix
, next_skip
- 1, blk
);
653 if (scan
->bm_bighint
< scan
[i
].bm_bighint
)
654 scan
->bm_bighint
= scan
[i
].bm_bighint
;
663 * BLIST_RADIX_COPY() - copy one radix tree to another
665 * Locates free space in the source tree and frees it in the destination
666 * tree. The space may not already be free in the destination.
669 static void blst_copy(
677 blist_blkno_t next_skip
;
684 if (radix
== BLIST_BMAP_RADIX
) {
685 blist_bitmap_t v
= scan
->u
.bmu_bitmap
;
687 if (v
== (blist_bitmap_t
)-1) {
688 blist_free(dest
, blk
, count
);
692 for (j
= 0; j
< BLIST_BMAP_RADIX
&& j
< count
; ++j
) {
694 blist_free(dest
, blk
+ j
, 1);
704 if (scan
->u
.bmu_avail
== 0) {
706 * Source all allocated, leave dest allocated
710 if (scan
->u
.bmu_avail
== radix
) {
712 * Source all free, free entire dest
715 blist_free(dest
, blk
, count
);
717 blist_free(dest
, blk
, radix
);
722 radix
/= BLIST_META_RADIX
;
723 next_skip
= (skip
/ BLIST_META_RADIX
);
725 for (i
= 1; count
&& i
<= skip
; i
+= next_skip
) {
726 if (scan
[i
].bm_bighint
== (blist_blkno_t
)-1)
729 if (count
>= radix
) {
757 * BLST_LEAF_FILL() - allocate specific blocks in leaf bitmap
759 * This routine allocates all blocks in the specified range
760 * regardless of any existing allocations in that range. Returns
761 * the number of blocks allocated by the call.
765 blst_leaf_fill(blmeta_t
*scan
, blist_blkno_t blk
, int count
)
767 int n
= blk
& (BLIST_BMAP_RADIX
- 1);
769 blist_bitmap_t mask
, bitmap
;
771 mask
= ((blist_bitmap_t
)-1 << n
) &
772 ((blist_bitmap_t
)-1 >> (BLIST_BMAP_RADIX
- count
- n
));
774 /* Count the number of blocks we're about to allocate */
775 bitmap
= scan
->u
.bmu_bitmap
& mask
;
776 for (nblks
= 0; bitmap
!= 0; nblks
++)
777 bitmap
&= bitmap
- 1;
779 scan
->u
.bmu_bitmap
&= ~mask
;
784 * BLIST_META_FILL() - allocate specific blocks at a meta node
786 * This routine allocates the specified range of blocks,
787 * regardless of any existing allocations in the range. The
788 * range must be within the extent of this node. Returns the
789 * number of blocks allocated by the call.
794 blist_blkno_t allocBlk
,
801 blist_blkno_t next_skip
= (skip
/ BLIST_META_RADIX
);
802 blist_blkno_t nblks
= 0;
804 if (count
== radix
|| scan
->u
.bmu_avail
== 0) {
806 * ALL-ALLOCATED special case
808 nblks
= scan
->u
.bmu_avail
;
809 scan
->u
.bmu_avail
= 0;
810 scan
->bm_bighint
= count
;
815 panic("blist_meta_fill: allocation too large");
817 if (scan
->u
.bmu_avail
== radix
) {
818 radix
/= BLIST_META_RADIX
;
821 * ALL-FREE special case, initialize sublevel
823 for (i
= 1; i
<= skip
; i
+= next_skip
) {
824 if (scan
[i
].bm_bighint
== (blist_blkno_t
)-1)
826 if (next_skip
== 1) {
827 scan
[i
].u
.bmu_bitmap
= (blist_bitmap_t
)-1;
828 scan
[i
].bm_bighint
= BLIST_BMAP_RADIX
;
830 scan
[i
].bm_bighint
= radix
;
831 scan
[i
].u
.bmu_avail
= radix
;
835 radix
/= BLIST_META_RADIX
;
838 i
= (allocBlk
- blk
) / radix
;
840 i
= i
* next_skip
+ 1;
842 while (i
<= skip
&& blk
< allocBlk
+ count
) {
845 v
= blk
+ radix
- allocBlk
;
849 if (scan
->bm_bighint
== (blist_blkno_t
)-1)
850 panic("blst_meta_fill: filling unexpected range");
852 if (next_skip
== 1) {
853 nblks
+= blst_leaf_fill(&scan
[i
], allocBlk
, v
);
855 nblks
+= blst_meta_fill(&scan
[i
], allocBlk
, v
,
856 radix
, next_skip
- 1, blk
);
863 scan
->u
.bmu_avail
-= nblks
;
868 * BLST_RADIX_INIT() - initialize radix tree
870 * Initialize our meta structures and bitmaps and calculate the exact
871 * amount of space required to manage 'count' blocks - this space may
872 * be considerably less then the calculated radix due to the large
873 * RADIX values we use.
877 blst_radix_init(blmeta_t
*scan
, blist_blkno_t radix
, blist_blkno_t skip
,
881 blist_blkno_t next_skip
;
882 blist_blkno_t memindex
= 0;
888 if (radix
== BLIST_BMAP_RADIX
) {
890 scan
->bm_bighint
= 0;
891 scan
->u
.bmu_bitmap
= 0;
897 * Meta node. If allocating the entire object we can special
898 * case it. However, we need to figure out how much memory
899 * is required to manage 'count' blocks, so we continue on anyway.
903 scan
->bm_bighint
= 0;
904 scan
->u
.bmu_avail
= 0;
907 radix
/= BLIST_META_RADIX
;
908 next_skip
= (skip
/ BLIST_META_RADIX
);
910 for (i
= 1; i
<= skip
; i
+= next_skip
) {
911 if (count
>= radix
) {
913 * Allocate the entire object
915 memindex
= i
+ blst_radix_init(
916 ((scan
) ? &scan
[i
] : NULL
),
922 } else if (count
> 0) {
924 * Allocate a partial object
926 memindex
= i
+ blst_radix_init(
927 ((scan
) ? &scan
[i
] : NULL
),
935 * Add terminator and break out
938 scan
[i
].bm_bighint
= (blist_blkno_t
)-1;
950 blst_radix_print(blmeta_t
*scan
, blist_blkno_t blk
, blist_blkno_t radix
,
951 blist_blkno_t skip
, int tab
)
954 blist_blkno_t next_skip
;
957 if (radix
== BLIST_BMAP_RADIX
) {
959 "%*.*s(%0*" PRIx64
",%" PRIu64
960 "): bitmap %0*" PRIx64
" big=%" PRIu64
"\n",
965 sizeof(scan
->u
.bmu_bitmap
) * 2,
966 (uint64_t)scan
->u
.bmu_bitmap
,
967 (uint64_t)scan
->bm_bighint
972 if (scan
->u
.bmu_avail
== 0) {
974 "%*.*s(%0*" PRIx64
",%" PRIu64
") ALL ALLOCATED\n",
982 if (scan
->u
.bmu_avail
== radix
) {
984 "%*.*s(%0*" PRIx64
",%" PRIu64
") ALL FREE\n",
994 "%*.*s(%0*" PRIx64
",%" PRIu64
"): subtree (%" PRIu64
"/%"
995 PRIu64
") big=%" PRIu64
" {\n",
1000 (uint64_t)scan
->u
.bmu_avail
,
1002 (uint64_t)scan
->bm_bighint
1005 radix
/= BLIST_META_RADIX
;
1006 next_skip
= (skip
/ BLIST_META_RADIX
);
1009 for (i
= 1; i
<= skip
; i
+= next_skip
) {
1010 if (scan
[i
].bm_bighint
== (blist_blkno_t
)-1) {
1012 "%*.*s(%0*" PRIx64
",%" PRIu64
"): Terminator\n",
1043 main(int ac
, char **av
)
1045 blist_blkno_t size
= 1024;
1049 for (i
= 1; i
< ac
; ++i
) {
1050 const char *ptr
= av
[i
];
1052 size
= strtol(ptr
, NULL
, 0);
1056 fprintf(stderr
, "Bad option: %s\n", ptr
- 2);
1059 bl
= blist_create(size
);
1060 blist_free(bl
, 0, size
);
1067 printf("%" PRIu64
"/%" PRIu64
"/%" PRIu64
"> ",
1068 (uint64_t)bl
->bl_free
,
1070 (uint64_t)bl
->bl_radix
);
1072 if (fgets(buf
, sizeof(buf
), stdin
) == NULL
)
1076 if (sscanf(buf
+ 1, "%" SCNu64
, &count
) == 1) {
1077 blist_resize(&bl
, count
, 1);
1085 if (sscanf(buf
+ 1, "%" SCNu64
, &count
) == 1) {
1086 blist_blkno_t blk
= blist_alloc(bl
, count
);
1087 printf(" R=%0*" PRIx64
"\n",
1095 if (sscanf(buf
+ 1, "%" SCNx64
" %" SCNu64
,
1096 &da
, &count
) == 2) {
1097 blist_free(bl
, da
, count
);
1103 if (sscanf(buf
+ 1, "%" SCNx64
" %" SCNu64
,
1104 &da
, &count
) == 2) {
1105 printf(" n=%" PRIu64
"\n",
1106 (uint64_t)blist_fill(bl
, da
, count
));
1131 panic(const char *ctl
, ...)
1136 vfprintf(stderr
, ctl
, va
);
1137 fprintf(stderr
, "\n");