2 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
27 #include <ipxe/bitmap.h>
31 * Bitmaps for multicast downloads
39 * @v new_length New length of bitmap, in bits
40 * @ret rc Return status code
42 int bitmap_resize ( struct bitmap *bitmap, unsigned int new_length ) {
43 unsigned int old_num_blocks;
44 unsigned int new_num_blocks;
46 bitmap_block_t *new_blocks;
48 old_num_blocks = BITMAP_INDEX ( bitmap->length + BITMAP_BLKSIZE - 1 );
49 new_num_blocks = BITMAP_INDEX ( new_length + BITMAP_BLKSIZE - 1 );
51 if ( old_num_blocks != new_num_blocks ) {
52 new_size = ( new_num_blocks * sizeof ( bitmap->blocks[0] ) );
53 new_blocks = realloc ( bitmap->blocks, new_size );
55 DBGC ( bitmap, "Bitmap %p could not resize to %d "
56 "bits\n", bitmap, new_length );
59 bitmap->blocks = new_blocks;
61 bitmap->length = new_length;
63 while ( old_num_blocks < new_num_blocks ) {
64 bitmap->blocks[old_num_blocks++] = 0;
67 DBGC ( bitmap, "Bitmap %p resized to %d bits\n", bitmap, new_length );
76 * @ret is_set Bit is set
78 int bitmap_test ( struct bitmap *bitmap, unsigned int bit ) {
79 unsigned int index = BITMAP_INDEX ( bit );
80 bitmap_block_t mask = BITMAP_MASK ( bit );
82 if ( bit >= bitmap->length )
84 return ( ( bitmap->blocks[index] & mask ) != 0 );
93 void bitmap_set ( struct bitmap *bitmap, unsigned int bit ) {
94 unsigned int index = BITMAP_INDEX ( bit );
95 bitmap_block_t mask = BITMAP_MASK ( bit );
97 DBGC ( bitmap, "Bitmap %p setting bit %d\n", bitmap, bit );
100 bitmap->blocks[index] |= mask;
102 /* Update first gap counter */
103 while ( bitmap_test ( bitmap, bitmap->first_gap ) ) {