2 * Copyright (c) 2007,2011 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Implements a power-of-2 aligned and sized resource bitmap. The
36 * number of blocks need not be a power-of-2, but any allocation which is
37 * not a power of 2 will first allocate the nearest higher power of 2
38 * and then free the remainder. Frees have no alignment requirements
39 * and piecemeal frees are allowed.
41 * alist = alist_create(blocks, malloctype)
42 * (void) alist_init(alist, blocks, records, nrecords)
43 * (void) alist_destroy(alist, malloctype)
44 * blkno = alist_alloc(alist, start, count)
45 * (void) alist_free(alist, blkno, count)
47 * A radix tree is constructed using radix 16 for the meta nodes and radix 32
48 * for the leaf nodes. Each meta node has a 32 bit bitmap using 2 bits
49 * per radix (32 bits), and each leaf node has a 32 bit bitmap using 1 bit
50 * per radix. The 2-bit bit patterns in the meta-node indicate how the
51 * sub-tree is allocated.
54 * 01 Partially allocated
58 * Each meta-node and leaf has a biggest-hint field indicating the largest
59 * possible allocation that can be made in that sub-tree. The value in
60 * this field may be too large but will never be too small.
67 #include <sys/types.h>
70 typedef u_int32_t alist_bmap_t
;
71 typedef u_int32_t alist_blk_t
;
74 * almeta and alist_bmap_t MUST be a power of 2 in size.
76 typedef struct almeta
{
77 alist_bmap_t bm_bitmap
; /* bitmap if we are a leaf */
78 alist_blk_t bm_bighint
; /* biggest contiguous block hint*/
81 typedef struct alist
{
82 alist_blk_t bl_blocks
; /* area of coverage */
83 alist_blk_t bl_radix
; /* coverage radix */
84 alist_blk_t bl_skip
; /* starting skip */
85 alist_blk_t bl_free
; /* number of free blocks */
86 almeta_t
*bl_root
; /* root of radix tree */
87 alist_blk_t bl_rootblks
; /* #blocks handled by tree */
90 #define ALIST_META_RADIX (sizeof(alist_bmap_t)*4) /* 16 */
91 #define ALIST_BMAP_RADIX (sizeof(alist_bmap_t)*8) /* 32 */
92 #define ALIST_BLOCK_NONE ((alist_blk_t)-1)
95 * When alist_init() is used the caller can pre-allocate the records
98 #define ALIST_RECORDS_65536 2193
99 #define ALIST_RECORDS_1048576 34961
101 extern alist_t
alist_create(alist_blk_t blocks
, struct malloc_type
*mtype
);
102 extern void alist_init(alist_t alist
, alist_blk_t blocks
,
103 almeta_t
*records
, alist_blk_t nrecords
);
104 extern void alist_destroy(alist_t alist
, struct malloc_type
*mtype
);
105 extern alist_blk_t
alist_alloc(alist_t alist
, alist_blk_t start
,
107 extern void alist_free(alist_t alist
, alist_blk_t blkno
, alist_blk_t count
);
108 extern alist_blk_t
alist_free_info(alist_t bl
, alist_blk_t
*startp
,
109 alist_blk_t
*countp
);
110 extern void alist_print(alist_t alist
);
112 #endif /* _SYS_ALIST_H_ */