[sundance] Add reset completion check
[gpxe.git] / src / core / bitmap.c
blobd02664719cea8ab732e36c1353874d2d23b72d2b
1 /*
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., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include <errno.h>
20 #include <gpxe/bitmap.h>
22 /** @file
24 * Bitmaps for multicast downloads
28 /**
29 * Resize bitmap
31 * @v bitmap Bitmap
32 * @v new_length New length of bitmap, in bits
33 * @ret rc Return status code
35 int bitmap_resize ( struct bitmap *bitmap, unsigned int new_length ) {
36 unsigned int old_num_blocks;
37 unsigned int new_num_blocks;
38 size_t new_size;
39 bitmap_block_t *new_blocks;
41 old_num_blocks = BITMAP_INDEX ( bitmap->length + BITMAP_BLKSIZE - 1 );
42 new_num_blocks = BITMAP_INDEX ( new_length + BITMAP_BLKSIZE - 1 );
44 if ( old_num_blocks != new_num_blocks ) {
45 new_size = ( new_num_blocks * sizeof ( bitmap->blocks[0] ) );
46 new_blocks = realloc ( bitmap->blocks, new_size );
47 if ( ! new_blocks ) {
48 DBGC ( bitmap, "Bitmap %p could not resize to %d "
49 "bits\n", bitmap, new_length );
50 return -ENOMEM;
52 bitmap->blocks = new_blocks;
54 bitmap->length = new_length;
56 while ( old_num_blocks < new_num_blocks ) {
57 bitmap->blocks[old_num_blocks++] = 0;
60 DBGC ( bitmap, "Bitmap %p resized to %d bits\n", bitmap, new_length );
61 return 0;
64 /**
65 * Test bit in bitmap
67 * @v bitmap Bitmap
68 * @v bit Bit index
69 * @ret is_set Bit is set
71 int bitmap_test ( struct bitmap *bitmap, unsigned int bit ) {
72 unsigned int index = BITMAP_INDEX ( bit );
73 bitmap_block_t mask = BITMAP_MASK ( bit );
75 if ( bit >= bitmap->length )
76 return 0;
77 return ( bitmap->blocks[index] & mask );
80 /**
81 * Set bit in bitmap
83 * @v bitmap Bitmap
84 * @v bit Bit index
86 void bitmap_set ( struct bitmap *bitmap, unsigned int bit ) {
87 unsigned int index = BITMAP_INDEX ( bit );
88 bitmap_block_t mask = BITMAP_MASK ( bit );
90 DBGC ( bitmap, "Bitmap %p setting bit %d\n", bitmap, bit );
92 /* Update bitmap */
93 bitmap->blocks[index] |= mask;
95 /* Update first gap counter */
96 while ( bitmap_test ( bitmap, bitmap->first_gap ) ) {
97 bitmap->first_gap++;