FreeBSD/sys/kern/subr_blist.c
/*
 * blist_leaf_alloc() -	allocate at a leaf in the radix tree (a bitmap).
 *
 *	This is the core of the allocator and is optimized for the 1 block
 *	and the BLIST_BMAP_RADIX block allocation cases.  Other cases are
 *	somewhat slower.  The 1 block allocation case is log2 and extremely
 *	quick.
 */
allocate at a leaf in the radix tree (a bitmap).
This is the core of the allocator and is optimized for the 1 block
and the BLIST_BMAP_RADIX block allocation cases.  Other cases are
somewhat slower.  The 1 block allocation case is log2 and extremely
quick.
allocate at a leaf in the radix tree ( a bitmap ) . This is the core of the allocator and is optimized for the 1 block and the BLIST_BMAP_RADIX block allocation cases . Other cases are somewhat slower . The 1 block allocation case is log2 and extremely quick .
static daddr_t
blst_leaf_alloc(
	blmeta_t *scan,
	daddr_t blk,
	int count
) {
	u_daddr_t orig = scan->u.bmu_bitmap;
	if (orig == 0) {
		scan->bm_bighint = 0;
		return(SWAPBLK_NONE);
	}
	if (count == 1) {
		u_daddr_t mask;
		int j = BLIST_BMAP_RADIX/2;
		int r = 0;
		mask = (u_daddr_t)-1 >> (BLIST_BMAP_RADIX/2);
		while (j) {
			if ((orig & mask) == 0) {
			    r += j;
			    orig >>= j;
			}
			j >>= 1;
			mask >>= j;
		}
		scan->u.bmu_bitmap &= ~(1 << r);
		return(blk + r);
	}
	if (count <= BLIST_BMAP_RADIX) {
		int j;
		int n = BLIST_BMAP_RADIX - count;
		u_daddr_t mask;
		mask = (u_daddr_t)-1 >> n;
		for (j = 0; j <= n; ++j) {
			if ((orig & mask) == mask) {
				scan->u.bmu_bitmap &= ~mask;
				return(blk + j);
			}
			mask = (mask << 1);
		}
	}
	scan->bm_bighint = count - 1;
	return(SWAPBLK_NONE);
}
static daddr_t blst_leaf_alloc ( blmeta_t * scan , daddr_t blk , int count ) { u_daddr_t orig = scan -> u . bmu_bitmap ; if ( orig == 0 ) { scan -> bm_bighint = 0 ; return ( SWAPBLK_NONE ) ; } if ( count == 1 ) { u_daddr_t mask ; int j = BLIST_BMAP_RADIX / 2 ; int r = 0 ; mask = ( u_daddr_t ) - 1 >> ( BLIST_BMAP_RADIX / 2 ) ; while ( j ) { if ( ( orig & mask ) == 0 ) { r += j ; orig >>= j ; } j >>= 1 ; mask >>= j ; } scan -> u . bmu_bitmap &= ~ ( 1 << r ) ; return ( blk + r ) ; } if ( count <= BLIST_BMAP_RADIX ) { int j ; int n = BLIST_BMAP_RADIX - count ; u_daddr_t mask ; mask = ( u_daddr_t ) - 1 >> n ; for ( j = 0 ; j <= n ; ++ j ) { if ( ( orig & mask ) == mask ) { scan -> u . bmu_bitmap &= ~ mask ; return ( blk + j ) ; } mask = ( mask << 1 ) ; } } scan -> bm_bighint = count - 1 ; return ( SWAPBLK_NONE ) ; }
blist_leaf_alloc() - allocate at a leaf in the radix tree (a bitmap).
blist_leaf_alloc () - allocate at a leaf in the radix tree ( a bitmap ) .
/*
		 * Optimize bitmap all-allocated case.  Also, count = 1
		 * case assumes at least 1 bit is free in the bitmap, so
		 * we have to take care of this case here.
		 */ /*
		 * Optimized code to allocate one bit out of the bitmap
		 */ /*
		 * non-optimized code to allocate N bits out of the bitmap.
		 * The more bits, the faster the code runs.  It will run
		 * the slowest allocating 2 bits, but since there aren't any
		 * memory ops in the core loop (or shouldn't be, anyway),
		 * you probably won't notice the difference.
		 */ /*
	 * We couldn't allocate count in this subtree, update bighint.
	 */