| 1 | /* $OpenBSD: blist.h,v 1.2 2022/08/06 13:44:04 semarie Exp $ */ |
| 2 | /* DragonFlyBSD:7b80531f545c7d3c51c1660130c71d01f6bccbe0:/sys/sys/blist.h */ |
| 3 | /* |
| 4 | * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved. |
| 5 | * |
| 6 | * This code is derived from software contributed to The DragonFly Project |
| 7 | * by Matthew Dillon <dillon@backplane.com> |
| 8 | * |
| 9 | * Redistribution and use in source and binary forms, with or without |
| 10 | * modification, are permitted provided that the following conditions |
| 11 | * are met: |
| 12 | * |
| 13 | * 1. Redistributions of source code must retain the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer. |
| 15 | * 2. Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in |
| 17 | * the documentation and/or other materials provided with the |
| 18 | * distribution. |
| 19 | * 3. Neither the name of The DragonFly Project nor the names of its |
| 20 | * contributors may be used to endorse or promote products derived |
| 21 | * from this software without specific, prior written permission. |
| 22 | * |
| 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 24 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 26 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 27 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 28 | * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 29 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 30 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 31 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 32 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 33 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 34 | * SUCH DAMAGE. |
| 35 | * |
| 36 | * Implements bitmap resource lists. |
| 37 | * |
| 38 | *	Usage: |
| 39 | *		blist = blist_create(blocks) |
| 40 | *		(void) blist_destroy(blist) |
| 41 | *		blkno = blist_alloc(blist, count) |
| 42 | *		(void) blist_free(blist, blkno, count) |
| 43 | *		nblks = blist_fill(blist, blkno, count) |
| 44 | *		(void) blist_resize(&blist, count, freeextra) |
| 45 | * |
| 46 | * |
| 47 | *	Notes: |
| 48 | *		on creation, the entire list is marked reserved. You should |
| 49 | *		first blist_free() the sections you want to make available |
| 50 | *		for allocation before doing general blist_alloc()/free() |
| 51 | *		ops. |
| 52 | * |
| 53 | *		SWAPBLK_NONE is returned on failure. This module is typically |
| 54 | *		capable of managing up to (2^31) blocks per blist, though |
| 55 | *		the memory utilization would be insane if you actually did |
| 56 | *		that. Managing something like 512MB worth of 4K blocks |
| 57 | *		eats around 32 KBytes of memory. |
| 58 | * |
| 59 | * $FreeBSD: src/sys/sys/blist.h,v 1.2.2.1 2003/01/12 09:23:12 dillon Exp $ |
| 60 | */ |
| 61 | |
| 62 | #ifndef _SYS_BLIST_H_ |
| 63 | #define _SYS_BLIST_H_ |
| 64 | |
| 65 | #ifndef _SYS_TYPES_H_ |
| 66 | #include <sys/types.h> |
| 67 | #endif |
| 68 | |
| 69 | typedef u_long		swblk_t; |
| 70 | typedef u_int64_t	u_swblk_t; |
| 71 | |
| 72 | /* |
| 73 | * note: currently use SWAPBLK_NONE as an absolute value rather then |
| 74 | * a flag bit. |
| 75 | */ |
| 76 | #define SWAPBLK_NONE	((swblk_t)-1) |
| 77 | |
| 78 | /* |
| 79 | * blmeta and bl_bitmap_t MUST be a power of 2 in size. |
| 80 | */ |
| 81 | |
| 82 | typedef struct blmeta { |
| 83 | 	union { |
| 84 | 	 swblk_t	bmu_avail;	/* space available under us	*/ |
| 85 | 	 u_swblk_t	bmu_bitmap;	/* bitmap if we are a leaf	*/ |
| 86 | 	} u; |
| 87 | 	swblk_t		bm_bighint;	/* biggest contiguous block hint*/ |
| 88 | } blmeta_t; |
| 89 | |
| 90 | typedef struct blist { |
| 91 | 	swblk_t		bl_blocks;	/* area of coverage		*/ |
| 92 | 	/* XXX int64_t bl_radix */ |
| 93 | 	swblk_t		bl_radix;	/* coverage radix		*/ |
| 94 | 	swblk_t		bl_skip;	/* starting skip		*/ |
| 95 | 	swblk_t		bl_free;	/* number of free blocks	*/ |
| 96 | 	blmeta_t	*bl_root;	/* root of radix tree		*/ |
| 97 | 	swblk_t		bl_rootblks;	/* swblk_t blks allocated for tree */ |
| 98 | } *blist_t; |
| 99 | |
| 100 | #define BLIST_META_RADIX	(sizeof(u_swblk_t)*8/2)	/* 2 bits per */ |
| 101 | #define BLIST_BMAP_RADIX	(sizeof(u_swblk_t)*8)	/* 1 bit per */ |
| 102 | |
| 103 | /* |
| 104 | * The radix may exceed the size of a 64 bit signed (or unsigned) int |
| 105 | * when the maximal number of blocks is allocated. With a 32-bit swblk_t |
| 106 | * this corresponds to ~1G x PAGE_SIZE = 4096GB. The swap code usually |
| 107 | * divides this by 4, leaving us with a capability of up to four 1TB swap |
| 108 | * devices. |
| 109 | * |
| 110 | * With a 64-bit swblk_t the limitation is some insane number. |
| 111 | * |
| 112 | * NOTE: For now I don't trust that we overflow-detect properly so we divide |
| 113 | *	 out to ensure that no overflow occurs. |
| 114 | */ |
| 115 | |
| 116 | #if defined(_LP64) |
| 117 | /* swblk_t 64bits */ |
| 118 | #define BLIST_MAXBLKS		(0x4000000000000000LL /		\ |
| 119 | 				 (BLIST_BMAP_RADIX / BLIST_META_RADIX)) |
| 120 | #else |
| 121 | #define BLIST_MAXBLKS		(0x40000000 /		\ |
| 122 | 				 (BLIST_BMAP_RADIX / BLIST_META_RADIX)) |
| 123 | #endif |
| 124 | |
| 125 | #define BLIST_MAX_ALLOC		BLIST_BMAP_RADIX |
| 126 | |
| 127 | blist_t blist_create(swblk_t); |
| 128 | void blist_destroy(blist_t); |
| 129 | swblk_t blist_alloc(blist_t, swblk_t); |
| 130 | swblk_t blist_allocat(blist_t, swblk_t, swblk_t); |
| 131 | void blist_free(blist_t, swblk_t, swblk_t); |
| 132 | swblk_t blist_fill(blist_t, swblk_t, swblk_t); |
| 133 | void blist_print(blist_t); |
| 134 | void blist_resize(blist_t *, swblk_t, int); |
| 135 | void blist_gapfind(blist_t, swblk_t *, swblk_t *); |
| 136 | |
| 137 | #endif	/* _SYS_BLIST_H_ */ |