bitmap.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 FILE_LICENCE ( GPL2_OR_LATER );
00020
00021 #include <errno.h>
00022 #include <gpxe/bitmap.h>
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
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
00068
00069
00070
00071
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
00084
00085
00086
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
00095 bitmap->blocks[index] |= mask;
00096
00097
00098 while ( bitmap_test ( bitmap, bitmap->first_gap ) ) {
00099 bitmap->first_gap++;
00100 }
00101 }