iPXE
bitmap.c
Go to the documentation of this file.
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 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25FILE_SECBOOT ( PERMITTED );
26
27#include <errno.h>
28#include <ipxe/bitmap.h>
29
30/** @file
31 *
32 * Bitmaps for multicast downloads
33 *
34 */
35
36/**
37 * Resize bitmap
38 *
39 * @v bitmap Bitmap
40 * @v new_length New length of bitmap, in bits
41 * @ret rc Return status code
42 */
43int bitmap_resize ( struct bitmap *bitmap, unsigned int new_length ) {
44 unsigned int old_num_blocks;
45 unsigned int new_num_blocks;
46 size_t new_size;
47 bitmap_block_t *new_blocks;
48
49 old_num_blocks = BITMAP_INDEX ( bitmap->length + BITMAP_BLKSIZE - 1 );
50 new_num_blocks = BITMAP_INDEX ( new_length + BITMAP_BLKSIZE - 1 );
51
52 if ( old_num_blocks != new_num_blocks ) {
53 new_size = ( new_num_blocks * sizeof ( bitmap->blocks[0] ) );
54 new_blocks = realloc ( bitmap->blocks, new_size );
55 if ( ! new_blocks ) {
56 DBGC ( bitmap, "Bitmap %p could not resize to %d "
57 "bits\n", bitmap, new_length );
58 return -ENOMEM;
59 }
60 bitmap->blocks = new_blocks;
61 }
62 bitmap->length = new_length;
63
64 while ( old_num_blocks < new_num_blocks ) {
65 bitmap->blocks[old_num_blocks++] = 0;
66 }
67
68 DBGC ( bitmap, "Bitmap %p resized to %d bits\n", bitmap, new_length );
69 return 0;
70}
71
72/**
73 * Test bit in bitmap
74 *
75 * @v bitmap Bitmap
76 * @v bit Bit index
77 * @ret is_set Bit is set
78 */
79int bitmap_test ( struct bitmap *bitmap, unsigned int bit ) {
80 unsigned int index = BITMAP_INDEX ( bit );
82
83 /* Treat out-of-range bits as implicitly being zero */
84 if ( bit >= bitmap->length )
85 return 0;
86
87 return ( ( bitmap->blocks[index] & mask ) != 0 );
88}
89
90/**
91 * Set bit in bitmap
92 *
93 * @v bitmap Bitmap
94 * @v bit Bit index
95 * @ret rc Return status code
96 */
97int bitmap_set ( struct bitmap *bitmap, unsigned int bit ) {
98 unsigned int index = BITMAP_INDEX ( bit );
100
101 DBGC ( bitmap, "Bitmap %p setting bit %d\n", bitmap, bit );
102
103 /* Fail if we cannot set this bit */
104 if ( bit >= bitmap->length ) {
105 DBGC ( bitmap, "Bitmap %p bit %d is outside range [0,%d)\n",
106 bitmap, bit, bitmap->length );
107 return -ERANGE;
108 }
109
110 /* Update bitmap */
111 bitmap->blocks[index] |= mask;
112
113 /* Update first gap counter */
114 while ( bitmap_test ( bitmap, bitmap->first_gap ) ) {
115 bitmap->first_gap++;
116 }
117
118 return 0;
119}
long index
Definition bigint.h:30
int bitmap_test(struct bitmap *bitmap, unsigned int bit)
Test bit in bitmap.
Definition bitmap.c:79
int bitmap_set(struct bitmap *bitmap, unsigned int bit)
Set bit in bitmap.
Definition bitmap.c:97
int bitmap_resize(struct bitmap *bitmap, unsigned int new_length)
Resize bitmap.
Definition bitmap.c:43
Bitmaps for multicast downloads.
#define BITMAP_MASK(bit)
Block mask within bitmap.
Definition bitmap.h:37
#define BITMAP_INDEX(bit)
Block index within bitmap.
Definition bitmap.h:29
#define BITMAP_BLKSIZE
Size of a block of bits (in bits).
Definition bitmap.h:21
unsigned long bitmap_block_t
A single block of bits within a bitmap.
Definition bitmap.h:18
Error codes.
#define DBGC(...)
Definition compiler.h:530
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:921
#define ENOMEM
Not enough space.
Definition errno.h:535
#define ERANGE
Result too large.
Definition errno.h:640
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:951
static unsigned int unsigned int bit
Definition bigint.h:390
void * realloc(void *old_ptr, size_t new_size)
Reallocate memory.
Definition malloc.c:607
A bitmap.
Definition bitmap.h:40
unsigned int first_gap
Index of first gap in the bitmap.
Definition bitmap.h:46
unsigned int length
Length of the bitmap, in bits.
Definition bitmap.h:44
bitmap_block_t * blocks
Bitmap data.
Definition bitmap.h:42