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

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