iPXE
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., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <errno.h>
00027 #include <ipxe/bitmap.h>
00028 
00029 /** @file
00030  *
00031  * Bitmaps for multicast downloads
00032  *
00033  */
00034 
00035 /**
00036  * Resize bitmap
00037  *
00038  * @v bitmap            Bitmap
00039  * @v new_length        New length of bitmap, in bits
00040  * @ret rc              Return status code
00041  */
00042 int bitmap_resize ( struct bitmap *bitmap, unsigned int new_length ) {
00043         unsigned int old_num_blocks;
00044         unsigned int new_num_blocks;
00045         size_t new_size;
00046         bitmap_block_t *new_blocks;
00047 
00048         old_num_blocks = BITMAP_INDEX ( bitmap->length + BITMAP_BLKSIZE - 1 );
00049         new_num_blocks = BITMAP_INDEX ( new_length + BITMAP_BLKSIZE - 1 );
00050 
00051         if ( old_num_blocks != new_num_blocks ) {
00052                 new_size = ( new_num_blocks * sizeof ( bitmap->blocks[0] ) );
00053                 new_blocks = realloc ( bitmap->blocks, new_size );
00054                 if ( ! new_blocks ) {
00055                         DBGC ( bitmap, "Bitmap %p could not resize to %d "
00056                                "bits\n", bitmap, new_length );
00057                         return -ENOMEM;
00058                 }
00059                 bitmap->blocks = new_blocks;
00060         }
00061         bitmap->length = new_length;
00062 
00063         while ( old_num_blocks < new_num_blocks ) {
00064                 bitmap->blocks[old_num_blocks++] = 0;
00065         }
00066 
00067         DBGC ( bitmap, "Bitmap %p resized to %d bits\n", bitmap, new_length );
00068         return 0;
00069 }
00070 
00071 /**
00072  * Test bit in bitmap
00073  *
00074  * @v bitmap            Bitmap
00075  * @v bit               Bit index
00076  * @ret is_set          Bit is set
00077  */
00078 int bitmap_test ( struct bitmap *bitmap, unsigned int bit ) {
00079         unsigned int index = BITMAP_INDEX ( bit );
00080         bitmap_block_t mask = BITMAP_MASK ( bit );
00081 
00082         if ( bit >= bitmap->length )
00083                 return 0;
00084         return ( ( bitmap->blocks[index] & mask ) != 0 );
00085 }
00086 
00087 /**
00088  * Set bit in bitmap
00089  *
00090  * @v bitmap            Bitmap
00091  * @v bit               Bit index
00092  */
00093 void bitmap_set ( struct bitmap *bitmap, unsigned int bit ) {
00094         unsigned int index = BITMAP_INDEX ( bit );
00095         bitmap_block_t mask = BITMAP_MASK ( bit );
00096 
00097         DBGC ( bitmap, "Bitmap %p setting bit %d\n", bitmap, bit );
00098 
00099         /* Update bitmap */
00100         bitmap->blocks[index] |= mask;
00101 
00102         /* Update first gap counter */
00103         while ( bitmap_test ( bitmap, bitmap->first_gap ) ) {
00104                 bitmap->first_gap++;
00105         }
00106 }