summaryrefslogtreecommitdiffstats
path: root/debian/grub-extras/disabled/gpxe/src/include/gpxe/bitmap.h
blob: d6911a51dd7af31577e4e0ad1b8bb4b68f7e2ed5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#ifndef _GPXE_BITMAP_H
#define _GPXE_BITMAP_H

/** @file
 *
 * Bitmaps for multicast downloads
 *
 */

FILE_LICENCE ( GPL2_OR_LATER );

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

/** A single block of bits within a bitmap */
typedef unsigned long bitmap_block_t;

/** Size of a block of bits (in bits) */
#define BITMAP_BLKSIZE ( sizeof ( bitmap_block_t ) * 8 )

/**
 * Block index within bitmap
 *
 * @v bit		Bit index
 * @ret index		Block index
 */
#define BITMAP_INDEX( bit ) ( (bit) / BITMAP_BLKSIZE )

/**
 * Block mask within bitmap
 *
 * @v bit		Bit index
 * @ret mask		Block mask
 */
#define BITMAP_MASK( bit ) ( 1 << ( (bit) % BITMAP_BLKSIZE ) )

/** A bitmap */
struct bitmap {
	/** Bitmap data */
	bitmap_block_t *blocks;
	/** Length of the bitmap, in bits */
	unsigned int length;
	/** Index of first gap in the bitmap */
	unsigned int first_gap;
};

extern int bitmap_resize ( struct bitmap *bitmap, unsigned int new_length );
extern int bitmap_test ( struct bitmap *bitmap, unsigned int bit );
extern void bitmap_set ( struct bitmap *bitmap, unsigned int bit );

/**
 * Free bitmap resources
 *
 * @v bitmap		Bitmap
 */
static inline void bitmap_free ( struct bitmap *bitmap ) {
	free ( bitmap->blocks );
}

/**
 * Get first gap within bitmap
 *
 * @v bitmap		Bitmap
 * @ret first_gap	First gap
 *
 * The first gap is the first unset bit within the bitmap.
 */
static inline unsigned int bitmap_first_gap ( struct bitmap *bitmap ) {
	return bitmap->first_gap;
}

/**
 * Check to see if bitmap is full
 *
 * @v bitmap		Bitmap
 * @ret is_full		Bitmap is full
 *
 * The bitmap is full if it has no gaps (i.e. no unset bits).
 */
static inline int bitmap_full ( struct bitmap *bitmap ) {
	return ( bitmap->first_gap == bitmap->length );
}

#endif /* _GPXE_BITMAP_H */