Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / core / bitmap.c
1 /*
2  * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <errno.h>
23 #include <ipxe/bitmap.h>
24
25 /** @file
26  *
27  * Bitmaps for multicast downloads
28  *
29  */
30
31 /**
32  * Resize bitmap
33  *
34  * @v bitmap            Bitmap
35  * @v new_length        New length of bitmap, in bits
36  * @ret rc              Return status code
37  */
38 int bitmap_resize ( struct bitmap *bitmap, unsigned int new_length ) {
39         unsigned int old_num_blocks;
40         unsigned int new_num_blocks;
41         size_t new_size;
42         bitmap_block_t *new_blocks;
43
44         old_num_blocks = BITMAP_INDEX ( bitmap->length + BITMAP_BLKSIZE - 1 );
45         new_num_blocks = BITMAP_INDEX ( new_length + BITMAP_BLKSIZE - 1 );
46
47         if ( old_num_blocks != new_num_blocks ) {
48                 new_size = ( new_num_blocks * sizeof ( bitmap->blocks[0] ) );
49                 new_blocks = realloc ( bitmap->blocks, new_size );
50                 if ( ! new_blocks ) {
51                         DBGC ( bitmap, "Bitmap %p could not resize to %d "
52                                "bits\n", bitmap, new_length );
53                         return -ENOMEM;
54                 }
55                 bitmap->blocks = new_blocks;
56         }
57         bitmap->length = new_length;
58
59         while ( old_num_blocks < new_num_blocks ) {
60                 bitmap->blocks[old_num_blocks++] = 0;
61         }
62
63         DBGC ( bitmap, "Bitmap %p resized to %d bits\n", bitmap, new_length );
64         return 0;
65 }
66
67 /**
68  * Test bit in bitmap
69  *
70  * @v bitmap            Bitmap
71  * @v bit               Bit index
72  * @ret is_set          Bit is set
73  */
74 int bitmap_test ( struct bitmap *bitmap, unsigned int bit ) {
75         unsigned int index = BITMAP_INDEX ( bit );
76         bitmap_block_t mask = BITMAP_MASK ( bit );
77
78         if ( bit >= bitmap->length )
79                 return 0;
80         return ( ( bitmap->blocks[index] & mask ) != 0 );
81 }
82
83 /**
84  * Set bit in bitmap
85  *
86  * @v bitmap            Bitmap
87  * @v bit               Bit index
88  */
89 void bitmap_set ( struct bitmap *bitmap, unsigned int bit ) {
90         unsigned int index = BITMAP_INDEX ( bit );
91         bitmap_block_t mask = BITMAP_MASK ( bit );
92
93         DBGC ( bitmap, "Bitmap %p setting bit %d\n", bitmap, bit );
94
95         /* Update bitmap */
96         bitmap->blocks[index] |= mask;
97
98         /* Update first gap counter */
99         while ( bitmap_test ( bitmap, bitmap->first_gap ) ) {
100                 bitmap->first_gap++;
101         }
102 }