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