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.8KB

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