2 * ALIST.C - Bitmap allocator/deallocator, using a radix tree with hinting.
3 * Unlimited-size allocations, power-of-2 only, power-of-2
4 * aligned results only.
6 * Copyright (c) 2007 The DragonFly Project. All rights reserved.
8 * This code is derived from software contributed to The DragonFly Project
9 * by Matthew Dillon <dillon@backplane.com>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in
19 * the documentation and/or other materials provided with the
21 * 3. Neither the name of The DragonFly Project nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific, prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
31 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
33 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
34 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
35 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * $DragonFly: src/sys/kern/subr_alist.c,v 1.2 2007/04/19 03:16:33 dillon Exp $
41 * This module has been adapted from the BLIST module, which was written
42 * by Matthew Dillon many years ago.
44 * This module implements a general power-of-2 bitmap allocator/deallocator.
45 * All allocations must be in powers of 2 and will return similarly aligned
46 * results. The module does not try to interpret the meaning of a 'block'
47 * other then to return ALIST_BLOCK_NONE on an allocation failure.
49 * A maximum of 2 billion blocks is supported so, for example, if one block
50 * represented 64 bytes a maximally sized ALIST would represent
53 * A radix tree is used to maintain the bitmap and layed out in a manner
54 * similar to the blist code. Meta nodes use a radix of 16 and 2 bits per
55 * block while leaf nodes use a radix of 32 and 1 bit per block (stored in
56 * a 32 bit bitmap field). Both meta and leaf nodes have a hint field.
57 * This field gives us a hint as to the largest free contiguous range of
58 * blocks under the node. It may contain a value that is too high, but
59 * will never contain a value that is too low. When the radix tree is
60 * searched, allocation failures in subtrees update the hint.
62 * The radix tree is layed out recursively using a linear array. Each meta
63 * node is immediately followed (layed out sequentially in memory) by
64 * ALIST_META_RADIX lower level nodes. This is a recursive structure but one
65 * that can be easily scanned through a very simple 'skip' calculation. In
66 * order to support large radixes, portions of the tree may reside outside our
67 * memory allocation. We handle this with an early-terminate optimization
68 * in the meta-node. The memory allocation is only large enough to cover
69 * the number of blocks requested at creation time even if it must be
70 * encompassed in larger root-node radix.
72 * This code can be compiled stand-alone for debugging.
77 #include <sys/param.h>
78 #include <sys/systm.h>
80 #include <sys/kernel.h>
81 #include <sys/alist.h>
82 #include <sys/malloc.h>
84 #include <vm/vm_object.h>
85 #include <vm/vm_kern.h>
86 #include <vm/vm_extern.h>
87 #include <vm/vm_page.h>
91 #ifndef ALIST_NO_DEBUG
95 #include <sys/types.h>
102 #define kmalloc(a,b,c) malloc(a)
103 #define kfree(a,b) free(a)
104 #define kprintf printf
105 #define KKASSERT(exp) assert(exp)
108 typedef unsigned int u_daddr_t;
110 #include <sys/alist.h>
112 void panic(const char *ctl, ...);
117 * static support functions
120 static daddr_t alst_leaf_alloc(almeta_t *scan, daddr_t blk, int count);
121 static daddr_t alst_meta_alloc(almeta_t *scan, daddr_t blk,
122 daddr_t count, daddr_t radix, int skip);
123 static void alst_leaf_free(almeta_t *scan, daddr_t relblk, int count);
124 static void alst_meta_free(almeta_t *scan, daddr_t freeBlk, daddr_t count,
125 daddr_t radix, int skip, daddr_t blk);
126 static daddr_t alst_radix_init(almeta_t *scan, daddr_t radix,
127 int skip, daddr_t count);
129 static void alst_radix_print(almeta_t *scan, daddr_t blk,
130 daddr_t radix, int skip, int tab);
134 * alist_create() - create a alist capable of handling up to the specified
137 * blocks must be greater then 0
139 * The smallest alist consists of a single leaf node capable of
140 * managing ALIST_BMAP_RADIX blocks.
144 alist_create(daddr_t blocks, struct malloc_type *mtype)
151 * Calculate radix and skip field used for scanning.
153 radix = ALIST_BMAP_RADIX;
155 while (radix < blocks) {
156 radix *= ALIST_META_RADIX;
157 skip = (skip + 1) * ALIST_META_RADIX;
160 bl = kmalloc(sizeof(struct alist), mtype, M_WAITOK);
162 bzero(bl, sizeof(*bl));
164 bl->bl_blocks = blocks;
165 bl->bl_radix = radix;
167 bl->bl_rootblks = 1 +
168 alst_radix_init(NULL, bl->bl_radix, bl->bl_skip, blocks);
169 bl->bl_root = kmalloc(sizeof(almeta_t) * bl->bl_rootblks, mtype, M_WAITOK);
171 #if defined(ALIST_DEBUG)
173 "ALIST representing %d blocks (%d MB of swap)"
174 ", requiring %dK of ram\n",
176 bl->bl_blocks * 4 / 1024,
177 (bl->bl_rootblks * sizeof(almeta_t) + 1023) / 1024
179 kprintf("ALIST raw radix tree contains %d records\n", bl->bl_rootblks);
181 alst_radix_init(bl->bl_root, bl->bl_radix, bl->bl_skip, blocks);
187 alist_destroy(alist_t bl, struct malloc_type *mtype)
189 kfree(bl->bl_root, mtype);
194 * alist_alloc() - reserve space in the block bitmap. Return the base
195 * of a contiguous region or ALIST_BLOCK_NONE if space
196 * could not be allocated.
200 alist_alloc(alist_t bl, daddr_t count)
202 daddr_t blk = ALIST_BLOCK_NONE;
204 KKASSERT((count | (count - 1)) == (count << 1) - 1);
206 if (bl && count < bl->bl_radix) {
207 if (bl->bl_radix == ALIST_BMAP_RADIX)
208 blk = alst_leaf_alloc(bl->bl_root, 0, count);
210 blk = alst_meta_alloc(bl->bl_root, 0, count, bl->bl_radix, bl->bl_skip);
211 if (blk != ALIST_BLOCK_NONE)
212 bl->bl_free -= count;
218 * alist_free() - free up space in the block bitmap. Return the base
219 * of a contiguous region. Panic if an inconsistancy is
224 alist_free(alist_t bl, daddr_t blkno, daddr_t count)
227 KKASSERT(blkno + count <= bl->bl_blocks);
228 if (bl->bl_radix == ALIST_BMAP_RADIX)
229 alst_leaf_free(bl->bl_root, blkno, count);
231 alst_meta_free(bl->bl_root, blkno, count, bl->bl_radix, bl->bl_skip, 0);
232 bl->bl_free += count;
239 * alist_print() - dump radix tree
243 alist_print(alist_t bl)
245 kprintf("ALIST {\n");
246 alst_radix_print(bl->bl_root, 0, bl->bl_radix, bl->bl_skip, 4);
252 /************************************************************************
253 * ALLOCATION SUPPORT FUNCTIONS *
254 ************************************************************************
256 * These support functions do all the actual work. They may seem
257 * rather longish, but that's because I've commented them up. The
258 * actual code is straight forward.
263 * alist_leaf_alloc() - allocate at a leaf in the radix tree (a bitmap).
265 * This is the core of the allocator and is optimized for the 1 block
266 * and the ALIST_BMAP_RADIX block allocation cases. Other cases are
267 * somewhat slower. The 1 block allocation case is log2 and extremely
277 u_daddr_t orig = scan->bm_bitmap;
280 * Optimize bitmap all-allocated case. Also, count = 1
281 * case assumes at least 1 bit is free in the bitmap, so
282 * we have to take care of this case here.
285 scan->bm_bighint = 0;
286 return(ALIST_BLOCK_NONE);
290 * Optimized code to allocate one bit out of the bitmap
294 int j = ALIST_BMAP_RADIX/2;
297 mask = (u_daddr_t)-1 >> (ALIST_BMAP_RADIX/2);
300 if ((orig & mask) == 0) {
307 scan->bm_bitmap &= ~(1 << r);
312 * non-optimized code to allocate N bits out of the bitmap.
313 * The more bits, the faster the code runs. It will run
314 * the slowest allocating 2 bits, but since there aren't any
315 * memory ops in the core loop (or shouldn't be, anyway),
316 * you probably won't notice the difference.
318 * Similar to the blist case, the alist code also requires
319 * allocations to be power-of-2 sized and aligned to the
320 * size of the allocation, which simplifies the algorithm.
324 int n = ALIST_BMAP_RADIX - count;
327 mask = (u_daddr_t)-1 >> n;
329 for (j = 0; j <= n; j += count) {
330 if ((orig & mask) == mask) {
331 scan->bm_bitmap &= ~mask;
334 mask = mask << count;
339 * We couldn't allocate count in this subtree, update bighint.
341 scan->bm_bighint = count - 1;
342 return(ALIST_BLOCK_NONE);
346 * alist_meta_alloc() - allocate at a meta in the radix tree.
348 * Attempt to allocate at a meta node. If we can't, we update
349 * bighint and return a failure. Updating bighint optimize future
350 * calls that hit this node. We have to check for our collapse cases
351 * and we have a few optimizations strewn in as well.
365 int next_skip = ((u_int)skip / ALIST_META_RADIX);
368 * ALL-ALLOCATED special case
370 if (scan->bm_bitmap == 0) {
371 scan->bm_bighint = 0;
372 return(ALIST_BLOCK_NONE);
375 radix /= ALIST_META_RADIX;
378 * Radix now represents each bitmap entry for this meta node. If
379 * the number of blocks being allocated can be fully represented,
380 * we allocate directly out of this meta node.
382 * Meta node bitmaps use 2 bits per block.
385 * 01 PARTIALLY-FREE/PARTIALLY-ALLOCATED
389 if (count >= radix) {
390 int n = count / radix * 2; /* number of bits */
393 mask = (u_daddr_t)-1 >> (ALIST_BMAP_RADIX - n);
394 for (j = 0; j < ALIST_META_RADIX; j += n / 2) {
395 if ((scan->bm_bitmap & mask) == mask) {
396 scan->bm_bitmap &= ~mask;
397 return(blk + j * radix);
401 if (scan->bm_bighint >= count)
402 scan->bm_bighint = count >> 1;
403 return(ALIST_BLOCK_NONE);
407 * If not we have to recurse.
411 for (i = 1; i <= skip; i += next_skip) {
412 if (scan[i].bm_bighint == (daddr_t)-1) {
418 if ((scan->bm_bitmap & mask) == mask) {
419 scan[i].bm_bitmap = (u_daddr_t)-1;
420 scan[i].bm_bighint = radix;
423 if (count <= scan[i].bm_bighint) {
425 * count fits in object
428 if (next_skip == 1) {
429 r = alst_leaf_alloc(&scan[i], blk, count);
431 r = alst_meta_alloc(&scan[i], blk, count, radix, next_skip - 1);
433 if (r != ALIST_BLOCK_NONE) {
434 if (scan[i].bm_bitmap == 0) {
435 scan->bm_bitmap &= ~mask;
437 scan->bm_bitmap &= ~mask;
438 scan->bm_bitmap |= pmask;
442 } else if (count > radix) {
444 * count does not fit in object even if it were
455 * We couldn't allocate count in this subtree, update bighint.
457 if (scan->bm_bighint >= count)
458 scan->bm_bighint = count >> 1;
459 return(ALIST_BLOCK_NONE);
463 * BLST_LEAF_FREE() - free allocated block from leaf bitmap
473 * free some data in this bitmap
476 * 0000111111111110000
480 int n = blk & (ALIST_BMAP_RADIX - 1);
483 mask = ((u_daddr_t)-1 << n) &
484 ((u_daddr_t)-1 >> (ALIST_BMAP_RADIX - count - n));
486 if (scan->bm_bitmap & mask)
487 panic("alst_radix_free: freeing free block");
488 scan->bm_bitmap |= mask;
491 * We could probably do a better job here. We are required to make
492 * bighint at least as large as the biggest contiguous block of
493 * data. If we just shoehorn it, a little extra overhead will
494 * be incured on the next allocation (but only that one typically).
496 scan->bm_bighint = ALIST_BMAP_RADIX;
500 * BLST_META_FREE() - free allocated blocks from radix tree meta info
502 * This support routine frees a range of blocks from the bitmap.
503 * The range must be entirely enclosed by this radix node. If a
504 * meta node, we break the range down recursively to free blocks
505 * in subnodes (which means that this code can free an arbitrary
506 * range whereas the allocation code cannot allocate an arbitrary
519 int next_skip = ((u_int)skip / ALIST_META_RADIX);
525 * Break the free down into its components. Because it is so easy
526 * to implement, frees are not limited to power-of-2 sizes.
528 * Each block in a meta-node bitmap takes two bits.
530 radix /= ALIST_META_RADIX;
532 i = (freeBlk - blk) / radix;
534 mask = 0x00000003 << (i * 2);
535 pmask = 0x00000001 << (i * 2);
537 i = i * next_skip + 1;
539 while (i <= skip && blk < freeBlk + count) {
542 v = blk + radix - freeBlk;
546 if (scan->bm_bighint == (daddr_t)-1)
547 panic("alst_meta_free: freeing unexpected range");
549 if (freeBlk == blk && count >= radix) {
551 * All-free case, no need to update sub-tree
553 scan->bm_bitmap |= mask;
554 scan->bm_bighint = radix * ALIST_META_RADIX;/*XXX*/
560 alst_leaf_free(&scan[i], freeBlk, v);
562 alst_meta_free(&scan[i], freeBlk, v, radix, next_skip - 1, blk);
563 if (scan[i].bm_bitmap == (u_daddr_t)-1)
564 scan->bm_bitmap |= mask;
566 scan->bm_bitmap |= pmask;
567 if (scan->bm_bighint < scan[i].bm_bighint)
568 scan->bm_bighint = scan[i].bm_bighint;
580 * BLST_RADIX_INIT() - initialize radix tree
582 * Initialize our meta structures and bitmaps and calculate the exact
583 * amount of space required to manage 'count' blocks - this space may
584 * be considerably less then the calculated radix due to the large
585 * RADIX values we use.
589 alst_radix_init(almeta_t *scan, daddr_t radix, int skip, daddr_t count)
593 daddr_t memindex = 0;
600 if (radix == ALIST_BMAP_RADIX) {
602 scan->bm_bighint = 0;
609 * Meta node. If allocating the entire object we can special
610 * case it. However, we need to figure out how much memory
611 * is required to manage 'count' blocks, so we continue on anyway.
615 scan->bm_bighint = 0;
619 radix /= ALIST_META_RADIX;
620 next_skip = ((u_int)skip / ALIST_META_RADIX);
624 for (i = 1; i <= skip; i += next_skip) {
625 if (count >= radix) {
627 * Allocate the entire object
629 memindex = i + alst_radix_init(
630 ((scan) ? &scan[i] : NULL),
636 /* already marked as wholely allocated */
637 } else if (count > 0) {
639 * Allocate a partial object
641 memindex = i + alst_radix_init(
642 ((scan) ? &scan[i] : NULL),
650 * Mark as partially allocated
653 scan->bm_bitmap |= pmask;
656 * Add terminator and break out
659 scan[i].bm_bighint = (daddr_t)-1;
660 /* already marked as wholely allocated */
674 alst_radix_print(almeta_t *scan, daddr_t blk, daddr_t radix, int skip, int tab)
681 if (radix == ALIST_BMAP_RADIX) {
683 "%*.*s(%04x,%d): bitmap %08x big=%d\n",
692 if (scan->bm_bitmap == 0) {
694 "%*.*s(%04x,%d) ALL ALLOCATED\n",
701 if (scan->bm_bitmap == (u_daddr_t)-1) {
703 "%*.*s(%04x,%d) ALL FREE\n",
712 "%*.*s(%04x,%d): subtree (%d) bitmap=%08x big=%d {\n",
720 radix /= ALIST_META_RADIX;
721 next_skip = ((u_int)skip / ALIST_META_RADIX);
725 for (i = 1; i <= skip; i += next_skip) {
726 if (scan[i].bm_bighint == (daddr_t)-1) {
728 "%*.*s(%04x,%d): Terminator\n",
735 if ((scan->bm_bitmap & mask) == mask) {
737 "%*.*s(%04x,%d): ALL FREE\n",
741 } else if ((scan->bm_bitmap & mask) == 0) {
743 "%*.*s(%04x,%d): ALL ALLOCATED\n",
772 main(int ac, char **av)
778 for (i = 1; i < ac; ++i) {
779 const char *ptr = av[i];
781 size = strtol(ptr, NULL, 0);
785 fprintf(stderr, "Bad option: %s\n", ptr - 2);
788 bl = alist_create(size, NULL);
789 alist_free(bl, 0, size);
797 kprintf("%d/%d/%d> ", bl->bl_free, size, bl->bl_radix);
799 if (fgets(buf, sizeof(buf), stdin) == NULL)
806 if (sscanf(buf + 1, "%d", &count) == 1) {
807 daddr_t blk = alist_alloc(bl, count);
808 kprintf(" R=%04x\n", blk);
814 if (sscanf(buf + 1, "%x %d", &da, &count) == 2) {
815 alist_free(bl, da, count);
838 panic(const char *ctl, ...)
843 vfprintf(stderr, ctl, va);
844 fprintf(stderr, "\n");