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.c 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <errno.h>
  19. #include <gpxe/bitmap.h>
  20. /** @file
  21. *
  22. * Bitmaps for multicast downloads
  23. *
  24. */
  25. /**
  26. * Resize bitmap
  27. *
  28. * @v bitmap Bitmap
  29. * @v new_length New length of bitmap, in bits
  30. * @ret rc Return status code
  31. */
  32. int bitmap_resize ( struct bitmap *bitmap, unsigned int new_length ) {
  33. unsigned int old_num_blocks;
  34. unsigned int new_num_blocks;
  35. size_t new_size;
  36. bitmap_block_t *new_blocks;
  37. old_num_blocks = BITMAP_INDEX ( bitmap->length + BITMAP_BLKSIZE - 1 );
  38. new_num_blocks = BITMAP_INDEX ( new_length + BITMAP_BLKSIZE - 1 );
  39. if ( old_num_blocks != new_num_blocks ) {
  40. new_size = ( new_num_blocks * sizeof ( bitmap->blocks[0] ) );
  41. new_blocks = realloc ( bitmap->blocks, new_size );
  42. if ( ! new_blocks ) {
  43. DBGC ( bitmap, "Bitmap %p could not resize to %d "
  44. "bits\n", bitmap, new_length );
  45. return -ENOMEM;
  46. }
  47. bitmap->blocks = new_blocks;
  48. }
  49. bitmap->length = new_length;
  50. while ( old_num_blocks < new_num_blocks ) {
  51. bitmap->blocks[old_num_blocks++] = 0;
  52. }
  53. DBGC ( bitmap, "Bitmap %p resized to %d bits\n", bitmap, new_length );
  54. return 0;
  55. }
  56. /**
  57. * Test bit in bitmap
  58. *
  59. * @v bitmap Bitmap
  60. * @v bit Bit index
  61. * @ret is_set Bit is set
  62. */
  63. int bitmap_test ( struct bitmap *bitmap, unsigned int bit ) {
  64. unsigned int index = BITMAP_INDEX ( bit );
  65. bitmap_block_t mask = BITMAP_MASK ( bit );
  66. if ( bit >= bitmap->length )
  67. return 0;
  68. return ( bitmap->blocks[index] & mask );
  69. }
  70. /**
  71. * Set bit in bitmap
  72. *
  73. * @v bitmap Bitmap
  74. * @v bit Bit index
  75. */
  76. void bitmap_set ( struct bitmap *bitmap, unsigned int bit ) {
  77. unsigned int index = BITMAP_INDEX ( bit );
  78. bitmap_block_t mask = BITMAP_MASK ( bit );
  79. DBGC ( bitmap, "Bitmap %p setting bit %d\n", bitmap, bit );
  80. /* Update bitmap */
  81. bitmap->blocks[index] |= mask;
  82. /* Update first gap counter */
  83. while ( bitmap_test ( bitmap, bitmap->first_gap ) ) {
  84. bitmap->first_gap++;
  85. }
  86. }