iPXE
bitmap.h File Reference

Bitmaps for multicast downloads. More...

#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>

Go to the source code of this file.

Data Structures

struct  bitmap
 A bitmap. More...

Macros

#define BITMAP_BLKSIZE   ( sizeof ( bitmap_block_t ) * 8 )
 Size of a block of bits (in bits).
#define BITMAP_INDEX(bit)
 Block index within bitmap.
#define BITMAP_MASK(bit)
 Block mask within bitmap.
#define BITMAP_BLOCKS(length)
 Number of blocks in the bitmap.

Typedefs

typedef unsigned long bitmap_block_t
 A single block of bits within a bitmap.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 FILE_SECBOOT (PERMITTED)
int bitmap_resize (struct bitmap *bitmap, unsigned int new_length)
 Resize bitmap.
int bitmap_test (struct bitmap *bitmap, unsigned int bit)
 Test bit in bitmap.
int bitmap_set (struct bitmap *bitmap, unsigned int bit)
 Set bit in bitmap.
static void bitmap_free (struct bitmap *bitmap)
 Free bitmap resources.
static unsigned int bitmap_first_gap (struct bitmap *bitmap)
 Get first gap within bitmap.
static int bitmap_full (struct bitmap *bitmap)
 Check to see if bitmap is full.

Detailed Description

Bitmaps for multicast downloads.

Definition in file bitmap.h.

Macro Definition Documentation

◆ BITMAP_BLKSIZE

#define BITMAP_BLKSIZE   ( sizeof ( bitmap_block_t ) * 8 )

Size of a block of bits (in bits).

Definition at line 21 of file bitmap.h.

◆ BITMAP_INDEX

#define BITMAP_INDEX ( bit)
Value:
#define BITMAP_BLKSIZE
Size of a block of bits (in bits).
Definition bitmap.h:21
static unsigned int unsigned int bit
Definition bigint.h:390

Block index within bitmap.

Parameters
bitBit index
Return values
indexBlock index

Definition at line 29 of file bitmap.h.

Referenced by bitmap_set(), and bitmap_test().

◆ BITMAP_MASK

#define BITMAP_MASK ( bit)
Value:
( 1UL << ( (bit) % BITMAP_BLKSIZE ) )

Block mask within bitmap.

Parameters
bitBit index
Return values
maskBlock mask

Definition at line 37 of file bitmap.h.

Referenced by bitmap_set(), and bitmap_test().

◆ BITMAP_BLOCKS

#define BITMAP_BLOCKS ( length)
Value:
( ( (length) / BITMAP_BLKSIZE ) + \
( !! ( (length) % BITMAP_BLKSIZE ) ) )
u16 length
Definition sky2.h:1

Number of blocks in the bitmap.

Parameters
lengthLength of the bitmap, in bits
Return values
blocksNumber of blocks

Definition at line 45 of file bitmap.h.

45#define BITMAP_BLOCKS( length ) ( ( (length) / BITMAP_BLKSIZE ) + \
46 ( !! ( (length) % BITMAP_BLKSIZE ) ) )

Referenced by bitmap_resize().

Typedef Documentation

◆ bitmap_block_t

typedef unsigned long bitmap_block_t

A single block of bits within a bitmap.

Definition at line 18 of file bitmap.h.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ bitmap_resize()

int bitmap_resize ( struct bitmap * bitmap,
unsigned int new_length )
extern

Resize bitmap.

Parameters
bitmapBitmap
new_lengthNew length of bitmap, in bits
Return values
rcReturn status code

Definition at line 43 of file bitmap.c.

43 {
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_BLOCKS ( bitmap->length );
50 new_num_blocks = BITMAP_BLOCKS ( new_length );
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}
#define BITMAP_BLOCKS(length)
Number of blocks in the bitmap.
Definition bitmap.h:45
unsigned long bitmap_block_t
A single block of bits within a bitmap.
Definition bitmap.h:18
#define DBGC(...)
Definition compiler.h:530
#define ENOMEM
Not enough space.
Definition errno.h:578
void * realloc(void *old_ptr, size_t new_size)
Reallocate memory.
Definition malloc.c:663
A bitmap.
Definition bitmap.h:49
unsigned int length
Length of the bitmap, in bits.
Definition bitmap.h:53
bitmap_block_t * blocks
Bitmap data.
Definition bitmap.h:51

References BITMAP_BLOCKS, bitmap::blocks, DBGC, ENOMEM, bitmap::length, and realloc().

Referenced by slam_open(), slam_pull_header(), and tftp_presize().

◆ bitmap_test()

int bitmap_test ( struct bitmap * bitmap,
unsigned int bit )
extern

Test bit in bitmap.

Parameters
bitmapBitmap
bitBit index
Return values
is_setBit is set

Definition at line 79 of file bitmap.c.

79 {
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}
long index
Definition bigint.h:30
#define BITMAP_MASK(bit)
Block mask within bitmap.
Definition bitmap.h:37
#define BITMAP_INDEX(bit)
Block index within bitmap.
Definition bitmap.h:29

References bit, BITMAP_INDEX, BITMAP_MASK, bitmap::blocks, index, and bitmap::length.

Referenced by bitmap_set(), slam_mc_socket_deliver(), and slam_tx_nack().

◆ bitmap_set()

int bitmap_set ( struct bitmap * bitmap,
unsigned int bit )
extern

Set bit in bitmap.

Parameters
bitmapBitmap
bitBit index
Return values
rcReturn status code

Definition at line 97 of file bitmap.c.

97 {
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}
int bitmap_test(struct bitmap *bitmap, unsigned int bit)
Test bit in bitmap.
Definition bitmap.c:79
#define ERANGE
Result too large.
Definition errno.h:683
unsigned int first_gap
Index of first gap in the bitmap.
Definition bitmap.h:55

References bit, BITMAP_INDEX, BITMAP_MASK, bitmap_test(), bitmap::blocks, DBGC, ERANGE, bitmap::first_gap, index, and bitmap::length.

Referenced by slam_mc_socket_deliver(), and tftp_rx_data().

◆ bitmap_free()

void bitmap_free ( struct bitmap * bitmap)
inlinestatic

Free bitmap resources.

Parameters
bitmapBitmap

Definition at line 67 of file bitmap.h.

67 {
68 free ( bitmap->blocks );
69}
static void(* free)(struct refcnt *refcnt))
Definition refcnt.h:55

References bitmap::blocks, and free.

Referenced by slam_free(), slam_pull_header(), tftp_free(), and tftp_timer_expired().

◆ bitmap_first_gap()

unsigned int bitmap_first_gap ( struct bitmap * bitmap)
inlinestatic

Get first gap within bitmap.

Parameters
bitmapBitmap
Return values
first_gapFirst gap

The first gap is the first unset bit within the bitmap.

Definition at line 79 of file bitmap.h.

79 {
80 return bitmap->first_gap;
81}

References bitmap::first_gap.

Referenced by slam_tx_nack(), tftp_rx_data(), and tftp_send_ack().

◆ bitmap_full()

int bitmap_full ( struct bitmap * bitmap)
inlinestatic

Check to see if bitmap is full.

Parameters
bitmapBitmap
Return values
is_fullBitmap is full

The bitmap is full if it has no gaps (i.e. no unset bits).

Definition at line 91 of file bitmap.h.

91 {
92 return ( bitmap->first_gap == bitmap->length );
93}

References bitmap::first_gap, and bitmap::length.

Referenced by slam_mc_socket_deliver(), and tftp_rx_data().