bitmap.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 FILE_LICENCE ( GPL2_OR_LATER );
00020 
00021 #include <errno.h>
00022 #include <gpxe/bitmap.h>
00023 
00024 /** @file
00025  *
00026  * Bitmaps for multicast downloads
00027  *
00028  */
00029 
00030 /**
00031  * Resize bitmap
00032  *
00033  * @v bitmap            Bitmap
00034  * @v new_length        New length of bitmap, in bits
00035  * @ret rc              Return status code
00036  */
00037 int bitmap_resize ( struct bitmap *bitmap, unsigned int new_length ) {
00038         unsigned int old_num_blocks;
00039         unsigned int new_num_blocks;
00040         size_t new_size;
00041         bitmap_block_t *new_blocks;
00042 
00043         old_num_blocks = BITMAP_INDEX ( bitmap->length + BITMAP_BLKSIZE - 1 );
00044         new_num_blocks = BITMAP_INDEX ( new_length + BITMAP_BLKSIZE - 1 );
00045 
00046         if ( old_num_blocks != new_num_blocks ) {
00047                 new_size = ( new_num_blocks * sizeof ( bitmap->blocks[0] ) );
00048                 new_blocks = realloc ( bitmap->blocks, new_size );
00049                 if ( ! new_blocks ) {
00050                         DBGC ( bitmap, "Bitmap %p could not resize to %d "
00051                                "bits\n", bitmap, new_length );
00052                         return -ENOMEM;
00053                 }
00054                 bitmap->blocks = new_blocks;
00055         }
00056         bitmap->length = new_length;
00057 
00058         while ( old_num_blocks < new_num_blocks ) {
00059                 bitmap->blocks[old_num_blocks++] = 0;
00060         }
00061 
00062         DBGC ( bitmap, "Bitmap %p resized to %d bits\n", bitmap, new_length );
00063         return 0;
00064 }
00065 
00066 /**
00067  * Test bit in bitmap
00068  *
00069  * @v bitmap            Bitmap
00070  * @v bit               Bit index
00071  * @ret is_set          Bit is set
00072  */
00073 int bitmap_test ( struct bitmap *bitmap, unsigned int bit ) {
00074         unsigned int index = BITMAP_INDEX ( bit );
00075         bitmap_block_t mask = BITMAP_MASK ( bit );
00076 
00077         if ( bit >= bitmap->length )
00078                 return 0;
00079         return ( bitmap->blocks[index] & mask );
00080 }
00081 
00082 /**
00083  * Set bit in bitmap
00084  *
00085  * @v bitmap            Bitmap
00086  * @v bit               Bit index
00087  */
00088 void bitmap_set ( struct bitmap *bitmap, unsigned int bit ) {
00089         unsigned int index = BITMAP_INDEX ( bit );
00090         bitmap_block_t mask = BITMAP_MASK ( bit );
00091 
00092         DBGC ( bitmap, "Bitmap %p setting bit %d\n", bitmap, bit );
00093 
00094         /* Update bitmap */
00095         bitmap->blocks[index] |= mask;
00096 
00097         /* Update first gap counter */
00098         while ( bitmap_test ( bitmap, bitmap->first_gap ) ) {
00099                 bitmap->first_gap++;
00100         }
00101 }

Generated on Tue Apr 6 20:00:51 2010 for gPXE by  doxygen 1.5.7.1