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