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