You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bitmap.h 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #ifndef _GPXE_BITMAP_H
  2. #define _GPXE_BITMAP_H
  3. /** @file
  4. *
  5. * Bitmaps for multicast downloads
  6. *
  7. */
  8. #include <stdint.h>
  9. #include <stddef.h>
  10. #include <stdlib.h>
  11. /** A single block of bits within a bitmap */
  12. typedef unsigned long bitmap_block_t;
  13. /** Size of a block of bits (in bits) */
  14. #define BITMAP_BLKSIZE ( sizeof ( bitmap_block_t ) * 8 )
  15. /**
  16. * Block index within bitmap
  17. *
  18. * @v bit Bit index
  19. * @ret index Block index
  20. */
  21. #define BITMAP_INDEX( bit ) ( (bit) / BITMAP_BLKSIZE )
  22. /**
  23. * Block mask within bitmap
  24. *
  25. * @v bit Bit index
  26. * @ret mask Block mask
  27. */
  28. #define BITMAP_MASK( bit ) ( 1 << ( (bit) % BITMAP_BLKSIZE ) )
  29. /** A bitmap */
  30. struct bitmap {
  31. /** Bitmap data */
  32. bitmap_block_t *blocks;
  33. /** Length of the bitmap, in bits */
  34. unsigned int length;
  35. /** Index of first gap in the bitmap */
  36. unsigned int first_gap;
  37. };
  38. extern int bitmap_resize ( struct bitmap *bitmap, unsigned int new_length );
  39. extern int bitmap_test ( struct bitmap *bitmap, unsigned int bit );
  40. extern void bitmap_set ( struct bitmap *bitmap, unsigned int bit );
  41. /**
  42. * Free bitmap resources
  43. *
  44. * @v bitmap Bitmap
  45. */
  46. static inline void bitmap_free ( struct bitmap *bitmap ) {
  47. free ( bitmap->blocks );
  48. }
  49. /**
  50. * Get first gap within bitmap
  51. *
  52. * @v bitmap Bitmap
  53. * @ret first_gap First gap
  54. *
  55. * The first gap is the first unset bit within the bitmap.
  56. */
  57. static inline unsigned int bitmap_first_gap ( struct bitmap *bitmap ) {
  58. return bitmap->first_gap;
  59. }
  60. /**
  61. * Check to see if bitmap is full
  62. *
  63. * @v bitmap Bitmap
  64. * @ret is_full Bitmap is full
  65. *
  66. * The bitmap is full if it has no gaps (i.e. no unset bits).
  67. */
  68. static inline int bitmap_full ( struct bitmap *bitmap ) {
  69. return ( bitmap->first_gap == bitmap->length );
  70. }
  71. #endif /* _GPXE_BITMAP_H */