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

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