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